Info/LLDP.pm untested

filter: Statementstmt 0.0% 0 / 280 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 108 MC/DCmcdc 0.0% 0 / 58 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 600 CC 54 SCAR 32.3
CC
54
Cov
0%
lldp_port
72.0
_lldp_addr_index
56.0
lldp_ip
30.0
CRAP 25.3
1 # SNMP::Info::LLDP
2 #
3 # Copyright (c) 2018 Eric Miller
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the University of California, Santa Cruz nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29  
30 package SNMP::Info::LLDP;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info;
36  
37 @SNMP::Info::LLDP::ISA = qw/SNMP::Info Exporter/;
38 @SNMP::Info::LLDP::EXPORT_OK = qw//;
39  
40 our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE);
41  
42 $VERSION = '3.985003';
43  
44 %MIBS = (
45 'LLDP-MIB' => 'lldpLocSysCapEnabled',
46 'LLDP-EXT-MED-MIB' => 'lldpXMedMIB',
47 'LLDP-EXT-DOT1-MIB' => 'lldpXdot1MIB',
48 'LLDP-EXT-DOT3-MIB' => 'lldpXdot3MIB',
49 'LLDP-V2-MIB' => 'lldpV2LocSysCapEnabled',
50 );
51  
52 %GLOBALS = (
53 'lldp_sysname' => 'lldpLocSysName',
54 'lldp_sysdesc' => 'lldpLocSysDesc',
55 'lldp_sys_cap' => 'lldpLocSysCapEnabled',
56 'lldp_loc_id_type' => 'lldpLocChassisIdSubtype',
57 'lldp_loc_id_os' => 'lldpLocChassisId',
58 'lldp_v2_sys_cap' => 'lldpV2LocSysCapEnabled',
59 );
60  
61 %FUNCS = (
62 # LLDP-MIB::lldpLocManAddrTable
63 'lldp_lman_addr' => 'lldpLocManAddrIfId',
64  
65 # LLDP-MIB::LldpLocPortEntry
66 'lldp_lport_id' => 'lldpLocPortId',
67 'lldp_lport_desc' => 'lldpLocPortDesc',
68  
69 # LLDP-MIB::lldpRemTable
70 'lldp_rem_id_type' => 'lldpRemChassisIdSubtype',
71 'lldp_rem_id' => 'lldpRemChassisId',
72 'lldp_rem_pid_type' => 'lldpRemPortIdSubtype',
73 'lldp_rem_pid' => 'lldpRemPortId',
74 'lldp_rem_desc' => 'lldpRemPortDesc',
75 'lldp_rem_sysname' => 'lldpRemSysName',
76 'lldp_rem_sysdesc' => 'lldpRemSysDesc',
77 'lldp_rem_sys_cap' => 'lldpRemSysCapEnabled',
78 'lldp_rem_cap_spt' => 'lldpRemSysCapSupported',
79  
80 # LLDP-MIB::lldpXMedRemInventoryTable
81 'lldp_rem_hw_rev' => 'lldpXMedRemHardwareRev',
82 'lldp_rem_fw_rev' => 'lldpXMedRemFirmwareRev',
83 'lldp_rem_sw_rev' => 'lldpXMedRemSoftwareRev',
84 'lldp_rem_serial' => 'lldpXMedRemSerialNum',
85 'lldp_rem_vendor' => 'lldpXMedRemMfgName',
86 'lldp_rem_model' => 'lldpXMedRemModelName',
87 'lldp_rem_asset' => 'lldpXMedRemAssetID',
88  
89 'lldp_rem_media_cap' => 'lldpXMedRemCapCurrent',
90 'lldp_rem_media_cap_spt' => 'lldpXMedRemCapSupported',
91  
92 # LLDP-MIB::lldpRemManAddrTable
93 'lldp_rman_addr' => 'lldpRemManAddrIfSubtype',
94  
95 # LLDP-V2-MIB::lldpV2RemTable
96 'lldp_v2_rem_id_type' => 'lldpV2RemChassisIdSubtype',
97 'lldp_v2_rem_id' => 'lldpV2RemChassisId',
98 'lldp_v2_rem_pid_type' => 'lldpV2RemPortIdSubtype',
99 'lldp_v2_rem_pid' => 'lldpV2RemPortId',
100 'lldp_v2_rem_desc' => 'lldpV2RemPortDesc',
101 'lldp_v2_rem_sysname' => 'lldpV2RemSysName',
102 'lldp_v2_rem_sysdesc' => 'lldpV2RemSysDesc',
103 'lldp_v2_rem_sys_cap' => 'lldpV2RemSysCapEnabled',
104 'lldp_v2_rem_cap_spt' => 'lldpV2RemSysCapSupported',
105  
106 # LLDP-V2-MIB::lldpV2RemManAddrTable
107 'lldp_v2_rman_addr' => 'lldpV2RemManAddrIfSubtype',
108  
109 # LLDP-V2-MIB::lldpV2DestAddressTable
110 'lldp_v2_dest_mac' => 'lldpV2DestMacAddress',
111 );
112  
113 %MUNGE = (
114 'lldp_sysdesc' => \&SNMP::Info::munge_null,
115 'lldp_sysname' => \&SNMP::Info::munge_null,
116 'lldp_rem_sysname' => \&SNMP::Info::munge_null,
117 'lldp_rem_sysdesc' => \&SNMP::Info::munge_null,
118 'lldp_rem_port_desc' => \&SNMP::Info::munge_null,
119 'lldp_lport_id' => \&SNMP::Info::munge_null,
120 'lldp_lport_desc' => \&SNMP::Info::munge_null,
121 'lldp_sys_cap' => \&SNMP::Info::munge_bits,
122 'lldp_rem_sys_cap' => \&SNMP::Info::munge_bits,
123 'lldp_rem_cap_spt' => \&SNMP::Info::munge_bits,
124  
125 'lldp_rem_hw_rev' => \&SNMP::Info::munge_null,
126 'lldp_rem_fw_rev' => \&SNMP::Info::munge_null,
127 'lldp_rem_sw_rev' => \&SNMP::Info::munge_null,
128 'lldp_rem_serial' => \&SNMP::Info::munge_null,
129 'lldp_rem_vendor' => \&SNMP::Info::munge_null,
130 'lldp_rem_model' => \&SNMP::Info::munge_null,
131 'lldp_rem_asset' => \&SNMP::Info::munge_null,
132  
133 'lldp_rem_media_cap' => \&SNMP::Info::munge_bits,
134 'lldp_rem_media_cap_spt' => \&SNMP::Info::munge_bits,
135  
136 'lldp_v2_sys_cap' => \&SNMP::Info::munge_bits,
137 'lldp_v2_rem_sysname' => \&SNMP::Info::munge_null,
138 'lldp_v2_rem_sysdesc' => \&SNMP::Info::munge_null,
139 'lldp_v2_rem_sys_cap' => \&SNMP::Info::munge_bits,
140 'lldp_v2_rem_cap_spt' => \&SNMP::Info::munge_bits,
141 'lldp_v2_dest_mac' => \&SNMP::Info::munge_mac,
142 );
143  
144 sub hasLLDP {
145 my $lldp = shift;
146  
147 # We may be have LLDP, but nothing in lldpRemoteSystemsData Tables
148 # so we could be running LLDP but not return any useful information
149 my $lldp_cap = $lldp->lldp_sys_cap();
150 return 1 if defined $lldp_cap;
151 return 1 if defined $lldp->lldp_v2_sys_cap();
152  
153 # If the device doesn't return local system capabilities, fallback
154 # by checking if it would report neighbors
155 return 1 if defined $lldp->_lldp_source();
156  
157 return;
158 }
159  
160 # Neighbors come from one MIB per device: devices serving both report each
161 # neighbor in both. LLDP-MIB local data may sit beside LLDP-V2-MIB neighbors,
162 # so the remote tables decide. No neighbors is remembered as '' so empty
163 # walks are not repeated on every call; clear_cache resets it.
164 sub _lldp_source {
165 my $lldp = shift;
166  
167 return $lldp->{_lldp_source} || undef if exists $lldp->{_lldp_source};
168  
169 my $source;
170 if ( scalar keys %{ $lldp->lldp_rem_id() || {} } ) {
171 $source = 'v1';
172 }
173 elsif ( scalar keys %{ $lldp->lldp_v2_rem_id() || {} } ) {
174 $source = 'v2';
175 }
176 $lldp->{_lldp_source} = $source // '';
177 return $source;
178 }
179  
180 # Index of the nearest-bridge agent in lldpV2DestAddressTable
181 sub _lldp_v2_agent {
182 my $lldp = shift;
183  
184 return $lldp->{_lldp_v2_agent} || undef if exists $lldp->{_lldp_v2_agent};
185  
186 my $agent;
187 my $dest = $lldp->lldp_v2_dest_mac() || {};
188 foreach my $idx ( keys %$dest ) {
189 my $mac = $dest->{$idx} || '';
190 $agent = $idx if lc $mac eq '01:80:c2:00:00:0e';
191 }
192 $lldp->{_lldp_v2_agent} = $agent // '';
193 return $agent;
194 }
195  
196 # Remote column from the chosen MIB, named by its LLDP-MIB method. LLDP-V2-MIB
197 # rows may repeat a neighbor once per agent, so keep the nearest-bridge agent.
198 sub _lldp_rem {
199 my ( $lldp, $method, $partial ) = @_;
200  
201 my $source = $lldp->_lldp_source() || '';
202 return $lldp->$method($partial) || {} unless $source eq 'v2';
203  
204 ( my $v2_method = $method ) =~ s/^lldp_/lldp_v2_/;
205 my $rows = $lldp->$v2_method($partial) || {};
206 my $agent = $lldp->_lldp_v2_agent();
207 return $rows unless defined $agent;
208  
209 return { map { $_ => $rows->{$_} }
210 grep { ( ( split /\./, $_ )[2] // '' ) eq $agent } keys %$rows };
211 }
212  
213 sub lldp_if {
214 my $lldp = shift;
215 my $partial = shift;
216  
217 my $addr = $lldp->_lldp_rem( 'lldp_rem_pid', $partial );
218 return { map { $_ => ( split /\./, $_ )[1] } keys %$addr }
219 if ( $lldp->_lldp_source() || '' ) eq 'v2';
220  
221 my $r_i_descr = _lldp_reverse_all( $lldp->i_description() );
222 my $r_i_alias = _lldp_reverse_all( $lldp->i_alias() );
223  
224 my %lldp_if;
225 my %port_id_lookup;
226 foreach my $key ( keys %$addr ) {
227 my @aOID = split( '\.', $key );
228 my $port = $aOID[1];
229 next unless $port;
230  
231 # Local LLDP port may not equate to ifIndex, see LldpPortNumber
232 # TEXTUAL-CONVENTION in LLDP-MIB. Cross reference lldpLocPortDesc
233 # with ifDescr and ifAlias to get ifIndex, prefer ifDescr over
234 # ifAlias because using cross ref with description is correct
235 # behavior according to the LLDP-MIB. Some devices (eg H3C gear)
236 # seem to use ifAlias though.
237 # snmp::info #372 -> to make things even more complex some devices
238 # have different behaviour depending on os version (nx-os 6 vs 7)
239 # so we made this a function to allow overrides in more specific
240 # modules
241 my $lldp_desc = $lldp->lldp_lport_desc($port);
242 my $desc = $lldp_desc->{$port};
243  
244 # If cross reference is successful use it, otherwise stick with
245 # lldpRemLocalPortNum
246 $lldp_if{$key} = _lldp_desc_if( $desc, $r_i_descr )
247 // _lldp_desc_if( $desc, $r_i_alias )
248 // $lldp->_lldp_lport_id_if( $port, $r_i_descr, \%port_id_lookup )
249 // $port;
250 }
251 return \%lldp_if;
252 }
253  
254 # Value => [ every key holding it ], so a value two interfaces share is seen
255 sub _lldp_reverse_all {
256 my $table = shift || {};
257  
258 my %reverse;
259 push @{ $reverse{ $table->{$_} } }, $_ for keys %$table;
260 return \%reverse;
261 }
262  
263 # A description several interfaces share (Junos gives both members of a
264 # link the same ifAlias) names none of them
265 sub _lldp_desc_if {
266 my ( $desc, $r_table ) = @_;
267  
268 return unless defined $desc and length $desc;
269 my $ifindexes = $r_table->{$desc} or return;
270 return unless @$ifindexes == 1;
271 return $ifindexes->[0];
272 }
273  
274 # Some agents (Dell OS10) serve no lldpLocPortDesc, only lldpLocPortId
275 # holding the interface name, and number ports neither by ifIndex nor by
276 # bridge port. ifName is read by its MIB name because Layer3 i_name returns
277 # ifAlias. Both columns are walked whole, once, and only when a port needs
278 # them: one bulk walk costs fewer requests than a fetch per neighbor.
279 sub _lldp_lport_id_if {
280 my ( $lldp, $port, $r_i_descr, $lookup ) = @_;
281  
282 $lookup->{port_id} //= $lldp->lldp_lport_id() || {};
283 my $id = $lookup->{port_id}{$port};
284 return unless defined $id and length $id;
285  
286 $lookup->{if_name} //= { reverse %{ $lldp->ifName() || {} } };
287 return $lookup->{if_name}{$id} // _lldp_desc_if( $id, $r_i_descr );
288 }
289  
290 sub lldp_ip {
291 my $lldp = shift;
292 my $partial = shift;
293  
294 my $rman_addr = $lldp->_lldp_rem( 'lldp_rman_addr', $partial );
295  
296 my %lldp_ip;
297 foreach my $key ( keys %$rman_addr ) {
298 my ( $index, $proto, $addr ) = $lldp->_lldp_addr_index($key);
299 next unless defined $index;
300 next unless $proto == 1;
301 $lldp_ip{$index} = $addr;
302 }
303  
304 # Neighbors often advertise only a MAC chassis ID. Return 0.0.0.0 for
305 # them, which callers resolve by looking up c_id among known devices.
306 my $id_types = $lldp->_lldp_rem( 'lldp_rem_id_type', $partial );
307 foreach my $key ( keys %$id_types ) {
308 next if defined $lldp_ip{$key};
309 $lldp_ip{$key} = '0.0.0.0'
310 if ( $id_types->{$key} // '' ) eq 'macAddress';
311 }
312 return \%lldp_ip;
313 }
314  
315 sub lldp_ipv6 {
316 my $lldp = shift;
317 my $partial = shift;
318  
319 my $rman_addr = $lldp->_lldp_rem( 'lldp_rman_addr', $partial );
320  
321 my %lldp_ipv6;
322 foreach my $key ( keys %$rman_addr ) {
323 my ( $index, $proto, $addr ) = $lldp->_lldp_addr_index($key);
324 next unless defined $index;
325 next unless $proto == 2;
326 $lldp_ipv6{$index} = $addr;
327 }
328 return \%lldp_ipv6;
329 }
330  
331 sub lldp_mac {
332 my $lldp = shift;
333 my $partial = shift;
334  
335 my $rman_addr = $lldp->_lldp_rem( 'lldp_rman_addr', $partial );
336  
337 my %lldp_ipv6;
338 foreach my $key ( keys %$rman_addr ) {
339 my ( $index, $proto, $addr ) = $lldp->_lldp_addr_index($key);
340 next unless defined $index;
341 next unless $proto == 6;
342 $lldp_ipv6{$index} = $addr;
343 }
344 return \%lldp_ipv6;
345 }
346  
347 sub lldp_addr {
348 my $lldp = shift;
349 my $partial = shift;
350  
351 my $rman_addr = $lldp->_lldp_rem( 'lldp_rman_addr', $partial );
352  
353 my %lldp_ip;
354 foreach my $key ( keys %$rman_addr ) {
355 my ( $index, $proto, $addr ) = $lldp->_lldp_addr_index($key);
356 next unless defined $index;
357 $lldp_ip{$index} = $addr;
358 }
359 return \%lldp_ip;
360 }
361  
362 sub lldp_addrs {
363 my $lldp = shift;
364 my $partial = shift;
365  
366 my $rman_addr = $lldp->_lldp_rem( 'lldp_rman_addr', $partial );
367  
368 my %rows;
369 foreach my $key ( keys %$rman_addr ) {
370 my ( $index, $proto, $addr ) = $lldp->_lldp_addr_index($key);
371 next unless defined $index;
372 push @{ $rows{$index} }, [ $proto, $addr ];
373 }
374  
375 my %lldp_addrs;
376 foreach my $index ( keys %rows ) {
377 $lldp_addrs{$index} = [
378 map { $_->[1] }
379 sort { $a->[0] <=> $b->[0] or $a->[1] cmp $b->[1] }
380 @{ $rows{$index} }
381 ];
382 }
383 return \%lldp_addrs;
384 }
385  
386 # For a class whose lldp_ip and lldp_ipv6 read another table than
387 # lldpRemManAddrTable: one address per family, from those methods.
388 # 0.0.0.0 is lldp_ip's stand-in for a neighbor with no address.
389 sub _lldp_addrs_from_families {
390 my ( $lldp, $partial ) = @_;
391  
392 my %lldp_addrs;
393 foreach my $method (qw/lldp_ip lldp_ipv6/) {
394 my $addrs = $lldp->$method($partial) || {};
395 foreach my $key ( keys %$addrs ) {
396 my $addr = $addrs->{$key};
397 next unless defined $addr and length $addr;
398 next if $addr eq '0.0.0.0';
399 push @{ $lldp_addrs{$key} }, $addr;
400 }
401 }
402 return \%lldp_addrs;
403 }
404  
405 sub lldp_port {
406 my $lldp = shift;
407 my $partial = shift;
408  
409 my $pdesc = $lldp->_lldp_rem( 'lldp_rem_desc', $partial );
410 my $pid = $lldp->_lldp_rem( 'lldp_rem_pid', $partial );
411 my $ptype = $lldp->_lldp_rem( 'lldp_rem_pid_type', $partial );
412 my $desc = $lldp->_lldp_rem( 'lldp_rem_sysdesc', $partial );
413  
414 my %lldp_port;
415 foreach my $key ( sort keys %$pid ) {
416 my $port = $pdesc->{$key};
417 my $type = $ptype->{$key};
418 if ( $type
419 and ( $type eq 'interfaceName' or $type eq 'local' )
420 and ( defined $pid->{$key} and $pid->{$key} !~ /^\d+$/ ) )
421 {
422  
423 # If the pid claims to be an interface name,
424 # believe it.
425 $port = $pid->{$key};
426 }
427 unless ($port) {
428 $port = $pid->{$key};
429 next unless $port;
430 next unless $type;
431  
432 # May need to format other types in the future, i.e. Network address
433 if ( $type =~ /mac/ ) {
434 $port = SNMP::Info::munge_mac($port)
435 }
436 }
437  
438 # Avaya/Nortel lldpRemPortDesc doesn't match ifDescr, but we can still
439 # figure out slot.port based upon lldpRemPortDesc
440 if ( defined $desc->{$key}
441 && $desc->{$key}
442 =~ /^Ethernet\s(?:Routing\s)?Switch\s\d|^Virtual\sServices\sPlatform\s\d/
443 && $port =~ /^(Unit\s+(\d+)\s+)?Port\s+(\d+)$/ )
444 {
445 $port = defined $1 ? "$2.$3" : "1.$3";
446 }
447  
448 $lldp_port{$key} = $port;
449 }
450 return \%lldp_port;
451 }
452  
453 sub _lldp_id_string {
454 my $lldp = shift;
455 my $id = shift;
456 my $type = shift // '';
457  
458 # May need to format other types in the future
459 if ( $type =~ /mac/ ) {
460 # Some agents, such as SONiC and FortiSwitch, serve the MAC as text
461 my $text = lc( $id // '' );
462 $text = undef unless $text =~ /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/;
463 $id = SNMP::Info::munge_mac($id) // $text;
464 } elsif ( $type eq 'networkAddress' ) {
465 if ( length( unpack( 'H*', $id ) ) == 10 ) {
466  
467 # IP address - first octet is IANA Address Family Number, need
468 # walk with IPv6
469 my @octets
470 = ( map { sprintf "%02x", $_ } unpack( 'C*', $id ) )
471 [ 1 .. 4 ];
472 $id = join '.', map { hex($_) } @octets;
473 }
474 }
475  
476 return $id;
477 }
478  
479 sub lldp_id {
480 my $lldp = shift;
481 my $partial = shift;
482  
483 my $ch_type = $lldp->_lldp_rem( 'lldp_rem_id_type', $partial );
484 my $ch = $lldp->_lldp_rem( 'lldp_rem_id', $partial );
485  
486 my %lldp_id;
487 foreach my $key ( keys %$ch ) {
488 my $id = $ch->{$key};
489 next unless $id;
490 my $type = $ch_type->{$key};
491 next unless $type;
492  
493 $id = $lldp->_lldp_id_string($id, $type);
494 $lldp_id{$key} = $id;
495 }
496 return \%lldp_id;
497 }
498  
499 sub lldp_loc_id {
500 my $lldp = shift;
501 my $ch_type = $lldp->lldp_loc_id_type();
502 my $ch = $lldp->lldp_loc_id_os();
503 return $lldp->_lldp_id_string($ch, $ch_type);
504 }
505  
506 sub lldp_platform {
507 my $lldp = shift;
508 my $partial = shift;
509  
510 my $rid = $lldp->_lldp_rem( 'lldp_rem_id', $partial );
511 my $desc = $lldp->_lldp_rem( 'lldp_rem_sysdesc', $partial );
512 my $name = $lldp->_lldp_rem( 'lldp_rem_sysname', $partial );
513  
514 my %lldp_platform;
515 foreach my $key ( keys %$rid ) {
516 $lldp_platform{$key} = $desc->{$key} || $name->{$key};
517 }
518 return \%lldp_platform;
519 }
520  
521 sub lldp_cap {
522 my $lldp = shift;
523 my $partial = shift;
524  
525 my $lldp_caps = $lldp->_lldp_rem( 'lldp_rem_cap_spt', $partial );
526  
527 # Encoded as BITS which Perl Net-SNMP implementation doesn't seem to
528 # be able to enumerate for us, so we have to get it from the MIB
529 # and enumerate ourselves
530 my $cap_leaf
531 = ( $lldp->_lldp_source() || '' ) eq 'v2'
532 ? 'LLDP-V2-MIB::lldpV2RemSysCapEnabled'
533 : 'LLDP-MIB::lldpRemSysCapEnabled';
534 my $oid = SNMP::translateObj( $cap_leaf, 0, 1 ) || '';
535 my $enums = (
536 ( ref {} eq ref $SNMP::MIB{$oid}{'enums'} )
537 ? $SNMP::MIB{$oid}{'enums'}
538 : {}
539 );
540 my %r_enums = reverse %$enums;
541  
542 my %lldp_cap;
543 foreach my $key ( keys %$lldp_caps ) {
544 my $cap_bits = $lldp_caps->{$key};
545 next unless $cap_bits;
546  
547 my $count = 0;
548 foreach my $bit ( split //, $cap_bits ) {
549 if ($bit) {
550 push( @{ $lldp_cap{$key} }, $r_enums{$count} );
551 }
552 $count++;
553 }
554 }
555 return \%lldp_cap;
556 }
557  
558 sub lldp_media_cap {
559 my $lldp = shift;
560 my $partial = shift;
561  
562 my $lldp_caps = $lldp->lldp_rem_media_cap_spt($partial) || {};
563  
564 # Encoded as BITS which Perl Net-SNMP implementation doesn't seem to
565 # be able to enumerate for us, so we have to get it from the MIB
566 # and enumerate ourselves
567 my $oid = SNMP::translateObj( 'lldpXMedRemCapCurrent', 0, 1 ) || '';
568 my $enums = (
569 ( ref {} eq ref $SNMP::MIB{$oid}{'enums'} )
570 ? $SNMP::MIB{$oid}{'enums'}
571 : {}
572 );
573 my %r_enums = reverse %$enums;
574  
575 my %lldp_cap;
576 foreach my $key ( keys %$lldp_caps ) {
577 my $cap_bits = $lldp_caps->{$key};
578 next unless $cap_bits;
579  
580 my $count = 0;
581 foreach my $bit ( split //, $cap_bits ) {
582 if ($bit) {
583 push( @{ $lldp_cap{$key} }, $r_enums{$count} );
584 }
585 $count++;
586 }
587 }
588 return \%lldp_cap;
589 }
590  
591 #sub root_ip {
592 # my $lldp = shift;
593 #
594 # my $man_addr = $lldp->lldp_lman_addr() || {};
595 #
596 # foreach my $key (keys %$man_addr) {
597 # my @oids = split(/\./, $key);
598 # my $proto = shift(@oids);
599 # my $length = shift(@oids);
600 # # IPv4
601 # if ($proto == 1) {
602 # my $addr = join('.',@oids);
603 # return $addr if (defined $addr and $lldp->snmp_connect_ip($addr));
604 # }
605 # }
606 # return;
607 #}
608  
609 # Break up the lldpRemManAddrTable INDEX into common index, protocol,
610 # and address.
611 sub _lldp_addr_index {
612 my $lldp = shift;
613 my $idx = shift;
614  
615 # LLDP-V2-MIB adds the destination address index
616 my $fields = ( $lldp->_lldp_source() || '' ) eq 'v2' ? 4 : 3;
617  
618 my @oids = split( /\./, $idx );
619 my $index = join( '.', splice( @oids, 0, $fields ) );
620 my $proto = shift(@oids);
621 shift(@oids) if scalar @oids > 4; # $length
622  
623 # Some agents serve rows whose index stops before the address type or
624 # the address, as a single number on some Cisco Fabric Interconnects
625 return unless defined $proto and @oids;
626  
627 # IPv4
628 if ( $proto == 1 ) {
629 return ( $index, $proto, join( '.', @oids ) );
630 }
631  
632 # IPv6
633 elsif ( $proto == 2 ) {
634 return ( $index, $proto,
635 join( ':', unpack( '(H4)*', pack( 'C*', @oids ) ) ) );
636 }
637  
638 # MAC
639 elsif ( $proto == 6 ) {
640 return ( $index, $proto,
641 join( ':', map { sprintf "%02x", $_ } @oids ) );
642 }
643  
644 # TODO - Other protocols may be used as well; implement when needed?
645 else {
646 return;
647 }
648 }
649  
650 1;
651 __END__