From 22e34a1efe44bbd2a287a87248c3cfc4d320d53c Mon Sep 17 00:00:00 2001 From: David Kerr Date: Sun, 28 Jun 2020 20:21:58 -0400 Subject: [PATCH] More code review updates --- ddclient | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/ddclient b/ddclient index 82311fa..15b8a2e 100755 --- a/ddclient +++ b/ddclient @@ -2156,7 +2156,11 @@ sub get_ip { $arg = '' unless $arg; if ($use eq 'ip') { - $reply = opt('ip', $h); + $ip = opt('ip', $h); + if (!is_ipv4($ip) && !is_ipv6($ip)) { + warning("'%s' is not a valid IPv4 or IPv6 address, ignoring value.",$ip); + $ip = undef; + } $arg = 'ip'; } elsif ($use eq 'if') { @@ -2253,7 +2257,7 @@ sub get_ip { $skip =~ s/ /\\s/is; $reply =~ s/^.*?${skip}//is; } - $ip //= ipv4_extract($reply) // ipv6_extract($reply); + $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; @@ -2270,18 +2274,19 @@ sub get_ip { ###################################################################### sub is_ipv4 { my ($value) = @_; - return (length($value // '') != 0) && ($value eq (ipv4_extract($value) // '')); + return (length($value // '') != 0) && ($value eq (extract_ipv4($value) // '')); } ###################################################################### -## ipv4_extract() extracts the first valid IPv4 address from 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 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 +sub extract_ipv4{ + if ((shift // '') !~ /\b((?:(?25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3} + (?&octet))\b/xa) { + return undef; + } + (my $ip = $1) =~ s/\b0+\B//g; ## remove embedded leading zeros return $ip; } @@ -2291,17 +2296,17 @@ sub ipv4_extract{ ###################################################################### sub is_ipv6 { my ($value) = @_; - return (length($value // '') != 0) && ($value eq (ipv6_extract($value) // '')); + return (length($value // '') != 0) && ($value eq (extract_ipv6($value) // '')); } ###################################################################### -## ipv6_extract() extracts the first valid IPv6 address from given string +## extract_ipv6() 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_extract{ - (shift // '') =~ /(?