bb6719eb2a3c24bb87515b5c49f5f860fea9f87d
[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)
232             *hostname = OPENSSL_strdup(host);
233         if (service)
234             *service = OPENSSL_strdup(serv);
235     } else {
236 #endif
237         if (hostname)
238             *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr));
239         if (service) {
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     return 1;
247 }
248
249 char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric)
250 {
251     char *hostname = NULL;
252
253     if (addr_strings(ap, numeric, &hostname, NULL))
254         return hostname;
255
256     return NULL;
257 }
258
259 char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
260 {
261     char *service = NULL;
262
263     if (addr_strings(ap, numeric, NULL, &service))
264         return service;
265
266     return NULL;
267 }
268
269 char *BIO_ADDR_path_string(const BIO_ADDR *ap)
270 {
271 #ifdef AF_UNIX
272     if (ap->sa.sa_family == AF_UNIX)
273         return OPENSSL_strdup(ap->s_un.sun_path);
274 #endif
275     return NULL;
276 }
277
278 /*
279  * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr
280  * for a given BIO_ADDR.  In reality, this is simply a type safe cast.
281  * The returned struct sockaddr is const, so it can't be tampered with.
282  */
283 const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap)
284 {
285     return &(ap->sa);
286 }
287
288 /*
289  * BIO_ADDR_sockaddr_noconst - non-public function that does the same
290  * as BIO_ADDR_sockaddr, but returns a non-const.  USE WITH CARE, as
291  * it allows you to tamper with the data (and thereby the contents
292  * of the input BIO_ADDR).
293  */
294 struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap)
295 {
296     return &(ap->sa);
297 }
298
299 /*
300  * BIO_ADDR_sockaddr_size - non-public function that returns the size
301  * of the struct sockaddr the BIO_ADDR is using.  If the protocol family
302  * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX,
303  * the size of the BIO_ADDR type is returned.
304  */
305 socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap)
306 {
307     if (ap->sa.sa_family == AF_INET)
308         return sizeof(ap->s_in);
309 #ifdef AF_INET6
310     if (ap->sa.sa_family == AF_INET6)
311         return sizeof(ap->s_in6);
312 #endif
313 #ifdef AF_UNIX
314     if (ap->sa.sa_family == AF_UNIX)
315         return sizeof(ap->s_un);
316 #endif
317     return sizeof(*ap);
318 }
319
320 /**********************************************************************
321  *
322  * Address info database
323  *
324  */
325
326 const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
327 {
328     if (bai != NULL)
329         return bai->bai_next;
330     return NULL;
331 }
332
333 int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
334 {
335     if (bai != NULL)
336         return bai->bai_family;
337     return 0;
338 }
339
340 int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
341 {
342     if (bai != NULL)
343         return bai->bai_socktype;
344     return 0;
345 }
346
347 int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
348 {
349     if (bai != NULL) {
350         if (bai->bai_protocol != 0)
351             return bai->bai_protocol;
352
353 #ifdef AF_UNIX
354         if (bai->bai_family == AF_UNIX)
355             return 0;
356 #endif
357
358         switch (bai->bai_socktype) {
359         case SOCK_STREAM:
360             return IPPROTO_TCP;
361         case SOCK_DGRAM:
362             return IPPROTO_UDP;
363         default:
364             break;
365         }
366     }
367     return 0;
368 }
369
370 /*
371  * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size
372  * of the struct sockaddr inside the BIO_ADDRINFO.
373  */
374 socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai)
375 {
376     if (bai != NULL)
377         return bai->bai_addrlen;
378     return 0;
379 }
380
381 /*
382  * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr
383  * as the struct sockaddr it is.
384  */
385 const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai)
386 {
387     if (bai != NULL)
388         return bai->bai_addr;
389     return NULL;
390 }
391
392 const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
393 {
394     if (bai != NULL)
395         return (BIO_ADDR *)bai->bai_addr;
396     return NULL;
397 }
398
399 void BIO_ADDRINFO_free(BIO_ADDRINFO *bai)
400 {
401     if (bai == NULL)
402         return;
403
404 #ifdef AI_PASSIVE
405 # ifdef AF_UNIX
406 #  define _cond bai->bai_family != AF_UNIX
407 # else
408 #  define _cond 1
409 # endif
410     if (_cond) {
411         freeaddrinfo(bai);
412         return;
413     }
414 #endif
415
416     /* Free manually when we know that addrinfo_wrap() was used.
417      * See further comment above addrinfo_wrap()
418      */
419     while (bai != NULL) {
420         BIO_ADDRINFO *next = bai->bai_next;
421         OPENSSL_free(bai->bai_addr);
422         OPENSSL_free(bai);
423         bai = next;
424     }
425 }
426
427 /**********************************************************************
428  *
429  * Service functions
430  *
431  */
432
433 /*-
434  * The specs in hostserv can take these forms:
435  *
436  * host:service         => *host = "host", *service = "service"
437  * host:*               => *host = "host", *service = NULL
438  * host:                => *host = "host", *service = NULL
439  * :service             => *host = NULL, *service = "service"
440  * *:service            => *host = NULL, *service = "service"
441  *
442  * in case no : is present in the string, the result depends on
443  * hostserv_prio, as follows:
444  *
445  * when hostserv_prio == BIO_PARSE_PRIO_HOST
446  * host                 => *host = "host", *service untouched
447  *
448  * when hostserv_prio == BIO_PARSE_PRIO_SERV
449  * service              => *host untouched, *service = "service"
450  *
451  */
452 int BIO_parse_hostserv(const char *hostserv, char **host, char **service,
453                        enum BIO_hostserv_priorities hostserv_prio)
454 {
455     const char *h = NULL; size_t hl = 0;
456     const char *p = NULL; size_t pl = 0;
457
458     if (*hostserv == '[') {
459         if ((p = strchr(hostserv, ']')) == NULL)
460             goto spec_err;
461         h = hostserv + 1;
462         hl = p - h;
463         p++;
464         if (*p == '\0')
465             p = NULL;
466         else if (*p != ':')
467             goto spec_err;
468         else {
469             p++;
470             pl = strlen(p);
471         }
472     } else {
473         const char *p2 = strrchr(hostserv, ':');
474         p = strchr(hostserv, ':');
475
476         /*-
477          * Check for more than one colon.  There are three possible
478          * interpretations:
479          * 1. IPv6 address with port number, last colon being separator.
480          * 2. IPv6 address only.
481          * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST,
482          *    IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV
483          * Because of this ambiguity, we currently choose to make it an
484          * error.
485          */
486         if (p != p2)
487             goto amb_err;
488
489         if (p != NULL) {
490             h = hostserv;
491             hl = p - h;
492             p++;
493             pl = strlen(p);
494         } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) {
495             h = hostserv;
496             hl = strlen(h);
497         } else {
498             p = hostserv;
499             pl = strlen(p);
500         }
501     }
502
503     if (p != NULL && strchr(p, ':'))
504         goto spec_err;
505
506     if (h != NULL && host != NULL) {
507         if (hl == 0
508             || (hl == 1 && h[0] == '*')) {
509             *host = NULL;
510         } else {
511             *host = OPENSSL_strndup(h, hl);
512             if (*host == NULL)
513                 goto memerr;
514         }
515     }
516     if (p != NULL && service != NULL) {
517         if (pl == 0
518             || (pl == 1 && p[0] == '*')) {
519             *service = NULL;
520         } else {
521             *service = OPENSSL_strndup(p, pl);
522             if (*service == NULL)
523                 goto memerr;
524         }
525     }
526
527     return 1;
528  amb_err:
529     BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_AMBIGUOUS_HOST_OR_SERVICE);
530     return 0;
531  spec_err:
532     BIOerr(BIO_F_BIO_PARSE_HOSTSERV, BIO_R_MALFORMED_HOST_OR_SERVICE);
533     return 0;
534  memerr:
535     BIOerr(BIO_F_BIO_PARSE_HOSTSERV, ERR_R_MALLOC_FAILURE);
536     return 0;
537 }
538
539 /* addrinfo_wrap is used to build our own addrinfo "chain".
540  * (it has only one entry, so calling it a chain may be a stretch)
541  * It should ONLY be called when getaddrinfo() and friends
542  * aren't available, OR when dealing with a non IP protocol
543  * family, such as AF_UNIX
544  *
545  * the return value is 1 on success, or 0 on failure, which
546  * only happens if a memory allocation error occurred.
547  */
548 static int addrinfo_wrap(int family, int socktype,
549                          const void *where, size_t wherelen,
550                          unsigned short port,
551                          BIO_ADDRINFO **bai)
552 {
553     OPENSSL_assert(bai != NULL);
554
555     *bai = OPENSSL_zalloc(sizeof(**bai));
556     if (*bai == NULL)
557         return 0;
558
559     (*bai)->bai_family = family;
560     (*bai)->bai_socktype = socktype;
561     if (socktype == SOCK_STREAM)
562         (*bai)->bai_protocol = IPPROTO_TCP;
563     if (socktype == SOCK_DGRAM)
564         (*bai)->bai_protocol = IPPROTO_UDP;
565 #ifdef AF_UNIX
566     if (family == AF_UNIX)
567         (*bai)->bai_protocol = 0;
568 #endif
569     {
570         /* Magic: We know that BIO_ADDR_sockaddr_noconst is really
571            just an advanced cast of BIO_ADDR* to struct sockaddr *
572            by the power of union, so while it may seem that we're
573            creating a memory leak here, we are not.  It will be
574            all right. */
575         BIO_ADDR *addr = BIO_ADDR_new();
576         if (addr != NULL) {
577             BIO_ADDR_rawmake(addr, family, where, wherelen, port);
578             (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr);
579         }
580     }
581     (*bai)->bai_next = NULL;
582     if ((*bai)->bai_addr == NULL) {
583         BIO_ADDRINFO_free(*bai);
584         *bai = NULL;
585         return 0;
586     }
587     return 1;
588 }
589
590 static void do_bio_lookup_init(void)
591 {
592     bio_lookup_lock = CRYPTO_THREAD_lock_new();
593 }
594
595 /*-
596  * BIO_lookup - look up the node and service you want to connect to.
597  * @node: the node you want to connect to.
598  * @service: the service you want to connect to.
599  * @lookup_type: declare intent with the result, client or server.
600  * @family: the address family you want to use.  Use AF_UNSPEC for any, or
601  *  AF_INET, AF_INET6 or AF_UNIX.
602  * @socktype: The socket type you want to use.  Can be SOCK_STREAM, SOCK_DGRAM
603  *  or 0 for all.
604  * @res: Storage place for the resulting list of returned addresses
605  *
606  * This will do a lookup of the node and service that you want to connect to.
607  * It returns a linked list of different addresses you can try to connect to.
608  *
609  * When no longer needed you should call BIO_ADDRINFO_free() to free the result.
610  *
611  * The return value is 1 on success or 0 in case of error.
612  */
613 int BIO_lookup(const char *host, const char *service,
614                enum BIO_lookup_type lookup_type,
615                int family, int socktype, BIO_ADDRINFO **res)
616 {
617     int ret = 0;                 /* Assume failure */
618
619     switch(family) {
620     case AF_INET:
621 #ifdef AF_INET6
622     case AF_INET6:
623 #endif
624 #ifdef AF_UNIX
625     case AF_UNIX:
626 #endif
627 #ifdef AF_UNSPEC
628     case AF_UNSPEC:
629 #endif
630         break;
631     default:
632         BIOerr(BIO_F_BIO_LOOKUP, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY);
633         return 0;
634     }
635
636 #ifdef AF_UNIX
637     if (family == AF_UNIX) {
638         if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res))
639             return 1;
640         else
641             BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
642         return 0;
643     }
644 #endif
645
646     if (BIO_sock_init() != 1)
647         return 0;
648
649     if (1) {
650         int gai_ret = 0;
651 #ifdef AI_PASSIVE
652         struct addrinfo hints;
653         memset(&hints, 0, sizeof hints);
654
655 # ifdef AI_ADDRCONFIG
656         hints.ai_flags = AI_ADDRCONFIG;
657 # endif
658         hints.ai_family = family;
659         hints.ai_socktype = socktype;
660
661         if (lookup_type == BIO_LOOKUP_SERVER)
662             hints.ai_flags |= AI_PASSIVE;
663
664         /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
665          * macro magic in bio_lcl.h
666          */
667         switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
668 # ifdef EAI_SYSTEM
669         case EAI_SYSTEM:
670             SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
671             BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
672             break;
673 # endif
674         case 0:
675             ret = 1;             /* Success */
676             break;
677         default:
678             BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
679             ERR_add_error_data(1, gai_strerror(gai_ret));
680             break;
681         }
682     } else {
683 #endif
684         const struct hostent *he;
685 /*
686  * Because struct hostent is defined for 32-bit pointers only with
687  * VMS C, we need to make sure that '&he_fallback_address' and
688  * '&he_fallback_addresses' are 32-bit pointers
689  */
690 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
691 # pragma pointer_size save
692 # pragma pointer_size 32
693 #endif
694         /* Windows doesn't seem to have in_addr_t */
695 #ifdef OPENSSL_SYS_WINDOWS
696         static uint32_t he_fallback_address;
697         static const uint32_t *he_fallback_addresses[] =
698             { &he_fallback_address, NULL };
699 #else
700         static in_addr_t he_fallback_address;
701         static const in_addr_t *he_fallback_addresses[] =
702             { &he_fallback_address, NULL };
703 #endif
704         static const struct hostent he_fallback =
705             { NULL, NULL, AF_INET, sizeof(he_fallback_address),
706               (char **)&he_fallback_addresses };
707 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
708 # pragma pointer_size restore
709 #endif
710
711         struct servent *se;
712         /* Apparently, on WIN64, s_proto and s_port have traded places... */
713 #ifdef _WIN64
714         struct servent se_fallback = { NULL, NULL, NULL, 0 };
715 #else
716         struct servent se_fallback = { NULL, NULL, 0, NULL };
717 #endif
718
719         CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
720
721         CRYPTO_THREAD_write_lock(bio_lookup_lock);
722         he_fallback_address = INADDR_ANY;
723         if (host == NULL) {
724             he = &he_fallback;
725             switch(lookup_type) {
726             case BIO_LOOKUP_CLIENT:
727                 he_fallback_address = INADDR_LOOPBACK;
728                 break;
729             case BIO_LOOKUP_SERVER:
730                 he_fallback_address = INADDR_ANY;
731                 break;
732             default:
733                 OPENSSL_assert(("We forgot to handle a lookup type!" == 0));
734                 break;
735             }
736         } else {
737             he = gethostbyname(host);
738
739             if (he == NULL) {
740 #ifndef OPENSSL_SYS_WINDOWS
741                 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
742                 ERR_add_error_data(1, hstrerror(h_errno));
743 #else
744                 SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
745 #endif
746                 ret = 0;
747                 goto err;
748             }
749         }
750
751         if (service == NULL) {
752             se_fallback.s_port = 0;
753             se_fallback.s_proto = NULL;
754             se = &se_fallback;
755         } else {
756             char *endp = NULL;
757             long portnum = strtol(service, &endp, 10);
758
759 /*
760  * Because struct servent is defined for 32-bit pointers only with
761  * VMS C, we need to make sure that 'proto' is a 32-bit pointer.
762  */
763 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
764 # pragma pointer_size save
765 # pragma pointer_size 32
766 #endif
767             char *proto = NULL;
768 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
769 # pragma pointer_size restore
770 #endif
771
772             switch (socktype) {
773             case SOCK_STREAM:
774                 proto = "tcp";
775                 break;
776             case SOCK_DGRAM:
777                 proto = "udp";
778                 break;
779             }
780
781             if (endp != service && *endp == '\0'
782                     && portnum > 0 && portnum < 65536) {
783                 se_fallback.s_port = htons(portnum);
784                 se_fallback.s_proto = proto;
785                 se = &se_fallback;
786             } else if (endp == service) {
787                 se = getservbyname(service, proto);
788
789                 if (se == NULL) {
790 #ifndef OPENSSL_SYS_WINDOWS
791                     BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
792                     ERR_add_error_data(1, hstrerror(h_errno));
793 #else
794                     SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
795 #endif
796                     goto err;
797                 }
798             } else {
799                 BIOerr(BIO_F_BIO_LOOKUP, BIO_R_MALFORMED_HOST_OR_SERVICE);
800                 goto err;
801             }
802         }
803
804         *res = NULL;
805
806         {
807 /*
808  * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C,
809  * we must make sure our iterator designates the same element type, hence
810  * the pointer size dance.
811  */
812 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
813 # pragma pointer_size save
814 # pragma pointer_size 32
815 #endif
816             char **addrlistp;
817 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
818 # pragma pointer_size restore
819 #endif
820             size_t addresses;
821             BIO_ADDRINFO *tmp_bai = NULL;
822
823             /* The easiest way to create a linked list from an
824                array is to start from the back */
825             for(addrlistp = he->h_addr_list; *addrlistp != NULL;
826                 addrlistp++)
827                 ;
828
829             for(addresses = addrlistp - he->h_addr_list;
830                 addrlistp--, addresses-- > 0; ) {
831                 if (!addrinfo_wrap(he->h_addrtype, socktype,
832                                    *addrlistp, he->h_length,
833                                    se->s_port, &tmp_bai))
834                     goto addrinfo_malloc_err;
835                 tmp_bai->bai_next = *res;
836                 *res = tmp_bai;
837                 continue;
838              addrinfo_malloc_err:
839                 BIO_ADDRINFO_free(*res);
840                 *res = NULL;
841                 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_MALLOC_FAILURE);
842                 ret = 0;
843                 goto err;
844             }
845
846             ret = 1;
847         }
848      err:
849         CRYPTO_THREAD_unlock(bio_lookup_lock);
850     }
851
852     return ret;
853 }
854
855 #endif /* OPENSSL_NO_SOCK */