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_lldp_802_3_mac_phy(_switchinfo, thisportinfo, tlvptr, tlvlen, pktend):
"""Decode 802.3 MAC/PHY TLV org-specific TLV (or not...)"""
if tlvlen != 5:
print(
"Invalid %d byte LLDP 802.3 MAC/PHY information (Annex G.2)." % tlvlen, file=stderr
)
return
autoneg_status = pySwitchDiscovery._byte0(tlvptr)
thisportinfo["autoneg_supported"] = (autoneg_status & 0x01) == 0x01
thisportinfo["autoneg_enabled"] = (autoneg_status & 0x10) == 0x10
# @TODO: Need to add info about autonegotiation speeds/duplexes supported (pmd_autoneg)
# I'm ignoring the PMD_AUTONEG field because it's not horribly important
# and it's incorrect on the switches in my test lab - and it's kinda
# complicated to decode and figure out ;-)
#
# It has one bit for each speed/duplex it is _capable_ of autonegotiating.
# PMD auto-negotiation advertised capability is described as the field
# ifMauAutoNegCapAdvertisedBits in RFC 3636 (page 42)
# pmd_autoneg_addr = pySwitchDiscovery._byte1addr(tlvptr)
# pmd_autoneg = tlv_get_guint16(pmd_autoneg_addr, pktend)
mau_addr = pySwitchDiscovery._byteNaddr(tlvptr, 3)
mau_type = tlv_get_guint16(mau_addr, pktend)
mauinfo = pySwitchDiscovery.dot3MauTypes(mau_type)
for key in mauinfo:
def _decode_lldp_802_1(_switchinfo, thisportinfo, tlvptr, tlvlen, pktend):
"""Decode 802.1 LLDP org-specific TLV sets (or not...)"""
subtype = pySwitchDiscovery._byte0(tlvptr)
tlvstart = pySwitchDiscovery._byte1addr(tlvptr)
if subtype == 1:
thisportinfo["pvid"] = tlv_get_guint16(tlvstart, pktend)
elif subtype == 2:
ppstatus = pySwitchDiscovery._byte0(tlvstart)
thisportinfo["pp_vlan_capable"] = (ppstatus & 2) == 2
thisportinfo["pp_vlan_enabled"] = (ppstatus & 4) == 4
thisportinfo["ppvid"] = tlv_get_guint16(pySwitchDiscovery._byte1addr(tlvstart), pktend)
elif subtype == 3:
vlannameaddr = pySwitchDiscovery._byteNaddr(tlvptr, 3)
namelen = pySwitchDiscovery._byte0(vlannameaddr)
if namelen != tlvlen - 4:
print(
"F.4: invalid name length %s out of total of %s" % (namelen, tlvlen),
file=stderr,
)
pySwitchDiscovery._dump_c_bytes("PACKET:", tlvptr, tlvlen)
# print ('ORG_OUI: 0x%06x' % oui, file=stderr)
tlv3ptr = pySwitchDiscovery._byteNaddr(tlvptr, 3)
if oui == 0x0080C2:
pySwitchDiscovery._decode_lldp_802_1(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x00120F:
pySwitchDiscovery._decode_lldp_802_3(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x0012BB:
pySwitchDiscovery._decode_lldp_med(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x00CEFF:
pySwitchDiscovery._decode_lldp_profibus(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x00CECF:
pySwitchDiscovery._decode_lldp_hytec(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
else:
print(
"Ignored %d bytes of unknown LLDP org-specific extensions. OUI: %06x."
% (tlvlen, oui),
file=stderr,
)
def _decode_lldp_org_specific(switchinfo, thisportinfo, tlvptr, tlvlen, pktend):
"""Decode LLDP org-specific TLV sets (or not...)"""
oui = tlv_get_guint24(tlvptr, pktend)
# print ('ORG_OUI: 0x%06x' % oui, file=stderr)
tlv3ptr = pySwitchDiscovery._byteNaddr(tlvptr, 3)
if oui == 0x0080C2:
pySwitchDiscovery._decode_lldp_802_1(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x00120F:
pySwitchDiscovery._decode_lldp_802_3(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x0012BB:
pySwitchDiscovery._decode_lldp_med(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x00CEFF:
pySwitchDiscovery._decode_lldp_profibus(
switchinfo, thisportinfo, tlv3ptr, tlvlen - 3, pktend
)
elif oui == 0x00CECF:
pySwitchDiscovery._decode_lldp_hytec(
# Civic Address LCI
# The Civic Address location data format uses common street address format, as described
# in RFC4776.
if subtype == 5:
pySwitchDiscovery._get_med_string(switchinfo, "hardware_revision", tlvptr, tlvlen)
elif subtype == 6:
pySwitchDiscovery._get_med_string(switchinfo, "firmware_revision", tlvptr, tlvlen)
elif subtype == 7:
pySwitchDiscovery._get_med_string(switchinfo, "software_revision", tlvptr, tlvlen)
elif subtype == 8:
pySwitchDiscovery._get_med_string(switchinfo, "serial_number", tlvptr, tlvlen)
elif subtype == 9:
pySwitchDiscovery._get_med_string(switchinfo, "manufacturer", tlvptr, tlvlen)
elif subtype == 10:
pySwitchDiscovery._get_med_string(switchinfo, "model", tlvptr, tlvlen)
elif subtype == 11:
pySwitchDiscovery._get_med_string(switchinfo, "asset_id", tlvptr, tlvlen)
else:
print(
"Ignored %d bytes of LLDP-MED extensions (subtype %d)." % (tlvlen, subtype),
file=stderr,
)
):
sloc = pySwitchDiscovery._byte1addr(tlvptr)
value = u_string_at(sloc, tlvlen - 1)
elif tlvtype == LLDP_TLV_CHID: #############################################
value = pySwitchDiscovery._decode_lldp_chid(tlvptr, tlvlen)
elif tlvtype == LLDP_TLV_MGMT_ADDR: #########################################
addrlen = pySwitchDiscovery._byte0(tlvptr)
byte1addr = pySwitchDiscovery._byte1addr(tlvptr)
value = pySwitchDiscovery._decode_netaddr(byte1addr, addrlen)
elif tlvtype == LLDP_TLV_SYS_CAPS: #########################################
byte0 = pySwitchDiscovery._byte0(tlvptr)
byte1 = pySwitchDiscovery._byteN(tlvptr, 1)
byte2 = pySwitchDiscovery._byteN(tlvptr, 2)
byte3 = pySwitchDiscovery._byteN(tlvptr, 3)
caps0 = byte0 << 8 | byte1
caps1 = byte2 << 8 | byte3
# The values we assign here have many possibilities
# pylint: disable=R0204
value = pyConfigContext()
mask = 2
for j in range(1, 7):
if caps0 & mask:
value[capnames[j]] = (caps1 & mask) != 0
mask <<= 1
elif tlvtype == LLDP_TLV_ORG_SPECIFIC: ######################################
pySwitchDiscovery._decode_lldp_org_specific(
switchinfo, thisportinfo, tlvptr, tlvlen, pktend
)
isswitchinfo = True # Gotta do _something_...
else:
(tlvname, isswitchinfo) = pySwitchDiscovery.cdpnames[tlvtype]
if tlvtype == CDP_TLV_DEVID:
value = u_string_at(tlvptr, tlvlen - 4)
elif tlvtype == CDP_TLV_ADDRESS:
# 4 byte count + 'count' addresses
value = pySwitchDiscovery.getcdpaddresses(tlvlen, tlvptr, pktend)
elif tlvtype == CDP_TLV_PORTID:
value = u_string_at(tlvptr, tlvlen - 4)
elif tlvtype == CDP_TLV_CAPS:
# bytez = [ '%02x' % pySwitchDiscovery._byteN(tlvptr, j) for j in range(0, tlvlen)]
# print('CAPBYTES = ', bytez, file=stderr)
caps = pySwitchDiscovery.getNint(tlvptr, 4, pktend)
# print('CAPS IS: 0x%08x (%d bytes)' % (caps, tlvlen)), file=stderr)
value = pySwitchDiscovery.construct_cdp_caps(caps)
elif tlvtype == CDP_TLV_VERS:
value = u_string_at(tlvptr, tlvlen - 4)
elif tlvtype == CDP_TLV_PLATFORM:
value = u_string_at(tlvptr, tlvlen - 4)
elif tlvtype == CDP_TLV_POWER:
tlen = tlvlen if tlvlen <= 2 else 2
value = pySwitchDiscovery.getNint(tlvptr, tlen, pktend)
elif tlvtype == CDP_TLV_MTU:
value = pySwitchDiscovery.getNint(tlvptr, tlvlen, pktend)
elif tlvtype == CDP_TLV_SYSTEM_NAME:
value = u_string_at(tlvptr, tlvlen - 4)
elif tlvtype == CDP_TLV_MANAGEMENT_ADDR:
# 4 byte count + 'count' addresses
value = pySwitchDiscovery.getcdpaddresses(tlvlen, tlvptr, pktend)
elif tlvtype == CDP_TLV_VTPDOMAIN:
value = u_string_at(tlvptr, tlvlen - 4)
def _decode_lldp_802_3(switchinfo, thisportinfo, tlvptr, tlvlen, pktend):
"""Decode 802.3 LLDP org-specific TLV sets (or not...)"""
subtype = pySwitchDiscovery._byte0(tlvptr)
if subtype == 1:
pySwitchDiscovery._decode_lldp_802_3_mac_phy(
switchinfo, thisportinfo, pySwitchDiscovery._byte1addr(tlvptr), tlvlen - 1, pktend
)
return
if subtype == LLDP_ORG802_3_MTU: # G.5: Maximum Frame Size TLV (4)
mtuaddr = pySwitchDiscovery._byte1addr(tlvptr)
thisportinfo["mtu"] = tlv_get_guint16(mtuaddr, pktend)
return
print(
"Ignored %d bytes of LLDP 802.3 extensions (Annex G, subtype %d)." % (tlvlen, subtype),
file=stderr,
)
or tlvtype == LLDP_TLV_SYS_DESCR
): #########################################
value = u_string_at(tlvptr, tlvlen)
elif tlvtype == LLDP_TLV_PID: ###############################################
pidtype = pySwitchDiscovery._byte0(tlvptr)
if (
pidtype == LLDP_PIDTYPE_ALIAS
or pidtype == LLDP_PIDTYPE_IFNAME
or pidtype == LLDP_PIDTYPE_LOCAL
):
sloc = pySwitchDiscovery._byte1addr(tlvptr)
value = u_string_at(sloc, tlvlen - 1)
elif tlvtype == LLDP_TLV_CHID: #############################################
value = pySwitchDiscovery._decode_lldp_chid(tlvptr, tlvlen)
elif tlvtype == LLDP_TLV_MGMT_ADDR: #########################################
addrlen = pySwitchDiscovery._byte0(tlvptr)
byte1addr = pySwitchDiscovery._byte1addr(tlvptr)
value = pySwitchDiscovery._decode_netaddr(byte1addr, addrlen)
elif tlvtype == LLDP_TLV_SYS_CAPS: #########################################
byte0 = pySwitchDiscovery._byte0(tlvptr)
byte1 = pySwitchDiscovery._byteN(tlvptr, 1)
byte2 = pySwitchDiscovery._byteN(tlvptr, 2)
byte3 = pySwitchDiscovery._byteN(tlvptr, 3)
caps0 = byte0 << 8 | byte1
caps1 = byte2 << 8 | byte3
# The values we assign here have many possibilities
# pylint: disable=R0204
value = pyConfigContext()
def decode_discovery(host, interface, instance, wallclock, pktstart, pktend):
"Return a JSON packet corresponding to the given switch discovery packet"
if is_valid_lldp_packet(pktstart, pktend):
# print('>>>>>>>>>>>>>>>LLDP PACKET', file=stderr)
return pySwitchDiscovery._decode_lldp(
host, interface, instance, wallclock, pktstart, pktend
)
if is_valid_cdp_packet(pktstart, pktend):
# print('>>>>>>>>>>>>>>>CDP PACKET', file=stderr)
return pySwitchDiscovery._decode_cdp(
host, interface, instance, wallclock, pktstart, pktend
)
raise ValueError("Malformed Switch Discovery Packet")