test servers: build adjustment
[platform/upstream/curl.git] / tests / server / resolve.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2012, 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 http://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_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
42 #endif
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
45 #endif
46 #ifdef HAVE_NETINET_IN_H
47 #include <netinet/in.h>
48 #endif
49 #ifdef _XOPEN_SOURCE_EXTENDED
50 /* This define is "almost" required to build on HPUX 11 */
51 #include <arpa/inet.h>
52 #endif
53 #ifdef HAVE_NETDB_H
54 #include <netdb.h>
55 #endif
56
57 #define ENABLE_CURLX_PRINTF
58 /* make the curlx header define all printf() functions to use the curlx_*
59    versions instead */
60 #include "curlx.h" /* from the private lib dir */
61 #include "util.h"
62
63 /* include memdebug.h last */
64 #include "memdebug.h"
65
66 static bool use_ipv6 = FALSE;
67 static const char *ipv_inuse = "IPv4";
68
69 const char *serverlogfile=""; /* for a util.c function we don't use */
70
71 int main(int argc, char *argv[])
72 {
73   int arg=1;
74   const char *host = NULL;
75   int rc = 0;
76
77   while(argc>arg) {
78     if(!strcmp("--version", argv[arg])) {
79       printf("resolve IPv4%s\n",
80 #ifdef ENABLE_IPV6
81              "/IPv6"
82 #else
83              ""
84 #endif
85              );
86       return 0;
87     }
88     else if(!strcmp("--ipv6", argv[arg])) {
89       ipv_inuse = "IPv6";
90       use_ipv6 = TRUE;
91       arg++;
92     }
93     else if(!strcmp("--ipv4", argv[arg])) {
94       /* for completeness, we support this option as well */
95       ipv_inuse = "IPv4";
96       use_ipv6 = FALSE;
97       arg++;
98     }
99     else {
100       host = argv[arg++];
101     }
102   }
103   if(!host) {
104     puts("Usage: resolve [option] <host>\n"
105          " --version\n"
106          " --ipv4"
107 #ifdef ENABLE_IPV6
108          "\n --ipv6"
109 #endif
110          );
111     return 1;
112   }
113
114 #ifdef WIN32
115   win32_init();
116   atexit(win32_cleanup);
117 #endif
118
119   if(!use_ipv6) {
120     /* gethostbyname() resolve */
121     struct hostent *he;
122
123     he = gethostbyname(host);
124
125     rc = !he;
126   }
127   else {
128 #ifdef ENABLE_IPV6
129     /* Check that the system has IPv6 enabled before checking the resolver */
130     curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
131     if(s == CURL_SOCKET_BAD)
132       /* an ipv6 address was requested and we can't get/use one */
133       rc = -1;
134     else {
135       sclose(s);
136     }
137
138     if (rc == 0) {
139       /* getaddrinfo() resolve */
140       struct addrinfo *ai;
141       struct addrinfo hints;
142
143       memset(&hints, 0, sizeof(hints));
144       hints.ai_family = PF_INET6;
145       hints.ai_socktype = SOCK_STREAM;
146       hints.ai_flags = AI_CANONNAME;
147       /* Use parenthesis around function to stop it from being replaced by
148       the macro in memdebug.h */
149       rc = (getaddrinfo)(host, "80", &hints, &ai);
150     }
151
152 #else
153     puts("IPv6 support has been disabled in this program");
154     return 1;
155 #endif
156   }
157   if(rc)
158     printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
159
160   return !!rc;
161 }