If we were given an IPv6 transport-specifier strip it before trying to resolve it

Since DestHost defaults to 'localhost' die instead of returning undef from resolve_desthost()
This commit is contained in:
Eric A. Miller
2018-05-06 20:26:51 -04:00
parent 721193ccb8
commit 4ff904f3cd
2 changed files with 15 additions and 5 deletions

View File

@@ -3756,14 +3756,17 @@ sub munge_e_type {
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.
If unable to determine the type of address or resolve a DNS name, dies with
C<croak>.
=cut
sub resolve_desthost {
my $desthost = shift;
# If we have an IPv6 transport-specifier strip it
$desthost =~ s/^(?:udp6:|udpv6:|udpipv6:)//x;
my $ip = NetAddr::IP::Lite->new($desthost);
if ($ip and $ip->bits == 32) {
@@ -3773,7 +3776,7 @@ sub resolve_desthost {
return 'udp6:' . $ip->addr;
}
else {
return;
croak "Unable to resolve DestHost: $desthost to an IP\n";
}
}
@@ -4548,7 +4551,6 @@ sub snmp_connect_ip {
return if $self->{Offline};
$ip = resolve_desthost($ip);
return unless $ip;
return if ( $ip eq '0.0.0.0' ) or ( $ip =~ /^127\./ );
# Create session object

View File

@@ -781,7 +781,7 @@ sub munge_e_type : Tests(3) {
'.100.3.6.1.2.1.11.4', 'OID returned when unable to translate');
}
sub resolve_desthost : Tests(4) {
sub resolve_desthost : Tests(6) {
my $test = shift;
can_ok($test->{info}, 'resolve_desthost');
@@ -792,11 +792,19 @@ sub resolve_desthost : Tests(4) {
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('udp6:fe80::2d0:b7ff:fe21:c6c0'),
'udp6:fe80:0:0:0:2d0:b7ff:fe21:c6c0',
q(Net-SNMP example with 'udp6:' prefix returns expected string)
);
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)
);
dies_ok { SNMP::Info::resolve_desthost('1.2.3.4.5') } 'Bad IP dies';
}
sub init : Tests(3) {