2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
16 #include <openssl/err.h>
18 #ifndef OPENSSL_NO_SOCK
20 # define MAX_LISTEN SO_MAXCONN
21 # elif defined(SOMAXCONN)
22 # define MAX_LISTEN SOMAXCONN
24 # define MAX_LISTEN 32
28 * BIO_socket - create a socket
29 * @domain: the socket domain (AF_INET, AF_INET6, AF_UNIX, ...)
30 * @socktype: the socket type (SOCK_STEAM, SOCK_DGRAM)
31 * @protocol: the protocol to use (IPPROTO_TCP, IPPROTO_UDP)
32 * @options: BIO socket options (currently unused)
34 * Creates a socket. This should be called before calling any
35 * of BIO_connect and BIO_listen.
37 * Returns the file descriptor on success or INVALID_SOCKET on failure. On
38 * failure errno is set, and a status is added to the OpenSSL error stack.
40 int BIO_socket(int domain, int socktype, int protocol, int options)
44 if (BIO_sock_init() != 1)
45 return INVALID_SOCKET;
47 sock = socket(domain, socktype, protocol);
49 SYSerr(SYS_F_SOCKET, get_last_socket_error());
50 BIOerr(BIO_F_BIO_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET);
51 return INVALID_SOCKET;
58 * BIO_connect - connect to an address
59 * @sock: the socket to connect with
60 * @addr: the address to connect to
61 * @options: BIO socket options
63 * Connects to the address using the given socket and options.
65 * Options can be a combination of the following:
66 * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
67 * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
68 * - BIO_SOCK_NODELAY: don't delay small messages.
70 * options holds BIO socket options that can be used
71 * You should call this for every address returned by BIO_lookup
72 * until the connection is successful.
74 * Returns 1 on success or 0 on failure. On failure errno is set
75 * and an error status is added to the OpenSSL error stack.
77 int BIO_connect(int sock, const BIO_ADDR *addr, int options)
82 BIOerr(BIO_F_BIO_CONNECT, BIO_R_INVALID_SOCKET);
86 if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
89 if (options & BIO_SOCK_KEEPALIVE) {
90 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
91 (const void *)&on, sizeof(on)) != 0) {
92 SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
93 BIOerr(BIO_F_BIO_CONNECT, BIO_R_UNABLE_TO_KEEPALIVE);
98 if (options & BIO_SOCK_NODELAY) {
99 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
100 (const void *)&on, sizeof(on)) != 0) {
101 SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
102 BIOerr(BIO_F_BIO_CONNECT, BIO_R_UNABLE_TO_NODELAY);
107 if (connect(sock, BIO_ADDR_sockaddr(addr),
108 BIO_ADDR_sockaddr_size(addr)) == -1) {
109 if (!BIO_sock_should_retry(-1)) {
110 SYSerr(SYS_F_CONNECT, get_last_socket_error());
111 BIOerr(BIO_F_BIO_CONNECT, BIO_R_CONNECT_ERROR);
119 * BIO_listen - Creates a listen socket
120 * @sock: the socket to listen with
121 * @addr: local address to bind to
122 * @options: BIO socket options
124 * Binds to the address using the given socket and options, then
125 * starts listening for incoming connections.
127 * Options can be a combination of the following:
128 * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
129 * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
130 * - BIO_SOCK_NODELAY: don't delay small messages.
131 * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
132 * for a recently closed port.
133 * - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only
134 * for IPv6 addresses and not IPv4 addresses mapped to IPv6.
136 * It's recommended that you set up both an IPv6 and IPv4 listen socket, and
137 * then check both for new clients that connect to it. You want to set up
138 * the socket as non-blocking in that case since else it could hang.
140 * Not all operating systems support IPv4 addresses on an IPv6 socket, and for
141 * others it's an option. If you pass the BIO_LISTEN_V6_ONLY it will try to
142 * create the IPv6 sockets to only listen for IPv6 connection.
144 * It could be that the first BIO_listen() call will listen to all the IPv6
145 * and IPv4 addresses and that then trying to bind to the IPv4 address will
146 * fail. We can't tell the difference between already listening ourself to
147 * it and someone else listening to it when failing and errno is EADDRINUSE, so
148 * it's recommended to not give an error in that case if the first call was
151 * When restarting the program it could be that the port is still in use. If
152 * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
153 * It's recommended that you use this.
155 int BIO_listen(int sock, const BIO_ADDR *addr, int options)
159 socklen_t socktype_len = sizeof(socktype);
162 BIOerr(BIO_F_BIO_LISTEN, BIO_R_INVALID_SOCKET);
166 if (getsockopt(sock, SOL_SOCKET, SO_TYPE,
167 (void *)&socktype, &socktype_len) != 0
168 || socktype_len != sizeof(socktype)) {
169 SYSerr(SYS_F_GETSOCKOPT, get_last_socket_error());
170 BIOerr(BIO_F_BIO_LISTEN, BIO_R_GETTING_SOCKTYPE);
174 if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
177 # ifndef OPENSSL_SYS_WINDOWS
179 * SO_REUSEADDR has different behavior on Windows than on
180 * other operating systems, don't set it there.
182 if (options & BIO_SOCK_REUSEADDR) {
183 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
184 (const void *)&on, sizeof(on)) != 0) {
185 SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
186 BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_REUSEADDR);
192 if (options & BIO_SOCK_KEEPALIVE) {
193 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
194 (const void *)&on, sizeof(on)) != 0) {
195 SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
196 BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_KEEPALIVE);
201 if (options & BIO_SOCK_NODELAY) {
202 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
203 (const void *)&on, sizeof(on)) != 0) {
204 SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
205 BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_NODELAY);
211 if (BIO_ADDR_family(addr) == AF_INET6) {
213 * Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF.
214 * Therefore we always have to use setsockopt here.
216 on = options & BIO_SOCK_V6_ONLY ? 1 : 0;
217 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
218 (const void *)&on, sizeof(on)) != 0) {
219 SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
220 BIOerr(BIO_F_BIO_LISTEN, BIO_R_LISTEN_V6_ONLY);
226 if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
227 SYSerr(SYS_F_BIND, get_last_socket_error());
228 BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_BIND_SOCKET);
232 if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) {
233 SYSerr(SYS_F_LISTEN, get_last_socket_error());
234 BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_LISTEN_SOCKET);
242 * BIO_accept_ex - Accept new incoming connections
243 * @sock: the listening socket
244 * @addr: the BIO_ADDR to store the peer address in
245 * @options: BIO socket options, applied on the accepted socket.
248 int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options)
253 BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_;
256 accepted_sock = accept(accept_sock,
257 BIO_ADDR_sockaddr_noconst(addr), &len);
258 if (accepted_sock == -1) {
259 if (!BIO_sock_should_retry(accepted_sock)) {
260 SYSerr(SYS_F_ACCEPT, get_last_socket_error());
261 BIOerr(BIO_F_BIO_ACCEPT_EX, BIO_R_ACCEPT_ERROR);
263 return INVALID_SOCKET;
266 if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) {
267 closesocket(accepted_sock);
268 return INVALID_SOCKET;
271 return accepted_sock;
275 * BIO_closesocket - Close a socket
276 * @sock: the socket to close
278 int BIO_closesocket(int sock)
280 if (closesocket(sock) < 0)