Refactoring BIO: add wrappers around sockaddr et al
[openssl.git] / crypto / bio / b_sock.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <errno.h>
61 #include "bio_lcl.h"
62 #if defined(NETWARE_CLIB)
63 # include <sys/ioctl.h>
64 NETDB_DEFINE_CONTEXT
65 #endif
66 #ifndef OPENSSL_NO_SOCK
67 # include <openssl/dso.h>
68 # define SOCKET_PROTOCOL IPPROTO_TCP
69 # ifdef SO_MAXCONN
70 #  define MAX_LISTEN  SO_MAXCONN
71 # elif defined(SOMAXCONN)
72 #  define MAX_LISTEN  SOMAXCONN
73 # else
74 #  define MAX_LISTEN  32
75 # endif
76 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
77 static int wsa_init_done = 0;
78 # endif
79
80 /*
81  * WSAAPI specifier is required to make indirect calls to run-time
82  * linked WinSock 2 functions used in this module, to be specific
83  * [get|free]addrinfo and getnameinfo. This is because WinSock uses
84  * uses non-C calling convention, __stdcall vs. __cdecl, on x86
85  * Windows. On non-WinSock platforms WSAAPI needs to be void.
86  */
87 # ifndef WSAAPI
88 #  define WSAAPI
89 # endif
90
91 static int get_ip(const char *str, unsigned char *ip);
92 int BIO_get_host_ip(const char *str, unsigned char *ip)
93 {
94     int i;
95     int err = 1;
96     int locked = 0;
97     struct hostent *he;
98
99     i = get_ip(str, ip);
100     if (i < 0) {
101         BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_INVALID_IP_ADDRESS);
102         goto err;
103     }
104
105     /*
106      * At this point, we have something that is most probably correct in some
107      * way, so let's init the socket.
108      */
109     if (BIO_sock_init() != 1)
110         return 0;               /* don't generate another error code here */
111
112     /*
113      * If the string actually contained an IP address, we need not do
114      * anything more
115      */
116     if (i > 0)
117         return (1);
118
119     /* do a gethostbyname */
120     CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
121     locked = 1;
122     he = BIO_gethostbyname(str);
123     if (he == NULL) {
124         BIOerr(BIO_F_BIO_GET_HOST_IP, BIO_R_BAD_HOSTNAME_LOOKUP);
125         goto err;
126     }
127
128     if (he->h_addrtype != AF_INET) {
129         BIOerr(BIO_F_BIO_GET_HOST_IP,
130                BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
131         goto err;
132     }
133     for (i = 0; i < 4; i++)
134         ip[i] = he->h_addr_list[0][i];
135     err = 0;
136
137  err:
138     if (locked)
139         CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
140     if (err) {
141         ERR_add_error_data(2, "host=", str);
142         return 0;
143     } else
144         return 1;
145 }
146
147 int BIO_get_port(const char *str, unsigned short *port_ptr)
148 {
149     int i;
150     struct servent *s;
151
152     if (str == NULL) {
153         BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
154         return (0);
155     }
156     i = atoi(str);
157     if (i != 0)
158         *port_ptr = (unsigned short)i;
159     else {
160         CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
161         /*
162          * Note: under VMS with SOCKETSHR, it seems like the first parameter
163          * is 'char *', instead of 'const char *'
164          */
165 # ifndef CONST_STRICT
166         s = getservbyname((char *)str, "tcp");
167 # else
168         s = getservbyname(str, "tcp");
169 # endif
170         if (s != NULL)
171             *port_ptr = ntohs((unsigned short)s->s_port);
172         CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
173         if (s == NULL) {
174             if (strcmp(str, "http") == 0)
175                 *port_ptr = 80;
176             else if (strcmp(str, "telnet") == 0)
177                 *port_ptr = 23;
178             else if (strcmp(str, "socks") == 0)
179                 *port_ptr = 1080;
180             else if (strcmp(str, "https") == 0)
181                 *port_ptr = 443;
182             else if (strcmp(str, "ssl") == 0)
183                 *port_ptr = 443;
184             else if (strcmp(str, "ftp") == 0)
185                 *port_ptr = 21;
186             else if (strcmp(str, "gopher") == 0)
187                 *port_ptr = 70;
188             else {
189                 SYSerr(SYS_F_GETSERVBYNAME, get_last_socket_error());
190                 ERR_add_error_data(3, "service='", str, "'");
191                 return (0);
192             }
193         }
194     }
195     return (1);
196 }
197
198 int BIO_sock_error(int sock)
199 {
200     int j, i;
201     union {
202         size_t s;
203         int i;
204     } size;
205
206     /* heuristic way to adapt for platforms that expect 64-bit optlen */
207     size.s = 0, size.i = sizeof(j);
208     /*
209      * Note: under Windows the third parameter is of type (char *) whereas
210      * under other systems it is (void *) if you don't have a cast it will
211      * choke the compiler: if you do have a cast then you can either go for
212      * (char *) or (void *).
213      */
214     i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, (void *)&size);
215     if (i < 0)
216         return (1);
217     else
218         return (j);
219 }
220
221 struct hostent *BIO_gethostbyname(const char *name)
222 {
223     /*
224      * Caching gethostbyname() results forever is wrong, so we have to let
225      * the true gethostbyname() worry about this
226      */
227 # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
228     return gethostbyname((char *)name);
229 # else
230     return gethostbyname(name);
231 # endif
232 }
233
234 int BIO_sock_init(void)
235 {
236 # ifdef OPENSSL_SYS_WINDOWS
237     static struct WSAData wsa_state;
238
239     if (!wsa_init_done) {
240         int err;
241
242         wsa_init_done = 1;
243         memset(&wsa_state, 0, sizeof(wsa_state));
244         /*
245          * Not making wsa_state available to the rest of the code is formally
246          * wrong. But the structures we use are [beleived to be] invariable
247          * among Winsock DLLs, while API availability is [expected to be]
248          * probed at run-time with DSO_global_lookup.
249          */
250         if (WSAStartup(0x0202, &wsa_state) != 0) {
251             err = WSAGetLastError();
252             SYSerr(SYS_F_WSASTARTUP, err);
253             BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
254             return (-1);
255         }
256     }
257 # endif                         /* OPENSSL_SYS_WINDOWS */
258 # ifdef WATT32
259     extern int _watt_do_exit;
260     _watt_do_exit = 0;          /* don't make sock_init() call exit() */
261     if (sock_init())
262         return (-1);
263 # endif
264
265 # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
266     WORD wVerReq;
267     WSADATA wsaData;
268     int err;
269
270     if (!wsa_init_done) {
271         wsa_init_done = 1;
272         wVerReq = MAKEWORD(2, 0);
273         err = WSAStartup(wVerReq, &wsaData);
274         if (err != 0) {
275             SYSerr(SYS_F_WSASTARTUP, err);
276             BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
277             return (-1);
278         }
279     }
280 # endif
281
282     return (1);
283 }
284
285 void BIO_sock_cleanup(void)
286 {
287 # ifdef OPENSSL_SYS_WINDOWS
288     if (wsa_init_done) {
289         wsa_init_done = 0;
290         WSACleanup();
291     }
292 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
293     if (wsa_init_done) {
294         wsa_init_done = 0;
295         WSACleanup();
296     }
297 # endif
298 }
299
300 # if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
301
302 int BIO_socket_ioctl(int fd, long type, void *arg)
303 {
304     int i;
305
306 #  ifdef __DJGPP__
307     i = ioctlsocket(fd, type, (char *)arg);
308 #  else
309 #   if defined(OPENSSL_SYS_VMS)
310     /*-
311      * 2011-02-18 SMS.
312      * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
313      * observe that all the consumers pass in an "unsigned long *",
314      * so we arrange a local copy with a short pointer, and use
315      * that, instead.
316      */
317 #    if __INITIAL_POINTER_SIZE == 64
318 #     define ARG arg_32p
319 #     pragma pointer_size save
320 #     pragma pointer_size 32
321     unsigned long arg_32;
322     unsigned long *arg_32p;
323 #     pragma pointer_size restore
324     arg_32p = &arg_32;
325     arg_32 = *((unsigned long *)arg);
326 #    else                       /* __INITIAL_POINTER_SIZE == 64 */
327 #     define ARG arg
328 #    endif                      /* __INITIAL_POINTER_SIZE == 64 [else] */
329 #   else                        /* defined(OPENSSL_SYS_VMS) */
330 #    define ARG arg
331 #   endif                       /* defined(OPENSSL_SYS_VMS) [else] */
332
333     i = ioctlsocket(fd, type, ARG);
334 #  endif                        /* __DJGPP__ */
335     if (i < 0)
336         SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
337     return (i);
338 }
339 # endif                         /* __VMS_VER */
340
341 /*
342  * The reason I have implemented this instead of using sscanf is because
343  * Visual C 1.52c gives an unresolved external when linking a DLL :-(
344  */
345 static int get_ip(const char *str, unsigned char ip[4])
346 {
347     unsigned int tmp[4];
348     int num = 0, c, ok = 0;
349
350     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
351
352     for (;;) {
353         c = *(str++);
354         if ((c >= '0') && (c <= '9')) {
355             ok = 1;
356             tmp[num] = tmp[num] * 10 + c - '0';
357             if (tmp[num] > 255)
358                 return (0);
359         } else if (c == '.') {
360             if (!ok)
361                 return (-1);
362             if (num == 3)
363                 return (0);
364             num++;
365             ok = 0;
366         } else if (c == '\0' && (num == 3) && ok)
367             break;
368         else
369             return (0);
370     }
371     ip[0] = tmp[0];
372     ip[1] = tmp[1];
373     ip[2] = tmp[2];
374     ip[3] = tmp[3];
375     return (1);
376 }
377
378 int BIO_get_accept_socket(char *host, int bind_mode)
379 {
380     int ret = 0;
381     union {
382         struct sockaddr sa;
383         struct sockaddr_in sa_in;
384 # if OPENSSL_USE_IPV6
385         struct sockaddr_in6 sa_in6;
386 # endif
387     } server, client;
388     int s = (int)INVALID_SOCKET, cs, addrlen;
389     unsigned char ip[4];
390     unsigned short port;
391     char *str = NULL, *e;
392     char *h, *p;
393     unsigned long l;
394     int err_num;
395
396     if (BIO_sock_init() != 1)
397         return ((int)INVALID_SOCKET);
398
399     if ((str = OPENSSL_strdup(host)) == NULL)
400         return ((int)INVALID_SOCKET);
401
402     h = p = NULL;
403     h = str;
404     for (e = str; *e; e++) {
405         if (*e == ':') {
406             p = e;
407         } else if (*e == '/') {
408             *e = '\0';
409             break;
410         }
411     }
412     if (p)
413         *p++ = '\0';            /* points at last ':', '::port' is special
414                                  * [see below] */
415     else
416         p = h, h = NULL;
417
418 # ifdef EAI_FAMILY
419     do {
420         static union {
421             void *p;
422             int (WSAAPI *f) (const char *, const char *,
423                              const struct addrinfo *, struct addrinfo **);
424         } p_getaddrinfo = {
425             NULL
426         };
427         static union {
428             void *p;
429             void (WSAAPI *f) (struct addrinfo *);
430         } p_freeaddrinfo = {
431             NULL
432         };
433         struct addrinfo *res, hint;
434
435         if (p_getaddrinfo.p == NULL) {
436             if ((p_getaddrinfo.p = DSO_global_lookup("getaddrinfo")) == NULL
437                 || (p_freeaddrinfo.p =
438                     DSO_global_lookup("freeaddrinfo")) == NULL)
439                 p_getaddrinfo.p = (void *)-1;
440         }
441         if (p_getaddrinfo.p == (void *)-1)
442             break;
443
444         /*
445          * '::port' enforces IPv6 wildcard listener. Some OSes, e.g. Solaris,
446          * default to IPv6 without any hint. Also note that commonly IPv6
447          * wildchard socket can service IPv4 connections just as well...
448          */
449         memset(&hint, 0, sizeof(hint));
450         hint.ai_flags = AI_PASSIVE;
451         if (h) {
452             if (strchr(h, ':')) {
453                 if (h[1] == '\0')
454                     h = NULL;
455 #  if OPENSSL_USE_IPV6
456                 hint.ai_family = AF_INET6;
457 #  else
458                 h = NULL;
459 #  endif
460             } else if (h[0] == '*' && h[1] == '\0') {
461                 hint.ai_family = AF_INET;
462                 h = NULL;
463             }
464         }
465
466         if ((*p_getaddrinfo.f) (h, p, &hint, &res))
467             break;
468
469         addrlen = res->ai_addrlen <= sizeof(server) ?
470             res->ai_addrlen : sizeof(server);
471         memcpy(&server, res->ai_addr, addrlen);
472
473         (*p_freeaddrinfo.f) (res);
474         goto again;
475     } while (0);
476 # endif
477
478     if (!BIO_get_port(p, &port))
479         goto err;
480
481     memset(&server, 0, sizeof(server));
482     server.sa_in.sin_family = AF_INET;
483     server.sa_in.sin_port = htons(port);
484     addrlen = sizeof(server.sa_in);
485
486     if (h == NULL || strcmp(h, "*") == 0)
487         server.sa_in.sin_addr.s_addr = INADDR_ANY;
488     else {
489         if (!BIO_get_host_ip(h, &(ip[0])))
490             goto err;
491         l = (unsigned long)
492             ((unsigned long)ip[0] << 24L) |
493             ((unsigned long)ip[1] << 16L) |
494             ((unsigned long)ip[2] << 8L) | ((unsigned long)ip[3]);
495         server.sa_in.sin_addr.s_addr = htonl(l);
496     }
497
498  again:
499     s = socket(server.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL);
500     if (s == (int)INVALID_SOCKET) {
501         SYSerr(SYS_F_SOCKET, get_last_socket_error());
502         ERR_add_error_data(3, "port='", host, "'");
503         BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET);
504         goto err;
505     }
506 # ifdef SO_REUSEADDR
507     if (bind_mode == BIO_BIND_REUSEADDR) {
508         int i = 1;
509
510         ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i));
511         bind_mode = BIO_BIND_NORMAL;
512     }
513 # endif
514     if (bind(s, &server.sa, addrlen) == -1) {
515 # ifdef SO_REUSEADDR
516         err_num = get_last_socket_error();
517         if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
518 #  ifdef OPENSSL_SYS_WINDOWS
519             /*
520              * Some versions of Windows define EADDRINUSE to a dummy value.
521              */
522             (err_num == WSAEADDRINUSE))
523 #  else
524             (err_num == EADDRINUSE))
525 #  endif
526         {
527             client = server;
528             if (h == NULL || strcmp(h, "*") == 0) {
529 #  if OPENSSL_USE_IPV6
530                 if (client.sa.sa_family == AF_INET6) {
531                     memset(&client.sa_in6.sin6_addr, 0,
532                            sizeof(client.sa_in6.sin6_addr));
533                     client.sa_in6.sin6_addr.s6_addr[15] = 1;
534                 } else
535 #  endif
536                 if (client.sa.sa_family == AF_INET) {
537                     client.sa_in.sin_addr.s_addr = htonl(0x7F000001);
538                 } else
539                     goto err;
540             }
541             cs = socket(client.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL);
542             if (cs != (int)INVALID_SOCKET) {
543                 int ii;
544                 ii = connect(cs, &client.sa, addrlen);
545                 closesocket(cs);
546                 if (ii == (int)INVALID_SOCKET) {
547                     bind_mode = BIO_BIND_REUSEADDR;
548                     closesocket(s);
549                     goto again;
550                 }
551                 /* else error */
552             }
553             /* else error */
554         }
555 # endif
556         SYSerr(SYS_F_BIND, err_num);
557         ERR_add_error_data(3, "port='", host, "'");
558         BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_BIND_SOCKET);
559         goto err;
560     }
561     if (listen(s, MAX_LISTEN) == -1) {
562         SYSerr(SYS_F_BIND, get_last_socket_error());
563         ERR_add_error_data(3, "port='", host, "'");
564         BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_LISTEN_SOCKET);
565         goto err;
566     }
567     ret = 1;
568  err:
569     OPENSSL_free(str);
570     if ((ret == 0) && (s != (int)INVALID_SOCKET)) {
571         closesocket(s);
572         s = (int)INVALID_SOCKET;
573     }
574     return (s);
575 }
576
577 int BIO_accept(int sock, char **addr)
578 {
579     int ret = (int)INVALID_SOCKET;
580     unsigned long l;
581     unsigned short port;
582     char *p;
583
584     struct {
585         /*
586          * As for following union. Trouble is that there are platforms
587          * that have socklen_t and there are platforms that don't, on
588          * some platforms socklen_t is int and on some size_t. So what
589          * one can do? One can cook #ifdef spaghetti, which is nothing
590          * but masochistic. Or one can do union between int and size_t.
591          * One naturally does it primarily for 64-bit platforms where
592          * sizeof(int) != sizeof(size_t). But would it work? Note that
593          * if size_t member is initialized to 0, then later int member
594          * assignment naturally does the job on little-endian platforms
595          * regardless accept's expectations! What about big-endians?
596          * If accept expects int*, then it works, and if size_t*, then
597          * length value would appear as unreasonably large. But this
598          * won't prevent it from filling in the address structure. The
599          * trouble of course would be if accept returns more data than
600          * actual buffer can accomodate and overwrite stack... That's
601          * where early OPENSSL_assert comes into picture. Besides, the
602          * only 64-bit big-endian platform found so far that expects
603          * size_t* is HP-UX, where stack grows towards higher address.
604          * <appro>
605          */
606         union {
607             size_t s;
608             int i;
609         } len;
610         union {
611             struct sockaddr sa;
612             struct sockaddr_in sa_in;
613 # if OPENSSL_USE_IPV6
614             struct sockaddr_in6 sa_in6;
615 # endif
616         } from;
617     } sa;
618
619     sa.len.s = 0;
620     sa.len.i = sizeof(sa.from);
621     memset(&sa.from, 0, sizeof(sa.from));
622     ret = accept(sock, &sa.from.sa, (void *)&sa.len);
623     if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
624         OPENSSL_assert(sa.len.s <= sizeof(sa.from));
625         sa.len.i = (int)sa.len.s;
626         /* use sa.len.i from this point */
627     }
628     if (ret == (int)INVALID_SOCKET) {
629         if (BIO_sock_should_retry(ret))
630             return -2;
631         SYSerr(SYS_F_ACCEPT, get_last_socket_error());
632         BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
633         goto end;
634     }
635
636     if (addr == NULL)
637         goto end;
638
639 # ifdef EAI_FAMILY
640     do {
641         char h[NI_MAXHOST], s[NI_MAXSERV];
642         size_t nl;
643         static union {
644             void *p;
645             int (WSAAPI *f) (const struct sockaddr *, size_t /* socklen_t */ ,
646                              char *, size_t, char *, size_t, int);
647         } p_getnameinfo = {
648             NULL
649         };
650         /*
651          * 2nd argument to getnameinfo is specified to be socklen_t.
652          * Unfortunately there is a number of environments where socklen_t is
653          * not defined. As it's passed by value, it's safe to pass it as
654          * size_t... <appro>
655          */
656
657         if (p_getnameinfo.p == NULL) {
658             if ((p_getnameinfo.p = DSO_global_lookup("getnameinfo")) == NULL)
659                 p_getnameinfo.p = (void *)-1;
660         }
661         if (p_getnameinfo.p == (void *)-1)
662             break;
663
664         if ((*p_getnameinfo.f) (&sa.from.sa, sa.len.i, h, sizeof(h), s,
665                                 sizeof(s), NI_NUMERICHOST | NI_NUMERICSERV))
666             break;
667         nl = strlen(h) + strlen(s) + 2;
668         p = *addr;
669         if (p)
670             *p = '\0';
671         p = OPENSSL_realloc(p, nl);
672         if (p == NULL) {
673             BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
674             goto end;
675         }
676         *addr = p;
677         BIO_snprintf(*addr, nl, "%s:%s", h, s);
678         goto end;
679     } while (0);
680 # endif
681     if (sa.from.sa.sa_family != AF_INET)
682         goto end;
683     l = ntohl(sa.from.sa_in.sin_addr.s_addr);
684     port = ntohs(sa.from.sa_in.sin_port);
685     if (*addr == NULL) {
686         if ((p = OPENSSL_malloc(24)) == NULL) {
687             BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
688             goto end;
689         }
690         *addr = p;
691     }
692     BIO_snprintf(*addr, 24, "%d.%d.%d.%d:%d",
693                  (unsigned char)(l >> 24L) & 0xff,
694                  (unsigned char)(l >> 16L) & 0xff,
695                  (unsigned char)(l >> 8L) & 0xff,
696                  (unsigned char)(l) & 0xff, port);
697  end:
698     return (ret);
699 }
700
701 int BIO_set_tcp_ndelay(int s, int on)
702 {
703     int ret = 0;
704 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
705     int opt;
706
707 #  ifdef SOL_TCP
708     opt = SOL_TCP;
709 #  else
710 #   ifdef IPPROTO_TCP
711     opt = IPPROTO_TCP;
712 #   endif
713 #  endif
714
715     ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
716 # endif
717     return (ret == 0);
718 }
719
720 int BIO_socket_nbio(int s, int mode)
721 {
722     int ret = -1;
723     int l;
724
725     l = mode;
726 # ifdef FIONBIO
727     ret = BIO_socket_ioctl(s, FIONBIO, &l);
728 # endif
729     return (ret == 0);
730 }
731 #endif