fix DXCIDR, show/badip, difft, speedup is_ipaddr()
authorDirk Koopman <djk@tobit.co.uk>
Thu, 23 Mar 2023 16:54:51 +0000 (16:54 +0000)
committerDirk Koopman <djk@tobit.co.uk>
Thu, 23 Mar 2023 16:54:51 +0000 (16:54 +0000)
Changes
cmd/show/badip.pl
perl/DXCIDR.pm
perl/DXUtil.pm

diff --git a/Changes b/Changes
index ab9c9c445c237a0b3a3aadf667f4d2e52274863f..d33326e94553432f5047f63de3ae611531667173 100644 (file)
--- 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(<ip address>). 
+22Mar23=======================================================================
+1. Fix warning in DXUtil::difft.
 21Mar23=======================================================================
 1. Fix DXUtil is_ipaddr function which was accidentally included in previous
    commit.
index 55fa556b8e675384e9ccd75d24439721d0ddfd16..a92329c9fde1d1c4ed350ef23fe9619b01493e2b 100644 (file)
@@ -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;
 }
index fae0f671140925cbe3aa9cfe7536cb663da510a5..680c66ddaa6fd29138d78143dfb5d46203059feb 100644 (file)
@@ -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;
 }
 
index b01c234b80bfeea0bf1f60b25cf1679f546b8af0..286f544b1bc017c00d612df26a927f893a85338b 100644 (file)
@@ -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') {