Esempio n. 1
0
	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)
Esempio n. 2
0
	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)
Esempio n. 3
0
	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)
Esempio n. 4
0
	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)
Esempio n. 5
0
    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"
        )
Esempio n. 6
0
    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)
Esempio n. 7
0
 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)
Esempio n. 8
0
 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)]
Esempio n. 9
0
	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)
Esempio n. 10
0
	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
		)
Esempio n. 11
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""
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
 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))
Esempio n. 15
0
 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))
Esempio n. 16
0
		def __init__ (self, afi, safi, sequence):
			OperationalFamily.__init__(
				self,
				Operational.CODE.NS,
				afi,safi,
				concat_bytes(sequence,self.ERROR_SUBCODE)
			)
Esempio n. 17
0
	def extract (self):
		return [concat_bytes(
			character(len(self.host_name)),
			self.host_name,
			character(len(self.domain_name)),
			self.domain_name,
		)]
Esempio n. 18
0
	def pack (self):
		return concat_bytes(
			pack('!B', self.TLV),
			pack('!H', self.LENGTH),
			pack('!B', 0),
			pack('!H', 0),
			IP.pton(self.v6sid)
		)
Esempio n. 19
0
 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 ?
     )
Esempio n. 20
0
	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()
		))
Esempio n. 21
0
 def extract(self):
     return [
         concat_bytes(
             character(len(self.host_name)),
             self.host_name,
             character(len(self.domain_name)),
             self.domain_name,
         )
     ]
Esempio n. 22
0
    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))
Esempio n. 23
0
	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),
		)
Esempio n. 24
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""
Esempio n. 25
0
	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)
Esempio n. 26
0
    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
Esempio n. 27
0
    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))
Esempio n. 28
0
	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
		))
Esempio n. 29
0
    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
Esempio n. 30
0
 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)
Esempio n. 31
0
    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
Esempio n. 32
0
	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")
Esempio n. 33
0
	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
Esempio n. 34
0
	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
Esempio n. 35
0
	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
Esempio n. 36
0
	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
Esempio n. 37
0
    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
Esempio n. 38
0
    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
Esempio n. 39
0
	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
Esempio n. 40
0
	def _message (self, data):
		return Message._message(self,concat_bytes(
			self.what.pack(),
			pack('!H',len(data)),
			data
		))
Esempio n. 41
0
	def message (self,negotiated=None):
		return self._message(concat_bytes(self.afi.pack(),character(self.reserved),self.safi.pack()))
Esempio n. 42
0
	def _message (self, message):
		message_len = pack('!H',19+len(message))
		return concat_bytes(self.MARKER,message_len,self.TYPE,message)
Esempio n. 43
0
	def _message (self, data):
		return Operational._message(self,concat_bytes(
			self.afi.pack(),
			self.safi.pack(),
			data
		))
Esempio n. 44
0
 def prefix(data):
     # This function needs renaming
     return concat_bytes(pack('!H', len(data)), data)
Esempio n. 45
0
 def _message(self, data):
     return Message._message(
         self, concat_bytes(self.what.pack(), pack('!H', len(data)), data))
Esempio n. 46
0
 def __init__(self, afi, safi, sequence):
     OperationalFamily.__init__(
         self, Operational.CODE.NS, afi, safi,
         concat_bytes(sequence, self.ERROR_SUBCODE))
Esempio n. 47
0
	def from_rt(cls, route_target):
		packed = route_target.pack()
		return cls.unpack(concat_bytes(packed[0:1], character(cls.COMMUNITY_SUBTYPE), packed[2:]))
Esempio n. 48
0
 def _message(self, data):
     return Operational._message(
         self, concat_bytes(self.afi.pack(), self.safi.pack(), data))
Esempio n. 49
0
	def pack (self):
		raw = self.cidr.pack_nlri()
		# ID is defined in subclasses
		return concat_bytes(character(self.ID),raw)  # pylint: disable=E1101
Esempio n. 50
0
	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
Esempio n. 51
0
 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))
Esempio n. 52
0
	def prefix (data):
		# This function needs renaming
		return concat_bytes(pack('!H',len(data)),data)
Esempio n. 53
0
	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)]
Esempio n. 54
0
	def pack (self):
		l,v = self.encode(self.value)
		op = self.operations | _len_to_bit(l)
		return concat_bytes(character(op),v)
Esempio n. 55
0
	def pack (self):
		return concat_bytes_i(concat_bytes(character(tlv.type),pack('!H',len(tlv.value)+3),tlv.value) for tlv in self)