| 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 |
||
| 106 | def MD5 (io, ip, port, md5, md5_base64): |
||
| 107 | platform_os = platform.system() |
||
| 108 | if platform_os == 'FreeBSD': |
||
| 109 | if md5: |
||
| 110 | if md5 != 'kernel': |
||
| 111 | raise MD5Error( |
||
| 112 | 'FreeBSD requires that you set your MD5 key via ipsec.conf.\n' |
||
| 113 | 'Something like:\n' |
||
| 114 | 'flush;\n' |
||
| 115 | 'add <local ip> <peer ip> tcp 0x1000 -A tcp-md5 "password";' |
||
| 116 | ) |
||
| 117 | try: |
||
| 118 | TCP_MD5SIG = 0x10 |
||
| 119 | io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, 1) |
||
| 120 | except socket.error: |
||
| 121 | raise MD5Error( |
||
| 122 | 'FreeBSD requires that you rebuild your kernel to enable TCP MD5 Signatures:\n' |
||
| 123 | 'options IPSEC\n' |
||
| 124 | 'options TCP_SIGNATURE\n' |
||
| 125 | 'device crypto\n' |
||
| 126 | ) |
||
| 127 | elif platform_os == 'Linux': |
||
| 128 | try: |
||
| 129 | md5_bytes = None |
||
| 130 | if md5: |
||
| 131 | if md5_base64 is True: |
||
| 132 | try: |
||
| 133 | md5_bytes = base64.b64decode(md5) |
||
| 134 | except TypeError: |
||
| 135 | raise MD5Error("Failed to decode base 64 encoded PSK") |
||
| 136 | elif md5_base64 is None and not re.match('.*[^a-f0-9].*', md5): # auto |
||
| 137 | options = [md5+'==', md5+'=', md5] |
||
| 138 | for md5 in options: |
||
| 139 | try: |
||
| 140 | md5_bytes = base64.b64decode(md5) |
||
| 141 | break |
||
| 142 | except TypeError: |
||
| 143 | pass |
||
| 144 | |||
| 145 | # __kernel_sockaddr_storage |
||
| 146 | n_af = IP.toaf(ip) |
||
| 147 | n_addr = IP.pton(ip) |
||
| 148 | n_port = socket.htons(port) |
||
| 149 | |||
| 150 | # pack 'x' is padding, so we want the struct |
||
| 151 | # Do not use '!' for the pack, the network (big) endian switch in |
||
| 152 | # struct.pack is fighting against inet_pton and htons (note the n) |
||
| 153 | |||
| 154 | if IP.toafi(ip) == AFI.ipv4: |
||
| 155 | # SS_MAXSIZE is 128 but addr_family, port and ipaddr (8 bytes total) are written independently of the padding |
||
| 156 | SS_MAXSIZE_PADDING = 128 - calcsize('HH4s') # 8 |
||
| 157 | sockaddr = pack('HH4s%dx' % SS_MAXSIZE_PADDING, socket.AF_INET, n_port, n_addr) |
||
| 158 | else: |
||
| 159 | SS_MAXSIZE_PADDING = 128 - calcsize('HI16sI') # 28 |
||
| 160 | SIN6_FLOWINFO = 0 |
||
| 161 | SIN6_SCOPE_ID = 0 |
||
| 162 | sockaddr = pack('HHI16sI%dx' % SS_MAXSIZE_PADDING, n_af, n_port, SIN6_FLOWINFO, n_addr, SIN6_SCOPE_ID) |
||
| 163 | |||
| 164 | TCP_MD5SIG_MAXKEYLEN = 80 |
||
| 165 | TCP_MD5SIG = 14 |
||
| 166 | |||
| 167 | if md5_bytes: |
||
| 168 | key = pack('2xH4x%ds' % TCP_MD5SIG_MAXKEYLEN, len(md5_bytes), md5_bytes) |
||
| 169 | io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, sockaddr + key) |
||
| 170 | elif md5: |
||
| 171 | md5_bytes = bytes_ascii(md5) |
||
| 172 | key = pack('2xH4x%ds' % TCP_MD5SIG_MAXKEYLEN, len(md5_bytes), md5_bytes) |
||
| 173 | io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, sockaddr + key) |
||
| 174 | # else: |
||
| 175 | # key = pack('2xH4x%ds' % TCP_MD5SIG_MAXKEYLEN, 0, b'') |
||
| 176 | # io.setsockopt(socket.IPPROTO_TCP, TCP_MD5SIG, sockaddr + key) |
||
| 177 | |||
| 178 | except socket.error as exc: |
||
| 179 | if exc.errno != errno.ENOENT: |
||
| 180 | raise MD5Error('This linux machine does not support TCP_MD5SIG, you can not use MD5 (%s)' % errstr(exc)) |
||
| 181 | elif md5: |
||
| 182 | raise MD5Error('ExaBGP has no MD5 support for %s' % platform_os) |
||
| 183 | |||
| 264 | return |