blob: 492ffdeefaa6196acf6a0472badacd4b549a8e42 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, 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.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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "server_setup.h"
25
26/* Purpose
27 *
28 * Resolve the given name, using system name resolve functions (NOT any
29 * function provided by libcurl). Used to see if the name exists and thus if
30 * we can allow a test case to use it for testing.
31 *
32 * Like if 'localhost' actual exists etc.
33 *
34 */
35
36#ifdef HAVE_SIGNAL_H
37#include <signal.h>
38#endif
39#ifdef HAVE_NETINET_IN_H
40#include <netinet/in.h>
41#endif
42#ifdef _XOPEN_SOURCE_EXTENDED
43/* This define is "almost" required to build on HPUX 11 */
44#include <arpa/inet.h>
45#endif
46#ifdef HAVE_NETDB_H
47#include <netdb.h>
48#endif
49
50#define ENABLE_CURLX_PRINTF
51/* make the curlx header define all printf() functions to use the curlx_*
52 versions instead */
53#include "curlx.h" /* from the private lib dir */
54#include "util.h"
55
56/* include memdebug.h last */
57#include "memdebug.h"
58
59static bool use_ipv6 = FALSE;
60static const char *ipv_inuse = "IPv4";
61
62const char *serverlogfile = ""; /* for a util.c function we don't use */
63
64int main(int argc, char *argv[])
65{
66 int arg = 1;
67 const char *host = NULL;
68 int rc = 0;
69
70 while(argc>arg) {
71 if(!strcmp("--version", argv[arg])) {
72 printf("resolve IPv4%s\n",
73#if defined(CURLRES_IPV6)
74 "/IPv6"
75#else
76 ""
77#endif
78 );
79 return 0;
80 }
81 else if(!strcmp("--ipv6", argv[arg])) {
82 ipv_inuse = "IPv6";
83 use_ipv6 = TRUE;
84 arg++;
85 }
86 else if(!strcmp("--ipv4", argv[arg])) {
87 /* for completeness, we support this option as well */
88 ipv_inuse = "IPv4";
89 use_ipv6 = FALSE;
90 arg++;
91 }
92 else {
93 host = argv[arg++];
94 }
95 }
96 if(!host) {
97 puts("Usage: resolve [option] <host>\n"
98 " --version\n"
99 " --ipv4"
100#if defined(CURLRES_IPV6)
101 "\n --ipv6"
102#endif
103 );
104 return 1;
105 }
106
107#ifdef WIN32
108 win32_init();
109 atexit(win32_cleanup);
110#endif
111
112#if defined(CURLRES_IPV6)
113 if(use_ipv6) {
114 /* Check that the system has IPv6 enabled before checking the resolver */
115 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
116 if(s == CURL_SOCKET_BAD)
117 /* an IPv6 address was requested and we can't get/use one */
118 rc = -1;
119 else {
120 sclose(s);
121 }
122 }
123
124 if(rc == 0) {
125 /* getaddrinfo() resolve */
126 struct addrinfo *ai;
127 struct addrinfo hints;
128
129 memset(&hints, 0, sizeof(hints));
130 hints.ai_family = use_ipv6 ? PF_INET6 : PF_INET;
131 hints.ai_socktype = SOCK_STREAM;
132 hints.ai_flags = 0;
133 /* Use parenthesis around functions to stop them from being replaced by
134 the macro in memdebug.h */
135 rc = (getaddrinfo)(host, "80", &hints, &ai);
136 if(rc == 0)
137 (freeaddrinfo)(ai);
138 }
139#else
140 if(use_ipv6) {
141 puts("IPv6 support has been disabled in this program");
142 return 1;
143 }
144 else {
145 /* gethostbyname() resolve */
146 struct hostent *he;
147
148 he = gethostbyname(host);
149
150 rc = !he;
151 }
152#endif
153
154 if(rc)
155 printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
156
157 return !!rc;
158}