-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.py
executable file
·207 lines (164 loc) · 5.75 KB
/
capture.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
import os
import struct
import time
try:
import pcap # python-pypcap
import numpy as np
except ImportError:
print "!! please make sure the following packages are installed:"
print "\tpython-pypcap"
print "\tpython-numpy"
exit(1)
try:
import gzip
except ImportError:
pass
import hphelper
options = hphelper.options
DEBUG = hphelper.DEBUG
ERROR = hphelper.err
################################################################################
def rcvloop(data_pipe, ns, geotimes=None):
"""receive ICMP packets from pcap, extract sequence number and
timestamp and forward to parser over pipe q"""
if ns.cnum:
pnum = ns.cnum
else:
pnum = options.pnum
hphelper.set_affinity('rcvloop')
ttl=None
struct_unpack = struct.unpack
def pcap_cb(time, pkt):
(icmp_type,) = struct_unpack('!B',pkt[34:34+1])
(seq,slot) = struct_unpack('!LL',pkt[50:58]) # ICMP (offset 34) + 16:50+4+4
if icmp_type==8: # ICMP echo request
s_times[seq] = time # store send time
elif icmp_type==0: # ICMP echo reply
snd_time = s_times[seq] # use captured send time to calculate RTT
data_pipe.send((seq, slot, time-snd_time)) # send to parser process
#(ttl,) = struct_unpack('!B',pkt[22:22+1]) # 14 Ethernet 14 + 8 IP
DEBUG('starting receiver ', __name__)
# init empty numpy arrays to store snd/rcv times
s_times = -1.0*np.ones(pnum)
try:
po = pcap.pcap(options.eth, snaplen=80, immediate=False, timeout_ms=3000) # timeout_ms works with dispatch only
po.setfilter('(icmp[icmptype] == icmp-echoreply or icmp[icmptype] == icmp-echo) and ip host ' + options.IPDST)
po_dispatch = po.dispatch
except OSError as e:
print e
exit(-1)
raise SystemExit(-1)
# notify sender that we are ready to capture
ns.RCV_READY = True
# block until sender says it is ready
while not ns.SND_READY:
time.sleep(0.1)
DEBUG('READY', __name__)
try:
while po_dispatch(0, pcap_cb):
pass
except KeyboardInterrupt:
pass
# timeout_ms was reached, notify parser that we are done
data_pipe.send('RCV_DONE')
DEBUG('DONE', __name__)
###############################################################################
def dumploop(pipe, ns, geotimes=None):
if not dump:
print 'ERROR: dump file not loaded!'
return
if ns.cnum:
pnum = ns.cnum
else:
pnum = options.pnum
hphelper.set_affinity('rcvloop')
# notify sender that we are ready to capture
ns.RCV_READY = True
# block until sender says it is ready
while not ns.SND_READY:
time.sleep(0.1)
DEBUG('READY', __name__)
try:
for s in dump.rcv_order[:pnum]:
if s == -1:
continue
seq = s
slot = dump.slottimes[seq]
rtt = dump.rtts[seq]
pipe.send((seq, slot, rtt)) # send to parser process
except IndexError:
print s
print seq
except KeyboardInterrupt:
print s
print 'canceled reading file.'
# notify parser that we are done
pipe.send('RCV_DONE')
#q.close()
DEBUG('done ', __name__)
class dumpdata(object):
""" Store the data loaded from a dumped trace file. """
def __init__(self, options):
self.slottimes = -1*np.ones(options.pnum).astype(int)
self.rcv_order = -1*np.ones(options.pnum).astype(int)
self.rtts = np.zeros(options.pnum)
self.dump_options = None
def pprint(self):
print 'loaded %d samples (dump mean %.6f)\n' % (np.sum(self.rcv_order!=-1), np.mean(self.rtts))
def dump_loader():
print "loading %d RTTs from " %(options.pnum) + options.loaddump
dump = dumpdata(options)
try:
if options.loaddump[-2:].lower()=='gz':
fs = gzip.open(options.loaddump, mode='r')
else:
fs = open(options.loaddump, mode='r')
# first line should be a comment containing options
l = fs.readline()
(c, STR, opt) = str.split(l,' ',2)
except IOError as e:
print 'error reading dump file'
print e
raise SystemExit(1)
except Exception as e:
print 'error parsing dump file'
raise SystemExit(1)
try:
dump.dump_options = eval(opt)
# set some options loaded from the dump file
options.IPDST = dump.dump_options['IPDST']
options.DST = dump.dump_options['DST']
options.plen = dump.dump_options['plen']
options.delta = dump.dump_options['delta']
options.tag = dump.dump_options['tag'] + options.tag
options.start_time = dump.dump_options['start_time']
except KeyError as ke:
pass
except SyntaxError as se:
print 'could not parse options'
line_count = 0
try:
while (line_count < options.pnum):
try:
l = fs.readline()
(s, snd_slot, rtt) = str.split(l)
seq = int(s)
dump.slottimes[seq] = int(snd_slot)
dump.rtts[seq] = float(rtt)
dump.rcv_order[line_count] = seq
except ValueError:
break
except IndexError:
# specified options.pnum is too small
break
line_count += 1
except KeyboardInterrupt:
pass # canceled reading file
fs.close()
dump.pprint()
if options.min_rtt==-1:
options.min_rtt = np.mean(dump.rtts)
return dump
if options.loaddump:
dump = dump_loader()
options.tag += '_offline'