]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
6738bf14 | 2 | * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. |
d33b215b | 3 | * |
09abbca1 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d33b215b RL |
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) { | |
ff988500 RS |
49 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
50 | "calling socket()"); | |
d33b215b RL |
51 | BIOerr(BIO_F_BIO_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET); |
52 | return INVALID_SOCKET; | |
53 | } | |
54 | ||
55 | return sock; | |
56 | } | |
57 | ||
58 | /*- | |
59 | * BIO_connect - connect to an address | |
60 | * @sock: the socket to connect with | |
61 | * @addr: the address to connect to | |
62 | * @options: BIO socket options | |
63 | * | |
64 | * Connects to the address using the given socket and options. | |
65 | * | |
66 | * Options can be a combination of the following: | |
67 | * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages. | |
68 | * - BIO_SOCK_NONBLOCK: Make the socket non-blocking. | |
69 | * - BIO_SOCK_NODELAY: don't delay small messages. | |
70 | * | |
71 | * options holds BIO socket options that can be used | |
72 | * You should call this for every address returned by BIO_lookup | |
60250017 | 73 | * until the connection is successful. |
d33b215b RL |
74 | * |
75 | * Returns 1 on success or 0 on failure. On failure errno is set | |
76 | * and an error status is added to the OpenSSL error stack. | |
77 | */ | |
78 | int BIO_connect(int sock, const BIO_ADDR *addr, int options) | |
79 | { | |
3a63c0ed | 80 | const int on = 1; |
d33b215b RL |
81 | |
82 | if (sock == -1) { | |
83 | BIOerr(BIO_F_BIO_CONNECT, BIO_R_INVALID_SOCKET); | |
84 | return 0; | |
85 | } | |
86 | ||
87 | if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0)) | |
88 | return 0; | |
89 | ||
90 | if (options & BIO_SOCK_KEEPALIVE) { | |
3a63c0ed AP |
91 | if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
92 | (const void *)&on, sizeof(on)) != 0) { | |
ff988500 RS |
93 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
94 | "calling setsockopt()"); | |
d33b215b RL |
95 | BIOerr(BIO_F_BIO_CONNECT, BIO_R_UNABLE_TO_KEEPALIVE); |
96 | return 0; | |
97 | } | |
98 | } | |
99 | ||
100 | if (options & BIO_SOCK_NODELAY) { | |
3a63c0ed AP |
101 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
102 | (const void *)&on, sizeof(on)) != 0) { | |
ff988500 RS |
103 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
104 | "calling setsockopt()"); | |
d33b215b RL |
105 | BIOerr(BIO_F_BIO_CONNECT, BIO_R_UNABLE_TO_NODELAY); |
106 | return 0; | |
107 | } | |
108 | } | |
109 | ||
110 | if (connect(sock, BIO_ADDR_sockaddr(addr), | |
111 | BIO_ADDR_sockaddr_size(addr)) == -1) { | |
81161070 | 112 | if (!BIO_sock_should_retry(-1)) { |
ff988500 RS |
113 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
114 | "calling connect()"); | |
81161070 MC |
115 | BIOerr(BIO_F_BIO_CONNECT, BIO_R_CONNECT_ERROR); |
116 | } | |
d33b215b RL |
117 | return 0; |
118 | } | |
119 | return 1; | |
120 | } | |
121 | ||
ebc01683 JH |
122 | /*- |
123 | * BIO_bind - bind socket to address | |
124 | * @sock: the socket to set | |
125 | * @addr: local address to bind to | |
126 | * @options: BIO socket options | |
127 | * | |
128 | * Binds to the address using the given socket and options. | |
129 | * | |
130 | * Options can be a combination of the following: | |
131 | * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination | |
132 | * for a recently closed port. | |
133 | * | |
134 | * When restarting the program it could be that the port is still in use. If | |
135 | * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway. | |
136 | * It's recommended that you use this. | |
137 | */ | |
138 | int BIO_bind(int sock, const BIO_ADDR *addr, int options) | |
139 | { | |
af7d8d34 | 140 | # ifndef OPENSSL_SYS_WINDOWS |
ebc01683 | 141 | int on = 1; |
af7d8d34 | 142 | # endif |
ebc01683 JH |
143 | |
144 | if (sock == -1) { | |
145 | BIOerr(BIO_F_BIO_BIND, BIO_R_INVALID_SOCKET); | |
146 | return 0; | |
147 | } | |
148 | ||
149 | # ifndef OPENSSL_SYS_WINDOWS | |
150 | /* | |
151 | * SO_REUSEADDR has different behavior on Windows than on | |
152 | * other operating systems, don't set it there. | |
153 | */ | |
154 | if (options & BIO_SOCK_REUSEADDR) { | |
155 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, | |
156 | (const void *)&on, sizeof(on)) != 0) { | |
ff988500 RS |
157 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
158 | "calling setsockopt()"); | |
ebc01683 JH |
159 | BIOerr(BIO_F_BIO_BIND, BIO_R_UNABLE_TO_REUSEADDR); |
160 | return 0; | |
161 | } | |
162 | } | |
163 | # endif | |
164 | ||
165 | if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) { | |
ff988500 RS |
166 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
167 | "calling bind()"); | |
ebc01683 JH |
168 | BIOerr(BIO_F_BIO_BIND, BIO_R_UNABLE_TO_BIND_SOCKET); |
169 | return 0; | |
170 | } | |
171 | ||
172 | return 1; | |
173 | } | |
174 | ||
d33b215b RL |
175 | /*- |
176 | * BIO_listen - Creates a listen socket | |
177 | * @sock: the socket to listen with | |
178 | * @addr: local address to bind to | |
179 | * @options: BIO socket options | |
180 | * | |
181 | * Binds to the address using the given socket and options, then | |
182 | * starts listening for incoming connections. | |
183 | * | |
184 | * Options can be a combination of the following: | |
185 | * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages. | |
186 | * - BIO_SOCK_NONBLOCK: Make the socket non-blocking. | |
187 | * - BIO_SOCK_NODELAY: don't delay small messages. | |
188 | * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination | |
189 | * for a recently closed port. | |
190 | * - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only | |
191 | * for IPv6 addresses and not IPv4 addresses mapped to IPv6. | |
192 | * | |
193 | * It's recommended that you set up both an IPv6 and IPv4 listen socket, and | |
194 | * then check both for new clients that connect to it. You want to set up | |
195 | * the socket as non-blocking in that case since else it could hang. | |
196 | * | |
197 | * Not all operating systems support IPv4 addresses on an IPv6 socket, and for | |
198 | * others it's an option. If you pass the BIO_LISTEN_V6_ONLY it will try to | |
199 | * create the IPv6 sockets to only listen for IPv6 connection. | |
200 | * | |
201 | * It could be that the first BIO_listen() call will listen to all the IPv6 | |
202 | * and IPv4 addresses and that then trying to bind to the IPv4 address will | |
203 | * fail. We can't tell the difference between already listening ourself to | |
204 | * it and someone else listening to it when failing and errno is EADDRINUSE, so | |
205 | * it's recommended to not give an error in that case if the first call was | |
60250017 | 206 | * successful. |
d33b215b RL |
207 | * |
208 | * When restarting the program it could be that the port is still in use. If | |
209 | * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway. | |
210 | * It's recommended that you use this. | |
211 | */ | |
212 | int BIO_listen(int sock, const BIO_ADDR *addr, int options) | |
213 | { | |
214 | int on = 1; | |
215 | int socktype; | |
216 | socklen_t socktype_len = sizeof(socktype); | |
217 | ||
218 | if (sock == -1) { | |
219 | BIOerr(BIO_F_BIO_LISTEN, BIO_R_INVALID_SOCKET); | |
220 | return 0; | |
221 | } | |
222 | ||
3a63c0ed AP |
223 | if (getsockopt(sock, SOL_SOCKET, SO_TYPE, |
224 | (void *)&socktype, &socktype_len) != 0 | |
d33b215b | 225 | || socktype_len != sizeof(socktype)) { |
ff988500 RS |
226 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
227 | "calling getsockopt()"); | |
d33b215b RL |
228 | BIOerr(BIO_F_BIO_LISTEN, BIO_R_GETTING_SOCKTYPE); |
229 | return 0; | |
230 | } | |
231 | ||
232 | if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0)) | |
233 | return 0; | |
234 | ||
d33b215b | 235 | if (options & BIO_SOCK_KEEPALIVE) { |
3a63c0ed AP |
236 | if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
237 | (const void *)&on, sizeof(on)) != 0) { | |
ff988500 RS |
238 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
239 | "calling setsockopt()"); | |
d33b215b RL |
240 | BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_KEEPALIVE); |
241 | return 0; | |
242 | } | |
243 | } | |
244 | ||
245 | if (options & BIO_SOCK_NODELAY) { | |
3a63c0ed AP |
246 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
247 | (const void *)&on, sizeof(on)) != 0) { | |
ff988500 RS |
248 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
249 | "calling setsockopt()"); | |
d33b215b RL |
250 | BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_NODELAY); |
251 | return 0; | |
252 | } | |
253 | } | |
254 | ||
255 | # ifdef IPV6_V6ONLY | |
f1a0f9fa BE |
256 | if (BIO_ADDR_family(addr) == AF_INET6) { |
257 | /* | |
258 | * Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF. | |
259 | * Therefore we always have to use setsockopt here. | |
260 | */ | |
261 | on = options & BIO_SOCK_V6_ONLY ? 1 : 0; | |
3a63c0ed AP |
262 | if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
263 | (const void *)&on, sizeof(on)) != 0) { | |
ff988500 RS |
264 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
265 | "calling setsockopt()"); | |
d33b215b RL |
266 | BIOerr(BIO_F_BIO_LISTEN, BIO_R_LISTEN_V6_ONLY); |
267 | return 0; | |
268 | } | |
269 | } | |
270 | # endif | |
271 | ||
ebc01683 | 272 | if (!BIO_bind(sock, addr, options)) |
d33b215b | 273 | return 0; |
d33b215b RL |
274 | |
275 | if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) { | |
ff988500 RS |
276 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
277 | "calling listen()"); | |
d33b215b RL |
278 | BIOerr(BIO_F_BIO_LISTEN, BIO_R_UNABLE_TO_LISTEN_SOCKET); |
279 | return 0; | |
280 | } | |
281 | ||
282 | return 1; | |
283 | } | |
284 | ||
285 | /*- | |
286 | * BIO_accept_ex - Accept new incoming connections | |
287 | * @sock: the listening socket | |
288 | * @addr: the BIO_ADDR to store the peer address in | |
289 | * @options: BIO socket options, applied on the accepted socket. | |
290 | * | |
291 | */ | |
d9d8e7a9 | 292 | int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options) |
d33b215b RL |
293 | { |
294 | socklen_t len; | |
295 | int accepted_sock; | |
d9d8e7a9 RS |
296 | BIO_ADDR locaddr; |
297 | BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_; | |
d33b215b RL |
298 | |
299 | len = sizeof(*addr); | |
300 | accepted_sock = accept(accept_sock, | |
301 | BIO_ADDR_sockaddr_noconst(addr), &len); | |
302 | if (accepted_sock == -1) { | |
81161070 | 303 | if (!BIO_sock_should_retry(accepted_sock)) { |
ff988500 RS |
304 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
305 | "calling accept()"); | |
81161070 MC |
306 | BIOerr(BIO_F_BIO_ACCEPT_EX, BIO_R_ACCEPT_ERROR); |
307 | } | |
d33b215b RL |
308 | return INVALID_SOCKET; |
309 | } | |
310 | ||
df0f2759 MC |
311 | if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) { |
312 | closesocket(accepted_sock); | |
d33b215b | 313 | return INVALID_SOCKET; |
df0f2759 | 314 | } |
d33b215b RL |
315 | |
316 | return accepted_sock; | |
317 | } | |
318 | ||
319 | /*- | |
320 | * BIO_closesocket - Close a socket | |
321 | * @sock: the socket to close | |
322 | */ | |
323 | int BIO_closesocket(int sock) | |
324 | { | |
325 | if (closesocket(sock) < 0) | |
326 | return 0; | |
327 | return 1; | |
328 | } | |
329 | #endif |