1
/***************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
* KIND, either express or implied.
21
* $Id: hostip6.c,v 1.2 2004/10/05 16:42:38 hoffman Exp $
22
***************************************************************************/
31
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
34
#ifdef HAVE_SYS_TYPES_H
35
#include <sys/types.h>
37
#ifdef HAVE_SYS_SOCKET_H
38
#include <sys/socket.h>
40
#ifdef HAVE_NETINET_IN_H
41
#include <netinet/in.h>
46
#ifdef HAVE_ARPA_INET_H
47
#include <arpa/inet.h>
50
#include <stdlib.h> /* required for free() prototypes */
53
#include <unistd.h> /* for the close() proto */
70
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
72
#define in_addr_t unsigned long
83
#define _MPRINTF_REPLACE /* use our functions only */
84
#include <curl/mprintf.h>
86
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
87
#include "inet_ntoa_r.h"
90
#include "curl_memory.h"
91
/* The last #include file should be: */
94
/***********************************************************************
95
* Only for ipv6-enabled builds
96
**********************************************************************/
99
* This is a wrapper function for freeing name information in a protocol
100
* independent way. This takes care of using the appropriate underlaying
103
void Curl_freeaddrinfo(Curl_addrinfo *p)
108
#ifdef CURLRES_ASYNCH
110
* Curl_addrinfo_copy() is used by the asynch callback to copy a given
111
* address. But this is an ipv6 build and then we don't copy the address, we
112
* just return the same pointer!
114
Curl_addrinfo *Curl_addrinfo_copy(void *source, int port)
122
/* These are strictly for memory tracing and are using the same style as the
123
* family otherwise present in memdebug.c. I put these ones here since they
124
* require a bunch of structs I didn't wanna include in memdebug.c
126
int curl_dogetaddrinfo(char *hostname, char *service,
127
struct addrinfo *hints,
128
struct addrinfo **result,
129
int line, const char *source)
131
int res=(getaddrinfo)(hostname, service, hints, result);
135
fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
136
source, line, (void *)*result);
140
fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
146
int curl_dogetnameinfo(const struct sockaddr *sa, socklen_t salen,
147
char *host, size_t hostlen,
148
char *serv, size_t servlen, int flags,
149
int line, const char *source)
151
int res=(getnameinfo)(sa, salen, host, hostlen, serv, servlen, flags);
155
fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
160
fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
166
void curl_dofreeaddrinfo(struct addrinfo *freethis,
167
int line, const char *source)
169
(freeaddrinfo)(freethis);
171
fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
172
source, line, (void *)freethis);
178
* Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
179
* been set and returns TRUE if they are OK.
181
bool Curl_ipvalid(struct SessionHandle *data)
183
if(data->set.ip_version == CURL_IPRESOLVE_V6) {
184
/* see if we have an IPv6 stack */
185
curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
186
if (s == CURL_SOCKET_BAD)
187
/* an ipv6 address was requested and we can't get/use one */
194
#ifndef USE_THREADING_GETADDRINFO
196
* Curl_getaddrinfo() when built ipv6-enabled (non-threading version).
198
* Returns name information about the given hostname and port number. If
199
* successful, the 'addrinfo' is returned and the forth argument will point to
200
* memory we need to free after use. That memory *MUST* be freed with
201
* Curl_freeaddrinfo(), nothing else.
203
Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
208
struct addrinfo hints, *res;
210
char sbuf[NI_MAXSERV];
213
struct SessionHandle *data = conn->data;
215
*waitp=0; /* don't wait, we have the response now */
217
/* see if we have an IPv6 stack */
218
s = socket(PF_INET6, SOCK_DGRAM, 0);
220
/* Some non-IPv6 stacks have been found to make very slow name resolves
221
* when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
222
* the stack seems to be a non-ipv6 one. */
227
/* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
228
* possible checks. And close the socket again.
233
* Check if a more limited name resolve has been requested.
235
switch(data->set.ip_version) {
236
case CURL_IPRESOLVE_V4:
239
case CURL_IPRESOLVE_V6:
248
memset(&hints, 0, sizeof(hints));
249
hints.ai_family = pf;
250
hints.ai_socktype = SOCK_STREAM;
251
hints.ai_flags = AI_CANONNAME;
252
snprintf(sbuf, sizeof(sbuf), "%d", port);
253
error = getaddrinfo(hostname, sbuf, &hints, &res);
255
infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
261
#endif /* USE_THREADING_GETADDRINFO */