]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/b_addr.c
Rearrange the use of 'proto' in BIO_lookup
[thirdparty/openssl.git] / crypto / bio / b_addr.c
CommitLineData
28a0841b
RL
1/* ====================================================================
2 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
52 *
53 */
54
55#include <string.h>
56
57#include "bio_lcl.h"
f989cd8c 58#include "internal/threads.h"
28a0841b 59
1288f26f 60#ifndef OPENSSL_NO_SOCK
28a0841b
RL
61#include <openssl/err.h>
62#include <openssl/buffer.h>
d40cf9bc 63#include <ctype.h>
28a0841b 64
f989cd8c
AG
65static CRYPTO_RWLOCK *bio_lookup_lock;
66static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT;
67
28a0841b
RL
68/*
69 * Throughout this file and bio_lcl.h, the existence of the macro
70 * AI_PASSIVE is used to detect the availability of struct addrinfo,
71 * getnameinfo() and getaddrinfo(). If that macro doesn't exist,
72 * we use our own implementation instead, using gethostbyname,
73 * getservbyname and a few other.
74 */
75
76/**********************************************************************
77 *
78 * Address structure
79 *
80 */
81
82BIO_ADDR *BIO_ADDR_new(void)
83{
43ecb9c3
RS
84 BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret));
85
7d1d48a2 86 ret->sa.sa_family = AF_UNSPEC;
28a0841b
RL
87 return ret;
88}
89
90void BIO_ADDR_free(BIO_ADDR *ap)
91{
92 OPENSSL_free(ap);
93}
94
7d1d48a2
MC
95void BIO_ADDR_clear(BIO_ADDR *ap)
96{
97 memset(ap, 0, sizeof(*ap));
98 ap->sa.sa_family = AF_UNSPEC;
99}
100
28a0841b
RL
101/*
102 * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents
103 * of a struct sockaddr.
104 */
105int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa)
106{
107 if (sa->sa_family == AF_INET) {
29620124 108 ap->s_in = *(const struct sockaddr_in *)sa;
28a0841b
RL
109 return 1;
110 }
111#ifdef AF_INET6
112 if (sa->sa_family == AF_INET6) {
29620124 113 ap->s_in6 = *(const struct sockaddr_in6 *)sa;
28a0841b
RL
114 return 1;
115 }
116#endif
117#ifdef AF_UNIX
118 if (ap->sa.sa_family == AF_UNIX) {
29620124 119 ap->s_un = *(const struct sockaddr_un *)sa;
28a0841b
RL
120 return 1;
121 }
122#endif
123
124 return 0;
125}
126
127int BIO_ADDR_rawmake(BIO_ADDR *ap, int family,
128 const void *where, size_t wherelen,
129 unsigned short port)
130{
131#ifdef AF_UNIX
132 if (family == AF_UNIX) {
29620124 133 if (wherelen + 1 > sizeof(ap->s_un.sun_path))
28a0841b 134 return 0;
29620124
RL
135 memset(&ap->s_un, 0, sizeof(ap->s_un));
136 ap->s_un.sun_family = family;
137 strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1);
28a0841b
RL
138 return 1;
139 }
140#endif
141 if (family == AF_INET) {
142 if (wherelen != sizeof(struct in_addr))
143 return 0;
29620124
RL
144 memset(&ap->s_in, 0, sizeof(ap->s_in));
145 ap->s_in.sin_family = family;
146 ap->s_in.sin_port = port;
147 ap->s_in.sin_addr = *(struct in_addr *)where;
28a0841b
RL
148 return 1;
149 }
150#ifdef AF_INET6
151 if (family == AF_INET6) {
152 if (wherelen != sizeof(struct in6_addr))
153 return 0;
29620124
RL
154 memset(&ap->s_in6, 0, sizeof(ap->s_in6));
155 ap->s_in6.sin6_family = family;
156 ap->s_in6.sin6_port = port;
157 ap->s_in6.sin6_addr = *(struct in6_addr *)where;
28a0841b
RL
158 return 1;
159 }
160#endif
161
162 return 0;
163}
164
165int BIO_ADDR_family(const BIO_ADDR *ap)
166{
167 return ap->sa.sa_family;
168}
169
170int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
171{
172 size_t len = 0;
173 const void *addrptr = NULL;
174
175 if (ap->sa.sa_family == AF_INET) {
29620124
RL
176 len = sizeof(ap->s_in.sin_addr);
177 addrptr = &ap->s_in.sin_addr;
28a0841b
RL
178 }
179#ifdef AF_INET6
180 else if (ap->sa.sa_family == AF_INET6) {
29620124
RL
181 len = sizeof(ap->s_in6.sin6_addr);
182 addrptr = &ap->s_in6.sin6_addr;
28a0841b
RL
183 }
184#endif
185#ifdef AF_UNIX
186 else if (ap->sa.sa_family == AF_UNIX) {
29620124
RL
187 len = strlen(ap->s_un.sun_path);
188 addrptr = &ap->s_un.sun_path;
28a0841b
RL
189 }
190#endif
191
192 if (addrptr == NULL)
193 return 0;
194
195 if (p != NULL) {
196 memcpy(p, addrptr, len);
197 }
198 if (l != NULL)
199 *l = len;
200
201 return 1;
202}
203
204unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
205{
206 if (ap->sa.sa_family == AF_INET)
29620124 207 return ap->s_in.sin_port;
28a0841b
RL
208#ifdef AF_INET6
209 if (ap->sa.sa_family == AF_INET6)
29620124 210 return ap->s_in6.sin6_port;
28a0841b
RL
211#endif
212 return 0;
213}
214
215/*-
216 * addr_strings - helper function to get host and service names
217 * @ap: the BIO_ADDR that has the input info
218 * @numeric: 0 if actual names should be returned, 1 if the numeric
219 * representation should be returned.
220 * @hostname: a pointer to a pointer to a memory area to store the
221 * host name or numeric representation. Unused if NULL.
222 * @service: a pointer to a pointer to a memory area to store the
223 * service name or numeric representation. Unused if NULL.
224 *
225 * The return value is 0 on failure, with the error code in the error
226 * stack, and 1 on success.
227 */
228static int addr_strings(const BIO_ADDR *ap, int numeric,
229 char **hostname, char **service)
230{
ed03c461
RL
231 if (BIO_sock_init() != 1)
232 return 0;
233
28a0841b
RL
234 if (1) {
235#ifdef AI_PASSIVE
236 int ret = 0;
6faffd0a 237 char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = "";
28a0841b
RL
238 int flags = 0;
239
240 if (numeric)
241 flags |= NI_NUMERICHOST | NI_NUMERICSERV;
242
243 if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap),
244 BIO_ADDR_sockaddr_size(ap),
245 host, sizeof(host), serv, sizeof(serv),
246 flags)) != 0) {
247# ifdef EAI_SYSTEM
248 if (ret == EAI_SYSTEM) {
249 SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
250 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
251 } else
252# endif
253 {
254 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
255 ERR_add_error_data(1, gai_strerror(ret));
256 }
257 return 0;
258 }
d40cf9bc 259
6faffd0a
RL
260 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
261 * leaves it with whatever garbage that happens to be there.
262 * However, we initialise serv with the empty string (serv[0]
263 * is therefore NUL), so it gets real easy to detect when things
264 * didn't go the way one might expect.
d40cf9bc 265 */
6faffd0a 266 if (serv[0] == '\0') {
d40cf9bc
RL
267 BIO_snprintf(serv, sizeof(serv), "%d",
268 ntohs(BIO_ADDR_rawport(ap)));
269 }
270
28a0841b
RL
271 if (hostname)
272 *hostname = OPENSSL_strdup(host);
273 if (service)
274 *service = OPENSSL_strdup(serv);
275 } else {
276#endif
277 if (hostname)
29620124 278 *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
28a0841b
RL
279 if (service) {
280 char serv[6]; /* port is 16 bits => max 5 decimal digits */
29620124 281 BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
28a0841b
RL
282 *service = OPENSSL_strdup(serv);
283 }
284 }
285
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:
569 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
570 return 0;
571 spec_err:
572 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
573 return 0;
574 memerr:
575 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
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
586 * only happens if a memory allocation error occured.
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{
593 OPENSSL_assert(bai != NULL);
594
43ecb9c3 595 *bai = OPENSSL_zalloc(sizeof(**bai));
28a0841b
RL
596 if (*bai == NULL)
597 return 0;
43ecb9c3 598
28a0841b
RL
599 (*bai)->bai_family = family;
600 (*bai)->bai_socktype = socktype;
601 if (socktype == SOCK_STREAM)
602 (*bai)->bai_protocol = IPPROTO_TCP;
603 if (socktype == SOCK_DGRAM)
604 (*bai)->bai_protocol = IPPROTO_UDP;
605#ifdef AF_UNIX
606 if (family == AF_UNIX)
607 (*bai)->bai_protocol = 0;
608#endif
609 {
610 /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
611 just an advanced cast of BIO_ADDR* to struct sockaddr *
612 by the power of union, so while it may seem that we're
613 creating a memory leak here, we are not. It will be
614 all right. */
615 BIO_ADDR *addr = BIO_ADDR_new();
616 if (addr != NULL) {
617 BIO_ADDR_rawmake(addr, family, where, wherelen, port);
618 (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
619 }
620 }
621 (*bai)->bai_next = NULL;
622 if ((*bai)->bai_addr == NULL) {
623 BIO_ADDRINFO_free(*bai);
624 *bai = NULL;
625 return 0;
626 }
627 return 1;
628}
629
f989cd8c
AG
630static void do_bio_lookup_init(void)
631{
632 bio_lookup_lock = CRYPTO_THREAD_lock_new();
633}
634
28a0841b
RL
635/*-
636 * BIO_lookup - look up the node and service you want to connect to.
637 * @node: the node you want to connect to.
638 * @service: the service you want to connect to.
639 * @lookup_type: declare intent with the result, client or server.
640 * @family: the address family you want to use. Use AF_UNSPEC for any, or
641 * AF_INET, AF_INET6 or AF_UNIX.
642 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
643 * or 0 for all.
644 * @res: Storage place for the resulting list of returned addresses
645 *
646 * This will do a lookup of the node and service that you want to connect to.
647 * It returns a linked list of different addresses you can try to connect to.
648 *
649 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
650 *
651 * The return value is 1 on success or 0 in case of error.
652 */
653int BIO_lookup(const char *host, const char *service,
654 enum BIO_lookup_type lookup_type,
655 int family, int socktype, BIO_ADDRINFO **res)
656{
657 int ret = 0; /* Assume failure */
658
659 switch(family) {
660 case AF_INET:
661#ifdef AF_INET6
662 case AF_INET6:
663#endif
664#ifdef AF_UNIX
665 case AF_UNIX:
666#endif
667#ifdef AF_UNSPEC
668 case AF_UNSPEC:
669#endif
670 break;
671 default:
672 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
673 return 0;
674 }
675
676#ifdef AF_UNIX
677 if (family == AF_UNIX) {
678 if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
679 return 1;
680 else
681 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
682 return 0;
683 }
684#endif
685
ed03c461
RL
686 if (BIO_sock_init() != 1)
687 return 0;
688
28a0841b 689 if (1) {
ed03c461 690 int gai_ret = 0;
28a0841b
RL
691#ifdef AI_PASSIVE
692 struct addrinfo hints;
f0496ad7 693 memset(&hints, 0, sizeof hints);
28a0841b 694
28a0841b
RL
695# ifdef AI_ADDRCONFIG
696 hints.ai_flags = AI_ADDRCONFIG;
697# endif
698 hints.ai_family = family;
699 hints.ai_socktype = socktype;
28a0841b
RL
700
701 if (lookup_type == BIO_LOOKUP_SERVER)
702 hints.ai_flags |= AI_PASSIVE;
703
704 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
705 * macro magic in bio_lcl.h
706 */
ed03c461 707 switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
28a0841b
RL
708# ifdef EAI_SYSTEM
709 case EAI_SYSTEM:
710 SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
711 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
712 break;
713# endif
714 case 0:
715 ret = 1; /* Success */
716 break;
717 default:
718 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
ed03c461 719 ERR_add_error_data(1, gai_strerror(gai_ret));
28a0841b
RL
720 break;
721 }
722 } else {
723#endif
37e3daf4 724 const struct hostent *he;
28a0841b
RL
725 /* Windows doesn't seem to have in_addr_t */
726#ifdef OPENSSL_SYS_WINDOWS
37e3daf4
KR
727 static uint32_t he_fallback_address;
728 static const uint32_t *he_fallback_addresses[] =
729 { &he_fallback_address, NULL };
28a0841b 730#else
37e3daf4
KR
731 static in_addr_t he_fallback_address;
732 static const in_addr_t *he_fallback_addresses[] =
733 { &he_fallback_address, NULL };
28a0841b 734#endif
37e3daf4
KR
735 static const struct hostent he_fallback =
736 { NULL, NULL, AF_INET, sizeof(he_fallback_address),
737 (char **)&he_fallback_addresses };
28a0841b
RL
738 struct servent *se;
739 /* Apprently, on WIN64, s_proto and s_port have traded places... */
740#ifdef _WIN64
741 struct servent se_fallback = { NULL, NULL, NULL, 0 };
742#else
743 struct servent se_fallback = { NULL, NULL, 0, NULL };
744#endif
28a0841b 745
f989cd8c
AG
746 CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
747
748 CRYPTO_THREAD_write_lock(bio_lookup_lock);
37e3daf4 749 he_fallback_address = INADDR_ANY;
28a0841b
RL
750 if (host == NULL) {
751 he = &he_fallback;
752 switch(lookup_type) {
753 case BIO_LOOKUP_CLIENT:
754 he_fallback_address = INADDR_LOOPBACK;
755 break;
756 case BIO_LOOKUP_SERVER:
757 he_fallback_address = INADDR_ANY;
758 break;
759 default:
760 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
761 break;
762 }
763 } else {
764 he = gethostbyname(host);
765
766 if (he == NULL) {
c86d1f19 767#ifndef OPENSSL_SYS_WINDOWS
28a0841b
RL
768 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
769 ERR_add_error_data(1, hstrerror(h_errno));
c86d1f19
KR
770#else
771 SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
772#endif
28a0841b
RL
773 ret = 0;
774 goto err;
775 }
776 }
777
778 if (service == NULL) {
779 se_fallback.s_port = 0;
622c7e99 780 se_fallback.s_proto = NULL;
28a0841b
RL
781 se = &se_fallback;
782 } else {
783 char *endp = NULL;
784 long portnum = strtol(service, &endp, 10);
622c7e99
RL
785 char *proto = NULL;
786
787 switch (socktype) {
788 case SOCK_STREAM:
789 proto = "tcp";
790 break;
791 case SOCK_DGRAM:
792 proto = "udp";
793 break;
794 }
28a0841b
RL
795
796 if (endp != service && *endp == '\0'
797 && portnum > 0 && portnum < 65536) {
798 se_fallback.s_port = htons(portnum);
799 se_fallback.s_proto = proto;
800 se = &se_fallback;
801 } else if (endp == service) {
28a0841b
RL
802 se = getservbyname(service, proto);
803
804 if (se == NULL) {
c86d1f19 805#ifndef OPENSSL_SYS_WINDOWS
28a0841b
RL
806 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
807 ERR_add_error_data(1, hstrerror(h_errno));
c86d1f19
KR
808#else
809 SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
810#endif
28a0841b
RL
811 goto err;
812 }
813 } else {
814 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
815 goto err;
816 }
817 }
818
819 *res = NULL;
820
821 {
822 char **addrlistp;
823 size_t addresses;
824 BIO_ADDRINFO *tmp_bai = NULL;
825
826 /* The easiest way to create a linked list from an
827 array is to start from the back */
828 for(addrlistp = he->h_addr_list; *addrlistp != NULL;
829 addrlistp++)
830 ;
831
832 for(addresses = addrlistp - he->h_addr_list;
833 addrlistp--, addresses-- > 0; ) {
834 if (!addrinfo_wrap(he->h_addrtype, socktype,
835 *addrlistp, he->h_length,
836 se->s_port, &tmp_bai))
837 goto addrinfo_malloc_err;
838 tmp_bai->bai_next = *res;
839 *res = tmp_bai;
840 continue;
841 addrinfo_malloc_err:
842 BIO_ADDRINFO_free(*res);
843 *res = NULL;
844 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
845 ret = 0;
846 goto err;
847 }
848
849 ret = 1;
850 }
851 err:
f989cd8c 852 CRYPTO_THREAD_unlock(bio_lookup_lock);
28a0841b
RL
853 }
854
855 return ret;
856}
1288f26f 857#endif /* OPENSSL_NO_SOCK */