Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/bio/bss_conn.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <errno.h>
12
13
#include "bio_local.h"
14
#include "internal/bio_tfo.h"
15
#include "internal/ktls.h"
16
17
#ifndef OPENSSL_NO_SOCK
18
19
typedef struct bio_connect_st {
20
    int state;
21
    int connect_family;
22
    int connect_sock_type;
23
    char *param_hostname;
24
    char *param_service;
25
    int connect_mode;
26
#ifndef OPENSSL_NO_KTLS
27
    unsigned char record_type;
28
#endif
29
    int tfo_first;
30
31
    BIO_ADDRINFO *addr_first;
32
    const BIO_ADDRINFO *addr_iter;
33
    /*
34
     * int socket; this will be kept in bio->num so that it is compatible
35
     * with the bss_sock bio
36
     */
37
    /*
38
     * called when the connection is initially made callback(BIO,state,ret);
39
     * The callback should return 'ret'.  state is for compatibility with the
40
     * ssl info_callback
41
     */
42
    BIO_info_cb *info_callback;
43
    /*
44
     * Used when connect_sock_type is SOCK_DGRAM. Owned by us; we forward
45
     * read/write(mmsg) calls to this if present.
46
     */
47
    BIO *dgram_bio;
48
} BIO_CONNECT;
49
50
static int conn_write(BIO *h, const char *buf, int num);
51
static int conn_read(BIO *h, char *buf, int size);
52
static int conn_puts(BIO *h, const char *str);
53
static int conn_gets(BIO *h, char *buf, int size);
54
static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
55
static int conn_new(BIO *h);
56
static int conn_free(BIO *data);
57
static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
58
static int conn_sendmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n,
59
    uint64_t f, size_t *mp);
60
static int conn_recvmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n,
61
    uint64_t f, size_t *mp);
62
63
static int conn_state(BIO *b, BIO_CONNECT *c);
64
static void conn_close_socket(BIO *data);
65
static BIO_CONNECT *BIO_CONNECT_new(void);
66
static void BIO_CONNECT_free(BIO_CONNECT *a);
67
68
0
#define BIO_CONN_S_BEFORE 1
69
0
#define BIO_CONN_S_GET_ADDR 2
70
0
#define BIO_CONN_S_CREATE_SOCKET 3
71
0
#define BIO_CONN_S_CONNECT 4
72
0
#define BIO_CONN_S_OK 5
73
0
#define BIO_CONN_S_BLOCKED_CONNECT 6
74
0
#define BIO_CONN_S_CONNECT_ERROR 7
75
76
static const BIO_METHOD methods_connectp = {
77
    BIO_TYPE_CONNECT,
78
    "socket connect",
79
    bwrite_conv,
80
    conn_write,
81
    bread_conv,
82
    conn_read,
83
    conn_puts,
84
    conn_gets,
85
    conn_ctrl,
86
    conn_new,
87
    conn_free,
88
    conn_callback_ctrl,
89
    conn_sendmmsg,
90
    conn_recvmmsg,
91
};
92
93
static int conn_create_dgram_bio(BIO *b, BIO_CONNECT *c)
94
0
{
95
0
    if (c->connect_sock_type != SOCK_DGRAM)
96
0
        return 1;
97
98
0
#ifndef OPENSSL_NO_DGRAM
99
0
    c->dgram_bio = BIO_new_dgram(b->num, 0);
100
0
    if (c->dgram_bio == NULL)
101
0
        goto err;
102
103
0
    return 1;
104
105
0
err:
106
0
#endif
107
0
    c->state = BIO_CONN_S_CONNECT_ERROR;
108
0
    return 0;
109
0
}
110
111
static int conn_state(BIO *b, BIO_CONNECT *c)
112
0
{
113
0
    int ret = -1, i, opts;
114
0
    BIO_info_cb *cb = NULL;
115
116
0
    if (c->info_callback != NULL)
117
0
        cb = c->info_callback;
118
119
0
    for (;;) {
120
0
        switch (c->state) {
121
0
        case BIO_CONN_S_BEFORE:
122
0
            if (c->param_hostname == NULL && c->param_service == NULL) {
123
0
                ERR_raise_data(ERR_LIB_BIO,
124
0
                    BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
125
0
                    "hostname=%s service=%s",
126
0
                    c->param_hostname, c->param_service);
127
0
                goto exit_loop;
128
0
            }
129
0
            c->state = BIO_CONN_S_GET_ADDR;
130
0
            break;
131
132
0
        case BIO_CONN_S_GET_ADDR: {
133
0
            int family = AF_UNSPEC;
134
0
            switch (c->connect_family) {
135
0
            case BIO_FAMILY_IPV6:
136
0
                if (1) { /* This is a trick we use to avoid bit rot.
137
                          * at least the "else" part will always be
138
                          * compiled.
139
                          */
140
0
#if OPENSSL_USE_IPV6
141
0
                    family = AF_INET6;
142
0
                } else {
143
0
#endif
144
0
                    ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
145
0
                    goto exit_loop;
146
0
                }
147
0
                break;
148
0
            case BIO_FAMILY_IPV4:
149
0
                family = AF_INET;
150
0
                break;
151
0
            case BIO_FAMILY_IPANY:
152
0
                family = AF_UNSPEC;
153
0
                break;
154
0
            default:
155
0
                ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
156
0
                goto exit_loop;
157
0
            }
158
0
            if (BIO_lookup(c->param_hostname, c->param_service,
159
0
                    BIO_LOOKUP_CLIENT,
160
0
                    family, c->connect_sock_type,
161
0
                    &c->addr_first)
162
0
                == 0)
163
0
                goto exit_loop;
164
0
        }
165
0
            if (c->addr_first == NULL) {
166
0
                ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
167
0
                goto exit_loop;
168
0
            }
169
0
            c->addr_iter = c->addr_first;
170
0
            c->state = BIO_CONN_S_CREATE_SOCKET;
171
0
            break;
172
173
0
        case BIO_CONN_S_CREATE_SOCKET:
174
0
            ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
175
0
                BIO_ADDRINFO_socktype(c->addr_iter),
176
0
                BIO_ADDRINFO_protocol(c->addr_iter), 0);
177
0
            if (ret == (int)INVALID_SOCKET) {
178
0
                ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
179
0
                    "calling socket(%s, %s)",
180
0
                    c->param_hostname, c->param_service);
181
0
                ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
182
0
                goto exit_loop;
183
0
            }
184
0
            b->num = ret;
185
0
            c->state = BIO_CONN_S_CONNECT;
186
0
            break;
187
188
0
        case BIO_CONN_S_CONNECT:
189
0
            BIO_clear_retry_flags(b);
190
0
            ERR_set_mark();
191
192
0
            opts = c->connect_mode;
193
0
            if (BIO_ADDRINFO_socktype(c->addr_iter) == SOCK_STREAM)
194
0
                opts |= BIO_SOCK_KEEPALIVE;
195
196
0
            ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter), opts);
197
0
            b->retry_reason = 0;
198
0
            if (ret == 0) {
199
0
                if (BIO_sock_should_retry(ret)) {
200
0
                    BIO_set_retry_special(b);
201
0
                    c->state = BIO_CONN_S_BLOCKED_CONNECT;
202
0
                    b->retry_reason = BIO_RR_CONNECT;
203
0
                    ERR_pop_to_mark();
204
0
                } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
205
0
                    != NULL) {
206
                    /*
207
                     * if there are more addresses to try, do that first
208
                     */
209
0
                    BIO_closesocket(b->num);
210
0
                    c->state = BIO_CONN_S_CREATE_SOCKET;
211
0
                    ERR_pop_to_mark();
212
0
                    break;
213
0
                } else {
214
0
                    ERR_clear_last_mark();
215
0
                    ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
216
0
                        "calling connect(%s, %s)",
217
0
                        c->param_hostname, c->param_service);
218
0
                    c->state = BIO_CONN_S_CONNECT_ERROR;
219
0
                    break;
220
0
                }
221
0
                goto exit_loop;
222
0
            } else {
223
0
                ERR_clear_last_mark();
224
0
                if (!conn_create_dgram_bio(b, c))
225
0
                    break;
226
0
                c->state = BIO_CONN_S_OK;
227
0
            }
228
0
            break;
229
230
0
        case BIO_CONN_S_BLOCKED_CONNECT:
231
            /* wait for socket being writable, before querying BIO_sock_error */
232
0
            if (BIO_socket_wait(b->num, 0, time(NULL)) == 0)
233
0
                break;
234
0
            i = BIO_sock_error(b->num);
235
0
            if (i != 0) {
236
0
                BIO_clear_retry_flags(b);
237
0
                if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
238
                    /*
239
                     * if there are more addresses to try, do that first
240
                     */
241
0
                    BIO_closesocket(b->num);
242
0
                    c->state = BIO_CONN_S_CREATE_SOCKET;
243
0
                    break;
244
0
                }
245
0
                ERR_raise_data(ERR_LIB_SYS, i,
246
0
                    "calling connect(%s, %s)",
247
0
                    c->param_hostname, c->param_service);
248
0
                ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
249
0
                ret = 0;
250
0
                goto exit_loop;
251
0
            } else {
252
0
                if (!conn_create_dgram_bio(b, c))
253
0
                    break;
254
0
                c->state = BIO_CONN_S_OK;
255
0
            }
256
0
            break;
257
258
0
        case BIO_CONN_S_CONNECT_ERROR:
259
0
            ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
260
0
            ret = 0;
261
0
            goto exit_loop;
262
263
0
        case BIO_CONN_S_OK:
264
0
            ret = 1;
265
0
            goto exit_loop;
266
0
        default:
267
            /* abort(); */
268
0
            goto exit_loop;
269
0
        }
270
271
0
        if (cb != NULL) {
272
0
            if ((ret = cb((BIO *)b, c->state, ret)) == 0)
273
0
                goto end;
274
0
        }
275
0
    }
276
277
    /* Loop does not exit */
278
0
exit_loop:
279
0
    if (cb != NULL)
280
0
        ret = cb((BIO *)b, c->state, ret);
281
0
end:
282
0
    return ret;
283
0
}
284
285
static BIO_CONNECT *BIO_CONNECT_new(void)
286
0
{
287
0
    BIO_CONNECT *ret;
288
289
0
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
290
0
        return NULL;
291
0
    ret->state = BIO_CONN_S_BEFORE;
292
0
    ret->connect_family = BIO_FAMILY_IPANY;
293
0
    ret->connect_sock_type = SOCK_STREAM;
294
0
    return ret;
295
0
}
296
297
static void BIO_CONNECT_free(BIO_CONNECT *a)
298
0
{
299
0
    if (a == NULL)
300
0
        return;
301
0
    OPENSSL_free(a->param_hostname);
302
0
    OPENSSL_free(a->param_service);
303
0
    BIO_ADDRINFO_free(a->addr_first);
304
0
    OPENSSL_free(a);
305
0
}
306
307
const BIO_METHOD *BIO_s_connect(void)
308
0
{
309
0
    return &methods_connectp;
310
0
}
311
312
static int conn_new(BIO *bi)
313
0
{
314
0
    bi->init = 0;
315
0
    bi->num = (int)INVALID_SOCKET;
316
0
    bi->flags = 0;
317
0
    if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
318
0
        return 0;
319
0
    else
320
0
        return 1;
321
0
}
322
323
static void conn_close_socket(BIO *bio)
324
0
{
325
0
    BIO_CONNECT *c;
326
327
0
    c = (BIO_CONNECT *)bio->ptr;
328
0
    if (bio->num != (int)INVALID_SOCKET) {
329
        /* Only do a shutdown if things were established */
330
0
        if (c->state == BIO_CONN_S_OK)
331
0
            shutdown(bio->num, 2);
332
0
        BIO_closesocket(bio->num);
333
0
        bio->num = (int)INVALID_SOCKET;
334
0
    }
335
0
}
336
337
static int conn_free(BIO *a)
338
0
{
339
0
    BIO_CONNECT *data;
340
341
0
    if (a == NULL)
342
0
        return 0;
343
0
    data = (BIO_CONNECT *)a->ptr;
344
345
0
    BIO_free(data->dgram_bio);
346
347
0
    if (a->shutdown) {
348
0
        conn_close_socket(a);
349
0
        BIO_CONNECT_free(data);
350
0
        a->ptr = NULL;
351
0
        a->flags = 0;
352
0
        a->init = 0;
353
0
    }
354
0
    return 1;
355
0
}
356
357
static int conn_read(BIO *b, char *out, int outl)
358
0
{
359
0
    int ret = 0;
360
0
    BIO_CONNECT *data;
361
362
0
    data = (BIO_CONNECT *)b->ptr;
363
0
    if (data->state != BIO_CONN_S_OK) {
364
0
        ret = conn_state(b, data);
365
0
        if (ret <= 0)
366
0
            return ret;
367
0
    }
368
369
0
    if (data->dgram_bio != NULL) {
370
0
        BIO_clear_retry_flags(b);
371
0
        ret = BIO_read(data->dgram_bio, out, outl);
372
0
        BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio));
373
0
        return ret;
374
0
    }
375
376
0
    if (out != NULL) {
377
0
        clear_socket_error();
378
#ifndef OPENSSL_NO_KTLS
379
        if (BIO_get_ktls_recv(b))
380
            ret = ktls_read_record(b->num, out, outl);
381
        else
382
#endif
383
0
            ret = readsocket(b->num, out, outl);
384
0
        BIO_clear_retry_flags(b);
385
0
        if (ret <= 0) {
386
0
            if (BIO_sock_should_retry(ret))
387
0
                BIO_set_retry_read(b);
388
0
            else if (ret == 0)
389
0
                b->flags |= BIO_FLAGS_IN_EOF;
390
0
        }
391
0
    }
392
0
    return ret;
393
0
}
394
395
static int conn_write(BIO *b, const char *in, int inl)
396
0
{
397
0
    int ret;
398
0
    BIO_CONNECT *data;
399
400
0
    data = (BIO_CONNECT *)b->ptr;
401
0
    if (data->state != BIO_CONN_S_OK) {
402
0
        ret = conn_state(b, data);
403
0
        if (ret <= 0)
404
0
            return ret;
405
0
    }
406
407
0
    if (data->dgram_bio != NULL) {
408
0
        BIO_clear_retry_flags(b);
409
0
        ret = BIO_write(data->dgram_bio, in, inl);
410
0
        BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio));
411
0
        return ret;
412
0
    }
413
414
0
    clear_socket_error();
415
#ifndef OPENSSL_NO_KTLS
416
    if (BIO_should_ktls_ctrl_msg_flag(b)) {
417
        ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
418
        if (ret >= 0) {
419
            ret = inl;
420
            BIO_clear_ktls_ctrl_msg_flag(b);
421
        }
422
    } else
423
#endif
424
#if defined(OSSL_TFO_SENDTO)
425
        if (data->tfo_first) {
426
        int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter);
427
428
        ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
429
            BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen);
430
        data->tfo_first = 0;
431
    } else
432
#endif
433
0
        ret = writesocket(b->num, in, inl);
434
0
    BIO_clear_retry_flags(b);
435
0
    if (ret <= 0) {
436
0
        if (BIO_sock_should_retry(ret))
437
0
            BIO_set_retry_write(b);
438
0
    }
439
0
    return ret;
440
0
}
441
442
static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
443
0
{
444
0
    BIO *dbio;
445
0
    int *ip;
446
0
    const char **pptr = NULL;
447
0
    long ret = 1;
448
0
    BIO_CONNECT *data;
449
0
    const BIO_ADDR *dg_addr;
450
#ifndef OPENSSL_NO_KTLS
451
    ktls_crypto_info_t *crypto_info;
452
#endif
453
454
0
    data = (BIO_CONNECT *)b->ptr;
455
456
0
    switch (cmd) {
457
0
    case BIO_CTRL_RESET:
458
0
        ret = 0;
459
0
        data->state = BIO_CONN_S_BEFORE;
460
0
        conn_close_socket(b);
461
0
        BIO_ADDRINFO_free(data->addr_first);
462
0
        data->addr_first = NULL;
463
0
        data->addr_iter = NULL;
464
0
        b->flags = 0;
465
0
        break;
466
0
    case BIO_C_DO_STATE_MACHINE:
467
        /* use this one to start the connection */
468
0
        if (data->state != BIO_CONN_S_OK)
469
0
            ret = (long)conn_state(b, data);
470
0
        else
471
0
            ret = 1;
472
0
        break;
473
0
    case BIO_C_GET_CONNECT:
474
0
        if (ptr != NULL) {
475
0
            pptr = (const char **)ptr;
476
0
            if (num == 0) {
477
0
                *pptr = data->param_hostname;
478
0
            } else if (num == 1) {
479
0
                *pptr = data->param_service;
480
0
            } else if (num == 2) {
481
0
                *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
482
0
            } else if (num == 3) {
483
0
                switch (BIO_ADDRINFO_family(data->addr_iter)) {
484
0
#if OPENSSL_USE_IPV6
485
0
                case AF_INET6:
486
0
                    ret = BIO_FAMILY_IPV6;
487
0
                    break;
488
0
#endif
489
0
                case AF_INET:
490
0
                    ret = BIO_FAMILY_IPV4;
491
0
                    break;
492
0
                case 0:
493
0
                    ret = data->connect_family;
494
0
                    break;
495
0
                default:
496
0
                    ret = -1;
497
0
                    break;
498
0
                }
499
0
            } else if (num == 4) {
500
0
                ret = data->connect_mode;
501
0
            } else {
502
0
                ret = 0;
503
0
            }
504
0
        } else {
505
0
            ret = 0;
506
0
        }
507
0
        break;
508
0
    case BIO_C_SET_CONNECT:
509
0
        if (ptr != NULL) {
510
0
            b->init = 1;
511
0
            if (num == 0) { /* BIO_set_conn_hostname */
512
0
                char *hold_service = data->param_service;
513
                /* We affect the hostname regardless.  However, the input
514
                 * string might contain a host:service spec, so we must
515
                 * parse it, which might or might not affect the service
516
                 */
517
518
0
                OPENSSL_free(data->param_hostname);
519
0
                data->param_hostname = NULL;
520
0
                ret = BIO_parse_hostserv(ptr,
521
0
                    &data->param_hostname,
522
0
                    &data->param_service,
523
0
                    BIO_PARSE_PRIO_HOST);
524
0
                if (hold_service != data->param_service)
525
0
                    OPENSSL_free(hold_service);
526
0
            } else if (num == 1) { /* BIO_set_conn_port */
527
0
                OPENSSL_free(data->param_service);
528
0
                if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
529
0
                    ret = 0;
530
0
            } else if (num == 2) { /* BIO_set_conn_address */
531
0
                const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
532
0
                char *host = BIO_ADDR_hostname_string(addr, 1);
533
0
                char *service = BIO_ADDR_service_string(addr, 1);
534
535
0
                ret = host != NULL && service != NULL;
536
0
                if (ret) {
537
0
                    OPENSSL_free(data->param_hostname);
538
0
                    data->param_hostname = host;
539
0
                    OPENSSL_free(data->param_service);
540
0
                    data->param_service = service;
541
0
                    BIO_ADDRINFO_free(data->addr_first);
542
0
                    data->addr_first = NULL;
543
0
                    data->addr_iter = NULL;
544
0
                } else {
545
0
                    OPENSSL_free(host);
546
0
                    OPENSSL_free(service);
547
0
                }
548
0
            } else if (num == 3) { /* BIO_set_conn_ip_family */
549
0
                data->connect_family = *(int *)ptr;
550
0
            } else {
551
0
                ret = 0;
552
0
            }
553
0
        }
554
0
        break;
555
0
    case BIO_C_SET_SOCK_TYPE:
556
0
        if ((num != SOCK_STREAM && num != SOCK_DGRAM)
557
0
            || data->state >= BIO_CONN_S_GET_ADDR) {
558
0
            ret = 0;
559
0
            break;
560
0
        }
561
562
0
        data->connect_sock_type = (int)num;
563
0
        ret = 1;
564
0
        break;
565
0
    case BIO_C_GET_SOCK_TYPE:
566
0
        ret = data->connect_sock_type;
567
0
        break;
568
0
    case BIO_C_GET_DGRAM_BIO:
569
0
        if (data->dgram_bio != NULL) {
570
0
            *(BIO **)ptr = data->dgram_bio;
571
0
            ret = 1;
572
0
        } else {
573
0
            ret = 0;
574
0
        }
575
0
        break;
576
0
    case BIO_CTRL_DGRAM_GET_PEER:
577
0
    case BIO_CTRL_DGRAM_DETECT_PEER_ADDR:
578
0
        if (data->state != BIO_CONN_S_OK)
579
0
            conn_state(b, data); /* best effort */
580
581
0
        if (data->state >= BIO_CONN_S_CREATE_SOCKET
582
0
            && data->addr_iter != NULL
583
0
            && (dg_addr = BIO_ADDRINFO_address(data->addr_iter)) != NULL) {
584
585
0
            ret = BIO_ADDR_sockaddr_size(dg_addr);
586
0
            if (num == 0 || num > ret)
587
0
                num = ret;
588
589
0
            memcpy(ptr, dg_addr, num);
590
0
            ret = num;
591
0
        } else {
592
0
            ret = 0;
593
0
        }
594
595
0
        break;
596
0
    case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
597
0
    case BIO_CTRL_GET_WPOLL_DESCRIPTOR: {
598
0
        BIO_POLL_DESCRIPTOR *pd = ptr;
599
600
0
        if (data->state != BIO_CONN_S_OK)
601
0
            conn_state(b, data); /* best effort */
602
603
0
        if (data->state >= BIO_CONN_S_CREATE_SOCKET) {
604
0
            pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD;
605
0
            pd->value.fd = b->num;
606
0
        } else {
607
0
            ret = 0;
608
0
        }
609
0
    } break;
610
0
    case BIO_C_SET_NBIO:
611
0
        if (num != 0)
612
0
            data->connect_mode |= BIO_SOCK_NONBLOCK;
613
0
        else
614
0
            data->connect_mode &= ~BIO_SOCK_NONBLOCK;
615
616
0
        if (data->dgram_bio != NULL)
617
0
            ret = BIO_set_nbio(data->dgram_bio, num);
618
619
0
        break;
620
#if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
621
    case BIO_C_SET_TFO:
622
        if (num != 0) {
623
            data->connect_mode |= BIO_SOCK_TFO;
624
            data->tfo_first = 1;
625
        } else {
626
            data->connect_mode &= ~BIO_SOCK_TFO;
627
            data->tfo_first = 0;
628
        }
629
        break;
630
#endif
631
0
    case BIO_C_SET_CONNECT_MODE:
632
0
        data->connect_mode = (int)num;
633
0
        if (num & BIO_SOCK_TFO)
634
0
            data->tfo_first = 1;
635
0
        else
636
0
            data->tfo_first = 0;
637
0
        break;
638
0
    case BIO_C_GET_FD:
639
0
        if (b->init) {
640
0
            ip = (int *)ptr;
641
0
            if (ip != NULL)
642
0
                *ip = b->num;
643
0
            ret = b->num;
644
0
        } else
645
0
            ret = -1;
646
0
        break;
647
0
    case BIO_CTRL_GET_CLOSE:
648
0
        ret = b->shutdown;
649
0
        break;
650
0
    case BIO_CTRL_SET_CLOSE:
651
0
        b->shutdown = (int)num;
652
0
        break;
653
0
    case BIO_CTRL_PENDING:
654
0
    case BIO_CTRL_WPENDING:
655
0
        ret = 0;
656
0
        break;
657
0
    case BIO_CTRL_FLUSH:
658
0
        break;
659
0
    case BIO_CTRL_DUP: {
660
0
        dbio = (BIO *)ptr;
661
0
        if (data->param_hostname)
662
0
            BIO_set_conn_hostname(dbio, data->param_hostname);
663
0
        if (data->param_service)
664
0
            BIO_set_conn_port(dbio, data->param_service);
665
0
        BIO_set_conn_ip_family(dbio, data->connect_family);
666
0
        BIO_set_conn_mode(dbio, data->connect_mode);
667
        /*
668
         * FIXME: the cast of the function seems unlikely to be a good
669
         * idea
670
         */
671
0
        (void)BIO_set_info_callback(dbio, data->info_callback);
672
0
    } break;
673
0
    case BIO_CTRL_SET_CALLBACK:
674
0
        ret = 0; /* use callback ctrl */
675
0
        break;
676
0
    case BIO_CTRL_GET_CALLBACK: {
677
0
        BIO_info_cb **fptr;
678
679
0
        fptr = (BIO_info_cb **)ptr;
680
0
        *fptr = data->info_callback;
681
0
    } break;
682
0
    case BIO_CTRL_EOF:
683
0
        ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
684
0
        break;
685
#ifndef OPENSSL_NO_KTLS
686
    case BIO_CTRL_SET_KTLS:
687
        crypto_info = (ktls_crypto_info_t *)ptr;
688
        ret = ktls_start(b->num, crypto_info, num);
689
        if (ret)
690
            BIO_set_ktls_flag(b, num);
691
        break;
692
    case BIO_CTRL_GET_KTLS_SEND:
693
        return BIO_should_ktls_flag(b, 1) != 0;
694
    case BIO_CTRL_GET_KTLS_RECV:
695
        return BIO_should_ktls_flag(b, 0) != 0;
696
    case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
697
        BIO_set_ktls_ctrl_msg_flag(b);
698
        data->record_type = num;
699
        ret = 0;
700
        break;
701
    case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
702
        BIO_clear_ktls_ctrl_msg_flag(b);
703
        ret = 0;
704
        break;
705
    case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
706
        ret = ktls_enable_tx_zerocopy_sendfile(b->num);
707
        if (ret)
708
            BIO_set_ktls_zerocopy_sendfile_flag(b);
709
        break;
710
#endif
711
0
    default:
712
0
        ret = 0;
713
0
        break;
714
0
    }
715
0
    return ret;
716
0
}
717
718
static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
719
0
{
720
0
    long ret = 1;
721
0
    BIO_CONNECT *data;
722
723
0
    data = (BIO_CONNECT *)b->ptr;
724
725
0
    switch (cmd) {
726
0
    case BIO_CTRL_SET_CALLBACK: {
727
0
        data->info_callback = fp;
728
0
    } break;
729
0
    default:
730
0
        ret = 0;
731
0
        break;
732
0
    }
733
0
    return ret;
734
0
}
735
736
static int conn_puts(BIO *bp, const char *str)
737
0
{
738
0
    int ret;
739
0
    size_t n = strlen(str);
740
741
0
    if (n > INT_MAX)
742
0
        return -1;
743
0
    ret = conn_write(bp, str, (int)n);
744
0
    return ret;
745
0
}
746
747
int conn_gets(BIO *bio, char *buf, int size)
748
0
{
749
0
    BIO_CONNECT *data;
750
0
    char *ptr = buf;
751
0
    int ret = 0;
752
753
0
    if (buf == NULL) {
754
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
755
0
        return -1;
756
0
    }
757
0
    if (size <= 0) {
758
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
759
0
        return -1;
760
0
    }
761
0
    *buf = '\0';
762
763
0
    if (bio == NULL || bio->ptr == NULL) {
764
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
765
0
        return -1;
766
0
    }
767
0
    data = (BIO_CONNECT *)bio->ptr;
768
0
    if (data->state != BIO_CONN_S_OK) {
769
0
        ret = conn_state(bio, data);
770
0
        if (ret <= 0)
771
0
            return ret;
772
0
    }
773
774
0
    if (data->dgram_bio != NULL) {
775
0
        ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
776
0
        return -1;
777
0
    }
778
779
0
    clear_socket_error();
780
0
    while (size-- > 1) {
781
#ifndef OPENSSL_NO_KTLS
782
        if (BIO_get_ktls_recv(bio))
783
            ret = ktls_read_record(bio->num, ptr, 1);
784
        else
785
#endif
786
0
            ret = readsocket(bio->num, ptr, 1);
787
0
        BIO_clear_retry_flags(bio);
788
0
        if (ret <= 0) {
789
0
            if (BIO_sock_should_retry(ret))
790
0
                BIO_set_retry_read(bio);
791
0
            else if (ret == 0)
792
0
                bio->flags |= BIO_FLAGS_IN_EOF;
793
0
            break;
794
0
        }
795
0
        if (*ptr++ == '\n')
796
0
            break;
797
0
    }
798
0
    *ptr = '\0';
799
0
    return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? (int)(ptr - buf) : ret;
800
0
}
801
802
static int conn_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs,
803
    uint64_t flags, size_t *msgs_processed)
804
0
{
805
0
    int ret;
806
0
    BIO_CONNECT *data;
807
808
0
    if (bio == NULL) {
809
0
        *msgs_processed = 0;
810
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
811
0
        return 0;
812
0
    }
813
814
0
    data = (BIO_CONNECT *)bio->ptr;
815
0
    if (data->state != BIO_CONN_S_OK) {
816
0
        ret = conn_state(bio, data);
817
0
        if (ret <= 0) {
818
0
            *msgs_processed = 0;
819
0
            return 0;
820
0
        }
821
0
    }
822
823
0
    if (data->dgram_bio == NULL) {
824
0
        *msgs_processed = 0;
825
0
        ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
826
0
        return 0;
827
0
    }
828
829
0
    return BIO_sendmmsg(data->dgram_bio, msg, stride, num_msgs,
830
0
        flags, msgs_processed);
831
0
}
832
833
static int conn_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs,
834
    uint64_t flags, size_t *msgs_processed)
835
0
{
836
0
    int ret;
837
0
    BIO_CONNECT *data;
838
839
0
    if (bio == NULL) {
840
0
        *msgs_processed = 0;
841
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
842
0
        return 0;
843
0
    }
844
845
0
    data = (BIO_CONNECT *)bio->ptr;
846
0
    if (data->state != BIO_CONN_S_OK) {
847
0
        ret = conn_state(bio, data);
848
0
        if (ret <= 0) {
849
0
            *msgs_processed = 0;
850
0
            return 0;
851
0
        }
852
0
    }
853
854
0
    if (data->dgram_bio == NULL) {
855
0
        *msgs_processed = 0;
856
0
        ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
857
0
        return 0;
858
0
    }
859
860
0
    return BIO_recvmmsg(data->dgram_bio, msg, stride, num_msgs,
861
0
        flags, msgs_processed);
862
0
}
863
864
BIO *BIO_new_connect(const char *str)
865
0
{
866
0
    BIO *ret;
867
868
0
    ret = BIO_new(BIO_s_connect());
869
0
    if (ret == NULL)
870
0
        return NULL;
871
0
    if (BIO_set_conn_hostname(ret, str))
872
0
        return ret;
873
0
    BIO_free(ret);
874
    return NULL;
875
0
}
876
877
#endif