File: udp.py

package info (click to toggle)
construct.legacy 2.5.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 444 kB
  • sloc: python: 4,731; makefile: 3
file content (26 lines) | stat: -rw-r--r-- 589 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
User Datagram Protocol (TCP/IP protocol stack)
"""
from construct_legacy import *
import six
from binascii import unhexlify


udp_header = Struct("udp_header",
    Value("header_length", lambda ctx: 8),
    UBInt16("source"),
    UBInt16("destination"),
    ExprAdapter(UBInt16("payload_length"), 
        encoder = lambda obj, ctx: obj + 8,
        decoder = lambda obj, ctx: obj - 8,
    ),
    UBInt16("checksum"),
)

if __name__ == "__main__":
    cap = unhexlify(six.b("0bcc003500280689"))
    obj = udp_header.parse(cap)
    print (obj)
    print (repr(udp_header.build(obj)))