From: Dirk Koopman Date: Thu, 23 Mar 2023 16:54:51 +0000 (+0000) Subject: fix DXCIDR, show/badip, difft, speedup is_ipaddr() X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=commitdiff_plain;h=19241b6f37923a92a56bf7890a5126f30f9b9d05;p=spider.git fix DXCIDR, show/badip, difft, speedup is_ipaddr() --- diff --git a/Changes b/Changes index ab9c9c44..d33326e9 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,10 @@ +23Mar23======================================================================= +1. Fix DXCIDR brokenness introduced recently, speeded up DXCIDR::sort, allow + sh/badip 2a05:dfc7:402a::1 45.134.225.96 159.69.38.160 to test individual + IP address as well as show the default (of every badip). +2. Speed up and be more strict with input for is_ipaddr(). +22Mar23======================================================================= +1. Fix warning in DXUtil::difft. 21Mar23======================================================================= 1. Fix DXUtil is_ipaddr function which was accidentally included in previous commit. diff --git a/cmd/show/badip.pl b/cmd/show/badip.pl index 55fa556b..a92329c9 100644 --- a/cmd/show/badip.pl +++ b/cmd/show/badip.pl @@ -16,36 +16,38 @@ my @added; my @in = split /\s+/, $line; my $maxlth = 0; my $width = $self->width // 80; +my $count = 0; #$DB::single = 1; -my @list = map {my $s = $_; $s =~ s|/32$||; $maxlth = length $s if length $s > $maxlth; $s =~ /^1$/?undef:$s} DXCIDR::list(); -my @l; -$maxlth //= 20; -my $n = int ($width/($maxlth+1)); -my $format = "\%-${maxlth}s " x $n; -chop $format; - -my $count = 0; - -foreach my $list (@list) { - if (@in) { - for my $in (@in) { - if ($list =~ /$in/i) { - push @out, $list; - ++$count; - } +# query +if (@in) { + foreach my $ip (@in) { + if (DXCIDR::find($ip)) { + push @out, "$ip FOUND"; + ++$count; + } else { + push @out, "$ip CLEAN"; } - } else { + } + return (1, @out); +} else { +# list + my @list = map {my $s = $_; $s =~ s!/(?:32|128)$!!; $maxlth = length $s if length $s > $maxlth; $s =~ /^1$/?undef:$s} DXCIDR::list(); + my @l; + $maxlth //= 20; + my $n = int ($width/($maxlth+1)); + my $format = "\%-${maxlth}s " x $n; + chop $format; + + foreach my $list (@list) { ++$count; if (@l > $n) { push @out, sprintf $format, @l; @l = (); } push @l, $list; - } -} -unless (@in) { + } push @l, "" while @l < $n; push @out, sprintf $format, @l; } diff --git a/perl/DXCIDR.pm b/perl/DXCIDR.pm index fae0f671..680c66dd 100644 --- a/perl/DXCIDR.pm +++ b/perl/DXCIDR.pm @@ -130,7 +130,7 @@ sub add next unless is_ipaddr($ip); next if $ip =~ /^127\./; next if $ip =~ /^::1$/; - next if find($ip); +# next if find($ip); if ($ip =~ /\./) { eval {$ipv4->add_any($ip)}; if ($@) { @@ -172,11 +172,27 @@ sub _sort { my @in; my @out; - for (@_) { - my @ip = split m|/|; - push @in, [inet_pton(m|:|?AF_INET6:AF_INET, $ip[0]), @ip]; + my $c; + for my $i (@_) { + my @s; + + my @ip = split m|/|, $i; + if ($ip[0] =~ /:/) { + @s = map{$_ ? hex($_) : 0} split /:/, $ip[0]; + } else { + @s = map{$_ ? $_+0 : 0} split /\./, $ip[0]; + } + while (@s < 8) { + push @s, 0; + } +# my $s = pack "S*", reverse @s; + my $s = pack "n*", @s; +# my $s = join ':', map {sprintf "%04d:", $_} @s; +# push @in, [inet_pton(m|:|?AF_INET6:AF_INET, $ip[0]), @ip]; + push @in, [$s, @ip]; } - @out = sort {$a->[0] <=> $b->[0]} @in; + @out = sort {$a->[0] cmp $b->[0]} @in; +# @out = sort {$a->[0] <=> $b->[0]} @in; return map { "$_->[1]/$_->[2]"} @out; } diff --git a/perl/DXUtil.pm b/perl/DXUtil.pm index b01c234b..286f544b 100644 --- a/perl/DXUtil.pm +++ b/perl/DXUtil.pm @@ -46,6 +46,14 @@ $pi = 3.141592653589; $d2r = ($pi/180); $r2d = (180/$pi); +our $ptonok; + +BEGIN { + $ptonok = !defined inet_pton(AF_INET, '016.17.184.1') + && !defined inet_pton(AF_INET6, '2067::1:') + # Some old versions of Socket are hopelessly broken + && length(inet_pton(AF_INET, '1.1.1.1')) == 4; +} # a full time for logging and other purposes sub atime @@ -449,16 +457,22 @@ sub is_latlong # is it an ip address? sub is_ipaddr { - - if ($_[0] =~ /:/) { - if (inet_pton(AF_INET6, $_[0])) { - return ($_[0] =~ /([:0-9a-f]+)/); + if ($ptonok) { + if ($_[0] =~ /:/) { + if (inet_pton(AF_INET6, $_[0])) { + return ($_[0] =~ /([:0-9a-f]+)/); + } + } else { + if (inet_pton(AF_INET, $_[0])) { + return ($_[0] =~ /([\.\d]+)/); + } } -# use re 'debug'; -# return ($1) if $_[0] =~ /^(\:?(?:\:?[0-9a-f]{1,4}){1,8}?)$/i; -# no re 'debug'; } else { - return ($_[0] =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/); + if ($_[0] =~ /:/) { + return ($_[0] =~ /^(:?:?(?:[0-9a-f]{1,4}\:)?(?:\:[0-9a-f]{1,4}(?:\:\:)?){1,8})$/i); + } else { + return ($_[0] =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/); + } } return undef; } @@ -563,7 +577,7 @@ sub diffms sub difft { my $b = shift; - my $adds = shift; + my $adds = shift // 0; my $t; if (ref $b eq 'ARRAY') {