1fd2e4074SMike Silbersack# Copyright (C) 2008 Michael J. Silbersack.  All rights reserved.
2fd2e4074SMike Silbersack#
3fd2e4074SMike Silbersack# Redistribution and use in source and binary forms, with or without
4fd2e4074SMike Silbersack# modification, are permitted provided that the following conditions
5fd2e4074SMike Silbersack# are met:
6fd2e4074SMike Silbersack# 1. Redistributions of source code must retain the above copyright
7fd2e4074SMike Silbersack#    notice unmodified, this list of conditions, and the following
8fd2e4074SMike Silbersack#    disclaimer.
9fd2e4074SMike Silbersack# 2. Redistributions in binary form must reproduce the above copyright
10fd2e4074SMike Silbersack#    notice, this list of conditions and the following disclaimer in the
11fd2e4074SMike Silbersack#    documentation and/or other materials provided with the distribution.
12fd2e4074SMike Silbersack#
13fd2e4074SMike Silbersack# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14fd2e4074SMike Silbersack# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15fd2e4074SMike Silbersack# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16fd2e4074SMike Silbersack# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17fd2e4074SMike Silbersack# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18fd2e4074SMike Silbersack# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19fd2e4074SMike Silbersack# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20fd2e4074SMike Silbersack# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21fd2e4074SMike Silbersack# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22fd2e4074SMike Silbersack# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23fd2e4074SMike Silbersack#
24fd2e4074SMike Silbersack#
25fd2e4074SMike Silbersack# This is a regression test to verify the proper behavior of IP ID generation
26fd2e4074SMike Silbersack# code.  It will push 200000 packets, then report back what the min and max
27fd2e4074SMike Silbersack# periods it saw for different IDs were.
28fd2e4074SMike Silbersack
29749f65e3SCraig Rodriguesfrom __future__ import print_function
30fd2e4074SMike Silbersackimport os
31fd2e4074SMike Silbersackimport signal
32fd2e4074SMike Silbersackimport subprocess
33fd2e4074SMike Silbersackimport time
34fd2e4074SMike Silbersack
35fd2e4074SMike Silbersackif os.path.exists('results.pcap'):
36fd2e4074SMike Silbersack    os.remove('results.pcap')
37fd2e4074SMike Silbersacktcpdump = subprocess.Popen('tcpdump -n -i lo0 -w results.pcap icmp', shell=True)
38fd2e4074SMike Silbersacktime.sleep(1) # Give tcpdump time to start
39fd2e4074SMike Silbersack
40fd2e4074SMike Silbersackos.system('sysctl net.inet.icmp.icmplim=0')
41fd2e4074SMike Silbersackos.system('ping -q -i .001 -c 100000 127.0.0.1')
42fd2e4074SMike Silbersack
43fd2e4074SMike Silbersacktime.sleep(3) # Give tcpdump time to catch up
44fd2e4074SMike Silbersackos.kill(tcpdump.pid, signal.SIGTERM)
45fd2e4074SMike Silbersack
46fd2e4074SMike Silbersackos.system('tcpdump -n -v -r results.pcap > results.txt')
47fd2e4074SMike Silbersack
48fd2e4074SMike Silbersackid_lastseen = {}
49fd2e4074SMike Silbersackid_minperiod = {}
50fd2e4074SMike Silbersack
51fd2e4074SMike Silbersackcount = 0
52fd2e4074SMike Silbersackfor line in open('results.txt').readlines():
53fd2e4074SMike Silbersack    id = int(line.split(' id ')[1].split(',')[0])
541f35187fSEitan Adler    if id in id_lastseen:
55fd2e4074SMike Silbersack        period = count - id_lastseen[id]
561f35187fSEitan Adler        if id not in id_minperiod or period < id_minperiod[id]:
57fd2e4074SMike Silbersack            id_minperiod[id] = period
58fd2e4074SMike Silbersack    id_lastseen[id] = count
59fd2e4074SMike Silbersack    count += 1
60fd2e4074SMike Silbersack
611f35187fSEitan Adlersorted_minperiod = list(zip(*reversed(list(zip(*list(id_minperiod.items()))))))
62fd2e4074SMike Silbersacksorted_minperiod.sort()
63fd2e4074SMike Silbersack
641f35187fSEitan Adlerprint("Lowest 10 ID periods detected:")
65fd2e4074SMike Silbersackx = 0
66fd2e4074SMike Silbersackwhile x < 10:
67fd2e4074SMike Silbersack    id_tuple = sorted_minperiod.pop(0)
681f35187fSEitan Adler    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
69fd2e4074SMike Silbersack    x += 1
70fd2e4074SMike Silbersack
711f35187fSEitan Adlerprint("Highest 10 ID periods detected:")
72fd2e4074SMike Silbersackx = 0
73fd2e4074SMike Silbersackwhile x < 10:
74fd2e4074SMike Silbersack    id_tuple = sorted_minperiod.pop()
751f35187fSEitan Adler    print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))
76fd2e4074SMike Silbersack    x += 1
77