diff options
author | Dorian Pula <dorian.pula@amber-penguin-software.ca> | 2017-05-24 17:26:00 -0700 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2017-05-31 17:14:40 -0700 |
commit | fa96fe0609b335c2b97aa7ff8db0dc4fb6710ff1 (patch) | |
tree | f47678dde8dd6be7ef16391c0fee490488391050 /paramiko/packet.py | |
parent | 28218bf90b97451d3aba9d2c9ee01d87349a8886 (diff) | |
download | paramiko-fa96fe0609b335c2b97aa7ff8db0dc4fb6710ff1.tar.gz |
Even more flake8.
Diffstat (limited to 'paramiko/packet.py')
-rw-r--r-- | paramiko/packet.py | 94 |
1 files changed, 63 insertions, 31 deletions
diff --git a/paramiko/packet.py b/paramiko/packet.py index c943fe3c..cae64727 100644 --- a/paramiko/packet.py +++ b/paramiko/packet.py @@ -54,8 +54,11 @@ class Packetizer (object): REKEY_PACKETS = pow(2, 29) REKEY_BYTES = pow(2, 29) - REKEY_PACKETS_OVERFLOW_MAX = pow(2, 29) # Allow receiving this many packets after a re-key request before terminating - REKEY_BYTES_OVERFLOW_MAX = pow(2, 29) # Allow receiving this many bytes after a re-key request before terminating + # Allow receiving this many packets after a re-key request before + # terminating + REKEY_PACKETS_OVERFLOW_MAX = pow(2, 29) + # Allow receiving this many bytes after a re-key request before terminating + REKEY_BYTES_OVERFLOW_MAX = pow(2, 29) def __init__(self, socket): self.__socket = socket @@ -113,7 +116,8 @@ class Packetizer (object): """ self.__logger = log - def set_outbound_cipher(self, block_engine, block_size, mac_engine, mac_size, mac_key, sdctr=False): + def set_outbound_cipher(self, block_engine, block_size, mac_engine, + mac_size, mac_key, sdctr=False): """ Switch outbound data cipher. """ @@ -125,13 +129,15 @@ class Packetizer (object): self.__mac_key_out = mac_key self.__sent_bytes = 0 self.__sent_packets = 0 - # wait until the reset happens in both directions before clearing rekey flag + # wait until the reset happens in both directions before clearing + # rekey flag self.__init_count |= 1 if self.__init_count == 3: self.__init_count = 0 self.__need_rekey = False - def set_inbound_cipher(self, block_engine, block_size, mac_engine, mac_size, mac_key): + def set_inbound_cipher( + self, block_engine, block_size, mac_engine, mac_size, mac_key): """ Switch inbound data cipher. """ @@ -144,7 +150,8 @@ class Packetizer (object): self.__received_packets = 0 self.__received_bytes_overflow = 0 self.__received_packets_overflow = 0 - # wait until the reset happens in both directions before clearing rekey flag + # wait until the reset happens in both directions before clearing + # rekey flag self.__init_count |= 2 if self.__init_count == 3: self.__init_count = 0 @@ -262,9 +269,11 @@ class Packetizer (object): # on Linux, sometimes instead of socket.timeout, we get # EAGAIN. this is a bug in recent (> 2.6.9) kernels but # we need to work around it. - if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): + if (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EAGAIN): got_timeout = True - elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): + elif (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EINTR): # syscall interrupted; try again pass elif self.__closed: @@ -289,9 +298,11 @@ class Packetizer (object): except socket.timeout: retry_write = True except socket.error as e: - if (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EAGAIN): + if (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EAGAIN): retry_write = True - elif (type(e.args) is tuple) and (len(e.args) > 0) and (e.args[0] == errno.EINTR): + elif (type(e.args) is tuple) and (len(e.args) > 0) and \ + (e.args[0] == errno.EINTR): # syscall interrupted; try again retry_write = True else: @@ -307,11 +318,11 @@ class Packetizer (object): n = -1 else: if n == 0 and iteration_with_zero_as_return_value > 10: - # We shouldn't retry the write, but we didn't - # manage to send anything over the socket. This might be an - # indication that we have lost contact with the remote side, - # but are yet to receive an EOFError or other socket errors. - # Let's give it some iteration to try and catch up. + # We shouldn't retry the write, but we didn't + # manage to send anything over the socket. This might be an + # indication that we have lost contact with the remote + # side, but are yet to receive an EOFError or other socket + # errors. Let's give it some iteration to try and catch up. n = -1 iteration_with_zero_as_return_value += 1 if n < 0: @@ -327,7 +338,7 @@ class Packetizer (object): line, so it's okay to attempt large reads. """ buf = self.__remainder - while not linefeed_byte in buf: + while linefeed_byte not in buf: buf += self._read_timeout(timeout) n = buf.index(linefeed_byte) self.__remainder = buf[n + 1:] @@ -354,7 +365,9 @@ class Packetizer (object): data = self.__compress_engine_out(data) packet = self._build_packet(data) if self.__dump_packets: - self._log(DEBUG, 'Write packet <%s>, length %d' % (cmd_name, orig_len)) + self._log( + DEBUG, + 'Write packet <%s>, length %d' % (cmd_name, orig_len)) self._log(DEBUG, util.format_binary(packet, 'OUT: ')) if self.__block_engine_out is not None: out = self.__block_engine_out.update(packet) @@ -362,14 +375,20 @@ class Packetizer (object): out = packet # + mac if self.__block_engine_out is not None: - payload = struct.pack('>I', self.__sequence_number_out) + packet - out += compute_hmac(self.__mac_key_out, payload, self.__mac_engine_out)[:self.__mac_size_out] - self.__sequence_number_out = (self.__sequence_number_out + 1) & xffffffff + payload = struct.pack( + '>I', self.__sequence_number_out) + packet + out += compute_hmac( + self.__mac_key_out, + payload, + self.__mac_engine_out)[:self.__mac_size_out] + self.__sequence_number_out = \ + (self.__sequence_number_out + 1) & xffffffff self.write_all(out) self.__sent_bytes += len(out) self.__sent_packets += 1 - if (self.__sent_packets >= self.REKEY_PACKETS or self.__sent_bytes >= self.REKEY_BYTES)\ + if (self.__sent_packets >= self.REKEY_PACKETS or + self.__sent_bytes >= self.REKEY_BYTES)\ and not self.__need_rekey: # only ask once for rekeying self._log(DEBUG, 'Rekeying (hit %d packets, %d bytes sent)' % @@ -410,15 +429,21 @@ class Packetizer (object): if self.__mac_size_in > 0: mac = post_packet[:self.__mac_size_in] - mac_payload = struct.pack('>II', self.__sequence_number_in, packet_size) + packet - my_mac = compute_hmac(self.__mac_key_in, mac_payload, self.__mac_engine_in)[:self.__mac_size_in] + mac_payload = struct.pack( + '>II', self.__sequence_number_in, packet_size) + packet + my_mac = compute_hmac( + self.__mac_key_in, + mac_payload, + self.__mac_engine_in)[:self.__mac_size_in] if not util.constant_time_bytes_eq(my_mac, mac): raise SSHException('Mismatched MAC') padding = byte_ord(packet[0]) payload = packet[1:packet_size - padding] if self.__dump_packets: - self._log(DEBUG, 'Got payload (%d bytes, %d padding)' % (packet_size, padding)) + self._log( + DEBUG, + 'Got payload (%d bytes, %d padding)' % (packet_size, padding)) if self.__compress_engine_in is not None: payload = self.__compress_engine_in(payload) @@ -436,9 +461,12 @@ class Packetizer (object): # dropping the connection self.__received_bytes_overflow += raw_packet_size self.__received_packets_overflow += 1 - if (self.__received_packets_overflow >= self.REKEY_PACKETS_OVERFLOW_MAX) or \ - (self.__received_bytes_overflow >= self.REKEY_BYTES_OVERFLOW_MAX): - raise SSHException('Remote transport is ignoring rekey requests') + if (self.__received_packets_overflow >= + self.REKEY_PACKETS_OVERFLOW_MAX) or \ + (self.__received_bytes_overflow >= + self.REKEY_BYTES_OVERFLOW_MAX): + raise SSHException( + 'Remote transport is ignoring rekey requests') elif (self.__received_packets >= self.REKEY_PACKETS) or \ (self.__received_bytes >= self.REKEY_BYTES): # only ask once for rekeying @@ -454,10 +482,12 @@ class Packetizer (object): else: cmd_name = '$%x' % cmd if self.__dump_packets: - self._log(DEBUG, 'Read packet <%s>, length %d' % (cmd_name, len(payload))) + self._log( + DEBUG, + 'Read packet <%s>, length %d' % (cmd_name, len(payload))) return cmd, msg - ########## protected + # ...protected... def _log(self, level, msg): if self.__logger is None: @@ -469,7 +499,8 @@ class Packetizer (object): self.__logger.log(level, msg) def _check_keepalive(self): - if (not self.__keepalive_interval) or (not self.__block_engine_out) or \ + if (not self.__keepalive_interval) or \ + (not self.__block_engine_out) or \ self.__need_rekey: # wait till we're encrypting, and not in the middle of rekeying return @@ -508,7 +539,8 @@ class Packetizer (object): packet = struct.pack('>IB', len(payload) + padding + 1, padding) packet += payload if self.__sdctr_out or self.__block_engine_out is None: - # cute trick i caught openssh doing: if we're not encrypting or SDCTR mode (RFC4344), + # cute trick i caught openssh doing: if we're not encrypting or + # SDCTR mode (RFC4344), # don't waste random bytes for the padding packet += (zero_byte * padding) else: |