Made the copyright year match the latest modification's year.
[platform/upstream/curl.git] / tests / server / resolve.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2006, 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 #include "setup.h" /* portability help from the lib directory */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <signal.h>
40 #include <time.h>
41 #include <ctype.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
50 #endif
51 #ifdef HAVE_NETINET_IN_H
52 #include <netinet/in.h>
53 #endif
54 #ifdef _XOPEN_SOURCE_EXTENDED
55 /* This define is "almost" required to build on HPUX 11 */
56 #include <arpa/inet.h>
57 #endif
58 #ifdef HAVE_NETDB_H
59 #include <netdb.h>
60 #endif
61
62 #define ENABLE_CURLX_PRINTF
63 /* make the curlx header define all printf() functions to use the curlx_*
64    versions instead */
65 #include "curlx.h" /* from the private lib dir */
66 #include "util.h"
67
68 /* include memdebug.h last */
69 #include "memdebug.h"
70
71 char use_ipv6=FALSE;
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   char *host;
79   int rc;
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 #ifdef ENABLE_IPV6
94       use_ipv6=TRUE;
95 #endif
96       arg++;
97     }
98     else if(!strcmp("--ipv4", argv[arg])) {
99       /* for completeness, we support this option as well */
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\n"
111          " --ipv6");
112     return 0;
113   }
114
115 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
116   win32_init();
117   atexit(win32_cleanup);
118 #endif
119
120 #ifdef ENABLE_IPV6
121   if(!use_ipv6)
122 #endif
123   {
124     /* gethostbyname() resolve */
125     struct hostent *he;
126
127     he = gethostbyname(host);
128
129     rc = !he;
130   }
131 #ifdef ENABLE_IPV6
132   else {
133     /* getaddrinfo() resolve */
134     struct addrinfo *ai;
135     struct addrinfo hints;
136
137     memset(&hints, 0, sizeof(hints));
138     hints.ai_family = PF_INET6;
139     hints.ai_socktype = SOCK_STREAM;
140     hints.ai_flags = AI_CANONNAME;
141     rc = (getaddrinfo)(host, "80", &hints, &ai);
142
143   }
144 #endif
145   if(rc)
146     printf("Resolving '%s' didn't work\n", host);
147
148   return !rc?0:1;
149 }