| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2017, Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This software is licensed as described in the file COPYING, which |
| 11 |
* you should have received as part of this distribution. The terms |
| 12 |
* are also available at https://curl.haxx.se/docs/copyright.html. |
| 13 |
* |
| 14 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 |
* copies of the Software, and permit persons to whom the Software is |
| 16 |
* furnished to do so, under the terms of the COPYING file. |
| 17 |
* |
| 18 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 |
* KIND, either express or implied. |
| 20 |
* |
| 21 |
***************************************************************************/ |
| 22 |
#include "server_setup.h" |
| 23 |
|
| 24 |
/* Purpose |
| 25 |
* |
| 26 |
* Resolve the given name, using system name resolve functions (NOT any |
| 27 |
* function provided by libcurl). Used to see if the name exists and thus if |
| 28 |
* we can allow a test case to use it for testing. |
| 29 |
* |
| 30 |
* Like if 'localhost' actual exists etc. |
| 31 |
* |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifdef HAVE_SIGNAL_H |
| 35 |
#include <signal.h> |
| 36 |
#endif |
| 37 |
#ifdef HAVE_NETINET_IN_H |
| 38 |
#include <netinet/in.h> |
| 39 |
#endif |
| 40 |
#ifdef _XOPEN_SOURCE_EXTENDED |
| 41 |
/* This define is "almost" required to build on HPUX 11 */ |
| 42 |
#include <arpa/inet.h> |
| 43 |
#endif |
| 44 |
#ifdef HAVE_NETDB_H |
| 45 |
#include <netdb.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#define ENABLE_CURLX_PRINTF |
| 49 |
/* make the curlx header define all printf() functions to use the curlx_* |
| 50 |
versions instead */ |
| 51 |
#include "curlx.h" /* from the private lib dir */ |
| 52 |
#include "util.h" |
| 53 |
|
| 54 |
/* include memdebug.h last */ |
| 55 |
#include "memdebug.h" |
| 56 |
|
| 57 |
static bool use_ipv6 = FALSE; |
| 58 |
static const char *ipv_inuse = "IPv4"; |
| 59 |
|
| 60 |
const char *serverlogfile = ""; /* for a util.c function we don't use */ |
| 61 |
|
| 62 |
int main(int argc, char *argv[]) |
| 63 |
{ |
| 64 |
int arg = 1; |
| 65 |
const char *host = NULL; |
| 66 |
int rc = 0; |
| 67 |
|
| 68 |
while(argc>arg) { |
| 69 |
if(!strcmp("--version", argv[arg])) { |
| 70 |
printf("resolve IPv4%s\n", |
| 71 |
#if defined(CURLRES_IPV6) |
| 72 |
"/IPv6" |
| 73 |
#else |
| 74 |
"" |
| 75 |
#endif |
| 76 |
); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
else if(!strcmp("--ipv6", argv[arg])) { |
| 80 |
ipv_inuse = "IPv6"; |
| 81 |
use_ipv6 = TRUE; |
| 82 |
arg++; |
| 83 |
} |
| 84 |
else if(!strcmp("--ipv4", argv[arg])) { |
| 85 |
/* for completeness, we support this option as well */ |
| 86 |
ipv_inuse = "IPv4"; |
| 87 |
use_ipv6 = FALSE; |
| 88 |
arg++; |
| 89 |
} |
| 90 |
else { |
| 91 |
host = argv[arg++]; |
| 92 |
} |
| 93 |
} |
| 94 |
if(!host) { |
| 95 |
puts("Usage: resolve [option] <host>\n" |
| 96 |
" --version\n" |
| 97 |
" --ipv4" |
| 98 |
#if defined(CURLRES_IPV6) |
| 99 |
"\n --ipv6" |
| 100 |
#endif |
| 101 |
); |
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
#ifdef WIN32 |
| 106 |
win32_init(); |
| 107 |
atexit(win32_cleanup); |
| 108 |
#endif |
| 109 |
|
| 110 |
#if defined(CURLRES_IPV6) |
| 111 |
if(use_ipv6) { |
| 112 |
/* Check that the system has IPv6 enabled before checking the resolver */ |
| 113 |
curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0); |
| 114 |
if(s == CURL_SOCKET_BAD) |
| 115 |
/* an IPv6 address was requested and we can't get/use one */ |
| 116 |
rc = -1; |
| 117 |
else { |
| 118 |
sclose(s); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if(rc == 0) { |
| 123 |
/* getaddrinfo() resolve */ |
| 124 |
struct addrinfo *ai; |
| 125 |
struct addrinfo hints; |
| 126 |
|
| 127 |
memset(&hints, 0, sizeof(hints)); |
| 128 |
hints.ai_family = use_ipv6 ? PF_INET6 : PF_INET; |
| 129 |
hints.ai_socktype = SOCK_STREAM; |
| 130 |
hints.ai_flags = AI_CANONNAME; |
| 131 |
/* Use parenthesis around functions to stop them from being replaced by |
| 132 |
the macro in memdebug.h */ |
| 133 |
rc = (getaddrinfo)(host, "80", &hints, &ai); |
| 134 |
if(rc == 0) |
| 135 |
(freeaddrinfo)(ai); |
| 136 |
} |
| 137 |
#else |
| 138 |
if(use_ipv6) { |
| 139 |
puts("IPv6 support has been disabled in this program"); |
| 140 |
return 1; |
| 141 |
} |
| 142 |
else { |
| 143 |
/* gethostbyname() resolve */ |
| 144 |
struct hostent *he; |
| 145 |
|
| 146 |
he = gethostbyname(host); |
| 147 |
|
| 148 |
rc = !he; |
| 149 |
} |
| 150 |
#endif |
| 151 |
|
| 152 |
if(rc) |
| 153 |
printf("Resolving %s '%s' didn't work\n", ipv_inuse, host); |
| 154 |
|
| 155 |
return !!rc; |
| 156 |
} |