#255 IPv6 support

This commit is contained in:
Eric A. Miller
2018-05-06 14:02:35 -04:00
parent b263fa4375
commit 721193ccb8
7 changed files with 75 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ Module::Build->new(
requires => { requires => {
'SNMP' => '0', 'SNMP' => '0',
'Math::BigInt' => '0', 'Math::BigInt' => '0',
'NetAddr::IP' => '4.068',
}, },
recommends => { recommends => {
'PPI' => '0', 'PPI' => '0',

View File

@@ -1,3 +1,9 @@
Version 3.61
[ENHANCEMENTS]
* #255 IPv6 support
Version 3.60 (2018-05-06) Version 3.60 (2018-05-06)
[ENHANCEMENTS] [ENHANCEMENTS]

View File

@@ -29,6 +29,7 @@
}, },
"requires" : { "requires" : {
"Math::BigInt" : "0", "Math::BigInt" : "0",
"NetAddr::IP" : "4.068",
"SNMP" : "0" "SNMP" : "0"
} }
}, },

View File

@@ -414,6 +414,7 @@ recommends:
PPI: '0' PPI: '0'
requires: requires:
Math::BigInt: '0' Math::BigInt: '0'
NetAddr::IP: '4.068'
SNMP: '0' SNMP: '0'
resources: resources:
IRC: irc://irc.freenode.org/#netdisco IRC: irc://irc.freenode.org/#netdisco

7
README
View File

@@ -1988,6 +1988,13 @@ SNMP::INFO INTERNALS
Takes an OID and return the object name if the right MIB is loaded. Takes an OID and return the object name if the right MIB is loaded.
Internally Used Functions Internally Used Functions
resolve_desthost()
Takes the SNMP::Session "DestHost" argument and determines if it is
an 'IPv4' or 'IPv6' host. 'IPv6' hosts are prefixed with the "udp6:"
"transport-specifier" as required by the undelying "Net-SNMP"
library. If unable to determine the type of address or resolve a DNS
name, 'undef' will be returned causing the session creation to fail.
$info->init() $info->init()
Used internally. Loads all entries in %MIBS. Used internally. Loads all entries in %MIBS.

View File

@@ -16,6 +16,7 @@ use Exporter;
use SNMP; use SNMP;
use Carp; use Carp;
use Math::BigInt; use Math::BigInt;
use NetAddr::IP::Lite ':lower';
@SNMP::Info::ISA = qw/Exporter/; @SNMP::Info::ISA = qw/Exporter/;
@SNMP::Info::EXPORT_OK = qw//; @SNMP::Info::EXPORT_OK = qw//;
@@ -1321,6 +1322,11 @@ sub new {
delete $sess_args{MibDirs}; delete $sess_args{MibDirs};
} }
# For IPv6 hosts set transport
if ( defined $sess_args{DestHost} ) {
$sess_args{DestHost} = resolve_desthost($sess_args{DestHost});
}
$new_obj->{nosuch} = $args{RetryNoSuch} || $NOSUCH; $new_obj->{nosuch} = $args{RetryNoSuch} || $NOSUCH;
# Initialize mibs if not done # Initialize mibs if not done
@@ -3745,6 +3751,32 @@ sub munge_e_type {
=over =over
=item resolve_desthost()
Takes the SNMP::Session C<DestHost> argument and determines if it is an
'IPv4' or 'IPv6' host. 'IPv6' hosts are prefixed with the C<udp6:>
C<transport-specifier> as required by the undelying C<Net-SNMP> library.
If unable to determine the type of address or resolve a DNS name, 'undef'
will be returned causing the session creation to fail.
=cut
sub resolve_desthost {
my $desthost = shift;
my $ip = NetAddr::IP::Lite->new($desthost);
if ($ip and $ip->bits == 32) {
return $ip->addr;
}
elsif ($ip and $ip->bits == 128) {
return 'udp6:' . $ip->addr;
}
else {
return;
}
}
=item $info->init() =item $info->init()
Used internally. Loads all entries in %MIBS. Used internally. Loads all entries in %MIBS.
@@ -4514,6 +4546,9 @@ sub snmp_connect_ip {
my $comm = $self->snmp_comm(); my $comm = $self->snmp_comm();
return if $self->{Offline}; return if $self->{Offline};
$ip = resolve_desthost($ip);
return unless $ip;
return if ( $ip eq '0.0.0.0' ) or ( $ip =~ /^127\./ ); return if ( $ip eq '0.0.0.0' ) or ( $ip =~ /^127\./ );
# Create session object # Create session object
@@ -4543,7 +4578,6 @@ sub snmp_connect_ip {
} }
return 1; return 1;
} }
=item modify_port_list(portlist,offset,replacement) =item modify_port_list(portlist,offset,replacement)

View File

@@ -781,6 +781,24 @@ sub munge_e_type : Tests(3) {
'.100.3.6.1.2.1.11.4', 'OID returned when unable to translate'); '.100.3.6.1.2.1.11.4', 'OID returned when unable to translate');
} }
sub resolve_desthost : Tests(4) {
my $test = shift;
can_ok($test->{info}, 'resolve_desthost');
is(SNMP::Info::resolve_desthost('1.2.3.4'),
'1.2.3.4', 'IPv4 address returns unchanged');
is(SNMP::Info::resolve_desthost('::1.2.3.4'),
'udp6:0:0:0:0:0:0:102:304', q(IPv6 address returns with 'udp6:' prefix));
is(
SNMP::Info::resolve_desthost('fe80::2d0:b7ff:fe21:c6c0'),
'udp6:fe80:0:0:0:2d0:b7ff:fe21:c6c0',
q(Net-SNMP example IPv6 address returns with 'udp6:' prefix)
);
}
sub init : Tests(3) { sub init : Tests(3) {
my $test = shift; my $test = shift;
@@ -1301,8 +1319,8 @@ sub private_load_attr : Tests(18) {
{1 => 0, 2 => 1828306359704, 3 => 1002545943585, 4 => 'ENDOFMIBVIEW'}, {1 => 0, 2 => 1828306359704, 3 => 1002545943585, 4 => 'ENDOFMIBVIEW'},
# Tables to test partial and full OIDs # Tables to test partial and full OIDs
'.1.3.6.1.4.1.171.12.1.1.12' => {1 => 'partial', 2 => 'oid', 3 => 'data'}, '.1.3.6.1.4.1.171.12.1.1.12' => {1 => 'partial', 2 => 'oid', 3 => 'data'},
'.100.3.6.1.4.1.171.12.1.1.12' => {2 => 'full', 3 => 'oid', 4 => 'leaf'}, '.100.3.6.1.4.1.171.12.1.1.12' => {2 => 'full', 3 => 'oid', 4 => 'leaf'},
}; };
# Load cache with data to for initial tests # Load cache with data to for initial tests