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