Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/bio/bio_sock2.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <errno.h>
13
14
#include "bio_local.h"
15
#include "internal/ktls.h"
16
17
#include <openssl/err.h>
18
19
#ifndef OPENSSL_NO_SOCK
20
#ifdef SO_MAXCONN
21
#define MAX_LISTEN SO_MAXCONN
22
#elif defined(SOMAXCONN)
23
0
#define MAX_LISTEN SOMAXCONN
24
#else
25
#define MAX_LISTEN 32
26
#endif
27
28
/*-
29
 * BIO_socket - create a socket
30
 * @domain: the socket domain (AF_INET, AF_INET6, AF_UNIX, ...)
31
 * @socktype: the socket type (SOCK_STEAM, SOCK_DGRAM)
32
 * @protocol: the protocol to use (IPPROTO_TCP, IPPROTO_UDP)
33
 * @options: BIO socket options (currently unused)
34
 *
35
 * Creates a socket.  This should be called before calling any
36
 * of BIO_connect and BIO_listen.
37
 *
38
 * Returns the file descriptor on success or INVALID_SOCKET on failure.  On
39
 * failure errno is set, and a status is added to the OpenSSL error stack.
40
 */
41
int BIO_socket(int domain, int socktype, int protocol, int options)
42
0
{
43
0
    int sock = -1;
44
45
0
    if (BIO_sock_init() != 1)
46
0
        return INVALID_SOCKET;
47
48
0
    sock = socket(domain, socktype, protocol);
49
0
    if (sock == -1) {
50
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
51
0
            "calling socket()");
52
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
53
0
        return INVALID_SOCKET;
54
0
    }
55
56
0
    return sock;
57
0
}
58
59
/*-
60
 * BIO_connect - connect to an address
61
 * @sock: the socket to connect with
62
 * @addr: the address to connect to
63
 * @options: BIO socket options
64
 *
65
 * Connects to the address using the given socket and options.
66
 *
67
 * Options can be a combination of the following:
68
 * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
69
 * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
70
 * - BIO_SOCK_NODELAY: don't delay small messages.
71
 *
72
 * options holds BIO socket options that can be used
73
 * You should call this for every address returned by BIO_lookup
74
 * until the connection is successful.
75
 *
76
 * Returns 1 on success or 0 on failure.  On failure errno is set
77
 * and an error status is added to the OpenSSL error stack.
78
 */
79
int BIO_connect(int sock, const BIO_ADDR *addr, int options)
80
0
{
81
0
    const int on = 1;
82
83
0
    if (sock == -1) {
84
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
85
0
        return 0;
86
0
    }
87
88
0
    if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
89
0
        return 0;
90
91
0
    if (options & BIO_SOCK_KEEPALIVE) {
92
0
        if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
93
0
                (const void *)&on, sizeof(on))
94
0
            != 0) {
95
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
96
0
                "calling setsockopt()");
97
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
98
0
            return 0;
99
0
        }
100
0
    }
101
102
0
    if (options & BIO_SOCK_NODELAY) {
103
0
        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
104
0
                (const void *)&on, sizeof(on))
105
0
            != 0) {
106
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
107
0
                "calling setsockopt()");
108
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
109
0
            return 0;
110
0
        }
111
0
    }
112
113
0
    if (connect(sock, BIO_ADDR_sockaddr(addr),
114
0
            BIO_ADDR_sockaddr_size(addr))
115
0
        == -1) {
116
0
        if (!BIO_sock_should_retry(-1)) {
117
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
118
0
                "calling connect()");
119
0
            ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
120
0
        }
121
0
        return 0;
122
0
    }
123
0
    return 1;
124
0
}
125
126
/*-
127
 * BIO_bind - bind socket to address
128
 * @sock: the socket to set
129
 * @addr: local address to bind to
130
 * @options: BIO socket options
131
 *
132
 * Binds to the address using the given socket and options.
133
 *
134
 * Options can be a combination of the following:
135
 * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
136
 *   for a recently closed port.
137
 *
138
 * When restarting the program it could be that the port is still in use.  If
139
 * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
140
 * It's recommended that you use this.
141
 */
142
int BIO_bind(int sock, const BIO_ADDR *addr, int options)
143
0
{
144
0
#ifndef OPENSSL_SYS_WINDOWS
145
0
    int on = 1;
146
0
#endif
147
148
0
    if (sock == -1) {
149
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
150
0
        return 0;
151
0
    }
152
153
0
#ifndef OPENSSL_SYS_WINDOWS
154
    /*
155
     * SO_REUSEADDR has different behavior on Windows than on
156
     * other operating systems, don't set it there.
157
     */
158
0
    if (options & BIO_SOCK_REUSEADDR) {
159
0
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
160
0
                (const void *)&on, sizeof(on))
161
0
            != 0) {
162
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
163
0
                "calling setsockopt()");
164
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_REUSEADDR);
165
0
            return 0;
166
0
        }
167
0
    }
168
0
#endif
169
170
0
    if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
171
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error() /* may be 0 */,
172
0
            "calling bind()");
173
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_BIND_SOCKET);
174
0
        return 0;
175
0
    }
176
177
0
    return 1;
178
0
}
179
180
/*-
181
 * BIO_listen - Creates a listen socket
182
 * @sock: the socket to listen with
183
 * @addr: local address to bind to
184
 * @options: BIO socket options
185
 *
186
 * Binds to the address using the given socket and options, then
187
 * starts listening for incoming connections.
188
 *
189
 * Options can be a combination of the following:
190
 * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
191
 * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
192
 * - BIO_SOCK_NODELAY: don't delay small messages.
193
 * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
194
 *   for a recently closed port.
195
 * - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only
196
 *   for IPv6 addresses and not IPv4 addresses mapped to IPv6.
197
 *
198
 * It's recommended that you set up both an IPv6 and IPv4 listen socket, and
199
 * then check both for new clients that connect to it.  You want to set up
200
 * the socket as non-blocking in that case since else it could hang.
201
 *
202
 * Not all operating systems support IPv4 addresses on an IPv6 socket, and for
203
 * others it's an option.  If you pass the BIO_LISTEN_V6_ONLY it will try to
204
 * create the IPv6 sockets to only listen for IPv6 connection.
205
 *
206
 * It could be that the first BIO_listen() call will listen to all the IPv6
207
 * and IPv4 addresses and that then trying to bind to the IPv4 address will
208
 * fail.  We can't tell the difference between already listening ourself to
209
 * it and someone else listening to it when failing and errno is EADDRINUSE, so
210
 * it's recommended to not give an error in that case if the first call was
211
 * successful.
212
 *
213
 * When restarting the program it could be that the port is still in use.  If
214
 * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
215
 * It's recommended that you use this.
216
 */
217
int BIO_listen(int sock, const BIO_ADDR *addr, int options)
218
0
{
219
0
    int on = 1;
220
0
    int socktype;
221
0
    socklen_t socktype_len = sizeof(socktype);
222
223
0
    if (sock == -1) {
224
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
225
0
        return 0;
226
0
    }
227
228
0
    if (getsockopt(sock, SOL_SOCKET, SO_TYPE,
229
0
            (void *)&socktype, &socktype_len)
230
0
            != 0
231
0
        || socktype_len != sizeof(socktype)) {
232
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
233
0
            "calling getsockopt()");
234
0
        ERR_raise(ERR_LIB_BIO, BIO_R_GETTING_SOCKTYPE);
235
0
        return 0;
236
0
    }
237
238
0
    if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
239
0
        return 0;
240
241
0
    if (options & BIO_SOCK_KEEPALIVE) {
242
0
        if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
243
0
                (const void *)&on, sizeof(on))
244
0
            != 0) {
245
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
246
0
                "calling setsockopt()");
247
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
248
0
            return 0;
249
0
        }
250
0
    }
251
252
0
    if (options & BIO_SOCK_NODELAY) {
253
0
        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
254
0
                (const void *)&on, sizeof(on))
255
0
            != 0) {
256
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
257
0
                "calling setsockopt()");
258
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
259
0
            return 0;
260
0
        }
261
0
    }
262
263
    /* On OpenBSD it is always ipv6 only with ipv6 sockets thus read-only */
264
0
#if defined(IPV6_V6ONLY) && !defined(__OpenBSD__)
265
0
    if (BIO_ADDR_family(addr) == AF_INET6) {
266
        /*
267
         * Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF.
268
         * Therefore we always have to use setsockopt here.
269
         */
270
0
        on = options & BIO_SOCK_V6_ONLY ? 1 : 0;
271
0
        if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
272
0
                (const void *)&on, sizeof(on))
273
0
            != 0) {
274
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
275
0
                "calling setsockopt()");
276
0
            ERR_raise(ERR_LIB_BIO, BIO_R_LISTEN_V6_ONLY);
277
0
            return 0;
278
0
        }
279
0
    }
280
0
#endif
281
282
0
    if (!BIO_bind(sock, addr, options))
283
0
        return 0;
284
285
0
    if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) {
286
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
287
0
            "calling listen()");
288
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_LISTEN_SOCKET);
289
0
        return 0;
290
0
    }
291
292
0
    return 1;
293
0
}
294
295
/*-
296
 * BIO_accept_ex - Accept new incoming connections
297
 * @sock: the listening socket
298
 * @addr: the BIO_ADDR to store the peer address in
299
 * @options: BIO socket options, applied on the accepted socket.
300
 *
301
 */
302
int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options)
303
0
{
304
0
    socklen_t len;
305
0
    int accepted_sock;
306
0
    BIO_ADDR locaddr;
307
0
    BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_;
308
309
0
    len = sizeof(*addr);
310
0
    accepted_sock = accept(accept_sock,
311
0
        BIO_ADDR_sockaddr_noconst(addr), &len);
312
0
    if (accepted_sock == -1) {
313
0
        if (!BIO_sock_should_retry(accepted_sock)) {
314
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
315
0
                "calling accept()");
316
0
            ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
317
0
        }
318
0
        return INVALID_SOCKET;
319
0
    }
320
321
0
    if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) {
322
0
        closesocket(accepted_sock);
323
0
        return INVALID_SOCKET;
324
0
    }
325
326
0
    return accepted_sock;
327
0
}
328
329
/*-
330
 * BIO_closesocket - Close a socket
331
 * @sock: the socket to close
332
 */
333
int BIO_closesocket(int sock)
334
0
{
335
0
    if (sock < 0 || closesocket(sock) < 0)
336
0
        return 0;
337
0
    return 1;
338
0
}
339
#endif