bss_conn.c 25 KB

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