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>
38 # elif defined _HPUX_SOURCE
39 # include <sys/time.h>
41 # include <sys/select.h>
45 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
46 int BIO_get_host_ip(const char *str, unsigned char *ip)
48 BIO_ADDRINFO *res = NULL;
51 if (BIO_sock_init() != 1)
52 return 0; /* don't generate another error code here */
54 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
57 if (BIO_ADDRINFO_family(res) != AF_INET) {
58 ERR_raise(ERR_LIB_BIO, BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
59 } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l)) {
61 * Because only AF_INET addresses will reach this far, we can assert
64 if (ossl_assert(l == 4))
65 ret = BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
67 BIO_ADDRINFO_free(res);
69 ERR_add_error_data(2, "host=", str);
75 int BIO_get_port(const char *str, unsigned short *port_ptr)
77 BIO_ADDRINFO *res = NULL;
81 ERR_raise(ERR_LIB_BIO, BIO_R_NO_PORT_DEFINED);
85 if (BIO_sock_init() != 1)
86 return 0; /* don't generate another error code here */
88 if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
89 if (BIO_ADDRINFO_family(res) != AF_INET) {
90 ERR_raise(ERR_LIB_BIO, BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
92 *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
95 BIO_ADDRINFO_free(res);
97 ERR_add_error_data(2, "host=", str);
104 int BIO_sock_error(int sock)
107 socklen_t size = sizeof(j);
110 * Note: under Windows the third parameter is of type (char *) whereas
111 * under other systems it is (void *) if you don't have a cast it will
112 * choke the compiler: if you do have a cast then you can either go for
113 * (char *) or (void *).
115 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
117 return get_last_socket_error();
122 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
123 struct hostent *BIO_gethostbyname(const char *name)
126 * Caching gethostbyname() results forever is wrong, so we have to let
127 * the true gethostbyname() worry about this
129 return gethostbyname(name);
133 # ifdef BIO_HAVE_WSAMSG
134 LPFN_WSARECVMSG bio_WSARecvMsg;
135 LPFN_WSASENDMSG bio_WSASendMsg;
138 int BIO_sock_init(void)
140 # ifdef OPENSSL_SYS_WINDOWS
141 static struct WSAData wsa_state;
143 if (!wsa_init_done) {
145 memset(&wsa_state, 0, sizeof(wsa_state));
147 * Not making wsa_state available to the rest of the code is formally
148 * wrong. But the structures we use are [believed to be] invariable
149 * among Winsock DLLs, while API availability is [expected to be]
150 * probed at run-time with DSO_global_lookup.
152 if (WSAStartup(0x0202, &wsa_state) != 0) {
153 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
154 "calling wsastartup()");
155 ERR_raise(ERR_LIB_BIO, BIO_R_WSASTARTUP);
160 * On Windows, some socket functions are not exposed as a prototype.
161 * Instead, their function pointers must be loaded via this elaborate
164 # ifdef BIO_HAVE_WSAMSG
166 GUID id_WSARecvMsg = WSAID_WSARECVMSG;
167 GUID id_WSASendMsg = WSAID_WSASENDMSG;
171 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
172 if (s != INVALID_SOCKET) {
173 if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
174 &id_WSARecvMsg, sizeof(id_WSARecvMsg),
175 &bio_WSARecvMsg, sizeof(bio_WSARecvMsg),
176 &len_out, NULL, NULL) != 0
177 || len_out != sizeof(bio_WSARecvMsg))
178 bio_WSARecvMsg = NULL;
180 if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
181 &id_WSASendMsg, sizeof(id_WSASendMsg),
182 &bio_WSASendMsg, sizeof(bio_WSASendMsg),
183 &len_out, NULL, NULL) != 0
184 || len_out != sizeof(bio_WSASendMsg))
185 bio_WSASendMsg = NULL;
192 # endif /* OPENSSL_SYS_WINDOWS */
194 extern int _watt_do_exit;
195 _watt_do_exit = 0; /* don't make sock_init() call exit() */
203 void bio_sock_cleanup_int(void)
205 # ifdef OPENSSL_SYS_WINDOWS
213 int BIO_socket_ioctl(int fd, long type, void *arg)
218 i = ioctlsocket(fd, type, (char *)arg);
220 # if defined(OPENSSL_SYS_VMS)
223 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
224 * observe that all the consumers pass in an "unsigned long *",
225 * so we arrange a local copy with a short pointer, and use
228 # if __INITIAL_POINTER_SIZE == 64
230 # pragma pointer_size save
231 # pragma pointer_size 32
232 unsigned long arg_32;
233 unsigned long *arg_32p;
234 # pragma pointer_size restore
236 arg_32 = *((unsigned long *)arg);
237 # else /* __INITIAL_POINTER_SIZE == 64 */
239 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
240 # else /* defined(OPENSSL_SYS_VMS) */
242 # endif /* defined(OPENSSL_SYS_VMS) [else] */
244 i = ioctlsocket(fd, type, ARG);
245 # endif /* __DJGPP__ */
247 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
248 "calling ioctlsocket()");
252 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
253 int BIO_get_accept_socket(char *host, int bind_mode)
255 int s = INVALID_SOCKET;
256 char *h = NULL, *p = NULL;
257 BIO_ADDRINFO *res = NULL;
259 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
260 return INVALID_SOCKET;
262 if (BIO_sock_init() != 1)
263 return INVALID_SOCKET;
265 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
268 if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
269 BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
274 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
275 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
281 BIO_ADDRINFO_free(res);
288 int BIO_accept(int sock, char **ip_port)
293 ret = BIO_accept_ex(sock, &res, 0);
294 if (ret == (int)INVALID_SOCKET) {
295 if (BIO_sock_should_retry(ret)) {
299 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
301 ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
305 if (ip_port != NULL) {
306 char *host = BIO_ADDR_hostname_string(&res, 1);
307 char *port = BIO_ADDR_service_string(&res, 1);
308 if (host != NULL && port != NULL) {
309 *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
312 ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
315 if (*ip_port == NULL) {
316 BIO_closesocket(ret);
317 ret = (int)INVALID_SOCKET;
319 strcpy(*ip_port, host);
320 strcat(*ip_port, ":");
321 strcat(*ip_port, port);
332 int BIO_set_tcp_ndelay(int s, int on)
335 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
346 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
351 int BIO_socket_nbio(int s, int mode)
357 # if defined(FIONBIO) && !defined(OPENSSL_SYS_TANDEM)
360 ret = BIO_socket_ioctl(s, FIONBIO, &l);
361 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
362 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
364 l = fcntl(s, F_GETFL, 0);
366 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
370 # if defined(O_NONBLOCK)
373 l &= ~FNDELAY; /* BSD4.x */
376 # if defined(O_NONBLOCK)
379 l |= FNDELAY; /* BSD4.x */
382 ret = fcntl(s, F_SETFL, l);
385 ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
390 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
391 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_INVALID_ARGUMENT);
397 int BIO_sock_info(int sock,
398 enum BIO_sock_info_type type, union BIO_sock_info_u *info)
401 case BIO_SOCK_INFO_ADDRESS:
405 addr_len = sizeof(*info->addr);
406 ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
409 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
410 "calling getsockname()");
411 ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_ERROR);
414 if ((size_t)addr_len > sizeof(*info->addr)) {
415 ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
421 ERR_raise(ERR_LIB_BIO, BIO_R_UNKNOWN_INFO_TYPE);
428 * Wait on fd at most until max_time; succeed immediately if max_time == 0.
429 * If for_read == 0 then assume to wait for writing, else wait for reading.
430 * Returns -1 on error, 0 on timeout, and 1 on success.
432 int BIO_socket_wait(int fd, int for_read, time_t max_time)
438 if (fd < 0 || fd >= FD_SETSIZE)
448 openssl_fdset(fd, &confds);
450 tv.tv_sec = (long)(max_time - now); /* might overflow */
451 return select(fd + 1, for_read ? &confds : NULL,
452 for_read ? NULL : &confds, NULL, &tv);
454 #endif /* !defined(OPENSSL_NO_SOCK) */