~ubuntu-branches/ubuntu/jaunty/cmake/jaunty-security

« back to all changes in this revision

Viewing changes to Source/CTest/Curl/hostip6.c

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-06-18 16:34:11 UTC
  • mfrom: (1.4.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060618163411-pi234s3v6jwlcmof
Tags: 2.4.2-1
* New upstream release (Closes: #338324)
* Put cmake .vim files into /usr/share/vim/addons/plugin/
  where they can be used. (Closes: #366663)
* Install cmake-mode.el so it can be used. (Closes: #366664)
* Ensure cmake FindKDE locates KDE libraries on Debian
  based distributions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _     
 
3
 *  Project                     ___| | | |  _ \| |    
 
4
 *                             / __| | | | |_) | |    
 
5
 *                            | (__| |_| |  _ <| |___ 
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2004, 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: hostip6.c,v 1.2 2004/10/05 16:42:38 hoffman Exp $
 
22
 ***************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#include <string.h>
 
27
#include <errno.h>
 
28
 
 
29
#define _REENTRANT
 
30
 
 
31
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
 
32
#include <malloc.h>
 
33
#else
 
34
#ifdef HAVE_SYS_TYPES_H
 
35
#include <sys/types.h>
 
36
#endif
 
37
#ifdef HAVE_SYS_SOCKET_H
 
38
#include <sys/socket.h>
 
39
#endif
 
40
#ifdef HAVE_NETINET_IN_H
 
41
#include <netinet/in.h>
 
42
#endif
 
43
#ifdef HAVE_NETDB_H
 
44
#include <netdb.h>
 
45
#endif
 
46
#ifdef HAVE_ARPA_INET_H
 
47
#include <arpa/inet.h>
 
48
#endif
 
49
#ifdef HAVE_STDLIB_H
 
50
#include <stdlib.h>     /* required for free() prototypes */
 
51
#endif
 
52
#ifdef HAVE_UNISTD_H
 
53
#include <unistd.h>     /* for the close() proto */
 
54
#endif
 
55
#ifdef  VMS
 
56
#include <in.h>
 
57
#include <inet.h>
 
58
#include <stdlib.h>
 
59
#endif
 
60
#endif
 
61
 
 
62
#ifdef HAVE_SETJMP_H
 
63
#include <setjmp.h>
 
64
#endif
 
65
 
 
66
#ifdef WIN32
 
67
#include <process.h>
 
68
#endif
 
69
 
 
70
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
 
71
#undef in_addr_t
 
72
#define in_addr_t unsigned long
 
73
#endif
 
74
 
 
75
#include "urldata.h"
 
76
#include "sendf.h"
 
77
#include "hostip.h"
 
78
#include "hash.h"
 
79
#include "share.h"
 
80
#include "strerror.h"
 
81
#include "url.h"
 
82
 
 
83
#define _MPRINTF_REPLACE /* use our functions only */
 
84
#include <curl/mprintf.h>
 
85
 
 
86
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
 
87
#include "inet_ntoa_r.h"
 
88
#endif
 
89
 
 
90
#include "curl_memory.h"
 
91
/* The last #include file should be: */
 
92
#include "memdebug.h"
 
93
 
 
94
/***********************************************************************
 
95
 * Only for ipv6-enabled builds
 
96
 **********************************************************************/
 
97
#ifdef CURLRES_IPV6
 
98
/*
 
99
 * This is a wrapper function for freeing name information in a protocol
 
100
 * independent way. This takes care of using the appropriate underlaying
 
101
 * function.
 
102
 */
 
103
void Curl_freeaddrinfo(Curl_addrinfo *p)
 
104
{
 
105
  freeaddrinfo(p);
 
106
}
 
107
 
 
108
#ifdef CURLRES_ASYNCH
 
109
/*
 
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!
 
113
 */
 
114
Curl_addrinfo *Curl_addrinfo_copy(void *source, int port)
 
115
{
 
116
  (void) port;
 
117
  return source;
 
118
}
 
119
#endif
 
120
 
 
121
#ifdef CURLDEBUG
 
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
 
125
 */
 
126
int curl_dogetaddrinfo(char *hostname, char *service,
 
127
                       struct addrinfo *hints,
 
128
                       struct addrinfo **result,
 
129
                       int line, const char *source)
 
130
{
 
131
  int res=(getaddrinfo)(hostname, service, hints, result);
 
132
  if(0 == res) {
 
133
    /* success */
 
134
    if(logfile)
 
135
      fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
 
136
              source, line, (void *)*result);
 
137
  }
 
138
  else {
 
139
    if(logfile)
 
140
      fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
 
141
              source, line);
 
142
  }
 
143
  return res;
 
144
}
 
145
 
 
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)
 
150
{
 
151
  int res=(getnameinfo)(sa, salen, host, hostlen, serv, servlen, flags);
 
152
  if(0 == res) {
 
153
    /* success */
 
154
    if(logfile)
 
155
      fprintf(logfile, "GETNAME %s:%d getnameinfo()\n",
 
156
              source, line);
 
157
  }
 
158
  else {
 
159
    if(logfile)
 
160
      fprintf(logfile, "GETNAME %s:%d getnameinfo() failed = %d\n",
 
161
              source, line, res);
 
162
  }
 
163
  return res;
 
164
}
 
165
 
 
166
void curl_dofreeaddrinfo(struct addrinfo *freethis,
 
167
                         int line, const char *source)
 
168
{
 
169
  (freeaddrinfo)(freethis);
 
170
  if(logfile)
 
171
    fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
 
172
            source, line, (void *)freethis);
 
173
}
 
174
 
 
175
#endif
 
176
 
 
177
/*
 
178
 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
 
179
 * been set and returns TRUE if they are OK.
 
180
 */
 
181
bool Curl_ipvalid(struct SessionHandle *data)
 
182
{
 
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 */
 
188
      return FALSE;
 
189
    sclose(s);
 
190
  }
 
191
  return TRUE;
 
192
}
 
193
 
 
194
#ifndef USE_THREADING_GETADDRINFO
 
195
/*
 
196
 * Curl_getaddrinfo() when built ipv6-enabled (non-threading version).
 
197
 *
 
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.
 
202
 */
 
203
Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
 
204
                                char *hostname,
 
205
                                int port,
 
206
                                int *waitp)
 
207
{
 
208
  struct addrinfo hints, *res;
 
209
  int error;
 
210
  char sbuf[NI_MAXSERV];
 
211
  curl_socket_t s;
 
212
  int pf;
 
213
  struct SessionHandle *data = conn->data;
 
214
 
 
215
  *waitp=0; /* don't wait, we have the response now */
 
216
 
 
217
  /* see if we have an IPv6 stack */
 
218
  s = socket(PF_INET6, SOCK_DGRAM, 0);
 
219
  if (s < 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. */
 
223
 
 
224
    pf = PF_INET;
 
225
  }
 
226
  else {
 
227
    /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
 
228
     * possible checks. And close the socket again.
 
229
     */
 
230
    sclose(s);
 
231
 
 
232
    /*
 
233
     * Check if a more limited name resolve has been requested.
 
234
     */
 
235
    switch(data->set.ip_version) {
 
236
    case CURL_IPRESOLVE_V4:
 
237
      pf = PF_INET;
 
238
      break;
 
239
    case CURL_IPRESOLVE_V6:
 
240
      pf = PF_INET6;
 
241
      break;
 
242
    default:
 
243
      pf = PF_UNSPEC;
 
244
      break;
 
245
    }
 
246
  }
 
247
 
 
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);
 
254
  if (error) {
 
255
    infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);    
 
256
    return NULL;
 
257
  }
 
258
 
 
259
  return res;
 
260
}
 
261
#endif /* USE_THREADING_GETADDRINFO */
 
262
#endif /* ipv6 */
 
263