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