Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Kratochvil <jan.kratochvil@redhat.com>
To: Daniel Jacobowitz <drow@false.org>
Cc: gdb-patches@sourceware.org
Subject: Re: [patch] IPv6 support for gdbserver
Date: Sat, 30 Sep 2006 15:28:00 -0000	[thread overview]
Message-ID: <20060930152757.GA27372@host0.dyn.jankratochvil.net> (raw)
In-Reply-To: <20060927190611.GA7326@nevyn.them.org>

[-- Attachment #1: Type: text/plain, Size: 676 bytes --]

Hi,

sanitized version here.


On Wed, 27 Sep 2006 21:06:11 +0200, Daniel Jacobowitz wrote:
> On Wed, Sep 27, 2006 at 08:55:47PM +0200, Jan Kratochvil wrote:
> > On Wed, 27 Sep 2006 20:20:38 +0200, Daniel Jacobowitz wrote:
> > ...
> > > focused on the environment you're working in (Red Hat Linux).
> > ...
> > > I suspect that this would break GDB builds on a number of targets

This version detects getaddrinfo(3)/getnameinfo(3)/AF_INET6;
tested only by hand, not on a real system missing IPv6.

...
> The advantage of providing just an fd interface is that you can leave
> writing complicated networking code

This patch extends the gdbserver part for IPv6.


Regards,
Jan

[-- Attachment #2: gdb-cvs-IPv6.patch --]
[-- Type: text/plain, Size: 32608 bytes --]

[ gdb/configure and gdb/gdbserver/configure missing ]


gdb:
2006-09-30  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* ser-tcp.c (net_open): Support IPv6, "tcp6:"&co. notation.
	* configure.ac: Check for IPv6 getaddrinfo, getnameinfo and AF_INET6.
	* configure, config.in: Regenerate.

gdb/gdbserver:
2006-09-30  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdbreplay.c (remote_open): Support IPv6, "tcp6:"&co. notation.
	* remote-utils.c (remote_open): Likewise.
	* configure.ac: Check for IPv6 getaddrinfo, getnameinfo and AF_INET6.
	* configure, config.in: Regenerate.

gdb/doc:
2006-09-30  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.texinfo (Connecting to a remote target): Support IPv6,
	"tcp6:"&co. notation.
	(Using the gdbserver program): Likewise.


Index: gdb/config.in
===================================================================
--- gdb/config.in	8 Aug 2006 20:32:15 -0000	1.84
+++ gdb/config.in	30 Sep 2006 15:13:04 -0000
@@ -67,6 +67,10 @@
 /* Define to 1 if you have the <curses.h> header file. */
 #undef HAVE_CURSES_H
 
+/* Define to 1 if you have the declaration of `AF_INET6', and to 0 if you
+   don't. */
+#undef HAVE_DECL_AF_INET6
+
 /* Define to 1 if you have the declaration of `free', and to 0 if you don't.
    */
 #undef HAVE_DECL_FREE
@@ -113,9 +117,15 @@
 /* Define if <sys/procfs.h> has fpregset_t. */
 #undef HAVE_FPREGSET_T
 
+/* Define to 1 if you have the `getaddrinfo' function. */
+#undef HAVE_GETADDRINFO
+
 /* Define to 1 if you have the `getgid' function. */
 #undef HAVE_GETGID
 
+/* Define to 1 if you have the `getnameinfo' function. */
+#undef HAVE_GETNAMEINFO
+
 /* Define to 1 if you have the `getpagesize' function. */
 #undef HAVE_GETPAGESIZE
 
Index: gdb/configure.ac
===================================================================
--- gdb/configure.ac	8 Aug 2006 20:26:23 -0000	1.34
+++ gdb/configure.ac	30 Sep 2006 15:13:08 -0000
@@ -446,6 +446,12 @@ AC_CHECK_FUNCS(socketpair)
 AC_CHECK_FUNCS(syscall)
 AC_CHECK_FUNCS(ttrace)
 AC_CHECK_FUNCS(wborder)
+AC_CHECK_FUNCS(getaddrinfo)
+AC_CHECK_FUNCS(getnameinfo)
+AC_CHECK_DECLS(AF_INET6, [], [],
+[#include <sys/types.h>
+#include <sys/socket.h>
+])
 
 # Check the return and argument types of ptrace.  No canned test for
 # this, so roll our own.
Index: gdb/ser-tcp.c
===================================================================
--- gdb/ser-tcp.c	10 Feb 2006 22:01:43 -0000	1.26
+++ gdb/ser-tcp.c	30 Sep 2006 15:13:08 -0000
@@ -68,67 +68,142 @@ void _initialize_ser_tcp (void);
 int
 net_open (struct serial *scb, const char *name)
 {
-  char *port_str, hostname[100];
-  int n, port, tmp;
-  int use_udp;
-  struct hostent *hostent;
-  struct sockaddr_in sockaddr;
+  char *name_base;
+  char *port_str;
+  int n, tmp;
 #ifdef USE_WIN32API
   u_long ioarg;
 #else
   int ioarg;
 #endif
-
-  use_udp = 0;
-  if (strncmp (name, "udp:", 4) == 0)
+  struct prefix
+    {
+      const char *string;
+      int family;
+      int socktype;
+    };
+  const struct prefix prefixes[] =
     {
-      use_udp = 1;
-      name = name + 4;
+      { "udp:",  AF_UNSPEC, SOCK_DGRAM  },
+      { "tcp:",  AF_UNSPEC, SOCK_STREAM },
+      { "udp4:", AF_INET,   SOCK_DGRAM  },
+      { "tcp4:", AF_INET,   SOCK_STREAM },
+/* We do not support `AF_INET6' without getaddrinfo(3).  */
+#if defined (HAVE_GETADDRINFO) && HAVE_DECL_AF_INET6
+      { "udp6:", AF_INET6,  SOCK_DGRAM  },
+      { "tcp6:", AF_INET6,  SOCK_STREAM },
+#endif  /* defined (HAVE_GETADDRINFO) && HAVE_DECL_AF_INET6 */
+    };
+  const struct prefix *prefix;
+#ifdef HAVE_GETADDRINFO
+  struct addrinfo hints;
+  struct addrinfo *addrinfo_base, *addrinfo = NULL;
+#else  /* !HAVE_GETADDRINFO */
+  struct hostent *hostent;
+  struct sockaddr_in sockaddr;
+#endif  /* !HAVE_GETADDRINFO */
+  /* Error by default.  */
+  int retval = -1;
+
+  name_base = xstrdup (name);
+  name = name_base;
+#ifdef HAVE_GETADDRINFO
+  memset (&hints, 0, sizeof (hints));
+  hints.ai_family = AF_UNSPEC;
+  hints.ai_flags = AI_ADDRCONFIG;
+#endif  /* HAVE_GETADDRINFO */
+  for (prefix = prefixes; prefix < prefixes + ARRAY_SIZE (prefixes); prefix++)
+    if (strncmp (name, prefix->string, strlen (prefix->string)) == 0)
+      {
+	name += strlen (prefix->string);
+#ifdef HAVE_GETADDRINFO
+	hints.ai_family   = prefix->family;
+	hints.ai_socktype = prefix->socktype;
+#endif  /* HAVE_GETADDRINFO */
+	break;
+      }
+  if (prefix >= prefixes + ARRAY_SIZE (prefixes))
+    prefix = NULL;
+  if ((prefix == NULL || prefix->family != AF_INET)
+      && name[0] == '[' && (port_str = strchr (name, ']')))
+    {
+      name++;
+      *port_str++ = 0;
+    }
+  else
+    port_str = strchr (name, ':');
+  /* It may happen with IPv6 for like "[::1]".  */
+  if (port_str == NULL || *port_str != ':')
+    error (_("net_open: No colon in host name!"));
+  *port_str++ = 0;
+
+  /* Default hostname for `node == NULL' is localhost
+     as we did not specify `hints.ai_flags & AI_PASSIVE'.  */
+  if (name[0] == 0)
+    name = NULL;
+
+#ifdef HAVE_GETADDRINFO
+  n = getaddrinfo (name, port_str, &hints, &addrinfo_base);
+  if (n != 0)
+    {
+      fprintf_unfiltered (gdb_stderr, "%s:%s: cannot resolve name: %s\n",
+			  (name != NULL ? name : "<local>"),
+			  port_str, gai_strerror (n));
+      errno = ENOENT;
+      free (name_base);
+      return -1;
     }
-  else if (strncmp (name, "tcp:", 4) == 0)
-    name = name + 4;
-
-  port_str = strchr (name, ':');
-
-  if (!port_str)
-    error (_("net_open: No colon in host name!"));	   /* Shouldn't ever happen */
-
-  tmp = min (port_str - name, (int) sizeof hostname - 1);
-  strncpy (hostname, name, tmp);	/* Don't want colon */
-  hostname[tmp] = '\000';	/* Tie off host name */
-  port = atoi (port_str + 1);
 
-  /* default hostname is localhost */
-  if (!hostname[0])
-    strcpy (hostname, "localhost");
+  /* Still used for `port_str' above.  */
+  free (name_base);
 
-  hostent = gethostbyname (hostname);
-  if (!hostent)
+  for (addrinfo = addrinfo_base; addrinfo != NULL; addrinfo = addrinfo->ai_next)
+    {
+      scb->fd = socket (addrinfo->ai_family, addrinfo->ai_socktype,
+			addrinfo->ai_protocol);
+      if (scb->fd >= 0)
+	break;
+    }
+  if (addrinfo == NULL)
     {
-      fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
+      freeaddrinfo (addrinfo_base);
+      return -1;
+    }
+#else  /* !HAVE_GETADDRINFO */
+  hostent = gethostbyname ((name != NULL ? name : "localhost"));
+  if (hostent == NULL)
+    {
+      fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", name);
       errno = ENOENT;
+      free (name_base);
       return -1;
     }
 
-  if (use_udp)
-    scb->fd = socket (PF_INET, SOCK_DGRAM, 0);
-  else
-    scb->fd = socket (PF_INET, SOCK_STREAM, 0);
+  sockaddr.sin_family = PF_INET;
+  sockaddr.sin_port = htons (atoi (port_str));
+  memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
+	 sizeof (struct in_addr));
 
+  /* Still used for `port_str' above.  */
+  free (name_base);
+
+  scb->fd = socket (sockaddr.sin_family,
+		    (prefix != NULL ? prefix->socktype : SOCK_STREAM),
+		    0);
   if (scb->fd < 0)
     return -1;
-  
-  sockaddr.sin_family = PF_INET;
-  sockaddr.sin_port = htons (port);
-  memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
-	  sizeof (struct in_addr));
+#endif  /* !HAVE_GETADDRINFO */
 
   /* set socket nonblocking */
   ioarg = 1;
   ioctl (scb->fd, FIONBIO, &ioarg);
 
   /* Use Non-blocking connect.  connect() will return 0 if connected already. */
+#ifdef HAVE_GETADDRINFO
+  n = connect (scb->fd, addrinfo->ai_addr, addrinfo->ai_addrlen);
+#else  /* !HAVE_GETADDRINFO */
   n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
+#endif  /* !HAVE_GETADDRINFO */
 
   if (n < 0
 #ifdef USE_WIN32API
@@ -143,8 +218,7 @@ net_open (struct serial *scb, const char
 #ifdef USE_WIN32API
       errno = WSAGetLastError();
 #endif
-      net_close (scb);
-      return -1;
+      goto cleanup_scb;
     }
 
   if (n)
@@ -165,8 +239,7 @@ net_open (struct serial *scb, const char
 	      if (deprecated_ui_loop_hook (0))
 		{
 		  errno = EINTR;
-		  net_close (scb);
-		  return -1;
+		  goto cleanup_scb;
 		}
 	    }
 	  
@@ -192,8 +265,7 @@ net_open (struct serial *scb, const char
 	{
 	  if (polls > TIMEOUT * POLL_INTERVAL)
 	    errno = ETIMEDOUT;
-	  net_close (scb);
-	  return -1;
+	  goto cleanup_scb;
 	}
     }
 
@@ -211,8 +283,7 @@ net_open (struct serial *scb, const char
       {
 	if (err)
 	  errno = err;
-	net_close (scb);
-	return -1;
+	goto cleanup_scb;
       }
   } 
 
@@ -220,7 +291,7 @@ net_open (struct serial *scb, const char
   ioarg = 0;
   ioctl (scb->fd, FIONBIO, &ioarg);
 
-  if (use_udp == 0)
+  if (prefix == NULL || prefix->socktype == SOCK_STREAM)
     {
       /* Disable Nagle algorithm. Needed in some cases. */
       tmp = 1;
@@ -234,7 +305,16 @@ net_open (struct serial *scb, const char
   signal (SIGPIPE, SIG_IGN);
 #endif
 
-  return 0;
+  retval = 0;
+  goto cleanup_addrinfo_base;
+
+cleanup_scb:
+  net_close (scb);
+cleanup_addrinfo_base:
+#ifdef HAVE_GETADDRINFO
+  freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
+  return retval;
 }
 
 void
Index: gdb/doc/gdb.texinfo
===================================================================
--- gdb/doc/gdb.texinfo	21 Sep 2006 14:01:12 -0000	1.355
+++ gdb/doc/gdb.texinfo	30 Sep 2006 15:13:19 -0000
@@ -12404,8 +12404,10 @@ If you're using a serial line, you may w
 (@pxref{Remote configuration, set remotebaud}) before the
 @code{target} command.
 
-@item target remote @code{@var{host}:@var{port}}
+@item  target remote @code{@var{host}:@var{port}}
 @itemx target remote @code{tcp:@var{host}:@var{port}}
+@itemx target remote @code{tcp4:@var{host}:@var{port}}
+@itemx target remote @code{tcp6:@var{host}:@var{port}}
 @cindex @acronym{TCP} port, @code{target remote}
 Debug using a @acronym{TCP} connection to @var{port} on @var{host}.
 The @var{host} may be either a host name or a numeric @acronym{IP}
@@ -12414,6 +12416,9 @@ the target machine itself, if it is dire
 it might be a terminal server which in turn has a serial line to the
 target.
 
+@code{tcp6:} prefix forces IPv6 network connection while @code{tcp4:} forces
+IPv4, both on the reliable stream TCP connection.
+
 For example, to connect to port 2828 on a terminal server named
 @code{manyfarms}:
 
@@ -12434,10 +12439,15 @@ target remote :1234
 Note that the colon is still required here.
 
 @item target remote @code{udp:@var{host}:@var{port}}
+@itemx target remote @code{udp6:@var{host}:@var{port}}
+@itemx target remote @code{udp4:@var{host}:@var{port}}
 @cindex @acronym{UDP} port, @code{target remote}
 Debug using @acronym{UDP} packets to @var{port} on @var{host}.  For example, to
 connect to @acronym{UDP} port 2828 on a terminal server named @code{manyfarms}:
 
+@code{udp6:} prefix forces IPv6 network connection while @code{udp4:} forces
+IPv4, both on the unreliable datagram UDP connection.
+
 @smallexample
 target remote udp:manyfarms:2828
 @end smallexample
@@ -12578,14 +12588,30 @@ The only difference from the previous ex
 specifying that you are communicating with the host @value{GDBN} via
 TCP.  The @samp{host:2345} argument means that @code{gdbserver} is to
 expect a TCP connection from machine @samp{host} to local TCP port 2345.
-(Currently, the @samp{host} part is ignored.)  You can choose any number
-you want for the port number as long as it does not conflict with any
-TCP ports already in use on the target system (for example, @code{23} is
-reserved for @code{telnet}).@footnote{If you choose a port number that
-conflicts with another service, @code{gdbserver} prints an error message
+(The @samp{host} part is usually omitted as it defaults to listen from any
+host. You may use a local address as expected by the @code{bind} syscall.)
+You can choose any number you want for the port number as long as it does not
+conflict with any TCP ports already in use on the target system (for example,
+@code{23} is reserved for @code{telnet}).@footnote{If you choose a port number
+that conflicts with another service, @code{gdbserver} prints an error message
 and exits.}  You must use the same port number with the host @value{GDBN}
 @code{target remote} command.
 
+@item  gdbserver @code{tcp:@var{host}:@var{port}} emacs foo.txt
+@itemx gdbserver @code{tcp6:@var{host}:@var{port}} emacs foo.txt
+@itemx gdbserver @code{tcp4:@var{host}:@var{port}} emacs foo.txt
+@itemx gdbserver @code{udp:@var{host}:@var{port}} emacs foo.txt
+@itemx gdbserver @code{udp6:@var{host}:@var{port}} emacs foo.txt
+@itemx gdbserver @code{udp4:@var{host}:@var{port}} emacs foo.txt
+
+The @code{::} part can be also replaced by the optional @var{host} part as
+in the sample case of @code{tcp:@var{host}:@var{port}}.
+These all alternative syntaxes force either the reliable stream TCP protocol or
+the unreliable datagram UDP protocol appropriately.  You may also force the use
+of IPv6 or IPv4 network connections; @code{tcp:} and @code{udp:} select the
+network version type according to the provided @var{host}.
+The connection type defaults to the @code{tcp:} prefix behavior.
+
 On some targets, @code{gdbserver} can also attach to running programs.
 This is accomplished via the @code{--attach} argument.  The syntax is:
 
Index: gdb/gdbserver/config.in
===================================================================
--- gdb/gdbserver/config.in	23 Jul 2006 03:52:15 -0000	1.14
+++ gdb/gdbserver/config.in	30 Sep 2006 15:13:19 -0000
@@ -3,6 +3,10 @@
 /* Define to 1 if you have the <arpa/inet.h> header file. */
 #undef HAVE_ARPA_INET_H
 
+/* Define to 1 if you have the declaration of `AF_INET6', and to 0 if you
+   don't. */
+#undef HAVE_DECL_AF_INET6
+
 /* Define to 1 if you have the declaration of `strerror', and to 0 if you
    don't. */
 #undef HAVE_DECL_STRERROR
@@ -10,6 +14,12 @@
 /* Define if <sys/procfs.h> has elf_fpregset_t. */
 #undef HAVE_ELF_FPREGSET_T
 
+/* Define to 1 if you have the `getaddrinfo' function. */
+#undef HAVE_GETADDRINFO
+
+/* Define to 1 if you have the `getnameinfo' function. */
+#undef HAVE_GETNAMEINFO
+
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 
Index: gdb/gdbserver/configure.ac
===================================================================
--- gdb/gdbserver/configure.ac	23 Jul 2006 03:52:15 -0000	1.10
+++ gdb/gdbserver/configure.ac	30 Sep 2006 15:13:21 -0000
@@ -49,6 +49,12 @@ AC_CHECK_TYPES(socklen_t, [], [],
 #include <sys/socket.h>
 ])
 . ${srcdir}/configure.srv
+AC_CHECK_FUNCS(getaddrinfo)
+AC_CHECK_FUNCS(getnameinfo)
+AC_CHECK_DECLS(AF_INET6, [], [],
+[#include <sys/types.h>
+#include <sys/socket.h>
+])
 
 if test "${srv_mingw}" = "yes"; then
   LIBS="$LIBS -lwsock32"
Index: gdb/gdbserver/gdbreplay.c
===================================================================
--- gdb/gdbserver/gdbreplay.c	23 Jul 2006 03:52:15 -0000	1.12
+++ gdb/gdbserver/gdbreplay.c	30 Sep 2006 15:13:21 -0000
@@ -115,6 +115,8 @@ remote_close (void)
 static void
 remote_open (char *name)
 {
+  char *name_orig = name;
+
   if (!strchr (name, ':'))
     {
       fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
@@ -127,14 +129,81 @@ remote_open (char *name)
       static int winsock_initialized;
 #endif
       char *port_str;
-      int port;
-      struct sockaddr_in sockaddr;
       socklen_t tmp;
-      int tmp_desc;
-
-      port_str = strchr (name, ':');
+      int tmp_desc = -1;
+      struct prefix
+	{
+	  const char *string;
+	  int family;
+	  int socktype;
+	};
+      const struct prefix prefixes[] =
+	{
+	  { "udp:",  AF_UNSPEC, SOCK_DGRAM  },
+	  { "tcp:",  AF_UNSPEC, SOCK_STREAM },
+	  { "udp4:", AF_INET,   SOCK_DGRAM  },
+	  { "tcp4:", AF_INET,   SOCK_STREAM },
+/* We do not support `AF_INET6' without getaddrinfo(3).  */
+#if defined (HAVE_GETADDRINFO) && HAVE_DECL_AF_INET6
+	  { "udp6:", AF_INET6,  SOCK_DGRAM  },
+	  { "tcp6:", AF_INET6,  SOCK_STREAM },
+#endif  /* defined (HAVE_GETADDRINFO) && HAVE_DECL_AF_INET6 */
+	};
+      const struct prefix *prefix;
+#ifdef HAVE_GETADDRINFO
+      struct addrinfo hints;
+      struct addrinfo *addrinfo_base, *addrinfo = NULL;
+      int err;
+#else  /* !HAVE_GETADDRINFO */
+      struct sockaddr_in sockaddr;
+#endif  /* !HAVE_GETADDRINFO */
+#ifdef HAVE_GETNAMEINFO
+      char back_host[64], back_port[16];
+#endif  /* HAVE_GETNAMEINFO */
+      char *name_base;
+
+      name_base = strdup (name);
+      name = name_base;
+#ifdef HAVE_GETADDRINFO
+      memset (&hints, 0, sizeof (hints));
+      hints.ai_family = AF_UNSPEC;
+      hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
+#endif  /* HAVE_GETADDRINFO */
+      for (prefix = prefixes;
+           prefix < prefixes + sizeof (prefixes) / sizeof (*prefixes);
+	   prefix++)
+	if (strncmp (name, prefix->string, strlen (prefix->string)) == 0)
+	  {
+	    name += strlen (prefix->string);
+#ifdef HAVE_GETADDRINFO
+	    hints.ai_family   = prefix->family;
+	    hints.ai_socktype = prefix->socktype;
+#endif  /* HAVE_GETADDRINFO */
+	    break;
+	  }
+      if (prefix >= prefixes + sizeof (prefixes) / sizeof (*prefixes))
+	prefix = NULL;
+      if ((prefix == NULL || prefix->family != AF_INET)
+	  && name[0] == '[' && (port_str = strchr (name, ']')))
+	{
+	  name++;
+	  *port_str++ = 0;
+	}
+      else
+	port_str = strchr (name, ':');
+      /* It may happen with IPv6 for like "[::1]".  */
+      if (port_str == NULL || *port_str != ':')
+        {
+	  fprintf (stderr, "net_open: No colon in host name!\n");
+	  fflush (stderr);
+	  exit (1);
+	}
+      *port_str++ = 0;
 
-      port = atoi (port_str + 1);
+      /* Default hostname for `node == NULL' is `INADDR_ANY'/`in6addr_any'.
+	 as we did specify `hints.ai_flags & AI_PASSIVE'.  */
+      if (name[0] == 0)
+	name = NULL;
 
 #ifdef USE_WIN32API
       if (!winsock_initialized)
@@ -146,55 +215,162 @@ remote_open (char *name)
 	}
 #endif
 
-      tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
+#ifdef HAVE_GETADDRINFO
+      err = getaddrinfo (name, port_str, &hints, &addrinfo_base);
+      if (err != 0)
+	{
+	  /* `name_base' is used here for `port_str'.  */
+	  fprintf (stderr, "%s:%s: cannot resolve name: %s\n",
+		   name, port_str, gai_strerror (err));
+	  fflush (stderr);
+	  exit (1);
+	}
+
+      for (addrinfo = addrinfo_base;
+	   addrinfo != NULL;
+	   addrinfo = addrinfo->ai_next)
+	{
+	  tmp_desc = socket (addrinfo->ai_family, addrinfo->ai_socktype,
+			    addrinfo->ai_protocol);
+	  if (tmp_desc >= 0)
+	    break;
+	}
+      if (addrinfo == NULL)
+	{
+	  freeaddrinfo (addrinfo_base);
+	  perror_with_name ("Can't open socket");
+	}
+#else  /* !HAVE_GETADDRINFO */
+      sockaddr.sin_family = PF_INET;
+      sockaddr.sin_port = htons (atoi (port_str));
+
+      if (name == NULL)
+	sockaddr.sin_addr.s_addr = INADDR_ANY;
+      else
+        {
+	  struct hostent *hostent;
+
+	  hostent = gethostbyname (name);
+	  if (hostent == NULL)
+	    {
+	      fprintf (stderr, "%s: unknown host\n", name);
+	      free (name_base);
+	      perror_with_name ("gethostbyname(3) resolving");
+	    }
+
+	  memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
+		  sizeof (struct in_addr));
+	  }
+
+      /* We assume matching `AF_*' and `PF_*'.  */
+      tmp_desc = socket (sockaddr.sin_family,
+			(prefix != NULL ? prefix->socktype : SOCK_STREAM),
+			0);
       if (tmp_desc < 0)
-	perror_with_name ("Can't open socket");
+        {
+	  free (name_base);
+	  perror_with_name ("Can't open socket");
+        }
+#endif  /* !HAVE_GETADDRINFO */
 
       /* Allow rapid reuse of this port. */
       tmp = 1;
       setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
 		  sizeof (tmp));
 
-      sockaddr.sin_family = PF_INET;
-      sockaddr.sin_port = htons (port);
-      sockaddr.sin_addr.s_addr = INADDR_ANY;
-
+#ifdef HAVE_GETADDRINFO
+      if (bind (tmp_desc, addrinfo->ai_addr, addrinfo->ai_addrlen)
+	  || ((prefix == NULL || prefix->socktype != SOCK_DGRAM)
+	      && listen (tmp_desc, 1)))
+#else  /* !HAVE_GETADDRINFO */
       if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
-	  || listen (tmp_desc, 1))
-	perror_with_name ("Can't bind address");
-
-      tmp = sizeof (sockaddr);
-      remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
-      if (remote_desc == -1)
-	perror_with_name ("Accept failed");
-
-      /* Enable TCP keep alive process. */
-      tmp = 1;
-      setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
+	  || ((prefix == NULL || prefix->socktype != SOCK_DGRAM)
+	      && listen (tmp_desc, 1)))
+#endif  /* !HAVE_GETADDRINFO */
+        {
+#ifdef HAVE_GETADDRINFO
+	  freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
+	  free (name_base);
+	  perror_with_name ("Can't bind address");
+        }
+
+#ifdef HAVE_GETNAMEINFO
+      if (0 == getnameinfo (addrinfo->ai_addr, addrinfo->ai_addrlen,
+			    back_host, sizeof (back_host),
+			    back_port, sizeof (back_port),
+			    NI_NUMERICHOST | NI_NUMERICSERV))
+	fprintf (stderr, "Listening on port %s (on host %s)\n",
+		 back_port, back_host);
+      else
+#endif  /* !HAVE_GETNAMEINFO */
+	fprintf (stderr, "Listening on port %s (on host %s)\n",
+		 port_str, (name != NULL ? name : "<local>"));
+      fflush (stderr);
 
-      /* Tell TCP not to delay small packets.  This greatly speeds up
-         interactive response. */
-      tmp = 1;
-      setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
-		  (char *) &tmp, sizeof (tmp));
+      /* Used for `port_str' above.  */
+      free (name_base);
 
-      close (tmp_desc);		/* No longer need this */
+      if (prefix != NULL && prefix->socktype == SOCK_DGRAM)
+        remote_desc = tmp_desc;
+      else
+        {
+	  struct sockaddr_storage sockaddr;
+	  socklen_t sockaddr_len = sizeof (sockaddr);
+	  remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &sockaddr_len);
+	  if (remote_desc == -1)
+	    {
+#ifdef HAVE_GETADDRINFO
+	      freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
+	      perror_with_name ("Accept failed");
+	    }
+
+	  /* Enable TCP keep alive process. */
+	  tmp = 1;
+	  setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
+
+	  /* Tell TCP not to delay small packets.  This greatly speeds up
+	     interactive response. */
+	  if (prefix == NULL || prefix->socktype == SOCK_STREAM)
+	    {
+	      tmp = 1;
+	      setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
+			  (char *) &tmp, sizeof (tmp));
+	    }
+#ifdef HAVE_GETADDRINFO
+	  freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
 
 #ifndef USE_WIN32API
-      close (tmp_desc);		/* No longer need this */
+	  close (tmp_desc);		/* No longer need this */
 
-      signal (SIGPIPE, SIG_IGN);	/* If we don't do this, then gdbreplay simply
-					   exits when the remote side dies.  */
+	  signal (SIGPIPE, SIG_IGN);	/* If we don't do this, then gdbserver simply
+					       exits when the remote side dies.  */
 #else
-      closesocket (tmp_desc);	/* No longer need this */
+	  closesocket (tmp_desc);	/* No longer need this */
 #endif
+
+#ifdef HAVE_GETNAMEINFO
+	  /* Convert IP address to string.  */
+	  if (0 == getnameinfo ((struct sockaddr *) &sockaddr, sockaddr_len,
+				back_host, sizeof (back_host),
+				back_port, sizeof (back_port),
+				NI_NUMERICHOST | NI_NUMERICSERV))
+	    fprintf (stderr, "Remote debugging from host %s port %s\n",
+		     back_host, back_port);
+	  else
+#endif  /* !HAVE_GETNAMEINFO */
+	    fprintf (stderr, "Remote debugging started\n");
+	}
+      fflush (stderr);
     }
 
-#if defined(F_SETFL) && defined (FASYNC)
+#if defined (F_SETFL) && defined (FASYNC)
   fcntl (remote_desc, F_SETFL, FASYNC);
 #endif
 
-  fprintf (stderr, "Replay logfile using %s\n", name);
+  fprintf (stderr, "Replay logfile using %s\n", name_orig);
   fflush (stderr);
 }
 
Index: gdb/gdbserver/remote-utils.c
===================================================================
--- gdb/gdbserver/remote-utils.c	21 Sep 2006 16:09:54 -0000	1.32
+++ gdb/gdbserver/remote-utils.c	30 Sep 2006 15:13:21 -0000
@@ -157,14 +157,77 @@ remote_open (char *name)
       static int winsock_initialized;
 #endif
       char *port_str;
-      int port;
-      struct sockaddr_in sockaddr;
       socklen_t tmp;
-      int tmp_desc;
-
-      port_str = strchr (name, ':');
-
-      port = atoi (port_str + 1);
+      int tmp_desc = -1;
+      struct prefix
+	{
+	  const char *string;
+	  int family;
+	  int socktype;
+	};
+      const struct prefix prefixes[] =
+	{
+	  { "udp:",  AF_UNSPEC, SOCK_DGRAM  },
+	  { "tcp:",  AF_UNSPEC, SOCK_STREAM },
+	  { "udp4:", AF_INET,   SOCK_DGRAM  },
+	  { "tcp4:", AF_INET,   SOCK_STREAM },
+/* We do not support `AF_INET6' without getaddrinfo(3).  */
+#if defined (HAVE_GETADDRINFO) && HAVE_DECL_AF_INET6
+	  { "udp6:", AF_INET6,  SOCK_DGRAM  },
+	  { "tcp6:", AF_INET6,  SOCK_STREAM },
+#endif  /* defined (HAVE_GETADDRINFO) && HAVE_DECL_AF_INET6 */
+	};
+      const struct prefix *prefix;
+#ifdef HAVE_GETADDRINFO
+      struct addrinfo hints;
+      struct addrinfo *addrinfo_base, *addrinfo = NULL;
+      int err;
+#else  /* !HAVE_GETADDRINFO */
+      struct sockaddr_in sockaddr;
+#endif  /* !HAVE_GETADDRINFO */
+#ifdef HAVE_GETNAMEINFO
+      char back_host[64], back_port[16];
+#endif  /* HAVE_GETNAMEINFO */
+      char *name_base;
+
+      name_base = strdup (name);
+      name = name_base;
+#ifdef HAVE_GETADDRINFO
+      memset (&hints, 0, sizeof (hints));
+      hints.ai_family = AF_UNSPEC;
+      hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
+#endif  /* HAVE_GETADDRINFO */
+      for (prefix = prefixes;
+           prefix < prefixes + sizeof (prefixes) / sizeof (*prefixes);
+	   prefix++)
+	if (strncmp (name, prefix->string, strlen (prefix->string)) == 0)
+	  {
+	    name += strlen (prefix->string);
+#ifdef HAVE_GETADDRINFO
+	    hints.ai_family   = prefix->family;
+	    hints.ai_socktype = prefix->socktype;
+#endif  /* HAVE_GETADDRINFO */
+	    break;
+	  }
+      if (prefix >= prefixes + sizeof (prefixes) / sizeof (*prefixes))
+	prefix = NULL;
+      if ((prefix == NULL || prefix->family != AF_INET)
+	  && name[0] == '[' && (port_str = strchr (name, ']')))
+	{
+	  name++;
+	  *port_str++ = 0;
+	}
+      else
+	port_str = strchr (name, ':');
+      /* It may happen with IPv6 for like "[::1]".  */
+      if (port_str == NULL || *port_str != ':')
+	error ("net_open: No colon in host name!");
+      *port_str++ = 0;
+
+      /* Default hostname for `node == NULL' is `INADDR_ANY'/`in6addr_any'.
+	 as we did specify `hints.ai_flags & AI_PASSIVE'.  */
+      if (name[0] == 0)
+	name = NULL;
 
 #ifdef USE_WIN32API
       if (!winsock_initialized)
@@ -176,54 +239,155 @@ remote_open (char *name)
 	}
 #endif
 
-      tmp_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
+#ifdef HAVE_GETADDRINFO
+      err = getaddrinfo (name, port_str, &hints, &addrinfo_base);
+      if (err != 0)
+	{
+	  /* `name_base' is used here for `port_str'.  */
+	  error ("%s:%s: cannot resolve name: %s\n",
+		   name, port_str, gai_strerror (err));
+	}
+
+      for (addrinfo = addrinfo_base;
+	   addrinfo != NULL;
+	   addrinfo = addrinfo->ai_next)
+	{
+	  tmp_desc = socket (addrinfo->ai_family, addrinfo->ai_socktype,
+			    addrinfo->ai_protocol);
+	  if (tmp_desc >= 0)
+	    break;
+	}
+      if (addrinfo == NULL)
+	{
+	  freeaddrinfo (addrinfo_base);
+	  free (name_base);
+	  perror_with_name ("Can't open socket");
+	}
+#else  /* !HAVE_GETADDRINFO */
+      sockaddr.sin_family = PF_INET;
+      sockaddr.sin_port = htons (atoi (port_str));
+
+      if (name == NULL)
+	sockaddr.sin_addr.s_addr = INADDR_ANY;
+      else
+        {
+	  struct hostent *hostent;
+
+	  hostent = gethostbyname (name);
+	  if (hostent == NULL)
+	    {
+	      fprintf (stderr, "%s: unknown host\n", name);
+	      fflush (stderr);
+	      free (name_base);
+	      perror_with_name ("gethostbyname(3) resolving");
+	    }
+
+	  memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
+		  sizeof (struct in_addr));
+	  }
+
+      /* We assume matching `AF_*' and `PF_*'.  */
+      tmp_desc = socket (sockaddr.sin_family,
+			(prefix != NULL ? prefix->socktype : SOCK_STREAM),
+			0);
       if (tmp_desc < 0)
-	perror_with_name ("Can't open socket");
+        {
+	  free (name_base);
+	  perror_with_name ("Can't open socket");
+        }
+#endif  /* !HAVE_GETADDRINFO */
 
       /* Allow rapid reuse of this port. */
       tmp = 1;
       setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
 		  sizeof (tmp));
 
-      sockaddr.sin_family = PF_INET;
-      sockaddr.sin_port = htons (port);
-      sockaddr.sin_addr.s_addr = INADDR_ANY;
-
+#ifdef HAVE_GETADDRINFO
+      if (bind (tmp_desc, addrinfo->ai_addr, addrinfo->ai_addrlen)
+	  || ((prefix == NULL || prefix->socktype != SOCK_DGRAM)
+	      && listen (tmp_desc, 1)))
+#else  /* !HAVE_GETADDRINFO */
       if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
-	  || listen (tmp_desc, 1))
-	perror_with_name ("Can't bind address");
+	  || ((prefix == NULL || prefix->socktype != SOCK_DGRAM)
+	      && listen (tmp_desc, 1)))
+#endif  /* !HAVE_GETADDRINFO */
+        {
+#ifdef HAVE_GETADDRINFO
+	  freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
+	  free (name_base);
+	  perror_with_name ("Can't bind address");
+        }
 
-      fprintf (stderr, "Listening on port %d\n", port);
+#ifdef HAVE_GETNAMEINFO
+      if (0 == getnameinfo (addrinfo->ai_addr, addrinfo->ai_addrlen,
+			    back_host, sizeof (back_host),
+			    back_port, sizeof (back_port),
+			    NI_NUMERICHOST | NI_NUMERICSERV))
+	fprintf (stderr, "Listening on port %s (on host %s)\n",
+		 back_port, back_host);
+      else
+#endif  /* !HAVE_GETNAMEINFO */
+	fprintf (stderr, "Listening on port %s (on host %s)\n",
+		 port_str, (name != NULL ? name : "<local>"));
       fflush (stderr);
 
-      tmp = sizeof (sockaddr);
-      remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
-      if (remote_desc == -1)
-	perror_with_name ("Accept failed");
+      /* Used for `port_str' above.  */
+      free (name_base);
 
-      /* Enable TCP keep alive process. */
-      tmp = 1;
-      setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
-
-      /* Tell TCP not to delay small packets.  This greatly speeds up
-         interactive response. */
-      tmp = 1;
-      setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
-		  (char *) &tmp, sizeof (tmp));
+      if (prefix != NULL && prefix->socktype == SOCK_DGRAM)
+        remote_desc = tmp_desc;
+      else
+        {
+	  struct sockaddr_storage sockaddr;
+	  socklen_t sockaddr_len = sizeof (sockaddr);
+	  remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &sockaddr_len);
+	  if (remote_desc == -1)
+	    {
+#ifdef HAVE_GETADDRINFO
+	      freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
+	      perror_with_name ("Accept failed");
+	    }
 
+	  /* Enable TCP keep alive process. */
+	  tmp = 1;
+	  setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
+
+	  /* Tell TCP not to delay small packets.  This greatly speeds up
+	     interactive response. */
+	  if (prefix == NULL || prefix->socktype == SOCK_STREAM)
+	    {
+	      tmp = 1;
+	      setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
+			  (char *) &tmp, sizeof (tmp));
+	    }
+#ifdef HAVE_GETADDRINFO
+	  freeaddrinfo (addrinfo_base);
+#endif  /* HAVE_GETADDRINFO */
 
 #ifndef USE_WIN32API
-      close (tmp_desc);		/* No longer need this */
+	  close (tmp_desc);		/* No longer need this */
 
-      signal (SIGPIPE, SIG_IGN);	/* If we don't do this, then gdbserver simply
+	  signal (SIGPIPE, SIG_IGN);	/* If we don't do this, then gdbserver simply
 					   exits when the remote side dies.  */
 #else
-      closesocket (tmp_desc);	/* No longer need this */
+	  closesocket (tmp_desc);	/* No longer need this */
 #endif
 
-      /* Convert IP address to string.  */
-      fprintf (stderr, "Remote debugging from host %s\n", 
-         inet_ntoa (sockaddr.sin_addr));
+#ifdef HAVE_GETNAMEINFO
+	  /* Convert IP address to string.  */
+	  if (0 == getnameinfo ((struct sockaddr *) &sockaddr, sockaddr_len,
+				back_host, sizeof (back_host),
+				back_port, sizeof (back_port),
+				NI_NUMERICHOST | NI_NUMERICSERV))
+	    fprintf (stderr, "Remote debugging from host %s port %s\n",
+		     back_host, back_port);
+	  else
+#endif  /* !HAVE_GETNAMEINFO */
+	    fprintf (stderr, "Remote debugging started\n");
+	}
+      fflush (stderr);
     }
 
 #if defined(F_SETFL) && defined (FASYNC)

  reply	other threads:[~2006-09-30 15:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-27 16:33 Jan Kratochvil
2006-09-27 18:20 ` Daniel Jacobowitz
2006-09-27 18:56   ` Jan Kratochvil
2006-09-27 19:06     ` Daniel Jacobowitz
2006-09-30 15:28       ` Jan Kratochvil [this message]
2006-10-08 19:03         ` Jan Kratochvil
2006-10-09  4:33           ` Eli Zaretskii
2006-10-09 14:17             ` Jan Kratochvil
2006-10-09 19:01               ` Daniel Jacobowitz
2006-10-09 19:36               ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060930152757.GA27372@host0.dyn.jankratochvil.net \
    --to=jan.kratochvil@redhat.com \
    --cc=drow@false.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox