Coverage Report

Created: 2025-06-22 06:56

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