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  # -*- coding: utf-8 -*- 
  3  """Routing Information Protocol.""" 
  4  from __future__ import print_function 
  5  from __future__ import absolute_import 
  6   
  7  from . import dpkt 
  8   
  9  # RIP v2 - RFC 2453 
 10  # http://tools.ietf.org/html/rfc2453 
 11   
 12  REQUEST = 1 
 13  RESPONSE = 2 
 14   
 15   
16 -class RIP(dpkt.Packet):
17 """Routing Information Protocol. 18 19 TODO: Longer class information.... 20 21 Attributes: 22 __hdr__: Header fields of RIP. 23 TODO. 24 """ 25 26 __hdr__ = ( 27 ('cmd', 'B', REQUEST), 28 ('v', 'B', 2), 29 ('rsvd', 'H', 0) 30 ) 31
32 - def unpack(self, buf):
33 dpkt.Packet.unpack(self, buf) 34 l = [] 35 self.auth = None 36 while self.data: 37 rte = RTE(self.data[:20]) 38 if rte.family == 0xFFFF: 39 self.auth = Auth(self.data[:20]) 40 else: 41 l.append(rte) 42 self.data = self.data[20:] 43 self.data = self.rtes = l
44
45 - def __len__(self):
46 n = self.__hdr_len__ 47 if self.auth: 48 n += len(self.auth) 49 n += sum(map(len, self.rtes)) 50 return n
51
52 - def __bytes__(self):
53 auth = b'' 54 if self.auth: 55 auth = bytes(self.auth) 56 return self.pack_hdr() + auth + b''.join(map(bytes, self.rtes))
57 58
59 -class RTE(dpkt.Packet):
60 __hdr__ = ( 61 ('family', 'H', 2), 62 ('route_tag', 'H', 0), 63 ('addr', 'I', 0), 64 ('subnet', 'I', 0), 65 ('next_hop', 'I', 0), 66 ('metric', 'I', 1) 67 )
68 69
70 -class Auth(dpkt.Packet):
71 __hdr__ = ( 72 ('rsvd', 'H', 0xFFFF), 73 ('type', 'H', 2), 74 ('auth', '16s', 0) 75 )
76 77 78 __s = b'\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' 79 80
81 -def test_rtp_pack():
82 r = RIP(__s) 83 assert (__s == bytes(r))
84 85
86 -def test_rtp_unpack():
87 r = RIP(__s) 88 assert (r.auth is None) 89 assert (len(r.rtes) == 2) 90 91 rte = r.rtes[1] 92 assert (rte.family == 2) 93 assert (rte.route_tag == 0) 94 assert (rte.metric == 1)
95 96 97 if __name__ == '__main__': 98 test_rtp_pack() 99 test_rtp_unpack() 100 print('Tests Successful...') 101