| 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 |
#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, opts; |
| 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 |
int family = AF_UNSPEC; |
| 134 |
switch (c->connect_family) { |
| 135 |
case BIO_FAMILY_IPV6: |
| 136 |
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 |
#if OPENSSL_USE_IPV6 |
| 141 |
family = AF_INET6; |
| 142 |
} else { |
| 143 |
#endif |
| 144 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY); |
| 145 |
goto exit_loop; |
| 146 |
} |
| 147 |
break; |
| 148 |
case BIO_FAMILY_IPV4: |
| 149 |
family = AF_INET; |
| 150 |
break; |
| 151 |
case BIO_FAMILY_IPANY: |
| 152 |
family = AF_UNSPEC; |
| 153 |
break; |
| 154 |
default: |
| 155 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY); |
| 156 |
goto exit_loop; |
| 157 |
} |
| 158 |
if (BIO_lookup(c->param_hostname, c->param_service, |
| 159 |
BIO_LOOKUP_CLIENT, |
| 160 |
family, c->connect_sock_type, |
| 161 |
&c->addr_first) |
| 162 |
== 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 |
|
| 192 |
opts = c->connect_mode; |
| 193 |
if (BIO_ADDRINFO_socktype(c->addr_iter) == SOCK_STREAM) |
| 194 |
opts |= BIO_SOCK_KEEPALIVE; |
| 195 |
|
| 196 |
ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter), opts); |
| 197 |
b->retry_reason = 0; |
| 198 |
if (ret == 0) { |
| 199 |
if (BIO_sock_should_retry(ret)) { |
| 200 |
BIO_set_retry_special(b); |
| 201 |
c->state = BIO_CONN_S_BLOCKED_CONNECT; |
| 202 |
b->retry_reason = BIO_RR_CONNECT; |
| 203 |
ERR_pop_to_mark(); |
| 204 |
} else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) |
| 205 |
!= NULL) { |
| 206 |
/* |
| 207 |
* if there are more addresses to try, do that first |
| 208 |
*/ |
| 209 |
BIO_closesocket(b->num); |
| 210 |
c->state = BIO_CONN_S_CREATE_SOCKET; |
| 211 |
ERR_pop_to_mark(); |
| 212 |
break; |
| 213 |
} else { |
| 214 |
ERR_clear_last_mark(); |
| 215 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 216 |
"calling connect(%s, %s)", |
| 217 |
c->param_hostname, c->param_service); |
| 218 |
c->state = BIO_CONN_S_CONNECT_ERROR; |
| 219 |
break; |
| 220 |
} |
| 221 |
goto exit_loop; |
| 222 |
} else { |
| 223 |
ERR_clear_last_mark(); |
| 224 |
if (!conn_create_dgram_bio(b, c)) |
| 225 |
break; |
| 226 |
c->state = BIO_CONN_S_OK; |
| 227 |
} |
| 228 |
break; |
| 229 |
|
| 230 |
case BIO_CONN_S_BLOCKED_CONNECT: |
| 231 |
/* wait for socket being writable, before querying BIO_sock_error */ |
| 232 |
if (BIO_socket_wait(b->num, 0, time(NULL)) == 0) |
| 233 |
break; |
| 234 |
i = BIO_sock_error(b->num); |
| 235 |
if (i != 0) { |
| 236 |
BIO_clear_retry_flags(b); |
| 237 |
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 |
BIO_closesocket(b->num); |
| 242 |
c->state = BIO_CONN_S_CREATE_SOCKET; |
| 243 |
break; |
| 244 |
} |
| 245 |
ERR_raise_data(ERR_LIB_SYS, i, |
| 246 |
"calling connect(%s, %s)", |
| 247 |
c->param_hostname, c->param_service); |
| 248 |
ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR); |
| 249 |
ret = 0; |
| 250 |
goto exit_loop; |
| 251 |
} else { |
| 252 |
if (!conn_create_dgram_bio(b, c)) |
| 253 |
break; |
| 254 |
c->state = BIO_CONN_S_OK; |
| 255 |
} |
| 256 |
break; |
| 257 |
|
| 258 |
case BIO_CONN_S_CONNECT_ERROR: |
| 259 |
ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); |
| 260 |
ret = 0; |
| 261 |
goto exit_loop; |
| 262 |
|
| 263 |
case BIO_CONN_S_OK: |
| 264 |
ret = 1; |
| 265 |
goto exit_loop; |
| 266 |
default: |
| 267 |
/* abort(); */ |
| 268 |
goto exit_loop; |
| 269 |
} |
| 270 |
|
| 271 |
if (cb != NULL) { |
| 272 |
if ((ret = cb((BIO *)b, c->state, ret)) == 0) |
| 273 |
goto end; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/* Loop does not exit */ |
| 278 |
exit_loop: |
| 279 |
if (cb != NULL) |
| 280 |
ret = cb((BIO *)b, c->state, ret); |
| 281 |
end: |
| 282 |
return ret; |
| 283 |
} |
| 284 |
|
| 285 |
static BIO_CONNECT *BIO_CONNECT_new(void) |
| 286 |
{ |
| 287 |
BIO_CONNECT *ret; |
| 288 |
|
| 289 |
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) |
| 290 |
return NULL; |
| 291 |
ret->state = BIO_CONN_S_BEFORE; |
| 292 |
ret->connect_family = BIO_FAMILY_IPANY; |
| 293 |
ret->connect_sock_type = SOCK_STREAM; |
| 294 |
return ret; |
| 295 |
} |
| 296 |
|
| 297 |
static void BIO_CONNECT_free(BIO_CONNECT *a) |
| 298 |
{ |
| 299 |
if (a == NULL) |
| 300 |
return; |
| 301 |
OPENSSL_free(a->param_hostname); |
| 302 |
OPENSSL_free(a->param_service); |
| 303 |
BIO_ADDRINFO_free(a->addr_first); |
| 304 |
OPENSSL_free(a); |
| 305 |
} |
| 306 |
|
| 307 |
const BIO_METHOD *BIO_s_connect(void) |
| 308 |
{ |
| 309 |
return &methods_connectp; |
| 310 |
} |
| 311 |
|
| 312 |
static int conn_new(BIO *bi) |
| 313 |
{ |
| 314 |
bi->init = 0; |
| 315 |
bi->num = (int)INVALID_SOCKET; |
| 316 |
bi->flags = 0; |
| 317 |
if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) |
| 318 |
return 0; |
| 319 |
else |
| 320 |
return 1; |
| 321 |
} |
| 322 |
|
| 323 |
static void conn_close_socket(BIO *bio) |
| 324 |
{ |
| 325 |
BIO_CONNECT *c; |
| 326 |
|
| 327 |
c = (BIO_CONNECT *)bio->ptr; |
| 328 |
if (bio->num != (int)INVALID_SOCKET) { |
| 329 |
/* Only do a shutdown if things were established */ |
| 330 |
if (c->state == BIO_CONN_S_OK) |
| 331 |
shutdown(bio->num, 2); |
| 332 |
BIO_closesocket(bio->num); |
| 333 |
bio->num = (int)INVALID_SOCKET; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
static int conn_free(BIO *a) |
| 338 |
{ |
| 339 |
BIO_CONNECT *data; |
| 340 |
|
| 341 |
if (a == NULL) |
| 342 |
return 0; |
| 343 |
data = (BIO_CONNECT *)a->ptr; |
| 344 |
|
| 345 |
BIO_free(data->dgram_bio); |
| 346 |
|
| 347 |
if (a->shutdown) { |
| 348 |
conn_close_socket(a); |
| 349 |
BIO_CONNECT_free(data); |
| 350 |
a->ptr = NULL; |
| 351 |
a->flags = 0; |
| 352 |
a->init = 0; |
| 353 |
} |
| 354 |
return 1; |
| 355 |
} |
| 356 |
|
| 357 |
static int conn_read(BIO *b, char *out, int outl) |
| 358 |
{ |
| 359 |
int ret = 0; |
| 360 |
BIO_CONNECT *data; |
| 361 |
|
| 362 |
data = (BIO_CONNECT *)b->ptr; |
| 363 |
if (data->state != BIO_CONN_S_OK) { |
| 364 |
ret = conn_state(b, data); |
| 365 |
if (ret <= 0) |
| 366 |
return ret; |
| 367 |
} |
| 368 |
|
| 369 |
if (data->dgram_bio != NULL) { |
| 370 |
BIO_clear_retry_flags(b); |
| 371 |
ret = BIO_read(data->dgram_bio, out, outl); |
| 372 |
BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio)); |
| 373 |
return ret; |
| 374 |
} |
| 375 |
|
| 376 |
if (out != NULL) { |
| 377 |
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 |
ret = readsocket(b->num, out, outl); |
| 384 |
BIO_clear_retry_flags(b); |
| 385 |
if (ret <= 0) { |
| 386 |
if (BIO_sock_should_retry(ret)) |
| 387 |
BIO_set_retry_read(b); |
| 388 |
else if (ret == 0) |
| 389 |
b->flags |= BIO_FLAGS_IN_EOF; |
| 390 |
} |
| 391 |
} |
| 392 |
return ret; |
| 393 |
} |
| 394 |
|
| 395 |
static int conn_write(BIO *b, const char *in, int inl) |
| 396 |
{ |
| 397 |
int ret; |
| 398 |
BIO_CONNECT *data; |
| 399 |
|
| 400 |
data = (BIO_CONNECT *)b->ptr; |
| 401 |
if (data->state != BIO_CONN_S_OK) { |
| 402 |
ret = conn_state(b, data); |
| 403 |
if (ret <= 0) |
| 404 |
return ret; |
| 405 |
} |
| 406 |
|
| 407 |
if (data->dgram_bio != NULL) { |
| 408 |
BIO_clear_retry_flags(b); |
| 409 |
ret = BIO_write(data->dgram_bio, in, inl); |
| 410 |
BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio)); |
| 411 |
return ret; |
| 412 |
} |
| 413 |
|
| 414 |
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 |
ret = writesocket(b->num, in, inl); |
| 434 |
BIO_clear_retry_flags(b); |
| 435 |
if (ret <= 0) { |
| 436 |
if (BIO_sock_should_retry(ret)) |
| 437 |
BIO_set_retry_write(b); |
| 438 |
} |
| 439 |
return ret; |
| 440 |
} |
| 441 |
|
| 442 |
static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) |
| 443 |
{ |
| 444 |
BIO *dbio; |
| 445 |
int *ip; |
| 446 |
const char **pptr = NULL; |
| 447 |
long ret = 1; |
| 448 |
BIO_CONNECT *data; |
| 449 |
const BIO_ADDR *dg_addr; |
| 450 |
#ifndef OPENSSL_NO_KTLS |
| 451 |
ktls_crypto_info_t *crypto_info; |
| 452 |
#endif |
| 453 |
|
| 454 |
data = (BIO_CONNECT *)b->ptr; |
| 455 |
|
| 456 |
switch (cmd) { |
| 457 |
case BIO_CTRL_RESET: |
| 458 |
ret = 0; |
| 459 |
data->state = BIO_CONN_S_BEFORE; |
| 460 |
conn_close_socket(b); |
| 461 |
BIO_ADDRINFO_free(data->addr_first); |
| 462 |
data->addr_first = NULL; |
| 463 |
data->addr_iter = NULL; |
| 464 |
b->flags = 0; |
| 465 |
break; |
| 466 |
case BIO_C_DO_STATE_MACHINE: |
| 467 |
/* use this one to start the connection */ |
| 468 |
if (data->state != BIO_CONN_S_OK) |
| 469 |
ret = (long)conn_state(b, data); |
| 470 |
else |
| 471 |
ret = 1; |
| 472 |
break; |
| 473 |
case BIO_C_GET_CONNECT: |
| 474 |
if (ptr != NULL) { |
| 475 |
pptr = (const char **)ptr; |
| 476 |
if (num == 0) { |
| 477 |
*pptr = data->param_hostname; |
| 478 |
} else if (num == 1) { |
| 479 |
*pptr = data->param_service; |
| 480 |
} else if (num == 2) { |
| 481 |
*pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter); |
| 482 |
} else if (num == 3) { |
| 483 |
switch (BIO_ADDRINFO_family(data->addr_iter)) { |
| 484 |
#if OPENSSL_USE_IPV6 |
| 485 |
case AF_INET6: |
| 486 |
ret = BIO_FAMILY_IPV6; |
| 487 |
break; |
| 488 |
#endif |
| 489 |
case AF_INET: |
| 490 |
ret = BIO_FAMILY_IPV4; |
| 491 |
break; |
| 492 |
case 0: |
| 493 |
ret = data->connect_family; |
| 494 |
break; |
| 495 |
default: |
| 496 |
ret = -1; |
| 497 |
break; |
| 498 |
} |
| 499 |
} else if (num == 4) { |
| 500 |
ret = data->connect_mode; |
| 501 |
} else { |
| 502 |
ret = 0; |
| 503 |
} |
| 504 |
} else { |
| 505 |
ret = 0; |
| 506 |
} |
| 507 |
break; |
| 508 |
case BIO_C_SET_CONNECT: |
| 509 |
if (ptr != NULL) { |
| 510 |
b->init = 1; |
| 511 |
if (num == 0) { /* BIO_set_conn_hostname */ |
| 512 |
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 |
OPENSSL_free(data->param_hostname); |
| 519 |
data->param_hostname = NULL; |
| 520 |
ret = BIO_parse_hostserv(ptr, |
| 521 |
&data->param_hostname, |
| 522 |
&data->param_service, |
| 523 |
BIO_PARSE_PRIO_HOST); |
| 524 |
if (hold_service != data->param_service) |
| 525 |
OPENSSL_free(hold_service); |
| 526 |
} else if (num == 1) { /* BIO_set_conn_port */ |
| 527 |
OPENSSL_free(data->param_service); |
| 528 |
if ((data->param_service = OPENSSL_strdup(ptr)) == NULL) |
| 529 |
ret = 0; |
| 530 |
} else if (num == 2) { /* BIO_set_conn_address */ |
| 531 |
const BIO_ADDR *addr = (const BIO_ADDR *)ptr; |
| 532 |
char *host = BIO_ADDR_hostname_string(addr, 1); |
| 533 |
char *service = BIO_ADDR_service_string(addr, 1); |
| 534 |
|
| 535 |
ret = host != NULL && service != NULL; |
| 536 |
if (ret) { |
| 537 |
OPENSSL_free(data->param_hostname); |
| 538 |
data->param_hostname = host; |
| 539 |
OPENSSL_free(data->param_service); |
| 540 |
data->param_service = service; |
| 541 |
BIO_ADDRINFO_free(data->addr_first); |
| 542 |
data->addr_first = NULL; |
| 543 |
data->addr_iter = NULL; |
| 544 |
} else { |
| 545 |
OPENSSL_free(host); |
| 546 |
OPENSSL_free(service); |
| 547 |
} |
| 548 |
} else if (num == 3) { /* BIO_set_conn_ip_family */ |
| 549 |
data->connect_family = *(int *)ptr; |
| 550 |
} else { |
| 551 |
ret = 0; |
| 552 |
} |
| 553 |
} |
| 554 |
break; |
| 555 |
case BIO_C_SET_SOCK_TYPE: |
| 556 |
if ((num != SOCK_STREAM && num != SOCK_DGRAM) |
| 557 |
|| data->state >= BIO_CONN_S_GET_ADDR) { |
| 558 |
ret = 0; |
| 559 |
break; |
| 560 |
} |
| 561 |
|
| 562 |
data->connect_sock_type = (int)num; |
| 563 |
ret = 1; |
| 564 |
break; |
| 565 |
case BIO_C_GET_SOCK_TYPE: |
| 566 |
ret = data->connect_sock_type; |
| 567 |
break; |
| 568 |
case BIO_C_GET_DGRAM_BIO: |
| 569 |
if (data->dgram_bio != NULL) { |
| 570 |
*(BIO **)ptr = data->dgram_bio; |
| 571 |
ret = 1; |
| 572 |
} else { |
| 573 |
ret = 0; |
| 574 |
} |
| 575 |
break; |
| 576 |
case BIO_CTRL_DGRAM_GET_PEER: |
| 577 |
case BIO_CTRL_DGRAM_DETECT_PEER_ADDR: |
| 578 |
if (data->state != BIO_CONN_S_OK) |
| 579 |
conn_state(b, data); /* best effort */ |
| 580 |
|
| 581 |
if (data->state >= BIO_CONN_S_CREATE_SOCKET |
| 582 |
&& data->addr_iter != NULL |
| 583 |
&& (dg_addr = BIO_ADDRINFO_address(data->addr_iter)) != NULL) { |
| 584 |
|
| 585 |
ret = BIO_ADDR_sockaddr_size(dg_addr); |
| 586 |
if (num == 0 || num > ret) |
| 587 |
num = ret; |
| 588 |
|
| 589 |
memcpy(ptr, dg_addr, num); |
| 590 |
ret = num; |
| 591 |
} else { |
| 592 |
ret = 0; |
| 593 |
} |
| 594 |
|
| 595 |
break; |
| 596 |
case BIO_CTRL_GET_RPOLL_DESCRIPTOR: |
| 597 |
case BIO_CTRL_GET_WPOLL_DESCRIPTOR: { |
| 598 |
BIO_POLL_DESCRIPTOR *pd = ptr; |
| 599 |
|
| 600 |
if (data->state != BIO_CONN_S_OK) |
| 601 |
conn_state(b, data); /* best effort */ |
| 602 |
|
| 603 |
if (data->state >= BIO_CONN_S_CREATE_SOCKET) { |
| 604 |
pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD; |
| 605 |
pd->value.fd = b->num; |
| 606 |
} else { |
| 607 |
ret = 0; |
| 608 |
} |
| 609 |
} break; |
| 610 |
case BIO_C_SET_NBIO: |
| 611 |
if (num != 0) |
| 612 |
data->connect_mode |= BIO_SOCK_NONBLOCK; |
| 613 |
else |
| 614 |
data->connect_mode &= ~BIO_SOCK_NONBLOCK; |
| 615 |
|
| 616 |
if (data->dgram_bio != NULL) |
| 617 |
ret = BIO_set_nbio(data->dgram_bio, num); |
| 618 |
|
| 619 |
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 |
case BIO_C_SET_CONNECT_MODE: |
| 632 |
data->connect_mode = (int)num; |
| 633 |
if (num & BIO_SOCK_TFO) |
| 634 |
data->tfo_first = 1; |
| 635 |
else |
| 636 |
data->tfo_first = 0; |
| 637 |
break; |
| 638 |
case BIO_C_GET_FD: |
| 639 |
if (b->init) { |
| 640 |
ip = (int *)ptr; |
| 641 |
if (ip != NULL) |
| 642 |
*ip = b->num; |
| 643 |
ret = b->num; |
| 644 |
} else |
| 645 |
ret = -1; |
| 646 |
break; |
| 647 |
case BIO_CTRL_GET_CLOSE: |
| 648 |
ret = b->shutdown; |
| 649 |
break; |
| 650 |
case BIO_CTRL_SET_CLOSE: |
| 651 |
b->shutdown = (int)num; |
| 652 |
break; |
| 653 |
case BIO_CTRL_PENDING: |
| 654 |
case BIO_CTRL_WPENDING: |
| 655 |
ret = 0; |
| 656 |
break; |
| 657 |
case BIO_CTRL_FLUSH: |
| 658 |
break; |
| 659 |
case BIO_CTRL_DUP: { |
| 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 |
} break; |
| 673 |
case BIO_CTRL_SET_CALLBACK: |
| 674 |
ret = 0; /* use callback ctrl */ |
| 675 |
break; |
| 676 |
case BIO_CTRL_GET_CALLBACK: { |
| 677 |
BIO_info_cb **fptr; |
| 678 |
|
| 679 |
fptr = (BIO_info_cb **)ptr; |
| 680 |
*fptr = data->info_callback; |
| 681 |
} break; |
| 682 |
case BIO_CTRL_EOF: |
| 683 |
ret = (b->flags & BIO_FLAGS_IN_EOF) != 0; |
| 684 |
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 |
default: |
| 712 |
ret = 0; |
| 713 |
break; |
| 714 |
} |
| 715 |
return ret; |
| 716 |
} |
| 717 |
|
| 718 |
static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
| 719 |
{ |
| 720 |
long ret = 1; |
| 721 |
BIO_CONNECT *data; |
| 722 |
|
| 723 |
data = (BIO_CONNECT *)b->ptr; |
| 724 |
|
| 725 |
switch (cmd) { |
| 726 |
case BIO_CTRL_SET_CALLBACK: { |
| 727 |
data->info_callback = fp; |
| 728 |
} break; |
| 729 |
default: |
| 730 |
ret = 0; |
| 731 |
break; |
| 732 |
} |
| 733 |
return ret; |
| 734 |
} |
| 735 |
|
| 736 |
static int conn_puts(BIO *bp, const char *str) |
| 737 |
{ |
| 738 |
int ret; |
| 739 |
size_t n = strlen(str); |
| 740 |
|
| 741 |
if (n > INT_MAX) |
| 742 |
return -1; |
| 743 |
ret = conn_write(bp, str, (int)n); |
| 744 |
return ret; |
| 745 |
} |
| 746 |
|
| 747 |
int conn_gets(BIO *bio, char *buf, int size) |
| 748 |
{ |
| 749 |
BIO_CONNECT *data; |
| 750 |
char *ptr = buf; |
| 751 |
int ret = 0; |
| 752 |
|
| 753 |
if (buf == NULL) { |
| 754 |
ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
| 755 |
return -1; |
| 756 |
} |
| 757 |
if (size <= 0) { |
| 758 |
ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); |
| 759 |
return -1; |
| 760 |
} |
| 761 |
*buf = '\0'; |
| 762 |
|
| 763 |
if (bio == NULL || bio->ptr == NULL) { |
| 764 |
ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
| 765 |
return -1; |
| 766 |
} |
| 767 |
data = (BIO_CONNECT *)bio->ptr; |
| 768 |
if (data->state != BIO_CONN_S_OK) { |
| 769 |
ret = conn_state(bio, data); |
| 770 |
if (ret <= 0) |
| 771 |
return ret; |
| 772 |
} |
| 773 |
|
| 774 |
if (data->dgram_bio != NULL) { |
| 775 |
ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 776 |
return -1; |
| 777 |
} |
| 778 |
|
| 779 |
clear_socket_error(); |
| 780 |
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 |
ret = readsocket(bio->num, ptr, 1); |
| 787 |
BIO_clear_retry_flags(bio); |
| 788 |
if (ret <= 0) { |
| 789 |
if (BIO_sock_should_retry(ret)) |
| 790 |
BIO_set_retry_read(bio); |
| 791 |
else if (ret == 0) |
| 792 |
bio->flags |= BIO_FLAGS_IN_EOF; |
| 793 |
break; |
| 794 |
} |
| 795 |
if (*ptr++ == '\n') |
| 796 |
break; |
| 797 |
} |
| 798 |
*ptr = '\0'; |
| 799 |
return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? (int)(ptr - buf) : ret; |
| 800 |
} |
| 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 |
{ |
| 805 |
int ret; |
| 806 |
BIO_CONNECT *data; |
| 807 |
|
| 808 |
if (bio == NULL) { |
| 809 |
*msgs_processed = 0; |
| 810 |
ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
| 811 |
return 0; |
| 812 |
} |
| 813 |
|
| 814 |
data = (BIO_CONNECT *)bio->ptr; |
| 815 |
if (data->state != BIO_CONN_S_OK) { |
| 816 |
ret = conn_state(bio, data); |
| 817 |
if (ret <= 0) { |
| 818 |
*msgs_processed = 0; |
| 819 |
return 0; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
if (data->dgram_bio == NULL) { |
| 824 |
*msgs_processed = 0; |
| 825 |
ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
return BIO_sendmmsg(data->dgram_bio, msg, stride, num_msgs, |
| 830 |
flags, msgs_processed); |
| 831 |
} |
| 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 |
{ |
| 836 |
int ret; |
| 837 |
BIO_CONNECT *data; |
| 838 |
|
| 839 |
if (bio == NULL) { |
| 840 |
*msgs_processed = 0; |
| 841 |
ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
| 842 |
return 0; |
| 843 |
} |
| 844 |
|
| 845 |
data = (BIO_CONNECT *)bio->ptr; |
| 846 |
if (data->state != BIO_CONN_S_OK) { |
| 847 |
ret = conn_state(bio, data); |
| 848 |
if (ret <= 0) { |
| 849 |
*msgs_processed = 0; |
| 850 |
return 0; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
if (data->dgram_bio == NULL) { |
| 855 |
*msgs_processed = 0; |
| 856 |
ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 857 |
return 0; |
| 858 |
} |
| 859 |
|
| 860 |
return BIO_recvmmsg(data->dgram_bio, msg, stride, num_msgs, |
| 861 |
flags, msgs_processed); |
| 862 |
} |
| 863 |
|
| 864 |
BIO *BIO_new_connect(const char *str) |
| 865 |
{ |
| 866 |
BIO *ret; |
| 867 |
|
| 868 |
ret = BIO_new(BIO_s_connect()); |
| 869 |
if (ret == NULL) |
| 870 |
return NULL; |
| 871 |
if (BIO_set_conn_hostname(ret, str)) |
| 872 |
return ret; |
| 873 |
BIO_free(ret); |
| 874 |
return NULL; |
| 875 |
} |
| 876 |
|
| 877 |
#endif |