Coverage Report

Created: 2026-07-16 06:59

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