def parse(cls, buf): (view_num, seq_num, prefix, prefix_len, status, originated_time, peer_ip, peer_as, attr_len) = struct.unpack_from(cls._HEADER_FMT, buf) prefix = ip.bin_to_text(prefix) peer_ip = ip.bin_to_text(peer_ip) bgp_attr_bin = buf[cls.HEADER_SIZE:cls.HEADER_SIZE + attr_len] bgp_attributes = [] while bgp_attr_bin: attr, bgp_attr_bin = bgp._PathAttribute.parser(bgp_attr_bin) bgp_attributes.append(attr) return cls(view_num, seq_num, prefix, prefix_len, status, originated_time, peer_ip, peer_as, bgp_attributes, attr_len)
def parse(cls, buf): (peer_as, local_as, if_index, afi) = struct.unpack_from(cls._HEADER_FMT, buf) offset = cls.HEADER_SIZE if afi == cls.AFI_IPv4: # IPv4 Address addrs_fmt = cls._ADDRS_FMT % (4, 4) elif afi == cls.AFI_IPv6: # IPv6 Address addrs_fmt = cls._ADDRS_FMT % (16, 16) else: raise struct.error('Unsupported address family: %d' % afi) (peer_ip, local_ip) = struct.unpack_from(addrs_fmt, buf, offset) peer_ip = ip.bin_to_text(peer_ip) local_ip = ip.bin_to_text(local_ip) offset += struct.calcsize(addrs_fmt) (old_state, new_state) = struct.unpack_from(cls._STATES_FMT, buf, offset) return cls(peer_as, local_as, if_index, peer_ip, local_ip, old_state, new_state, afi)
def parse(cls, buf): (peer_as, local_as, if_index, afi) = struct.unpack_from(cls._HEADER_FMT, buf) offset = cls.HEADER_SIZE if afi == cls.AFI_IPv4: # IPv4 Address addrs_fmt = cls._ADDRS_FMT % (4, 4) elif afi == cls.AFI_IPv6: # IPv6 Address addrs_fmt = cls._ADDRS_FMT % (16, 16) else: raise struct.error('Unsupported address family: %d' % afi) (peer_ip, local_ip) = struct.unpack_from(addrs_fmt, buf, offset) peer_ip = ip.bin_to_text(peer_ip) local_ip = ip.bin_to_text(local_ip) offset += struct.calcsize(addrs_fmt) rest = buf[offset:] bgp_message, _, _ = bgp.BGPMessage.parser(rest) return cls(peer_as, local_as, if_index, peer_ip, local_ip, bgp_message, afi)
def parse(cls, buf): (peer_as, local_as, if_index, afi) = struct.unpack_from( cls._HEADER_FMT, buf) offset = cls.HEADER_SIZE if afi == cls.AFI_IPv4: # IPv4 Address addrs_fmt = cls._ADDRS_FMT % (4, 4) elif afi == cls.AFI_IPv6: # IPv6 Address addrs_fmt = cls._ADDRS_FMT % (16, 16) else: raise struct.error('Unsupported address family: %d' % afi) (peer_ip, local_ip) = struct.unpack_from(addrs_fmt, buf, offset) peer_ip = ip.bin_to_text(peer_ip) local_ip = ip.bin_to_text(local_ip) offset += struct.calcsize(addrs_fmt) rest = buf[offset:] bgp_message, _, _ = bgp.BGPMessage.parser(rest) return cls(peer_as, local_as, if_index, peer_ip, local_ip, bgp_message, afi)
def parse(cls, buf): (peer_as, local_as, if_index, afi) = struct.unpack_from( cls._HEADER_FMT, buf) offset = cls.HEADER_SIZE if afi == cls.AFI_IPv4: # IPv4 Address addrs_fmt = cls._ADDRS_FMT % (4, 4) elif afi == cls.AFI_IPv6: # IPv6 Address addrs_fmt = cls._ADDRS_FMT % (16, 16) else: raise struct.error('Unsupported address family: %d' % afi) (peer_ip, local_ip) = struct.unpack_from(addrs_fmt, buf, offset) peer_ip = ip.bin_to_text(peer_ip) local_ip = ip.bin_to_text(local_ip) offset += struct.calcsize(addrs_fmt) (old_state, new_state) = struct.unpack_from( cls._STATES_FMT, buf, offset) return cls(peer_as, local_as, if_index, peer_ip, local_ip, old_state, new_state, afi)
def parse(cls, buf): (type_, bgp_id) = struct.unpack_from(cls._HEADER_FMT, buf) bgp_id = addrconv.ipv4.bin_to_text(bgp_id) offset = cls.HEADER_SIZE if type_ & cls.IP_ADDR_FAMILY_BIT: # IPv6 address family ip_addr_len = 16 else: # IPv4 address family ip_addr_len = 4 ip_addr = ip.bin_to_text(buf[offset:offset + ip_addr_len]) offset += ip_addr_len if type_ & cls.AS_NUMBER_SIZE_BIT: # Four octet AS number (as_num, ) = struct.unpack_from('!I', buf, offset) offset += 4 else: # Two octet AS number (as_num, ) = struct.unpack_from('!H', buf, offset) offset += 2 return cls(bgp_id, ip_addr, as_num, type_), buf[offset:]
def parse(cls, buf): (type_, bgp_id) = struct.unpack_from(cls._HEADER_FMT, buf) bgp_id = addrconv.ipv4.bin_to_text(bgp_id) offset = cls.HEADER_SIZE if type_ & cls.IP_ADDR_FAMILY_BIT: # IPv6 address family ip_addr_len = 16 else: # IPv4 address family ip_addr_len = 4 ip_addr = ip.bin_to_text(buf[offset:offset + ip_addr_len]) offset += ip_addr_len if type_ & cls.AS_NUMBER_SIZE_BIT: # Four octet AS number (as_num,) = struct.unpack_from('!I', buf, offset) offset += 4 else: # Two octet AS number (as_num,) = struct.unpack_from('!H', buf, offset) offset += 2 return cls(bgp_id, ip_addr, as_num, type_), buf[offset:]
def test_bin_to_text_with_invalid_bin(self): invalid_bin = b'invalid' ip.bin_to_text(invalid_bin)
def test_bin_to_text_from_ipv6_bin(self): ipv6_bin = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20, 0x66ff, 0xfe4c, 0x9c3c) val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c' res = ip.bin_to_text(ipv6_bin) eq_(val, res)
def test_bin_to_text_from_ipv4_bin(self): ipv4_bin = struct.pack('!4B', 10, 28, 197, 1) val = '10.28.197.1' res = ip.bin_to_text(ipv4_bin) eq_(val, res)