Coverage Report

Created: 2025-11-16 06:40

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)) != 0) {
94
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
95
0
                           "calling setsockopt()");
96
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
97
0
            return 0;
98
0
        }
99
0
    }
100
101
0
    if (options & BIO_SOCK_NODELAY) {
102
0
        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
103
0
                       (const void *)&on, sizeof(on)) != 0) {
104
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
105
0
                           "calling setsockopt()");
106
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
107
0
            return 0;
108
0
        }
109
0
    }
110
111
0
    if (connect(sock, BIO_ADDR_sockaddr(addr),
112
0
                BIO_ADDR_sockaddr_size(addr)) == -1) {
113
0
        if (!BIO_sock_should_retry(-1)) {
114
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
115
0
                           "calling connect()");
116
0
            ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
117
0
        }
118
0
        return 0;
119
0
    }
120
0
    return 1;
121
0
}
122
123
/*-
124
 * BIO_bind - bind socket to address
125
 * @sock: the socket to set
126
 * @addr: local address to bind to
127
 * @options: BIO socket options
128
 *
129
 * Binds to the address using the given socket and options.
130
 *
131
 * Options can be a combination of the following:
132
 * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
133
 *   for a recently closed port.
134
 *
135
 * When restarting the program it could be that the port is still in use.  If
136
 * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
137
 * It's recommended that you use this.
138
 */
139
int BIO_bind(int sock, const BIO_ADDR *addr, int options)
140
0
{
141
0
# ifndef OPENSSL_SYS_WINDOWS
142
0
    int on = 1;
143
0
# endif
144
145
0
    if (sock == -1) {
146
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
147
0
        return 0;
148
0
    }
149
150
0
# ifndef OPENSSL_SYS_WINDOWS
151
    /*
152
     * SO_REUSEADDR has different behavior on Windows than on
153
     * other operating systems, don't set it there.
154
     */
155
0
    if (options & BIO_SOCK_REUSEADDR) {
156
0
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
157
0
                       (const void *)&on, sizeof(on)) != 0) {
158
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
159
0
                           "calling setsockopt()");
160
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_REUSEADDR);
161
0
            return 0;
162
0
        }
163
0
    }
164
0
# endif
165
166
0
    if (bind(sock, BIO_ADDR_sockaddr(addr), BIO_ADDR_sockaddr_size(addr)) != 0) {
167
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error() /* may be 0 */,
168
0
                       "calling bind()");
169
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_BIND_SOCKET);
170
0
        return 0;
171
0
    }
172
173
0
    return 1;
174
0
}
175
176
/*-
177
 * BIO_listen - Creates a listen socket
178
 * @sock: the socket to listen with
179
 * @addr: local address to bind to
180
 * @options: BIO socket options
181
 *
182
 * Binds to the address using the given socket and options, then
183
 * starts listening for incoming connections.
184
 *
185
 * Options can be a combination of the following:
186
 * - BIO_SOCK_KEEPALIVE: enable regularly sending keep-alive messages.
187
 * - BIO_SOCK_NONBLOCK: Make the socket non-blocking.
188
 * - BIO_SOCK_NODELAY: don't delay small messages.
189
 * - BIO_SOCK_REUSEADDR: Try to reuse the address and port combination
190
 *   for a recently closed port.
191
 * - BIO_SOCK_V6_ONLY: When creating an IPv6 socket, make it listen only
192
 *   for IPv6 addresses and not IPv4 addresses mapped to IPv6.
193
 *
194
 * It's recommended that you set up both an IPv6 and IPv4 listen socket, and
195
 * then check both for new clients that connect to it.  You want to set up
196
 * the socket as non-blocking in that case since else it could hang.
197
 *
198
 * Not all operating systems support IPv4 addresses on an IPv6 socket, and for
199
 * others it's an option.  If you pass the BIO_LISTEN_V6_ONLY it will try to
200
 * create the IPv6 sockets to only listen for IPv6 connection.
201
 *
202
 * It could be that the first BIO_listen() call will listen to all the IPv6
203
 * and IPv4 addresses and that then trying to bind to the IPv4 address will
204
 * fail.  We can't tell the difference between already listening ourself to
205
 * it and someone else listening to it when failing and errno is EADDRINUSE, so
206
 * it's recommended to not give an error in that case if the first call was
207
 * successful.
208
 *
209
 * When restarting the program it could be that the port is still in use.  If
210
 * you set to BIO_SOCK_REUSEADDR option it will try to reuse the port anyway.
211
 * It's recommended that you use this.
212
 */
213
int BIO_listen(int sock, const BIO_ADDR *addr, int options)
214
0
{
215
0
    int on = 1;
216
0
    int socktype;
217
0
    socklen_t socktype_len = sizeof(socktype);
218
219
0
    if (sock == -1) {
220
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_SOCKET);
221
0
        return 0;
222
0
    }
223
224
0
    if (getsockopt(sock, SOL_SOCKET, SO_TYPE,
225
0
                   (void *)&socktype, &socktype_len) != 0
226
0
        || socktype_len != sizeof(socktype)) {
227
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
228
0
                       "calling getsockopt()");
229
0
        ERR_raise(ERR_LIB_BIO, BIO_R_GETTING_SOCKTYPE);
230
0
        return 0;
231
0
    }
232
233
0
    if (!BIO_socket_nbio(sock, (options & BIO_SOCK_NONBLOCK) != 0))
234
0
        return 0;
235
236
0
    if (options & BIO_SOCK_KEEPALIVE) {
237
0
        if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
238
0
                       (const void *)&on, sizeof(on)) != 0) {
239
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
240
0
                           "calling setsockopt()");
241
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_KEEPALIVE);
242
0
            return 0;
243
0
        }
244
0
    }
245
246
0
    if (options & BIO_SOCK_NODELAY) {
247
0
        if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
248
0
                       (const void *)&on, sizeof(on)) != 0) {
249
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
250
0
                           "calling setsockopt()");
251
0
            ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_NODELAY);
252
0
            return 0;
253
0
        }
254
0
    }
255
256
  /* On OpenBSD it is always ipv6 only with ipv6 sockets thus read-only */
257
0
# if defined(IPV6_V6ONLY) && !defined(__OpenBSD__)
258
0
    if (BIO_ADDR_family(addr) == AF_INET6) {
259
        /*
260
         * Note: Windows default of IPV6_V6ONLY is ON, and Linux is OFF.
261
         * Therefore we always have to use setsockopt here.
262
         */
263
0
        on = options & BIO_SOCK_V6_ONLY ? 1 : 0;
264
0
        if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
265
0
                       (const void *)&on, sizeof(on)) != 0) {
266
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
267
0
                           "calling setsockopt()");
268
0
            ERR_raise(ERR_LIB_BIO, BIO_R_LISTEN_V6_ONLY);
269
0
            return 0;
270
0
        }
271
0
    }
272
0
# endif
273
274
0
    if (!BIO_bind(sock, addr, options))
275
0
        return 0;
276
277
0
    if (socktype != SOCK_DGRAM && listen(sock, MAX_LISTEN) == -1) {
278
0
        ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
279
0
                       "calling listen()");
280
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_LISTEN_SOCKET);
281
0
        return 0;
282
0
    }
283
284
0
    return 1;
285
0
}
286
287
/*-
288
 * BIO_accept_ex - Accept new incoming connections
289
 * @sock: the listening socket
290
 * @addr: the BIO_ADDR to store the peer address in
291
 * @options: BIO socket options, applied on the accepted socket.
292
 *
293
 */
294
int BIO_accept_ex(int accept_sock, BIO_ADDR *addr_, int options)
295
0
{
296
0
    socklen_t len;
297
0
    int accepted_sock;
298
0
    BIO_ADDR locaddr;
299
0
    BIO_ADDR *addr = addr_ == NULL ? &locaddr : addr_;
300
301
0
    len = sizeof(*addr);
302
0
    accepted_sock = accept(accept_sock,
303
0
                           BIO_ADDR_sockaddr_noconst(addr), &len);
304
0
    if (accepted_sock == -1) {
305
0
        if (!BIO_sock_should_retry(accepted_sock)) {
306
0
            ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
307
0
                           "calling accept()");
308
0
            ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
309
0
        }
310
0
        return INVALID_SOCKET;
311
0
    }
312
313
0
    if (!BIO_socket_nbio(accepted_sock, (options & BIO_SOCK_NONBLOCK) != 0)) {
314
0
        closesocket(accepted_sock);
315
0
        return INVALID_SOCKET;
316
0
    }
317
318
0
    return accepted_sock;
319
0
}
320
321
/*-
322
 * BIO_closesocket - Close a socket
323
 * @sock: the socket to close
324
 */
325
int BIO_closesocket(int sock)
326
0
{
327
0
    if (sock < 0 || closesocket(sock) < 0)
328
0
        return 0;
329
0
    return 1;
330
0
}
331
#endif