ipaddr = Adapter(byte[4], decode=lambda arr, _: ".".join(map(str, arr)), encode=lambda ipstr, _: map(int, ipstr.split("."))) print ipaddr.unpack("ABCD") # 65.66.67.68 print repr(ipaddr.pack("127.0.0.1")) # '\x7f\x00\x00\x01' mac_addr = Adapter( byte[6], decode=lambda arr, _: "-".join(map("%02x".__mod__, arr)), encode=lambda macstr, _: macstr.replace("-", "").decode("hex")) ethernet_header = Struct("destination" / mac_addr, "source" / mac_addr, "type" / int16ub) print ethernet_header.unpack("123456ABCDEF\x86\xDD") ethernet_header = Struct( "destination" / mac_addr, "source" / mac_addr, "type" / Enum( int16ub, IPv4=0x0800, ARP=0x0806, RARP=0x8035, X25=0x0805, IPX=0x8137, IPv6=0x86DD, )) print ethernet_header.unpack("123456ABCDEF\x86\xDD") prefixed_string = byte >> Raw(this[0])
"dont_fragment" / flag, "more_fragments" / flag, ), "frame_offset" / Bits(13), ), "ttl" / int8u, "protocol" / Enum(int8u, ICMP = 1, TCP = 6, UDP = 17), "checksum" / int16ub, "source" / ipaddr, "destination" / ipaddr, "options" / Raw(this.header_length - 20), ) if __name__ == "__main__": cap = "4500003ca0e3000080116185c0a80205d474a126".decode("hex") obj = ipv4_header.unpack(cap) print (obj) #rebuilt = ipv4_header.pack(obj) #print (repr(rebuilt)) #assert cap == rebuilt
) print ipaddr.unpack("ABCD") # 65.66.67.68 print repr(ipaddr.pack("127.0.0.1")) # '\x7f\x00\x00\x01' mac_addr = Adapter(byte[6], decode = lambda arr, _: "-".join(map("%02x".__mod__, arr)), encode = lambda macstr, _: macstr.replace("-", "").decode("hex") ) ethernet_header = Struct( "destination" / mac_addr, "source" / mac_addr, "type" / int16ub ) print ethernet_header.unpack("123456ABCDEF\x86\xDD") ethernet_header = Struct( "destination" / mac_addr, "source" / mac_addr, "type" / Enum(int16ub, IPv4 = 0x0800, ARP = 0x0806, RARP = 0x8035, X25 = 0x0805, IPX = 0x8137, IPv6 = 0x86DD, ) ) print ethernet_header.unpack("123456ABCDEF\x86\xDD")