Package dpkt :: Module rip
[hide private]
[frames] | no frames]

Source Code for Module dpkt.rip

 1  # $Id: rip.py 23 2006-11-08 15:45:33Z dugsong $ 
 2   
 3  """Routing Information Protocol.""" 
 4   
 5  import dpkt 
 6   
 7  # RIP v2 - RFC 2453 
 8  # http://tools.ietf.org/html/rfc2453 
 9   
10  REQUEST = 1 
11  RESPONSE = 2 
12   
13 -class RIP(dpkt.Packet):
14 __hdr__ = ( 15 ('cmd', 'B', REQUEST), 16 ('v', 'B', 2), 17 ('rsvd', 'H', 0) 18 ) 19
20 - def unpack(self, buf):
21 dpkt.Packet.unpack(self, buf) 22 l = [] 23 self.auth = None 24 while self.data: 25 rte = RTE(self.data[:20]) 26 if rte.family == 0xFFFF: 27 self.auth = Auth(self.data[:20]) 28 else: 29 l.append(rte) 30 self.data = self.data[20:] 31 self.data = self.rtes = l
32
33 - def __len__(self):
34 len = self.__hdr_len__ 35 if self.auth: 36 len += len(self.auth) 37 len += sum(map(len, self.rtes)) 38 return len
39
40 - def __str__(self):
41 auth = '' 42 if self.auth: 43 auth = str(self.auth) 44 return self.pack_hdr() + \ 45 auth + \ 46 ''.join(map(str, self.rtes))
47
48 -class RTE(dpkt.Packet):
49 __hdr__ = ( 50 ('family', 'H', 2), 51 ('route_tag', 'H', 0), 52 ('addr', 'I', 0), 53 ('subnet', 'I', 0), 54 ('next_hop', 'I', 0), 55 ('metric', 'I', 1) 56 )
57
58 -class Auth(dpkt.Packet):
59 __hdr__ = ( 60 ('rsvd', 'H', 0xFFFF), 61 ('type', 'H', 2), 62 ('auth', '16s', 0) 63 )
64 65 if __name__ == '__main__': 66 import unittest 67
68 - class RIPTestCase(unittest.TestCase):
69 - def testPack(self):
70 r = RIP(self.s) 71 self.failUnless(self.s == str(r))
72
73 - def testUnpack(self):
74 r = RIP(self.s) 75 self.failUnless(r.auth == None) 76 self.failUnless(len(r.rtes) == 2) 77 78 rte = r.rtes[1] 79 self.failUnless(rte.family == 2) 80 self.failUnless(rte.route_tag == 0) 81 self.failUnless(rte.metric == 1)
82 83 s = '\x02\x02\x00\x00\x00\x02\x00\x00\x01\x02\x03\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x02\x00\x00\xc0\xa8\x01\x08\xff\xff\xff\xfc\x00\x00\x00\x00\x00\x00\x00\x01'
84 unittest.main() 85