1 /* crypto/bio/b_sock.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
63 #include "internal/cryptlib.h"
64 #include <openssl/bio.h>
65 #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
67 # if defined(NETWARE_CLIB)
68 # include <sys/ioctl.h>
72 #ifndef OPENSSL_NO_SOCK
73 # include <openssl/dso.h>
74 # define SOCKET_PROTOCOL IPPROTO_TCP
76 # define MAX_LISTEN SO_MAXCONN
77 # elif defined(SOMAXCONN)
78 # define MAX_LISTEN SOMAXCONN
80 # define MAX_LISTEN 32
82 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
83 static int wsa_init_done
= 0;
87 * WSAAPI specifier is required to make indirect calls to run-time
88 * linked WinSock 2 functions used in this module, to be specific
89 * [get|free]addrinfo and getnameinfo. This is because WinSock uses
90 * uses non-C calling convention, __stdcall vs. __cdecl, on x86
91 * Windows. On non-WinSock platforms WSAAPI needs to be void.
97 static int get_ip(const char *str
, unsigned char *ip
);
98 int BIO_get_host_ip(const char *str
, unsigned char *ip
)
107 BIOerr(BIO_F_BIO_GET_HOST_IP
, BIO_R_INVALID_IP_ADDRESS
);
112 * At this point, we have something that is most probably correct in some
113 * way, so let's init the socket.
115 if (BIO_sock_init() != 1)
116 return 0; /* don't generate another error code here */
119 * If the string actually contained an IP address, we need not do
125 /* do a gethostbyname */
126 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME
);
128 he
= BIO_gethostbyname(str
);
130 BIOerr(BIO_F_BIO_GET_HOST_IP
, BIO_R_BAD_HOSTNAME_LOOKUP
);
134 if (he
->h_addrtype
!= AF_INET
) {
135 BIOerr(BIO_F_BIO_GET_HOST_IP
,
136 BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET
);
139 for (i
= 0; i
< 4; i
++)
140 ip
[i
] = he
->h_addr_list
[0][i
];
145 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME
);
147 ERR_add_error_data(2, "host=", str
);
153 int BIO_get_port(const char *str
, unsigned short *port_ptr
)
159 BIOerr(BIO_F_BIO_GET_PORT
, BIO_R_NO_PORT_DEFINED
);
164 *port_ptr
= (unsigned short)i
;
166 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME
);
168 * Note: under VMS with SOCKETSHR, it seems like the first parameter
169 * is 'char *', instead of 'const char *'
171 # ifndef CONST_STRICT
172 s
= getservbyname((char *)str
, "tcp");
174 s
= getservbyname(str
, "tcp");
177 *port_ptr
= ntohs((unsigned short)s
->s_port
);
178 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME
);
180 if (strcmp(str
, "http") == 0)
182 else if (strcmp(str
, "telnet") == 0)
184 else if (strcmp(str
, "socks") == 0)
186 else if (strcmp(str
, "https") == 0)
188 else if (strcmp(str
, "ssl") == 0)
190 else if (strcmp(str
, "ftp") == 0)
192 else if (strcmp(str
, "gopher") == 0)
195 SYSerr(SYS_F_GETSERVBYNAME
, get_last_socket_error());
196 ERR_add_error_data(3, "service='", str
, "'");
204 int BIO_sock_error(int sock
)
212 /* heuristic way to adapt for platforms that expect 64-bit optlen */
213 size
.s
= 0, size
.i
= sizeof(j
);
215 * Note: under Windows the third parameter is of type (char *) whereas
216 * under other systems it is (void *) if you don't have a cast it will
217 * choke the compiler: if you do have a cast then you can either go for
218 * (char *) or (void *).
220 i
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, (void *)&j
, (void *)&size
);
227 struct hostent
*BIO_gethostbyname(const char *name
)
230 * Caching gethostbyname() results forever is wrong, so we have to let
231 * the true gethostbyname() worry about this
233 # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
234 return gethostbyname((char *)name
);
236 return gethostbyname(name
);
240 int BIO_sock_init(void)
242 # ifdef OPENSSL_SYS_WINDOWS
243 static struct WSAData wsa_state
;
245 if (!wsa_init_done
) {
249 memset(&wsa_state
, 0, sizeof(wsa_state
));
251 * Not making wsa_state available to the rest of the code is formally
252 * wrong. But the structures we use are [beleived to be] invariable
253 * among Winsock DLLs, while API availability is [expected to be]
254 * probed at run-time with DSO_global_lookup.
256 if (WSAStartup(0x0202, &wsa_state
) != 0) {
257 err
= WSAGetLastError();
258 SYSerr(SYS_F_WSASTARTUP
, err
);
259 BIOerr(BIO_F_BIO_SOCK_INIT
, BIO_R_WSASTARTUP
);
263 # endif /* OPENSSL_SYS_WINDOWS */
265 extern int _watt_do_exit
;
266 _watt_do_exit
= 0; /* don't make sock_init() call exit() */
271 # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
276 if (!wsa_init_done
) {
278 wVerReq
= MAKEWORD(2, 0);
279 err
= WSAStartup(wVerReq
, &wsaData
);
281 SYSerr(SYS_F_WSASTARTUP
, err
);
282 BIOerr(BIO_F_BIO_SOCK_INIT
, BIO_R_WSASTARTUP
);
291 void BIO_sock_cleanup(void)
293 # ifdef OPENSSL_SYS_WINDOWS
298 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
306 # if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
308 int BIO_socket_ioctl(int fd
, long type
, void *arg
)
313 i
= ioctlsocket(fd
, type
, (char *)arg
);
315 # if defined(OPENSSL_SYS_VMS)
318 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
319 * observe that all the consumers pass in an "unsigned long *",
320 * so we arrange a local copy with a short pointer, and use
323 # if __INITIAL_POINTER_SIZE == 64
325 # pragma pointer_size save
326 # pragma pointer_size 32
327 unsigned long arg_32
;
328 unsigned long *arg_32p
;
329 # pragma pointer_size restore
331 arg_32
= *((unsigned long *)arg
);
332 # else /* __INITIAL_POINTER_SIZE == 64 */
334 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
335 # else /* defined(OPENSSL_SYS_VMS) */
337 # endif /* defined(OPENSSL_SYS_VMS) [else] */
339 i
= ioctlsocket(fd
, type
, ARG
);
340 # endif /* __DJGPP__ */
342 SYSerr(SYS_F_IOCTLSOCKET
, get_last_socket_error());
345 # endif /* __VMS_VER */
348 * The reason I have implemented this instead of using sscanf is because
349 * Visual C 1.52c gives an unresolved external when linking a DLL :-(
351 static int get_ip(const char *str
, unsigned char ip
[4])
354 int num
= 0, c
, ok
= 0;
356 tmp
[0] = tmp
[1] = tmp
[2] = tmp
[3] = 0;
360 if ((c
>= '0') && (c
<= '9')) {
362 tmp
[num
] = tmp
[num
] * 10 + c
- '0';
365 } else if (c
== '.') {
372 } else if (c
== '\0' && (num
== 3) && ok
)
384 int BIO_get_accept_socket(char *host
, int bind_mode
)
389 struct sockaddr_in sa_in
;
390 # if OPENSSL_USE_IPV6
391 struct sockaddr_in6 sa_in6
;
394 int s
= (int)INVALID_SOCKET
, cs
, addrlen
;
397 char *str
= NULL
, *e
;
402 if (BIO_sock_init() != 1)
403 return ((int)INVALID_SOCKET
);
405 if ((str
= OPENSSL_strdup(host
)) == NULL
)
406 return ((int)INVALID_SOCKET
);
410 for (e
= str
; *e
; e
++) {
413 } else if (*e
== '/') {
419 *p
++ = '\0'; /* points at last ':', '::port' is special
428 int (WSAAPI
*f
) (const char *, const char *,
429 const struct addrinfo
*, struct addrinfo
**);
435 void (WSAAPI
*f
) (struct addrinfo
*);
439 struct addrinfo
*res
, hint
;
441 if (p_getaddrinfo
.p
== NULL
) {
442 if ((p_getaddrinfo
.p
= DSO_global_lookup("getaddrinfo")) == NULL
443 || (p_freeaddrinfo
.p
=
444 DSO_global_lookup("freeaddrinfo")) == NULL
)
445 p_getaddrinfo
.p
= (void *)-1;
447 if (p_getaddrinfo
.p
== (void *)-1)
451 * '::port' enforces IPv6 wildcard listener. Some OSes, e.g. Solaris,
452 * default to IPv6 without any hint. Also note that commonly IPv6
453 * wildchard socket can service IPv4 connections just as well...
455 memset(&hint
, 0, sizeof(hint
));
456 hint
.ai_flags
= AI_PASSIVE
;
458 if (strchr(h
, ':')) {
461 # if OPENSSL_USE_IPV6
462 hint
.ai_family
= AF_INET6
;
466 } else if (h
[0] == '*' && h
[1] == '\0') {
467 hint
.ai_family
= AF_INET
;
472 if ((*p_getaddrinfo
.f
) (h
, p
, &hint
, &res
))
475 addrlen
= res
->ai_addrlen
<= sizeof(server
) ?
476 res
->ai_addrlen
: sizeof(server
);
477 memcpy(&server
, res
->ai_addr
, addrlen
);
479 (*p_freeaddrinfo
.f
) (res
);
484 if (!BIO_get_port(p
, &port
))
487 memset(&server
, 0, sizeof(server
));
488 server
.sa_in
.sin_family
= AF_INET
;
489 server
.sa_in
.sin_port
= htons(port
);
490 addrlen
= sizeof(server
.sa_in
);
492 if (h
== NULL
|| strcmp(h
, "*") == 0)
493 server
.sa_in
.sin_addr
.s_addr
= INADDR_ANY
;
495 if (!BIO_get_host_ip(h
, &(ip
[0])))
498 ((unsigned long)ip
[0] << 24L) |
499 ((unsigned long)ip
[1] << 16L) |
500 ((unsigned long)ip
[2] << 8L) | ((unsigned long)ip
[3]);
501 server
.sa_in
.sin_addr
.s_addr
= htonl(l
);
505 s
= socket(server
.sa
.sa_family
, SOCK_STREAM
, SOCKET_PROTOCOL
);
506 if (s
== (int)INVALID_SOCKET
) {
507 SYSerr(SYS_F_SOCKET
, get_last_socket_error());
508 ERR_add_error_data(3, "port='", host
, "'");
509 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET
, BIO_R_UNABLE_TO_CREATE_SOCKET
);
513 if (bind_mode
== BIO_BIND_REUSEADDR
) {
516 ret
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&i
, sizeof(i
));
517 bind_mode
= BIO_BIND_NORMAL
;
520 if (bind(s
, &server
.sa
, addrlen
) == -1) {
522 err_num
= get_last_socket_error();
523 if ((bind_mode
== BIO_BIND_REUSEADDR_IF_UNUSED
) &&
524 # ifdef OPENSSL_SYS_WINDOWS
526 * Some versions of Windows define EADDRINUSE to a dummy value.
528 (err_num
== WSAEADDRINUSE
))
530 (err_num
== EADDRINUSE
))
534 if (h
== NULL
|| strcmp(h
, "*") == 0) {
535 # if OPENSSL_USE_IPV6
536 if (client
.sa
.sa_family
== AF_INET6
) {
537 memset(&client
.sa_in6
.sin6_addr
, 0,
538 sizeof(client
.sa_in6
.sin6_addr
));
539 client
.sa_in6
.sin6_addr
.s6_addr
[15] = 1;
542 if (client
.sa
.sa_family
== AF_INET
) {
543 client
.sa_in
.sin_addr
.s_addr
= htonl(0x7F000001);
547 cs
= socket(client
.sa
.sa_family
, SOCK_STREAM
, SOCKET_PROTOCOL
);
548 if (cs
!= (int)INVALID_SOCKET
) {
550 ii
= connect(cs
, &client
.sa
, addrlen
);
552 if (ii
== (int)INVALID_SOCKET
) {
553 bind_mode
= BIO_BIND_REUSEADDR
;
562 SYSerr(SYS_F_BIND
, err_num
);
563 ERR_add_error_data(3, "port='", host
, "'");
564 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET
, BIO_R_UNABLE_TO_BIND_SOCKET
);
567 if (listen(s
, MAX_LISTEN
) == -1) {
568 SYSerr(SYS_F_BIND
, get_last_socket_error());
569 ERR_add_error_data(3, "port='", host
, "'");
570 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET
, BIO_R_UNABLE_TO_LISTEN_SOCKET
);
576 if ((ret
== 0) && (s
!= (int)INVALID_SOCKET
)) {
578 s
= (int)INVALID_SOCKET
;
583 int BIO_accept(int sock
, char **addr
)
585 int ret
= (int)INVALID_SOCKET
;
592 * As for following union. Trouble is that there are platforms
593 * that have socklen_t and there are platforms that don't, on
594 * some platforms socklen_t is int and on some size_t. So what
595 * one can do? One can cook #ifdef spaghetti, which is nothing
596 * but masochistic. Or one can do union between int and size_t.
597 * One naturally does it primarily for 64-bit platforms where
598 * sizeof(int) != sizeof(size_t). But would it work? Note that
599 * if size_t member is initialized to 0, then later int member
600 * assignment naturally does the job on little-endian platforms
601 * regardless accept's expectations! What about big-endians?
602 * If accept expects int*, then it works, and if size_t*, then
603 * length value would appear as unreasonably large. But this
604 * won't prevent it from filling in the address structure. The
605 * trouble of course would be if accept returns more data than
606 * actual buffer can accomodate and overwrite stack... That's
607 * where early OPENSSL_assert comes into picture. Besides, the
608 * only 64-bit big-endian platform found so far that expects
609 * size_t* is HP-UX, where stack grows towards higher address.
618 struct sockaddr_in sa_in
;
619 # if OPENSSL_USE_IPV6
620 struct sockaddr_in6 sa_in6
;
626 sa
.len
.i
= sizeof(sa
.from
);
627 memset(&sa
.from
, 0, sizeof(sa
.from
));
628 ret
= accept(sock
, &sa
.from
.sa
, (void *)&sa
.len
);
629 if (sizeof(sa
.len
.i
) != sizeof(sa
.len
.s
) && sa
.len
.i
== 0) {
630 OPENSSL_assert(sa
.len
.s
<= sizeof(sa
.from
));
631 sa
.len
.i
= (int)sa
.len
.s
;
632 /* use sa.len.i from this point */
634 if (ret
== (int)INVALID_SOCKET
) {
635 if (BIO_sock_should_retry(ret
))
637 SYSerr(SYS_F_ACCEPT
, get_last_socket_error());
638 BIOerr(BIO_F_BIO_ACCEPT
, BIO_R_ACCEPT_ERROR
);
647 char h
[NI_MAXHOST
], s
[NI_MAXSERV
];
651 int (WSAAPI
*f
) (const struct sockaddr
*, size_t /* socklen_t */ ,
652 char *, size_t, char *, size_t, int);
657 * 2nd argument to getnameinfo is specified to be socklen_t.
658 * Unfortunately there is a number of environments where socklen_t is
659 * not defined. As it's passed by value, it's safe to pass it as
663 if (p_getnameinfo
.p
== NULL
) {
664 if ((p_getnameinfo
.p
= DSO_global_lookup("getnameinfo")) == NULL
)
665 p_getnameinfo
.p
= (void *)-1;
667 if (p_getnameinfo
.p
== (void *)-1)
670 if ((*p_getnameinfo
.f
) (&sa
.from
.sa
, sa
.len
.i
, h
, sizeof(h
), s
,
671 sizeof(s
), NI_NUMERICHOST
| NI_NUMERICSERV
))
673 nl
= strlen(h
) + strlen(s
) + 2;
677 p
= OPENSSL_realloc(p
, nl
);
679 BIOerr(BIO_F_BIO_ACCEPT
, ERR_R_MALLOC_FAILURE
);
683 BIO_snprintf(*addr
, nl
, "%s:%s", h
, s
);
687 if (sa
.from
.sa
.sa_family
!= AF_INET
)
689 l
= ntohl(sa
.from
.sa_in
.sin_addr
.s_addr
);
690 port
= ntohs(sa
.from
.sa_in
.sin_port
);
692 if ((p
= OPENSSL_malloc(24)) == NULL
) {
693 BIOerr(BIO_F_BIO_ACCEPT
, ERR_R_MALLOC_FAILURE
);
698 BIO_snprintf(*addr
, 24, "%d.%d.%d.%d:%d",
699 (unsigned char)(l
>> 24L) & 0xff,
700 (unsigned char)(l
>> 16L) & 0xff,
701 (unsigned char)(l
>> 8L) & 0xff,
702 (unsigned char)(l
) & 0xff, port
);
707 int BIO_set_tcp_ndelay(int s
, int on
)
710 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
721 ret
= setsockopt(s
, opt
, TCP_NODELAY
, (char *)&on
, sizeof(on
));
726 int BIO_socket_nbio(int s
, int mode
)
733 ret
= BIO_socket_ioctl(s
, FIONBIO
, &l
);