Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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:]