Search code examples
pythonprotocolsscapypacket

Bitfields in Scapy


I've created a new packet type:

class goober(Packet):
    fields_desc=[
        Bitfield("alpha",5,3),
        ByteField("bravo",100),
        Bitfield("charlie",3,0)
    ]

This throws an error when sending. But when I reorder as follows, it works:

class goober(Packet):
    fields_desc=[
        Bitfield("alpha",5,3),
        Bitfield("charlie",3,0),
        ByteField("bravo",100),
    ]

Problem is, I really need the first form. Can anyone suggest a way of doing this? Can you point me to an example?


Solution

  • You simply must keep using BitField until you make a multiple of 8.

    class goober(Packet):
        fields_desc=[
            BitField("alpha",0,3),
            BitField("bravo",100, 8),
            BitField("charlie",0,5)
        ]