IPv4 Header¶
[1]:
from byteclasses.print import byteclass_inspect, collection_table, byteclass_info
from byteclasses.handlers.network.ipv4_hdr import IPv4Hdr
[2]:
ipv4_hdr = IPv4Hdr()
data = bytearray(b"\x45\x00\x00\x34\x1d\x12\x40\x00\x80\x06\xcb\xc3\xc0\xa8\xc8\x87\xc0\xa8\xc8\x15")
ipv4_hdr.attach(memoryview(data))
[3]:
byteclass_info(ipv4_hdr)
Byteclass Info ┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ IPv4Hdr │ │ mro │ IPv4Hdr -> object │ │ len() │ 20 │ │ str() │ IPv4Hdr(ver_ihl=VerIhl(data=b'E'), dscp_ecn=DscpEcn(data=b'\x00'), total_length=UInt16(52), │ │ │ identification=UInt16(7442), flags_off=FlagsOff(data=b'@\x00'), time_to_live=UInt8(128), │ │ │ protocol=UInt8(6), header_checksum=UInt16(52163), src_ip=IPv4Addr(uint8=[UInt8(192), UInt8(168), │ │ │ UInt8(200), UInt8(135)], uint32=UInt32(3232286855)), dst_ip=IPv4Addr(uint8=[UInt8(192), UInt8(168), │ │ │ UInt8(200), UInt8(21)], uint32=UInt32(3232286741))) │ │ repr() │ IPv4Hdr(ver_ihl=VerIhl(data=b'E'), dscp_ecn=DscpEcn(data=b'\x00'), total_length=UInt16(52), │ │ │ identification=UInt16(7442), flags_off=FlagsOff(data=b'@\x00'), time_to_live=UInt8(128), │ │ │ protocol=UInt8(6), header_checksum=UInt16(52163), src_ip=IPv4Addr(uint8=[UInt8(192), UInt8(168), │ │ │ UInt8(200), UInt8(135)], uint32=UInt32(3232286855)), dst_ip=IPv4Addr(uint8=[UInt8(192), UInt8(168), │ │ │ UInt8(200), UInt8(21)], uint32=UInt32(3232286741))) │ │ .data │ bytearray(b'E\x00\x004\x1d\x12@\x00\x80\x06\xcb\xc3\xc0\xa8\xc8\x87\xc0\xa8\xc8\x15') │ └──────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────┘
[4]:
byteclass_inspect(ipv4_hdr)
╭─────────────────────────────────────────── Byteclass Inspect ───────────────────────────────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f │ │ ----------------------------------------------------------------------------------------------------- │ │ 0x0 |45|00|00 34|1d 12|40 00|80|06|cb c3|c0 a8 c8 87|c0 a8 c8 15 │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ ver_ihl │ VerIhl(10100010, flags={'version': 5, 'ihl': 4}) │ │ dscp_ecn │ DscpEcn(00000000, flags={'dscp': 0, 'ecn': 0}) │ │ total_length │ 52 │ │ identification │ 7442 │ │ flags_off │ FlagsOff(0000001000000000, flags=0) │ │ time_to_live │ 128 │ │ protocol │ 6 │ │ header_checksum │ 52163 │ │ src_ip │ IPv4Addr(uint8=[UInt8(192), UInt8(168), UInt8(200), UInt8(135)], uint32=UInt32(3232286855)) │ │ dst_ip │ IPv4Addr(uint8=[UInt8(192), UInt8(168), UInt8(200), UInt8(21)], uint32=UInt32(3232286741)) │ └─────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────┘
[5]:
collection_table(ipv4_hdr)
IPv4Hdr ┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ Data ┃ ┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ ver_ihl │ VerIhl(10100010, flags={'version': 5, 'ihl': 4}) │ b'E' │ │ dscp_ecn │ DscpEcn(00000000, flags={'dscp': 0, 'ecn': 0}) │ b'\x00' │ │ total_length │ 52 │ b'\x004' │ │ identification │ 7442 │ b'\x1d\x12' │ │ flags_off │ FlagsOff(0000001000000000, flags=0) │ b'@\x00' │ │ time_to_live │ 128 │ b'\x80' │ │ protocol │ 6 │ b'\x06' │ │ header_checksum │ 52163 │ b'\xcb\xc3' │ │ src_ip │ IPv4Addr(uint8=[UInt8(192), UInt8(168), UInt8(200), │ bytearray(b'\xc0\xa8\xc8\x87') │ │ │ UInt8(135)], uint32=UInt32(3232286855)) │ │ │ dst_ip │ IPv4Addr(uint8=[UInt8(192), UInt8(168), UInt8(200), │ bytearray(b'\xc0\xa8\xc8\x15') │ │ │ UInt8(21)], uint32=UInt32(3232286741)) │ │ └─────────────────┴──────────────────────────────────────────────────────────────┴────────────────────────────────┘
[6]:
ipv4_hdr.src_ip.uint8[:] = [192,168,1,1]
ipv4_hdr.dst_ip.uint8[:] = [127, 0, 0, 1]
print(hex(ipv4_hdr.src_ip.uint32), hex(ipv4_hdr.dst_ip.uint32))
print(ipv4_hdr.src_ip.data, ipv4_hdr.dst_ip.data)
0xc0a80101 0x7f000001
bytearray(b'\xc0\xa8\x01\x01') bytearray(b'\x7f\x00\x00\x01')
[7]:
ipv4_hdr.src_ip.uint32 = 0xdeadbeef
ipv4_hdr.dst_ip.uint32 = 0xcafebabe
print(ipv4_hdr.src_ip.data, ipv4_hdr.dst_ip.data)
print(ipv4_hdr.data)
bytearray(b'\xde\xad\xbe\xef') bytearray(b'\xca\xfe\xba\xbe')
bytearray(b'E\x00\x004\x1d\x12@\x00\x80\x06\xcb\xc3\xde\xad\xbe\xef\xca\xfe\xba\xbe')