ares.h: there is no ares_free_soa function
[platform/upstream/c-ares.git] / inet_ntop.c
index cc25a59..9420f6c 100644 (file)
  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static const char rcsid[] = "$Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp $";
-#endif /* LIBC_SCCS and not lint */
+#include "ares_setup.h"
 
-#include "port_before.h"
-
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <arpa/nameser.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "port_after.h"
-
-#ifdef SPRINTF_CHAR
-# define SPRINTF(x) strlen(sprintf/**/x)
+#ifdef HAVE_NETINET_IN_H
+#  include <netinet/in.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
+#  include <arpa/inet.h>
+#endif
+#ifdef HAVE_ARPA_NAMESER_H
+#  include <arpa/nameser.h>
 #else
-# define SPRINTF(x) ((size_t)sprintf x)
+#  include "nameser.h"
+#endif
+#ifdef HAVE_ARPA_NAMESER_COMPAT_H
+#  include <arpa/nameser_compat.h>
 #endif
 
+#include "ares.h"
+#include "ares_ipv6.h"
+
+#ifndef HAVE_INET_NTOP
+
 /*
  * WARNING: Don't even consider trying to compile this on a system where
  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
  */
 
-static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
-static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
+static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size);
+static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
 
 /* char *
  * inet_ntop(af, src, dst, size)
  *     convert a network format address to presentation format.
  * return:
  *     pointer to presentation format address (`dst'), or NULL (see errno).
+ * note:
+ *     On Windows we store the error in the thread errno, not
+ *     in the winsock error code. This is to avoid loosing the
+ *     actual last winsock error. So use macro ERRNO to fetch the
+ *     errno this funtion sets when returning NULL, not SOCKERRNO.
  * author:
  *     Paul Vixie, 1996.
  */
 const char *
-inet_ntop(int af, const void *src, char *dst, size_t size)
+ares_inet_ntop(int af, const void *src, char *dst, ares_socklen_t size)
 {
   switch (af) {
   case AF_INET:
-    return (inet_ntop4(src, dst, size));
+    return (inet_ntop4(src, dst, (size_t)size));
   case AF_INET6:
-    return (inet_ntop6(src, dst, size));
+    return (inet_ntop6(src, dst, (size_t)size));
   default:
-    errno = EAFNOSUPPORT;
+    SET_ERRNO(EAFNOSUPPORT);
     return (NULL);
   }
   /* NOTREACHED */
@@ -79,18 +80,18 @@ inet_ntop(int af, const void *src, char *dst, size_t size)
  *     `dst' (as a const)
  * notes:
  *     (1) uses no statics
- *     (2) takes a u_char* not an in_addr as input
+ *     (2) takes a unsigned char* not an in_addr as input
  * author:
  *     Paul Vixie, 1996.
  */
 static const char *
-inet_ntop4(const u_char *src, char *dst, size_t size)
+inet_ntop4(const unsigned char *src, char *dst, size_t size)
 {
   static const char fmt[] = "%u.%u.%u.%u";
   char tmp[sizeof("255.255.255.255")];
 
-  if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) >= size) {
-    errno = ENOSPC;
+  if ((size_t)sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= size) {
+    SET_ERRNO(ENOSPC);
     return (NULL);
   }
   strcpy(dst, tmp);
@@ -104,7 +105,7 @@ inet_ntop4(const u_char *src, char *dst, size_t size)
  *     Paul Vixie, 1996.
  */
 static const char *
-inet_ntop6(const u_char *src, char *dst, size_t size)
+inet_ntop6(const unsigned char *src, char *dst, size_t size)
 {
   /*
    * Note that int32_t and int16_t need only be "at least" large enough
@@ -113,9 +114,10 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
    * Keep this in mind if you think this function should have been coded
    * to use pointer overlays.  All the world's not a VAX.
    */
-  char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
+  char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
+  char *tp;
   struct { int base, len; } best, cur;
-  u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
+  unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
   int i;
 
   /*
@@ -175,7 +177,7 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
       tp += strlen(tp);
       break;
     }
-    tp += SPRINTF((tp, "%x", words[i]));
+    tp += sprintf(tp, "%x", words[i]);
   }
   /* Was it a trailing run of 0x00's? */
   if (best.base != -1 && (best.base + best.len) == 
@@ -187,11 +189,20 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
    * Check for overflow, copy, and we're done.
    */
   if ((size_t)(tp - tmp) > size) {
-    errno = ENOSPC;
+    SET_ERRNO(ENOSPC);
     return (NULL);
   }
   strcpy(dst, tmp);
   return (dst);
 }
 
-/*! \file */
+#else /* HAVE_INET_NTOP */
+
+const char *
+ares_inet_ntop(int af, const void *src, char *dst, ares_socklen_t size)
+{
+  /* just relay this to the underlying function */
+  return inet_ntop(af, src, dst, size);
+}
+
+#endif /* HAVE_INET_NTOP */