Add get_default_interface

This commit is contained in:
David Kerr 2020-08-04 18:18:50 -04:00
parent 213cf6ad09
commit 1b2f45cc59
4 changed files with 356 additions and 2 deletions

View file

@ -38,6 +38,7 @@ jobs:
libtest-tcp-perl \ libtest-tcp-perl \
libtest-warnings-perl \ libtest-warnings-perl \
liburi-perl \ liburi-perl \
net-tools \
make \ make \
; ;
- uses: actions/checkout@v2 - uses: actions/checkout@v2
@ -117,6 +118,7 @@ jobs:
perl-Test-MockModule \ perl-Test-MockModule \
perl-Test-TCP \ perl-Test-TCP \
perl-Test-Warnings \ perl-Test-Warnings \
net-tools \
; ;
- name: autogen - name: autogen
run: ./autogen run: ./autogen
@ -142,6 +144,7 @@ jobs:
perl-HTTP-Daemon \ perl-HTTP-Daemon \
perl-IO-Socket-INET6 \ perl-IO-Socket-INET6 \
perl-core \ perl-core \
iproute \
; ;
- name: autogen - name: autogen
run: ./autogen run: ./autogen

View file

@ -2705,12 +2705,69 @@ my $regex_ipv6_ula = qr{
$regex_ipv6 # And is a valid IPv6 address $regex_ipv6 # And is a valid IPv6 address
}xi; }xi;
######################################################################
## get_default_interface finds the default network interface based on
## the IP routing table on the system. We validate that the interface
## found is likely to have global routing (e.g. is not LOOPBACK).
## Returns undef if no global scope interface can be found for IP version.
######################################################################
sub get_default_interface {
my $ipver = int(shift // 4); ## Defaults to IPv4 if not specified
my $ipstr = ($ipver == 6) ? 'inet6' : 'inet';
my $reply = shift // ''; ## Pass in data for unit testing purposes only
my $cmd = "test";
return undef if (($ipver != 4) && ($ipver != 6));
if (!$reply) { ## skip if test data passed in.
## Best option is the ip command from iproute2 package
$cmd = "ip -$ipver -o route list match default"; $reply = qx{ $cmd 2>/dev/null };
## Fallback is the netstat command. This is only option on MacOS.
if ($?) { $cmd = "netstat -rn -$ipver"; $reply = qx{ $cmd 2>/dev/null }; } # Linux, FreeBSD
if ($?) { $cmd = "netstat -rn -f $ipstr"; $reply = qx{ $cmd 2>/dev/null }; } # MacOS
if ($?) { $cmd = "netstat -rn"; $reply = qx{ $cmd 2>/dev/null }; } # Busybox
if ($?) { $cmd = "missing ip or netstat command";
failed("Unable to obtain default route information -- %s", $cmd)
}
}
debug("Reply from '%s' :\n------\n%s------", $cmd, $reply);
# Check we have IPv6 address in case we got routing table from non-specific cmd above
return undef if (($ipver == 6) && !extract_ipv6($reply));
# Filter down to just the default interfaces
my @list = split(/\n/, $reply);
@list = grep(/^default|^(?:0\.){3}0|^::\/0/, @list); # Select 'default' or '0.0.0.0' or '::/0'
return undef if (scalar(@list) == 0);
debug("Default routes found for IPv%s :\n%s", $ipver, join("\n",@list));
# now check each interface to make sure it is global (not loopback).
foreach my $line (@list) {
## Interface will be after "dev" or the last word in the line. Must accept blank spaces
## at the end. Interface name may not have any whitespace or forward slash.
$line =~ /\bdev\b\s*\K[^\s\/]+|\b[^\s\/]+(?=[\s\/]*$)/;
my $interface = $&;
## If test data was passed in skip following tests
if ($cmd ne "test") {
## We do not want the loopback interface or anything interface without global scope
$cmd = "ip -$ipver -o addr show dev $interface scope global"; $reply = qx{$cmd 2>/dev/null};
if ($?) { $cmd = "ifconfig $interface"; $reply = qx{$cmd 2>/dev/null}; }
if ($?) { $cmd = "missing ip or ifconfig command";
failed("Unable to obtain information for '%s' -- %s", $interface, $cmd);
}
debug("Reply from '%s' :\n------\n%s------", $cmd, $reply);
}
## Has global scope, is not LOOPBACK
return($interface) if (($reply) && ($reply !~ /\bLOOPBACK\b/));
}
return undef;
}
###################################################################### ######################################################################
## get_ip_from_interface() finds an IPv4 or IPv6 address from a network ## get_ip_from_interface() finds an IPv4 or IPv6 address from a network
## interface. Defaults to IPv4 unless '6' passed as 2nd parameter. ## interface. Defaults to IPv4 unless '6' passed as 2nd parameter.
###################################################################### ######################################################################
sub get_ip_from_interface { sub get_ip_from_interface {
my $interface = shift; my $interface = shift // "default";
my $ipver = int(shift // 4); ## Defaults to IPv4 if not specified my $ipver = int(shift // 4); ## Defaults to IPv4 if not specified
my $scope = lc(shift // "gua"); ## "gua" or "ula" my $scope = lc(shift // "gua"); ## "gua" or "ula"
my $reply = shift // ''; ## Pass in data for unit testing purposes only my $reply = shift // ''; ## Pass in data for unit testing purposes only
@ -2723,7 +2780,8 @@ sub get_ip_from_interface {
return undef; return undef;
} }
if ($reply eq '') { ## skip if test data passed in. if ((lc($interface) eq "default") && (!$reply)) { ## skip if test data passed in.
$interface = get_default_interface($ipver);
return undef if !defined($interface); return undef if !defined($interface);
} }

View file

@ -8,6 +8,19 @@ eval { require 'ddclient'; } or BAIL_OUT($@);
#STDOUT->autoflush(1); #STDOUT->autoflush(1);
#$ddclient::globals{'debug'} = 1; #$ddclient::globals{'debug'} = 1;
subtest "get_default_interface tests" => sub {
for my $sample (@ddclient::t::routing_samples) {
if (defined($sample->{want_ipv4_if})) {
my $interface = ddclient::get_default_interface(4, $sample->{text});
is($interface, $sample->{want_ipv4_if}, $sample->{name});
}
if (defined($sample->{want_ipv6_if})) {
my $interface = ddclient::get_default_interface(6, $sample->{text});
is($interface, $sample->{want_ipv6_if}, $sample->{name});
}
}
};
subtest "get_ip_from_interface tests" => sub { subtest "get_ip_from_interface tests" => sub {
for my $sample (@ddclient::t::interface_samples) { for my $sample (@ddclient::t::interface_samples) {
# interface name is undef as we are passing in test data # interface name is undef as we are passing in test data
@ -26,4 +39,25 @@ subtest "get_ip_from_interface tests" => sub {
} }
}; };
subtest "Get default interface and IP for test system" => sub {
my $interface = ddclient::get_default_interface(4);
if ($interface) {
isnt($interface, "lo", "Check for loopback 'lo'");
isnt($interface, "lo0", "Check for loopback 'lo0'");
my $ip1 = ddclient::get_ip_from_interface("default", 4);
my $ip2 = ddclient::get_ip_from_interface($interface, 4);
is($ip1, $ip2, "Check IPv4 from default interface");
ok(ddclient::is_ipv4($ip1), "Valid IPv4 from get_ip_from_interface($interface)");
}
$interface = ddclient::get_default_interface(6);
if ($interface) {
isnt($interface, "lo", "Check for loopback 'lo'");
isnt($interface, "lo0", "Check for loopback 'lo0'");
my $ip1 = ddclient::get_ip_from_interface("default", 6);
my $ip2 = ddclient::get_ip_from_interface($interface, 6);
is($ip1, $ip2, "Check IPv6 from default interface");
ok(ddclient::is_ipv6($ip1), "Valid IPv6 from get_ip_from_interface($interface)");
}
};
done_testing(); done_testing();

View file

@ -301,3 +301,262 @@ EOF
want_ipv4_from_if => "198.51.157.237", want_ipv4_from_if => "198.51.157.237",
}, },
); );
######################################################################
## Outputs from ip route and netstat commands to find default route (and therefore interface)
## Samples from Ubuntu 20.04, RHEL8, Buildroot, Busybox, MacOS 10.15, FreeBSD
## NOTE: Any tabs/whitespace at start or end of lines are intentional to match real life data.
######################################################################
our @routing_samples = (
{ name => "ip -4 -o route list match default (most linux)",
text => <<EOF,
default via 198.51.100.1 dev ens33 proto dhcp metric 100
EOF
want_ipv4_if => "ens33",
},
{ name => "ip -4 -o route list match default (most linux)",
text => <<EOF,
default via fe80::4262:31ff:fe08:60b3 dev ens33 proto ra metric 20100 pref medium
EOF
want_ipv4_if => "ens33",
},
{ name => "ip -4 -o route list match default (buildroot)",
text => <<EOF,
default via 198.51.156.1 dev eth0
EOF
want_ipv4_if => "eth0",
},
{ name => "ip -6 -o route list match default (buildroot)",
text => <<EOF,
default via fe80::1ee8:5dff:fef4:b822 dev eth0 proto ra metric 1024 expires 1797sec mtu 1500 hoplimit 64
EOF
want_ipv6_if => "eth0",
},
{ name => "netstat -rn -4 (most linux)",
text => <<EOF,
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 198.51.100.1 0.0.0.0 UG 0 0 0 ens33
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ens33
198.51.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
EOF
want_ipv4_if => "ens33",
},
{ name => "netstat -rn -4 (FreeBSD)",
text => <<EOF,
Routing tables
Internet:
Destination Gateway Flags Netif Expire
default 198.51.100.1 UGS em0
127.0.0.1 link#2 UH lo0
198.51.100.0/24 link#1 U em0
198.51.100.207 link#1 UHS lo0
EOF
want_ipv4_if => "em0",
},
{ name => "netstat -rn -6 (FreeBSD)",
text => <<EOF,
Routing tables
Internet6:
Destination Gateway Flags Netif Expire
::/96 ::1 UGRS lo0
default fe80::4262:31ff:fe08:60b3%em0 UG em0
::1 link#2 UH lo0
::ffff:0.0.0.0/96 ::1 UGRS lo0
2001:db8:450a:e723::/64 link#1 U em0
2001:db8:450a:e723:20c:29ff:fe9f:c532 link#1 UHS lo0
fdb6:1d86:d9bd:3::/64 link#1 U em0
fdb6:1d86:d9bd:3:20c:29ff:fe9f:c532 link#1 UHS lo0
fe80::/10 ::1 UGRS lo0
fe80::%em0/64 link#1 U em0
fe80::20c:29ff:fe9f:c532%em0 link#1 UHS lo0
fe80::%lo0/64 link#2 U lo0
fe80::1%lo0 link#2 UHS lo0
ff02::/16 ::1 UGRS lo0
EOF
want_ipv6_if => "em0",
},
{ name => "netstat -rn -6 (most linux)",
text => <<EOF,
Kernel IPv6 routing table
Destination Next Hop Flag Met Ref Use If
::1/128 :: U 256 2 0 lo
2001:db8:450a:e723::21/128 :: U 100 1 0 ens33
2001:db8:450a:e723::/64 :: U 100 4 0 ens33
fdb6:1d86:d9bd:3::21/128 :: U 100 1 0 ens33
fdb6:1d86:d9bd:3::/64 :: U 100 3 0 ens33
fe80::/64 :: U 100 2 0 ens33
::/0 fe80::4262:31ff:fe08:60b3 UG 20100 5 0 ens33
::1/128 :: Un 0 4 0 lo
2001:db8:450a:e723::21/128 :: Un 0 4 0 ens33
2001:db8:450a:e723:514:cbd9:c55f:8e2a/128 :: Un 0 4 0 ens33
2001:db8:450a:e723:adee:be82:7fba:ffb2/128 :: Un 0 3 0 ens33
2001:db8:450a:e723:dbc5:1c4e:9e9b:97a2/128 :: Un 0 3 0 ens33
fdb6:1d86:d9bd:3::21/128 :: Un 0 2 0 ens33
fdb6:1d86:d9bd:3:514:cbd9:c55f:8e2a/128 :: Un 0 5 0 ens33
fdb6:1d86:d9bd:3:a1fd:1ed9:6211:4268/128 :: Un 0 4 0 ens33
fdb6:1d86:d9bd:3:adee:be82:7fba:ffb2/128 :: Un 0 2 0 ens33
fe80::32c0:b270:245b:d3b4/128 :: Un 0 3 0 ens33
ff00::/8 :: U 256 7 0 ens33
::/0 :: !n -1 1 0 lo
EOF
want_ipv6_if => "ens33",
},
{ name => "netstat -rn -f inet (MacOS)",
text => <<EOF,
Routing tables
Internet:
Destination Gateway Flags Netif Expire
default 198.51.100.1 UGSc en0
default 198.51.100.1 UGScI en1
127 127.0.0.1 UCS lo0
127.0.0.1 127.0.0.1 UH lo0
169.254 link#4 UCS en0 !
169.254 link#5 UCSI en1 !
172.16.114/24 link#15 UC vmnet8 !
172.16.114.1 0:50:56:c0:0:8 UHLWIi lo0
172.16.114.255 ff:ff:ff:ff:ff:ff UHLWbI vmnet8 !
198.51.17 link#4 UCS en0 !
198.51.17 link#5 UCSI en1 !
198.51.100.1/32 link#4 UCS en0 !
198.51.100.1 40:62:31:8:60:b3 UHLWIir en0 1180
198.51.100.1 40:62:31:8:60:b3 UHLWIir en1 1160
198.51.100.1/32 link#5 UCSI en1 !
198.51.100.2 0:c:29:47:b8:d1 UHLWI en0 1108
198.51.100.5/32 link#4 UCS en0 !
198.51.100.5 00:00:00:90:32:8f UHLWIi lo0
198.51.100.5 00:00:00:90:32:8f UHLWI en1 1182
198.51.100.6 0:8:9b:ee:d4:e UHLWIi en0 158
198.51.100.12 0:c:29:70:89:8b UHLWI en0 1107
198.51.100.33 0:c:29:da:24:b1 UHLWI en0 1108
198.51.100.34 0:c:29:6d:aa:8b UHLWI en0 1107
198.51.100.137 70:ea:5a:79:45:4b UHLWI en0 317
198.51.100.137 70:ea:5a:79:45:4b UHLWI en1 561
198.51.100.152 8c:79:67:a7:c4:45 UHLWI en0 376
198.51.100.155 f0:18:98:29:ef:a3 UHLWIi en0 694
198.51.100.167 a0:2:dc:f7:7a:9a UHLWI en0 1160
198.51.100.167 a0:2:dc:f7:7a:9a UHLWI en1 1161
198.51.100.184 8:66:98:92:0:55 UHLWIi en0 644
198.51.100.187 link#4 UHLWIi en0 !
198.51.100.187 link#5 UHLWIi en1 !
198.51.100.199/32 link#5 UCS en1 !
198.51.100.199 c8:e0:eb:42:96:eb UHLWIi lo0
198.51.100.201 90:e1:7b:b9:e5:38 UHLWI en0 1182
198.51.100.201 90:e1:7b:b9:e5:38 UHLWI en1 1182
198.51.100.210 0:61:71:cd:0:10 UHLWI en0 112
198.51.100.210 0:61:71:cd:0:10 UHLWI en1 112
198.51.100.211 8c:85:90:55:49:a7 UHLWIi en0 762
198.51.100.211 8c:85:90:55:49:a7 UHLWI en1 762
198.51.100.240 f0:18:98:20:f9:d7 UHLWIi en0 1172
198.51.100.240 f0:18:98:20:f9:d7 UHLWIi en1 1173
198.51.100.241 e0:33:8e:38:44:3 UHLWIi en0 961
198.51.100.241 e0:33:8e:38:44:3 UHLWI en1 961
198.51.100.242 98:1:a7:49:1e:1c UHLWIi en0 899
198.51.100.242 98:1:a7:49:1e:1c UHLWIi en1 899
198.51.100.255 ff:ff:ff:ff:ff:ff UHLWbI en0 !
198.51.196 link#14 UC vmnet1 !
198.51.196.1 0:50:56:c0:0:1 UHLWIi lo0
198.51.196.255 ff:ff:ff:ff:ff:ff UHLWbI vmnet1 !
224.0.0/4 link#4 UmCS en0 !
224.0.0/4 link#5 UmCSI en1 !
224.0.0.251 1:0:5e:0:0:fb UHmLWI en0
224.0.0.251 1:0:5e:0:0:fb UHmLWI en1
239.255.255.250 1:0:5e:7f:ff:fa UHmLWI en0
239.255.255.250 1:0:5e:7f:ff:fa UHmLWI en1
255.255.255.255/32 link#4 UCS en0 !
255.255.255.255 ff:ff:ff:ff:ff:ff UHLWbI en0 !
255.255.255.255/32 link#5 UCSI en1 !
EOF
want_ipv4_if => "en0",
},
{ name => "netstat -rn -f inet6 (MacOS)",
text => <<EOF,
Routing tables
Internet6:
Destination Gateway Flags Netif Expire
default fe80::4262:31ff:fe08:60b3%en0 UGc en0
default fe80::4262:31ff:fe08:60b3%en1 UGcI en1
default fe80::%utun0 UGcI utun0
default fe80::%utun1 UGcI utun1
::1 ::1 UHL lo0
2001:db8:450a:e723::/64 link#4 UC en0
2001:db8:450a:e723::/64 link#5 UCI en1
2001:db8:450a:e723::1 40:62:31:8:60:b3 UHLWIi en0
2001:db8:450a:e723:208:9bff:feee:d40e 0:8:9b:ee:d4:e UHLWI en0
2001:db8:450a:e723:208:9bff:feee:d40f 0:8:9b:ee:d4:f UHLWI en0
2001:db8:450a:e723:881:db49:835c:e83e c8:e0:eb:42:96:eb UHL lo0
2001:db8:450a:e723:1820:2961:5878:fb72 c8:e0:eb:42:96:eb UHL lo0
2001:db8:450a:e723:1c99:99e2:21d0:79e6 00:00:00:90:32:8f UHL lo0
2001:db8:450a:e723:2474:39fd:f5c0:6845 00:00:00:90:32:8f UHL lo0
2001:db8:450a:e723:808d:d894:e4db:157e 00:00:00:90:32:8f UHL lo0
2001:db8:450a:e723:9022:cdf6:728c:81cc c8:e0:eb:42:96:eb UHL lo0
fdb6:1d86:d9bd:3::/64 link#4 UC en0
fdb6:1d86:d9bd:3::/64 link#5 UCI en1
fdb6:1d86:d9bd:3::1 40:62:31:8:60:b3 UHLWI en0
fdb6:1d86:d9bd:3::8076 00:00:00:90:32:8f UHL lo0
fdb6:1d86:d9bd:3::85ba c8:e0:eb:42:96:eb UHL lo0
fdb6:1d86:d9bd:3:208:9bff:feee:d40e 0:8:9b:ee:d4:e UHLWI en0
fdb6:1d86:d9bd:3:208:9bff:feee:d40f 0:8:9b:ee:d4:f UHLWI en0
fdb6:1d86:d9bd:3:837:e1c7:4895:269e 00:00:00:90:32:8f UHL lo0
fdb6:1d86:d9bd:3:8a5:4e16:4924:ca7d c8:e0:eb:42:96:eb UHL lo0
fdb6:1d86:d9bd:3:dbb:dd72:928a:1f4 c8:e0:eb:42:96:eb UHL lo0
fdb6:1d86:d9bd:3:2474:39fd:f5c0:6845 00:00:00:90:32:8f UHL lo0
fdb6:1d86:d9bd:3:9022:cdf6:728c:81cc c8:e0:eb:42:96:eb UHL lo0
fdb6:1d86:d9bd:3:a0b3:aa4d:9e76:e1ab 00:00:00:90:32:8f UHL lo0
fe80::%lo0/64 fe80::1%lo0 UcI lo0
fe80::1%lo0 link#1 UHLI lo0
fe80::%en0/64 link#4 UCI en0
fe80::2e:996d:54e6:daa0%en0 70:ea:5a:79:45:4b UHLWI en0
fe80::208:9bff:feee:d40f%en0 0:8:9b:ee:d4:f UHLWI en0
fe80::4ba:362c:664:c432%en0 7c:a1:ae:f:4:f4 UHLWI en0
fe80::85b:d150:cdd9:3198%en0 00:00:00:90:32:8f UHLI lo0
fe80::8f2:20e6:a10b:3cdd%en0 70:56:81:ba:5f:37 UHLWI en0
fe80::c20:19a:2ac2:79a1%en0 cc:d2:81:5a:8d:ee UHLWI en0
fe80::10e4:937a:51ce:a8d9%en0 f0:18:98:29:ef:a3 UHLWI en0
fe80::142a:3ac5:7cb9:2218%en0 90:e1:7b:b9:e5:38 UHLWI en0
fe80::1445:78b9:1d5c:11eb%en0 c8:e0:eb:42:96:eb UHLWI en0
fe80::1450:3f80:6143:4f7c%en0 b8:e8:56:a3:67:5 UHLWI en0
fe80::18d5:2b64:b66b:88b%en0 e0:33:8e:38:44:3 UHLWI en0
fe80::1c88:3c7:f97b:e538%en0 98:1:a7:49:1e:1c UHLWIi en0
fe80::4262:31ff:fe08:60b3%en0 40:62:31:8:60:b3 UHLWIir en0
fe80::%en1/64 link#5 UCI en1
fe80::2e:996d:54e6:daa0%en1 70:ea:5a:79:45:4b UHLWI en1
fe80::70:2494:f602:7479%en1 0:61:71:cd:0:10 UHLWI en1
fe80::4ba:362c:664:c432%en1 7c:a1:ae:f:4:f4 UHLWI en1
fe80::85b:d150:cdd9:3198%en1 00:00:00:90:32:8f UHLWI en1
fe80::8f2:20e6:a10b:3cdd%en1 70:56:81:ba:5f:37 UHLWI en1
fe80::c20:19a:2ac2:79a1%en1 cc:d2:81:5a:8d:ee UHLWI en1
fe80::1445:78b9:1d5c:11eb%en1 c8:e0:eb:42:96:eb UHLI lo0
fe80::18d5:2b64:b66b:88b%en1 e0:33:8e:38:44:3 UHLWI en1
fe80::1c88:3c7:f97b:e538%en1 98:1:a7:49:1e:1c UHLWIi en1
fe80::4262:31ff:fe08:60b3%en1 40:62:31:8:60:b3 UHLWIir en1
fe80::%awdl0/64 link#10 UCI awdl0
fe80::54df:1aff:fee1:2df5%awdl0 56:df:1a:e1:2d:f5 UHLI lo0
fe80::%llw0/64 link#11 UCI llw0
fe80::54df:1aff:fee1:2df5%llw0 56:df:1a:e1:2d:f5 UHLI lo0
fe80::%utun0/64 fe80::aeea:9fe9:9194:6e66%utun0 UcI utun0
fe80::aeea:9fe9:9194:6e66%utun0 link#12 UHLI lo0
fe80::%utun1/64 fe80::583f:da5f:e2bc:4773%utun1 UcI utun1
fe80::583f:da5f:e2bc:4773%utun1 link#13 UHLI lo0
ff01::%lo0/32 ::1 UmCI lo0
ff01::%en0/32 link#4 UmCI en0
ff01::%en1/32 link#5 UmCI en1
ff01::%awdl0/32 link#10 UmCI awdl0
ff01::%llw0/32 link#11 UmCI llw0
ff01::%utun0/32 fe80::aeea:9fe9:9194:6e66%utun0 UmCI utun0
ff01::%utun1/32 fe80::583f:da5f:e2bc:4773%utun1 UmCI utun1
ff02::%lo0/32 ::1 UmCI lo0
ff02::%en0/32 link#4 UmCI en0
ff02::%en1/32 link#5 UmCI en1
ff02::%awdl0/32 link#10 UmCI awdl0
ff02::%llw0/32 link#11 UmCI llw0
ff02::%utun0/32 fe80::aeea:9fe9:9194:6e66%utun0 UmCI utun0
ff02::%utun1/32 fe80::583f:da5f:e2bc:4773%utun1 UmCI utun1
EOF
want_ipv6_if => "en0",
},
);