bss_conn.c 25 KB

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