Coverage Report

Created: 2025-07-01 06:23

/src/irssi/subprojects/openssl-1.1.1l/crypto/bio/b_addr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#ifndef _GNU_SOURCE
11
# define _GNU_SOURCE
12
#endif
13
14
#include <assert.h>
15
#include <string.h>
16
17
#include "bio_local.h"
18
#include <openssl/crypto.h>
19
20
#ifndef OPENSSL_NO_SOCK
21
#include <openssl/err.h>
22
#include <openssl/buffer.h>
23
#include "internal/thread_once.h"
24
25
CRYPTO_RWLOCK *bio_lookup_lock;
26
static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT;
27
28
/*
29
 * Throughout this file and bio_local.h, the existence of the macro
30
 * AI_PASSIVE is used to detect the availability of struct addrinfo,
31
 * getnameinfo() and getaddrinfo().  If that macro doesn't exist,
32
 * we use our own implementation instead, using gethostbyname,
33
 * getservbyname and a few other.
34
 */
35
36
/**********************************************************************
37
 *
38
 * Address structure
39
 *
40
 */
41
42
BIO_ADDR *BIO_ADDR_new(void)
43
0
{
44
0
    BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret));
45
46
0
    if (ret == NULL) {
47
0
        BIOerr(BIO_F_BIO_ADDR_NEW, ERR_R_MALLOC_FAILURE);
48
0
        return NULL;
49
0
    }
50
51
0
    ret->sa.sa_family = AF_UNSPEC;
52
0
    return ret;
53
0
}
54
55
void BIO_ADDR_free(BIO_ADDR *ap)
56
0
{
57
0
    OPENSSL_free(ap);
58
0
}
59
60
void BIO_ADDR_clear(BIO_ADDR *ap)
61
0
{
62
0
    memset(ap, 0, sizeof(*ap));
63
0
    ap->sa.sa_family = AF_UNSPEC;
64
0
}
65
66
/*
67
 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
68
 * of a struct sockaddr.
69
 */
70
int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa)
71
0
{
72
0
    if (sa->sa_family == AF_INET) {
73
0
        memcpy(&(ap->s_in), sa, sizeof(struct sockaddr_in));
74
0
        return 1;
75
0
    }
76
0
#ifdef AF_INET6
77
0
    if (sa->sa_family == AF_INET6) {
78
0
        memcpy(&(ap->s_in6), sa, sizeof(struct sockaddr_in6));
79
0
        return 1;
80
0
    }
81
0
#endif
82
0
#ifdef AF_UNIX
83
0
    if (sa->sa_family == AF_UNIX) {
84
0
        memcpy(&(ap->s_un), sa, sizeof(struct sockaddr_un));
85
0
        return 1;
86
0
    }
87
0
#endif
88
89
0
    return 0;
90
0
}
91
92
int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
93
                     const void *where, size_t wherelen,
94
                     unsigned short port)
95
0
{
96
0
#ifdef AF_UNIX
97
0
    if (family == AF_UNIX) {
98
0
        if (wherelen + 1 > sizeof(ap->s_un.sun_path))
99
0
            return 0;
100
0
        memset(&ap->s_un, 0, sizeof(ap->s_un));
101
0
        ap->s_un.sun_family = family;
102
0
        strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1);
103
0
        return 1;
104
0
    }
105
0
#endif
106
0
    if (family == AF_INET) {
107
0
        if (wherelen != sizeof(struct in_addr))
108
0
            return 0;
109
0
        memset(&ap->s_in, 0, sizeof(ap->s_in));
110
0
        ap->s_in.sin_family = family;
111
0
        ap->s_in.sin_port = port;
112
0
        ap->s_in.sin_addr = *(struct in_addr *)where;
113
0
        return 1;
114
0
    }
115
0
#ifdef AF_INET6
116
0
    if (family == AF_INET6) {
117
0
        if (wherelen != sizeof(struct in6_addr))
118
0
            return 0;
119
0
        memset(&ap->s_in6, 0, sizeof(ap->s_in6));
120
0
        ap->s_in6.sin6_family = family;
121
0
        ap->s_in6.sin6_port = port;
122
0
        ap->s_in6.sin6_addr = *(struct in6_addr *)where;
123
0
        return 1;
124
0
    }
125
0
#endif
126
127
0
    return 0;
128
0
}
129
130
int BIO_ADDR_family(const BIO_ADDR *ap)
131
0
{
132
0
    return ap->sa.sa_family;
133
0
}
134
135
int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
136
0
{
137
0
    size_t len = 0;
138
0
    const void *addrptr = NULL;
139
140
0
    if (ap->sa.sa_family == AF_INET) {
141
0
        len = sizeof(ap->s_in.sin_addr);
142
0
        addrptr = &ap->s_in.sin_addr;
143
0
    }
144
0
#ifdef AF_INET6
145
0
    else if (ap->sa.sa_family == AF_INET6) {
146
0
        len = sizeof(ap->s_in6.sin6_addr);
147
0
        addrptr = &ap->s_in6.sin6_addr;
148
0
    }
149
0
#endif
150
0
#ifdef AF_UNIX
151
0
    else if (ap->sa.sa_family == AF_UNIX) {
152
0
        len = strlen(ap->s_un.sun_path);
153
0
        addrptr = &ap->s_un.sun_path;
154
0
    }
155
0
#endif
156
157
0
    if (addrptr == NULL)
158
0
        return 0;
159
160
0
    if (p != NULL) {
161
0
        memcpy(p, addrptr, len);
162
0
    }
163
0
    if (l != NULL)
164
0
        *l = len;
165
166
0
    return 1;
167
0
}
168
169
unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
170
0
{
171
0
    if (ap->sa.sa_family == AF_INET)
172
0
        return ap->s_in.sin_port;
173
0
#ifdef AF_INET6
174
0
    if (ap->sa.sa_family == AF_INET6)
175
0
        return ap->s_in6.sin6_port;
176
0
#endif
177
0
    return 0;
178
0
}
179
180
/*-
181
 * addr_strings - helper function to get host and service names
182
 * @ap: the BIO_ADDR that has the input info
183
 * @numeric: 0 if actual names should be returned, 1 if the numeric
184
 * representation should be returned.
185
 * @hostname: a pointer to a pointer to a memory area to store the
186
 * host name or numeric representation.  Unused if NULL.
187
 * @service: a pointer to a pointer to a memory area to store the
188
 * service name or numeric representation.  Unused if NULL.
189
 *
190
 * The return value is 0 on failure, with the error code in the error
191
 * stack, and 1 on success.
192
 */
193
static int addr_strings(const BIO_ADDR *ap, int numeric,
194
                        char **hostname, char **service)
195
0
{
196
0
    if (BIO_sock_init() != 1)
197
0
        return 0;
198
199
0
    if (1) {
200
0
#ifdef AI_PASSIVE
201
0
        int ret = 0;
202
0
        char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
203
0
        int flags = 0;
204
205
0
        if (numeric)
206
0
            flags |= NI_NUMERICHOST | NI_NUMERICSERV;
207
208
0
        if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap),
209
0
                               BIO_ADDR_sockaddr_size(ap),
210
0
                               host, sizeof(host), serv, sizeof(serv),
211
0
                               flags)) != 0) {
212
0
# ifdef EAI_SYSTEM
213
0
            if (ret == EAI_SYSTEM) {
214
0
                SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
215
0
                BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
216
0
            } else
217
0
# endif
218
0
            {
219
0
                BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
220
0
                ERR_add_error_data(1, gai_strerror(ret));
221
0
            }
222
0
            return 0;
223
0
        }
224
225
        /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
226
         * leaves it with whatever garbage that happens to be there.
227
         * However, we initialise serv with the empty string (serv[0]
228
         * is therefore NUL), so it gets real easy to detect when things
229
         * didn't go the way one might expect.
230
         */
231
0
        if (serv[0] == '\0') {
232
0
            BIO_snprintf(serv, sizeof(serv), "%d",
233
0
                         ntohs(BIO_ADDR_rawport(ap)));
234
0
        }
235
236
0
        if (hostname != NULL)
237
0
            *hostname = OPENSSL_strdup(host);
238
0
        if (service != NULL)
239
0
            *service = OPENSSL_strdup(serv);
240
0
    } else {
241
0
#endif
242
0
        if (hostname != NULL)
243
0
            *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
244
0
        if (service != NULL) {
245
0
            char serv[6];        /* port is 16 bits => max 5 decimal digits */
246
0
            BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
247
0
            *service = OPENSSL_strdup(serv);
248
0
        }
249
0
    }
250
251
0
    if ((hostname != NULL && *hostname == NULL)
252
0
            || (service != NULL && *service == NULL)) {
253
0
        if (hostname != NULL) {
254
0
            OPENSSL_free(*hostname);
255
0
            *hostname = NULL;
256
0
        }
257
0
        if (service != NULL) {
258
0
            OPENSSL_free(*service);
259
0
            *service = NULL;
260
0
        }
261
0
        BIOerr(BIO_F_ADDR_STRINGS, ERR_R_MALLOC_FAILURE);
262
0
        return 0;
263
0
    }
264
265
0
    return 1;
266
0
}
267
268
char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
269
0
{
270
0
    char *hostname = NULL;
271
272
0
    if (addr_strings(ap, numeric, &hostname, NULL))
273
0
        return hostname;
274
275
0
    return NULL;
276
0
}
277
278
char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
279
0
{
280
0
    char *service = NULL;
281
282
0
    if (addr_strings(ap, numeric, NULL, &service))
283
0
        return service;
284
285
0
    return NULL;
286
0
}
287
288
char *BIO_ADDR_path_string(const BIO_ADDR *ap)
289
0
{
290
0
#ifdef AF_UNIX
291
0
    if (ap->sa.sa_family == AF_UNIX)
292
0
        return OPENSSL_strdup(ap->s_un.sun_path);
293
0
#endif
294
0
    return NULL;
295
0
}
296
297
/*
298
 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
299
 * for a given BIO_ADDR.  In reality, this is simply a type safe cast.
300
 * The returned struct sockaddr is const, so it can't be tampered with.
301
 */
302
const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
303
0
{
304
0
    return &(ap->sa);
305
0
}
306
307
/*
308
 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
309
 * as BIO_ADDR_sockaddr, but returns a non-const.  USE WITH CARE, as
310
 * it allows you to tamper with the data (and thereby the contents
311
 * of the input BIO_ADDR).
312
 */
313
struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
314
0
{
315
0
    return &(ap->sa);
316
0
}
317
318
/*
319
 * BIO_ADDR_sockaddr_size - non-public function that returns the size
320
 * of the struct sockaddr the BIO_ADDR is using.  If the protocol family
321
 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
322
 * the size of the BIO_ADDR type is returned.
323
 */
324
socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
325
0
{
326
0
    if (ap->sa.sa_family == AF_INET)
327
0
        return sizeof(ap->s_in);
328
0
#ifdef AF_INET6
329
0
    if (ap->sa.sa_family == AF_INET6)
330
0
        return sizeof(ap->s_in6);
331
0
#endif
332
0
#ifdef AF_UNIX
333
0
    if (ap->sa.sa_family == AF_UNIX)
334
0
        return sizeof(ap->s_un);
335
0
#endif
336
0
    return sizeof(*ap);
337
0
}
338
339
/**********************************************************************
340
 *
341
 * Address info database
342
 *
343
 */
344
345
const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
346
0
{
347
0
    if (bai != NULL)
348
0
        return bai->bai_next;
349
0
    return NULL;
350
0
}
351
352
int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
353
0
{
354
0
    if (bai != NULL)
355
0
        return bai->bai_family;
356
0
    return 0;
357
0
}
358
359
int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
360
0
{
361
0
    if (bai != NULL)
362
0
        return bai->bai_socktype;
363
0
    return 0;
364
0
}
365
366
int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
367
0
{
368
0
    if (bai != NULL) {
369
0
        if (bai->bai_protocol != 0)
370
0
            return bai->bai_protocol;
371
372
0
#ifdef AF_UNIX
373
0
        if (bai->bai_family == AF_UNIX)
374
0
            return 0;
375
0
#endif
376
377
0
        switch (bai->bai_socktype) {
378
0
        case SOCK_STREAM:
379
0
            return IPPROTO_TCP;
380
0
        case SOCK_DGRAM:
381
0
            return IPPROTO_UDP;
382
0
        default:
383
0
            break;
384
0
        }
385
0
    }
386
0
    return 0;
387
0
}
388
389
/*
390
 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
391
 * of the struct sockaddr inside the BIO_ADDRINFO.
392
 */
393
socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
394
0
{
395
0
    if (bai != NULL)
396
0
        return bai->bai_addrlen;
397
0
    return 0;
398
0
}
399
400
/*
401
 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
402
 * as the struct sockaddr it is.
403
 */
404
const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
405
0
{
406
0
    if (bai != NULL)
407
0
        return bai->bai_addr;
408
0
    return NULL;
409
0
}
410
411
const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
412
0
{
413
0
    if (bai != NULL)
414
0
        return (BIO_ADDR *)bai->bai_addr;
415
0
    return NULL;
416
0
}
417
418
void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
419
0
{
420
0
    if (bai == NULL)
421
0
        return;
422
423
0
#ifdef AI_PASSIVE
424
0
# ifdef AF_UNIX
425
0
#  define _cond bai->bai_family != AF_UNIX
426
# else
427
#  define _cond 1
428
# endif
429
0
    if (_cond) {
430
0
        freeaddrinfo(bai);
431
0
        return;
432
0
    }
433
0
#endif
434
435
    /* Free manually when we know that addrinfo_wrap() was used.
436
     * See further comment above addrinfo_wrap()
437
     */
438
0
    while (bai != NULL) {
439
0
        BIO_ADDRINFO *next = bai->bai_next;
440
0
        OPENSSL_free(bai->bai_addr);
441
0
        OPENSSL_free(bai);
442
0
        bai = next;
443
0
    }
444
0
}
445
446
/**********************************************************************
447
 *
448
 * Service functions
449
 *
450
 */
451
452
/*-
453
 * The specs in hostserv can take these forms:
454
 *
455
 * host:service         => *host = "host", *service = "service"
456
 * host:*               => *host = "host", *service = NULL
457
 * host:                => *host = "host", *service = NULL
458
 * :service             => *host = NULL, *service = "service"
459
 * *:service            => *host = NULL, *service = "service"
460
 *
461
 * in case no : is present in the string, the result depends on
462
 * hostserv_prio, as follows:
463
 *
464
 * when hostserv_prio == BIO_PARSE_PRIO_HOST
465
 * host                 => *host = "host", *service untouched
466
 *
467
 * when hostserv_prio == BIO_PARSE_PRIO_SERV
468
 * service              => *host untouched, *service = "service"
469
 *
470
 */
471
int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
472
                       enum BIO_hostserv_priorities hostserv_prio)
473
0
{
474
0
    const char *h = NULL; size_t hl = 0;
475
0
    const char *p = NULL; size_t pl = 0;
476
477
0
    if (*hostserv == '[') {
478
0
        if ((p = strchr(hostserv, ']')) == NULL)
479
0
            goto spec_err;
480
0
        h = hostserv + 1;
481
0
        hl = p - h;
482
0
        p++;
483
0
        if (*p == '\0')
484
0
            p = NULL;
485
0
        else if (*p != ':')
486
0
            goto spec_err;
487
0
        else {
488
0
            p++;
489
0
            pl = strlen(p);
490
0
        }
491
0
    } else {
492
0
        const char *p2 = strrchr(hostserv, ':');
493
0
        p = strchr(hostserv, ':');
494
495
        /*-
496
         * Check for more than one colon.  There are three possible
497
         * interpretations:
498
         * 1. IPv6 address with port number, last colon being separator.
499
         * 2. IPv6 address only.
500
         * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
501
         *    IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
502
         * Because of this ambiguity, we currently choose to make it an
503
         * error.
504
         */
505
0
        if (p != p2)
506
0
            goto amb_err;
507
508
0
        if (p != NULL) {
509
0
            h = hostserv;
510
0
            hl = p - h;
511
0
            p++;
512
0
            pl = strlen(p);
513
0
        } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
514
0
            h = hostserv;
515
0
            hl = strlen(h);
516
0
        } else {
517
0
            p = hostserv;
518
0
            pl = strlen(p);
519
0
        }
520
0
    }
521
522
0
    if (p != NULL && strchr(p, ':'))
523
0
        goto spec_err;
524
525
0
    if (h != NULL && host != NULL) {
526
0
        if (hl == 0
527
0
            || (hl == 1 && h[0] == '*')) {
528
0
            *host = NULL;
529
0
        } else {
530
0
            *host = OPENSSL_strndup(h, hl);
531
0
            if (*host == NULL)
532
0
                goto memerr;
533
0
        }
534
0
    }
535
0
    if (p != NULL && service != NULL) {
536
0
        if (pl == 0
537
0
            || (pl == 1 && p[0] == '*')) {
538
0
            *service = NULL;
539
0
        } else {
540
0
            *service = OPENSSL_strndup(p, pl);
541
0
            if (*service == NULL)
542
0
                goto memerr;
543
0
        }
544
0
    }
545
546
0
    return 1;
547
0
 amb_err:
548
0
    BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
549
0
    return 0;
550
0
 spec_err:
551
0
    BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
552
0
    return 0;
553
0
 memerr:
554
0
    BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
555
0
    return 0;
556
0
}
557
558
/* addrinfo_wrap is used to build our own addrinfo "chain".
559
 * (it has only one entry, so calling it a chain may be a stretch)
560
 * It should ONLY be called when getaddrinfo() and friends
561
 * aren't available, OR when dealing with a non IP protocol
562
 * family, such as AF_UNIX
563
 *
564
 * the return value is 1 on success, or 0 on failure, which
565
 * only happens if a memory allocation error occurred.
566
 */
567
static int addrinfo_wrap(int family, int socktype,
568
                         const void *where, size_t wherelen,
569
                         unsigned short port,
570
                         BIO_ADDRINFO **bai)
571
0
{
572
0
    if ((*bai = OPENSSL_zalloc(sizeof(**bai))) == NULL) {
573
0
        BIOerr(BIO_F_ADDRINFO_WRAP, ERR_R_MALLOC_FAILURE);
574
0
        return 0;
575
0
    }
576
577
0
    (*bai)->bai_family = family;
578
0
    (*bai)->bai_socktype = socktype;
579
0
    if (socktype == SOCK_STREAM)
580
0
        (*bai)->bai_protocol = IPPROTO_TCP;
581
0
    if (socktype == SOCK_DGRAM)
582
0
        (*bai)->bai_protocol = IPPROTO_UDP;
583
0
#ifdef AF_UNIX
584
0
    if (family == AF_UNIX)
585
0
        (*bai)->bai_protocol = 0;
586
0
#endif
587
0
    {
588
        /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
589
           just an advanced cast of BIO_ADDR* to struct sockaddr *
590
           by the power of union, so while it may seem that we're
591
           creating a memory leak here, we are not.  It will be
592
           all right. */
593
0
        BIO_ADDR *addr = BIO_ADDR_new();
594
0
        if (addr != NULL) {
595
0
            BIO_ADDR_rawmake(addr, family, where, wherelen, port);
596
0
            (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
597
0
        }
598
0
    }
599
0
    (*bai)->bai_next = NULL;
600
0
    if ((*bai)->bai_addr == NULL) {
601
0
        BIO_ADDRINFO_free(*bai);
602
0
        *bai = NULL;
603
0
        return 0;
604
0
    }
605
0
    return 1;
606
0
}
607
608
DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init)
609
0
{
610
0
    if (!OPENSSL_init_crypto(0, NULL))
611
0
        return 0;
612
0
    bio_lookup_lock = CRYPTO_THREAD_lock_new();
613
0
    return bio_lookup_lock != NULL;
614
0
}
615
616
int BIO_lookup(const char *host, const char *service,
617
               enum BIO_lookup_type lookup_type,
618
               int family, int socktype, BIO_ADDRINFO **res)
619
0
{
620
0
    return BIO_lookup_ex(host, service, lookup_type, family, socktype, 0, res);
621
0
}
622
623
/*-
624
 * BIO_lookup_ex - look up the node and service you want to connect to.
625
 * @node: the node you want to connect to.
626
 * @service: the service you want to connect to.
627
 * @lookup_type: declare intent with the result, client or server.
628
 * @family: the address family you want to use.  Use AF_UNSPEC for any, or
629
 *  AF_INET, AF_INET6 or AF_UNIX.
630
 * @socktype: The socket type you want to use.  Can be SOCK_STREAM, SOCK_DGRAM
631
 *  or 0 for all.
632
 * @protocol: The protocol to use, e.g. IPPROTO_TCP or IPPROTO_UDP or 0 for all.
633
 *            Note that some platforms may not return IPPROTO_SCTP without
634
 *            explicitly requesting it (i.e. IPPROTO_SCTP may not be returned
635
 *            with 0 for the protocol)
636
 * @res: Storage place for the resulting list of returned addresses
637
 *
638
 * This will do a lookup of the node and service that you want to connect to.
639
 * It returns a linked list of different addresses you can try to connect to.
640
 *
641
 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
642
 *
643
 * The return value is 1 on success or 0 in case of error.
644
 */
645
int BIO_lookup_ex(const char *host, const char *service, int lookup_type,
646
                  int family, int socktype, int protocol, BIO_ADDRINFO **res)
647
0
{
648
0
    int ret = 0;                 /* Assume failure */
649
650
0
    switch(family) {
651
0
    case AF_INET:
652
0
#ifdef AF_INET6
653
0
    case AF_INET6:
654
0
#endif
655
0
#ifdef AF_UNIX
656
0
    case AF_UNIX:
657
0
#endif
658
0
#ifdef AF_UNSPEC
659
0
    case AF_UNSPEC:
660
0
#endif
661
0
        break;
662
0
    default:
663
0
        BIOerr(BIO_F_BIO_LOOKUP_EX, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
664
0
        return 0;
665
0
    }
666
667
0
#ifdef AF_UNIX
668
0
    if (family == AF_UNIX) {
669
0
        if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
670
0
            return 1;
671
0
        else
672
0
            BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
673
0
        return 0;
674
0
    }
675
0
#endif
676
677
0
    if (BIO_sock_init() != 1)
678
0
        return 0;
679
680
0
    if (1) {
681
0
#ifdef AI_PASSIVE
682
0
        int gai_ret = 0, old_ret = 0;
683
0
        struct addrinfo hints;
684
685
0
        memset(&hints, 0, sizeof(hints));
686
687
0
        hints.ai_family = family;
688
0
        hints.ai_socktype = socktype;
689
0
        hints.ai_protocol = protocol;
690
0
# ifdef AI_ADDRCONFIG
691
0
#  ifdef AF_UNSPEC
692
0
        if (host != NULL && family == AF_UNSPEC)
693
0
#  endif
694
0
            hints.ai_flags |= AI_ADDRCONFIG;
695
0
# endif
696
697
0
        if (lookup_type == BIO_LOOKUP_SERVER)
698
0
            hints.ai_flags |= AI_PASSIVE;
699
700
        /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
701
         * macro magic in bio_local.h
702
         */
703
0
# if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST)
704
0
      retry:
705
0
# endif
706
0
        switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
707
0
# ifdef EAI_SYSTEM
708
0
        case EAI_SYSTEM:
709
0
            SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
710
0
            BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_SYS_LIB);
711
0
            break;
712
0
# endif
713
0
# ifdef EAI_MEMORY
714
0
        case EAI_MEMORY:
715
0
            BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
716
0
            break;
717
0
# endif
718
0
        case 0:
719
0
            ret = 1;             /* Success */
720
0
            break;
721
0
        default:
722
0
# if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST)
723
0
            if (hints.ai_flags & AI_ADDRCONFIG) {
724
0
                hints.ai_flags &= ~AI_ADDRCONFIG;
725
0
                hints.ai_flags |= AI_NUMERICHOST;
726
0
                old_ret = gai_ret;
727
0
                goto retry;
728
0
            }
729
0
# endif
730
0
            BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_SYS_LIB);
731
0
            ERR_add_error_data(1, gai_strerror(old_ret ? old_ret : gai_ret));
732
0
            break;
733
0
        }
734
0
    } else {
735
0
#endif
736
0
        const struct hostent *he;
737
/*
738
 * Because struct hostent is defined for 32-bit pointers only with
739
 * VMS C, we need to make sure that '&he_fallback_address' and
740
 * '&he_fallback_addresses' are 32-bit pointers
741
 */
742
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
743
# pragma pointer_size save
744
# pragma pointer_size 32
745
#endif
746
        /* Windows doesn't seem to have in_addr_t */
747
#ifdef OPENSSL_SYS_WINDOWS
748
        static uint32_t he_fallback_address;
749
        static const char *he_fallback_addresses[] =
750
            { (char *)&he_fallback_address, NULL };
751
#else
752
0
        static in_addr_t he_fallback_address;
753
0
        static const char *he_fallback_addresses[] =
754
0
            { (char *)&he_fallback_address, NULL };
755
0
#endif
756
0
        static const struct hostent he_fallback =
757
0
            { NULL, NULL, AF_INET, sizeof(he_fallback_address),
758
0
              (char **)&he_fallback_addresses };
759
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
760
# pragma pointer_size restore
761
#endif
762
763
0
        struct servent *se;
764
        /* Apparently, on WIN64, s_proto and s_port have traded places... */
765
#ifdef _WIN64
766
        struct servent se_fallback = { NULL, NULL, NULL, 0 };
767
#else
768
0
        struct servent se_fallback = { NULL, NULL, 0, NULL };
769
0
#endif
770
771
0
        if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) {
772
0
            BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
773
0
            ret = 0;
774
0
            goto err;
775
0
        }
776
777
0
        CRYPTO_THREAD_write_lock(bio_lookup_lock);
778
0
        he_fallback_address = INADDR_ANY;
779
0
        if (host == NULL) {
780
0
            he = &he_fallback;
781
0
            switch(lookup_type) {
782
0
            case BIO_LOOKUP_CLIENT:
783
0
                he_fallback_address = INADDR_LOOPBACK;
784
0
                break;
785
0
            case BIO_LOOKUP_SERVER:
786
0
                he_fallback_address = INADDR_ANY;
787
0
                break;
788
0
            default:
789
                /* We forgot to handle a lookup type! */
790
0
                assert("We forgot to handle a lookup type!" == NULL);
791
0
                BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_INTERNAL_ERROR);
792
0
                ret = 0;
793
0
                goto err;
794
0
            }
795
0
        } else {
796
0
            he = gethostbyname(host);
797
798
0
            if (he == NULL) {
799
0
#ifndef OPENSSL_SYS_WINDOWS
800
                /*
801
                 * This might be misleading, because h_errno is used as if
802
                 * it was errno. To minimize mixup add 1000. Underlying
803
                 * reason for this is that hstrerror is declared obsolete,
804
                 * not to mention that a) h_errno is not always guaranteed
805
                 * to be meaningless; b) hstrerror can reside in yet another
806
                 * library, linking for sake of hstrerror is an overkill;
807
                 * c) this path is not executed on contemporary systems
808
                 * anyway [above getaddrinfo/gai_strerror is]. We just let
809
                 * system administrator figure this out...
810
                 */
811
# if defined(OPENSSL_SYS_VXWORKS)
812
                /* h_errno doesn't exist on VxWorks */
813
                SYSerr(SYS_F_GETHOSTBYNAME, 1000 );
814
# else
815
0
                SYSerr(SYS_F_GETHOSTBYNAME, 1000 + h_errno);
816
0
# endif
817
#else
818
                SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
819
#endif
820
0
                ret = 0;
821
0
                goto err;
822
0
            }
823
0
        }
824
825
0
        if (service == NULL) {
826
0
            se_fallback.s_port = 0;
827
0
            se_fallback.s_proto = NULL;
828
0
            se = &se_fallback;
829
0
        } else {
830
0
            char *endp = NULL;
831
0
            long portnum = strtol(service, &endp, 10);
832
833
/*
834
 * Because struct servent is defined for 32-bit pointers only with
835
 * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
836
 */
837
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
838
# pragma pointer_size save
839
# pragma pointer_size 32
840
#endif
841
0
            char *proto = NULL;
842
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
843
# pragma pointer_size restore
844
#endif
845
846
0
            switch (socktype) {
847
0
            case SOCK_STREAM:
848
0
                proto = "tcp";
849
0
                break;
850
0
            case SOCK_DGRAM:
851
0
                proto = "udp";
852
0
                break;
853
0
            }
854
855
0
            if (endp != service && *endp == '\0'
856
0
                    && portnum > 0 && portnum < 65536) {
857
0
                se_fallback.s_port = htons((unsigned short)portnum);
858
0
                se_fallback.s_proto = proto;
859
0
                se = &se_fallback;
860
0
            } else if (endp == service) {
861
0
                se = getservbyname(service, proto);
862
863
0
                if (se == NULL) {
864
0
#ifndef OPENSSL_SYS_WINDOWS
865
0
                    SYSerr(SYS_F_GETSERVBYNAME, errno);
866
#else
867
                    SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
868
#endif
869
0
                    goto err;
870
0
                }
871
0
            } else {
872
0
                BIOerr(BIO_F_BIO_LOOKUP_EX, BIO_R_MALFORMED_HOST_OR_SERVICE);
873
0
                goto err;
874
0
            }
875
0
        }
876
877
0
        *res = NULL;
878
879
0
        {
880
/*
881
 * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
882
 * we must make sure our iterator designates the same element type, hence
883
 * the pointer size dance.
884
 */
885
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
886
# pragma pointer_size save
887
# pragma pointer_size 32
888
#endif
889
0
            char **addrlistp;
890
#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
891
# pragma pointer_size restore
892
#endif
893
0
            size_t addresses;
894
0
            BIO_ADDRINFO *tmp_bai = NULL;
895
896
            /* The easiest way to create a linked list from an
897
               array is to start from the back */
898
0
            for(addrlistp = he->h_addr_list; *addrlistp != NULL;
899
0
                addrlistp++)
900
0
                ;
901
902
0
            for(addresses = addrlistp - he->h_addr_list;
903
0
                addrlistp--, addresses-- > 0; ) {
904
0
                if (!addrinfo_wrap(he->h_addrtype, socktype,
905
0
                                   *addrlistp, he->h_length,
906
0
                                   se->s_port, &tmp_bai))
907
0
                    goto addrinfo_malloc_err;
908
0
                tmp_bai->bai_next = *res;
909
0
                *res = tmp_bai;
910
0
                continue;
911
0
             addrinfo_malloc_err:
912
0
                BIO_ADDRINFO_free(*res);
913
0
                *res = NULL;
914
0
                BIOerr(BIO_F_BIO_LOOKUP_EX, ERR_R_MALLOC_FAILURE);
915
0
                ret = 0;
916
0
                goto err;
917
0
            }
918
919
0
            ret = 1;
920
0
        }
921
0
     err:
922
0
        CRYPTO_THREAD_unlock(bio_lookup_lock);
923
0
    }
924
925
0
    return ret;
926
0
}
927
928
#endif /* OPENSSL_NO_SOCK */