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