Base code merged to SPIN 2.4
[platform/upstream/curl.git] / tests / server / resolve.c
index e0a4e16..39849e7 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -18,8 +18,8 @@
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id$
  ***************************************************************************/
+#include "server_setup.h"
 
 /* Purpose
  *
  * Like if 'localhost' actual exists etc.
  *
  */
-#include "setup.h" /* portability help from the lib directory */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
+#ifdef HAVE_SIGNAL_H
 #include <signal.h>
-#include <time.h>
-#include <ctype.h>
-#include <sys/time.h>
-#include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
 #endif
 #ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 /* include memdebug.h last */
 #include "memdebug.h"
 
-char use_ipv6=FALSE;
+static bool use_ipv6 = FALSE;
+static const char *ipv_inuse = "IPv4";
 
 const char *serverlogfile=""; /* for a util.c function we don't use */
 
 int main(int argc, char *argv[])
 {
   int arg=1;
-  char *host;
-  int rc;
+  const char *host = NULL;
+  int rc = 0;
 
   while(argc>arg) {
     if(!strcmp("--version", argv[arg])) {
@@ -90,14 +77,14 @@ int main(int argc, char *argv[])
       return 0;
     }
     else if(!strcmp("--ipv6", argv[arg])) {
-#ifdef ENABLE_IPV6
-      use_ipv6=TRUE;
-#endif
+      ipv_inuse = "IPv6";
+      use_ipv6 = TRUE;
       arg++;
     }
     else if(!strcmp("--ipv4", argv[arg])) {
       /* for completeness, we support this option as well */
-      use_ipv6=FALSE;
+      ipv_inuse = "IPv4";
+      use_ipv6 = FALSE;
       arg++;
     }
     else {
@@ -107,20 +94,20 @@ int main(int argc, char *argv[])
   if(!host) {
     puts("Usage: resolve [option] <host>\n"
          " --version\n"
-         " --ipv4\n"
-         " --ipv6");
-    return 0;
+         " --ipv4"
+#ifdef ENABLE_IPV6
+         "\n --ipv6"
+#endif
+         );
+    return 1;
   }
 
-#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
+#ifdef WIN32
   win32_init();
   atexit(win32_cleanup);
 #endif
 
-#ifdef ENABLE_IPV6
-  if(!use_ipv6)
-#endif
-  {
+  if(!use_ipv6) {
     /* gethostbyname() resolve */
     struct hostent *he;
 
@@ -128,22 +115,40 @@ int main(int argc, char *argv[])
 
     rc = !he;
   }
-#ifdef ENABLE_IPV6
   else {
-    /* getaddrinfo() resolve */
-    struct addrinfo *ai;
-    struct addrinfo hints;
+#ifdef ENABLE_IPV6
+    /* Check that the system has IPv6 enabled before checking the resolver */
+    curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
+    if(s == CURL_SOCKET_BAD)
+      /* an IPv6 address was requested and we can't get/use one */
+      rc = -1;
+    else {
+      sclose(s);
+    }
 
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = PF_INET6;
-    hints.ai_socktype = SOCK_STREAM;
-    hints.ai_flags = AI_CANONNAME;
-    rc = (getaddrinfo)(host, "80", &hints, &ai);
+    if (rc == 0) {
+      /* getaddrinfo() resolve */
+      struct addrinfo *ai;
+      struct addrinfo hints;
+
+      memset(&hints, 0, sizeof(hints));
+      hints.ai_family = PF_INET6;
+      hints.ai_socktype = SOCK_STREAM;
+      hints.ai_flags = AI_CANONNAME;
+      /* Use parenthesis around functions to stop them from being replaced by
+         the macro in memdebug.h */
+      rc = (getaddrinfo)(host, "80", &hints, &ai);
+      if (rc == 0)
+        (freeaddrinfo)(ai);
+    }
 
-  }
+#else
+    puts("IPv6 support has been disabled in this program");
+    return 1;
 #endif
+  }
   if(rc)
-    printf("Resolving '%s' FAILED\n", host);
+    printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
 
-  return !rc?0:1;
+  return !!rc;
 }