Coverage Report

Created: 2023-11-19 06:21

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