]> git.99rst.org Git - irssi-scripts.irssi.org.git/commitdiff
[dns] add ipv6
authorbw1 <redacted>
Thu, 24 Jan 2019 15:31:43 +0000 (16:31 +0100)
committerbw1 <redacted>
Thu, 24 Jan 2019 15:31:43 +0000 (16:31 +0100)
scripts/dns.pl

index 989cdc3ec49a2acb2934b3a5099d108b94c0b5b8..9e9f4789ce2d90bc9d202dd63685a44d1189cace 100644 (file)
@@ -1,16 +1,18 @@
 # /DNS <nick>|<host>|<ip> ...
-# version 2.1.1
-# 
-# updated the script to fix a bug where the script would let
-# a trailing whitespace go through (ex: tab completion)
-# - inch <inch@stmpd.net>
+#
+# v2.2
+#   add ipv6 support
+# v2.1.1
+#   updated the script to fix a bug where the script would let
+#   a trailing whitespace go through (ex: tab completion)
+#   - inch <inch@stmpd.net>
 
 use strict;
 use Socket;
 use POSIX;
 
 use vars qw($VERSION %IRSSI); 
-$VERSION = "2.1.1";
+$VERSION = "2.2";
 %IRSSI = (
     authors    => "Timo \'cras\' Sirainen",
     contact    => "tss\@iki.fi", 
@@ -18,7 +20,7 @@ $VERSION = "2.1.1";
     description        => "/DNS <nick>|<host>|<ip> ...",
     license    => "Public Domain",
     url                => "http://irssi.org/",
-    changed    => "2002-03-04T22:47+0100"
+    changed    => "2019-01-24"
 );
 
 my (%resolve_hosts, %resolve_nicks, %resolve_print); # resolve queues
@@ -102,6 +104,42 @@ sub sig_userhost {
   host_lookup() if (!$lookup_waiting);
 }
 
+sub dns {
+  my ($host) =@_;
+  my %hints = (socktype => SOCK_STREAM);
+  my ($err, @res) = Socket::getaddrinfo($host, "http", \%hints);
+  my @res1;
+  if ($err ==0 ) {
+    foreach(@res) {
+      if ($_->{family}==AF_INET) {
+       my ($proto,$ip)=unpack_sockaddr_in($_->{addr});
+       push @res1, Socket::inet_ntop(AF_INET,$ip);
+      }
+      if ($_->{family}==AF_INET6) {
+       my ($proto,$ip)=unpack_sockaddr_in6($_->{addr});
+       push @res1, Socket::inet_ntop(AF_INET6,$ip);
+      }
+    }
+    return join(' ',@res1);
+  }
+}
+
+sub rdns {
+  my ($host) =@_;
+  my %hints = (socktype => SOCK_STREAM);
+  my ($err, @res) = Socket::getaddrinfo($host, "http", \%hints);
+  my @res1;
+  if ($err ==0 ) {
+    foreach(@res) {
+      my ($err, $hostname, $servicename) = Socket::getnameinfo $_->{addr};
+      if ($err ==0) {
+       push @res1, $hostname;
+      }
+    }
+    return join(' ',@res1);
+  }
+}
+
 sub host_lookup {
   return if (!%resolve_hosts);
 
@@ -145,16 +183,13 @@ sub host_lookup {
   eval {
     # child, do the lookup
     my $name = "";
-    if ($host =~ /^[0-9\.]*$/) {
+    if ($host =~ /^[0-9\.]*$/ || $host =~ m/^[0-9a-f:]*$/) {
       # ip -> host
-      $name = gethostbyaddr(inet_aton($host), AF_INET);
+      #$name = gethostbyaddr(inet_aton($host), AF_INET);
+      $name = rdns($host);
     } else {
       # host -> ip
-      my @addrs = gethostbyname($host);
-      if (@addrs) {
-       @addrs = map { inet_ntoa($_) } @addrs[4 .. $#addrs];
-       $name = join (" ", @addrs);
-      }
+      $name = dns($host);
     }
 
     $print_name = $input_query if !$print_name;
@@ -197,3 +232,5 @@ Irssi::command_bind('dns', 'cmd_dns');
 Irssi::signal_add( {
         'redir dns failure' => \&sig_failure,
         'redir dns host' => \&sig_userhost } );
+
+# vim:set sw=2 ts=8:
git clone https://git.99rst.org/PROJECT