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