Coverage Report

Created: 2018-08-29 13:53

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