780f0f42b0046f9ac9643ed3c3845238b2dde572
[openssl.git] / crypto / bio / b_sock2.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13
14 #include "bio_lcl.h"
15
16 #include <openssl/err.h>
17
18 #ifndef OPENSSL_NO_SOCK
19 # ifdef SO_MAXCONN
20 #  define MAX_LISTEN  SO_MAXCONN
21 # elif defined(SOMAXCONN)
22 #  define MAX_LISTEN  SOMAXCONN
23 # else
24 #  define MAX_LISTEN  32
25 # endif
26
27 /*-
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)
33  *
34  * Creates a socket.  This should be called before calling any
35  * of BIO_connect and BIO_listen.
36  *
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.
39  */
40 int BIO_socket(int domain, int socktype, int protocol, int options)
41 {
42     int sock = -1;
43
44     if (BIO_sock_init() != 1)
45         return INVALID_SOCKET;
46
47     sock = socket(domain, socktype, protocol);
48     if (sock == -1) {
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;
52     }
53
54     return sock;
55 }
56
57 /*-
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
62  *
63  * Connects to the address using the given socket and options.
64  *
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.
69  *
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.
73  *
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.
76  */
77 int BIO_connect(int sock, const BIO_ADDR *addr, int options)
78 {
79     int on = 1;
80
81     if (sock == -1) {
82         BIOerr(BIO_F_BIO_CONNECT, BIO_R_INVALID_SOCKET);
83         return 0;
84     }
85
86     if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
87         return 0;
88
89     if (options & BIO_SOCK_KEEPALIVE) {
90         if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) != 0) {
91             SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
92             BIOerr(BIO_F_BIO_CONNECT, BIO_R_UNABLE_TO_KEEPALIVE);
93             return 0;
94         }
95     }
96
97     if (options & BIO_SOCK_NODELAY) {
98         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) {
99             SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
100             BIOerr(BIO_F_BIO_CONNECT, BIO_R_UNABLE_TO_NODELAY);
101             return 0;
102         }
103     }
104
105     if (connect(sock, BIO_ADDR_sockaddr(addr),
106                 BIO_ADDR_sockaddr_size(addr)) == -1) {
107         if (!BIO_sock_should_retry(-1)) {
108             SYSerr(SYS_F_CONNECT, get_last_socket_error());
109             BIOerr(BIO_F_BIO_CONNECT, BIO_R_CONNECT_ERROR);
110         }
111         return 0;
112     }
113     return 1;
114 }
115
116 /*-
117  * BIO_listen - Creates a listen socket
118  * @sock: the socket to listen with
119  * @addr: local address to bind to
120  * @options: BIO socket options
121  *
122  * Binds to the address using the given socket and options, then
123  * starts listening for incoming connections.
124  *
125  * Options can be a combination of the following:
126  * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
127  * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
128  * - BIO_SOCK_NODELAY: don't delay small messages.
129  * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
130  *   for a recently closed port.
131  * - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only
132  *   for IPv6 addresses and not IPv4 addresses mapped to IPv6.
133  *
134  * It's recommended that you set up both an IPv6 and IPv4 listen socket, and
135  * then check both for new clients that connect to it.  You want to set up
136  * the socket as non-blocking in that case since else it could hang.
137  *
138  * Not all operating systems support IPv4 addresses on an IPv6 socket, and for
139  * others it's an option.  If you pass the BIO_LISTEN_V6_ONLY it will try to
140  * create the IPv6 sockets to only listen for IPv6 connection.
141  *
142  * It could be that the first BIO_listen() call will listen to all the IPv6
143  * and IPv4 addresses and that then trying to bind to the IPv4 address will
144  * fail.  We can't tell the difference between already listening ourself to
145  * it and someone else listening to it when failing and errno is EADDRINUSE, so
146  * it's recommended to not give an error in that case if the first call was
147  * successful.
148  *
149  * When restarting the program it could be that the port is still in use.  If
150  * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
151  * It's recommended that you use this.
152  */
153 int BIO_listen(int sock, const BIO_ADDR *addr, int options)
154 {
155     int on = 1;
156     int socktype;
157     socklen_t socktype_len = sizeof(socktype);
158
159     if (sock == -1) {
160         BIOerr(BIO_F_BIO_LISTEN, BIO_R_INVALID_SOCKET);
161         return 0;
162     }
163
164     if (getsockopt(sock, SOL_SOCKET, SO_TYPE, &socktype, &socktype_len) != 0
165         || socktype_len != sizeof(socktype)) {
166         SYSerr(SYS_F_GETSOCKOPT, get_last_socket_error());
167         BIOerr(BIO_F_BIO_LISTEN, BIO_R_GETTING_SOCKTYPE);
168         return 0;
169     }
170
171     if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
172         return 0;
173
174 # ifndef OPENSSL_SYS_WINDOWS
175     /*
176      * SO_REUSEADDR has different behavior on Windows than on
177      * other operating systems, don't set it there.
178      */
179     if (options & BIO_SOCK_REUSEADDR) {
180         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
181             SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
182             BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_REUSEADDR);
183             return 0;
184         }
185     }
186 # endif
187
188     if (options & BIO_SOCK_KEEPALIVE) {
189         if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) != 0) {
190             SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
191             BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_KEEPALIVE);
192             return 0;
193         }
194     }
195
196     if (options & BIO_SOCK_NODELAY) {
197         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) {
198             SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
199             BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_NODELAY);
200             return 0;
201         }
202     }
203
204 # ifdef IPV6_V6ONLY
205     if (BIO_ADDR_family(addr) == AF_INET6) {
206         /*
207          * Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF.
208          * Therefore we always have to use setsockopt here.
209          */
210         on = options & BIO_SOCK_V6_ONLY ? 1 : 0;
211         if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) != 0) {
212             SYSerr(SYS_F_SETSOCKOPT, get_last_socket_error());
213             BIOerr(BIO_F_BIO_LISTEN, BIO_R_LISTEN_V6_ONLY);
214             return 0;
215         }
216     }
217 # endif
218
219     if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
220         SYSerr(SYS_F_BIND, get_last_socket_error());
221         BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_BIND_SOCKET);
222         return 0;
223     }
224
225     if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) {
226         SYSerr(SYS_F_LISTEN, get_last_socket_error());
227         BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_LISTEN_SOCKET);
228         return 0;
229     }
230
231     return 1;
232 }
233
234 /*-
235  * BIO_accept_ex - Accept new incoming connections
236  * @sock: the listening socket
237  * @addr: the BIO_ADDR to store the peer address in
238  * @options: BIO socket options, applied on the accepted socket.
239  *
240  */
241 int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options)
242 {
243     socklen_t len;
244     int accepted_sock;
245     BIO_ADDR locaddr;
246     BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_;
247
248     len = sizeof(*addr);
249     accepted_sock = accept(accept_sock,
250                            BIO_ADDR_sockaddr_noconst(addr), &len);
251     if (accepted_sock == -1) {
252         if (!BIO_sock_should_retry(accepted_sock)) {
253             SYSerr(SYS_F_ACCEPT, get_last_socket_error());
254             BIOerr(BIO_F_BIO_ACCEPT_EX, BIO_R_ACCEPT_ERROR);
255         }
256         return INVALID_SOCKET;
257     }
258
259     if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) {
260         closesocket(accepted_sock);
261         return INVALID_SOCKET;
262     }
263
264     return accepted_sock;
265 }
266
267 /*-
268  * BIO_closesocket - Close a socket
269  * @sock: the socket to close
270  */
271 int BIO_closesocket(int sock)
272 {
273     if (closesocket(sock) < 0)
274         return 0;
275     return 1;
276 }
277 #endif