This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (128 loc) · 5.13 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sys, getopt
from subprocess import Popen, PIPE
import shlex
from dpkt.ip import IP
import dpkt, socket
from statistics import mean_confidence_interval
TARGET_IPV4 = ("127.1.1.1", 0)
# To send signal of resetting flows
sock4 = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
class Route:
def __init__(self, source, destination):
self.source = source
self.destination = destination
def __eq__(self, other):
return self.source == other.source and self.destination == other.destination
def parseRoutes(topologyFile):
routes = []
with open(topologyFile) as topology:
for route in topology:
addresses = route.split(" ")
if len(addresses) == 2:
# Remove the backslash n from the destination
addresses[1] = addresses[1].replace("\n", "")
routes.append(Route(addresses[0], addresses[1]))
return routes
def compareRoutes(parsedRoutes, foundRoutes):
for route in parsedRoutes:
if route not in foundRoutes:
return False
if len(parsedRoutes) != len(foundRoutes):
return False
return True
def startParisTraceroute(parisTracerouteCmd):
args = shlex.split(parisTracerouteCmd)
p = Popen(args, stdout=PIPE, bufsize=1)
startedLattice = False
foundRoutes = []
with p.stdout:
for line in iter(p.stdout.readline, b''):
if startedLattice:
tokens = line.split("-> [")
# If there is only one token, means that we have reached our destination
if len(tokens) == 1:
continue
# If it is None, deduce it as source localhost
if tokens[0] == "None ":
tokens[0] = "127.0.0.1"
# Remove space from source too
source = tokens[0].replace(" ", "")
# Check if there are more than 1 destination
destinations = tokens[1].split(", ")
# Remove the ] from the last destination address
destinations[len(destinations) - 1] = destinations[len(destinations) - 1].replace("]", "")
for destination in destinations:
destination = destination.replace(" ", "")
destination = destination.replace("\n", "")
foundRoutes.append(Route(source, destination))
if line.startswith("Lattice:"):
startedLattice = True
return foundRoutes
p.wait()
def main(argv):
topologyFile = ''
outputfile = ''
parisTracerouteCmd = ""
confidenceIntervalPercent = 0.95
runNumber = 1000
try:
opts, args = getopt.getopt(argv, "ht:p:o:n:c:", ["topology=", "ofile=", "paris-traceroute="])
except getopt.GetoptError:
print 'main.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'main.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-t", "--topology"):
topologyFile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-p", "--paris-traceroute"):
parisTracerouteCmd = arg
elif opt in ("-n", "--run-number"):
runNumber = int(arg)
elif opt in ("-c", "--confidence-interval"):
confidenceIntervalPercent = float(arg)
print 'Topology file is "', topologyFile
print "Will execute this paris-traceroute command: ", parisTracerouteCmd
print 'Output file is "', outputfile
parsedRoutes = parseRoutes(topologyFile)
ipPacket = IP(
src="127.0.0.1",
dst="127.1.1.1",
p=dpkt.ip.IP_PROTO_UDP,
ttl=128
)
sampleNumber = 50
samples = []
for k in range(0, sampleNumber):
failure = 0
success = 0
if k % 10 == 0:
print "Iteration number : ", k
for x in range(0, runNumber):
foundRoutes = startParisTraceroute(parisTracerouteCmd)
sock4.sendto(str(ipPacket), TARGET_IPV4)
if compareRoutes(parsedRoutes, foundRoutes):
success += 1
else:
failure += 1
samples.append(failure / float((failure + success)))
print "sample number: ", sampleNumber
print "run number by sample : ", runNumber
m, interval = mean_confidence_interval(samples,confidenceIntervalPercent)
print "mean estimated: ", m
print "interval :", interval
confidenceInterval = "interval of confidence with "+str(confidenceIntervalPercent)+": " + "[" + str(m - interval) + ", " + str(m + interval) + "]"
print confidenceInterval
if outputfile == "":
outputfile = topologyFile + "_output"
with open(outputfile, "w") as output:
output.write("sample number: " + str(sampleNumber) + "\n")
output.write("run number by sample: " + str(runNumber) + "\n")
output.write("mean estimated of failure: " + str(m) + "\n")
output.write("interval :" + str(interval) + "\n")
output.write("confidenceInterval: " + confidenceInterval + "\n")
if __name__ == "__main__":
main(sys.argv[1:])