1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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#include "setup.h"
24
25#ifdef HAVE_SYS_SOCKET_H
26#include <sys/socket.h>
27#endif
28#ifdef HAVE_NETINET_IN_H
29#include <netinet/in.h>
30#endif
31#ifdef HAVE_NETDB_H
32#include <netdb.h>
33#endif
34#ifdef HAVE_ARPA_INET_H
35#include <arpa/inet.h>
36#endif
37#ifdef HAVE_UNISTD_H
38#include <unistd.h>     /* for the close() proto */
39#endif
40#ifdef __VMS
41#include <in.h>
42#include <inet.h>
43#endif
44
45#ifdef HAVE_PROCESS_H
46#include <process.h>
47#endif
48
49#include "urldata.h"
50#include "sendf.h"
51#include "hostip.h"
52#include "hash.h"
53#include "share.h"
54#include "strerror.h"
55#include "url.h"
56#include "inet_pton.h"
57#include "connect.h"
58
59#define _MPRINTF_REPLACE /* use our functions only */
60#include <curl/mprintf.h>
61
62#include "curl_memory.h"
63/* The last #include file should be: */
64#include "memdebug.h"
65
66/***********************************************************************
67 * Only for ipv6-enabled builds
68 **********************************************************************/
69#ifdef CURLRES_IPV6
70
71
72#if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
73/* These are strictly for memory tracing and are using the same style as the
74 * family otherwise present in memdebug.c. I put these ones here since they
75 * require a bunch of structs I didn't want to include in memdebug.c
76 */
77
78/*
79 * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
80 * (ignoring the fact c-ares doesn't return 'serv').
81 */
82
83int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
84                       GETNAMEINFO_TYPE_ARG2 salen,
85                       char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
86                       char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
87                       GETNAMEINFO_TYPE_ARG7 flags,
88                       int line, const char *source)
89{
90  int res = (getnameinfo)(sa, salen,
91                          host, hostlen,
92                          serv, servlen,
93                          flags);
94  if(0 == res)
95    /* success */
96    curl_memlog("GETNAME %s:%d getnameinfo()\n",
97                source, line);
98  else
99    curl_memlog("GETNAME %s:%d getnameinfo() failed = %d\n",
100                source, line, res);
101  return res;
102}
103#endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
104
105/*
106 * Curl_ipv6works() returns TRUE if ipv6 seems to work.
107 */
108bool Curl_ipv6works(void)
109{
110  /* the nature of most system is that IPv6 status doesn't come and go
111     during a program's lifetime so we only probe the first time and then we
112     have the info kept for fast re-use */
113  static int ipv6_works = -1;
114  if(-1 == ipv6_works) {
115    /* probe to see if we have a working IPv6 stack */
116    curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
117    if(s == CURL_SOCKET_BAD)
118      /* an ipv6 address was requested but we can't get/use one */
119      ipv6_works = 0;
120    else {
121      ipv6_works = 1;
122      Curl_closesocket(NULL, s);
123    }
124  }
125  return (ipv6_works>0)?TRUE:FALSE;
126}
127
128/*
129 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
130 * been set and returns TRUE if they are OK.
131 */
132bool Curl_ipvalid(struct connectdata *conn)
133{
134  if(conn->ip_version == CURL_IPRESOLVE_V6)
135    return Curl_ipv6works();
136  return TRUE;
137}
138
139#if defined(CURLRES_SYNCH)
140
141#ifdef DEBUG_ADDRINFO
142static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
143{
144  printf("dump_addrinfo:\n");
145  for(; ai; ai = ai->ai_next) {
146    char  buf[INET6_ADDRSTRLEN];
147
148    printf("    fam %2d, CNAME %s, ",
149           ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
150    if(Curl_printable_address(ai, buf, sizeof(buf)))
151      printf("%s\n", buf);
152    else
153      printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
154  }
155}
156#else
157#define dump_addrinfo(x,y) Curl_nop_stmt
158#endif
159
160/*
161 * Curl_getaddrinfo() when built ipv6-enabled (non-threading and
162 * non-ares version).
163 *
164 * Returns name information about the given hostname and port number. If
165 * successful, the 'addrinfo' is returned and the forth argument will point to
166 * memory we need to free after use. That memory *MUST* be freed with
167 * Curl_freeaddrinfo(), nothing else.
168 */
169Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
170                                const char *hostname,
171                                int port,
172                                int *waitp)
173{
174  struct addrinfo hints;
175  Curl_addrinfo *res;
176  int error;
177  char sbuf[NI_MAXSERV];
178  char *sbufptr = NULL;
179  char addrbuf[128];
180  int pf;
181  struct SessionHandle *data = conn->data;
182
183  *waitp = 0; /* synchronous response only */
184
185  /*
186   * Check if a limited name resolve has been requested.
187   */
188  switch(conn->ip_version) {
189  case CURL_IPRESOLVE_V4:
190    pf = PF_INET;
191    break;
192  case CURL_IPRESOLVE_V6:
193    pf = PF_INET6;
194    break;
195  default:
196    pf = PF_UNSPEC;
197    break;
198  }
199
200  if((pf != PF_INET) && !Curl_ipv6works())
201    /* the stack seems to be a non-ipv6 one */
202    pf = PF_INET;
203
204  memset(&hints, 0, sizeof(hints));
205  hints.ai_family = pf;
206  hints.ai_socktype = conn->socktype;
207
208  if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
209     (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
210    /* the given address is numerical only, prevent a reverse lookup */
211    hints.ai_flags = AI_NUMERICHOST;
212  }
213
214  if(port) {
215    snprintf(sbuf, sizeof(sbuf), "%d", port);
216    sbufptr=sbuf;
217  }
218  error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
219  if(error) {
220    infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
221    return NULL;
222  }
223
224  dump_addrinfo(conn, res);
225
226  return res;
227}
228#endif /* CURLRES_SYNCH */
229#endif /* CURLRES_IPV6 */
230
231