forked from zdavkeos/addp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addp.py
255 lines (217 loc) · 8.52 KB
/
addp.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
#!/usr/bin/env python
"""
Advanced Device Discovery Protocol (ADDP)
http://www.digi.com/wiki/developer/index.php/Advanced_Device_Discovery_Protocol_(ADDP)
Almost all info for this module comes from:
http://qbeukes.blogspot.com/2009/11/advanced-digi-discovery-protocol_21.html
"""
import sys
import socket
import struct
typ_codes = {
0x0001: "Discovery Request",
0x0002: "Discovery Response",
0x0003: "Static Network Configuration Request",
0x0004: "Static Network Configuration Response",
0x0005: "Reboot Request",
0x0006: "Reboot Response",
0x0007: "DHCP Network Configuration Request",
0x0008: "DHCP Network Configuration Response"}
# {code: (desc, encoder, decoder)}
fld_codes = {
0x01: ("MAC address", lambda x: struct.pack("6B", *x), lambda x: struct.unpack("6B", x)),
0x02: ("IP address", lambda x: struct.pack("4B", *x), lambda x: struct.unpack("4B", x)),
0x03: ("Netmask", lambda x: struct.pack("4B", *x), lambda x: struct.unpack("4B", x)),
0x04: ("Network Name", lambda x: x, lambda x: x),
0x05: ("Domain", lambda x: x, lambda x: x),
0x06: ("HW Type", lambda x: struct.pack("B", x), lambda x: struct.unpack("B", x)[0]),
0x07: ("HW Revision", lambda x: struct.pack("B", x), lambda x: struct.unpack("B", x)[0]),
0x08: ("Firmware", lambda x: x, lambda x: x),
0x09: ("Result message", lambda x: x, lambda x: x),
0x0a: ("Result flag", lambda x: struct.pack("B", x), lambda x: struct.unpack("B", x)[0]),
0x0b: ("IP Gateway", lambda x: struct.pack("BBBB", *x), lambda x: struct.unpack("BBBB", x)),
0x0c: ("Configuration error code", lambda x: struct.pack(">H", x), lambda x: struct.unpack('>H', x)[0]),
0x0d: ("device name", lambda x: x, lambda x: x),
0x0e: ("Real Port number", lambda x: struct.pack(">L", x), lambda x: struct.unpack('>L', x)[0]),
0x0f: ("DNS IP address", lambda x: struct.pack("BBBB", *x), lambda x: struct.unpack("BBBB", x)),
0x10: ("UNKNOWN16", lambda x: struct.pack("BBBB", *x), lambda x: code_16_parser(x)),
0x11: ("Error code", lambda x: struct.pack("B", x), lambda x: error_codes[ord(x)]),
0x12: ("Serial Port Count", lambda x: struct.pack("B", x), lambda x: struct.unpack("B", x)[0]),
0x13: ("Encrypted Real Port number", lambda x: struct.pack(">L", x), lambda x: struct.unpack('>L', x)[0]),
0x19: ("UNKNOWN19", lambda x: struct.pack("B", x), lambda x: struct.unpack("B", x)[0]),
0x1a: ("Device-ID", lambda x: x, lambda x: "%08X-%08X-%08X-%08X"%struct.unpack('>4L', x))}
error_codes = {
0x00: "Success",
0x01: "Authentication Failure",
0x03: "Invalid Value",
0x06: "Unable to save value"}
def build_frame(typ, body):
return "DIGI".encode() + struct.pack('>HH', typ, len(body)) + body
def build_fields(flds):
body = ""
for c, v in flds.items():
val = fld_codes[c][1](v)
body += struct.pack("BB", c, len(val)) + val
return body
def parse_frame(d):
info = {}
if d[:4] != b'DIGI':
print('Invalid magic header:', repr(d[:4]))
return None
hdr = d[4:8]
bdy = d[8:]
(typ, ln) = struct.unpack(">HH", hdr)
if len(bdy) != ln:
print('Invalid format: lengths did not match:')
print('expected: %d, got: %d' % (ln, len(bdy)))
print(repr(d))
return None
if typ not in typ_codes:
print('Unknown message code: {}'.format(typ))
return None
info['code'] = typ
info['msg_len'] = ln
info['message'] = typ_codes[typ]
info['msg_type'] = 'request'
if typ == 0x01:
# discovery req
info['mac'] = struct.unpack("BBBBBB", bdy)
elif typ == 0x03:
# change ip req
info['ip_addr'] = struct.unpack("BBBB", bdy[:4])
info['subnet'] = struct.unpack("BBBB", bdy[4:8])
info['gatway'] = struct.unpack("BBBB", bdy[8:12])
info['mac'] = struct.unpack("BBBBBB", bdy[12:18])
info['auth'] = bdy[18:]
elif typ == 0x05:
# reboot req
info['mac'] = struct.unpack("BBBBBB", bdy[:6])
info['auth'] = bdy[6:]
elif typ == 0x07:
# dhcp req
#info['byte'] = struct.unpack("B", bdy[:1])
info['mac'] = struct.unpack("BBBBBB", bdy[1:7])
info['auth'] = bdy[7:]
elif typ in [0x02, 0x04, 0x06, 0x08]:
info['msg_type'] = 'response'
vals = parse_response(bdy)
info.update(vals)
return info
def build_request(typ, **kwargs):
if typ == 0x01:
# discover - requires mac
mac = kwargs['mac']
body = struct.pack("6B", *mac)
if typ == 0x03:
# static network configuration
mac = kwargs['mac']
auth = kwargs['auth']
ipaddr = kwargs['ipaddr']
subnet = kwargs['subnet']
gateway = kwargs['gateway']
body = struct.pack("4B", *ipaddr)
body += struct.pack("4B", *subnet)
body += struct.pack("4B", *gateway)
body += struct.pack("6B", *mac)
body += struct.pack("B", len(auth)) + auth
if typ == 0x05:
# reboot - requires mac, auth
mac = kwargs['mac']
auth = kwargs['auth']
body = struct.pack("6B", *mac)
body += struct.pack("B", len(auth)) + auth
if typ == 0x07:
# dhcp - requires mac, auth,
mac = kwargs['mac']
auth = kwargs['auth']
body = struct.pack("B", 1)
body += struct.pack("6B", *mac)
body += struct.pack("B", len(auth)) + auth
return build_frame(typ, body)
def build_response(info):
resp = None
if info['code'] == 0x01:
flds = {0x01: (0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
0x02: (1, 1, 1, 1),
0x03: (255, 255, 255, 0),
0x04: "test",
0x0b: (1, 1, 1, 1),
0x0d: "ADDP Emulator",
#0x10: 0,
0x07: 0,
0x08: "V.1 04-25-2013",
0x0e: 771,
0x13: 1027,
0x12: 1}
resp = build_frame(0x02, build_fields(flds))
elif info['code'] == 0x05:
flds = {0x01: (0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
0x09: "Operation FOO",
0x0a: 0,
0x11: 0}
resp = build_frame(0x06, build_fields(flds))
return resp
def parse_response(body):
info = {}
while body != b"":
code = body[0]
ln = body[1]
fld = body[2:ln+2]
body = body[ln+2:]
if code not in fld_codes:
# Skip Unrecognized Codes
continue
info[fld_codes[code][0]] = fld_codes[code][2](fld)
return info
def code_16_parser(x):
if len(x) == 1:
return ord(x)
elif len(x) == 4:
return struct.unpack("BBBB", x)
else:
return x
def discover_devices():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
# you can only use the REUSE options OR the bind, not both
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
pass
sock.bind(("", 2362))
sock.settimeout(1.0)
msg = build_request(0x01, mac=(255,255,255,255,255,255))
sock.sendto(msg, ("224.0.5.128", 2362))
responses = []
while True:
try:
data, addr = sock.recvfrom(2048)
except:
break
if data is None or data == '':
break
info = parse_frame(data)
if info:
info['addp_ip'] = addr[0]
responses.append(info)
sock.close()
return responses
if __name__ == '__main__':
print("{: ^16}|{: ^16}|{: ^16}".format("MAC Address","IP Address","Hardware"))
print("=================================================")
for device in discover_devices():
_mac = "{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}".format(
device['MAC address'][0],
device['MAC address'][1],
device['MAC address'][2],
device['MAC address'][3],
device['MAC address'][4],
device['MAC address'][5])
_ip = "{}.{}.{}.{}".format(
device['IP address'][0],
device['IP address'][1],
device['IP address'][2],
device['IP address'][3])
_name = device['device name'].decode()
print("{: ^16}|{: ^16}|{: ^16}".format(_mac,_ip,_name))