Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
capnames = [
None,
CMAconsts.ROLE_repeater,
CMAconsts.ROLE_bridge,
CMAconsts.ROLE_AccessPoint,
CMAconsts.ROLE_router,
CMAconsts.ROLE_phone,
CMAconsts.ROLE_DOCSIS,
CMAconsts.ROLE_Station,
]
sourcemacptr = pySwitchDiscovery._byteNaddr(cast(pktstart, cClass.guint8), 6)
if not sourcemacptr:
return metadata
Cmacaddr = netaddr_mac48_new(sourcemacptr)
sourcemac = pyNetAddr(None, Cstruct=Cmacaddr)
this = cast(get_lldptlv_first(pktstart, pktend), cClass.guint8)
while this:
tlvtype = get_lldptlv_type(this, pktend)
tlvlen = get_lldptlv_len(this, pktend)
tlvptr = cast(get_lldptlv_body(this, pktend), cClass.guint8)
# print("EXAMINE:", tlvptr, tlvtype, tlvlen, file=stderr)
value = None
if tlvtype not in pySwitchDiscovery.lldpnames:
print("Cannot find tlvtype %d" % tlvtype, file=stderr)
tlvtype = None
else:
(tlvname, isswitchinfo) = pySwitchDiscovery.lldpnames[tlvtype]
if (
tlvtype == LLDP_TLV_PORT_DESCR
c_netaddr = None
if byte0 == ADDR_FAMILY_IPV6:
if addrlen != 17:
return None
c_netaddr = netaddr_ipv6_new(byte1addr, 0)
elif byte0 == ADDR_FAMILY_IPV4:
if addrlen != 5:
return None
c_netaddr = netaddr_ipv4_new(byte1addr, 0)
elif byte0 == ADDR_FAMILY_802:
if addrlen == 7:
c_netaddr = netaddr_mac48_new(byte1addr)
elif addrlen == 9:
c_netaddr = netaddr_mac64_new(byte1addr)
if c_netaddr is not None:
return str(pyNetAddr(None, Cstruct=c_netaddr))
return None
chidtype == LLDP_CHIDTYPE_COMPONENT
or chidtype == LLDP_CHIDTYPE_ALIAS
or chidtype == LLDP_CHIDTYPE_IFNAME
or chidtype == LLDP_CHIDTYPE_LOCAL
):
sloc = pySwitchDiscovery._byte1addr(tlvptr)
return u_string_at(sloc, tlvlen - 1)
elif chidtype == LLDP_CHIDTYPE_MACADDR:
byte1addr = pySwitchDiscovery._byte1addr(tlvptr)
Cmacaddr = None
if tlvlen == 7:
Cmacaddr = netaddr_mac48_new(byte1addr)
elif tlvlen == 9:
Cmacaddr = netaddr_mac64_new(byte1addr)
if Cmacaddr is not None:
return pyNetAddr(None, Cstruct=Cmacaddr)
elif chidtype == LLDP_CHIDTYPE_NETADDR:
byte1addr = pySwitchDiscovery._byte1addr(tlvptr)
return pySwitchDiscovery._decode_netaddr(byte1addr, tlvlen - 1)
return None
break
addrlen = pySwitchDiscovery.getNint(
pySwitchDiscovery._byteNaddr(cast(tlvstart, cClass.guint8), offset), 2, pktend
)
if protolen == 1 and addrlen == 4 and prototype == 0xCC:
addrstr = ""
for j in (offset + 2, offset + 3, offset, offset + 1):
addrstr += chr(pySwitchDiscovery._byteN(tlvstart, j))
addr = netaddr_ipv4_new(c_char_p(addrstr.encode("utf8")), 0)
elif protolen == 8 and addrlen == 16 and prototype == 0x86DD:
# protocol type == 0xAAAA0300000086DD
addr = netaddr_ipv6_new(
pySwitchDiscovery._byteNaddr(cast(tlvstart, cClass.guint8), offset), 0
)
if addr is not None:
pyaddr = pyNetAddr(Cstruct=addr, addrstring=None)
retlist.append(pyaddr)
offset += addrlen
if len(retlist) == 0:
return None
if len(retlist) == 1:
return retlist[0]
return retlist
def _init_from_binary(self, frametype, addrstring, port):
"""Initialize a pyIpAddrFrame from a binary argument"""
addrlen = len(addrstring)
self._pyNetAddr = pyNetAddr(addrstring, port=port)
if self._pyNetAddr is None:
raise ValueError("Invalid initializer.")
addrstr = create_string_buffer(addrlen)
for j in range(0, addrlen):
addrstr[j] = addrstring[j]
if addrlen == 4:
Cstruct = ipportframe_ipv4_new(frametype, port, addrstr)
elif addrlen == 16:
Cstruct = ipportframe_ipv6_new(frametype, port, addrstr)
else:
raise ValueError("Bad address length: %d" % addrlen)
self.port = port
if port == 0:
raise ValueError("zero port")
if not Cstruct:
raise ValueError("invalid initializer")
return int(self._Cstruct[0].u.intvalue)
elif vtype == CFG_STRING:
strval = self._Cstruct[0].u.strvalue
value = strval.data.decode("utf8") if isinstance(strval, String) else strval
assert isinstance(value, str)
return value
# return u_string_at(self._Cstruct[0].u.strvalue)
return str(self._Cstruct[0].u.strvalue)
elif vtype == CFG_FLOAT:
return float(self._Cstruct[0].u.floatvalue)
elif vtype == CFG_CFGCTX:
# We're creating a new reference to the pre-existing NetAddr
CCref(self._Cstruct[0].u.cfgctxvalue)
return pyConfigContext(Cstruct=self._Cstruct[0].u.cfgctxvalue)
elif vtype == CFG_NETADDR:
net = pyNetAddr(None, Cstruct=self._Cstruct[0].u.addrvalue)
# We're creating a new reference to the pre-existing NetAddr
CCref(net._Cstruct)
return net
elif vtype == CFG_FRAME:
# Cstruct2Frame calls CCref() - so we don't need to
return pyFrame.Cstruct2Frame(self._Cstruct[0].u.framevalue)
elif vtype == CFG_ARRAY:
# An Array is a linked list (GSList) of ConfigValue objects...
ret = []
this = self._Cstruct[0].u.arrayvalue
while this:
dataptr = cast(this[0].data, struct__GSList._fields_[0][1])
dataptr = cast(dataptr, cClass.ConfigValue)
CCref(dataptr)
thisobj = pyConfigValue(cast(dataptr, cClass.ConfigValue)).get()
ret.append(thisobj)
metadata = pyConfigContext(
init={
"discovertype": "__LinkDiscovery",
"description": "Link Level Switch Discovery (cdp)",
"source": "_decode_cdp()",
"host": host,
"instance": instance,
"localtime": str(wallclock),
"data": switchinfo,
}
)
sourcemacptr = pySwitchDiscovery._byteNaddr(cast(pktstart, cClass.guint8), 6)
if not sourcemacptr:
return metadata
Cmacaddr = netaddr_mac48_new(sourcemacptr)
sourcemac = pyNetAddr(None, Cstruct=Cmacaddr)
this = cast(get_cdptlv_first(pktstart, pktend), cClass.guint8)
while this:
tlvtype = get_cdptlv_type(this, pktend)
tlvlen = get_cdptlv_len(this, pktend)
tlvptr = cast(get_cdptlv_body(this, pktend), cClass.guint8)
this = cast(get_cdptlv_next(this, pktend), cClass.guint8)
value = None
# Each of the different cases handles 'value' differently
# pylint: disable=R0204
if tlvtype not in pySwitchDiscovery.cdpnames:
tlvname = "TLV_0x%02x" % tlvtype
isswitchinfo = True # Gotta do _something_...
else:
(tlvname, isswitchinfo) = pySwitchDiscovery.cdpnames[tlvtype]
if tlvtype == CDP_TLV_DEVID:
value = u_string_at(tlvptr, tlvlen - 4)
def getaddr(self, name):
"""Return the NetAddr associated with "name\""""
naddr = self._Cstruct[0].getaddr(self._Cstruct, name)
if naddr:
naddr = cast(naddr, cClass.NetAddr)
# We're creating a new reference to the pre-existing NetAddr
CCref(naddr)
return pyNetAddr(None, Cstruct=naddr)
raise IndexError("No such NetAddr value [%s]" % name)