From 36d8b511b37ec981700330ccc3dd795a8b708989 Mon Sep 17 00:00:00 2001 From: David Kerr Date: Tue, 30 Jun 2020 13:29:10 -0400 Subject: [PATCH] Move `un_zero_pad` into `extract_ipv4`, `extract_ipv6` This also causes `is_ipv4` to reject IPv4 addresses with leading zeros. --- ddclient.in | 51 ++++++++++++++++----------------------------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/ddclient.in b/ddclient.in index 9883d31..90a2f96 100755 --- a/ddclient.in +++ b/ddclient.in @@ -2181,26 +2181,7 @@ sub geturl { $reply =~ s/\r//g if defined $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 ###################################################################### @@ -2312,15 +2293,8 @@ sub get_ip { $skip =~ s/ /\\s/is; $reply =~ s/^.*?${skip}//is; } - if (defined($ip)) { - # no need to parse $reply - } 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"); - } + $ip //= extract_ipv4($reply) // extract_ipv6($reply); + warning("found neither IPv4 nor IPv6 address") if !defined($ip); if ($use ne 'ip' && ($ip // '') eq '0.0.0.0') { $ip = undef; } @@ -2330,8 +2304,9 @@ sub get_ip { } ###################################################################### -## is_ipv4() validates if string is valid IPv4 address and only -## a valid IPv4 address (no preceding or trailing spaces/characters) +## is_ipv4() validates if string is valid IPv4 address and only a +## valid address with no preceding or trailing spaces/characters +## and no embedded leading zeros. ###################################################################### sub is_ipv4 { my ($value) = @_; @@ -2339,11 +2314,14 @@ 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 { - (shift // '') =~ /\b((?:(?25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?&octet))\b/a; - return $1; + (shift // '') =~ /\b((?:(?25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?&octet))\b/a + or return undef; + (my $ip = $1) =~ s/\b0+\B//g; ## remove embedded leading zeros + return $ip; } ###################################################################### @@ -2352,11 +2330,13 @@ sub extract_ipv4 { ###################################################################### sub is_ipv6 { my ($value) = @_; - return (length($value // '') != 0) && ((extract_ipv6($value) // '') eq $value); + return (length($value // '') != 0) && + ((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 { my $content = shift; @@ -2378,6 +2358,7 @@ sub extract_ipv6 { next } } + $parsed =~ s/\b0+\B//g; ## remove embedded leading zeros return $parsed; } return;