source: UsbWattMeter/trunk/curl-7.47.1/lib/curl_addrinfo.c@ 167

Last change on this file since 167 was 167, checked in by coas-nagasima, 6 years ago

MIMEにSJISを設定

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc; charset=SHIFT_JIS
File size: 14.4 KB
RevLine 
[164]1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2014, 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 https://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 "curl_setup.h"
24
25#include <curl/curl.h>
26
27#ifdef HAVE_NETINET_IN_H
28# include <netinet/in.h>
29#endif
30#ifdef HAVE_NETDB_H
31# include <netdb.h>
32#endif
33#ifdef HAVE_ARPA_INET_H
34# include <arpa/inet.h>
35#endif
36#ifdef HAVE_SYS_UN_H
37# include <sys/un.h>
38#endif
39
40#ifdef __VMS
41# include <in.h>
42# include <inet.h>
43#endif
44
45#if defined(NETWARE) && defined(__NOVELL_LIBC__)
46# undef in_addr_t
47# define in_addr_t unsigned long
48#endif
49
50#include "curl_addrinfo.h"
51#include "inet_pton.h"
52#include "warnless.h"
53#include "curl_printf.h"
54#include "curl_memory.h"
55
56/* The last #include file should be: */
57#include "memdebug.h"
58
59/*
60 * Curl_freeaddrinfo()
61 *
62 * This is used to free a linked list of Curl_addrinfo structs along
63 * with all its associated allocated storage. This function should be
64 * called once for each successful call to Curl_getaddrinfo_ex() or to
65 * any function call which actually allocates a Curl_addrinfo struct.
66 */
67
68#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
69 defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
70 /* workaround icc 9.1 optimizer issue */
71# define vqualifier volatile
72#else
73# define vqualifier
74#endif
75
76void
77Curl_freeaddrinfo(Curl_addrinfo *cahead)
78{
79 Curl_addrinfo *vqualifier canext;
80 Curl_addrinfo *ca;
81
82 for(ca = cahead; ca != NULL; ca = canext) {
83 free(ca->ai_addr);
84 free(ca->ai_canonname);
85 canext = ca->ai_next;
86
87 free(ca);
88 }
89}
90
91
92#ifdef HAVE_GETADDRINFO
93/*
94 * Curl_getaddrinfo_ex()
95 *
96 * This is a wrapper function around system's getaddrinfo(), with
97 * the only difference that instead of returning a linked list of
98 * addrinfo structs this one returns a linked list of Curl_addrinfo
99 * ones. The memory allocated by this function *MUST* be free'd with
100 * Curl_freeaddrinfo(). For each successful call to this function
101 * there must be an associated call later to Curl_freeaddrinfo().
102 *
103 * There should be no single call to system's getaddrinfo() in the
104 * whole library, any such call should be 'routed' through this one.
105 */
106
107int
108Curl_getaddrinfo_ex(const char *nodename,
109 const char *servname,
110 const struct addrinfo *hints,
111 Curl_addrinfo **result)
112{
113 const struct addrinfo *ai;
114 struct addrinfo *aihead;
115 Curl_addrinfo *cafirst = NULL;
116 Curl_addrinfo *calast = NULL;
117 Curl_addrinfo *ca;
118 size_t ss_size;
119 int error;
120
121 *result = NULL; /* assume failure */
122
123 error = getaddrinfo(nodename, servname, hints, &aihead);
124 if(error)
125 return error;
126
127 /* traverse the addrinfo list */
128
129 for(ai = aihead; ai != NULL; ai = ai->ai_next) {
130
131 /* ignore elements with unsupported address family, */
132 /* settle family-specific sockaddr structure size. */
133 if(ai->ai_family == AF_INET)
134 ss_size = sizeof(struct sockaddr_in);
135#ifdef ENABLE_IPV6
136 else if(ai->ai_family == AF_INET6)
137 ss_size = sizeof(struct sockaddr_in6);
138#endif
139 else
140 continue;
141
142 /* ignore elements without required address info */
143 if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
144 continue;
145
146 /* ignore elements with bogus address size */
147 if((size_t)ai->ai_addrlen < ss_size)
148 continue;
149
150 if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
151 error = EAI_MEMORY;
152 break;
153 }
154
155 /* copy each structure member individually, member ordering, */
156 /* size, or padding might be different for each platform. */
157
158 ca->ai_flags = ai->ai_flags;
159 ca->ai_family = ai->ai_family;
160 ca->ai_socktype = ai->ai_socktype;
161 ca->ai_protocol = ai->ai_protocol;
162 ca->ai_addrlen = (curl_socklen_t)ss_size;
163 ca->ai_addr = NULL;
164 ca->ai_canonname = NULL;
165 ca->ai_next = NULL;
166
167 if((ca->ai_addr = malloc(ss_size)) == NULL) {
168 error = EAI_MEMORY;
169 free(ca);
170 break;
171 }
172 memcpy(ca->ai_addr, ai->ai_addr, ss_size);
173
174 if(ai->ai_canonname != NULL) {
175 if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
176 error = EAI_MEMORY;
177 free(ca->ai_addr);
178 free(ca);
179 break;
180 }
181 }
182
183 /* if the return list is empty, this becomes the first element */
184 if(!cafirst)
185 cafirst = ca;
186
187 /* add this element last in the return list */
188 if(calast)
189 calast->ai_next = ca;
190 calast = ca;
191
192 }
193
194 /* destroy the addrinfo list */
195 if(aihead)
196 freeaddrinfo(aihead);
197
198 /* if we failed, also destroy the Curl_addrinfo list */
199 if(error) {
200 Curl_freeaddrinfo(cafirst);
201 cafirst = NULL;
202 }
203 else if(!cafirst) {
204#ifdef EAI_NONAME
205 /* rfc3493 conformant */
206 error = EAI_NONAME;
207#else
208 /* rfc3493 obsoleted */
209 error = EAI_NODATA;
210#endif
211#ifdef USE_WINSOCK
212 SET_SOCKERRNO(error);
213#endif
214 }
215
216 *result = cafirst;
217
218 /* This is not a CURLcode */
219 return error;
220}
221#endif /* HAVE_GETADDRINFO */
222
223
224/*
225 * Curl_he2ai()
226 *
227 * This function returns a pointer to the first element of a newly allocated
228 * Curl_addrinfo struct linked list filled with the data of a given hostent.
229 * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
230 * stack, but usable also for IPv4, all hosts and environments.
231 *
232 * The memory allocated by this function *MUST* be free'd later on calling
233 * Curl_freeaddrinfo(). For each successful call to this function there
234 * must be an associated call later to Curl_freeaddrinfo().
235 *
236 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
237 *
238 * struct Curl_addrinfo {
239 * int ai_flags;
240 * int ai_family;
241 * int ai_socktype;
242 * int ai_protocol;
243 * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
244 * char *ai_canonname;
245 * struct sockaddr *ai_addr;
246 * struct Curl_addrinfo *ai_next;
247 * };
248 * typedef struct Curl_addrinfo Curl_addrinfo;
249 *
250 * hostent defined in <netdb.h>
251 *
252 * struct hostent {
253 * char *h_name;
254 * char **h_aliases;
255 * int h_addrtype;
256 * int h_length;
257 * char **h_addr_list;
258 * };
259 *
260 * for backward compatibility:
261 *
262 * #define h_addr h_addr_list[0]
263 */
264
265Curl_addrinfo *
266Curl_he2ai(const struct hostent *he, int port)
267{
268 Curl_addrinfo *ai;
269 Curl_addrinfo *prevai = NULL;
270 Curl_addrinfo *firstai = NULL;
271 struct sockaddr_in *addr;
272#ifdef ENABLE_IPV6
273 struct sockaddr_in6 *addr6;
274#endif
275 CURLcode result = CURLE_OK;
276 int i;
277 char *curr;
278
279 if(!he)
280 /* no input == no output! */
281 return NULL;
282
283 DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
284
285 for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
286
287 size_t ss_size;
288#ifdef ENABLE_IPV6
289 if(he->h_addrtype == AF_INET6)
290 ss_size = sizeof (struct sockaddr_in6);
291 else
292#endif
293 ss_size = sizeof (struct sockaddr_in);
294
295 if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
296 result = CURLE_OUT_OF_MEMORY;
297 break;
298 }
299 if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
300 result = CURLE_OUT_OF_MEMORY;
301 free(ai);
302 break;
303 }
304 if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
305 result = CURLE_OUT_OF_MEMORY;
306 free(ai->ai_canonname);
307 free(ai);
308 break;
309 }
310
311 if(!firstai)
312 /* store the pointer we want to return from this function */
313 firstai = ai;
314
315 if(prevai)
316 /* make the previous entry point to this */
317 prevai->ai_next = ai;
318
319 ai->ai_family = he->h_addrtype;
320
321 /* we return all names as STREAM, so when using this address for TFTP
322 the type must be ignored and conn->socktype be used instead! */
323 ai->ai_socktype = SOCK_STREAM;
324
325 ai->ai_addrlen = (curl_socklen_t)ss_size;
326
327 /* leave the rest of the struct filled with zero */
328
329 switch (ai->ai_family) {
330 case AF_INET:
331 addr = (void *)ai->ai_addr; /* storage area for this info */
332
333 memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
334 addr->sin_family = (unsigned short)(he->h_addrtype);
335 addr->sin_port = htons((unsigned short)port);
336 break;
337
338#ifdef ENABLE_IPV6
339 case AF_INET6:
340 addr6 = (void *)ai->ai_addr; /* storage area for this info */
341
342 memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
343 addr6->sin6_family = (unsigned short)(he->h_addrtype);
344 addr6->sin6_port = htons((unsigned short)port);
345 break;
346#endif
347 }
348
349 prevai = ai;
350 }
351
352 if(result) {
353 Curl_freeaddrinfo(firstai);
354 firstai = NULL;
355 }
356
357 return firstai;
358}
359
360
361struct namebuff {
362 struct hostent hostentry;
363 union {
364 struct in_addr ina4;
365#ifdef ENABLE_IPV6
366 struct in6_addr ina6;
367#endif
368 } addrentry;
369 char *h_addr_list[2];
370};
371
372
373/*
374 * Curl_ip2addr()
375 *
376 * This function takes an internet address, in binary form, as input parameter
377 * along with its address family and the string version of the address, and it
378 * returns a Curl_addrinfo chain filled in correctly with information for the
379 * given address/host
380 */
381
382Curl_addrinfo *
383Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
384{
385 Curl_addrinfo *ai;
386
387#if defined(__VMS) && \
388 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
389#pragma pointer_size save
390#pragma pointer_size short
391#pragma message disable PTRMISMATCH
392#endif
393
394 struct hostent *h;
395 struct namebuff *buf;
396 char *addrentry;
397 char *hoststr;
398 size_t addrsize;
399
400 DEBUGASSERT(inaddr && hostname);
401
402 buf = malloc(sizeof(struct namebuff));
403 if(!buf)
404 return NULL;
405
406 hoststr = strdup(hostname);
407 if(!hoststr) {
408 free(buf);
409 return NULL;
410 }
411
412 switch(af) {
413 case AF_INET:
414 addrsize = sizeof(struct in_addr);
415 addrentry = (void *)&buf->addrentry.ina4;
416 memcpy(addrentry, inaddr, sizeof(struct in_addr));
417 break;
418#ifdef ENABLE_IPV6
419 case AF_INET6:
420 addrsize = sizeof(struct in6_addr);
421 addrentry = (void *)&buf->addrentry.ina6;
422 memcpy(addrentry, inaddr, sizeof(struct in6_addr));
423 break;
424#endif
425 default:
426 free(hoststr);
427 free(buf);
428 return NULL;
429 }
430
431 h = &buf->hostentry;
432 h->h_name = hoststr;
433 h->h_aliases = NULL;
434 h->h_addrtype = (short)af;
435 h->h_length = (short)addrsize;
436 h->h_addr_list = &buf->h_addr_list[0];
437 h->h_addr_list[0] = addrentry;
438 h->h_addr_list[1] = NULL; /* terminate list of entries */
439
440#if defined(__VMS) && \
441 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
442#pragma pointer_size restore
443#pragma message enable PTRMISMATCH
444#endif
445
446 ai = Curl_he2ai(h, port);
447
448 free(hoststr);
449 free(buf);
450
451 return ai;
452}
453
454/*
455 * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
456 * allocated Curl_addrinfo struct and returns it.
457 */
458Curl_addrinfo *Curl_str2addr(char *address, int port)
459{
460 struct in_addr in;
461 if(Curl_inet_pton(AF_INET, address, &in) > 0)
462 /* This is a dotted IP address 123.123.123.123-style */
463 return Curl_ip2addr(AF_INET, &in, address, port);
464#ifdef ENABLE_IPV6
465 else {
466 struct in6_addr in6;
467 if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
468 /* This is a dotted IPv6 address ::1-style */
469 return Curl_ip2addr(AF_INET6, &in6, address, port);
470 }
471#endif
472 return NULL; /* bad input format */
473}
474
475#ifdef USE_UNIX_SOCKETS
476/**
477 * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
478 * struct initialized with this path.
479 */
480Curl_addrinfo *Curl_unix2addr(const char *path)
481{
482 Curl_addrinfo *ai;
483 struct sockaddr_un *sa_un;
484 size_t path_len;
485
486 ai = calloc(1, sizeof(Curl_addrinfo));
487 if(!ai)
488 return NULL;
489 if((ai->ai_addr = calloc(1, sizeof(struct sockaddr_un))) == NULL) {
490 free(ai);
491 return NULL;
492 }
493 /* sun_path must be able to store the NUL-terminated path */
494 path_len = strlen(path);
495 if(path_len >= sizeof(sa_un->sun_path)) {
496 free(ai->ai_addr);
497 free(ai);
498 return NULL;
499 }
500
501 ai->ai_family = AF_UNIX;
502 ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
503 ai->ai_addrlen = (curl_socklen_t) sizeof(struct sockaddr_un);
504 sa_un = (void *) ai->ai_addr;
505 sa_un->sun_family = AF_UNIX;
506 memcpy(sa_un->sun_path, path, path_len + 1); /* copy NUL byte */
507 return ai;
508}
509#endif
510
511#if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
512/*
513 * curl_dofreeaddrinfo()
514 *
515 * This is strictly for memory tracing and are using the same style as the
516 * family otherwise present in memdebug.c. I put these ones here since they
517 * require a bunch of structs I didn't want to include in memdebug.c
518 */
519
520void
521curl_dofreeaddrinfo(struct addrinfo *freethis,
522 int line, const char *source)
523{
524#ifdef USE_LWIPSOCK
525 lwip_freeaddrinfo(freethis);
526#else
527 (freeaddrinfo)(freethis);
528#endif
529 curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
530 source, line, (void *)freethis);
531}
532#endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
533
534
535#if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
536/*
537 * curl_dogetaddrinfo()
538 *
539 * This is strictly for memory tracing and are using the same style as the
540 * family otherwise present in memdebug.c. I put these ones here since they
541 * require a bunch of structs I didn't want to include in memdebug.c
542 */
543
544int
545curl_dogetaddrinfo(const char *hostname,
546 const char *service,
547 const struct addrinfo *hints,
548 struct addrinfo **result,
549 int line, const char *source)
550{
551#ifdef USE_LWIPSOCK
552 int res=lwip_getaddrinfo(hostname, service, hints, result);
553#else
554 int res=(getaddrinfo)(hostname, service, hints, result);
555#endif
556 if(0 == res)
557 /* success */
558 curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
559 source, line, (void *)*result);
560 else
561 curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
562 source, line);
563 return res;
564}
565#endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
566
Note: See TracBrowser for help on using the repository browser.