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