Exemplo n.º 1
0
 def __serialize__(self):
     encoder = self.ENCODER()
     writeStream = io.BytesIO()
     fieldWrapper = ComplexFieldType(PacketType)
     fieldWrapper.setData(self)
     encoder.encode(writeStream, fieldWrapper)
     return writeStream.getvalue()
Exemplo n.º 2
0
class VNICSocketOpenPacket(PacketType):
    DEFINITION_IDENTIFIER = "vsockets.VNICSocketOpenPacket"
    DEFINITION_VERSION    = "1.0"
    
    class SocketConnectData(PacketFields):
        FIELDS = [
            ("destination", STRING),
            ("destinationPort", UINT16)
        ]
        
    class SocketListenData(PacketFields):
        FIELDS = [
            ("sourcePort", UINT16)
        ]
    
    FIELDS = [
        ("callbackAddress", STRING),
        ("callbackPort", UINT16),
        
        ("connectData", ComplexFieldType(SocketConnectData, {Optional:True})),
        ("listenData", ComplexFieldType(SocketListenData, {Optional:True}))
    ]
    
    def isConnectType(self):
        return self.connectData != FIELD_NOT_SET and self.listenData == FIELD_NOT_SET
        
    def isListenType(self):
        return self.connectData == FIELD_NOT_SET and self.listenData != FIELD_NOT_SET
Exemplo n.º 3
0
 def __serialize__(self):
     encoder = self.ENCODER()
     writeStream = io.BytesIO()
     fieldWrapper = ComplexFieldType(PacketType)
     fieldWrapper.setData(self)
     encoder.encode(writeStream, fieldWrapper)
     return writeStream.getvalue()
Exemplo n.º 4
0
    def Deserialize(cls, buffer):
        encoder = cls.ENCODER()

        # The encoders work on field types. The packet itself, isn't one.
        # We create a ComplexFieldType(NamedPacketType) and pass it to decoder.
        # The type's data will be set to the decoded stream.
        fieldWrapper = ComplexFieldType(cls)
        encoder.decode(io.BytesIO(buffer), fieldWrapper)
        return fieldWrapper.data()
Exemplo n.º 5
0
 def Deserialize(cls, buffer):
     encoder = cls.ENCODER()
     
     # The encoders work on field types. The packet itself, isn't one.
     # We create a ComplexFieldType(NamedPacketType) and pass it to decoder.
     # The type's data will be set to the decoded stream.
     fieldWrapper = ComplexFieldType(cls)
     encoder.decode(io.BytesIO(buffer), fieldWrapper)
     return fieldWrapper.data()
Exemplo n.º 6
0
    class TestPacket1(PacketType):
        DEFINITION_IDENTIFIER = "packettype.basicunittest.TestPacket1"
        DEFINITION_VERSION = "1.0"

        class SubFields(PacketFields):
            FIELDS = [("subfield1", Uint({Bits: 16})),
                      ("subfield2", Uint({Bits: 16}))]

        FIELDS = [("header", ComplexFieldType(SubFields)),
                  ("field1", Uint({MaxValue: 1000})),
                  ("field2", StringFieldType),
                  ("trailer", ComplexFieldType(SubFields))]
Exemplo n.º 7
0
 def DeserializeStream(cls, stream):
     encoder = cls.ENCODER()
     
     # The encoders work on field types. The packet itself, isn't one.
     # We create a ComplexFieldType(PacketType) and pass it to decoder.
     # The type's data will be set to the decoded stream.
     fieldWrapper = ComplexFieldType(cls)
     yield from encoder.decodeIterator(stream, fieldWrapper)
     packet = fieldWrapper.data()
     if not isinstance(packet, cls):
         raise Exception("Deserialized packet of class {} but expected class {}.".format(packet.__class__, cls))
     return packet
Exemplo n.º 8
0
    def DeserializeStream(cls, stream):
        encoder = cls.ENCODER()

        # The encoders work on field types. The packet itself, isn't one.
        # We create a ComplexFieldType(PacketType) and pass it to decoder.
        # The type's data will be set to the decoded stream.
        fieldWrapper = ComplexFieldType(cls)
        yield from encoder.decodeIterator(stream, fieldWrapper)
        packet = fieldWrapper.data()
        if not isinstance(packet, cls):
            raise Exception(
                "Deserialized packet of class {} but expected class {}.".
                format(packet.__class__, cls))
        return packet
Exemplo n.º 9
0
	class TestSubPacket(PacketType):
		DEFINITION_IDENTIFIER = "lab1b.student_qxf.testSubPacket"
		DEFINITION_VERSION = "1.0"			

		class SubFields(PacketFields):
			FIELDS = [
				("subfield1",UINT32),
				("subfield2",UINT32)
			]

		FIELDS = [
			("testWords",STRING),
			("sub1",ComplexFieldType(SubFields)),
			("sub2",ComplexFieldType(SubFields))
		]
Exemplo n.º 10
0
 class TestPacket1(PacketType):
     DEFINITION_IDENTIFIER = "packettype.basicunittest.TestPacket1"
     DEFINITION_VERSION    = "1.0"
     
     class SubFields(PacketFields):
         FIELDS = [("subfield1",Uint({Bits:16})), ("subfield2",Uint({Bits:16}))]
     
     FIELDS = [  
                 ("ls", ListFieldType(ComplexFieldType(SubFields)))
             ]
Exemplo n.º 11
0
class SendMenu(PacketType):

    DEFINITION_IDENTIFIER = "lab1b.Ruofan.SendMenu"
    DEFINITION_VERSION = "1.0"

    class MenuContent(PacketFields):
        FIELDS = [("Appetizers", ListFieldType(STRING)),
                  ("Sandwiches", ListFieldType(STRING)),
                  ("Salads_and_Soups", ListFieldType(STRING)),
                  ("Desert", ListFieldType(STRING))]

    FIELDS = [("ID", UINT16), ("name", STRING), ("tableNumber", UINT16),
              ("menuContent", ComplexFieldType(MenuContent))]
class WirePacket(PacketType):
    DEFINITION_IDENTIFIER = "switching.WirePacket"
    DEFINITION_VERSION = "1.0"

    class FragmentData(PacketFields):
        FIELDS = [("fragId", UINT32), ("totalSize", UINT64),
                  ("offset", UINT64)]

    FIELDS = [("source", STRING), ("sourcePort", UINT16),
              ("destination", STRING), ("destinationPort", UINT16),
              ("fragData", ComplexFieldType(FragmentData, {Optional: True})),
              ("data", BUFFER)]

    def isFragment(self):
        return self.fragData != FIELD_NOT_SET
Exemplo n.º 13
0
def basicUnitTest():
    import io
    
    uint1, uint2 = UINT(), UINT()
    int1, int2 = INT(), INT()
    bool1, bool2 = BOOL(), BOOL()
    stream = io.BytesIO()
    encoder = PlaygroundStandardPacketEncoder()
    
    uint1.setData(10)
    encoder.encode(stream, uint1)
    stream.seek(0)
    encoder.decode(stream, uint2)
    assert uint2.data() == uint1.data()
    
    stream = io.BytesIO()
    int1.setData(-10)
    encoder.encode(stream, int1)
    stream.seek(0)
    encoder.decode(stream, int2)
    assert int1.data() == int2.data()
    
    stream = io.BytesIO()
    bool1.setData(False)
    encoder.encode(stream, bool1)
    stream.seek(0)
    encoder.decode(stream, bool2)
    assert bool1.data() == bool2.data()
    
    listfield1 = ListFieldType(UINT)
    listfield2 = ListFieldType(UINT)
    listfield1.append(10)
    listfield1.append(100)
    listfield1.append(1000)
    
    stream = io.BytesIO()
    encoder.encode(stream, listfield1)
    stream.seek(0)
    encoder.decode(stream, listfield2)
    
    assert len(listfield1) == len(listfield2)
    for i in range(len(listfield1)):
        assert listfield1[i] == listfield2[i]
    
    str1 = StringFieldType()
    str2 = StringFieldType()
    str1.setData("Test1 string")
    
    stream = io.BytesIO()
    encoder.encode(stream, str1)
    stream.seek(0)
    encoder.decode(stream, str2)
    
    assert str1.data() == str2.data()
    
    class SomeFields(PacketFields):
        FIELDS = [  ("field1", UINT({Bits:32})),
                    ("field2", UINT({Bits:32})),
                    ("list1",  ListFieldType(UINT({Bits:8})))
                    ]
    
    fields1Field = ComplexFieldType(SomeFields)
    fields2Field = ComplexFieldType(SomeFields)
    
    fields1 = SomeFields()
    fields1.field1 = 50
    fields1.field2 = 500
    fields1.list1 = []
    fields1.list1.append(0)
    fields1.list1.append(255)
    
    fields1Field.setData(fields1)
    fields2Field.setData(SomeFields())
    
    stream = io.BytesIO()
    encoder.encode(stream, fields1Field)
    stream.seek(0)
    encoder.decode(stream, fields2Field)
    
    fields2 = fields2Field.data()
    
    assert fields1.field1 == fields2.field1
    assert fields1.field2 == fields2.field2
    assert len(fields1.list1) == len(fields2.list1)
    assert fields1.list1[0] == fields2.list1[0]
    assert fields1.list1[-1] == fields2.list1[-1]
Exemplo n.º 14
0
        packetFields = complexType.data()
        fieldToTag = self._processFields(packetFields.FIELDS)
        fieldCount = yield from stream.unpackIterator(self.FIELD_COUNT_PACK_CODE)
        
        for i in range(fieldCount):
            fieldID = yield from stream.unpackIterator(self.FIELD_TAG_PACK_CODE)
            fieldName = fieldToTag.inverse()[fieldID]
            rawField  = packetFields.__getrawfield__(fieldName)
            if isinstance(rawField, ComplexFieldType):
                # complex types must be initialized prior to decoding
                rawField.initializeData()
            try:
                yield from topDecoder.decodeIterator(stream, rawField)
            except Exception as encodingException:
                raise PacketEncodingError("Error decoding field {}.".format(fieldName)) from encodingException
PlaygroundStandardPacketEncoder.RegisterTypeEncoder(ComplexFieldType(PacketFields), PacketFieldsEncoder)

class ListEncoder:
    LIST_SIZE_PACK_CODE = "!H"
    
    def encode(self, stream, listType, topEncoder):
        stream.pack(self.LIST_SIZE_PACK_CODE, len(listType))
        for i in range(len(listType)):
            topEncoder.encode(stream, listType.__getrawitem__(i))
            
    def decodeIterator(self, stream, listType, topDecoder):
        listSize = yield from stream.unpackIterator(self.LIST_SIZE_PACK_CODE)
        for i in range(listSize):
            listType.append(PacketFieldType.UNSET) # Create a "null" entry in the list
            rawListData = listType.__getrawitem__(-1)
            try:
Exemplo n.º 15
0
 def PacketType(cls, newAttributes=None):
     return ComplexFieldType(cls, newAttributes)
Exemplo n.º 16
0
    def Deserialize(cls, buffer):
        encoder = cls.ENCODER()

        fieldWrapper = ComplexFieldType(cls)
        encoder.decode(io.BytesIO(buffer), fieldWrapper)
        return fieldWrapper.data()
            fieldID = yield from stream.unpackIterator(
                self.FIELD_TAG_PACK_CODE)
            fieldName = fieldToTag.inverse()[fieldID]
            rawField = packetFields.__getrawfield__(fieldName)
            if isinstance(rawField, ComplexFieldType):
                # complex types must be initialized prior to decoding
                rawField.initializeData()
            try:
                yield from topDecoder.decodeIterator(stream, rawField)
            except Exception as encodingException:
                raise PacketEncodingError("Error decoding field {}.".format(
                    fieldName)) from encodingException


PlaygroundStandardPacketEncoder.RegisterTypeEncoder(
    ComplexFieldType(PacketFields), PacketFieldsEncoder)


class ListEncoder:
    LIST_SIZE_PACK_CODE = "!H"

    def encode(self, stream, listType, topEncoder):
        stream.pack(self.LIST_SIZE_PACK_CODE, len(listType))
        for i in range(len(listType)):
            topEncoder.encode(stream, listType.__getrawitem__(i))

    def decodeIterator(self, stream, listType, topDecoder):
        listSize = yield from stream.unpackIterator(self.LIST_SIZE_PACK_CODE)
        for i in range(listSize):
            listType.append(
                PacketFieldType.UNSET)  # Create a "null" entry in the list