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

Source Code for Module dpkt.vrrp

  1  # $Id: vrrp.py 88 2013-03-05 19:43:17Z andrewflnr@gmail.com $ 
  2  # -*- coding: utf-8 -*- 
  3  """Virtual Router Redundancy Protocol.""" 
  4  from __future__ import print_function 
  5  from __future__ import absolute_import 
  6   
  7  from . import dpkt 
  8  from .decorators import deprecated 
9 10 11 -class VRRP(dpkt.Packet):
12 """Virtual Router Redundancy Protocol. 13 14 TODO: Longer class information.... 15 16 Attributes: 17 __hdr__: Header fields of VRRP. 18 TODO. 19 """ 20 21 __hdr__ = ( 22 ('_v_type', 'B', 0x21), 23 ('vrid', 'B', 0), 24 ('priority', 'B', 0), 25 ('count', 'B', 0), 26 ('atype', 'B', 0), 27 ('advtime', 'B', 0), 28 ('sum', 'H', 0), 29 ) 30 addrs = () 31 auth = '' 32 33 @property
34 - def v(self): # high 4 bits of _v_type
35 return self._v_type >> 4
36 37 @v.setter
38 - def v(self, v):
39 self._v_type = (self._v_type & 0x0f) | (v << 4)
40 41 @property
42 - def type(self): # low 4 bits of _v_type
43 return self._v_type & 0x0f 44 45 @type.setter
46 - def type(self, v):
47 self._v_type = (self._v_type & 0xf0) | (v & 0x0f)
48
49 - def unpack(self, buf):
50 dpkt.Packet.unpack(self, buf) 51 l = [] 52 off = 0 53 for off in range(0, 4 * self.count, 4): 54 l.append(self.data[off:off + 4]) 55 self.addrs = l 56 self.auth = self.data[off + 4:] 57 self.data = ''
58
59 - def __len__(self):
60 return self.__hdr_len__ + (4 * self.count) + len(self.auth)
61
62 - def __bytes__(self):
63 data = b''.join(self.addrs) + self.auth 64 if not self.sum: 65 self.sum = dpkt.in_cksum(self.pack_hdr() + data) 66 return self.pack_hdr() + data
67
68 -def test_vrrp():
69 # no addresses 70 s = b'\x00\x00\x00\x00\x00\x00\xff\xff' 71 v = VRRP(s) 72 assert v.sum == 0xffff 73 assert bytes(v) == s 74 75 # have address 76 s = b'\x21\x01\x64\x01\x00\x01\xba\x52\xc0\xa8\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00' 77 v = VRRP(s) 78 assert v.count == 1 79 assert v.addrs == [b'\xc0\xa8\x00\x01'] # 192.168.0.1 80 assert bytes(v) == s 81 82 # test checksum generation 83 v.sum = 0 84 assert bytes(v) == s 85 86 # test length 87 assert len(v) == len(s) 88 89 # test getters 90 assert v.v == 2 91 assert v.type == 1 92 93 # test setters 94 v.v = 3 95 v.type = 2 96 assert bytes(v)[0] == b'\x32'[0]
97 98 99 if __name__ == '__main__': 100 test_vrrp() 101 102 print('Tests Successful...') 103