resolve.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "server_setup.h"
00023 
00024 /* Purpose
00025  *
00026  * Resolve the given name, using system name resolve functions (NOT any
00027  * function provided by libcurl). Used to see if the name exists and thus if
00028  * we can allow a test case to use it for testing.
00029  *
00030  * Like if 'localhost' actual exists etc.
00031  *
00032  */
00033 
00034 #ifdef HAVE_SIGNAL_H
00035 #include <signal.h>
00036 #endif
00037 #ifdef HAVE_NETINET_IN_H
00038 #include <netinet/in.h>
00039 #endif
00040 #ifdef _XOPEN_SOURCE_EXTENDED
00041 /* This define is "almost" required to build on HPUX 11 */
00042 #include <arpa/inet.h>
00043 #endif
00044 #ifdef HAVE_NETDB_H
00045 #include <netdb.h>
00046 #endif
00047 
00048 #define ENABLE_CURLX_PRINTF
00049 /* make the curlx header define all printf() functions to use the curlx_*
00050    versions instead */
00051 #include "curlx.h" /* from the private lib dir */
00052 #include "util.h"
00053 
00054 /* include memdebug.h last */
00055 #include "memdebug.h"
00056 
00057 static bool use_ipv6 = FALSE;
00058 static const char *ipv_inuse = "IPv4";
00059 
00060 const char *serverlogfile=""; /* for a util.c function we don't use */
00061 
00062 int main(int argc, char *argv[])
00063 {
00064   int arg=1;
00065   const char *host = NULL;
00066   int rc = 0;
00067 
00068   while(argc>arg) {
00069     if(!strcmp("--version", argv[arg])) {
00070       printf("resolve IPv4%s\n",
00071 #ifdef ENABLE_IPV6
00072              "/IPv6"
00073 #else
00074              ""
00075 #endif
00076              );
00077       return 0;
00078     }
00079     else if(!strcmp("--ipv6", argv[arg])) {
00080       ipv_inuse = "IPv6";
00081       use_ipv6 = TRUE;
00082       arg++;
00083     }
00084     else if(!strcmp("--ipv4", argv[arg])) {
00085       /* for completeness, we support this option as well */
00086       ipv_inuse = "IPv4";
00087       use_ipv6 = FALSE;
00088       arg++;
00089     }
00090     else {
00091       host = argv[arg++];
00092     }
00093   }
00094   if(!host) {
00095     puts("Usage: resolve [option] <host>\n"
00096          " --version\n"
00097          " --ipv4"
00098 #ifdef ENABLE_IPV6
00099          "\n --ipv6"
00100 #endif
00101          );
00102     return 1;
00103   }
00104 
00105 #ifdef WIN32
00106   win32_init();
00107   atexit(win32_cleanup);
00108 #endif
00109 
00110   if(!use_ipv6) {
00111     /* gethostbyname() resolve */
00112     struct hostent *he;
00113 
00114     he = gethostbyname(host);
00115 
00116     rc = !he;
00117   }
00118   else {
00119 #ifdef ENABLE_IPV6
00120     /* Check that the system has IPv6 enabled before checking the resolver */
00121     curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
00122     if(s == CURL_SOCKET_BAD)
00123       /* an IPv6 address was requested and we can't get/use one */
00124       rc = -1;
00125     else {
00126       sclose(s);
00127     }
00128 
00129     if(rc == 0) {
00130       /* getaddrinfo() resolve */
00131       struct addrinfo *ai;
00132       struct addrinfo hints;
00133 
00134       memset(&hints, 0, sizeof(hints));
00135       hints.ai_family = PF_INET6;
00136       hints.ai_socktype = SOCK_STREAM;
00137       hints.ai_flags = AI_CANONNAME;
00138       /* Use parenthesis around functions to stop them from being replaced by
00139          the macro in memdebug.h */
00140       rc = (getaddrinfo)(host, "80", &hints, &ai);
00141       if(rc == 0)
00142         (freeaddrinfo)(ai);
00143     }
00144 
00145 #else
00146     puts("IPv6 support has been disabled in this program");
00147     return 1;
00148 #endif
00149   }
00150   if(rc)
00151     printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
00152 
00153   return !!rc;
00154 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06