Coverage Report

Created: 2018-08-29 13:53

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