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
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
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