Compare fully qualified IPv6 addresses

This commit is contained in:
Jamie Cameron
2016-03-14 23:03:23 -07:00
parent 1db718fd95
commit 581047a965
2 changed files with 30 additions and 2 deletions

View File

@@ -403,5 +403,32 @@ else {
}
}
# canonicalize_ip6(address)
# Converts an address to its full long form. Ie. 2001:db8:0:f101::20 to
# 2001:0db8:0000:f101:0000:0000:0000:0020
sub canonicalize_ip6
{
my ($addr) = @_;
return $addr if (!&check_ip6address($addr));
my @w = split(/:/, $addr);
my $idx = &indexof("", @w);
if ($idx >= 0) {
# Expand ::
my $mis = 8 - scalar(@w);
my @nw = @w[0..$idx];
for(my $i=0; $i<$mis; $i++) {
push(@nw, 0);
}
push(@nw, @w[$idx+1 .. $#w]);
@w = @nw;
}
foreach my $w (@w) {
while(length($w) < 4) {
$w = "0".$w;
}
}
return lc(join(":", @w));
}
1;

View File

@@ -61,13 +61,14 @@ if (@ips && &foreign_installed("net")) {
foreach $a (&net::active_interfaces()) {
$onsystem{$a->{'address'}} = $a;
foreach $ip6 (@{$a->{'address6'}}) {
$onsystem{$ip6} = $a;
$onsystem{&canonicalize_ip6($ip6)} = $a;
}
}
}
if (%onsystem) {
foreach $ip (@ips) {
$onsystem{$ip} || &error(&text('bind_eonsystem', $ip));
$onsystem{&canonicalize_ip6($ip)} ||
&error(&text('bind_eonsystem', $ip));
}
}
}