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