1#!/usr/local/bin/python2.7
2# start of new fragment overlaps old one
3
4# |--------|
5#     |>>>>>----|
6
7# If the tail of an older fragment overlaps the beginning of the
8# current fragment, cut the newer fragment.
9#                 m_adj(frent->fe_m, precut);
10# Newer data wins.
11
12import os
13import threading
14from addr import *
15from scapy.all import *
16
17class Sniff1(threading.Thread):
18	filter = None
19	captured = None
20	packet = None
21	def run(self):
22		self.captured = sniff(iface=SRC_IF, filter=self.filter,
23		    count=1, timeout=3)
24		if self.captured:
25			self.packet = self.captured[0]
26
27dstaddr=sys.argv[1]
28pid=os.getpid() & 0xffff
29payload="ABCDEFGHIJKLOMNO"
30dummy="01234567"
31packet=IP(src=SRC_OUT, dst=dstaddr)/ICMP(id=pid)/payload
32frag0=str(packet)[20:36]
33frag1=dummy+str(packet)[36:44]
34pkt0=IP(src=SRC_OUT, dst=dstaddr, proto=1, id=pid, frag=0, flags='MF')/frag0
35pkt1=IP(src=SRC_OUT, dst=dstaddr, proto=1, id=pid, frag=1)/frag1
36eth=[]
37eth.append(Ether(src=SRC_MAC, dst=PF_MAC)/pkt0)
38eth.append(Ether(src=SRC_MAC, dst=PF_MAC)/pkt1)
39
40sniffer = Sniff1();
41sniffer.filter = "ip and src %s and dst %s and icmp" % (dstaddr, SRC_OUT)
42sniffer.start()
43time.sleep(1)
44sendp(eth, iface=SRC_IF)
45sniffer.join(timeout=5)
46a = sniffer.packet
47
48if a and a.type == ETH_P_IP and \
49    a.payload.proto == 1 and \
50    a.payload.frag == 0 and a.payload.flags == 0 and \
51    icmptypes[a.payload.payload.type] == 'echo-reply':
52	id=a.payload.payload.id
53	print "id=%#x" % (id)
54	if id != pid:
55		print "WRONG ECHO REPLY ID"
56		exit(2)
57	load=a.payload.payload.payload.load
58	print "payload=%s" % (load)
59	if load == payload:
60		exit(0)
61	print "PAYLOAD!=%s" % (payload)
62	exit(1)
63print "NO ECHO REPLY"
64exit(2)
65