diff --git a/ddclient b/ddclient index 6869ebc..50ad5ce 100755 --- a/ddclient +++ b/ddclient @@ -942,18 +942,12 @@ sub update_nics { 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}; } else { - my $reply = get_ip($use, $h) // ''; - if (!$reply) { + my $ip = get_ip($use, $h) // ''; + if (!$ip) { warning("unable to determine IP address") if !$daemon || opt('verbose'); 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; } $config{$h}{'wantip'} = $ip; @@ -1900,11 +1894,7 @@ sub check_value { return undef if $value eq ""; } elsif ($type eq T_IP) { - if (!ipv6_match($value)) { - return undef if !($value = ipv4_match($value)); - } else { - $value = ipv6_match($value); - } + return undef if !is_ipv4($value) && !is_ipv6($value); } return $value; } @@ -2166,7 +2156,7 @@ sub get_ip { $arg = '' unless $arg; if ($use eq 'ip') { - $ip = opt('ip', $h); + $reply = opt('ip', $h); $arg = 'ip'; } elsif ($use eq 'if') { @@ -2263,15 +2253,8 @@ sub get_ip { $skip =~ s/ /\\s/is; $reply =~ s/^.*?${skip}//is; } - if (defined($ip)) { - # no need to parse $reply - } 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"); - } + $ip //= ipv4_extract($reply) // ipv6_extract($reply); + warning("found neither IPv4 nor IPv6 address") if !defined($ip); if ($use ne 'ip' && ($ip // '') eq '0.0.0.0') { $ip = undef; } @@ -2287,17 +2270,19 @@ sub get_ip { ###################################################################### sub is_ipv4 { 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 ###################################################################### -sub ipv4_match { - (shift // '') =~ /\b((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} ## first three bytes - (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\b/xai; ## last one - return ($1 // '') =~ s/\b0+\B//gr; ## remove leading zeros +sub ipv4_extract{ + (shift // '') =~ /\b((?:(?25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3} ## first three bytes + (?&octet))\b/xa; ## last one (without period) + my $ip = ($1 // ''); + $ip =~ s/\b0+\B//g; ## remove leading zeros + return $ip; } ###################################################################### @@ -2306,18 +2291,18 @@ sub ipv4_match { ###################################################################### sub is_ipv6 { 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. ## Mixed IPv6/IPv4 not supported. ## Accepts leading zeros in the address but removes them in returned value ###################################################################### -sub ipv6_match { - (shift // '') =~ /(?