Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fromElements (cls, prefix, suffix):
try:
if '.' in prefix:
data = [character(0),character(1)]
data.extend([character(int(_)) for _ in prefix.split('.')])
data.extend([character(suffix >> 8),character(suffix & 0xFF)])
distinguisher = concat_bytes_i(data)
else:
number = int(prefix)
if number < pow(2,16) and suffix < pow(2,32):
distinguisher = character(0) + character(0) + pack('!H',number) + pack('!L',suffix)
elif number < pow(2,32) and suffix < pow(2,16):
distinguisher = character(0) + character(2) + pack('!L',number) + pack('!H',suffix)
else:
raise ValueError('invalid route-distinguisher %s' % number)
return cls(distinguisher)
except ValueError:
raise ValueError('invalid route-distinguisher %s:%s' % (prefix,suffix))
def message (self,negotiated=None):
return self._message(
character(self.code) +
character(self.subcode) +
self.data
)
def pack (self, negotiated=None):
addpath = self.path_info.pack() if negotiated and negotiated.addpath.send(self.afi,self.safi) else b''
mask = character(len(self.labels)*8 + self.cidr.mask)
return addpath + mask + self.labels.pack() + self.cidr.pack_ip()
if value == ']':
break
lc = _large_community(value)
if lc in large_communities.communities:
continue
large_communities.add(lc)
else:
large_communities.add(_large_community(value))
return large_communities
_HEADER = {
# header and subheader
'target': character(0x00)+character(0x02),
'target4': character(0x02)+character(0x02),
'origin': character(0x00)+character(0x03),
'origin4': character(0x02)+character(0x03),
'redirect': character(0x80)+character(0x08),
'l2info': character(0x80)+character(0x0A),
'redirect-to-nexthop': character(0x08)+character(0x00),
'bandwidth': character(0x40)+character(0x04),
}
_SIZE = {
'target': 2,
'target4': 2,
'origin': 2,
'origin4': 2,
'redirect': 2,
'l2info': 4,
'redirect-to-nexthop': 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_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 __init__ (self, origin, packed=None):
self.origin = origin
self._packed = self._attribute(packed if packed else character(origin))
def pack_int (afi, integer):
return concat_bytes_i(character((integer >> (offset * 8)) & 0xff) for offset in range(IP.length(afi)-1,-1,-1))