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/select.h>
30 # include <winsock.h> /* for type fd_set */
33 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
34 int BIO_get_host_ip(const char *str
, unsigned char *ip
)
36 BIO_ADDRINFO
*res
= NULL
;
39 if (BIO_sock_init() != 1)
40 return 0; /* don't generate another error code here */
42 if (BIO_lookup(str
, NULL
, BIO_LOOKUP_CLIENT
, AF_INET
, SOCK_STREAM
, &res
)) {
45 if (BIO_ADDRINFO_family(res
) != AF_INET
) {
46 BIOerr(BIO_F_BIO_GET_HOST_IP
,
47 BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET
);
48 } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res
), NULL
, &l
)) {
50 * Because only AF_INET addresses will reach this far, we can assert
53 if (ossl_assert(l
== 4))
54 ret
= BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res
), ip
, &l
);
56 BIO_ADDRINFO_free(res
);
58 ERR_add_error_data(2, "host=", str
);
64 int BIO_get_port(const char *str
, unsigned short *port_ptr
)
66 BIO_ADDRINFO
*res
= NULL
;
70 BIOerr(BIO_F_BIO_GET_PORT
, BIO_R_NO_PORT_DEFINED
);
74 if (BIO_sock_init() != 1)
75 return 0; /* don't generate another error code here */
77 if (BIO_lookup(NULL
, str
, BIO_LOOKUP_CLIENT
, AF_INET
, SOCK_STREAM
, &res
)) {
78 if (BIO_ADDRINFO_family(res
) != AF_INET
) {
79 BIOerr(BIO_F_BIO_GET_PORT
,
80 BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET
);
82 *port_ptr
= ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res
)));
85 BIO_ADDRINFO_free(res
);
87 ERR_add_error_data(2, "host=", str
);
94 int BIO_sock_error(int sock
)
97 socklen_t size
= sizeof(j
);
100 * Note: under Windows the third parameter is of type (char *) whereas
101 * under other systems it is (void *) if you don't have a cast it will
102 * choke the compiler: if you do have a cast then you can either go for
103 * (char *) or (void *).
105 i
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, (void *)&j
, &size
);
107 return get_last_socket_error();
112 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
113 struct hostent
*BIO_gethostbyname(const char *name
)
116 * Caching gethostbyname() results forever is wrong, so we have to let
117 * the true gethostbyname() worry about this
119 return gethostbyname(name
);
123 int BIO_sock_init(void)
125 # ifdef OPENSSL_SYS_WINDOWS
126 static struct WSAData wsa_state
;
128 if (!wsa_init_done
) {
130 memset(&wsa_state
, 0, sizeof(wsa_state
));
132 * Not making wsa_state available to the rest of the code is formally
133 * wrong. But the structures we use are [believed to be] invariable
134 * among Winsock DLLs, while API availability is [expected to be]
135 * probed at run-time with DSO_global_lookup.
137 if (WSAStartup(0x0202, &wsa_state
) != 0) {
138 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
139 "calling wsastartup()");
140 BIOerr(BIO_F_BIO_SOCK_INIT
, BIO_R_WSASTARTUP
);
144 # endif /* OPENSSL_SYS_WINDOWS */
146 extern int _watt_do_exit
;
147 _watt_do_exit
= 0; /* don't make sock_init() call exit() */
155 void bio_sock_cleanup_int(void)
157 # ifdef OPENSSL_SYS_WINDOWS
165 int BIO_socket_ioctl(int fd
, long type
, void *arg
)
170 i
= ioctlsocket(fd
, type
, (char *)arg
);
172 # if defined(OPENSSL_SYS_VMS)
175 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
176 * observe that all the consumers pass in an "unsigned long *",
177 * so we arrange a local copy with a short pointer, and use
180 # if __INITIAL_POINTER_SIZE == 64
182 # pragma pointer_size save
183 # pragma pointer_size 32
184 unsigned long arg_32
;
185 unsigned long *arg_32p
;
186 # pragma pointer_size restore
188 arg_32
= *((unsigned long *)arg
);
189 # else /* __INITIAL_POINTER_SIZE == 64 */
191 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
192 # else /* defined(OPENSSL_SYS_VMS) */
194 # endif /* defined(OPENSSL_SYS_VMS) [else] */
196 i
= ioctlsocket(fd
, type
, ARG
);
197 # endif /* __DJGPP__ */
199 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
200 "calling ioctlsocket()");
204 # ifndef OPENSSL_NO_DEPRECATED_1_1_0
205 int BIO_get_accept_socket(char *host
, int bind_mode
)
207 int s
= INVALID_SOCKET
;
208 char *h
= NULL
, *p
= NULL
;
209 BIO_ADDRINFO
*res
= NULL
;
211 if (!BIO_parse_hostserv(host
, &h
, &p
, BIO_PARSE_PRIO_SERV
))
212 return INVALID_SOCKET
;
214 if (BIO_sock_init() != 1)
215 return INVALID_SOCKET
;
217 if (BIO_lookup(h
, p
, BIO_LOOKUP_SERVER
, AF_UNSPEC
, SOCK_STREAM
, &res
) != 0)
220 if ((s
= BIO_socket(BIO_ADDRINFO_family(res
), BIO_ADDRINFO_socktype(res
),
221 BIO_ADDRINFO_protocol(res
), 0)) == INVALID_SOCKET
) {
226 if (!BIO_listen(s
, BIO_ADDRINFO_address(res
),
227 bind_mode
? BIO_SOCK_REUSEADDR
: 0)) {
233 BIO_ADDRINFO_free(res
);
240 int BIO_accept(int sock
, char **ip_port
)
245 ret
= BIO_accept_ex(sock
, &res
, 0);
246 if (ret
== (int)INVALID_SOCKET
) {
247 if (BIO_sock_should_retry(ret
)) {
251 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
253 BIOerr(BIO_F_BIO_ACCEPT
, BIO_R_ACCEPT_ERROR
);
257 if (ip_port
!= NULL
) {
258 char *host
= BIO_ADDR_hostname_string(&res
, 1);
259 char *port
= BIO_ADDR_service_string(&res
, 1);
260 if (host
!= NULL
&& port
!= NULL
)
261 *ip_port
= OPENSSL_zalloc(strlen(host
) + strlen(port
) + 2);
265 if (*ip_port
== NULL
) {
266 BIOerr(BIO_F_BIO_ACCEPT
, ERR_R_MALLOC_FAILURE
);
267 BIO_closesocket(ret
);
268 ret
= (int)INVALID_SOCKET
;
270 strcpy(*ip_port
, host
);
271 strcat(*ip_port
, ":");
272 strcat(*ip_port
, port
);
283 int BIO_set_tcp_ndelay(int s
, int on
)
286 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
297 ret
= setsockopt(s
, opt
, TCP_NODELAY
, (char *)&on
, sizeof(on
));
302 int BIO_socket_nbio(int s
, int mode
)
311 ret
= BIO_socket_ioctl(s
, FIONBIO
, &l
);
312 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
313 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
315 l
= fcntl(s
, F_GETFL
, 0);
317 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
321 # if defined(O_NONBLOCK)
324 l
&= ~FNDELAY
; /* BSD4.x */
327 # if defined(O_NONBLOCK)
330 l
|= FNDELAY
; /* BSD4.x */
333 ret
= fcntl(s
, F_SETFL
, l
);
336 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
341 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
342 BIOerr(BIO_F_BIO_SOCKET_NBIO
, ERR_R_PASSED_INVALID_ARGUMENT
);
348 int BIO_sock_info(int sock
,
349 enum BIO_sock_info_type type
, union BIO_sock_info_u
*info
)
352 case BIO_SOCK_INFO_ADDRESS
:
356 addr_len
= sizeof(*info
->addr
);
357 ret
= getsockname(sock
, BIO_ADDR_sockaddr_noconst(info
->addr
),
360 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
361 "calling getsockname()");
362 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_GETSOCKNAME_ERROR
);
365 if ((size_t)addr_len
> sizeof(*info
->addr
)) {
366 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS
);
372 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_UNKNOWN_INFO_TYPE
);
378 /* TODO simplify by BIO_socket_wait() further other uses of select() in apps/ */
380 * Wait on fd at most until max_time; succeed immediately if max_time == 0.
381 * If for_read == 0 then assume to wait for writing, else wait for reading.
382 * Returns -1 on error, 0 on timeout, and 1 on success.
384 int BIO_socket_wait(int fd
, int for_read
, time_t max_time
)
390 if (fd
< 0 || fd
>= FD_SETSIZE
)
400 openssl_fdset(fd
, &confds
);
402 tv
.tv_sec
= (long)(max_time
- now
); /* might overflow */
403 return select(fd
+ 1, for_read
? &confds
: NULL
,
404 for_read
? NULL
: &confds
, NULL
, &tv
);
406 #endif /* !defined(OPENSSL_NO_SOCK) */