My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
#!/usr/bin/env python

import gzip, bz2
import dpkt

BZ2_MAGIC = '\x42\x5a\x68'
GZIP_MAGIC = dpkt.gzip.GZIP_MAGIC
MRT_HEADER_LEN = dpkt.mrt.MRTHeader.__hdr_len__
SUPPORTED_AFIS = ( dpkt.mrt.AFI_IPv4, )
SUPPORTED_TYPES = ( dpkt.bgp.UPDATE, )

class BGPDump:
def __init__(self, filename):
f = file(filename, 'rb')
hdr = f.read(max(len(BZ2_MAGIC), len(GZIP_MAGIC)))
f.close()

if filename.endswith('.bz2') and hdr.startswith(BZ2_MAGIC):
self.fobj = bz2.BZ2File
elif filename.endswith('.gz') and hdr.startswith(GZIP_MAGIC):
self.fobj = gzip.GzipFile
else:
self.fobj = file
self.open(filename)

def open(self, filename):
self.f = self.fobj(filename, 'rb')

def close(self):
self.f.close()
raise StopIteration

def __iter__(self):
return self

def next(self):
while True:
s = self.f.read(MRT_HEADER_LEN)
if len(s) < MRT_HEADER_LEN:
self.close()

mrt_h = dpkt.mrt.MRTHeader(s)
s = self.f.read(mrt_h.len)
if len(s) < mrt_h.len:
self.close()

if mrt_h.type != dpkt.mrt.BGP4MP:
continue

if mrt_h.subtype == dpkt.mrt.BGP4MP_MESSAGE:
bgp_h = dpkt.mrt.BGP4MPMessage(s)
elif mrt_h.subtype == dpkt.mrt.BGP4MP_MESSAGE_32BIT_AS:
bgp_h = dpkt.mrt.BGP4MPMessage_32(s)
else:
continue

if bgp_h.family not in SUPPORTED_AFIS:
continue

bgp_m = dpkt.bgp.BGP(bgp_h.data)
if bgp_m.type not in SUPPORTED_TYPES:
continue
if bgp_m.marker != '\xff' * 16:
continue
break
return (mrt_h, bgp_h, bgp_m)

Change log

r8 by jon.oberheide on Jan 15, 2007   Diff
release 0.2
Go to: 
Project members, sign in to write a code review

Older revisions

r4 by jon.oberheide on Sep 20, 2006   Diff
minor fixups before release
r2 by jon.oberheide on Sep 20, 2006   Diff
initial import (version 0.1)
All revisions of this file

File info

Size: 1843 bytes, 66 lines

File properties

svn:executable
*
Powered by Google Project Hosting