]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/b_addr.c
Fix typo in OPENSSL_LH_new compat API
[thirdparty/openssl.git] / crypto / bio / b_addr.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
28a0841b 3 *
b1322259
RS
4 * Licensed under the OpenSSL license (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
28a0841b
RL
8 */
9
10#include <string.h>
11
12#include "bio_lcl.h"
5c4328f0 13#include <openssl/crypto.h>
28a0841b 14
1288f26f 15#ifndef OPENSSL_NO_SOCK
28a0841b
RL
16#include <openssl/err.h>
17#include <openssl/buffer.h>
c2e4e5d2 18#include <internal/thread_once.h>
d40cf9bc 19#include <ctype.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) {
29620124 69 ap->s_in = *(const struct sockaddr_in *)sa;
28a0841b
RL
70 return 1;
71 }
72#ifdef AF_INET6
73 if (sa->sa_family == AF_INET6) {
29620124 74 ap->s_in6 = *(const struct sockaddr_in6 *)sa;
28a0841b
RL
75 return 1;
76 }
77#endif
78#ifdef AF_UNIX
79 if (ap->sa.sa_family == AF_UNIX) {
29620124 80 ap->s_un = *(const struct sockaddr_un *)sa;
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) {
210 SYSerr(SYS_F_GETNAMEINFO, get_last_socket_error());
211 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
212 } else
213# endif
214 {
215 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_SYS_LIB);
216 ERR_add_error_data(1, gai_strerror(ret));
217 }
218 return 0;
219 }
d40cf9bc 220
6faffd0a
RL
221 /* VMS getnameinfo() has a bug, it doesn't fill in serv, which
222 * leaves it with whatever garbage that happens to be there.
223 * However, we initialise serv with the empty string (serv[0]
224 * is therefore NUL), so it gets real easy to detect when things
225 * didn't go the way one might expect.
d40cf9bc 226 */
6faffd0a 227 if (serv[0] == '\0') {
d40cf9bc
RL
228 BIO_snprintf(serv, sizeof(serv), "%d",
229 ntohs(BIO_ADDR_rawport(ap)));
230 }
231
24854e01 232 if (hostname != NULL)
28a0841b 233 *hostname = OPENSSL_strdup(host);
24854e01 234 if (service != NULL)
28a0841b
RL
235 *service = OPENSSL_strdup(serv);
236 } else {
237#endif
24854e01 238 if (hostname != NULL)
29620124 239 *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
24854e01 240 if (service != NULL) {
28a0841b 241 char serv[6]; /* port is 16 bits => max 5 decimal digits */
29620124 242 BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port));
28a0841b
RL
243 *service = OPENSSL_strdup(serv);
244 }
245 }
246
24854e01
MC
247 if ((hostname != NULL && *hostname == NULL)
248 || (service != NULL && *service == NULL)) {
249 if (hostname != NULL) {
250 OPENSSL_free(*hostname);
251 *hostname = NULL;
252 }
253 if (service != NULL) {
254 OPENSSL_free(*service);
255 *service = NULL;
256 }
257 BIOerr(BIO_F_ADDR_STRINGS, ERR_R_MALLOC_FAILURE);
258 return 0;
259 }
260
28a0841b
RL
261 return 1;
262}
263
264char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
265{
266 char *hostname = NULL;
267
268 if (addr_strings(ap, numeric, &hostname, NULL))
269 return hostname;
270
271 return NULL;
272}
273
274char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
275{
276 char *service = NULL;
277
278 if (addr_strings(ap, numeric, NULL, &service))
279 return service;
280
281 return NULL;
282}
283
284char *BIO_ADDR_path_string(const BIO_ADDR *ap)
285{
286#ifdef AF_UNIX
287 if (ap->sa.sa_family == AF_UNIX)
29620124 288 return OPENSSL_strdup(ap->s_un.sun_path);
28a0841b
RL
289#endif
290 return NULL;
291}
292
293/*
294 * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
295 * for a given BIO_ADDR. In reality, this is simply a type safe cast.
296 * The returned struct sockaddr is const, so it can't be tampered with.
297 */
298const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
299{
300 return &(ap->sa);
301}
302
303/*
304 * BIO_ADDR_sockaddr_noconst - non-public function that does the same
305 * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as
306 * it allows you to tamper with the data (and thereby the contents
307 * of the input BIO_ADDR).
308 */
309struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
310{
311 return &(ap->sa);
312}
313
314/*
315 * BIO_ADDR_sockaddr_size - non-public function that returns the size
316 * of the struct sockaddr the BIO_ADDR is using. If the protocol family
317 * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
318 * the size of the BIO_ADDR type is returned.
319 */
320socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
321{
322 if (ap->sa.sa_family == AF_INET)
29620124 323 return sizeof(ap->s_in);
28a0841b
RL
324#ifdef AF_INET6
325 if (ap->sa.sa_family == AF_INET6)
29620124 326 return sizeof(ap->s_in6);
28a0841b
RL
327#endif
328#ifdef AF_UNIX
329 if (ap->sa.sa_family == AF_UNIX)
29620124 330 return sizeof(ap->s_un);
28a0841b
RL
331#endif
332 return sizeof(*ap);
333}
334
335/**********************************************************************
336 *
e5a82bfd 337 * Address info database
28a0841b
RL
338 *
339 */
340
341const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
342{
343 if (bai != NULL)
344 return bai->bai_next;
345 return NULL;
346}
347
348int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
349{
350 if (bai != NULL)
351 return bai->bai_family;
352 return 0;
353}
354
355int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
356{
357 if (bai != NULL)
358 return bai->bai_socktype;
359 return 0;
360}
361
362int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
363{
c72fb77f
RL
364 if (bai != NULL) {
365 if (bai->bai_protocol != 0)
366 return bai->bai_protocol;
367
368#ifdef AF_UNIX
369 if (bai->bai_family == AF_UNIX)
370 return 0;
371#endif
372
373 switch (bai->bai_socktype) {
374 case SOCK_STREAM:
375 return IPPROTO_TCP;
376 case SOCK_DGRAM:
377 return IPPROTO_UDP;
378 default:
379 break;
380 }
381 }
28a0841b
RL
382 return 0;
383}
384
385/*
386 * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
387 * of the struct sockaddr inside the BIO_ADDRINFO.
388 */
389socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
390{
391 if (bai != NULL)
392 return bai->bai_addrlen;
393 return 0;
394}
395
396/*
397 * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
398 * as the struct sockaddr it is.
399 */
400const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
401{
402 if (bai != NULL)
403 return bai->bai_addr;
404 return NULL;
405}
406
407const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
408{
409 if (bai != NULL)
410 return (BIO_ADDR *)bai->bai_addr;
411 return NULL;
412}
413
414void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
415{
416 if (bai == NULL)
417 return;
418
419#ifdef AI_PASSIVE
420# ifdef AF_UNIX
421# define _cond bai->bai_family != AF_UNIX
422# else
423# define _cond 1
424# endif
425 if (_cond) {
426 freeaddrinfo(bai);
427 return;
428 }
429#endif
430
431 /* Free manually when we know that addrinfo_wrap() was used.
432 * See further comment above addrinfo_wrap()
433 */
434 while (bai != NULL) {
435 BIO_ADDRINFO *next = bai->bai_next;
436 OPENSSL_free(bai->bai_addr);
437 OPENSSL_free(bai);
438 bai = next;
439 }
440}
441
442/**********************************************************************
443 *
444 * Service functions
445 *
446 */
447
448/*-
449 * The specs in hostserv can take these forms:
450 *
451 * host:service => *host = "host", *service = "service"
452 * host:* => *host = "host", *service = NULL
453 * host: => *host = "host", *service = NULL
454 * :service => *host = NULL, *service = "service"
455 * *:service => *host = NULL, *service = "service"
456 *
457 * in case no : is present in the string, the result depends on
458 * hostserv_prio, as follows:
459 *
460 * when hostserv_prio == BIO_PARSE_PRIO_HOST
461 * host => *host = "host", *service untouched
462 *
463 * when hostserv_prio == BIO_PARSE_PRIO_SERV
464 * service => *host untouched, *service = "service"
465 *
466 */
467int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
468 enum BIO_hostserv_priorities hostserv_prio)
469{
470 const char *h = NULL; size_t hl = 0;
471 const char *p = NULL; size_t pl = 0;
472
473 if (*hostserv == '[') {
474 if ((p = strchr(hostserv, ']')) == NULL)
475 goto spec_err;
476 h = hostserv + 1;
477 hl = p - h;
478 p++;
479 if (*p == '\0')
480 p = NULL;
481 else if (*p != ':')
482 goto spec_err;
483 else {
484 p++;
485 pl = strlen(p);
486 }
487 } else {
488 const char *p2 = strrchr(hostserv, ':');
489 p = strchr(hostserv, ':');
490
491 /*-
492 * Check for more than one colon. There are three possible
493 * interpretations:
494 * 1. IPv6 address with port number, last colon being separator.
495 * 2. IPv6 address only.
496 * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
497 * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
498 * Because of this ambiguity, we currently choose to make it an
499 * error.
500 */
501 if (p != p2)
502 goto amb_err;
503
504 if (p != NULL) {
505 h = hostserv;
506 hl = p - h;
507 p++;
508 pl = strlen(p);
509 } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
510 h = hostserv;
511 hl = strlen(h);
512 } else {
513 p = hostserv;
514 pl = strlen(p);
515 }
516 }
517
80926502 518 if (p != NULL && strchr(p, ':'))
28a0841b
RL
519 goto spec_err;
520
521 if (h != NULL && host != NULL) {
522 if (hl == 0
523 || (hl == 1 && h[0] == '*')) {
524 *host = NULL;
525 } else {
526 *host = OPENSSL_strndup(h, hl);
527 if (*host == NULL)
528 goto memerr;
529 }
530 }
531 if (p != NULL && service != NULL) {
532 if (pl == 0
533 || (pl == 1 && p[0] == '*')) {
534 *service = NULL;
535 } else {
536 *service = OPENSSL_strndup(p, pl);
537 if (*service == NULL)
538 goto memerr;
539 }
540 }
541
542 return 1;
543 amb_err:
544 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
545 return 0;
546 spec_err:
547 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
548 return 0;
549 memerr:
550 BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
551 return 0;
552}
553
554/* addrinfo_wrap is used to build our own addrinfo "chain".
555 * (it has only one entry, so calling it a chain may be a stretch)
556 * It should ONLY be called when getaddrinfo() and friends
557 * aren't available, OR when dealing with a non IP protocol
558 * family, such as AF_UNIX
559 *
560 * the return value is 1 on success, or 0 on failure, which
8483a003 561 * only happens if a memory allocation error occurred.
28a0841b
RL
562 */
563static int addrinfo_wrap(int family, int socktype,
564 const void *where, size_t wherelen,
565 unsigned short port,
566 BIO_ADDRINFO **bai)
567{
568 OPENSSL_assert(bai != NULL);
569
43ecb9c3 570 *bai = OPENSSL_zalloc(sizeof(**bai));
28a0841b
RL
571 if (*bai == NULL)
572 return 0;
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{
135648bc 607 OPENSSL_init_crypto(0, NULL);
f989cd8c 608 bio_lookup_lock = CRYPTO_THREAD_lock_new();
5a7ad1f0 609 return bio_lookup_lock != NULL;
f989cd8c
AG
610}
611
28a0841b
RL
612/*-
613 * BIO_lookup - look up the node and service you want to connect to.
614 * @node: the node you want to connect to.
615 * @service: the service you want to connect to.
616 * @lookup_type: declare intent with the result, client or server.
617 * @family: the address family you want to use. Use AF_UNSPEC for any, or
618 * AF_INET, AF_INET6 or AF_UNIX.
619 * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM
620 * or 0 for all.
621 * @res: Storage place for the resulting list of returned addresses
622 *
623 * This will do a lookup of the node and service that you want to connect to.
624 * It returns a linked list of different addresses you can try to connect to.
625 *
626 * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
627 *
628 * The return value is 1 on success or 0 in case of error.
629 */
630int BIO_lookup(const char *host, const char *service,
631 enum BIO_lookup_type lookup_type,
632 int family, int socktype, BIO_ADDRINFO **res)
633{
634 int ret = 0; /* Assume failure */
635
636 switch(family) {
637 case AF_INET:
638#ifdef AF_INET6
639 case AF_INET6:
640#endif
641#ifdef AF_UNIX
642 case AF_UNIX:
643#endif
644#ifdef AF_UNSPEC
645 case AF_UNSPEC:
646#endif
647 break;
648 default:
649 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
650 return 0;
651 }
652
653#ifdef AF_UNIX
654 if (family == AF_UNIX) {
655 if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
656 return 1;
657 else
658 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
659 return 0;
660 }
661#endif
662
ed03c461
RL
663 if (BIO_sock_init() != 1)
664 return 0;
665
28a0841b
RL
666 if (1) {
667#ifdef AI_PASSIVE
3e49ee23 668 int gai_ret = 0;
28a0841b 669 struct addrinfo hints;
3e49ee23 670
f0496ad7 671 memset(&hints, 0, sizeof hints);
28a0841b 672
28a0841b
RL
673 hints.ai_family = family;
674 hints.ai_socktype = socktype;
28a0841b
RL
675
676 if (lookup_type == BIO_LOOKUP_SERVER)
677 hints.ai_flags |= AI_PASSIVE;
678
679 /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
680 * macro magic in bio_lcl.h
681 */
ed03c461 682 switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
28a0841b
RL
683# ifdef EAI_SYSTEM
684 case EAI_SYSTEM:
685 SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
686 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
687 break;
688# endif
689 case 0:
690 ret = 1; /* Success */
691 break;
692 default:
693 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
ed03c461 694 ERR_add_error_data(1, gai_strerror(gai_ret));
28a0841b
RL
695 break;
696 }
697 } else {
698#endif
37e3daf4 699 const struct hostent *he;
fcd9c8c0
RL
700/*
701 * Because struct hostent is defined for 32-bit pointers only with
702 * VMS C, we need to make sure that '&he_fallback_address' and
703 * '&he_fallback_addresses' are 32-bit pointers
704 */
705#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
706# pragma pointer_size save
707# pragma pointer_size 32
708#endif
28a0841b
RL
709 /* Windows doesn't seem to have in_addr_t */
710#ifdef OPENSSL_SYS_WINDOWS
37e3daf4 711 static uint32_t he_fallback_address;
7fb4b92c
MC
712 static const char *he_fallback_addresses[] =
713 { (char *)&he_fallback_address, NULL };
28a0841b 714#else
37e3daf4 715 static in_addr_t he_fallback_address;
7fb4b92c
MC
716 static const char *he_fallback_addresses[] =
717 { (char *)&he_fallback_address, NULL };
28a0841b 718#endif
37e3daf4
KR
719 static const struct hostent he_fallback =
720 { NULL, NULL, AF_INET, sizeof(he_fallback_address),
721 (char **)&he_fallback_addresses };
fcd9c8c0
RL
722#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
723# pragma pointer_size restore
724#endif
725
28a0841b 726 struct servent *se;
8483a003 727 /* Apparently, on WIN64, s_proto and s_port have traded places... */
28a0841b
RL
728#ifdef _WIN64
729 struct servent se_fallback = { NULL, NULL, NULL, 0 };
730#else
731 struct servent se_fallback = { NULL, NULL, 0, NULL };
732#endif
28a0841b 733
c2e4e5d2
RL
734 if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) {
735 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
736 ret = 0;
737 goto err;
738 }
f989cd8c
AG
739
740 CRYPTO_THREAD_write_lock(bio_lookup_lock);
37e3daf4 741 he_fallback_address = INADDR_ANY;
28a0841b
RL
742 if (host == NULL) {
743 he = &he_fallback;
744 switch(lookup_type) {
745 case BIO_LOOKUP_CLIENT:
746 he_fallback_address = INADDR_LOOPBACK;
747 break;
748 case BIO_LOOKUP_SERVER:
749 he_fallback_address = INADDR_ANY;
750 break;
751 default:
752 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
753 break;
754 }
755 } else {
756 he = gethostbyname(host);
757
758 if (he == NULL) {
c86d1f19 759#ifndef OPENSSL_SYS_WINDOWS
3e49ee23
AP
760 /*
761 * This might be misleading, because h_errno is used as if
762 * it was errno. To minimize mixup add 1000. Underlying
763 * reason for this is that hstrerror is declared obsolete,
764 * not to mention that a) h_errno is not always guaranteed
69687aa8 765 * to be meaningless; b) hstrerror can reside in yet another
3e49ee23
AP
766 * library, linking for sake of hstrerror is an overkill;
767 * c) this path is not executed on contemporary systems
768 * anyway [above getaddrinfo/gai_strerror is]. We just let
769 * system administrator figure this out...
770 */
771 SYSerr(SYS_F_GETHOSTBYNAME, 1000 + h_errno);
c86d1f19
KR
772#else
773 SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
774#endif
28a0841b
RL
775 ret = 0;
776 goto err;
777 }
778 }
779
780 if (service == NULL) {
781 se_fallback.s_port = 0;
622c7e99 782 se_fallback.s_proto = NULL;
28a0841b
RL
783 se = &se_fallback;
784 } else {
785 char *endp = NULL;
786 long portnum = strtol(service, &endp, 10);
fcd9c8c0
RL
787
788/*
789 * Because struct servent is defined for 32-bit pointers only with
790 * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
791 */
792#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
793# pragma pointer_size save
794# pragma pointer_size 32
795#endif
622c7e99 796 char *proto = NULL;
fcd9c8c0
RL
797#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
798# pragma pointer_size restore
799#endif
622c7e99
RL
800
801 switch (socktype) {
802 case SOCK_STREAM:
803 proto = "tcp";
804 break;
805 case SOCK_DGRAM:
806 proto = "udp";
807 break;
808 }
28a0841b
RL
809
810 if (endp != service && *endp == '\0'
811 && portnum > 0 && portnum < 65536) {
812 se_fallback.s_port = htons(portnum);
813 se_fallback.s_proto = proto;
814 se = &se_fallback;
815 } else if (endp == service) {
28a0841b
RL
816 se = getservbyname(service, proto);
817
818 if (se == NULL) {
c86d1f19 819#ifndef OPENSSL_SYS_WINDOWS
3e49ee23 820 SYSerr(SYS_F_GETSERVBYNAME, errno);
c86d1f19
KR
821#else
822 SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
823#endif
28a0841b
RL
824 goto err;
825 }
826 } else {
827 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
828 goto err;
829 }
830 }
831
832 *res = NULL;
833
834 {
fcd9c8c0
RL
835/*
836 * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
837 * we must make sure our iterator designates the same element type, hence
838 * the pointer size dance.
839 */
840#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
841# pragma pointer_size save
842# pragma pointer_size 32
843#endif
28a0841b 844 char **addrlistp;
fcd9c8c0
RL
845#if defined(OPENSSL_SYS_VMS) && defined(__DECC)
846# pragma pointer_size restore
847#endif
28a0841b
RL
848 size_t addresses;
849 BIO_ADDRINFO *tmp_bai = NULL;
850
851 /* The easiest way to create a linked list from an
852 array is to start from the back */
853 for(addrlistp = he->h_addr_list; *addrlistp != NULL;
854 addrlistp++)
855 ;
856
857 for(addresses = addrlistp - he->h_addr_list;
858 addrlistp--, addresses-- > 0; ) {
859 if (!addrinfo_wrap(he->h_addrtype, socktype,
860 *addrlistp, he->h_length,
861 se->s_port, &tmp_bai))
862 goto addrinfo_malloc_err;
863 tmp_bai->bai_next = *res;
864 *res = tmp_bai;
865 continue;
866 addrinfo_malloc_err:
867 BIO_ADDRINFO_free(*res);
868 *res = NULL;
869 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
870 ret = 0;
871 goto err;
872 }
873
874 ret = 1;
875 }
876 err:
f989cd8c 877 CRYPTO_THREAD_unlock(bio_lookup_lock);
28a0841b
RL
878 }
879
880 return ret;
881}
ff234405 882
1288f26f 883#endif /* OPENSSL_NO_SOCK */