Updates to address code review comment.

This commit is contained in:
David Kerr 2020-06-28 14:17:08 -04:00
parent 050b98eca1
commit 70d7ff3f4e

View file

@ -942,18 +942,12 @@ sub update_nics {
if (exists $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd}) { if (exists $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 = $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd};
} else { } else {
my $reply = get_ip($use, $h) // ''; my $ip = get_ip($use, $h) // '';
if (!$reply) { if (!$ip) {
warning("unable to determine IP address") warning("unable to determine IP address")
if !$daemon || opt('verbose'); if !$daemon || opt('verbose');
next; next;
} }
if (!($ip = ipv4_match($reply))) {
if (!($ip = ipv6_match($reply))) {
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;
@ -1900,11 +1894,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 (!ipv6_match($value)) { return undef if !is_ipv4($value) && !is_ipv6($value);
return undef if !($value = ipv4_match($value));
} else {
$value = ipv6_match($value);
}
} }
return $value; return $value;
} }
@ -2166,7 +2156,7 @@ sub get_ip {
$arg = '' unless $arg; $arg = '' unless $arg;
if ($use eq 'ip') { if ($use eq 'ip') {
$ip = opt('ip', $h); $reply = opt('ip', $h);
$arg = 'ip'; $arg = 'ip';
} elsif ($use eq 'if') { } elsif ($use eq 'if') {
@ -2263,15 +2253,8 @@ sub get_ip {
$skip =~ s/ /\\s/is; $skip =~ s/ /\\s/is;
$reply =~ s/^.*?${skip}//is; $reply =~ s/^.*?${skip}//is;
} }
if (defined($ip)) { $ip //= ipv4_extract($reply) // ipv6_extract($reply);
# no need to parse $reply warning("found neither IPv4 nor IPv6 address") if !defined($ip);
} elsif ($ip = ipv4_match($reply)) {
# got an IPv4 address
} elsif ($ip = ipv6_match($reply)) {
# got a IPv6 address
} 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;
} }
@ -2287,17 +2270,19 @@ sub get_ip {
###################################################################### ######################################################################
sub is_ipv4 { sub is_ipv4 {
my ($value) = @_; my ($value) = @_;
return (length($value // '') != 0) && ($value eq (ipv4_match($value) // '')); return (length($value // '') != 0) && ($value eq (ipv4_extract($value) // ''));
} }
###################################################################### ######################################################################
## ipv4_match() extracts the first valid IPv4 address from given string. ## ipv4_extract() extracts the first valid IPv4 address from given string.
## Accepts leading zeros in the address but removes them in returned value ## Accepts leading zeros in the address but removes them in returned value
###################################################################### ######################################################################
sub ipv4_match { sub ipv4_extract{
(shift // '') =~ /\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} ## first three bytes (shift // '') =~ /\b((?:(?<octet>25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3} ## first three bytes
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b/xai; ## last one (?&octet))\b/xa; ## last one (without period)
return ($1 // '') =~ s/\b0+\B//gr; ## remove leading zeros my $ip = ($1 // '');
$ip =~ s/\b0+\B//g; ## remove leading zeros
return $ip;
} }
###################################################################### ######################################################################
@ -2306,18 +2291,18 @@ sub ipv4_match {
###################################################################### ######################################################################
sub is_ipv6 { sub is_ipv6 {
my ($value) = @_; my ($value) = @_;
return (length($value // '') != 0) && ($value eq (ipv6_match($value) // '')); return (length($value // '') != 0) && ($value eq (ipv6_extract($value) // ''));
} }
###################################################################### ######################################################################
## ipv6_match() extracts the first valid IPv6 address from given string ## ipv6_extract() extracts the first valid IPv6 address from given string
## IPv6 must be in standard or compressed format. ## IPv6 must be in standard or compressed format.
## Mixed IPv6/IPv4 not supported. ## Mixed IPv6/IPv4 not supported.
## Accepts leading zeros in the address but removes them in returned value ## Accepts leading zeros in the address but removes them in returned value
###################################################################### ######################################################################
sub ipv6_match { sub ipv6_extract{
(shift // '') =~ /(?<![:.\w]) ## Negative Look behind for word boundry (shift // '') =~ /(?<![:.\w]) ## Negative lookbehind for word boundry
( (?:[0-9A-F]{1,4}:){7}[0-9A-F]{1,4} ## Standard format ( (?:[0-9A-F]{1,4}:){7}[0-9A-F]{1,4} ## Is in standard format
|(?=(?:[0-9A-F]{0,4}:){0,7}[0-9A-F]{0,4} ## OR compressed with at most 7 colons |(?=(?:[0-9A-F]{0,4}:){0,7}[0-9A-F]{0,4} ## OR compressed with at most 7 colons
(?![:.\w])) ## and negative lookahead for boundry (?![:.\w])) ## and negative lookahead for boundry
(?:(?:[0-9A-F]{1,4}:){1,7}|:)(?:(?::[0-9A-F]{1,4}){1,7}|:) ## and only 1 double-colon (?:(?:[0-9A-F]{1,4}:){1,7}|:)(?:(?::[0-9A-F]{1,4}){1,7}|:) ## and only 1 double-colon
@ -2325,18 +2310,20 @@ sub ipv6_match {
|:(?::[0-9A-F]{1,4}){7} ## OR compressed with 8 colons (double at front) |:(?::[0-9A-F]{1,4}){7} ## OR compressed with 8 colons (double at front)
) ## End of first capture group ) ## End of first capture group
(?![:.\w])/xai; ## Negative lookahead for boundry (?![:.\w])/xai; ## Negative lookahead for boundry
return ($1 // '') =~ s/\b0+\B//gr; ## Remove leading zeros my $ip = ($1 // '');
$ip =~ s/\b0+\B//g; ## remove leading zeros
return $ip;
} }
###################################################################### ######################################################################
## ipv6_match_gua() extracts the first valid IPv6 Global Unicast Address (GUA) ## ipv6_extract_gua() extracts the first valid IPv6 Global Unicast Address (GUA)
## from given string. IPv6 must be in standard or compressed format. ## from given string. IPv6 must be in standard or compressed format.
## Mixed IPv6/IPv4 not supported. ## Mixed IPv6/IPv4 not supported.
## Accepts leading zeros in the address but removes them in returned value ## Accepts leading zeros in the address but removes them in returned value
###################################################################### ######################################################################
sub ipv6_match_gua { sub ipv6_extract_gua {
(shift // '') =~ /(?<![:.\w]) ## Negative Look behind for word boundry (shift // '') =~ /(?<![:.\w]) ## Negative lookbehind for word boundry
( [23][A-F0-9]{3}: ## Starts 2xxx: or 3xxx: ( [23][A-F0-9]{3}: ## Starts with 2xxx: or 3xxx:
(?:[0-9A-F]{1,4}:){6}[0-9A-F]{1,4} ## in standard format (?:[0-9A-F]{1,4}:){6}[0-9A-F]{1,4} ## in standard format
|(?=[23][A-F0-9]{3}: ## OR starts with 2xxx: or 3xxx: |(?=[23][A-F0-9]{3}: ## OR starts with 2xxx: or 3xxx:
(?:[0-9A-F]{0,4}:){0,6}[0-9A-F]{0,4} ## in compressed with at most 7 colons (?:[0-9A-F]{0,4}:){0,6}[0-9A-F]{0,4} ## in compressed with at most 7 colons
@ -2346,7 +2333,9 @@ sub ipv6_match_gua {
(?:[0-9A-F]{1,4}:){6}: ## compressed with 8 colons (double at end) (?:[0-9A-F]{1,4}:){6}: ## compressed with 8 colons (double at end)
) ## End of first capture group ) ## End of first capture group
(?![:.\w])/xai; ## Negative lookahead for boundry (?![:.\w])/xai; ## Negative lookahead for boundry
return ($1 // '') =~ s/\b0+\B//gr; ## Remove leading zeros my $ip = ($1 // '');
$ip =~ s/\b0+\B//g; ## remove leading zeros
return $ip;
} }
###################################################################### ######################################################################
@ -4254,7 +4243,7 @@ sub nic_nsupdate_update {
my $zone = $config{$h}{'zone'}; my $zone = $config{$h}{'zone'};
my $ip = $config{$h}{'wantip'}; my $ip = $config{$h}{'wantip'};
my $recordtype = ''; my $recordtype = '';
if (ipv6_match($ip)) { if (is_ipv6($ip)) {
$recordtype = 'AAAA'; $recordtype = 'AAAA';
} else { } else {
$recordtype = 'A'; $recordtype = 'A';
@ -4403,7 +4392,7 @@ sub nic_cloudflare_update {
# Get DNS record ID # Get DNS record ID
$url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?"; $url = "https://$config{$key}{'server'}/zones/$zone_id/dns_records?";
if (ipv6_match($ip)) { if (is_ipv6($ip)) {
$url .= "type=AAAA&name=$domain"; $url .= "type=AAAA&name=$domain";
} else { } else {
$url .= "type=A&name=$domain"; $url .= "type=A&name=$domain";