1 #!/usr/local/bin/python2.7
6 from sniffer import Sniffer
8 PAYLOAD_MAGIC = 0x42c0ffee
10 def check_ping_request(args, packet):
12 return check_ping6_request(args, packet)
14 return check_ping4_request(args, packet)
16 def check_ping4_request(args, packet):
18 Verify that the packet matches what we'd have sent
22 ip = packet.getlayer(sp.IP)
28 icmp = packet.getlayer(sp.ICMP)
31 if sp.icmptypes[icmp.type] != 'echo-request':
34 raw = packet.getlayer(sp.Raw)
37 if raw.load != str(PAYLOAD_MAGIC):
40 # Wait to check expectations until we've established this is the packet we
43 if ip.tos != int(args.expect_tos[0]):
44 print "Unexpected ToS value %d, expected %s" \
45 % (ip.tos, args.expect_tos[0])
50 def check_ping6_request(args, packet):
52 Verify that the packet matches what we'd have sent
56 ip = packet.getlayer(sp.IPv6)
62 icmp = packet.getlayer(sp.ICMPv6EchoRequest)
65 if icmp.data != str(PAYLOAD_MAGIC):
70 def ping(send_if, dst_ip, args):
72 ip = sp.IP(dst=dst_ip)
73 icmp = sp.ICMP(type='echo-request')
74 raw = sp.Raw(str(PAYLOAD_MAGIC))
77 ip.tos = int(args.send_tos[0])
79 req = ether / ip / icmp / raw
80 sp.sendp(req, iface=send_if, verbose=False)
82 def ping6(send_if, dst_ip, args):
84 ip6 = sp.IPv6(dst=dst_ip)
85 icmp = sp.ICMPv6EchoRequest(data=PAYLOAD_MAGIC)
87 req = ether / ip6 / icmp
88 sp.sendp(req, iface=send_if, verbose=False)
91 parser = argparse.ArgumentParser("pft_ping.py",
92 description="Ping test tool")
93 parser.add_argument('--sendif', nargs=1,
95 help='The interface through which the packet(s) will be sent')
96 parser.add_argument('--recvif', nargs=1,
97 help='The interface on which to expect the ICMP echo response')
98 parser.add_argument('--ip6', action='store_true',
100 parser.add_argument('--to', nargs=1,
102 help='The destination IP address for the ICMP echo request')
105 parser.add_argument('--send-tos', nargs=1,
106 help='Set the ToS value for the transmitted packet')
109 parser.add_argument('--expect-tos', nargs=1,
110 help='The expected ToS value in the received packet')
112 args = parser.parse_args()
114 # We may not have a default route. Tell scapy where to start looking for routes
115 sp.conf.iface6 = args.sendif[0]
118 if not args.recvif is None:
119 sniffer = Sniffer(args, check_ping_request)
122 ping6(args.sendif[0], args.to[0], args)
124 ping(args.sendif[0], args.to[0], args)
129 if sniffer.foundCorrectPacket:
134 if __name__ == '__main__':