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