diff options
author | Christian Breunig <christian@breunig.cc> | 2023-02-25 22:11:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-25 22:11:52 +0100 |
commit | 2eb30a51aae84c3b3fe316b12f0b44b9a8527131 (patch) | |
tree | a2d06b6c54873511aff3394426d84ab623720d97 | |
parent | 3735e38c6e919d6fe059e58d7682562c51dd7afb (diff) | |
parent | 3d6d27368891128a40de6047d38771cea2c90760 (diff) | |
download | vyatta-cfg-1.3.4.tar.gz vyatta-cfg-1.3.4.zip |
Merge pull request #61 from zdc/T4992-equuleus1.3.41.3.3-epa11.3.3
misc: T4992: Replaced logic in `is_local_address`
-rwxr-xr-x | lib/Vyatta/Misc.pm | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/lib/Vyatta/Misc.pm b/lib/Vyatta/Misc.pm index 7d47b3c..baae576 100755 --- a/lib/Vyatta/Misc.pm +++ b/lib/Vyatta/Misc.pm @@ -39,6 +39,7 @@ our @EXPORT_OK = qw(generate_dhclient_intf_files use Vyatta::Config; use Vyatta::Interface; use NetAddr::IP; +use NetAddr::IP::Lite; use Socket; Socket6->import(qw(inet_pton getaddrinfo)); @@ -211,27 +212,32 @@ sub getInterfaces { } # Test if IP address is local to the system. -# Implemented by doing bind since by default -# Linux will only allow binding to local addresses +# Implemented by checking a local route table sub is_local_address { my $addr = shift; - my $ip = new NetAddr::IP $addr; + my $ip = new NetAddr::IP::Lite($addr); die "$addr: not a valid IP address" unless $ip; - my ($pf, $sockaddr); + # prepare an ip command + my $iproute_cmd; if ($ip->version() == 4) { - $pf = PF_INET; - $sockaddr = sockaddr_in(0, $ip->aton()); - } else { - $pf = PF_INET6; - $sockaddr = sockaddr_in6(0, $ip->aton()); + $iproute_cmd = "ip -N -4 route show table local match $ip 2> /dev/null"; + } + if ($ip->version() == 6) { + $iproute_cmd = "ip -N -6 route show table local match $ip 2> /dev/null"; } - socket( my $sock, $pf, SOCK_STREAM, 0) - or die "socket failed\n"; + # execute the command and check output + my $iproute_output = qx/$iproute_cmd/; - return bind($sock, $sockaddr); + if ($iproute_output) { + # return true if a route exists + return 1; + } else { + # return false if address is not in a local routing table + return undef; + } } # Test if the given port is currently in use by attempting |