Merge pull request #196 from dkerr64/Cleanup-extract_ipv4-and-is_ipv6-and-usage

Cleanup extract ipv4 and is ipv6 and usage
This commit is contained in:
Richard Hansen 2020-06-30 15:54:25 -04:00 committed by GitHub
commit 034e6501ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -984,12 +984,6 @@ sub update_nics {
if !$daemon || opt('verbose'); if !$daemon || opt('verbose');
next; next;
} }
if (!is_ipv4($ip)) {
if (!extract_ipv6($ip)) {
warning("malformed IP address (%s)", $ip);
next;
}
}
$iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd} = $ip; $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd} = $ip;
} }
$config{$h}{'wantip'} = $ip; $config{$h}{'wantip'} = $ip;
@ -1936,9 +1930,7 @@ sub check_value {
return undef if $value eq ""; return undef if $value eq "";
} elsif ($type eq T_IP) { } elsif ($type eq T_IP) {
if (!extract_ipv6($value)) { return undef if !is_ipv4($value) && !is_ipv6($value);
return undef if !is_ipv4($value);
}
} }
return $value; return $value;
} }
@ -2189,26 +2181,7 @@ sub geturl {
$reply =~ s/\r//g if defined $reply; $reply =~ s/\r//g if defined $reply;
return $reply; return $reply;
} }
######################################################################
## un_zero_pad
######################################################################
sub un_zero_pad {
my $in_str = shift(@_);
my @out_str = ();
if ($in_str eq '0.0.0.0') {
return $in_str;
}
foreach my $block (split /\./, $in_str) {
$block =~ s/^0+//;
if ($block eq '') {
$block = '0';
}
push @out_str, $block;
}
return join('.', @out_str);
}
###################################################################### ######################################################################
## get_ip ## get_ip
###################################################################### ######################################################################
@ -2220,6 +2193,10 @@ sub get_ip {
if ($use eq 'ip') { if ($use eq 'ip') {
$ip = opt('ip', $h); $ip = opt('ip', $h);
if (!is_ipv4($ip) && !is_ipv6($ip)) {
warning("'%s' is not a valid IPv4 or IPv6 address", $ip);
$ip = undef;
}
$arg = 'ip'; $arg = 'ip';
} elsif ($use eq 'if') { } elsif ($use eq 'if') {
@ -2316,15 +2293,8 @@ sub get_ip {
$skip =~ s/ /\\s/is; $skip =~ s/ /\\s/is;
$reply =~ s/^.*?${skip}//is; $reply =~ s/^.*?${skip}//is;
} }
if (defined($ip)) { $ip //= extract_ipv4($reply) // extract_ipv6($reply);
# no need to parse $reply warning("found neither IPv4 nor IPv6 address") if !defined($ip);
} elsif ($ip = extract_ipv4($reply)) {
$ip = un_zero_pad($ip);
} elsif ($ip = extract_ipv6($reply)) {
$ip = un_zero_pad($ip);
} else {
warning("found neither ipv4 nor ipv6 address");
}
if ($use ne 'ip' && ($ip // '') eq '0.0.0.0') { if ($use ne 'ip' && ($ip // '') eq '0.0.0.0') {
$ip = undef; $ip = undef;
} }
@ -2334,8 +2304,9 @@ sub get_ip {
} }
###################################################################### ######################################################################
## is_ipv4() validates if string is valid IPv4 address and only ## is_ipv4() validates if string is valid IPv4 address and only a
## a valid IPv4 address (no preceding or trailing spaces/characters) ## valid address with no preceding or trailing spaces/characters
## and no embedded leading zeros.
###################################################################### ######################################################################
sub is_ipv4 { sub is_ipv4 {
my ($value) = @_; my ($value) = @_;
@ -2343,24 +2314,29 @@ sub is_ipv4 {
} }
###################################################################### ######################################################################
## extract_ipv4() extracts the first valid IPv4 address from the given string ## extract_ipv4() extracts the first valid IPv4 address from given string.
## Accepts leading zeros in the address but removes them in returned value
###################################################################### ######################################################################
sub extract_ipv4 { sub extract_ipv4 {
(shift // '') =~ /\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b/ai; (shift // '') =~ /\b((?:(?<octet>25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?&octet))\b/a
return $1; or return undef;
(my $ip = $1) =~ s/\b0+\B//g; ## remove embedded leading zeros
return $ip;
} }
###################################################################### ######################################################################
## is_ipv6() validates if string is valid IPv6 address ## is_ipv6() validates if string is valid IPv6 address with no preceding
## or trailing spaces/characters.
###################################################################### ######################################################################
sub is_ipv6 { sub is_ipv6 {
my ($value) = @_; my ($value) = @_;
# This little gem from http://home.deds.nl/~aeron/regex/ return (length($value // '') != 0) &&
return $value =~ /^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/ai; ((extract_ipv6($value) // '') eq (($value =~ s/\b0+\B//g) ? $value : $value));
} }
###################################################################### ######################################################################
## extract_ipv6() extracts the first valid IPv6 address from the given string ## extract_ipv6() extracts the first valid IPv6 address from the given string.
## Accepts leading zeros in the address but removes them in returned value.
###################################################################### ######################################################################
sub extract_ipv6 { sub extract_ipv6 {
my $content = shift; my $content = shift;
@ -2382,6 +2358,7 @@ sub extract_ipv6 {
next next
} }
} }
$parsed =~ s/\b0+\B//g; ## remove embedded leading zeros
return $parsed; return $parsed;
} }
return; return;