autoupdate
[gnulib.git] / lib / getaddrinfo.c
index 358214a..6a36bf0 100644 (file)
@@ -1,5 +1,5 @@
 /* Get address information (partial implementation).
-   Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006 Free Software
+   Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006, 2007, 2008 Free Software
    Foundation, Inc.
    Contributed by Simon Josefsson <simon@josefsson.org>.
 
 
 #include <config.h>
 
-#include "getaddrinfo.h"
+#include <netdb.h>
 
 #if HAVE_NETINET_IN_H
 # include <netinet/in.h>
 #endif
 
+/* Get inet_ntop.  */
+#include <arpa/inet.h>
+
 /* Get calloc. */
 #include <stdlib.h>
 
-/* Get memcpy. */
+/* Get memcpy, strdup. */
 #include <string.h>
 
+/* Get snprintf. */
+#include <stdio.h>
+
 #include <stdbool.h>
 
 #include "gettext.h"
 #define _(String) gettext (String)
 #define N_(String) String
 
-#include "inet_ntop.h"
-#include "intprops.h"
-#include "inttostr.h"
-#include "strdup.h"
-
 /* BeOS has AF_INET, but not PF_INET.  */
 #ifndef PF_INET
 # define PF_INET AF_INET
@@ -179,7 +180,7 @@ getaddrinfo (const char *restrict nodename,
       const char *proto =
        (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
 
-      if (!(hints->ai_flags & AI_NUMERICSERV))
+      if (hints == NULL || !(hints->ai_flags & AI_NUMERICSERV))
        /* FIXME: Use getservbyname_r if available. */
        se = getservbyname (servname, proto);
 
@@ -300,6 +301,22 @@ getaddrinfo (const char *restrict nodename,
   tmp->ai_addr->sa_family = he->h_addrtype;
   tmp->ai_family = he->h_addrtype;
 
+#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
+  switch (he->h_addrtype)
+    {
+#if HAVE_IPV4
+    case AF_INET:
+      tmp->ai_addr->sa_len = sizeof (struct sockaddr_in);
+      break;
+#endif
+#if HAVE_IPV6
+    case AF_INET6:
+      tmp->ai_addr->sa_len = sizeof (struct sockaddr_in6);
+      break;
+#endif
+    }
+#endif
+
   /* FIXME: If more than one address, create linked list of addrinfo's. */
 
   *res = tmp;
@@ -326,7 +343,7 @@ freeaddrinfo (struct addrinfo *ai)
       cur = ai;
       ai = ai->ai_next;
 
-      if (cur->ai_canonname) free (cur->ai_canonname);
+      free (cur->ai_canonname);
       free (cur);
     }
 }
@@ -408,11 +425,8 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
        {
          unsigned short int port
            = ntohs (((const struct sockaddr_in *) sa)->sin_port);
-         char buf[INT_BUFSIZE_BOUND (port)];
-         char const *s = uinttostr (port, buf);
-         if (strlen (s) + 1 > servicelen)
+         if (servicelen <= snprintf (service, servicelen, "%u", port))
            return EAI_OVERFLOW;
-         memcpy (service, s, strlen (s) + 1);
        }
        break;
       }