Read TTL from an ICMP message received via Python raw sockets -


i'm building traceroute-ish tool determine number of hops required udp packet reach address using 1 probe. this, want extract ttl icmp message receive after sending probe. i'm doing following , receiving icmp message:

data, source = in_socket.recvfrom(d_bufsize) 

but have no idea how turn data can read ttl from. in_socket declared this:

in_socket = socket.socket(socket.af_inet, socket.sock_raw, icmp_proto) 

here, icmp_proto protocol number icmp (obtained doing icmp_proto = socket.getprotobyname("icmp")).

any appreciated!

but have no idea how turn data can read ttl from.

pyping way:

    def header2dict(self, names, struct_format, data):         """ unpack raw received ip , icmp header informations dict """         unpacked_data = struct.unpack(struct_format, data)         return dict(zip(names, unpacked_data)) …              packet_data, address = current_socket.recvfrom(icmp_max_recv)              icmp_header = self.header2dict(                 names=[                     "type", "code", "checksum",                     "packet_id", "seq_number"                 ],                 struct_format="!bbhhh",                 data=packet_data[20:28]             )              if icmp_header["packet_id"] == self.own_id: # our packet                 ip_header = self.header2dict(                     names=[                         "version", "type", "length",                         "id", "flags", "ttl", "protocol",                         "checksum", "src_ip", "dest_ip"                     ],                     struct_format="!bbhhhbbhii",                     data=packet_data[:20]                 )                 packet_size = len(packet_data) - 28                 ip = socket.inet_ntoa(struct.pack("!i", ip_header["src_ip"])) 

the ttl can read ip_header["ttl"].


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -