forked from pratiknarang/peershark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlow.py
239 lines (206 loc) · 6.56 KB
/
Flow.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from Packet import *
#input: list of packets, timegap - real number
#return val: list of flows
#
#merges collection of packets(objects) into collection of flows(many-to-one)
#Working: group packets with same ip-pair(direction irrelevant) and merge all packets for
#which |packet1.time - packet2.time| < threshold(timegap)
def packetsToFlows(packets,timegap):
#sanity check for 0 packets
if len(packets) == 0:
return None
outputflows = []
#perform a radix-sort to group together packets
#with same ip-pairs(packet.key represents an ip-pair)
#and sort these packets according to timestamp
packets.sort(key = lambda packet:packet.timestamp)
packets.sort(key = lambda packet:packet.key)
nextflow = Flow(None)
for nextpacket in packets:
#if ip-pairs dont match or time-difference of prev and current packet greater
#than timegap, create a new flow
if (nextflow.key != nextpacket.key) or ((nextpacket.timestamp - nextflow.getEnd()) > timegap):
nextflow = Flow(nextpacket)
outputflows.append(nextflow)
#if not then add packet to previous flow
else:
nextflow.addPacket(nextpacket)
return outputflows
#same as function packetsToFlow but merges flows instead of packets
def combineFlows(flows, timegap):
if len(flows) == 0:
return None
outputflows = []
flows.sort(key = lambda flow:flow.getStart())
flows.sort(key = lambda flow:flow.key)
nextoutflow = Flow(None)
for nextflow in flows:
if (nextoutflow.key != nextflow.key) or ((nextflow.getStart() - nextoutflow.getEnd()) > timegap):
nextoutflow = nextflow
outputflows.append(nextoutflow)
else:
nextoutflow.addFlow(nextflow)
return outputflows
def getCustomWeightedAvg(n1, w1, n2, w2):
num = 0
den = 0
if w1 > 0:
num += w1 * n1
den += w1
if w2 > 0:
num += w2 * n2
den += w2
if den <= 0:
den = 1
return num / den
#write list of flows into file in desired format
def writeFlowsToFile(flowlist, filename):
outfile = open(filename, 'w')
for flow in flowlist:
outfile.write(
socket.inet_ntoa(flow.ip1) + ',' +
socket.inet_ntoa(flow.ip2) + ',' +
str(flow.n_packet1) + ',' +
str(flow.n_byte1) + ',' +
'%.6f'%flow.t_start1 + ',' +
'%.6f'%flow.t_end1 + ',' +
'%.6f'%flow.getInterArrivaltime1() + ',' +
str(flow.n_packet2) + ',' +
str(flow.n_byte2) + ',' +
'%.6f'%flow.t_start2 + ',' +
'%.6f'%flow.t_end2 + ',' +
'%.6f'%flow.getInterArrivaltime2() + '\n')
outfile.close()
#class which defines the structure of flows
class Flow:
#constructor of default flow
def __init__(self,firstpacket):
if firstpacket == None:
self.ip1 = None
self.ip2 = None
self.key = None
self.n_packet1 = 0
self.n_byte1 = 0
self.t_start1 = 0
self.t_end1 = 0
self.t_interarrival1 = []
self.n_packet2 = 0
self.n_byte2 = 0
self.t_start2 = 0
self.t_end2 = 0
self.t_interarrival2 = []
else:
if firstpacket.source < firstpacket.dest:
self.ip1 = firstpacket.source
self.ip2 = firstpacket.dest
self.n_packet1 = 1
self.n_byte1 = firstpacket.size
self.t_start1 = firstpacket.timestamp
self.t_end1 = firstpacket.timestamp
self.t_interarrival1 = []
self.n_packet2 = 0
self.n_byte2 = 0
self.t_start2 = 0
self.t_end2 = 0
self.t_interarrival2 = []
else:
self.ip1 = firstpacket.dest
self.ip2 = firstpacket.source
self.n_packet1 = 0
self.n_byte1 = 0
self.t_start1 = 0
self.t_end1 = 0
self.t_interarrival1 = []
self.n_packet2 = 1
self.n_byte2 = firstpacket.size
self.t_start2 = firstpacket.timestamp
self.t_end2 = firstpacket.timestamp
self.t_interarrival2 = []
self.key = firstpacket.key
#add a flow to the current flow (by changing volume and duration)
def addFlow(self,flow):
self.t_interarrival1 += flow.t_interarrival1
self.t_interarrival2 += flow.t_interarrival2
self.n_packet1 += flow.n_packet1
self.n_packet2 += flow.n_packet2
self.n_byte1 += flow.n_byte1
self.n_byte2 += flow.n_byte2
temp = min(self.t_start1,flow.t_start1)
if temp == 0:
self.t_start1 = self.t_start1 + flow.t_start1
else:
self.t_start1 = temp
temp = min(self.t_start2,flow.t_start2)
if temp == 0:
self.t_start2 = self.t_start2 + flow.t_start2
else:
self.t_start2 = temp
if(self.t_end1 < flow.t_end1):
self.t_end1 = flow.t_end1
if(self.t_end2 < flow.t_end2):
self.t_end2 = flow.t_end2
#add a packet to the current flow (by changing volume and duration)
def addPacket(self,packet):
if packet.source == self.ip1 and packet.dest == self.ip2:
#initialize flow if not initialized
if self.n_packet1 == 0:
self.t_start1 = packet.timestamp
self.t_end1 = packet.timestamp
self.n_packet1 += 1
self.n_byte1 += packet.size
return
if self.t_end1 < packet.timestamp:
self.t_interarrival1.append(packet.timestamp-self.t_end1)
self.t_end1 = packet.timestamp
elif self.t_start1 > packet.timestamp:
self.t_interarrival1.append(self.t_start1-packet.timestamp)
self.t_start1 = packet.timestamp
self.n_packet1 += 1
self.n_byte1 += packet.size
elif packet.source == self.ip2 and packet.dest == self.ip1:
#initialize flow if not initialized
if self.n_packet2 == 0:
self.t_start2 = packet.timestamp
self.t_end2 = packet.timestamp
self.n_packet2 += 1
self.n_byte2 += packet.size
return
if self.t_end2 < packet.timestamp:
self.t_interarrival2.append(packet.timestamp-self.t_end2)
self.t_end2 = packet.timestamp
elif self.t_start2 > packet.timestamp:
self.t_interarrival2.append(self.t_start2-packet.timestamp)
self.t_start2 = packet.timestamp
self.n_packet2 += 1
self.n_byte2 += packet.size
else:
raise Exception('packet does not belong to flow')
def getDurationInSeconds(self):
return self.getEnd() - self.getStart()
def getInterArrivaltime(self):
combined = (self.t_interarrival1+self.t_interarrival2).sort()
if len(combined) > 0:
return combined[len(combined)/2]
return 0
def getInterArrivaltime1(self):
self.t_interarrival1.sort()
if len(self.t_interarrival1) > 0:
return self.t_interarrival1[len(self.t_interarrival1)/2]
return 0
def getInterArrivaltime2(self):
self.t_interarrival2.sort()
if len(self.t_interarrival2) > 0:
return self.t_interarrival2[len(self.t_interarrival2)/2]
return 0
def getNoOfBytes(self):
return self.n_byte1 + self.n_byte2
def getNoOfPackets(self):
return self.n_packet1 + self.n_packet2
def getStart(self):
temp = min(self.t_start1, self.t_start2)
if temp == 0:
return self.t_start1 + self.t_start2
else:
return temp
def getEnd(self):
return max(self.t_end1, self.t_end2)