From 0f7661da0ef92c9673e4555c6a9a601afb118d6e Mon Sep 17 00:00:00 2001 From: Kiran Bandla Date: Wed, 6 Jun 2012 14:53:43 -0400 Subject: [PATCH] patched with Mattia Rossi's code from MDFMT --- dpkt/mrt.py | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/dpkt/mrt.py b/dpkt/mrt.py index 9f3c719..d3809c0 100644 --- a/dpkt/mrt.py +++ b/dpkt/mrt.py @@ -1,9 +1,13 @@ # $Id: mrt.py 29 2007-01-26 02:29:07Z jon.oberheide $ +# Patched with Mattia Rossi's code from MDFMT: +# http://caia.swin.edu.au/urp/bgp/tools.html """Multi-threaded Routing Toolkit.""" import dpkt import bgp +import struct +import socket # Multi-threaded Routing Toolkit # http://www.ietf.org/internet-drafts/draft-ietf-grow-mrt-03.txt @@ -22,6 +26,7 @@ BGP4PLUS = 9 # Deprecated by BGP4MP BGP4PLUS_01 = 10 # Deprecated by BGP4MP OSPF = 11 TABLE_DUMP = 12 +TABLE_DUMP_V2 = 13 BGP4MP = 16 BGP4MP_ET = 17 ISIS = 32 @@ -39,6 +44,14 @@ BGP4MP_MESSAGE_32BIT_AS = 4 AFI_IPv4 = 1 AFI_IPv6 = 2 +# TableDump v2 Subtypes +TABLE_DUMP_V2_PEER_INDEX_TABLE = 1 +TABLE_DUMP_V2_RIB_IPV4_UNICAST = 2 +TABLE_DUMP_V2_RIB_IPV4_MULTICAST = 3 +TABLE_DUMP_V2_RIB_IPV6_UNICAST = 4 +TABLE_DUMP_V2_RIB_IPV6_MULTICAST = 5 +TABLE_DUMP_V2_RIB_GENERIC = 6 + class MRTHeader(dpkt.Packet): __hdr__ = ( ('ts', 'I', 0), @@ -71,6 +84,154 @@ class TableDump(dpkt.Packet): l.append(attr) self.attributes = l +class TableDump2_PeerIndex(dpkt.Packet): + __hdr__ = ( + ('id', 'I', 0), + ('viewname_len', 'H', 0), + ) + + __hdr_defaults__ = { + 'view_name' : 0, + 'peer_count' : 0, + 'peers' : [] + } + + def unpack(self, buf): + dpkt.Packet.unpack(self, buf) + vlen = self.viewname_len + self.view_name = self.data[:vlen] + pcount = self.peer_count = struct.unpack('>H', \ + self.data[vlen:vlen + 2])[0] + l = [] + for i in range(pcount): + peer = self.Peer(self.data[vlen + 2:]) + self.data = self.data[len(peer):] + l.append(peer) + self.data = self.peers = l + + class Peer(dpkt.Packet): + + __hdr__ = ( + ('type', 'B', 0), + ('id', 'I', 0) + ) + + __hdr_defaults__ = { + 'address': 0, + 'asn': 0, + } + + def unpack(self, buf): + self.len=0 + dpkt.Packet.unpack(self, buf) + if (self.type >> 0) & 0x01 : + self.address = self.data[:16] + self.data = self.data[16:] + self.len += 16 + else: + self.address = socket.inet_ntoa(self.data[:4]) + self.data = self.data[4:] + self.len += 4 + if (self.type >> 1) & 0x01 : + self.asn = struct.unpack('>L', \ + self.data[:4])[0] + self.len += 4 + else : + self.asn = struct.unpack('>H', \ + self.data[:2])[0] + self.len += 2 + self.data='' + + def __len__(self): + return self.__hdr_len__ + \ + self.len + +class TableDump2_IPV4(dpkt.Packet): + __hdr__ = ( + ('seq','I',0), + ) + + __hdr_defaults__ = { + 'ribentry' : [] + } + + def unpack(self,buf): + dpkt.Packet.unpack(self, buf) + pre = bgp.RouteIPV4(self.data) + self.prefix_len = pre.len + self.prefix = pre.prefix + self.entry_count = struct.unpack('>H', \ + self.data[len(pre):len(pre)+2])[0] + self.data = self.data[len(pre)+2:] + l = [] + for i in range(self.entry_count): + entry = TableDump2_RIBEntry(self.data) + self.data = self.data[len(entry):] + l.append(entry) + self.data = self.ribentry = l + +class TableDump2_IPV6(dpkt.Packet): + __hdr__ = ( + ('seq','I',0), + ) + + def unpack(self,buf): + dpkt.Packet.unpack(self, buf) + pre = bgp.RouteIPV6(self.data) + self.prefix_len = pre.len + self.prefix = pre.prefix + self.entry_count = struct.unpack('>H', \ + self.data[len(pre):len(pre)+2])[0] + self.data = self.data[len(pre)+2:] + l = [] + for i in range(self.entry_count): + entry = TableDump2_RIBEntry(self.data) + self.data = self.data[len(entry):] + l.append(entry) + self.data = self.ribentry = l + +class TableDump2_RIBGeneric(dpkt.Packet): + __hdr__ = ( + ('seq', 'I', 0), + ('afi', 'H', 0), + ('safi', 'B', 0) + ) + + def unpack(self,buf): + dpkt.Packet.unpack(self, buf) + route = bgp.RouteGeneric(self.data) + self.entry_count = struct.unpack('>H', \ + self.data[len(route):len(route)+2])[0] + self.data = self.data[len(route)+2:] + l = [] + for i in range(self.entry_count): + entry = TableDump2_RIBEntry(self.data) + self.data = self.data[len(entry):] + l.append(entry) + self.data = self.ribentry = l + +class TableDump2_RIBEntry(dpkt.Packet): + __hdr__ = ( + ('peer_index', 'H', 0), + ('originated_ts', 'I', 0), + ('attr_len', 'H', 0) + ) + + def unpack(self,buf): + dpkt.Packet.unpack(self,buf) + plen = self.attr_len + l = [] + while plen > 0: + attr = bgp.BGP.Update.Attribute(self.data) + self.data = self.data[len(attr):] + plen -= len(attr) + l.append(attr) + self.attributes = l + + def __len__(self): + return self.__hdr_len__ + self.attr_len + + class BGP4MPMessage(dpkt.Packet): __hdr__ = ( ('src_as', 'H', 0),