Exemplo n.º 1
0
    def DecodeFromData(self, stream):
        '''
        Decodes a packet from a data stream.
        
        @type stream: Stream (file-like) object
        @param stream: Stream from which to decode the packet.
        
        @raise IncompletePacket: Raised if not enough data is present.
        @raise CorruptPacket: Raised if the data is invalid.
        '''
        # Read in the header.
        try:
            ph_type = BinaryStructs.DeserializeUint16(stream)
            ph_flags = BinaryStructs.DeserializeUint16(stream)
        except BinaryStructs.EndOfFile:
            # Packet seems incomplete
            raise IncompletePacket

        # Validate the packet header.
        if ph_type > MAX_VALID_PACKET:
            # Unknown type; corrupt packet.
            raise CorruptPacket("Invalid packet type in header.")

        # Now try reading the body.
        compressed = bool(ph_flags & HeaderFlag_zlib)
        self._GetBodyFromBinary(stream, compressed)
Exemplo n.º 2
0
    def DeserializeBody(self, stream):
        # check the protocol signature
        signature = stream.read(len(ProtocolSignature))
        if len(signature) != len(ProtocolSignature):
            raise IncompletePacket
        self.Signature = signature

        # check the version
        try:
            self.Revision = BinaryStructs.DeserializeUint16(stream)
            self.MajorVersion = BinaryStructs.DeserializeUint16(stream)
            self.MinorVersion = BinaryStructs.DeserializeUint16(stream)
        except BinaryStructs.EndOfFile:
            raise IncompletePacket
        except:
            msg = "Unhandled exception while deserializing packet type 0.\n\n"
            msg += traceback.format_exc()
            logging.error(msg)
            raise CorruptPacket
Exemplo n.º 3
0
 def DeserializeBody(self, stream):
     # Read values.
     try:
         self.RequestSerial = BinaryStructs.DeserializeUint32(stream)
         self.ReasonCode = BinaryStructs.DeserializeUint16(stream)
     except BinaryStructs.EndOfFile:
         raise IncompletePacket
     except:
         msg = "Unhandled exception in DeserializeBody, packet type 4.\n\n"
         msg += traceback.format_exc()
         logging.error(msg)
         raise CorruptPacket
Exemplo n.º 4
0
def BuildPacketFromStream(stream, connection):
    '''
    Attempts to build a packet from a data stream.
    
    @type stream: file-like object
    @param stream: Data stream to build packet from.
    
    @type connection: Networking.BaseConnectionHandler
    @param connection: Connection with which to associate the packet.
    
    @raise IncompletePacket: Raised if not enough data is present.
    @raise CorruptPacket: Raised if something is wrong with the data.
    
    @return: A Packet object, or an object of a Packet subclass.
    '''
    # try peeking ahead at the packet type
    startpos = stream.tell()
    try:
        type = BinaryStructs.DeserializeUint16(stream)
        stream.seek(startpos)
    except:
        # not enough data, make sure we rewind
        stream.seek(startpos)
        raise IncompletePacket

    # check the type
    if type < 0 or type > MAX_VALID_PACKET:
        # packet type is out of bounds
        raise CorruptPacket

    # now build up a packet of the appropriate type
    try:
        PacketProto = PacketTypes[type]
    except KeyError:
        # unrecognized packet type
        raise CorruptPacket
    NewPacket = PacketProto(connection)

    # and now fill in the packet with the data...
    NewPacket.DecodeFromData(stream)
    return NewPacket
Exemplo n.º 5
0
 def DeserializeBody(self, stream):
     # Read data.
     try:
         self.Reason = BinaryStructs.DeserializeUint16(stream)
     except BinaryStructs.EndOfFile:
         raise IncompletePacket