Beispiel #1
0
    def _processIncomingPacket(self, packet):
        '''
        Takes a raw packet, checks it and returns the correct packet class
        '''
        # Check for special bytes n packet that shouldn't be there
        if protocol.START_BYTE in packet or protocol.END_BYTE in packet:
            raise ParsingException, 'Unescaped bytes found in packet: %s' % protocols.toHex(packet)

        # Process escapes
        processed = 0
        while protocol.ESCAPE_BYTE in packet[processed:]:
            i = packet.index(protocol.ESCAPE_BYTE, processed)
            processed = i+1

            # Convert escaped byte to an integer to XOR, and then convert back to a string
            unescaped = chr(ord(packet[i+1]) ^ 0xFF)

            if unescaped in protocol.SPECIAL_BYTES:
                packet = packet[:i] + unescaped + packet[i+2:]
            else:
                raise ParsingException, 'Wrongly escaped byte found in packet (%s): %s' % (hex(ord(unescaped)), protocols.toHex(packet))

        if not len(packet):
            raise IgnorableParsingException, 'Packet is empty'

        if len(packet) < 4:
            raise ParsingException, 'Packet does not contain required information. Packet content: %s' % protocols.toHex(packet)

        # Split up packet
        contents = {
                'flags':        packet[0],
                'payload_id':   protocols.shortFrom8bit(packet[1:3]),
                'payload':      packet[3:-1],
                'checksum':     packet[-1]
        }

        # Check checksum
        gen_checksum = packetlib.getChecksum(packet[:-1])

        if contents['checksum'] != gen_checksum:
            raise ParsingException, 'Checksum is incorrect! Provided: %d, generated: %d, packet: [%s]' % (ord(contents['checksum']), ord(gen_checksum), protocols.toHex(packet))

        # Create response packet object
        response = responses.getPacket(contents['payload_id'])

        # Populate data
        response.parseHeaderFlags(contents['flags'])
        response.setPayloadId(contents['payload_id'])
        response.parsePayload(contents['payload'])
        response.validate()

        return response
Beispiel #2
0
    def _processIncomingPacket(self, packet):
        '''
        Takes a raw packet, checks it and returns the correct packet class
        '''
        contents = {}
        contents['flags'] = None
        contents['payload_id'] = None
        contents['payload'] = []
        contents['checksum'] = None

        index = 1

        # Grab flags
        contents['flags'] = flags = packet[index]
        index += 1

        # Grab payload id
        contents['payload_id'] = payload_id = protocols.from8bit(packet[index:index+2])
        index += 2

        # Grab payload
        contents['payload'] = payload = packet[index:(len(packet) - 2)]
        index += len(payload)

        # Grab checksum
        contents['checksum'] = checksum = packet[index]
        index += 1

        # Check checksum
        gen_checksum = packetlib.getChecksum(packet[1:index-1])

        if checksum != gen_checksum:
            raise ParsingException, 'Checksum is incorrect! Provided: %d, generated: %d' % (checksum, gen_checksum)

        # Just double check we only have one byte left
        if index != (len(packet) - 1):
            raise ParsingException, 'Packet incorrectly processed, %d bytes left' % (len(packet) - 1 - index)

        # Create response packet object
        response = responses.getPacket(payload_id)

        # Populate data
        response.parseHeaderFlags(contents['flags'])
        response.setPayloadId(contents['payload_id'])
        response.parsePayload(contents['payload'])
        response.validate()

        return response