def parse_packet(stream, msg_spec, side): """Parse a single packet out of stream, and return it.""" # read Packet ID msgtype = parse_unsigned_byte(stream) if not msg_spec[msgtype]: raise UnsupportedPacketException(msgtype) logger.debug("%s trying to parse message type %x" % (side, msgtype)) msg_parser = msg_spec[msgtype] msg = msg_parser.parse(stream) msg['raw_bytes'] = stream.packet_finished() return Message(msg)
def parse_packet(sock, msg_spec, expecting=None, cipher=None, backup_cipher=None, title=False): if expecting is not None: packet = msg_spec[expecting] print t.bold_cyan("\nExpecting %s (0x%02x) packet" % (packet.name, expecting)) if title and is_terminal: sys.stdout.write(t.move_down * 2) elif title: print if cipher is None: stream = LoggingSocketStream(sock) else: stream = LoggingSocketCipherStream(sock, cipher) with stream: msgtype = parse_unsigned_byte(stream) if expecting is not None and msgtype != expecting: if backup_cipher is None: raise UnexpectedPacketException(msgtype) else: raise UnexpectedPacketException( msgtype, ord(backup_cipher.encrypt(chr(expecting)))) if not msg_spec[msgtype]: raise UnsupportedPacketException(msgtype) msg_parser = msg_spec[msgtype] msg = msg_parser.parse(stream) if title: if is_terminal: sys.stdout.write(t.move_up * 3) sys.stdout.write( t.bold_cyan("Received %s (0x%02x) packet" % (msg_parser.name, msgtype))) if is_terminal: sys.stdout.write(t.move_down * 3) else: print PacketFormatter.print_packet(msg) if backup_cipher is not None: backup_cipher.encrypt(msg_parser.emit(msg)) return msg
def parse_packet(sock, msg_spec, expecting=None, cipher=None, backup_cipher=None, title=False): if expecting is not None: packet = msg_spec[expecting] print t.bold_cyan("\nExpecting %s (0x%02x) packet" % (packet.name, expecting)) if title and is_terminal: sys.stdout.write(t.move_down * 2) elif title: print if cipher is None: stream = LoggingSocketStream(sock) else: stream = LoggingSocketCipherStream(sock, cipher) with stream: msgtype = parse_unsigned_byte(stream) if expecting is not None and msgtype != expecting: if backup_cipher is None: raise UnexpectedPacketException(msgtype) else: raise UnexpectedPacketException( msgtype, ord(backup_cipher.encrypt(chr(expecting))) ) if not msg_spec[msgtype]: raise UnsupportedPacketException(msgtype) msg_parser = msg_spec[msgtype] msg = msg_parser.parse(stream) if title: if is_terminal: sys.stdout.write(t.move_up * 3) sys.stdout.write(t.bold_cyan("Received %s (0x%02x) packet" % (msg_parser.name, msgtype))) if is_terminal: sys.stdout.write(t.move_down * 3) else: print PacketFormatter.print_packet(msg) if backup_cipher is not None: backup_cipher.encrypt(msg_parser.emit(msg)) return msg