Getting into protocol development with Scapy, going through documentation and watching videos on Scapy but I'm a bit confused with field types. Let's assume something generic for a packet structure:
| version number - 8 bits | message type - 8 bits | payload size - 16 bits | payload of varying length |
Now, I want to implement this in python using Scapy, but I'm confused about what fields I should be using. Scapy documentation doesn't explain most of the different fields. The tutorials in "Adding new protocols" section of the Scapy documentation uses different fields but doesn't explain what they are. Some of them are easy enough to understand based on their names, but what is a ShortField for example? LongField?
Some of the fields have default functionality, like LenField, that gets autopopulated with payload size when you build the packet. However there's no way to set the size of the LenField like you can with a BitField. In my example protocol, payload size is supposed to be 16 bits, how do I enforce that in a LenField or similar. I could always use a BitField but that seems dumb when LenField seems to exist specifically for this kind of purpose.
Looking at another example I found on the Internet while researching this topic:
class Foo(Packet):
name = "my_packet"
fields_desc = [
ShortField("index", 0)
FieldLenField("len", None, length_of="data")
StrLenField("data", "", length_from="len")
FieldLenField("sig_len", None, length_of="data")
StrLenField("sig", "", length_from="sig_len")
Why did they choose these specific fields? Why ShortField and not BitField?
In conclusion the questions I'm looking answers for are the following:
- Is there a place to lookup explanations for each of the field types? Are they a standard? Scapy documentation leaves a beginner like me lacking.
- How should I decide what type of field to use? I guess this might be answered by question 1 but in case there's additional details I would love to hear them.
- How do I know the bit length of field types that don't explicitly state it, for example in their naming? Some of these also do not have a size attribute like BitField does where I can manually set it.
- How should variable length values like frame size be handled in code to build a field of specific size without having to worry about overflowing or bit length that is lower than defined in the protocol spec. Does the LenField, for example do this automatically as part of it's functionality? How would I make sure the value is 16bits long in my example?