2 * Copyright 1995-2021 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)>
33 # include <winsock.h> /* for type fd_set */
37 # include <sys/socket.h>
39 # include <sys/select.h>
43 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
44 int BIO_get_host_ip(const char *str, unsigned char *ip)
46 BIO_ADDRINFO *res = NULL;
49 if (BIO_sock_init() != 1)
50 return 0; /* don't generate another error code here */
52 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
55 if (BIO_ADDRINFO_family(res) != AF_INET) {
56 ERR_raise(ERR_LIB_BIO, BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
57 } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l)) {
59 * Because only AF_INET addresses will reach this far, we can assert
62 if (ossl_assert(l == 4))
63 ret = BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
65 BIO_ADDRINFO_free(res);
67 ERR_add_error_data(2, "host=", str);
73 int BIO_get_port(const char *str, unsigned short *port_ptr)
75 BIO_ADDRINFO *res = NULL;
79 ERR_raise(ERR_LIB_BIO, BIO_R_NO_PORT_DEFINED);
83 if (BIO_sock_init() != 1)
84 return 0; /* don't generate another error code here */
86 if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
87 if (BIO_ADDRINFO_family(res) != AF_INET) {
88 ERR_raise(ERR_LIB_BIO, BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
90 *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
93 BIO_ADDRINFO_free(res);
95 ERR_add_error_data(2, "host=", str);
102 int BIO_sock_error(int sock)
105 socklen_t size = sizeof(j);
108 * Note: under Windows the third parameter is of type (char *) whereas
109 * under other systems it is (void *) if you don't have a cast it will
110 * choke the compiler: if you do have a cast then you can either go for
111 * (char *) or (void *).
113 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
115 return get_last_socket_error();
120 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
121 struct hostent *BIO_gethostbyname(const char *name)
124 * Caching gethostbyname() results forever is wrong, so we have to let
125 * the true gethostbyname() worry about this
127 return gethostbyname(name);
131 int BIO_sock_init(void)
133 # ifdef OPENSSL_SYS_WINDOWS
134 static struct WSAData wsa_state;
136 if (!wsa_init_done) {
138 memset(&wsa_state, 0, sizeof(wsa_state));
140 * Not making wsa_state available to the rest of the code is formally
141 * wrong. But the structures we use are [believed to be] invariable
142 * among Winsock DLLs, while API availability is [expected to be]
143 * probed at run-time with DSO_global_lookup.
145 if (WSAStartup(0x0202, &wsa_state) != 0) {
146 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
147 "calling wsastartup()");
148 ERR_raise(ERR_LIB_BIO, BIO_R_WSASTARTUP);
152 # endif /* OPENSSL_SYS_WINDOWS */
154 extern int _watt_do_exit;
155 _watt_do_exit = 0; /* don't make sock_init() call exit() */
163 void bio_sock_cleanup_int(void)
165 # ifdef OPENSSL_SYS_WINDOWS
173 int BIO_socket_ioctl(int fd, long type, void *arg)
178 i = ioctlsocket(fd, type, (char *)arg);
180 # if defined(OPENSSL_SYS_VMS)
183 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
184 * observe that all the consumers pass in an "unsigned long *",
185 * so we arrange a local copy with a short pointer, and use
188 # if __INITIAL_POINTER_SIZE == 64
190 # pragma pointer_size save
191 # pragma pointer_size 32
192 unsigned long arg_32;
193 unsigned long *arg_32p;
194 # pragma pointer_size restore
196 arg_32 = *((unsigned long *)arg);
197 # else /* __INITIAL_POINTER_SIZE == 64 */
199 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
200 # else /* defined(OPENSSL_SYS_VMS) */
202 # endif /* defined(OPENSSL_SYS_VMS) [else] */
204 i = ioctlsocket(fd, type, ARG);
205 # endif /* __DJGPP__ */
207 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
208 "calling ioctlsocket()");
212 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
213 int BIO_get_accept_socket(char *host, int bind_mode)
215 int s = INVALID_SOCKET;
216 char *h = NULL, *p = NULL;
217 BIO_ADDRINFO *res = NULL;
219 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
220 return INVALID_SOCKET;
222 if (BIO_sock_init() != 1)
223 return INVALID_SOCKET;
225 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
228 if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
229 BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
234 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
235 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
241 BIO_ADDRINFO_free(res);
248 int BIO_accept(int sock, char **ip_port)
253 ret = BIO_accept_ex(sock, &res, 0);
254 if (ret == (int)INVALID_SOCKET) {
255 if (BIO_sock_should_retry(ret)) {
259 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
261 ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
265 if (ip_port != NULL) {
266 char *host = BIO_ADDR_hostname_string(&res, 1);
267 char *port = BIO_ADDR_service_string(&res, 1);
268 if (host != NULL && port != NULL)
269 *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
273 if (*ip_port == NULL) {
274 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
275 BIO_closesocket(ret);
276 ret = (int)INVALID_SOCKET;
278 strcpy(*ip_port, host);
279 strcat(*ip_port, ":");
280 strcat(*ip_port, port);
291 int BIO_set_tcp_ndelay(int s, int on)
294 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
305 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
310 int BIO_socket_nbio(int s, int mode)
319 ret = BIO_socket_ioctl(s, FIONBIO, &l);
320 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
321 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
323 l = fcntl(s, F_GETFL, 0);
325 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
329 # if defined(O_NONBLOCK)
332 l &= ~FNDELAY; /* BSD4.x */
335 # if defined(O_NONBLOCK)
338 l |= FNDELAY; /* BSD4.x */
341 ret = fcntl(s, F_SETFL, l);
344 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
349 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
350 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_INVALID_ARGUMENT);
356 int BIO_sock_info(int sock,
357 enum BIO_sock_info_type type, union BIO_sock_info_u *info)
360 case BIO_SOCK_INFO_ADDRESS:
364 addr_len = sizeof(*info->addr);
365 ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
368 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
369 "calling getsockname()");
370 ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_ERROR);
373 if ((size_t)addr_len > sizeof(*info->addr)) {
374 ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
380 ERR_raise(ERR_LIB_BIO, BIO_R_UNKNOWN_INFO_TYPE);
387 * Wait on fd at most until max_time; succeed immediately if max_time == 0.
388 * If for_read == 0 then assume to wait for writing, else wait for reading.
389 * Returns -1 on error, 0 on timeout, and 1 on success.
391 int BIO_socket_wait(int fd, int for_read, time_t max_time)
397 if (fd < 0 || fd >= FD_SETSIZE)
407 openssl_fdset(fd, &confds);
409 tv.tv_sec = (long)(max_time - now); /* might overflow */
410 return select(fd + 1, for_read ? &confds : NULL,
411 for_read ? NULL : &confds, NULL, &tv);
413 #endif /* !defined(OPENSSL_NO_SOCK) */