File: ethernet.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 (38 lines) | stat: -rw-r--r-- 882 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
27
28
29
30
31
32
33
34
35
36
37
38
"""
Ethernet (TCP/IP protocol stack)
"""
from construct_legacy import *
from binascii import hexlify, unhexlify
import six


class MacAddressAdapter(Adapter):
    def _encode(self, obj, context):
        return unhexlify(obj.replace("-", ""))
    def _decode(self, obj, context):
        return "-".join(hexlify(b) for b in obj)

def MacAddress(name):
    return MacAddressAdapter(Bytes(name, 6))

ethernet_header = Struct("ethernet_header",
    MacAddress("destination"),
    MacAddress("source"),
    Enum(UBInt16("type"),
        IPv4 = 0x0800,
        ARP = 0x0806,
        RARP = 0x8035,
        X25 = 0x0805,
        IPX = 0x8137,
        IPv6 = 0x86DD,
        _default_ = Pass,
    ),
)


if __name__ == "__main__":
    cap = unhexlify(six.b("0011508c283c0002e34260090800"))
    obj = ethernet_header.parse(cap)
    print (obj)
    print (repr(ethernet_header.build(obj)))