Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/bio/b_sock.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-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
#include "bio_lcl.h"
14
#ifndef OPENSSL_NO_SOCK
15
# define SOCKET_PROTOCOL IPPROTO_TCP
16
# ifdef SO_MAXCONN
17
#  define MAX_LISTEN  SO_MAXCONN
18
# elif defined(SOMAXCONN)
19
#  define MAX_LISTEN  SOMAXCONN
20
# else
21
#  define MAX_LISTEN  32
22
# endif
23
# if defined(OPENSSL_SYS_WINDOWS)
24
static int wsa_init_done = 0;
25
# endif
26
27
# if OPENSSL_API_COMPAT < 0x10100000L
28
int BIO_get_host_ip(const char *str, unsigned char *ip)
29
0
{
30
0
    BIO_ADDRINFO *res = NULL;
31
0
    int ret = 0;
32
0
33
0
    if (BIO_sock_init() != 1)
34
0
        return 0;               /* don't generate another error code here */
35
0
36
0
    if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
37
0
        size_t l;
38
0
39
0
        if (BIO_ADDRINFO_family(res) != AF_INET) {
40
0
            BIOerr(BIO_F_BIO_GET_HOST_IP,
41
0
                   BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
42
0
        } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l)) {
43
0
            /*
44
0
             * Because only AF_INET addresses will reach this far, we can assert
45
0
             * that l should be 4
46
0
             */
47
0
            if (ossl_assert(l == 4))
48
0
                ret = BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
49
0
        }
50
0
        BIO_ADDRINFO_free(res);
51
0
    } else {
52
0
        ERR_add_error_data(2, "host=", str);
53
0
    }
54
0
55
0
    return ret;
56
0
}
57
58
int BIO_get_port(const char *str, unsigned short *port_ptr)
59
0
{
60
0
    BIO_ADDRINFO *res = NULL;
61
0
    int ret = 0;
62
0
63
0
    if (str == NULL) {
64
0
        BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
65
0
        return 0;
66
0
    }
67
0
68
0
    if (BIO_sock_init() != 1)
69
0
        return 0;               /* don't generate another error code here */
70
0
71
0
    if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
72
0
        if (BIO_ADDRINFO_family(res) != AF_INET) {
73
0
            BIOerr(BIO_F_BIO_GET_PORT,
74
0
                   BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
75
0
        } else {
76
0
            *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
77
0
            ret = 1;
78
0
        }
79
0
        BIO_ADDRINFO_free(res);
80
0
    } else {
81
0
        ERR_add_error_data(2, "host=", str);
82
0
    }
83
0
84
0
    return ret;
85
0
}
86
# endif
87
88
int BIO_sock_error(int sock)
89
0
{
90
0
    int j = 0, i;
91
0
    socklen_t size = sizeof(j);
92
0
93
0
    /*
94
0
     * Note: under Windows the third parameter is of type (char *) whereas
95
0
     * under other systems it is (void *) if you don't have a cast it will
96
0
     * choke the compiler: if you do have a cast then you can either go for
97
0
     * (char *) or (void *).
98
0
     */
99
0
    i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
100
0
    if (i < 0)
101
0
        return get_last_socket_error();
102
0
    else
103
0
        return j;
104
0
}
105
106
# if OPENSSL_API_COMPAT < 0x10100000L
107
struct hostent *BIO_gethostbyname(const char *name)
108
0
{
109
0
    /*
110
0
     * Caching gethostbyname() results forever is wrong, so we have to let
111
0
     * the true gethostbyname() worry about this
112
0
     */
113
0
    return gethostbyname(name);
114
0
}
115
# endif
116
117
int BIO_sock_init(void)
118
0
{
119
# ifdef OPENSSL_SYS_WINDOWS
120
    static struct WSAData wsa_state;
121
122
    if (!wsa_init_done) {
123
        int err;
124
125
        wsa_init_done = 1;
126
        memset(&wsa_state, 0, sizeof(wsa_state));
127
        /*
128
         * Not making wsa_state available to the rest of the code is formally
129
         * wrong. But the structures we use are [believed to be] invariable
130
         * among Winsock DLLs, while API availability is [expected to be]
131
         * probed at run-time with DSO_global_lookup.
132
         */
133
        if (WSAStartup(0x0202, &wsa_state) != 0) {
134
            err = WSAGetLastError();
135
            SYSerr(SYS_F_WSASTARTUP, err);
136
            BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
137
            return -1;
138
        }
139
    }
140
# endif                         /* OPENSSL_SYS_WINDOWS */
141
# ifdef WATT32
142
    extern int _watt_do_exit;
143
    _watt_do_exit = 0;          /* don't make sock_init() call exit() */
144
    if (sock_init())
145
        return -1;
146
# endif
147
148
0
    return 1;
149
0
}
150
151
void bio_sock_cleanup_int(void)
152
8
{
153
# ifdef OPENSSL_SYS_WINDOWS
154
    if (wsa_init_done) {
155
        wsa_init_done = 0;
156
        WSACleanup();
157
    }
158
# endif
159
}
160
161
int BIO_socket_ioctl(int fd, long type, void *arg)
162
0
{
163
0
    int i;
164
0
165
#  ifdef __DJGPP__
166
    i = ioctlsocket(fd, type, (char *)arg);
167
#  else
168
#   if defined(OPENSSL_SYS_VMS)
169
    /*-
170
     * 2011-02-18 SMS.
171
     * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
172
     * observe that all the consumers pass in an "unsigned long *",
173
     * so we arrange a local copy with a short pointer, and use
174
     * that, instead.
175
     */
176
#    if __INITIAL_POINTER_SIZE == 64
177
#     define ARG arg_32p
178
#     pragma pointer_size save
179
#     pragma pointer_size 32
180
    unsigned long arg_32;
181
    unsigned long *arg_32p;
182
#     pragma pointer_size restore
183
    arg_32p = &arg_32;
184
    arg_32 = *((unsigned long *)arg);
185
#    else                       /* __INITIAL_POINTER_SIZE == 64 */
186
#     define ARG arg
187
#    endif                      /* __INITIAL_POINTER_SIZE == 64 [else] */
188
#   else                        /* defined(OPENSSL_SYS_VMS) */
189
#    define ARG arg
190
0
#   endif                       /* defined(OPENSSL_SYS_VMS) [else] */
191
0
192
0
    i = ioctlsocket(fd, type, ARG);
193
0
#  endif                        /* __DJGPP__ */
194
0
    if (i < 0)
195
0
        SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
196
0
    return i;
197
0
}
198
199
# if OPENSSL_API_COMPAT < 0x10100000L
200
int BIO_get_accept_socket(char *host, int bind_mode)
201
0
{
202
0
    int s = INVALID_SOCKET;
203
0
    char *h = NULL, *p = NULL;
204
0
    BIO_ADDRINFO *res = NULL;
205
0
206
0
    if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
207
0
        return INVALID_SOCKET;
208
0
209
0
    if (BIO_sock_init() != 1)
210
0
        return INVALID_SOCKET;
211
0
212
0
    if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
213
0
        goto err;
214
0
215
0
    if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
216
0
                        BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
217
0
        s = INVALID_SOCKET;
218
0
        goto err;
219
0
    }
220
0
221
0
    if (!BIO_listen(s, BIO_ADDRINFO_address(res),
222
0
                    bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
223
0
        BIO_closesocket(s);
224
0
        s = INVALID_SOCKET;
225
0
    }
226
0
227
0
 err:
228
0
    BIO_ADDRINFO_free(res);
229
0
    OPENSSL_free(h);
230
0
    OPENSSL_free(p);
231
0
232
0
    return s;
233
0
}
234
235
int BIO_accept(int sock, char **ip_port)
236
0
{
237
0
    BIO_ADDR res;
238
0
    int ret = -1;
239
0
240
0
    ret = BIO_accept_ex(sock, &res, 0);
241
0
    if (ret == (int)INVALID_SOCKET) {
242
0
        if (BIO_sock_should_retry(ret)) {
243
0
            ret = -2;
244
0
            goto end;
245
0
        }
246
0
        SYSerr(SYS_F_ACCEPT, get_last_socket_error());
247
0
        BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
248
0
        goto end;
249
0
    }
250
0
251
0
    if (ip_port != NULL) {
252
0
        char *host = BIO_ADDR_hostname_string(&res, 1);
253
0
        char *port = BIO_ADDR_service_string(&res, 1);
254
0
        if (host != NULL && port != NULL)
255
0
            *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
256
0
        else
257
0
            *ip_port = NULL;
258
0
259
0
        if (*ip_port == NULL) {
260
0
            BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
261
0
            BIO_closesocket(ret);
262
0
            ret = (int)INVALID_SOCKET;
263
0
        } else {
264
0
            strcpy(*ip_port, host);
265
0
            strcat(*ip_port, ":");
266
0
            strcat(*ip_port, port);
267
0
        }
268
0
        OPENSSL_free(host);
269
0
        OPENSSL_free(port);
270
0
    }
271
0
272
0
 end:
273
0
    return ret;
274
0
}
275
# endif
276
277
int BIO_set_tcp_ndelay(int s, int on)
278
0
{
279
0
    int ret = 0;
280
0
# if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
281
0
    int opt;
282
0
283
0
#  ifdef SOL_TCP
284
0
    opt = SOL_TCP;
285
#  else
286
#   ifdef IPPROTO_TCP
287
    opt = IPPROTO_TCP;
288
#   endif
289
#  endif
290
291
0
    ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
292
0
# endif
293
0
    return (ret == 0);
294
0
}
295
296
int BIO_socket_nbio(int s, int mode)
297
0
{
298
0
    int ret = -1;
299
0
    int l;
300
0
301
0
    l = mode;
302
0
# ifdef FIONBIO
303
0
    l = mode;
304
0
305
0
    ret = BIO_socket_ioctl(s, FIONBIO, &l);
306
# elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
307
    /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
308
309
    l = fcntl(s, F_GETFL, 0);
310
    if (l == -1) {
311
        SYSerr(SYS_F_FCNTL, get_last_sys_error());
312
        ret = -1;
313
    } else {
314
#  if defined(O_NONBLOCK)
315
        l &= ~O_NONBLOCK;
316
#  else
317
        l &= ~FNDELAY; /* BSD4.x */
318
#  endif
319
        if (mode) {
320
#  if defined(O_NONBLOCK)
321
            l |= O_NONBLOCK;
322
#  else
323
            l |= FNDELAY; /* BSD4.x */
324
#  endif
325
        }
326
        ret = fcntl(s, F_SETFL, l);
327
328
        if (ret < 0) {
329
            SYSerr(SYS_F_FCNTL, get_last_sys_error());
330
        }
331
    }
332
# else
333
    /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
334
    BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT);
335
# endif
336
337
0
    return (ret == 0);
338
0
}
339
340
int BIO_sock_info(int sock,
341
                  enum BIO_sock_info_type type, union BIO_sock_info_u *info)
342
0
{
343
0
    switch (type) {
344
0
    case BIO_SOCK_INFO_ADDRESS:
345
0
        {
346
0
            socklen_t addr_len;
347
0
            int ret = 0;
348
0
            addr_len = sizeof(*info->addr);
349
0
            ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
350
0
                              &addr_len);
351
0
            if (ret == -1) {
352
0
                SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error());
353
0
                BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR);
354
0
                return 0;
355
0
            }
356
0
            if ((size_t)addr_len > sizeof(*info->addr)) {
357
0
                BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
358
0
                return 0;
359
0
            }
360
0
        }
361
0
        break;
362
0
    default:
363
0
        BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE);
364
0
        return 0;
365
0
    }
366
0
    return 1;
367
0
}
368
369
#endif