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