From 76501f836662d9666ed4357de6bfcad61eb019cb Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Mon, 20 Dec 2010 17:59:36 -0800 Subject: Handle IPv6 in is_local_address Bug 6590 The code to check for local address has to be smarter to handle IPv6. Introduces dependency on Socket6 module. --- debian/control | 1 + lib/Vyatta/Misc.pm | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/debian/control b/debian/control index 1f62716..f603233 100644 --- a/debian/control +++ b/debian/control @@ -23,6 +23,7 @@ Depends: sed (>= 4.1.5), ethtool, iproute, curl, + libsocket6-perl, libvyatta-cfg1 (=${binary:Version}), ${perl:Depends}, ${shlibs:Depends} Replaces: vyatta-cfg-firewall, diff --git a/lib/Vyatta/Misc.pm b/lib/Vyatta/Misc.pm index 40c4602..c6a51c8 100755 --- a/lib/Vyatta/Misc.pm +++ b/lib/Vyatta/Misc.pm @@ -36,6 +36,8 @@ use Vyatta::Config; use Vyatta::Interface; use NetAddr::IP; use Socket; +use Socket6; + # # returns a hash of ipaddrs => interface # @@ -151,11 +153,23 @@ sub getInterfaces { # Linux will only allow binding to local addresses sub is_local_address { my $addr = shift; + my $ip = new NetAddr::IP $addr; + die "$addr: not a valid IP address" + unless $ip; + + my ($pf, $sockaddr); + if ($ip->version() == 4) { + $pf = PF_INET; + $sockaddr = sockaddr_in(0, $ip->aton()); + } else { + $pf = PF_INET6; + $sockaddr = sockaddr_in6(0, $ip->aton()); + } - socket( my $sock, PF_INET, SOCK_STREAM, 0) + socket( my $sock, $pf, SOCK_STREAM, 0) or die "socket failed\n"; - return bind($sock, sockaddr_in(0, inet_aton($addr))); + return bind($sock, $sockaddr); } # get list of IPv4 and IPv6 addresses -- cgit v1.2.3