bss_conn.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. #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. char *param_hostname;
  19. char *param_service;
  20. int connect_mode;
  21. # ifndef OPENSSL_NO_KTLS
  22. unsigned char record_type;
  23. # endif
  24. int tfo_first;
  25. BIO_ADDRINFO *addr_first;
  26. const BIO_ADDRINFO *addr_iter;
  27. /*
  28. * int socket; this will be kept in bio->num so that it is compatible
  29. * with the bss_sock bio
  30. */
  31. /*
  32. * called when the connection is initially made callback(BIO,state,ret);
  33. * The callback should return 'ret'. state is for compatibility with the
  34. * ssl info_callback
  35. */
  36. BIO_info_cb *info_callback;
  37. } BIO_CONNECT;
  38. static int conn_write(BIO *h, const char *buf, int num);
  39. static int conn_read(BIO *h, char *buf, int size);
  40. static int conn_puts(BIO *h, const char *str);
  41. static int conn_gets(BIO *h, char *buf, int size);
  42. static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  43. static int conn_new(BIO *h);
  44. static int conn_free(BIO *data);
  45. static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
  46. static int conn_state(BIO *b, BIO_CONNECT *c);
  47. static void conn_close_socket(BIO *data);
  48. BIO_CONNECT *BIO_CONNECT_new(void);
  49. void BIO_CONNECT_free(BIO_CONNECT *a);
  50. #define BIO_CONN_S_BEFORE 1
  51. #define BIO_CONN_S_GET_ADDR 2
  52. #define BIO_CONN_S_CREATE_SOCKET 3
  53. #define BIO_CONN_S_CONNECT 4
  54. #define BIO_CONN_S_OK 5
  55. #define BIO_CONN_S_BLOCKED_CONNECT 6
  56. #define BIO_CONN_S_CONNECT_ERROR 7
  57. static const BIO_METHOD methods_connectp = {
  58. BIO_TYPE_CONNECT,
  59. "socket connect",
  60. bwrite_conv,
  61. conn_write,
  62. bread_conv,
  63. conn_read,
  64. conn_puts,
  65. conn_gets,
  66. conn_ctrl,
  67. conn_new,
  68. conn_free,
  69. conn_callback_ctrl,
  70. };
  71. static int conn_state(BIO *b, BIO_CONNECT *c)
  72. {
  73. int ret = -1, i;
  74. BIO_info_cb *cb = NULL;
  75. if (c->info_callback != NULL)
  76. cb = c->info_callback;
  77. for (;;) {
  78. switch (c->state) {
  79. case BIO_CONN_S_BEFORE:
  80. if (c->param_hostname == NULL && c->param_service == NULL) {
  81. ERR_raise_data(ERR_LIB_BIO,
  82. BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
  83. "hostname=%s service=%s",
  84. c->param_hostname, c->param_service);
  85. goto exit_loop;
  86. }
  87. c->state = BIO_CONN_S_GET_ADDR;
  88. break;
  89. case BIO_CONN_S_GET_ADDR:
  90. {
  91. int family = AF_UNSPEC;
  92. switch (c->connect_family) {
  93. case BIO_FAMILY_IPV6:
  94. if (1) { /* This is a trick we use to avoid bit rot.
  95. * at least the "else" part will always be
  96. * compiled.
  97. */
  98. #if OPENSSL_USE_IPV6
  99. family = AF_INET6;
  100. } else {
  101. #endif
  102. ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
  103. goto exit_loop;
  104. }
  105. break;
  106. case BIO_FAMILY_IPV4:
  107. family = AF_INET;
  108. break;
  109. case BIO_FAMILY_IPANY:
  110. family = AF_UNSPEC;
  111. break;
  112. default:
  113. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
  114. goto exit_loop;
  115. }
  116. if (BIO_lookup(c->param_hostname, c->param_service,
  117. BIO_LOOKUP_CLIENT,
  118. family, SOCK_STREAM, &c->addr_first) == 0)
  119. goto exit_loop;
  120. }
  121. if (c->addr_first == NULL) {
  122. ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
  123. goto exit_loop;
  124. }
  125. c->addr_iter = c->addr_first;
  126. c->state = BIO_CONN_S_CREATE_SOCKET;
  127. break;
  128. case BIO_CONN_S_CREATE_SOCKET:
  129. ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
  130. BIO_ADDRINFO_socktype(c->addr_iter),
  131. BIO_ADDRINFO_protocol(c->addr_iter), 0);
  132. if (ret == (int)INVALID_SOCKET) {
  133. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  134. "calling socket(%s, %s)",
  135. c->param_hostname, c->param_service);
  136. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
  137. goto exit_loop;
  138. }
  139. b->num = ret;
  140. c->state = BIO_CONN_S_CONNECT;
  141. break;
  142. case BIO_CONN_S_CONNECT:
  143. BIO_clear_retry_flags(b);
  144. ERR_set_mark();
  145. ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
  146. BIO_SOCK_KEEPALIVE | c->connect_mode);
  147. b->retry_reason = 0;
  148. if (ret == 0) {
  149. if (BIO_sock_should_retry(ret)) {
  150. BIO_set_retry_special(b);
  151. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  152. b->retry_reason = BIO_RR_CONNECT;
  153. ERR_pop_to_mark();
  154. } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
  155. != NULL) {
  156. /*
  157. * if there are more addresses to try, do that first
  158. */
  159. BIO_closesocket(b->num);
  160. c->state = BIO_CONN_S_CREATE_SOCKET;
  161. ERR_pop_to_mark();
  162. break;
  163. } else {
  164. ERR_clear_last_mark();
  165. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  166. "calling connect(%s, %s)",
  167. c->param_hostname, c->param_service);
  168. c->state = BIO_CONN_S_CONNECT_ERROR;
  169. break;
  170. }
  171. goto exit_loop;
  172. } else {
  173. ERR_clear_last_mark();
  174. c->state = BIO_CONN_S_OK;
  175. }
  176. break;
  177. case BIO_CONN_S_BLOCKED_CONNECT:
  178. /* wait for socket being writable, before querying BIO_sock_error */
  179. if (BIO_socket_wait(b->num, 0, time(NULL)) == 0)
  180. break;
  181. i = BIO_sock_error(b->num);
  182. if (i != 0) {
  183. BIO_clear_retry_flags(b);
  184. if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != 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. break;
  191. }
  192. ERR_raise_data(ERR_LIB_SYS, i,
  193. "calling connect(%s, %s)",
  194. c->param_hostname, c->param_service);
  195. ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
  196. ret = 0;
  197. goto exit_loop;
  198. } else {
  199. c->state = BIO_CONN_S_OK;
  200. # ifndef OPENSSL_NO_KTLS
  201. /*
  202. * The new socket is created successfully regardless of ktls_enable.
  203. * ktls_enable doesn't change any functionality of the socket, except
  204. * changing the setsockopt to enable the processing of ktls_start.
  205. * Thus, it is not a problem to call it for non-TLS sockets.
  206. */
  207. ktls_enable(b->num);
  208. # endif
  209. }
  210. break;
  211. case BIO_CONN_S_CONNECT_ERROR:
  212. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  213. ret = 0;
  214. goto exit_loop;
  215. case BIO_CONN_S_OK:
  216. ret = 1;
  217. goto exit_loop;
  218. default:
  219. /* abort(); */
  220. goto exit_loop;
  221. }
  222. if (cb != NULL) {
  223. if ((ret = cb((BIO *)b, c->state, ret)) == 0)
  224. goto end;
  225. }
  226. }
  227. /* Loop does not exit */
  228. exit_loop:
  229. if (cb != NULL)
  230. ret = cb((BIO *)b, c->state, ret);
  231. end:
  232. return ret;
  233. }
  234. BIO_CONNECT *BIO_CONNECT_new(void)
  235. {
  236. BIO_CONNECT *ret;
  237. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  238. return NULL;
  239. ret->state = BIO_CONN_S_BEFORE;
  240. ret->connect_family = BIO_FAMILY_IPANY;
  241. return ret;
  242. }
  243. void BIO_CONNECT_free(BIO_CONNECT *a)
  244. {
  245. if (a == NULL)
  246. return;
  247. OPENSSL_free(a->param_hostname);
  248. OPENSSL_free(a->param_service);
  249. BIO_ADDRINFO_free(a->addr_first);
  250. OPENSSL_free(a);
  251. }
  252. const BIO_METHOD *BIO_s_connect(void)
  253. {
  254. return &methods_connectp;
  255. }
  256. static int conn_new(BIO *bi)
  257. {
  258. bi->init = 0;
  259. bi->num = (int)INVALID_SOCKET;
  260. bi->flags = 0;
  261. if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
  262. return 0;
  263. else
  264. return 1;
  265. }
  266. static void conn_close_socket(BIO *bio)
  267. {
  268. BIO_CONNECT *c;
  269. c = (BIO_CONNECT *)bio->ptr;
  270. if (bio->num != (int)INVALID_SOCKET) {
  271. /* Only do a shutdown if things were established */
  272. if (c->state == BIO_CONN_S_OK)
  273. shutdown(bio->num, 2);
  274. BIO_closesocket(bio->num);
  275. bio->num = (int)INVALID_SOCKET;
  276. }
  277. }
  278. static int conn_free(BIO *a)
  279. {
  280. BIO_CONNECT *data;
  281. if (a == NULL)
  282. return 0;
  283. data = (BIO_CONNECT *)a->ptr;
  284. if (a->shutdown) {
  285. conn_close_socket(a);
  286. BIO_CONNECT_free(data);
  287. a->ptr = NULL;
  288. a->flags = 0;
  289. a->init = 0;
  290. }
  291. return 1;
  292. }
  293. static int conn_read(BIO *b, char *out, int outl)
  294. {
  295. int ret = 0;
  296. BIO_CONNECT *data;
  297. data = (BIO_CONNECT *)b->ptr;
  298. if (data->state != BIO_CONN_S_OK) {
  299. ret = conn_state(b, data);
  300. if (ret <= 0)
  301. return ret;
  302. }
  303. if (out != NULL) {
  304. clear_socket_error();
  305. # ifndef OPENSSL_NO_KTLS
  306. if (BIO_get_ktls_recv(b))
  307. ret = ktls_read_record(b->num, out, outl);
  308. else
  309. # endif
  310. ret = readsocket(b->num, out, outl);
  311. BIO_clear_retry_flags(b);
  312. if (ret <= 0) {
  313. if (BIO_sock_should_retry(ret))
  314. BIO_set_retry_read(b);
  315. else if (ret == 0)
  316. b->flags |= BIO_FLAGS_IN_EOF;
  317. }
  318. }
  319. return ret;
  320. }
  321. static int conn_write(BIO *b, const char *in, int inl)
  322. {
  323. int ret;
  324. BIO_CONNECT *data;
  325. data = (BIO_CONNECT *)b->ptr;
  326. if (data->state != BIO_CONN_S_OK) {
  327. ret = conn_state(b, data);
  328. if (ret <= 0)
  329. return ret;
  330. }
  331. clear_socket_error();
  332. # ifndef OPENSSL_NO_KTLS
  333. if (BIO_should_ktls_ctrl_msg_flag(b)) {
  334. ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
  335. if (ret >= 0) {
  336. ret = inl;
  337. BIO_clear_ktls_ctrl_msg_flag(b);
  338. }
  339. } else
  340. # endif
  341. # if defined(OSSL_TFO_SENDTO)
  342. if (data->tfo_first) {
  343. int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter);
  344. ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO,
  345. BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen);
  346. data->tfo_first = 0;
  347. } else
  348. # endif
  349. ret = writesocket(b->num, in, inl);
  350. BIO_clear_retry_flags(b);
  351. if (ret <= 0) {
  352. if (BIO_sock_should_retry(ret))
  353. BIO_set_retry_write(b);
  354. }
  355. return ret;
  356. }
  357. static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
  358. {
  359. BIO *dbio;
  360. int *ip;
  361. const char **pptr = NULL;
  362. long ret = 1;
  363. BIO_CONNECT *data;
  364. # ifndef OPENSSL_NO_KTLS
  365. ktls_crypto_info_t *crypto_info;
  366. # endif
  367. data = (BIO_CONNECT *)b->ptr;
  368. switch (cmd) {
  369. case BIO_CTRL_RESET:
  370. ret = 0;
  371. data->state = BIO_CONN_S_BEFORE;
  372. conn_close_socket(b);
  373. BIO_ADDRINFO_free(data->addr_first);
  374. data->addr_first = NULL;
  375. b->flags = 0;
  376. break;
  377. case BIO_C_DO_STATE_MACHINE:
  378. /* use this one to start the connection */
  379. if (data->state != BIO_CONN_S_OK)
  380. ret = (long)conn_state(b, data);
  381. else
  382. ret = 1;
  383. break;
  384. case BIO_C_GET_CONNECT:
  385. if (ptr != NULL) {
  386. pptr = (const char **)ptr;
  387. if (num == 0) {
  388. *pptr = data->param_hostname;
  389. } else if (num == 1) {
  390. *pptr = data->param_service;
  391. } else if (num == 2) {
  392. *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
  393. } else if (num == 3) {
  394. switch (BIO_ADDRINFO_family(data->addr_iter)) {
  395. # if OPENSSL_USE_IPV6
  396. case AF_INET6:
  397. ret = BIO_FAMILY_IPV6;
  398. break;
  399. # endif
  400. case AF_INET:
  401. ret = BIO_FAMILY_IPV4;
  402. break;
  403. case 0:
  404. ret = data->connect_family;
  405. break;
  406. default:
  407. ret = -1;
  408. break;
  409. }
  410. } else if (num == 4) {
  411. ret = data->connect_mode;
  412. } else {
  413. ret = 0;
  414. }
  415. } else {
  416. ret = 0;
  417. }
  418. break;
  419. case BIO_C_SET_CONNECT:
  420. if (ptr != NULL) {
  421. b->init = 1;
  422. if (num == 0) { /* BIO_set_conn_hostname */
  423. char *hold_service = data->param_service;
  424. /* We affect the hostname regardless. However, the input
  425. * string might contain a host:service spec, so we must
  426. * parse it, which might or might not affect the service
  427. */
  428. OPENSSL_free(data->param_hostname);
  429. data->param_hostname = NULL;
  430. ret = BIO_parse_hostserv(ptr,
  431. &data->param_hostname,
  432. &data->param_service,
  433. BIO_PARSE_PRIO_HOST);
  434. if (hold_service != data->param_service)
  435. OPENSSL_free(hold_service);
  436. } else if (num == 1) { /* BIO_set_conn_port */
  437. OPENSSL_free(data->param_service);
  438. if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
  439. ret = 0;
  440. } else if (num == 2) { /* BIO_set_conn_address */
  441. const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
  442. char *host = BIO_ADDR_hostname_string(addr, 1);
  443. char *service = BIO_ADDR_service_string(addr, 1);
  444. ret = host != NULL && service != NULL;
  445. if (ret) {
  446. OPENSSL_free(data->param_hostname);
  447. data->param_hostname = host;
  448. OPENSSL_free(data->param_service);
  449. data->param_service = service;
  450. BIO_ADDRINFO_free(data->addr_first);
  451. data->addr_first = NULL;
  452. data->addr_iter = NULL;
  453. } else {
  454. OPENSSL_free(host);
  455. OPENSSL_free(service);
  456. }
  457. } else if (num == 3) { /* BIO_set_conn_ip_family */
  458. data->connect_family = *(int *)ptr;
  459. } else {
  460. ret = 0;
  461. }
  462. }
  463. break;
  464. case BIO_C_SET_NBIO:
  465. if (num != 0)
  466. data->connect_mode |= BIO_SOCK_NONBLOCK;
  467. else
  468. data->connect_mode &= ~BIO_SOCK_NONBLOCK;
  469. break;
  470. #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO)
  471. case BIO_C_SET_TFO:
  472. if (num != 0) {
  473. data->connect_mode |= BIO_SOCK_TFO;
  474. data->tfo_first = 1;
  475. } else {
  476. data->connect_mode &= ~BIO_SOCK_TFO;
  477. data->tfo_first = 0;
  478. }
  479. break;
  480. #endif
  481. case BIO_C_SET_CONNECT_MODE:
  482. data->connect_mode = (int)num;
  483. if (num & BIO_SOCK_TFO)
  484. data->tfo_first = 1;
  485. else
  486. data->tfo_first = 0;
  487. break;
  488. case BIO_C_GET_FD:
  489. if (b->init) {
  490. ip = (int *)ptr;
  491. if (ip != NULL)
  492. *ip = b->num;
  493. ret = b->num;
  494. } else
  495. ret = -1;
  496. break;
  497. case BIO_CTRL_GET_CLOSE:
  498. ret = b->shutdown;
  499. break;
  500. case BIO_CTRL_SET_CLOSE:
  501. b->shutdown = (int)num;
  502. break;
  503. case BIO_CTRL_PENDING:
  504. case BIO_CTRL_WPENDING:
  505. ret = 0;
  506. break;
  507. case BIO_CTRL_FLUSH:
  508. break;
  509. case BIO_CTRL_DUP:
  510. {
  511. dbio = (BIO *)ptr;
  512. if (data->param_hostname)
  513. BIO_set_conn_hostname(dbio, data->param_hostname);
  514. if (data->param_service)
  515. BIO_set_conn_port(dbio, data->param_service);
  516. BIO_set_conn_ip_family(dbio, data->connect_family);
  517. BIO_set_conn_mode(dbio, data->connect_mode);
  518. /*
  519. * FIXME: the cast of the function seems unlikely to be a good
  520. * idea
  521. */
  522. (void)BIO_set_info_callback(dbio, data->info_callback);
  523. }
  524. break;
  525. case BIO_CTRL_SET_CALLBACK:
  526. ret = 0; /* use callback ctrl */
  527. break;
  528. case BIO_CTRL_GET_CALLBACK:
  529. {
  530. BIO_info_cb **fptr;
  531. fptr = (BIO_info_cb **)ptr;
  532. *fptr = data->info_callback;
  533. }
  534. break;
  535. case BIO_CTRL_EOF:
  536. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
  537. break;
  538. # ifndef OPENSSL_NO_KTLS
  539. case BIO_CTRL_SET_KTLS:
  540. crypto_info = (ktls_crypto_info_t *)ptr;
  541. ret = ktls_start(b->num, crypto_info, num);
  542. if (ret)
  543. BIO_set_ktls_flag(b, num);
  544. break;
  545. case BIO_CTRL_GET_KTLS_SEND:
  546. return BIO_should_ktls_flag(b, 1) != 0;
  547. case BIO_CTRL_GET_KTLS_RECV:
  548. return BIO_should_ktls_flag(b, 0) != 0;
  549. case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
  550. BIO_set_ktls_ctrl_msg_flag(b);
  551. data->record_type = num;
  552. ret = 0;
  553. break;
  554. case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
  555. BIO_clear_ktls_ctrl_msg_flag(b);
  556. ret = 0;
  557. break;
  558. case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE:
  559. ret = ktls_enable_tx_zerocopy_sendfile(b->num);
  560. if (ret)
  561. BIO_set_ktls_zerocopy_sendfile_flag(b);
  562. break;
  563. # endif
  564. default:
  565. ret = 0;
  566. break;
  567. }
  568. return ret;
  569. }
  570. static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  571. {
  572. long ret = 1;
  573. BIO_CONNECT *data;
  574. data = (BIO_CONNECT *)b->ptr;
  575. switch (cmd) {
  576. case BIO_CTRL_SET_CALLBACK:
  577. {
  578. data->info_callback = fp;
  579. }
  580. break;
  581. default:
  582. ret = 0;
  583. break;
  584. }
  585. return ret;
  586. }
  587. static int conn_puts(BIO *bp, const char *str)
  588. {
  589. int n, ret;
  590. n = strlen(str);
  591. ret = conn_write(bp, str, n);
  592. return ret;
  593. }
  594. int conn_gets(BIO *bio, char *buf, int size)
  595. {
  596. BIO_CONNECT *data;
  597. char *ptr = buf;
  598. int ret = 0;
  599. if (buf == NULL) {
  600. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  601. return -1;
  602. }
  603. if (size <= 0) {
  604. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
  605. return -1;
  606. }
  607. *buf = '\0';
  608. if (bio == NULL || bio->ptr == NULL) {
  609. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  610. return -1;
  611. }
  612. data = (BIO_CONNECT *)bio->ptr;
  613. if (data->state != BIO_CONN_S_OK) {
  614. ret = conn_state(bio, data);
  615. if (ret <= 0)
  616. return ret;
  617. }
  618. clear_socket_error();
  619. while (size-- > 1) {
  620. # ifndef OPENSSL_NO_KTLS
  621. if (BIO_get_ktls_recv(bio))
  622. ret = ktls_read_record(bio->num, ptr, 1);
  623. else
  624. # endif
  625. ret = readsocket(bio->num, ptr, 1);
  626. BIO_clear_retry_flags(bio);
  627. if (ret <= 0) {
  628. if (BIO_sock_should_retry(ret))
  629. BIO_set_retry_read(bio);
  630. else if (ret == 0)
  631. bio->flags |= BIO_FLAGS_IN_EOF;
  632. break;
  633. }
  634. if (*ptr++ == '\n')
  635. break;
  636. }
  637. *ptr = '\0';
  638. return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret;
  639. }
  640. BIO *BIO_new_connect(const char *str)
  641. {
  642. BIO *ret;
  643. ret = BIO_new(BIO_s_connect());
  644. if (ret == NULL)
  645. return NULL;
  646. if (BIO_set_conn_hostname(ret, str))
  647. return ret;
  648. BIO_free(ret);
  649. return NULL;
  650. }
  651. #endif