fix DXCIDR, show/badip, difft, speedup is_ipaddr()
[spider.git] / perl / DXUtil.pm
index b89d88ea9a89def800ffdfcba3ba39d1cc4e26aa..286f544b1bc017c00d612df26a927f893a85338b 100644 (file)
@@ -15,6 +15,7 @@ use File::Copy;
 use Data::Dumper;
 use Time::HiRes qw(gettimeofday tv_interval);
 use Text::Wrap;
+use Socket qw(AF_INET6 AF_INET inet_pton);
 
 use strict;
 
@@ -45,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
@@ -448,7 +457,24 @@ sub is_latlong
 # is it an ip address?
 sub is_ipaddr
 {
-    return $_[0] =~ /^(?:(?:\:\:)?\d+\.\d+\.\d+\.\d+)|(?:[0-9a-f]{1,4}\:)?(?:\:[0-9a-f]{1,4}(?:\:\:)?){1,6}$/i;
+       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]+)/);
+                       }
+               }
+       } else {
+               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;
 }
 
 # is it a zulu time hhmmZ
@@ -551,7 +577,7 @@ sub diffms
 sub difft
 {
        my $b = shift;
-       my $adds = shift;
+       my $adds = shift // 0;
        
        my $t;
        if (ref $b eq 'ARRAY') {