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