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