def pack (self): rs = [] for k,capabilities in six.iteritems(self): for capability in capabilities.extract(): rs.append(concat_bytes(character(k),character(len(capability)),capability)) parameters = concat_bytes_i(concat_bytes(character(2),character(len(r)),r) for r in rs) return concat_bytes(character(len(parameters)),parameters)
def packed_attributes (self, negotiated, maximum=Negotiated.FREE_SIZE): if not self.nlris: return # we changed the API to nrli.pack from addpath to negotiated but not pack itself mpurnlri = [] for nlri in self.nlris: if nlri.family() != self.family(): # nlri is not part of specified family continue mpurnlri.append(nlri.pack(negotiated)) payload = concat_bytes(self.afi.pack(), self.safi.pack()) header_length = len(payload) for nlri in mpurnlri: if self._len(payload + nlri) > maximum: if len(payload) == header_length or len(payload) > maximum: raise Notify(6, 0, 'attributes size is so large we can not even pack on MPURNLRI') yield self._attribute(payload) payload = concat_bytes(self.afi.pack(), self.safi.pack(), nlri) continue payload = concat_bytes(payload, nlri) if len(payload) == header_length or len(payload) > maximum: raise Notify(6, 0, 'attributes size is so large we can not even pack on MPURNLRI') yield self._attribute(payload)
def pack (self): rs = [] for k,capabilities in six.iteritems(self): for capability in capabilities.extract(): rs.append(concat_bytes(character(k),character(len(capability)),capability)) parameters = concat_bytes_i(concat_bytes(character(2),character(len(r)),r) for r in rs) return concat_bytes(character(len(parameters)),parameters)
def packed_attributes (self, negotiated, maximum=Negotiated.FREE_SIZE): if not self.nlris: return # addpath = negotiated.addpath.send(self.afi,self.safi) # nexthopself = negotiated.nexthopself(self.afi) mpnlri = {} for nlri in self.nlris: if nlri.family() != self.family(): # nlri is not part of specified family continue if nlri.nexthop is NoNextHop: # EOR and Flow may not have any next_hop nexthop = b'' else: _,rd_size = Family.size.get(self.family(),(0,0)) nh_rd = character(0)*rd_size if rd_size else b'' nexthop = nh_rd + nlri.nexthop.ton(negotiated,nlri.afi) # mpunli[nexthop] = nlri mpnlri.setdefault(nexthop,[]).append(nlri.pack(negotiated)) for nexthop,nlris in six.iteritems(mpnlri): payload = concat_bytes(self.afi.pack(), self.safi.pack(), character(len(nexthop)), nexthop, character(0)) header_length = len(payload) for nlri in nlris: if self._len(payload + nlri) > maximum: if len(payload) == header_length or len(payload) > maximum: raise Notify(6, 0, 'attributes size is so large we can not even pack on MPRNLRI') yield self._attribute(payload) payload = concat_bytes(self.afi.pack(), self.safi.pack(), character(len(nexthop)), nexthop, character(0), nlri) continue payload = concat_bytes(payload, nlri) if len(payload) == header_length or len(payload) > maximum: raise Notify(6, 0, 'attributes size is so large we can not even pack on MPRNLRI') yield self._attribute(payload)
def pack_nlri(self, negotiated=None): ordered_rules = [] # the order is a RFC requirement for ID in sorted(self.rules.keys()): rules = self.rules[ID] # for each component get all the operation to do # the format use does not prevent two opposing rules meaning that no packet can ever match for rule in rules: rule.operations &= (CommonOperator.EOL ^ 0xFF) rules[-1].operations |= CommonOperator.EOL # and add it to the last rule if ID not in (FlowDestination.ID, FlowSource.ID): ordered_rules.append(character(ID)) ordered_rules.append(concat_bytes_i(rule.pack() for rule in rules)) components = self.rd.pack() + concat_bytes_i(ordered_rules) lc = len(components) if lc < 0xF0: return concat_bytes(character(lc), components) if lc < 0x0FFF: return concat_bytes(pack('!H', lc | 0xF000), components) raise Notify( 3, 0, "my administrator attempted to announce a Flow Spec rule larger than encoding allows, protecting the innocent the only way I can" )
def packed_attributes(self, negotiated, maximum=Negotiated.FREE_SIZE): if not self.nlris: return # we changed the API to nrli.pack from addpath to negotiated but not pack itself mpurnlri = [] for nlri in self.nlris: if nlri.family() != self.family( ): # nlri is not part of specified family continue mpurnlri.append(nlri.pack(negotiated)) payload = concat_bytes(self.afi.pack(), self.safi.pack()) header_length = len(payload) for nlri in mpurnlri: if self._len(payload + nlri) > maximum: if len(payload) == header_length or len(payload) > maximum: raise Notify( 6, 0, 'attributes size is so large we can not even pack on MPURNLRI' ) yield self._attribute(payload) payload = concat_bytes(self.afi.pack(), self.safi.pack(), nlri) continue payload = concat_bytes(payload, nlri) if len(payload) == header_length or len(payload) > maximum: raise Notify( 6, 0, 'attributes size is so large we can not even pack on MPURNLRI') yield self._attribute(payload)
def pack(self): payload = pack('!H', 0) # flags for b, r in self.srgbs: payload = concat_bytes(payload, pack("!L", b)[1:], pack("!L", r)[1:]) return concat_bytes(pack('!B', self.TLV), pack('!H', len(payload)), payload)
def extract(self): restart = pack('!H', ((self.restart_flag << 12) | (self.restart_time & Graceful.TIME_MASK))) families = [(afi.pack(), safi.pack(), character(self[(afi, safi)])) for (afi, safi) in self.keys()] sfamilies = concat_bytes_i( concat_bytes(pafi, psafi, family) for (pafi, psafi, family) in families) return [concat_bytes(restart, sfamilies)]
def pack (self): t = pack('!B', self.TLV) val = b'' for srgb in self.srgbs: base = pack("!L", srgb[0])[1:] srange = pack("!L", srgb[1])[1:] val = concat_bytes(base,srange,val) flags = pack('!H',0) val = concat_bytes(flags,val) l = pack('!H', len(val)) return concat_bytes(t,l,val)
def pack (self): payload = pack('!H',0) # flags for b,r in self.srgbs: payload = concat_bytes( payload, pack("!L",b)[1:], pack("!L",r)[1:] ) return concat_bytes( pack('!B', self.TLV), pack('!H', len(payload)), payload )
def _segment (self, seg_type, values, asn4): length = len(values) if length: if length > 255: return self._segment(seg_type,values[:255],asn4) + self._segment(seg_type,values[255:],asn4) return concat_bytes(character(seg_type),character(len(values)),concat_bytes_i(v.pack(asn4) for v in values)) return b""
def pack(self): t = pack('!B', self.TLV) l = pack('!H', self.LENGTH) r0 = pack('!B', 0) r1 = pack('!H', 0) val = IP.pton(self.v6sid) return concat_bytes(t, l, r0, r1, val)
def pack(self): t = pack('!B', self.TLV) l = pack('!H', self.LENGTH) val = pack('!I', self.labelindex) reserved = pack('!B', 0) flags = pack('!H', 0) return concat_bytes(t, l, reserved, flags, val)
def pack(self): return concat_bytes( pack('!B', self.TLV), pack('!H', self.LENGTH), pack('!B', 0), # reserved pack('!H', 0), # flags pack('!I', self.labelindex))
def _signature_blocks(self, negotiated, asns=None, skis=None): self.signature_block = self._signature_block(negotiated, asns, skis) if not self.signature_block: return None return concat_bytes( pack('!H', (self.signature_block_len + self.SIG_BLOCK_LEN)), b''.join(self.signature_block))
def __init__ (self, afi, safi, sequence): OperationalFamily.__init__( self, Operational.CODE.NS, afi,safi, concat_bytes(sequence,self.ERROR_SUBCODE) )
def extract (self): return [concat_bytes( character(len(self.host_name)), self.host_name, character(len(self.domain_name)), self.domain_name, )]
def pack (self): return concat_bytes( pack('!B', self.TLV), pack('!H', self.LENGTH), pack('!B', 0), pack('!H', 0), IP.pton(self.v6sid) )
def pack_nlri(self, negotiated=None): return concat_bytes( b'\x00\x11', # pack('!H',17) self.rd.pack(), pack('!HHH', self.endpoint, self.offset, self.size), pack('!L', (self.base << 4) | 0x1)[1:] # setting the bottom of stack, should we ? )
def message (self,negotiated=None): return self._message(concat_bytes( self.version.pack(), self.asn.trans().pack(), self.hold_time.pack(), self.router_id.pack(), self.capabilities.pack() ))
def extract(self): return [ concat_bytes( character(len(self.host_name)), self.host_name, character(len(self.domain_name)), self.domain_name, ) ]
def _signature(self, asn=None, ski=None): signature = [] signature = self._signature_from_lib(asn, ski) if not signature: # in case None #self.logger.BGPSEC("Signature is not made due to Key issues") return None self.signature_block_len += len(signature) return concat_bytes(pack('!H', len(signature)), b''.join(signature))
def pack (self): return concat_bytes( pack('!B', self.TLV), pack('!H', self.LENGTH), pack('!B', 0), IP.pton(self.l3vpnsid), pack('!B', 0), pack('!H', 0xffff), pack('!B', 0), )
def _segment(self, seg_type, values, asn4): length = len(values) if length: if length > 255: return self._segment(seg_type, values[:255], asn4) + self._segment( seg_type, values[255:], asn4) return concat_bytes(character(seg_type), character(len(values)), concat_bytes_i(v.pack(asn4) for v in values)) return b""
def pack (self, negotiated=None): flag = self.FLAG length = len(self.data) if length > 0xFF: flag |= Attribute.Flag.EXTENDED_LENGTH if flag & Attribute.Flag.EXTENDED_LENGTH: len_value = pack('!H',length) else: len_value = character(length) return concat_bytes(character(flag),character(self.ID),len_value,self.data)
def _pack(self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes(self.rd.pack(), self.esi.pack(), self.etag.pack(), self.label.pack()) return self._packed
def message(self, negotiated): self.sent_routerid = self.routerid if self.routerid else negotiated.sent_open.router_id if self.sequence is None: self.sent_sequence = (self.__sequence_number.setdefault( self.routerid, 0) + 1) % 0xFFFFFFFF self.__sequence_number[self.sent_routerid] = self.sent_sequence else: self.sent_sequence = self.sequence return self._message( concat_bytes(self.sent_routerid.pack(), pack('!L', self.sent_sequence), self.data))
def message (self, negotiated): self.sent_routerid = self.routerid if self.routerid else negotiated.sent_open.router_id if self.sequence is None: self.sent_sequence = (self.__sequence_number.setdefault(self.routerid,0) + 1) % 0xFFFFFFFF self.__sequence_number[self.sent_routerid] = self.sent_sequence else: self.sent_sequence = self.sequence return self._message(concat_bytes( self.sent_routerid.pack(),pack('!L',self.sent_sequence), self.data ))
def _pack(self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes(self.rd.pack(), self.etag.pack(), character(len(self.ip) * 8), self.ip.pack()) return self._packed
def _attribute(self, value): flag = self.FLAG if flag & Attribute.Flag.OPTIONAL and not value: return b'' length = len(value) if length > 0xFF: flag |= Attribute.Flag.EXTENDED_LENGTH if flag & Attribute.Flag.EXTENDED_LENGTH: len_value = pack('!H', length) else: len_value = character(length) return concat_bytes(character(flag), character(self.ID), len_value, value)
def _pack(self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), character(len(self.ip) * 8 if self.ip else 0), self.ip.pack() if self.ip else b'') return self._packed
def pack_nlri (self, negotiated=None): ordered_rules = [] # the order is a RFC requirement for ID in sorted(self.rules.keys()): rules = self.rules[ID] # for each component get all the operation to do # the format use does not prevent two opposing rules meaning that no packet can ever match for rule in rules: rule.operations &= (CommonOperator.EOL ^ 0xFF) rules[-1].operations |= CommonOperator.EOL # and add it to the last rule if ID not in (FlowDestination.ID,FlowSource.ID): ordered_rules.append(character(ID)) ordered_rules.append(concat_bytes_i(rule.pack() for rule in rules)) components = self.rd.pack() + concat_bytes_i(ordered_rules) lc = len(components) if lc < 0xF0: return concat_bytes(character(lc),components) if lc < 0x0FFF: return concat_bytes(pack('!H',lc | 0xF000),components) raise Notify(3,0,"my administrator attempted to announce a Flow Spec rule larger than encoding allows, protecting the innocent the only way I can")
def _pack (self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), self.etag.pack(), self.label.pack() ) return self._packed
def _pack (self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), character(len(self.ip)*8 if self.ip else 0), self.ip.pack() if self.ip else b'' ) return self._packed
def _pack (self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.etag.pack(), character(len(self.ip)*8), self.ip.pack() ) return self._packed
def _pack (self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), self.etag.pack(), character(self.iplen), self.ip.pack(), self.gwip.pack(), self.label.pack(), ) return self._packed
def _pack(self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), self.etag.pack(), character(self.iplen), self.ip.pack(), self.gwip.pack(), self.label.pack(), ) return self._packed
def _pack(self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), self.etag.pack(), character(self.maclen), # only 48 supported by the draft self.mac.pack(), character(len(self.ip) * 8 if self.ip else 0), self.ip.pack() if self.ip else b'', self.label.pack()) return self._packed
def _pack (self, packed=None): if self._packed: return self._packed if packed: self._packed = packed return packed self._packed = concat_bytes( self.rd.pack(), self.esi.pack(), self.etag.pack(), character(self.maclen), # only 48 supported by the draft self.mac.pack(), character(len(self.ip)*8 if self.ip else 0), self.ip.pack() if self.ip else b'', self.label.pack() ) return self._packed
def _message (self, data): return Message._message(self,concat_bytes( self.what.pack(), pack('!H',len(data)), data ))
def message (self,negotiated=None): return self._message(concat_bytes(self.afi.pack(),character(self.reserved),self.safi.pack()))
def _message (self, message): message_len = pack('!H',19+len(message)) return concat_bytes(self.MARKER,message_len,self.TYPE,message)
def _message (self, data): return Operational._message(self,concat_bytes( self.afi.pack(), self.safi.pack(), data ))
def prefix(data): # This function needs renaming return concat_bytes(pack('!H', len(data)), data)
def _message(self, data): return Message._message( self, concat_bytes(self.what.pack(), pack('!H', len(data)), data))
def __init__(self, afi, safi, sequence): OperationalFamily.__init__( self, Operational.CODE.NS, afi, safi, concat_bytes(sequence, self.ERROR_SUBCODE))
def from_rt(cls, route_target): packed = route_target.pack() return cls.unpack(concat_bytes(packed[0:1], character(cls.COMMUNITY_SUBTYPE), packed[2:]))
def _message(self, data): return Operational._message( self, concat_bytes(self.afi.pack(), self.safi.pack(), data))
def pack (self): raw = self.cidr.pack_nlri() # ID is defined in subclasses return concat_bytes(character(self.ID),raw) # pylint: disable=E1101
def pack (self): # ID is defined in subclasses return concat_bytes(character(self.ID),character(self.cidr.mask),character(self.offset),self.cidr.pack_ip()) # pylint: disable=E1101
def _secure_path(self, negotiated, asns=None): self.secure_path = self._secure_path_segment(negotiated, asns) return concat_bytes( pack('!H', (self.secure_path_len + self.SEC_PATH_LEN)), b''.join(self.secure_path))
def prefix (data): # This function needs renaming return concat_bytes(pack('!H',len(data)),data)
def extract (self): restart = pack('!H',((self.restart_flag << 12) | (self.restart_time & Graceful.TIME_MASK))) families = [(afi.pack(),safi.pack(),character(self[(afi,safi)])) for (afi,safi) in self.keys()] sfamilies = concat_bytes_i(concat_bytes(pafi,psafi,family) for (pafi,psafi,family) in families) return [concat_bytes(restart,sfamilies)]
def pack (self): l,v = self.encode(self.value) op = self.operations | _len_to_bit(l) return concat_bytes(character(op),v)
def pack (self): return concat_bytes_i(concat_bytes(character(tlv.type),pack('!H',len(tlv.value)+3),tlv.value) for tlv in self)