def __init__(self, code, subcode, data=b'', parse_data=True): self.code = code self.subcode = subcode if not parse_data: self.data = data return if not (code, subcode) in [(6, 2), (6, 4)]: self.data = data if not len( [_ for _ in str(data) if _ not in string.printable]) else hexbytes(data) return if len(data) == 0: # shutdown without shutdown communication (the old fashioned way) self.data = b'' return # draft-ietf-idr-shutdown or the peer was using 6,2 with data shutdown_length = ordinal(data[0]) data = data[1:] if shutdown_length == 0: self.data = b"empty Shutdown Communication." # move offset past length field return if len(data) < shutdown_length: self.data = b"invalid Shutdown Communication (buffer underrun) length : %i [%s]" % ( shutdown_length, hexstring(data)) return if shutdown_length > 128: self.data = b"invalid Shutdown Communication (too large) length : %i [%s]" % ( shutdown_length, hexstring(data)) return try: self.data = b'Shutdown Communication: "%s"' % \ data[:shutdown_length].decode('utf-8').replace('\r',' ').replace('\n',' ') except UnicodeDecodeError: self.data = b"invalid Shutdown Communication (invalid UTF-8) length : %i [%s]" % ( shutdown_length, hexstring(data)) return trailer = data[shutdown_length:] if trailer: self.data += b", trailing data: " + hexstring(trailer)
def __init__ (self, code, subcode, data=b'', parse_data=True): self.code = code self.subcode = subcode if not parse_data: self.data = data return if not (code, subcode) in [(6, 2), (6, 4)]: self.data = data if not len([_ for _ in str(data) if _ not in string.printable]) else hexbytes(data) return if len(data) == 0: # shutdown without shutdown communication (the old fashioned way) self.data = b'' return # draft-ietf-idr-shutdown or the peer was using 6,2 with data shutdown_length = ordinal(data[0]) data = data[1:] if shutdown_length == 0: self.data = b"empty Shutdown Communication." # move offset past length field return if len(data) < shutdown_length: self.data = b"invalid Shutdown Communication (buffer underrun) length : %i [%s]" % (shutdown_length, hexstring(data)) return if shutdown_length > 128: self.data = b"invalid Shutdown Communication (too large) length : %i [%s]" % (shutdown_length, hexstring(data)) return try: self.data = b'Shutdown Communication: "%s"' % \ data[:shutdown_length].decode('utf-8').replace('\r',' ').replace('\n',' ') except UnicodeDecodeError: self.data = b"invalid Shutdown Communication (invalid UTF-8) length : %i [%s]" % (shutdown_length, hexstring(data)) return trailer = data[shutdown_length:] if trailer: self.data += b", trailing data: " + hexstring(trailer)