Conditions | 19 |
Total Lines | 77 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like exabgp.reactor.network.tcp.MD5() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # encoding: utf-8 |
||
108 | def MD5(io, ip, port, md5, md5_base64): |
||
109 | platform_os = platform.system() |
||
110 | if platform_os == 'FreeBSD': |
||
111 | if md5: |
||
112 | if md5 != 'kernel': |
||
113 | raise MD5Error( |
||
114 | 'FreeBSD requires that you set your MD5 key via ipsec.conf.\n' |
||
115 | 'Something like:\n' |
||
116 | 'flush;\n' |
||
117 | 'add <local ip> <peer ip> tcp 0x1000 -A tcp-md5 "password";' |
||
118 | ) |
||
119 | try: |
||
120 | TCP_MD5SIG = 0x10 |
||
121 | io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, 1) |
||
122 | except socket.error: |
||
123 | raise MD5Error( |
||
124 | 'FreeBSD requires that you rebuild your kernel to enable TCP MD5 Signatures:\n' |
||
125 | 'options IPSEC\n' |
||
126 | 'options TCP_SIGNATURE\n' |
||
127 | 'device crypto\n' |
||
128 | ) |
||
129 | elif platform_os == 'Linux': |
||
130 | try: |
||
131 | md5_bytes = None |
||
132 | if md5: |
||
133 | if md5_base64 is True: |
||
134 | try: |
||
135 | md5_bytes = base64.b64decode(md5) |
||
136 | except TypeError: |
||
137 | raise MD5Error("Failed to decode base 64 encoded PSK") |
||
138 | elif md5_base64 is None and not re.match('.*[^a-f0-9].*', md5): # auto |
||
139 | options = [md5 + '==', md5 + '=', md5] |
||
140 | for md5 in options: |
||
141 | try: |
||
142 | md5_bytes = base64.b64decode(md5) |
||
143 | break |
||
144 | except TypeError: |
||
145 | pass |
||
146 | |||
147 | # __kernel_sockaddr_storage |
||
148 | n_af = IP.toaf(ip) |
||
149 | n_addr = IP.pton(ip) |
||
150 | n_port = socket.htons(port) |
||
151 | |||
152 | # pack 'x' is padding, so we want the struct |
||
153 | # Do not use '!' for the pack, the network (big) endian switch in |
||
154 | # struct.pack is fighting against inet_pton and htons (note the n) |
||
155 | |||
156 | if IP.toafi(ip) == AFI.ipv4: |
||
157 | # SS_MAXSIZE is 128 but addr_family, port and ipaddr (8 bytes total) are written independently of the padding |
||
158 | SS_MAXSIZE_PADDING = 128 - calcsize('HH4s') # 8 |
||
159 | sockaddr = pack('HH4s%dx' % SS_MAXSIZE_PADDING, socket.AF_INET, n_port, n_addr) |
||
160 | else: |
||
161 | SS_MAXSIZE_PADDING = 128 - calcsize('HI16sI') # 28 |
||
162 | SIN6_FLOWINFO = 0 |
||
163 | SIN6_SCOPE_ID = 0 |
||
164 | sockaddr = pack('HHI16sI%dx' % SS_MAXSIZE_PADDING, n_af, n_port, SIN6_FLOWINFO, n_addr, SIN6_SCOPE_ID) |
||
165 | |||
166 | TCP_MD5SIG_MAXKEYLEN = 80 |
||
167 | TCP_MD5SIG = 14 |
||
168 | |||
169 | if md5_bytes: |
||
170 | key = pack('2xH4x%ds' % TCP_MD5SIG_MAXKEYLEN, len(md5_bytes), md5_bytes) |
||
171 | io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, sockaddr + key) |
||
172 | elif md5: |
||
173 | md5_bytes = bytes(md5, 'ascii') |
||
174 | key = pack('2xH4x%ds' % TCP_MD5SIG_MAXKEYLEN, len(md5_bytes), md5_bytes) |
||
175 | io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, sockaddr + key) |
||
176 | # else: |
||
177 | # key = pack('2xH4x%ds' % TCP_MD5SIG_MAXKEYLEN, 0, b'') |
||
178 | # io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, sockaddr + key) |
||
179 | |||
180 | except socket.error as exc: |
||
181 | if exc.errno != errno.ENOENT: |
||
182 | raise MD5Error('This linux machine does not support TCP_MD5SIG, you can not use MD5 (%s)' % errstr(exc)) |
||
183 | elif md5: |
||
184 | raise MD5Error('ExaBGP has no MD5 support for %s' % platform_os) |
||
185 | |||
267 |