-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWhoIsIAmDeviceFilter.py
executable file
·191 lines (164 loc) · 5.99 KB
/
WhoIsIAmDeviceFilter.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
#!/usr/bin/python
"""
Given a device identifier and a list of PCAP files, this application prints
a summary line of Who-Is packets such that the device should respond, and
the I-Am packets that are sent by the device. This is useful for repeated
attempts to 'bind' where it fails, or where more than one device responds to
the request (which is bad) or the same device responds but it comes from
changing source addresses (which is really bad).
This application accepts the same --source, --destination, and --host options
as the other filters, and accepts the debugging options of other BACpypes
applications.
"""
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ArgumentParser
from bacpypes.pdu import Address
from bacpypes.analysis import trace, strftimestamp, Tracer
from bacpypes.apdu import WhoIsRequest, IAmRequest
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# globals
filterSource = None
filterDestination = None
filterHost = None
filterDevice = None
#
# Match
#
@bacpypes_debugging
def Match(addr1, addr2):
"""Return true iff addr1 matches addr2."""
if _debug:
Match._debug("Match %r %r", addr1, addr2)
if addr2.addrType == Address.localBroadcastAddr:
# match any local station
return (addr1.addrType == Address.localStationAddr) or (
addr1.addrType == Address.localBroadcastAddr
)
elif addr2.addrType == Address.localStationAddr:
# match a specific local station
return (addr1.addrType == Address.localStationAddr) and (
addr1.addrAddr == addr2.addrAddr
)
elif addr2.addrType == Address.remoteBroadcastAddr:
# match any remote station or remote broadcast on a matching network
return (
(addr1.addrType == Address.remoteStationAddr)
or (addr1.addrType == Address.remoteBroadcastAddr)
) and (addr1.addrNet == addr2.addrNet)
elif addr2.addrType == Address.remoteStationAddr:
# match a specific remote station
return (
(addr1.addrType == Address.remoteStationAddr)
and (addr1.addrNet == addr2.addrNet)
and (addr1.addrAddr == addr2.addrAddr)
)
elif addr2.addrType == Address.globalBroadcastAddr:
# match a global broadcast address
return addr1.addrType == Address.globalBroadcastAddr
else:
raise RuntimeError("invalid match combination")
#
# WhoIsIAmDevice
#
@bacpypes_debugging
class WhoIsIAmDevice(Tracer):
def __init__(self):
if _debug:
WhoIsIAmDevice._debug("__init__")
Tracer.__init__(self, self.Filter)
def Filter(self, pkt):
if _debug:
WhoIsIAmDevice._debug("Filter %r", pkt)
global requests
# apply the filters
if filterSource:
if not Match(pkt.pduSource, filterSource):
if _debug:
WhoIsIAmDevice._debug(" - source filter fail")
return
if filterDestination:
if not Match(pkt.pduDestination, filterDestination):
if _debug:
WhoIsIAmDevice._debug(" - destination filter fail")
return
if filterHost:
if (not Match(pkt.pduSource, filterHost)) and (
not Match(pkt.pduDestination, filterHost)
):
if _debug:
WhoIsIAmDevice._debug(" - host filter fail")
return
# check for Who-Is
if isinstance(pkt, WhoIsRequest):
match = False
if (pkt.deviceInstanceRangeLowLimit is None) or (
pkt.deviceInstanceRangeHighLimit is None
):
match = True
elif (pkt.deviceInstanceRangeLowLimit >= filterDevice) and (
pkt.deviceInstanceRangeHighLimit <= filterDevice
):
match = True
if match:
print(
"[%d] %s WhoIs %-20s %-20s %8s %8s"
% (
pkt._number,
strftimestamp(pkt._timestamp),
pkt.pduSource,
pkt.pduDestination,
pkt.deviceInstanceRangeLowLimit,
pkt.deviceInstanceRangeHighLimit,
)
)
# check for I-Am
elif isinstance(pkt, IAmRequest):
if pkt.iAmDeviceIdentifier[1] == filterDevice:
print(
"[%d] %s IAm %-20s %-20s"
% (
pkt._number,
strftimestamp(pkt._timestamp),
pkt.pduSource,
pkt.pduDestination,
)
)
#
# __main__
#
# parse the command line arguments
parser = ArgumentParser(description=__doc__)
parser.add_argument("-s", "--source", nargs="?", type=str, help="source address")
parser.add_argument(
"-d", "--destination", nargs="?", type=str, help="destination address"
)
parser.add_argument("--host", nargs="?", type=str, help="source or destination")
parser.add_argument("device", nargs=1, type=int, help="device identifier")
parser.add_argument("pcap", nargs="+", type=str, help="pcap file(s)")
args = parser.parse_args()
if _debug:
_log.debug("initialization")
if _debug:
_log.debug(" - args: %r", args)
# interpret the arguments
if args.source:
filterSource = Address(args.source)
if _debug:
_log.debug(" - filterSource: %r", filterSource)
if args.destination:
filterDestination = Address(args.destination)
if _debug:
_log.debug(" - filterDestination: %r", filterDestination)
if args.host:
filterHost = Address(args.host)
if _debug:
_log.debug(" - filterHost: %r", filterHost)
# which device instance to look for
filterDevice = args.device[0]
if _debug:
_log.debug(" - filterDevice: %r", filterDevice)
# trace the file(s)
for fname in args.pcap:
trace(fname, [WhoIsIAmDevice])