Esempio n. 1
0
    def serialize(self):
        bgp_attrs_bin = bytearray()
        for attr in self.bgp_attributes:
            bgp_attrs_bin += attr.serialize()
        self.attr_len = len(bgp_attrs_bin)  # fixup

        prefix = ip.text_to_bin(self.prefix)
        peer_ip = ip.text_to_bin(self.peer_ip)

        return struct.pack(self._HEADER_FMT, self.view_num, self.seq_num,
                           prefix, self.prefix_len, self.status,
                           self.originated_time, peer_ip, self.peer_as,
                           self.attr_len) + bgp_attrs_bin
Esempio n. 2
0
    def serialize(self):
        bgp_attrs_bin = bytearray()
        for attr in self.bgp_attributes:
            bgp_attrs_bin += attr.serialize()
        self.attr_len = len(bgp_attrs_bin)  # fixup

        prefix = ip.text_to_bin(self.prefix)
        peer_ip = ip.text_to_bin(self.peer_ip)

        return struct.pack(self._HEADER_FMT,
                           self.view_num, self.seq_num,
                           prefix,
                           self.prefix_len, self.status,
                           self.originated_time,
                           peer_ip,
                           self.peer_as, self.attr_len) + bgp_attrs_bin
Esempio n. 3
0
    def serialize(self):
        # fixup
        if ip.valid_ipv4(self.peer_ip) and ip.valid_ipv4(self.local_ip):
            self.afi = self.AFI_IPv4
        elif ip.valid_ipv6(self.peer_ip) and ip.valid_ipv6(self.local_ip):
            self.afi = self.AFI_IPv6
        else:
            raise ValueError(
                'peer_ip and local_ip must be the same address family: '
                'peer_ip=%s, local_ip=%s' % (self.peer_ip, self.local_ip))

        buf = struct.pack(self._HEADER_FMT, self.peer_as, self.local_as,
                          self.if_index, self.afi)

        buf += ip.text_to_bin(self.peer_ip)
        buf += ip.text_to_bin(self.local_ip)

        buf += struct.pack(self._STATES_FMT, self.old_state, self.new_state)

        return buf
Esempio n. 4
0
    def serialize(self):
        # fixup
        if ip.valid_ipv4(self.peer_ip) and ip.valid_ipv4(self.local_ip):
            self.afi = self.AFI_IPv4
        elif ip.valid_ipv6(self.peer_ip) and ip.valid_ipv6(self.local_ip):
            self.afi = self.AFI_IPv6
        else:
            raise ValueError(
                'peer_ip and local_ip must be the same address family: '
                'peer_ip=%s, local_ip=%s' % (self.peer_ip, self.local_ip))

        buf = struct.pack(self._HEADER_FMT,
                          self.peer_as, self.local_as,
                          self.if_index, self.afi)

        buf += ip.text_to_bin(self.peer_ip)
        buf += ip.text_to_bin(self.local_ip)

        buf += self.bgp_message.serialize()

        return buf
Esempio n. 5
0
    def serialize(self):
        # fixup
        if (netaddr.valid_ipv4(self.peer_ip)
                and netaddr.valid_ipv4(self.local_ip)):
            self.afi = self.AFI_IPv4
        elif (netaddr.valid_ipv6(self.peer_ip)
              and netaddr.valid_ipv6(self.local_ip)):
            self.afi = self.AFI_IPv6
        else:
            raise ValueError(
                'peer_ip and local_ip must be the same address family: '
                'peer_ip=%s, local_ip=%s' % (self.peer_ip, self.local_ip))

        buf = struct.pack(self._HEADER_FMT,
                          self.peer_as, self.local_as,
                          self.if_index, self.afi)

        buf += ip.text_to_bin(self.peer_ip)
        buf += ip.text_to_bin(self.local_ip)

        buf += self.bgp_message.serialize()

        return buf
Esempio n. 6
0
    def serialize(self):
        # fixup
        if (netaddr.valid_ipv4(self.peer_ip)
                and netaddr.valid_ipv4(self.local_ip)):
            self.afi = self.AFI_IPv4
        elif (netaddr.valid_ipv6(self.peer_ip)
              and netaddr.valid_ipv6(self.local_ip)):
            self.afi = self.AFI_IPv6
        else:
            raise ValueError(
                'peer_ip and local_ip must be the same address family: '
                'peer_ip=%s, local_ip=%s' % (self.peer_ip, self.local_ip))

        buf = struct.pack(self._HEADER_FMT,
                          self.peer_as, self.local_as,
                          self.if_index, self.afi)

        buf += ip.text_to_bin(self.peer_ip)
        buf += ip.text_to_bin(self.local_ip)

        buf += struct.pack(self._STATES_FMT,
                           self.old_state, self.new_state)

        return buf
Esempio n. 7
0
    def serialize(self):
        if ip.valid_ipv6(self.ip_addr):
            # Sets Peer IP Address family bit to IPv6
            self.type |= self.IP_ADDR_FAMILY_BIT
        ip_addr = ip.text_to_bin(self.ip_addr)

        if self.type & self.AS_NUMBER_SIZE_BIT or self.as_num > 0xffff:
            # Four octet AS number
            self.type |= self.AS_NUMBER_SIZE_BIT
            as_num = struct.pack('!I', self.as_num)
        else:
            # Two octet AS number
            as_num = struct.pack('!H', self.as_num)

        buf = struct.pack(self._HEADER_FMT, self.type,
                          addrconv.ipv4.text_to_bin(self.bgp_id))

        return buf + ip_addr + as_num
Esempio n. 8
0
    def serialize(self):
        if ip.valid_ipv6(self.ip_addr):
            # Sets Peer IP Address family bit to IPv6
            self.type |= self.IP_ADDR_FAMILY_BIT
        ip_addr = ip.text_to_bin(self.ip_addr)

        if self.type & self.AS_NUMBER_SIZE_BIT or self.as_num > 0xffff:
            # Four octet AS number
            self.type |= self.AS_NUMBER_SIZE_BIT
            as_num = struct.pack('!I', self.as_num)
        else:
            # Two octet AS number
            as_num = struct.pack('!H', self.as_num)

        buf = struct.pack(self._HEADER_FMT,
                          self.type,
                          addrconv.ipv4.text_to_bin(self.bgp_id))

        return buf + ip_addr + as_num
Esempio n. 9
0
 def test_text_to_bin_from_ipv6_text(self):
     ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
     val = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20,
                       0x66ff, 0xfe4c, 0x9c3c)
     res = ip.text_to_bin(ipv6_str)
     eq_(val, res)
Esempio n. 10
0
 def test_text_to_bin_from_ipv4_text(self):
     ipv4_str = '10.28.197.1'
     val = struct.pack('!4B', 10, 28, 197, 1)
     res = ip.text_to_bin(ipv4_str)
     eq_(val, res)
Esempio n. 11
0
 def test_text_to_bin_from_ipv6_text(self):
     ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
     val = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20, 0x66ff,
                       0xfe4c, 0x9c3c)
     res = ip.text_to_bin(ipv6_str)
     eq_(val, res)
Esempio n. 12
0
 def test_text_to_bin_from_ipv4_text(self):
     ipv4_str = '10.28.197.1'
     val = struct.pack('!4B', 10, 28, 197, 1)
     res = ip.text_to_bin(ipv4_str)
     eq_(val, res)