-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaddp.py
194 lines (167 loc) · 5.95 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
#!/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 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" + 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] != '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:', 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 = dict(info.items() + vals.items())
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 != "":
code = ord(body[0])
ln = ord(body[1])
fld = body[2:ln+2]
body = body[ln+2:]
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