Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def decode(self):
nla_base.decode(self)
data_length = len(self['value'])
element_size = struct.calcsize(self.fmt)
array_size = data_length // element_size
trail = (data_length % element_size) or -data_length
data = self['value'][:-trail]
fmt = '%s%i%s' % (self.fmt[:-1], array_size, self.fmt[-1:])
self.value = struct.unpack(fmt, data)
array_size = data_length // element_size
trail = (data_length % element_size) or -data_length
data = self['value'][:-trail]
fmt = '%s%i%s' % (self.fmt[:-1], array_size, self.fmt[-1:])
self.value = struct.unpack(fmt, data)
class cdata(nla_base):
'''
Binary data
'''
__slots__ = ()
fields = [('value', 's')]
class string(nla_base):
'''
UTF-8 string.
'''
__slots__ = ()
sql_type = 'TEXT'
fields = [('value', 's')]
def encode(self):
if isinstance(self['value'], str) and sys.version[0] == '3':
self['value'] = bytes(self['value'], 'utf-8')
nla_base.encode(self)
def decode(self):
nla_base.decode(self)
self['value'][i * 4:i * 4 + 4])[0]
record = {'label': (label & 0xFFFFF000) >> 12,
'tc': (label & 0x00000E00) >> 9,
'bos': (label & 0x00000100) >> 8,
'ttl': label & 0x000000FF}
self.value.append(record)
else:
raise TypeError('socket family not supported')
class mpls_target(target):
__slots__ = ()
family = AF_MPLS
class l2addr(nla_base):
'''
Decode MAC address.
'''
__slots__ = ()
sql_type = 'TEXT'
fields = [('value', '=6s')]
def encode(self):
self['value'] = struct.pack('BBBBBB',
*[int(i, 16) for i in
self.value.split(':')])
nla_base.encode(self)
def decode(self):
sql_type = 'TEXT'
fields = [('value', '=6s')]
def encode(self):
self['value'] = struct.pack('BBBBBB',
*[int(i, 16) for i in
self.value.split(':')])
nla_base.encode(self)
def decode(self):
nla_base.decode(self)
self.value = ':'.join('%02x' % (i) for i in
struct.unpack('BBBBBB', self['value']))
class hex(nla_base):
'''
Represent NLA's content with header as hex string.
'''
__slots__ = ()
fields = [('value', 's')]
def decode(self):
nla_base.decode(self)
self.value = hexdump(self['value'])
class array(nla_base):
'''
Array of simple data type
'''
family = AF_INET6
else:
family = AF_INET
self['value'] = inet_pton(family, self.value)
nla_base.encode(self)
def decode(self):
nla_base.decode(self)
# use real provided family, not implicit
if self.length > 8:
family = AF_INET6
else:
family = AF_INET
self.value = inet_ntop(family, self['value'])
class target(nla_base):
'''
A universal target class. The target type depends on the msg
family:
* AF_INET: IPv4 addr, string: "127.0.0.1"
* AF_INET6: IPv6 addr, string: "::1"
* AF_MPLS: MPLS labels, 0 .. k: [{"label": 0x20, "ttl": 16}, ...]
'''
__slots__ = ()
sql_type = 'TEXT'
fields = [('value', 's')]
family = None
own_parent = True
msg_class = msg_class(self,
data=self.data,
offset=offset)
# decode NLA
nla = msg_class(data=self.data,
offset=offset,
parent=self,
length=length,
init=prime['init'])
nla._nla_array = prime['nla_array']
nla._nla_flags = base_msg_type & (NLA_F_NESTED |
NLA_F_NET_BYTEORDER)
name = prime['name']
else:
name = 'UNKNOWN'
nla = nla_base(data=self.data,
offset=offset,
length=length)
self['attrs'].append(nla_slot(name, nla))
offset += (length + 4 - 1) & ~ (4 - 1)
struct.unpack('BBBBBB', self['value']))
class hex(nla_base):
'''
Represent NLA's content with header as hex string.
'''
__slots__ = ()
fields = [('value', 's')]
def decode(self):
nla_base.decode(self)
self.value = hexdump(self['value'])
class array(nla_base):
'''
Array of simple data type
'''
__slots__ = (
"_fmt",
)
fields = [('value', 's')]
own_parent = True
@property
def fmt(self):
# try to get format from parent
# work only with elementary types
if getattr(self, "_fmt", None) is not None:
class int8(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', 'b')]
class int16(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', 'h')]
class int32(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', 'i')]
class int64(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', 'q')]
class be8(nla_base):
__slots__ = ()
class be16(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', '>H')]
class be32(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', '>I')]
class be64(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', '>Q')]
class ipXaddr(nla_base):
__slots__ = ()
sql_type = 'TEXT'
fields = [('value', 's')]
family = None
def encode(self):
self['value'] = inet_pton(self.family, self.value)
class be32(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', '>I')]
class be64(nla_base):
__slots__ = ()
sql_type = 'INTEGER'
fields = [('value', '>Q')]
class ipXaddr(nla_base):
__slots__ = ()
sql_type = 'TEXT'
fields = [('value', 's')]
family = None
def encode(self):
self['value'] = inet_pton(self.family, self.value)
nla_base.encode(self)
def decode(self):
nla_base.decode(self)
self.value = inet_ntop(self.family, self['value'])
class ip4addr(ipXaddr):