Skip to content

Fix ipv6 and add backward compatible new networking #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 54 additions & 24 deletions lib/SNMP_Session.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ my $dont_wait_flags;
BEGIN {
$ipv6_addr_len = undef;
$SNMP_Session::ipv6available = 0;
$SNMP_Session::old_ipv6_networking = 0;
$dont_wait_flags = 0;

if (eval {local $SIG{__DIE__};require Socket6;} &&
if (eval {local $SIG{__DIE__};Socket->VERSION("2.000");}) {
Socket->import(qw(inet_pton inet_ntop getaddrinfo unpack_sockaddr_in6));
$ipv6_addr_len = length(pack_sockaddr_in6(161, inet_pton(AF_INET6(), "::1")));
$SNMP_Session::ipv6available = 1;
} elsif (eval {local $SIG{__DIE__};require Socket6;} &&
eval {local $SIG{__DIE__};require IO::Socket::INET6; IO::Socket::INET6->VERSION("1.26");}) {
Socket6->import(qw(inet_pton inet_ntop getaddrinfo));
$ipv6_addr_len = length(Socket6::pack_sockaddr_in6(161, inet_pton(AF_INET6(), "::1")));
Socket6->import(qw(inet_pton inet_ntop getaddrinfo unpack_sockaddr_in6));
$ipv6_addr_len = length(pack_sockaddr_in6(161, inet_pton(AF_INET6(), "::1")));
$SNMP_Session::ipv6available = 1;
$SNMP_Session::old_ipv6_networking = 1;
}
eval 'local $SIG{__DIE__};local $SIG{__WARN__};$dont_wait_flags = MSG_DONTWAIT();';
}
Expand Down Expand Up @@ -684,11 +690,8 @@ sub pretty_address {
my($addr) = shift;
my($port, $addrunpack, $addrstr);

# Disable strict subs to stop old versions of perl from
# complaining about AF_INET6 when Socket6 is not available

if( (defined $ipv6_addr_len) && (length $addr == $ipv6_addr_len)) {
($port,$addrunpack) = Socket6::unpack_sockaddr_in6 ($addr);
($port,$addrunpack) = unpack_sockaddr_in6 ($addr);
$addrstr = inet_ntop (AF_INET6(), $addrunpack);
} else {
($port,$addrunpack) = unpack_sockaddr_in ($addr);
Expand Down Expand Up @@ -774,7 +777,7 @@ sub receive_trap ($ ) {

my ($iaddr, $port, $af);
if ((defined $ipv6_addr_len) && (length $remote_addr == $ipv6_addr_len)) {
($port, $iaddr) = Socket6::unpack_sockaddr_in6 ($remote_addr);
($port, $iaddr) = unpack_sockaddr_in6 ($remote_addr);
$af = AF_INET6;
} else {
($port, $iaddr) = unpack_sockaddr_in ($remote_addr);
Expand Down Expand Up @@ -849,8 +852,16 @@ use Carp;

BEGIN {
if($SNMP_Session::ipv6available) {
import IO::Socket::INET6;
Socket6->import(qw(inet_pton inet_ntop getaddrinfo));
if (! $SNMP_Session::old_ipv6_networking
&& eval{local $SIG{__DIE__}; require IO::Socket::IP; IO::Socket::IP->VERSION('0.25');}) {

import IO::Socket::IP;
Socket->import(qw(inet_pton inet_ntop getaddrinfo));
} elsif (eval{local $SIG{__DIE__}; require IO::Socket::INET6;}) {
import IO::Socket::INET6;
Socket6->import(qw(inet_pton inet_ntop getaddrinfo));
$SNMP_Session::old_ipv6_networking = 1;
}
}
}

Expand Down Expand Up @@ -902,7 +913,7 @@ sub open {
$local_hostname,$ipv4only) = @_;
my($remote_addr,$socket,$sockfamily);

$ipv4only = 1 unless defined $ipv4only;
$ipv4only = 1 if defined $ipv4only;
$sockfamily = AF_INET;

$community = 'public' unless defined $community;
Expand Down Expand Up @@ -940,11 +951,22 @@ sub open {
$remote_hostname = $1;
}
my (@res, $socktype_tmp, $proto_tmp, $canonname_tmp);
@res = getaddrinfo($remote_hostname, $port, AF_UNSPEC, SOCK_DGRAM);
($sockfamily, $socktype_tmp, $proto_tmp, $remote_addr, $canonname_tmp) = @res;
if (scalar(@res) < 5) {
return $this->error_return ("can't resolve \"$remote_hostname\" to IPv6 address");
}
if ($SNMP_Session::old_ipv6_networking) {
@res = getaddrinfo($remote_hostname, $port, AF_UNSPEC, SOCK_DGRAM);
($sockfamily, $socktype_tmp, $proto_tmp, $remote_addr, $canonname_tmp) = @res;
if (scalar(@res) < 5) {
return $this->error_return ("can't resolve \"$remote_hostname\" to IPv6 address");
}
} else {
my ($err, $result) = getaddrinfo($remote_hostname, $port,
{'family' => AF_UNSPEC, 'socktype' => SOCK_DGRAM});
($sockfamily, $socktype_tmp, $proto_tmp, $remote_addr, $canonname_tmp)
= ($result->{'family'}, $result->{'socktype'}, $result->{'protocol'},
$result->{'addr'}, $result->{'canonname'});
if ($err) {
return $this->error_return ("can't resolve \"$remote_hostname\" to IPv6 address");
}
}
} else {
$sockfamily = AF_INET6;
}
Expand All @@ -958,12 +980,20 @@ sub open {
LocalPort => $local_port)
|| return $this->error_return ("creating socket: $!");
} else {
$socket = IO::Socket::INET6->new(Domain => AF_INET6,
Proto => 17,
Type => SOCK_DGRAM,
LocalAddr => $local_hostname,
LocalPort => $local_port)
|| return $this->error_return ("creating socket: $!");
my %net_setting = (
Domain => AF_INET6,
Proto => 17,
Type => SOCK_DGRAM,
LocalAddr => $local_hostname,
LocalPort => $local_port,
);
if ($SNMP_Session::old_ipv6_networking) {
$socket = IO::Socket::INET6->new(%net_setting)
|| return $this->error_return ("creating socket: $!");
} else {
$socket = IO::Socket::IP->new(%net_setting)
|| return $this->error_return ("creating socket: $!");
}
$the_socket{$sockfamily} = $socket
if $SNMP_Session::recycle_socket;
}
Expand Down Expand Up @@ -1125,8 +1155,8 @@ sub sa_equal_p ($$$) {
($p2,$a2) = unpack_sockaddr_in ($sa2);
} elsif($this->{'sockfamily'} == AF_INET6()) {
# IPv6 addresses
($p1,$a1) = Socket6::unpack_sockaddr_in6 ($sa1);
($p2,$a2) = Socket6::unpack_sockaddr_in6 ($sa2);
($p1,$a1) = unpack_sockaddr_in6 ($sa1);
($p2,$a2) = unpack_sockaddr_in6 ($sa2);
} else {
return 0;
}
Expand Down