-
Notifications
You must be signed in to change notification settings - Fork 4
/
BGPDump.py
407 lines (266 loc) · 10.7 KB
/
BGPDump.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# -*- coding: utf-8 -*-
########################################################################################################################
# Copyright © 2019 Alex Forster. All rights reserved.
# This software is licensed under the 3-Clause ("New") BSD license.
# See the LICENSE file for details.
########################################################################################################################
import os
from datetime import datetime
from socket import inet_ntop, AF_INET, AF_INET6
from cffi import FFI
from pkg_resources import resource_filename
from .CDefs import CTypes, CConst
class BGPAttributes:
def __init__(self, bgp, attributes):
self.bgp = bgp
self.attributes = attributes
self._cluster = None
@property
def flag(self):
return self.attributes.flag
@property
def origin(self):
return self.attributes.origin
@property
def nexthop(self):
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.attributes.nexthop))[:])
@property
def med(self):
return self.attributes.med
@property
def localPref(self):
return self.attributes.local_pref
@property
def aggregatorAS(self):
return self.attributes.aggregator_as
@property
def aggregatorAddr(self):
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.attributes.aggregator_addr))[:])
@property
def weight(self):
return self.attributes.weight
@property
def originatorID(self):
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.attributes.originator_id))[:])
@property
def cluster(self):
if self._cluster is not None:
return self._cluster
self._cluster = []
for i in range(0, self.attributes.cluster.length):
self._cluster.append(
inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.attributes.cluster.list[i]))[:]))
return self._cluster
@property
def asPath(self):
return self.bgp.ffi.string(self.attributes.aspath.str).decode() if self.attributes.aspath != self.bgp.ffi.NULL else None
@property
def community(self):
return self.bgp.ffi.string(self.attributes.community.str).decode() if self.attributes.community != self.bgp.ffi.NULL else None
@property
def ecommunity(self):
return self.bgp.ffi.string(self.attributes.ecommunity.str).decode() if self.attributes.ecommunity != self.bgp.ffi.NULL else None
@property
def transit(self):
return self.attributes.transit
@property
def newASPath(self):
return self.bgp.ffi.string(self.attributes.new_aspath.str).decode() if self.attributes.new_aspath != self.bgp.ffi.NULL else None
@property
def oldASPath(self):
return self.bgp.ffi.string(self.attributes.old_aspath.str).decode() if self.attributes.old_aspath != self.bgp.ffi.NULL else None
@property
def newAggregatorAS(self):
return self.attributes.new_aggregator_as
@property
def oldAggregatorAS(self):
return self.attributes.old_aggregator_as
@property
def newAggregatorAddr(self):
return self.attributes.new_aggregator_addr
@property
def oldAggregatorAddr(self):
return self.attributes.old_aggregator_addr
class TableDumpV1:
def __init__(self, bgp, subtype, body):
self.bgp = bgp
self.subtype = subtype
self.body = body
@property
def view(self):
return self.body.view
@property
def sequence(self):
return self.body.sequence
@property
def prefix(self):
if self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP \
or self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.body.prefix.v4_addr))[:])
if self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6 \
or self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
return inet_ntop(AF_INET6, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.body.prefix.v6_addr))[:])
@property
def mask(self):
return self.body.mask
@property
def status(self):
return self.body.status
@property
def uptime(self):
return datetime.fromtimestamp(self.body.uptime)
@property
def peerIP(self):
if self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP \
or self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP_32BIT_AS:
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.body.prefix.v4_addr))[:])
if self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6 \
or self.subtype == CConst.BGPDUMP_SUBTYPE_MRTD_TABLE_DUMP_AFI_IP6_32BIT_AS:
return inet_ntop(AF_INET6, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.body.prefix.v6_addr))[:])
@property
def peerAS(self):
return self.body.peer_as
@property
def attrLength(self):
return self.body.attr_len
class TableDumpV2PeerIndexTableEntry:
def __init__(self, bgp, entry):
self.bgp = bgp
self.entry = entry
@property
def afi(self):
return AF_INET if self.entry.afi == CConst.AFI_IP else AF_INET6 if self.entry.afi == CConst.AFI_IP6 else None
@property
def peerIP(self):
if self.afi == AF_INET:
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.entry.peer_ip.v4_addr))[:])
if self.afi == AF_INET6:
return inet_ntop(AF_INET6, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.entry.peer_ip.v6_addr))[:])
@property
def peerRouterID(self):
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.entry.peer_bgp_id))[:])
@property
def peerAS(self):
return self.entry.peer_as
class TableDumpV2RouteEntry:
def __init__(self, bgp, entry):
self.bgp = bgp
self.entry = entry
self._attr = None
@property
def peerIndex(self):
return self.entry.peer_index
@property
def originatedTime(self):
return datetime.fromtimestamp(self.entry.originated_time)
@property
def peer(self):
return TableDumpV2PeerIndexTableEntry(self.bgp, self.entry.peer)
@property
def attr(self):
if self._attr is not None:
return self._attr
self._attr = BGPAttributes(self.bgp, self.entry.attr) if self.entry.attr != self.bgp.ffi.NULL else None
return self._attr
class TableDumpV2:
def __init__(self, bgp, subtype, body):
self.bgp = bgp
self.subtype = subtype
self.body = body
self._routeEntries = None
@property
def seq(self):
return self.body.seq
@property
def afi(self):
return AF_INET if self.body.afi == CConst.AFI_IP else AF_INET6 if self.body.afi == CConst.AFI_IP6 else None
@property
def safi(self):
return AF_INET if self.body.safi == CConst.AFI_IP else AF_INET6 if self.body.safi == CConst.AFI_IP6 else None
@property
def prefixLength(self):
return self.body.prefix_length
@property
def prefix(self):
if self.afi == AF_INET:
return inet_ntop(AF_INET, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.body.prefix.v4_addr))[:])
if self.afi == AF_INET6:
return inet_ntop(AF_INET6, self.bgp.ffi.buffer(self.bgp.ffi.addressof(self.body.prefix.v6_addr))[:])
@property
def routeEntries(self):
if self._routeEntries is not None:
return self._routeEntries
self._routeEntries = []
for i in range(0, self.body.entry_count):
self._routeEntries.append(TableDumpV2RouteEntry(self.bgp, self.body.entries[i]))
return self._routeEntries
class BGPEntry:
def __init__(self, bgp, entry):
self.bgp = bgp
self.entry = entry
self._body = None
self._attr = None
@property
def time(self):
return datetime.fromtimestamp(self.entry.time)
@property
def length(self):
return self.entry.length
@property
def attr(self):
if self._attr is not None:
return self._attr
self._attr = BGPAttributes(self.bgp, self.entry.attr) if self.entry.attr != self.bgp.ffi.NULL else None
return self._attr
@property
def body(self):
if self._body is not None:
return self._body
if self.entry.type == CConst.BGPDUMP_TYPE_MRTD_TABLE_DUMP:
self._body = TableDumpV1(self.bgp, self.entry.subtype, self.entry.body.mrtd_table_dump)
if self.entry.type == CConst.BGPDUMP_TYPE_TABLE_DUMP_V2:
if self.entry.subtype == CConst.BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV4_UNICAST \
or self.entry.subtype == CConst.BGPDUMP_SUBTYPE_TABLE_DUMP_V2_RIB_IPV6_UNICAST:
self._body = TableDumpV2(self.bgp, self.entry.subtype, self.entry.body.mrtd_table_dump_v2_prefix)
return self._body
class BGPDump:
def __init__(self, filename, libbgpdump_path=None):
self.filename = filename
self.libbgpdump_path = libbgpdump_path
self.ffi = None
self.libc = None
self.libbgpdump = None
self.handle = None
def __enter__(self):
self.ffi = FFI()
libdir = resource_filename('bgpdumpy', 'lib') or ''
self.libc = self.ffi.dlopen(None)
if self.libbgpdump_path is not None:
self.libbgpdump = self.ffi.dlopen(os.path.abspath(os.path.expanduser(self.libbgpdump_path)))
else:
self.libbgpdump = self.ffi.dlopen(os.path.join(libdir, 'libbgpdump.so'))
self.ffi.cdef(CTypes)
self.handle = self.libbgpdump.bgpdump_open_dump(str(self.filename).encode())
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.libbgpdump.bgpdump_close_dump(self.handle)
del self.libbgpdump
self.libbgpdump = None
del self.libc
self.libc = None
del self.ffi
self.ffi = None
def __iter__(self):
while True:
if self.handle.eof == 1:
break
currentEntry = self.libbgpdump.bgpdump_read_next(self.handle)
if currentEntry == self.ffi.NULL:
continue # sometimes we get NULL back for no apparent reason
try:
yield BGPEntry(self, currentEntry)
finally:
self.libbgpdump.bgpdump_free_mem(currentEntry)
@property
def version(self):
return self.ffi.string(self.libbgpdump.bgpdump_version()).decode()