Path: blob/main/tools/regression/netinet/ip_id_period/ip_id_period.py
39491 views
# Copyright (C) 2008 Michael J. Silbersack. All rights reserved.1#2# Redistribution and use in source and binary forms, with or without3# modification, are permitted provided that the following conditions4# are met:5# 1. Redistributions of source code must retain the above copyright6# notice unmodified, this list of conditions, and the following7# disclaimer.8# 2. Redistributions in binary form must reproduce the above copyright9# notice, this list of conditions and the following disclaimer in the10# documentation and/or other materials provided with the distribution.11#12# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR13# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES14# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.15# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,16# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT17# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,18# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY19# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT20# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF21# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.22#23#24# This is a regression test to verify the proper behavior of IP ID generation25# code. It will push 200000 packets, then report back what the min and max26# periods it saw for different IDs were.2728from __future__ import print_function29import os30import signal31import subprocess32import time3334if os.path.exists('results.pcap'):35os.remove('results.pcap')36tcpdump = subprocess.Popen('tcpdump -n -i lo0 -w results.pcap icmp', shell=True)37time.sleep(1) # Give tcpdump time to start3839os.system('sysctl net.inet.icmp.icmplim=0')40os.system('ping -q -i .001 -c 100000 127.0.0.1')4142time.sleep(3) # Give tcpdump time to catch up43os.kill(tcpdump.pid, signal.SIGTERM)4445os.system('tcpdump -n -v -r results.pcap > results.txt')4647id_lastseen = {}48id_minperiod = {}4950count = 051for line in open('results.txt').readlines():52id = int(line.split(' id ')[1].split(',')[0])53if id in id_lastseen:54period = count - id_lastseen[id]55if id not in id_minperiod or period < id_minperiod[id]:56id_minperiod[id] = period57id_lastseen[id] = count58count += 15960sorted_minperiod = list(zip(*reversed(list(zip(*list(id_minperiod.items()))))))61sorted_minperiod.sort()6263print("Lowest 10 ID periods detected:")64x = 065while x < 10:66id_tuple = sorted_minperiod.pop(0)67print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))68x += 16970print("Highest 10 ID periods detected:")71x = 072while x < 10:73id_tuple = sorted_minperiod.pop()74print("id: %d period: %d" % (id_tuple[1], id_tuple[0]))75x += 1767778