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