forked from etdey/gdl90
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdl90_recorder.py
291 lines (225 loc) · 9.82 KB
/
gdl90_recorder.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
#
"""GDL-90 Recorder
This program records RAW output from the GDL-90 data link interface.
This depends upon the 'netifaces' package in order to get information about
the hosts network interfaces. It is installed with:
# apt-get install python-pip
# pip install netifaces
"""
__progTitle__ = "GDL-90 Recorder"
__author__ = "Eric Dey <[email protected]>"
__created__ = "September 2012"
__copyright__ = "Copyright (C) 2018 by Eric Dey"
__version__ = "1.3"
__date__ = "16-NOV-2018"
import os, sys, time, datetime, re, optparse, socket, struct, threading
try:
import netifaces
except ImportError:
sys.stderr.write("ERROR: could not import 'netifaces' package; use 'pip install netifaces' to add it/n")
sys.exit(1)
# Default values for options
DEF_RECV_PORT=43211
DEF_RECV_MAXSIZE=1500
DEF_DATA_FLUSH_SECS=10
DEF_LOG_PREFIX="/root/skyradar"
SLOWEXIT_DELAY=15
# Exit codes
EXIT_CODE = {
"OK" : 0,
"OPTIONS" : 1,
"OTHER" : 99,
}
def print_error(msg):
"""print an error message"""
print >> sys.stderr, msg
def _isNumeric(n):
"""test if 'n' can be converted for use in numeric calculations"""
try:
b = float(n)
return True
except:
pass
return False
def _options_okay(options):
"""test to see if options are valid"""
errors = False
if int(options.port) <=0 or int(options.port) >=65536:
errors = True
print_error("Argument '--port' must between 1 and 65535")
if options.interface == '':
# this means use all interfaces
pass
elif _getAddressByIfaceName(options.interface) is None:
errors = True
print_error("Argument '--interface' is not a valid interface name")
else:
try:
netifaces.ifaddresses(options.interface)[netifaces.AF_INET][0]
except KeyError:
errors = True
print_error("Receive interface does not have an IP address")
if options.rebroadcast != "":
if options.interface == '':
errors = True
print_error("Argument '--interface' cannot by all when using --rebroadcast option")
if _getAddressByIfaceName(options.rebroadcast) is None:
print_error("Argument '--rebroadcast' is not a valid interface name; disabling rebroadcast")
options.rebroadcast = ""
elif options.interface == options.rebroadcast:
print_error("Receive interface must be different from rebroadcast interface; disabling rebroadcast")
options.rebroadcast = ""
else:
try:
netifaces.ifaddresses(options.rebroadcast)[netifaces.AF_INET][0]
except KeyError:
print_error("Rebroadcast interface does not have an IP address; disabling rebroadcast")
options.rebroadcast = ""
return not errors
def _get_progVersion():
"""return program version string"""
return "%s" % (__version__)
def _getTimeStamp():
"""create a time stamp string"""
return datetime.datetime.utcnow().isoformat(' ')
def _extractSvnKeywordValue(s):
"""Extracts the value string from an SVN keyword property string."""
if re.match(r'^\$[^:]*\$$', s):
return ""
return re.sub(r'^\$[^:]*: (.*)\$$', r'\1', s).strip(' ')
def _nextFileName(dirName, baseName='gdl90_cap', fmt=r'%s/%s.%03d'):
if not os.path.isdir(dirName):
Exception("Directory %s does not exist" % (dirName))
i = 0
while i < 1000:
fname = fmt % (dirName, baseName, i)
if not os.path.exists(fname):
return fname
i += 1
raise Exception("Search exhausted; too many files exist already.")
def _getAddressByIfaceName(ifname, broadcast=False):
"""return an IP address for a named interface
Only the first IP address is returned if multiple exist.
@ifname: interface name string
@broadcast: return network broadcast address instead of interface addr
@return: IP address string or None if error
"""
if ifname == '':
return ''
if not ifname in netifaces.interfaces():
return None
try:
ifdetails = netifaces.ifaddresses(ifname)[netifaces.AF_INET][0]
except KeyError:
return None
if broadcast:
return ifdetails['broadcast']
return ifdetails['addr']
def _record(options):
"""record packets"""
logFile = None
logFileName = _nextFileName(options.logprefix)
if options.verbose == True:
print_error("will use log file name '%s'" % (logFileName))
try:
if options.subnetbcast:
listenIP = netifaces.ifaddresses(options.interface)[netifaces.AF_INET][0]['broadcast']
elif options.bcast:
listenIP = '<broadcast>'
else:
listenIP = ''
except KeyError as e:
sys.stderr.write("ERROR: error getting network details for '%s' %s\n" % (options.interface,e))
sys.exit(1)
sockIn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockIn.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sockIn.bind((listenIP, options.port))
packetTotal = 0
bytesTotal = 0
lastFlushTime = time.time()
if options.verbose == True:
print_error("Listening on interface '%s' at address '%s' port '%s'" % (options.interface, listenIP, options.port))
sockOut = None
if options.rebroadcast != "":
sockOut = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockOut.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sockOutSendToAddr = _getAddressByIfaceName(options.rebroadcast, broadcast=True)
if options.verbose == True:
print_error("Rebroadcasting on interface ''%s' at address '%s' port '%s'" % (options.rebroadcast, _getAddressByIfaceName(options.rebroadcast), options.port))
try:
while True:
(data, dataSrc) = sockIn.recvfrom(DEF_RECV_MAXSIZE)
(saddr, sport) = dataSrc
packetTotal += 1
bytesTotal += len(data)
#optionally rebroadcast onto another network
if sockOut is not None:
sockOut.sendto(data, (sockOutSendToAddr, options.port))
# Create log file only when the first bytes arrive
if logFile is None:
logFile = open(logFileName, "wb")
if options.verbose == True:
print_error("created log file '%s'" %(logFileName))
logFile.write(data)
# Ensure periodic flush to disk
if int(time.time() - lastFlushTime) > options.dataflush:
logFile.flush()
os.fsync(logFile.fileno())
lastFlushTime = time.time()
if options.verbose == True:
print_error("[%s] disk flush" %(lastFlushTime))
except Exception, e:
print e
if logFile: logFile.close()
sockIn.close()
if sockOut is not None:
sockOut.close()
print "Recorded %d packets and %d bytes." % (packetTotal, bytesTotal)
# Interactive Runs
if __name__ == '__main__':
# get default network interface device
try:
def_interface = netifaces.interfaces()[1]
except IndexError:
def_interface = netifaces.interfaces()[0] # loopback device
# Get name of program from command line or else use embedded default
progName = os.path.basename(sys.argv[0])
# Create other program tags from SVN strings
progDate = _extractSvnKeywordValue(__date__)
progVersion = _get_progVersion()
#
# Setup option parsing
#
usageMsg = "usage: %s [options]" % (progName)
versionMsg = "%s version %s (%s)" % (progName, progVersion, progDate)
descriptionMsg = __progTitle__ + """ is a data receiver."""
epilogMsg = """"""
optParser = optparse.OptionParser(usage=usageMsg,
version=versionMsg,
description=descriptionMsg,
epilog=epilogMsg)
# add options outside of any option group
optParser.add_option("--verbose", "-v", action="store_true", help="Verbose reporting on STDERR")
optParser.add_option("--slowexit", action="store_true", help="Delay error exit for %s seconds" % (SLOWEXIT_DELAY))
# optional options
group = optparse.OptionGroup(optParser,"Optional")
group.add_option("--interface", action="store", default=def_interface, metavar="name", help="receive interface name (default=%default)")
group.add_option("--port","-p", action="store", default=DEF_RECV_PORT, type="int", metavar="NUM", help="receive port (default=%default)")
group.add_option("--maxsize","-s", action="store", default=DEF_RECV_MAXSIZE, type="int", metavar="BYTES", help="maximum packet size (default=%default)")
group.add_option("--dataflush", action="store", default=DEF_DATA_FLUSH_SECS, type="int", metavar="SECS", help="seconds between data file flush (default=%default)")
group.add_option("--logprefix", action="store", default=DEF_LOG_PREFIX, metavar="PATH", help="path prefix for log file names (default=%default)")
group.add_option("--rebroadcast", action="store", default="", metavar="name", help="rebroadcast interface (default=off)")
group.add_option("--bcast", action="store_true", help="listen on 255.255.255.255")
group.add_option("--subnetbcast", action="store_true", help="listen on subnet broadcast")
optParser.add_option_group(group)
# do the option parsing
(options, args) = optParser.parse_args(args=sys.argv[1:])
# check options
if not _options_okay(options):
print_error("Stopping due to option errors.")
if options.slowexit == True:
print_error(" ... pausing for %s seconds before exit." % (SLOWEXIT_DELAY))
time.sleep(SLOWEXIT_DELAY)
sys.exit(EXIT_CODE['OPTIONS'])
_record(options)