secdev / scapy

Scapy: the Python-based interactive packet manipulation program & library.
https://scapy.net
GNU General Public License v2.0
10.81k stars 2.03k forks source link

How can a Packet/Field in a PacketListField/FieldListField find its index or access the preceding Packet/Field neighbor? #4552

Closed douniwan5788 closed 1 month ago

douniwan5788 commented 1 month ago
In some protocols, a common pattern is followed: Fields
Payloads Count
Payload 1 Offset
Payload 1 Size
Payload 2 Offset
Payload 2 Size
Payload N Offset
Payload N Size
...
Payload 1
Payload 2
...
Payload N

I have two PacketListFields, one for offsets and sizes and the other for payloads. However, the payload Packet cannot determine its size if it is unable to locate its index within the PacketListFields.

If adding an index attribute similar to parent to Field/Packet is necessary, I can make a PR

gpotter2 commented 1 month ago

For very complex cases such as this one, you'll likely have to do a custom field.

douniwan5788 commented 1 month ago

That's not a complex scenario. I believe it's a very common variant of the TLV format pattern for protocols with optional fields. Rewriting custom fields similar to PacketListField/FieldListField requires considerable effort and becomes hard to maintain. Would it be considered if I were to submit a PR for this change?

gpotter2 commented 1 month ago

Sorry I read that too quickly. In your case the payload PacketListField can use the pkt.parent attribute to go back one layer and get the size.

douniwan5788 commented 1 month ago

Yes, the payloadPacket can retrieve PacketListFields through the parent, but cannot determine its size if it is unable to locate its index within the PacketListFields.

gpotter2 commented 1 month ago

You can use next_cb_cls which contains the current "list", therefore the index.

gpotter2 commented 1 month ago

Please note that this is not a forum but a bugtracker. Please ask your question on stack overflow or on the gitter. Thanks for your understanding