-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathvlan_to_vlan.py
executable file
·361 lines (276 loc) · 9.81 KB
/
vlan_to_vlan.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env python
"""
VLAN to VLAN
This application started out being a way to test various combinations of
traffic before the tests were written. All of the traffic patterns in this
application are in the test suite but this is simpler.
It doesn't generate any output, turn on debugging to see what each node is
sending (the request() calls) and receiving (the indication() calls).
$ python vlan_to_vlan.py 1 --debug __main__.VLANApplication
Note how the source and destination addresses change as packets go through
routers.
"""
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ArgumentParser
from bacpypes.core import run, deferred
from bacpypes.comm import bind
from bacpypes.pdu import Address, LocalBroadcast
from bacpypes.netservice import NetworkServiceAccessPoint, NetworkServiceElement
from bacpypes.app import Application
from bacpypes.appservice import StateMachineAccessPoint, ApplicationServiceAccessPoint
from bacpypes.service.device import WhoIsIAmServices
from bacpypes.service.object import ReadWritePropertyServices
from bacpypes.local.device import LocalDeviceObject
from bacpypes.apdu import ReadPropertyRequest
from bacpypes.vlan import Network, Node
# some debugging
_debug = 0
_log = ModuleLogger(globals())
#
# VLANApplication
#
@bacpypes_debugging
class VLANApplication(Application, WhoIsIAmServices, ReadWritePropertyServices):
def __init__(self, objectName, deviceInstance, address, aseID=None):
if _debug:
VLANApplication._debug(
"__init__ %r %r %r aseID=%r", objectName, deviceInstance, address, aseID
)
# make an address
vlan_address = Address(address)
_log.debug(" - vlan_address: %r", vlan_address)
# make a device object
vlan_device = LocalDeviceObject(
objectName=objectName,
objectIdentifier=("device", deviceInstance),
maxApduLengthAccepted=1024,
segmentationSupported="noSegmentation",
vendorIdentifier=15,
)
_log.debug(" - vlan_device: %r", vlan_device)
# continue with the initialization
Application.__init__(self, vlan_device, vlan_address, aseID)
# include a application decoder
self.asap = ApplicationServiceAccessPoint()
# pass the device object to the state machine access point so it
# can know if it should support segmentation
self.smap = StateMachineAccessPoint(vlan_device)
# the segmentation state machines need access to the same device
# information cache as the application
self.smap.deviceInfoCache = self.deviceInfoCache
# a network service access point will be needed
self.nsap = NetworkServiceAccessPoint()
# give the NSAP a generic network layer service element
self.nse = NetworkServiceElement()
bind(self.nse, self.nsap)
# bind the top layers
bind(self, self.asap, self.smap, self.nsap)
# create a vlan node at the assigned address
self.vlan_node = Node(vlan_address)
if _debug:
VLANApplication._debug(" - vlan_node: %r", self.vlan_node)
# bind the stack to the node, no network number
self.nsap.bind(self.vlan_node)
if _debug:
VLANApplication._debug(" - node bound")
def request(self, apdu):
if _debug:
VLANApplication._debug("[%s]request %r", self.localDevice.objectName, apdu)
Application.request(self, apdu)
def indication(self, apdu):
if _debug:
VLANApplication._debug(
"[%s]indication %r", self.localDevice.objectName, apdu
)
Application.indication(self, apdu)
def response(self, apdu):
if _debug:
VLANApplication._debug("[%s]response %r", self.localDevice.objectName, apdu)
Application.response(self, apdu)
def confirmation(self, apdu):
if _debug:
VLANApplication._debug(
"[%s]confirmation %r", self.localDevice.objectName, apdu
)
#
# VLANRouter
#
@bacpypes_debugging
class VLANRouter:
def __init__(self):
if _debug:
VLANRouter._debug("__init__")
# a network service access point will be needed
self.nsap = NetworkServiceAccessPoint()
# give the NSAP a generic network layer service element
self.nse = NetworkServiceElement()
bind(self.nse, self.nsap)
def bind(self, vlan, address, net):
if _debug:
VLANRouter._debug("bind %r %r %r", vlan, address, net)
# create a VLAN node for the router with the given address
vlan_node = Node(Address(address))
# add it to the VLAN
vlan.add_node(vlan_node)
# bind the router stack to the vlan network through this node
self.nsap.bind(vlan_node, net)
if _debug:
_log.debug(" - bound to vlan")
def main():
# parse the command line arguments
parser = ArgumentParser(description=__doc__)
# add an argument for which test to run
parser.add_argument("test_id", type=int, help="test number")
# now parse the arguments
args = parser.parse_args()
if _debug:
_log.debug("initialization")
if _debug:
_log.debug(" - args: %r", args)
# VLAN needs to know what a broadcast address looks like
vlan_broadcast_address = LocalBroadcast()
#
# Router1
#
# create the router
router1 = VLANRouter()
if _debug:
_log.debug(" - router1: %r", router1)
#
# VLAN-1
#
# create VLAN-1
vlan1 = Network(name="1", broadcast_address=vlan_broadcast_address)
if _debug:
_log.debug(" - vlan1: %r", vlan1)
# bind the router to the vlan
router1.bind(vlan1, 1, 1)
if _debug:
_log.debug(" - router1 bound to VLAN-1")
# make the application, add it to the network
vlan1_app = VLANApplication(
objectName="VLAN Node 102", deviceInstance=102, address=2
)
vlan1.add_node(vlan1_app.vlan_node)
_log.debug(" - vlan1_app: %r", vlan1_app)
#
# VLAN-2
#
# create VLAN-2
vlan2 = Network(name="2", broadcast_address=vlan_broadcast_address)
if _debug:
_log.debug(" - vlan2: %r", vlan2)
# bind the router stack to the vlan network through this node
router1.bind(vlan2, 1, 2)
if _debug:
_log.debug(" - router1 bound to VLAN-2")
# make the application, add it to the network
vlan2_app = VLANApplication(
objectName="VLAN Node 202", deviceInstance=202, address=2
)
vlan2.add_node(vlan2_app.vlan_node)
_log.debug(" - vlan2_app: %r", vlan2_app)
#
# VLAN-3
#
# create VLAN-3
vlan3 = Network(name="3", broadcast_address=vlan_broadcast_address)
if _debug:
_log.debug(" - vlan3: %r", vlan3)
# bind the router stack to the vlan network through this node
router1.bind(vlan3, 1, 3)
if _debug:
_log.debug(" - router1 bound to VLAN-3")
# make a vlan device object
vlan3_device = LocalDeviceObject(
objectName="VLAN Node 302",
objectIdentifier=("device", 302),
maxApduLengthAccepted=1024,
segmentationSupported="noSegmentation",
vendorIdentifier=15,
)
_log.debug(" - vlan3_device: %r", vlan3_device)
# make the application, add it to the network
vlan3_app = VLANApplication(
objectName="VLAN Node 302", deviceInstance=302, address=2
)
vlan3.add_node(vlan3_app.vlan_node)
_log.debug(" - vlan3_app: %r", vlan3_app)
#
# Router2
#
# create the router
router2 = VLANRouter()
if _debug:
_log.debug(" - router2: %r", router2)
# bind the router stack to the vlan network through this node
router2.bind(vlan3, 255, 3)
if _debug:
_log.debug(" - router2 bound to VLAN-3")
#
# VLAN-4
#
# create VLAN-4
vlan4 = Network(name="4", broadcast_address=vlan_broadcast_address)
if _debug:
_log.debug(" - vlan4: %r", vlan4)
# bind the router stack to the vlan network through this node
router2.bind(vlan4, 1, 4)
if _debug:
_log.debug(" - router2 bound to VLAN-4")
# make the application, add it to the network
vlan4_app = VLANApplication(
objectName="VLAN Node 402", deviceInstance=402, address=2
)
vlan4.add_node(vlan4_app.vlan_node)
_log.debug(" - vlan4_app: %r", vlan4_app)
#
# Test 1
#
if args.test_id == 1:
# ask the first device to Who-Is everybody
deferred(vlan1_app.who_is)
#
# Test 2
#
if args.test_id == 2:
# make a read request
read_property_request = ReadPropertyRequest(
destination=Address("2:2"),
objectIdentifier=("device", 202),
propertyIdentifier="objectName",
)
# ask the first device to send it
deferred(vlan1_app.request, read_property_request)
#
# Test 3
#
if args.test_id == 3:
# make a read request
read_property_request = ReadPropertyRequest(
destination=Address("3:2"),
objectIdentifier=("device", 302),
propertyIdentifier="objectName",
)
# ask the first device to send it
deferred(vlan1_app.request, read_property_request)
#
# Test 4
#
if args.test_id == 4:
# make a read request
read_property_request = ReadPropertyRequest(
destination=Address("4:2"),
objectIdentifier=("device", 402),
propertyIdentifier="objectName",
)
# ask the first device to send it
deferred(vlan1_app.request, read_property_request)
#
# Let the test run
#
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()