Coverage Report

Created: 2023-04-12 06:22

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