2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include "bio_local.h"
13 #ifndef OPENSSL_NO_SOCK
14 # define SOCKET_PROTOCOL IPPROTO_TCP
16 # define MAX_LISTEN SO_MAXCONN
17 # elif defined(SOMAXCONN)
18 # define MAX_LISTEN SOMAXCONN
20 # define MAX_LISTEN 32
22 # if defined(OPENSSL_SYS_WINDOWS)
23 static int wsa_init_done = 0;
28 # include <sys/time.h> /* select */
29 # if defined(OPENSSL_TANDEM_FLOSS)
30 # include <floss.h(floss_select)>
32 # elif !defined _WIN32
34 # include <sys/select.h>
36 # include <winsock.h> /* for type fd_set */
39 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
40 int BIO_get_host_ip(const char *str, unsigned char *ip)
42 BIO_ADDRINFO *res = NULL;
45 if (BIO_sock_init() != 1)
46 return 0; /* don't generate another error code here */
48 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
51 if (BIO_ADDRINFO_family(res) != AF_INET) {
52 ERR_raise(ERR_LIB_BIO, BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
53 } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l)) {
55 * Because only AF_INET addresses will reach this far, we can assert
58 if (ossl_assert(l == 4))
59 ret = BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
61 BIO_ADDRINFO_free(res);
63 ERR_add_error_data(2, "host=", str);
69 int BIO_get_port(const char *str, unsigned short *port_ptr)
71 BIO_ADDRINFO *res = NULL;
75 ERR_raise(ERR_LIB_BIO, BIO_R_NO_PORT_DEFINED);
79 if (BIO_sock_init() != 1)
80 return 0; /* don't generate another error code here */
82 if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
83 if (BIO_ADDRINFO_family(res) != AF_INET) {
84 ERR_raise(ERR_LIB_BIO, BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
86 *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
89 BIO_ADDRINFO_free(res);
91 ERR_add_error_data(2, "host=", str);
98 int BIO_sock_error(int sock)
101 socklen_t size = sizeof(j);
104 * Note: under Windows the third parameter is of type (char *) whereas
105 * under other systems it is (void *) if you don't have a cast it will
106 * choke the compiler: if you do have a cast then you can either go for
107 * (char *) or (void *).
109 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
111 return get_last_socket_error();
116 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
117 struct hostent *BIO_gethostbyname(const char *name)
120 * Caching gethostbyname() results forever is wrong, so we have to let
121 * the true gethostbyname() worry about this
123 return gethostbyname(name);
127 int BIO_sock_init(void)
129 # ifdef OPENSSL_SYS_WINDOWS
130 static struct WSAData wsa_state;
132 if (!wsa_init_done) {
134 memset(&wsa_state, 0, sizeof(wsa_state));
136 * Not making wsa_state available to the rest of the code is formally
137 * wrong. But the structures we use are [believed to be] invariable
138 * among Winsock DLLs, while API availability is [expected to be]
139 * probed at run-time with DSO_global_lookup.
141 if (WSAStartup(0x0202, &wsa_state) != 0) {
142 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
143 "calling wsastartup()");
144 ERR_raise(ERR_LIB_BIO, BIO_R_WSASTARTUP);
148 # endif /* OPENSSL_SYS_WINDOWS */
150 extern int _watt_do_exit;
151 _watt_do_exit = 0; /* don't make sock_init() call exit() */
159 void bio_sock_cleanup_int(void)
161 # ifdef OPENSSL_SYS_WINDOWS
169 int BIO_socket_ioctl(int fd, long type, void *arg)
174 i = ioctlsocket(fd, type, (char *)arg);
176 # if defined(OPENSSL_SYS_VMS)
179 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
180 * observe that all the consumers pass in an "unsigned long *",
181 * so we arrange a local copy with a short pointer, and use
184 # if __INITIAL_POINTER_SIZE == 64
186 # pragma pointer_size save
187 # pragma pointer_size 32
188 unsigned long arg_32;
189 unsigned long *arg_32p;
190 # pragma pointer_size restore
192 arg_32 = *((unsigned long *)arg);
193 # else /* __INITIAL_POINTER_SIZE == 64 */
195 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
196 # else /* defined(OPENSSL_SYS_VMS) */
198 # endif /* defined(OPENSSL_SYS_VMS) [else] */
200 i = ioctlsocket(fd, type, ARG);
201 # endif /* __DJGPP__ */
203 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
204 "calling ioctlsocket()");
208 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
209 int BIO_get_accept_socket(char *host, int bind_mode)
211 int s = INVALID_SOCKET;
212 char *h = NULL, *p = NULL;
213 BIO_ADDRINFO *res = NULL;
215 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
216 return INVALID_SOCKET;
218 if (BIO_sock_init() != 1)
219 return INVALID_SOCKET;
221 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
224 if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
225 BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
230 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
231 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
237 BIO_ADDRINFO_free(res);
244 int BIO_accept(int sock, char **ip_port)
249 ret = BIO_accept_ex(sock, &res, 0);
250 if (ret == (int)INVALID_SOCKET) {
251 if (BIO_sock_should_retry(ret)) {
255 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
257 ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
261 if (ip_port != NULL) {
262 char *host = BIO_ADDR_hostname_string(&res, 1);
263 char *port = BIO_ADDR_service_string(&res, 1);
264 if (host != NULL && port != NULL)
265 *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
269 if (*ip_port == NULL) {
270 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
271 BIO_closesocket(ret);
272 ret = (int)INVALID_SOCKET;
274 strcpy(*ip_port, host);
275 strcat(*ip_port, ":");
276 strcat(*ip_port, port);
287 int BIO_set_tcp_ndelay(int s, int on)
290 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
301 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
306 int BIO_socket_nbio(int s, int mode)
315 ret = BIO_socket_ioctl(s, FIONBIO, &l);
316 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
317 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
319 l = fcntl(s, F_GETFL, 0);
321 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
325 # if defined(O_NONBLOCK)
328 l &= ~FNDELAY; /* BSD4.x */
331 # if defined(O_NONBLOCK)
334 l |= FNDELAY; /* BSD4.x */
337 ret = fcntl(s, F_SETFL, l);
340 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
345 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
346 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_INVALID_ARGUMENT);
352 int BIO_sock_info(int sock,
353 enum BIO_sock_info_type type, union BIO_sock_info_u *info)
356 case BIO_SOCK_INFO_ADDRESS:
360 addr_len = sizeof(*info->addr);
361 ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
364 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
365 "calling getsockname()");
366 ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_ERROR);
369 if ((size_t)addr_len > sizeof(*info->addr)) {
370 ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
376 ERR_raise(ERR_LIB_BIO, BIO_R_UNKNOWN_INFO_TYPE);
382 /* TODO simplify by BIO_socket_wait() further other uses of select() in apps/ */
384 * Wait on fd at most until max_time; succeed immediately if max_time == 0.
385 * If for_read == 0 then assume to wait for writing, else wait for reading.
386 * Returns -1 on error, 0 on timeout, and 1 on success.
388 int BIO_socket_wait(int fd, int for_read, time_t max_time)
394 if (fd < 0 || fd >= FD_SETSIZE)
404 openssl_fdset(fd, &confds);
406 tv.tv_sec = (long)(max_time - now); /* might overflow */
407 return select(fd + 1, for_read ? &confds : NULL,
408 for_read ? NULL : &confds, NULL, &tv);
410 #endif /* !defined(OPENSSL_NO_SOCK) */