d24e64f
diff -up screen/src/doc/screen.1.ipv6 screen/src/doc/screen.1
d24e64f
--- screen/src/doc/screen.1.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/doc/screen.1	2011-02-03 16:05:35.957087079 +0100
d24e64f
@@ -398,6 +398,12 @@ Send the specified command to a running 
d24e64f
 the \fB-d\fP or \fB-r\fP option to tell screen to look only for
d24e64f
 attached or detached screen sessions. Note that this command doesn't
d24e64f
 work if the session is password protected.
d24e64f
+.TP 5
d24e64f
+.B \-4
d24e64f
+Resolve hostnames only to IPv4 addresses.
d24e64f
+.TP 5
d24e64f
+.B \-6
d24e64f
+Resolve hostnames only to IPv6 addresses.
d24e64f
 
d24e64f
 .SH "DEFAULT KEY BINDINGS"
d24e64f
 .ta 12n 26n
d24e64f
diff -up screen/src/extern.h.ipv6 screen/src/extern.h
d24e64f
--- screen/src/extern.h.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/extern.h	2011-02-03 16:04:35.995743620 +0100
d24e64f
@@ -455,8 +455,7 @@ extern void  ExitOverlayPage __P((void))
d24e64f
 
d24e64f
 /* teln.c */
d24e64f
 #ifdef BUILTIN_TELNET
d24e64f
-extern int   TelOpen __P((char **));
d24e64f
-extern int   TelConnect __P((struct win *));
d24e64f
+extern int   TelOpenAndConnect __P((struct win *));
d24e64f
 extern int   TelIsline __P((struct win *p));
d24e64f
 extern void  TelProcessLine __P((char **, int *));
d24e64f
 extern int   DoTelnet __P((char *, int *, int));
d24e64f
diff -up screen/src/help.c.ipv6 screen/src/help.c
d24e64f
--- screen/src/help.c.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/help.c	2011-02-03 16:04:35.992744154 +0100
d24e64f
@@ -66,6 +66,10 @@ char *myname, *message, *arg;
d24e64f
 {
d24e64f
   printf("Use: %s [-opts] [cmd [args]]\n", myname);
d24e64f
   printf(" or: %s -r [host.tty]\n\nOptions:\n", myname);
d24e64f
+#ifdef BUILTIN_TELNET
d24e64f
+  printf("-4            Resolve hostnames only to IPv4 addresses.\n");
d24e64f
+  printf("-6            Resolve hostnames only to IPv6 addresses.\n");
d24e64f
+#endif
d24e64f
   printf("-a            Force all capabilities into each window's termcap.\n");
d24e64f
   printf("-A -[r|R]     Adapt all windows to the new display width & height.\n");
d24e64f
   printf("-c file       Read configuration file instead of '.screenrc'.\n");
d24e64f
diff -up screen/src/screen.c.ipv6 screen/src/screen.c
d24e64f
--- screen/src/screen.c.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/screen.c	2011-02-03 16:04:35.994743798 +0100
d24e64f
@@ -248,8 +248,9 @@ struct layer *flayer;
d24e64f
 struct win *fore;
d24e64f
 struct win *windows;
d24e64f
 struct win *console_window;
d24e64f
-
d24e64f
-
d24e64f
+#ifdef BUILTIN_TELNET
d24e64f
+int af;
d24e64f
+#endif
d24e64f
 
d24e64f
 /*
d24e64f
  * Do this last
d24e64f
@@ -507,6 +508,9 @@ char **av;
d24e64f
   nwin = nwin_undef;
d24e64f
   nwin_options = nwin_undef;
d24e64f
   strcpy(screenterm, "screen");
d24e64f
+#ifdef BUILTIN_TELNET
d24e64f
+  af = AF_UNSPEC;
d24e64f
+#endif
d24e64f
 
d24e64f
   logreopen_register(lf_secreopen);
d24e64f
 
d24e64f
@@ -541,6 +545,14 @@ char **av;
d24e64f
 	    {
d24e64f
 	      switch (*ap)
d24e64f
 		{
d24e64f
+#ifdef BUILTIN_TELNET
d24e64f
+               case '4':
d24e64f
+                 af = AF_INET;
d24e64f
+                 break;
d24e64f
+               case '6':
d24e64f
+                 af = AF_INET6;
d24e64f
+                 break;
d24e64f
+#endif
d24e64f
 		case 'a':
d24e64f
 		  nwin_options.aflag = 1;
d24e64f
 		  break;
d24e64f
diff -up screen/src/teln.c.ipv6 screen/src/teln.c
d24e64f
--- screen/src/teln.c.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/teln.c	2011-02-03 16:04:35.992744154 +0100
d24e64f
@@ -30,6 +30,7 @@
d24e64f
 #include <sys/socket.h>
d24e64f
 #include <fcntl.h>
d24e64f
 #include <netdb.h>
d24e64f
+#include <stdio.h>
d24e64f
 
d24e64f
 #include "config.h"
d24e64f
 
d24e64f
@@ -42,12 +43,13 @@ extern struct win *fore;
d24e64f
 extern struct layer *flayer;
d24e64f
 extern int visual_bell;
d24e64f
 extern char screenterm[];
d24e64f
+extern int af;
d24e64f
 
d24e64f
 static void TelReply __P((struct win *, char *, int));
d24e64f
 static void TelDocmd __P((struct win *, int, int));
d24e64f
 static void TelDosub __P((struct win *));
d24e64f
-
d24e64f
-#define TEL_DEFPORT	23
d24e64f
+// why TEL_DEFPORT has "
d24e64f
+#define TEL_DEFPORT	"23"
d24e64f
 #define TEL_CONNECTING	(-2)
d24e64f
 
d24e64f
 #define TC_IAC          255
d24e64f
@@ -105,86 +107,78 @@ char *data;
d24e64f
 }
d24e64f
 
d24e64f
 int
d24e64f
-TelOpen(args)
d24e64f
-char **args;
d24e64f
-{
d24e64f
-  int fd;
d24e64f
-  int on = 1;
d24e64f
-
d24e64f
-  if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
d24e64f
-    {
d24e64f
-      Msg(errno, "TelOpen: socket");
d24e64f
-      return -1;
d24e64f
-    }
d24e64f
-  if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)))
d24e64f
-    Msg(errno, "TelOpen: setsockopt SO_OOBINLINE");
d24e64f
-  return fd;
d24e64f
-}
d24e64f
-
d24e64f
-int
d24e64f
-TelConnect(p)
d24e64f
-struct win *p;
d24e64f
-{
d24e64f
-  int port = TEL_DEFPORT;
d24e64f
-  struct hostent *hp;
d24e64f
-  char **args;
d24e64f
+TelOpenAndConnect(struct win *p) {
d24e64f
+  int fd, on = 1;
d24e64f
   char buf[256];
d24e64f
 
d24e64f
-  args = p->w_cmdargs + 1;
d24e64f
-
d24e64f
-  if (!*args)
d24e64f
-    {
d24e64f
-      Msg(0, "Usage: screen //telnet host [port]");
d24e64f
-      return -1;
d24e64f
-    }
d24e64f
-  if (args[1])
d24e64f
-    port = atoi(args[1]);
d24e64f
-  p->w_telsa.sin_family = AF_INET;
d24e64f
-  if((p->w_telsa.sin_addr.s_addr = inet_addr(*args)) == -1)
d24e64f
-    {
d24e64f
-      if ((hp = gethostbyname(*args)) == NULL)
d24e64f
-        {
d24e64f
-	  Msg(0, "unknown host: %s", *args);
d24e64f
-	  return -1;
d24e64f
-        }
d24e64f
-      if (hp->h_length != sizeof(p->w_telsa.sin_addr.s_addr) || hp->h_addrtype != AF_INET)
d24e64f
-	{
d24e64f
-	  Msg(0, "Bad address type for %s", hp->h_name);
d24e64f
-	  return -1;
d24e64f
-	}
d24e64f
-      bcopy((char *)hp->h_addr,(char *)&p->w_telsa.sin_addr.s_addr, hp->h_length);
d24e64f
-      p->w_telsa.sin_family = hp->h_addrtype;
d24e64f
-    }
d24e64f
-  p->w_telsa.sin_port = htons(port);
d24e64f
-  if (port != TEL_DEFPORT)
d24e64f
-    sprintf(buf, "Trying %s %d...", inet_ntoa(p->w_telsa.sin_addr), port);
d24e64f
-  else
d24e64f
-    sprintf(buf, "Trying %s...", inet_ntoa(p->w_telsa.sin_addr));
d24e64f
-  WriteString(p, buf, strlen(buf));
d24e64f
-  if (connect(p->w_ptyfd, (struct sockaddr *)&p->w_telsa, sizeof(p->w_telsa)))
d24e64f
-    {
d24e64f
-      if (errno == EINPROGRESS)
d24e64f
-        {
d24e64f
-	  p->w_telstate = TEL_CONNECTING;
d24e64f
-	  p->w_telconnev.fd = p->w_ptyfd;
d24e64f
-	  p->w_telconnev.handler = tel_connev_fn;
d24e64f
-	  p->w_telconnev.data = (char *)p;
d24e64f
-	  p->w_telconnev.type = EV_WRITE;
d24e64f
-	  p->w_telconnev.pri = 1;
d24e64f
-	  debug("telnet connect in progress...\n");
d24e64f
-	  evenq(&p->w_telconnev);
d24e64f
-	}
d24e64f
-      else
d24e64f
-        {
d24e64f
-	  Msg(errno, "TelOpen: connect");
d24e64f
-	  return -1;
d24e64f
-	}
d24e64f
-    }
d24e64f
-  else
d24e64f
-    WriteString(p, "connected.\r\n", 12);
d24e64f
-  if (port == TEL_DEFPORT)
d24e64f
-    TelReply(p, (char *)tn_init, sizeof(tn_init));
d24e64f
-  return 0;
d24e64f
+  struct addrinfo hints, *res0, *res;
d24e64f
+
d24e64f
+  if (!(p->w_cmdargs[1])) {
d24e64f
+    Msg(0, "Usage: screen //telnet host [port]");
d24e64f
+    return -1;
d24e64f
+  }
d24e64f
+
d24e64f
+  memset(&hints, 0, sizeof(hints));
d24e64f
+  hints.ai_family = af;
d24e64f
+  hints.ai_socktype = SOCK_STREAM;
d24e64f
+  hints.ai_protocol = IPPROTO_TCP;
d24e64f
+  if(getaddrinfo(p->w_cmdargs[1], p->w_cmdargs[2] ? p->w_cmdargs[2] : TEL_DEFPORT,
d24e64f
+                 &hints, &res0)) {
d24e64f
+     Msg(0, "unknown host: %s", p->w_cmdargs[1]);
d24e64f
+     return -1;
d24e64f
+  }
d24e64f
+
d24e64f
+  for(res = res0; res; res = res->ai_next) {
d24e64f
+    if((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
d24e64f
+      if(res->ai_next)
d24e64f
+        continue;
d24e64f
+      else {
d24e64f
+        Msg(errno, "TelOpenAndConnect: socket");
d24e64f
+        freeaddrinfo(res0);
d24e64f
+        return -1;
d24e64f
+      }
d24e64f
+    }
d24e64f
+
d24e64f
+    if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)))
d24e64f
+      Msg(errno, "TelOpenAndConnect: setsockopt SO_OOBINLINE");
d24e64f
+
d24e64f
+    if (p->w_cmdargs[2] && strcmp(p->w_cmdargs[2], TEL_DEFPORT))
d24e64f
+      snprintf(buf, 256, "Trying %s %s...", p->w_cmdargs[1], p->w_cmdargs[2]);
d24e64f
+    else
d24e64f
+      snprintf(buf, 256, "Trying %s...", p->w_cmdargs[1]);
d24e64f
+    WriteString(p, buf, strlen(buf));
d24e64f
+    if (connect(fd, res->ai_addr, res->ai_addrlen)) {
d24e64f
+      if (errno == EINPROGRESS) {
d24e64f
+       p->w_telstate = TEL_CONNECTING;
d24e64f
+       p->w_telconnev.fd = fd;
d24e64f
+       p->w_telconnev.handler = tel_connev_fn;
d24e64f
+       p->w_telconnev.data = (char *)p;
d24e64f
+       p->w_telconnev.type = EV_WRITE;
d24e64f
+       p->w_telconnev.pri = 1;
d24e64f
+       debug("telnet connect in progress...\n");
d24e64f
+       evenq(&p->w_telconnev);
d24e64f
+      }
d24e64f
+      else {
d24e64f
+        close(fd);
d24e64f
+       if(res->ai_next)
d24e64f
+         continue;
d24e64f
+       else {
d24e64f
+          Msg(errno, "TelOpenAndConnect: connect");
d24e64f
+          freeaddrinfo(res0);
d24e64f
+          return -1;
d24e64f
+       }
d24e64f
+      }
d24e64f
+    }
d24e64f
+    else
d24e64f
+      WriteString(p, "connected.\r\n", 12);
d24e64f
+    if (!(p->w_cmdargs[2] && strcmp(p->w_cmdargs[2], TEL_DEFPORT)))
d24e64f
+      TelReply(p, (char *)tn_init, sizeof(tn_init));
d24e64f
+    p->w_ptyfd = fd;
d24e64f
+    memcpy(&p->w_telsa, &res->ai_addr, sizeof(res->ai_addr));
d24e64f
+    freeaddrinfo(res0);
d24e64f
+    return 0;
d24e64f
+  }
d24e64f
+  return -1;
d24e64f
 }
d24e64f
 
d24e64f
 int
d24e64f
diff -up screen/src/window.c.ipv6 screen/src/window.c
d24e64f
--- screen/src/window.c.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/window.c	2011-02-03 16:30:56.900750293 +0100
d24e64f
@@ -605,6 +605,13 @@ struct NewWindow *newwin;
d24e64f
   n = pp - wtab;
d24e64f
   debug1("Makewin creating %d\n", n);
d24e64f
 
d24e64f
+#ifdef BUILTIN_TELNET
d24e64f
+  if(!strcmp(nwin.args[0], "//telnet")) {
d24e64f
+         type = W_TYPE_TELNET;
d24e64f
+         TtyName = "telnet";
d24e64f
+  }
d24e64f
+  else
d24e64f
+#endif
d24e64f
   if ((f = OpenDevice(nwin.args, nwin.lflag, &type, &TtyName)) < 0)
d24e64f
     return -1;
d24e64f
   if (type == W_TYPE_GROUP)
d24e64f
@@ -766,7 +773,7 @@ struct NewWindow *newwin;
d24e64f
 #ifdef BUILTIN_TELNET
d24e64f
   if (type == W_TYPE_TELNET)
d24e64f
     {
d24e64f
-      if (TelConnect(p))
d24e64f
+      if (TelOpenAndConnect(p))
d24e64f
 	{
d24e64f
 	  FreeWindow(p);
d24e64f
 	  return -1;
d24e64f
@@ -878,6 +885,13 @@ struct win *p;
d24e64f
   int lflag, f;
d24e64f
 
d24e64f
   lflag = nwin_default.lflag;
d24e64f
+#ifdef BUILTIN_TELNET
d24e64f
+  if(!strcmp(p->w_cmdargs[0], "//telnet")) {
d24e64f
+         p->w_type = W_TYPE_TELNET;
d24e64f
+         TtyName = "telnet";
d24e64f
+  }
d24e64f
+  else
d24e64f
+#endif
d24e64f
   if ((f = OpenDevice(p->w_cmdargs, lflag, &p->w_type, &TtyName)) < 0)
d24e64f
     return -1;
d24e64f
 
d24e64f
@@ -909,7 +923,7 @@ struct win *p;
d24e64f
 #ifdef BUILTIN_TELNET
d24e64f
   if (p->w_type == W_TYPE_TELNET)
d24e64f
     {
d24e64f
-      if (TelConnect(p))
d24e64f
+      if (TelOpenAndConnect(p))
d24e64f
         return -1;
d24e64f
     }
d24e64f
   else
d24e64f
@@ -1068,16 +1082,6 @@ char **namep;
d24e64f
       *namep = "telnet";
d24e64f
       return 0;
d24e64f
     }
d24e64f
-#ifdef BUILTIN_TELNET
d24e64f
-  if (strcmp(arg, "//telnet") == 0)
d24e64f
-    {
d24e64f
-      f = TelOpen(args + 1);
d24e64f
-      lflag = 0;
d24e64f
-      *typep = W_TYPE_TELNET;
d24e64f
-      *namep = "telnet";
d24e64f
-    }
d24e64f
-  else
d24e64f
-#endif
d24e64f
   if (strncmp(arg, "//", 2) == 0)
d24e64f
     {
d24e64f
       Msg(0, "Invalid argument '%s'", arg);
d24e64f
diff -up screen/src/window.h.ipv6 screen/src/window.h
d24e64f
--- screen/src/window.h.ipv6	2010-11-11 03:47:46.000000000 +0100
d24e64f
+++ screen/src/window.h	2011-02-03 16:04:35.989744687 +0100
d24e64f
@@ -268,7 +268,7 @@ struct win
d24e64f
   struct display *w_zdisplay;
d24e64f
 #endif
d24e64f
 #ifdef BUILTIN_TELNET
d24e64f
-  struct sockaddr_in w_telsa;
d24e64f
+  struct sockaddr_storage w_telsa;
d24e64f
   char   w_telbuf[IOSIZE];
d24e64f
   int    w_telbufl;
d24e64f
   char   w_telmopts[256];