Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/curl_addrinfo.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 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.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
0
{
72
0
  struct Curl_addrinfo *vqualifier canext;
73
0
  struct Curl_addrinfo *ca;
74
75
0
  for(ca = cahead; ca; ca = canext) {
76
0
    canext = ca->ai_next;
77
0
    curlx_free(ca);
78
0
  }
79
0
}
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
0
{
100
0
  const struct addrinfo *ai;
101
0
  struct addrinfo *aihead;
102
0
  struct Curl_addrinfo *cafirst = NULL;
103
0
  struct Curl_addrinfo *calast = NULL;
104
0
  struct Curl_addrinfo *ca;
105
0
  size_t ss_size;
106
0
  int error;
107
108
0
  *result = NULL; /* assume failure */
109
110
0
  error = CURL_GETADDRINFO(nodename, servname, hints, &aihead);
111
0
  if(error)
112
0
    return error;
113
114
  /* traverse the addrinfo list */
115
116
0
  for(ai = aihead; ai != NULL; ai = ai->ai_next) {
117
0
    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
0
    if(ai->ai_family == AF_INET)
121
0
      ss_size = sizeof(struct sockaddr_in);
122
0
#ifdef USE_IPV6
123
0
    else if(ai->ai_family == AF_INET6)
124
0
      ss_size = sizeof(struct sockaddr_in6);
125
0
#endif
126
0
    else
127
0
      continue;
128
129
    /* ignore elements without required address info */
130
0
    if(!ai->ai_addr || !(ai->ai_addrlen > 0))
131
0
      continue;
132
133
    /* ignore elements with bogus address size */
134
0
    if((size_t)ai->ai_addrlen < ss_size)
135
0
      continue;
136
137
0
    ca = curlx_malloc(sizeof(struct Curl_addrinfo) + ss_size + namelen);
138
0
    if(!ca) {
139
0
      error = EAI_MEMORY;
140
0
      break;
141
0
    }
142
143
    /* copy each structure member individually, member ordering,
144
       size, or padding might be different for each platform. */
145
146
0
    ca->ai_flags     = ai->ai_flags;
147
0
    ca->ai_family    = ai->ai_family;
148
0
    ca->ai_socktype  = ai->ai_socktype;
149
0
    ca->ai_protocol  = ai->ai_protocol;
150
0
    ca->ai_addrlen   = (curl_socklen_t)ss_size;
151
0
    ca->ai_addr      = NULL;
152
0
    ca->ai_canonname = NULL;
153
0
    ca->ai_next      = NULL;
154
155
0
    ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
156
0
    memcpy(ca->ai_addr, ai->ai_addr, ss_size);
157
158
0
    if(namelen) {
159
0
      ca->ai_canonname = (void *)((char *)ca->ai_addr + ss_size);
160
0
      memcpy(ca->ai_canonname, ai->ai_canonname, namelen);
161
0
    }
162
163
    /* if the return list is empty, this becomes the first element */
164
0
    if(!cafirst)
165
0
      cafirst = ca;
166
167
    /* add this element last in the return list */
168
0
    if(calast)
169
0
      calast->ai_next = ca;
170
0
    calast = ca;
171
0
  }
172
173
  /* destroy the addrinfo list */
174
0
  if(aihead)
175
0
    CURL_FREEADDRINFO(aihead);
176
177
  /* if we failed, also destroy the Curl_addrinfo list */
178
0
  if(error) {
179
0
    Curl_freeaddrinfo(cafirst);
180
0
    cafirst = NULL;
181
0
  }
182
0
  else if(!cafirst) {
183
0
#ifdef EAI_NONAME
184
    /* rfc3493 conformant */
185
0
    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
0
  }
194
195
0
  *result = cafirst;
196
197
  /* This is not a CURLcode */
198
0
  return error;
199
0
}
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
0
{
344
0
  struct Curl_addrinfo *ai;
345
0
  size_t addrsize;
346
0
  size_t namelen;
347
0
  struct sockaddr_in *addr;
348
0
#ifdef USE_IPV6
349
0
  struct sockaddr_in6 *addr6;
350
0
#endif
351
352
0
  DEBUGASSERT(inaddr && hostname);
353
354
0
  namelen = strlen(hostname) + 1;
355
0
  *addrp = NULL;
356
357
0
  if(af == AF_INET)
358
0
    addrsize = sizeof(struct sockaddr_in);
359
0
#ifdef USE_IPV6
360
0
  else if(af == AF_INET6)
361
0
    addrsize = sizeof(struct sockaddr_in6);
362
0
#endif
363
0
  else
364
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
365
366
  /* allocate memory to hold the struct, the address and the name */
367
0
  ai = curlx_calloc(1, sizeof(struct Curl_addrinfo) + addrsize + namelen);
368
0
  if(!ai)
369
0
    return CURLE_OUT_OF_MEMORY;
370
  /* put the address after the struct */
371
0
  ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
372
  /* then put the name after the address */
373
0
  ai->ai_canonname = (char *)ai->ai_addr + addrsize;
374
0
  memcpy(ai->ai_canonname, hostname, namelen);
375
0
  ai->ai_family = af;
376
0
  ai->ai_socktype = SOCK_STREAM;
377
0
  ai->ai_addrlen = (curl_socklen_t)addrsize;
378
  /* leave the rest of the struct filled with zero */
379
380
0
  switch(af) {
381
0
  case AF_INET:
382
0
    addr = (void *)ai->ai_addr; /* storage area for this info */
383
384
0
    memcpy(&addr->sin_addr, inaddr, sizeof(struct in_addr));
385
0
    addr->sin_family = (CURL_SA_FAMILY_T)af;
386
0
    addr->sin_port = htons((unsigned short)port);
387
0
    break;
388
389
0
#ifdef USE_IPV6
390
0
  case AF_INET6:
391
0
    addr6 = (void *)ai->ai_addr; /* storage area for this info */
392
393
0
    memcpy(&addr6->sin6_addr, inaddr, sizeof(struct in6_addr));
394
0
    addr6->sin6_family = (CURL_SA_FAMILY_T)af;
395
0
    addr6->sin6_port = htons((unsigned short)port);
396
0
    break;
397
0
#endif
398
0
  }
399
0
  *addrp = ai;
400
0
  return CURLE_OK;
401
0
}
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 *dotted, uint16_t port,
408
                       struct Curl_addrinfo **addrp)
409
0
{
410
0
  struct in_addr in;
411
0
  if(curlx_inet_pton(AF_INET, dotted, &in) > 0)
412
    /* This is a dotted IP address 123.123.123.123-style */
413
0
    return ip2addr(addrp, AF_INET, &in, dotted, port);
414
0
#ifdef USE_IPV6
415
0
  {
416
0
    struct in6_addr in6;
417
0
    if(curlx_inet_pton(AF_INET6, dotted, &in6) > 0)
418
      /* This is a dotted IPv6 address ::1-style */
419
0
      return ip2addr(addrp, AF_INET6, &in6, dotted, port);
420
0
  }
421
0
#endif
422
0
  return CURLE_BAD_FUNCTION_ARGUMENT; /* bad input format */
423
0
}
424
425
bool Curl_is_ipv4addr(const char *address)
426
0
{
427
0
  struct in_addr in;
428
0
  return (curlx_inet_pton(AF_INET, address, &in) > 0);
429
0
}
430
431
bool Curl_is_ipaddr(const char *address)
432
0
{
433
0
  if(Curl_is_ipv4addr(address))
434
0
    return TRUE;
435
0
#ifdef USE_IPV6
436
0
  {
437
0
    struct in6_addr in6;
438
0
    if(curlx_inet_pton(AF_INET6, address, &in6) > 0)
439
      /* This is a dotted IPv6 address ::1-style */
440
0
      return TRUE;
441
0
  }
442
0
#endif
443
0
  return FALSE;
444
0
}
445
446
#ifdef USE_UNIX_SOCKETS
447
/**
448
 * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
449
 * struct initialized with this path.
450
 * Returns CURLE_TOO_LARGE when path is too long.
451
 */
452
CURLcode Curl_unix2addr(const char *path, bool abstract,
453
                        struct Curl_addrinfo **paddr)
454
{
455
  struct Curl_addrinfo *ai;
456
  struct sockaddr_un *sa_un;
457
  size_t path_len;
458
459
  *paddr = NULL;
460
461
  /* sun_path must be able to store the null-terminated path */
462
  path_len = strlen(path) + 1;
463
  if(path_len > sizeof(sa_un->sun_path))
464
    return CURLE_TOO_LARGE;
465
466
  ai = curlx_calloc(1,
467
                    sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un));
468
  if(!ai)
469
    return CURLE_OUT_OF_MEMORY;
470
471
  ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
472
  sa_un = (void *)ai->ai_addr;
473
  sa_un->sun_family = AF_UNIX;
474
475
  ai->ai_family = AF_UNIX;
476
  ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
477
  ai->ai_addrlen = (curl_socklen_t)
478
    ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
479
480
  /* Abstract Unix domain socket have NULL prefix instead of suffix */
481
  if(abstract)
482
    memcpy(sa_un->sun_path + 1, path, path_len - 1);
483
  else
484
    memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
485
486
  *paddr = ai;
487
  return CURLE_OK;
488
}
489
#endif
490
491
#if defined(CURL_MEMDEBUG) && defined(HAVE_GETADDRINFO) && \
492
  defined(HAVE_FREEADDRINFO)
493
/*
494
 * curl_dbg_freeaddrinfo()
495
 *
496
 * This is strictly for memory tracing and are using the same style as the
497
 * family otherwise present in memdebug.c. I put these ones here since they
498
 * require a bunch of structs I did not want to include in memdebug.c
499
 */
500
void curl_dbg_freeaddrinfo(struct addrinfo *freethis,
501
                           int line, const char *source)
502
{
503
  curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n",
504
               source, line, (void *)freethis);
505
#ifdef USE_LWIPSOCK
506
  lwip_freeaddrinfo(freethis);
507
#elif defined(USE_FAKE_GETADDRINFO)
508
  {
509
    const char *env = getenv("CURL_DNS_SERVER");
510
    if(env)
511
      r_freeaddrinfo(freethis);
512
    else
513
      /* !checksrc! disable BANNEDFUNC 1 */
514
      freeaddrinfo(freethis);
515
  }
516
#else
517
  /* !checksrc! disable BANNEDFUNC 1 */
518
  freeaddrinfo(freethis);
519
#endif
520
}
521
#endif /* CURL_MEMDEBUG && HAVE_FREEADDRINFO */
522
523
#if defined(CURL_MEMDEBUG) && defined(HAVE_GETADDRINFO)
524
/*
525
 * curl_dbg_getaddrinfo()
526
 *
527
 * This is strictly for memory tracing and are using the same style as the
528
 * family otherwise present in memdebug.c. I put these ones here since they
529
 * require a bunch of structs I did not want to include in memdebug.c
530
 */
531
int 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", source, line,
554
                 (void *)*result);
555
  else
556
    curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n", source, line);
557
  return res;
558
}
559
#endif /* CURL_MEMDEBUG && HAVE_GETADDRINFO */
560
561
#if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
562
/*
563
 * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and macOS
564
 * 10.11.5.
565
 */
566
void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port)
567
{
568
  struct Curl_addrinfo *ca;
569
  struct sockaddr_in *addr;
570
#ifdef USE_IPV6
571
  struct sockaddr_in6 *addr6;
572
#endif
573
  for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
574
    switch(ca->ai_family) {
575
    case AF_INET:
576
      addr = (void *)ca->ai_addr; /* storage area for this info */
577
      addr->sin_port = htons((unsigned short)port);
578
      break;
579
580
#ifdef USE_IPV6
581
    case AF_INET6:
582
      addr6 = (void *)ca->ai_addr; /* storage area for this info */
583
      addr6->sin6_port = htons((unsigned short)port);
584
      break;
585
#endif
586
    }
587
  }
588
}
589
#endif