- raise Exception('Timeout waiting for packet')
-except:
- src_if.close()
- if dst_if_defined:
- dst_if.close()
- raise
-
-if dst_if_defined:
- check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
- pkt_resp_send_mod = pkt_resp_send.copy()
- pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
- del pkt_resp_send_mod[IP].chksum # update checksum
- ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
-
-src_if.close()
-if dst_if_defined:
- dst_if.close()
+ raise RuntimeError('Timeout waiting for packet')
+
+ if is_dst_tg:
+ check_ttl(pkt_resp_send[IP].ttl, pkt_resp_recv[IP].ttl, hops)
+ pkt_resp_send_mod = pkt_resp_send.copy()
+ pkt_resp_send_mod[IP].ttl = pkt_resp_recv[IP].ttl
+ del pkt_resp_send_mod[IP].chksum # update checksum
+ ckeck_packets_equal(pkt_resp_send_mod[IP], pkt_resp_recv[IP])
+
+ if not pkt_resp_recv.haslayer(IP):
+ raise RuntimeError('Received packet does not contain IPv4 header: {}'.
+ format(pkt_resp_recv.__repr__()))
+
+ if pkt_resp_recv[IP].src != pkt_req_send[IP].dst:
+ raise RuntimeError(
+ 'Received IPv4 packet contains wrong src IP address, '
+ '{} instead of {}'.format(pkt_resp_recv[IP].src,
+ pkt_req_send[IP].dst))
+
+ if pkt_resp_recv[IP].dst != pkt_req_send[IP].src:
+ raise RuntimeError(
+ 'Received IPv4 packet contains wrong dst IP address, '
+ '{} instead of {}'.format(pkt_resp_recv[IP].dst,
+ pkt_req_send[IP].src))
+
+ # verify IPv4 checksum
+ copy = pkt_resp_recv.copy()
+ chksum = copy[IP].chksum
+ del copy[IP].chksum
+ tmp = IP(str(copy[IP]))
+ if not checksum_equal(tmp.chksum, chksum):
+ raise RuntimeError('Received IPv4 packet contains invalid checksum, '
+ '{} instead of {}'.format(chksum, tmp.chksum))
+
+ if not pkt_resp_recv[IP].haslayer(ICMP):
+ raise RuntimeError(
+ 'Received IPv4 packet does not contain ICMP header: {}'.
+ format(pkt_resp_recv[IP].__repr__()))
+
+ # verify ICMP checksum
+ copy = pkt_resp_recv.copy()
+ chksum = copy[IP][ICMP].chksum
+ del copy[IP][ICMP].chksum
+ tmp = ICMP(str(copy[IP][ICMP]))
+ if not checksum_equal(tmp.chksum, chksum):
+ raise RuntimeError('Received ICMP packet contains invalid checksum, '
+ '{} instead of {}'.format(chksum, tmp.chksum))
+
+ pkt_req_send_mod = pkt_req_send.copy()
+ pkt_req_send_mod[IP][ICMP].type = pkt_resp_recv[IP][ICMP].type
+ del pkt_req_send_mod[IP][ICMP].chksum # update checksum
+ ckeck_packets_equal(pkt_req_send_mod[IP][ICMP], pkt_resp_recv[IP][ICMP])
+
+
+if __name__ == "__main__":
+ main()