bss_conn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright 1995-2018 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_lcl.h"
  12. #ifndef OPENSSL_NO_SOCK
  13. typedef struct bio_connect_st {
  14. int state;
  15. int connect_family;
  16. char *param_hostname;
  17. char *param_service;
  18. int connect_mode;
  19. BIO_ADDRINFO *addr_first;
  20. const BIO_ADDRINFO *addr_iter;
  21. /*
  22. * int socket; this will be kept in bio->num so that it is compatible
  23. * with the bss_sock bio
  24. */
  25. /*
  26. * called when the connection is initially made callback(BIO,state,ret);
  27. * The callback should return 'ret'. state is for compatibility with the
  28. * ssl info_callback
  29. */
  30. BIO_info_cb *info_callback;
  31. } BIO_CONNECT;
  32. static int conn_write(BIO *h, const char *buf, int num);
  33. static int conn_read(BIO *h, char *buf, int size);
  34. static int conn_puts(BIO *h, const char *str);
  35. static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  36. static int conn_new(BIO *h);
  37. static int conn_free(BIO *data);
  38. static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
  39. static int conn_state(BIO *b, BIO_CONNECT *c);
  40. static void conn_close_socket(BIO *data);
  41. BIO_CONNECT *BIO_CONNECT_new(void);
  42. void BIO_CONNECT_free(BIO_CONNECT *a);
  43. #define BIO_CONN_S_BEFORE 1
  44. #define BIO_CONN_S_GET_ADDR 2
  45. #define BIO_CONN_S_CREATE_SOCKET 3
  46. #define BIO_CONN_S_CONNECT 4
  47. #define BIO_CONN_S_OK 5
  48. #define BIO_CONN_S_BLOCKED_CONNECT 6
  49. static const BIO_METHOD methods_connectp = {
  50. BIO_TYPE_CONNECT,
  51. "socket connect",
  52. /* TODO: Convert to new style write function */
  53. bwrite_conv,
  54. conn_write,
  55. /* TODO: Convert to new style read function */
  56. bread_conv,
  57. conn_read,
  58. conn_puts,
  59. NULL, /* conn_gets, */
  60. conn_ctrl,
  61. conn_new,
  62. conn_free,
  63. conn_callback_ctrl,
  64. };
  65. static int conn_state(BIO *b, BIO_CONNECT *c)
  66. {
  67. int ret = -1, i;
  68. BIO_info_cb *cb = NULL;
  69. if (c->info_callback != NULL)
  70. cb = c->info_callback;
  71. for (;;) {
  72. switch (c->state) {
  73. case BIO_CONN_S_BEFORE:
  74. if (c->param_hostname == NULL && c->param_service == NULL) {
  75. BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);
  76. ERR_add_error_data(4,
  77. "hostname=", c->param_hostname,
  78. " service=", c->param_service);
  79. goto exit_loop;
  80. }
  81. c->state = BIO_CONN_S_GET_ADDR;
  82. break;
  83. case BIO_CONN_S_GET_ADDR:
  84. {
  85. int family = AF_UNSPEC;
  86. switch (c->connect_family) {
  87. case BIO_FAMILY_IPV6:
  88. if (1) { /* This is a trick we use to avoid bit rot.
  89. * at least the "else" part will always be
  90. * compiled.
  91. */
  92. #ifdef AF_INET6
  93. family = AF_INET6;
  94. } else {
  95. #endif
  96. BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
  97. goto exit_loop;
  98. }
  99. break;
  100. case BIO_FAMILY_IPV4:
  101. family = AF_INET;
  102. break;
  103. case BIO_FAMILY_IPANY:
  104. family = AF_UNSPEC;
  105. break;
  106. default:
  107. BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
  108. goto exit_loop;
  109. }
  110. if (BIO_lookup(c->param_hostname, c->param_service,
  111. BIO_LOOKUP_CLIENT,
  112. family, SOCK_STREAM, &c->addr_first) == 0)
  113. goto exit_loop;
  114. }
  115. if (c->addr_first == NULL) {
  116. BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
  117. goto exit_loop;
  118. }
  119. c->addr_iter = c->addr_first;
  120. c->state = BIO_CONN_S_CREATE_SOCKET;
  121. break;
  122. case BIO_CONN_S_CREATE_SOCKET:
  123. ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
  124. BIO_ADDRINFO_socktype(c->addr_iter),
  125. BIO_ADDRINFO_protocol(c->addr_iter), 0);
  126. if (ret == (int)INVALID_SOCKET) {
  127. SYSerr(SYS_F_SOCKET, get_last_socket_error());
  128. ERR_add_error_data(4,
  129. "hostname=", c->param_hostname,
  130. " service=", c->param_service);
  131. BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
  132. goto exit_loop;
  133. }
  134. b->num = ret;
  135. c->state = BIO_CONN_S_CONNECT;
  136. break;
  137. case BIO_CONN_S_CONNECT:
  138. BIO_clear_retry_flags(b);
  139. ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
  140. BIO_SOCK_KEEPALIVE | c->connect_mode);
  141. b->retry_reason = 0;
  142. if (ret == 0) {
  143. if (BIO_sock_should_retry(ret)) {
  144. BIO_set_retry_special(b);
  145. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  146. b->retry_reason = BIO_RR_CONNECT;
  147. ERR_clear_error();
  148. } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
  149. != NULL) {
  150. /*
  151. * if there are more addresses to try, do that first
  152. */
  153. BIO_closesocket(b->num);
  154. c->state = BIO_CONN_S_CREATE_SOCKET;
  155. ERR_clear_error();
  156. break;
  157. } else {
  158. SYSerr(SYS_F_CONNECT, get_last_socket_error());
  159. ERR_add_error_data(4,
  160. "hostname=", c->param_hostname,
  161. " service=", c->param_service);
  162. BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
  163. }
  164. goto exit_loop;
  165. } else {
  166. c->state = BIO_CONN_S_OK;
  167. }
  168. break;
  169. case BIO_CONN_S_BLOCKED_CONNECT:
  170. i = BIO_sock_error(b->num);
  171. if (i) {
  172. BIO_clear_retry_flags(b);
  173. SYSerr(SYS_F_CONNECT, i);
  174. ERR_add_error_data(4,
  175. "hostname=", c->param_hostname,
  176. " service=", c->param_service);
  177. BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
  178. ret = 0;
  179. goto exit_loop;
  180. } else
  181. c->state = BIO_CONN_S_OK;
  182. break;
  183. case BIO_CONN_S_OK:
  184. ret = 1;
  185. goto exit_loop;
  186. default:
  187. /* abort(); */
  188. goto exit_loop;
  189. }
  190. if (cb != NULL) {
  191. if ((ret = cb((BIO *)b, c->state, ret)) == 0)
  192. goto end;
  193. }
  194. }
  195. /* Loop does not exit */
  196. exit_loop:
  197. if (cb != NULL)
  198. ret = cb((BIO *)b, c->state, ret);
  199. end:
  200. return ret;
  201. }
  202. BIO_CONNECT *BIO_CONNECT_new(void)
  203. {
  204. BIO_CONNECT *ret;
  205. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  206. BIOerr(BIO_F_BIO_CONNECT_NEW, ERR_R_MALLOC_FAILURE);
  207. return NULL;
  208. }
  209. ret->state = BIO_CONN_S_BEFORE;
  210. ret->connect_family = BIO_FAMILY_IPANY;
  211. return ret;
  212. }
  213. void BIO_CONNECT_free(BIO_CONNECT *a)
  214. {
  215. if (a == NULL)
  216. return;
  217. OPENSSL_free(a->param_hostname);
  218. OPENSSL_free(a->param_service);
  219. BIO_ADDRINFO_free(a->addr_first);
  220. OPENSSL_free(a);
  221. }
  222. const BIO_METHOD *BIO_s_connect(void)
  223. {
  224. return &methods_connectp;
  225. }
  226. static int conn_new(BIO *bi)
  227. {
  228. bi->init = 0;
  229. bi->num = (int)INVALID_SOCKET;
  230. bi->flags = 0;
  231. if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
  232. return 0;
  233. else
  234. return 1;
  235. }
  236. static void conn_close_socket(BIO *bio)
  237. {
  238. BIO_CONNECT *c;
  239. c = (BIO_CONNECT *)bio->ptr;
  240. if (bio->num != (int)INVALID_SOCKET) {
  241. /* Only do a shutdown if things were established */
  242. if (c->state == BIO_CONN_S_OK)
  243. shutdown(bio->num, 2);
  244. BIO_closesocket(bio->num);
  245. bio->num = (int)INVALID_SOCKET;
  246. }
  247. }
  248. static int conn_free(BIO *a)
  249. {
  250. BIO_CONNECT *data;
  251. if (a == NULL)
  252. return 0;
  253. data = (BIO_CONNECT *)a->ptr;
  254. if (a->shutdown) {
  255. conn_close_socket(a);
  256. BIO_CONNECT_free(data);
  257. a->ptr = NULL;
  258. a->flags = 0;
  259. a->init = 0;
  260. }
  261. return 1;
  262. }
  263. static int conn_read(BIO *b, char *out, int outl)
  264. {
  265. int ret = 0;
  266. BIO_CONNECT *data;
  267. data = (BIO_CONNECT *)b->ptr;
  268. if (data->state != BIO_CONN_S_OK) {
  269. ret = conn_state(b, data);
  270. if (ret <= 0)
  271. return ret;
  272. }
  273. if (out != NULL) {
  274. clear_socket_error();
  275. ret = readsocket(b->num, out, outl);
  276. BIO_clear_retry_flags(b);
  277. if (ret <= 0) {
  278. if (BIO_sock_should_retry(ret))
  279. BIO_set_retry_read(b);
  280. }
  281. }
  282. return ret;
  283. }
  284. static int conn_write(BIO *b, const char *in, int inl)
  285. {
  286. int ret;
  287. BIO_CONNECT *data;
  288. data = (BIO_CONNECT *)b->ptr;
  289. if (data->state != BIO_CONN_S_OK) {
  290. ret = conn_state(b, data);
  291. if (ret <= 0)
  292. return ret;
  293. }
  294. clear_socket_error();
  295. ret = writesocket(b->num, in, inl);
  296. BIO_clear_retry_flags(b);
  297. if (ret <= 0) {
  298. if (BIO_sock_should_retry(ret))
  299. BIO_set_retry_write(b);
  300. }
  301. return ret;
  302. }
  303. static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
  304. {
  305. BIO *dbio;
  306. int *ip;
  307. const char **pptr = NULL;
  308. long ret = 1;
  309. BIO_CONNECT *data;
  310. data = (BIO_CONNECT *)b->ptr;
  311. switch (cmd) {
  312. case BIO_CTRL_RESET:
  313. ret = 0;
  314. data->state = BIO_CONN_S_BEFORE;
  315. conn_close_socket(b);
  316. BIO_ADDRINFO_free(data->addr_first);
  317. data->addr_first = NULL;
  318. b->flags = 0;
  319. break;
  320. case BIO_C_DO_STATE_MACHINE:
  321. /* use this one to start the connection */
  322. if (data->state != BIO_CONN_S_OK)
  323. ret = (long)conn_state(b, data);
  324. else
  325. ret = 1;
  326. break;
  327. case BIO_C_GET_CONNECT:
  328. if (ptr != NULL) {
  329. pptr = (const char **)ptr;
  330. if (num == 0) {
  331. *pptr = data->param_hostname;
  332. } else if (num == 1) {
  333. *pptr = data->param_service;
  334. } else if (num == 2) {
  335. *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
  336. } else if (num == 3) {
  337. switch (BIO_ADDRINFO_family(data->addr_iter)) {
  338. # ifdef AF_INET6
  339. case AF_INET6:
  340. ret = BIO_FAMILY_IPV6;
  341. break;
  342. # endif
  343. case AF_INET:
  344. ret = BIO_FAMILY_IPV4;
  345. break;
  346. case 0:
  347. ret = data->connect_family;
  348. break;
  349. default:
  350. ret = -1;
  351. break;
  352. }
  353. } else {
  354. ret = 0;
  355. }
  356. } else {
  357. ret = 0;
  358. }
  359. break;
  360. case BIO_C_SET_CONNECT:
  361. if (ptr != NULL) {
  362. b->init = 1;
  363. if (num == 0) {
  364. char *hold_service = data->param_service;
  365. /* We affect the hostname regardless. However, the input
  366. * string might contain a host:service spec, so we must
  367. * parse it, which might or might not affect the service
  368. */
  369. OPENSSL_free(data->param_hostname);
  370. data->param_hostname = NULL;
  371. ret = BIO_parse_hostserv(ptr,
  372. &data->param_hostname,
  373. &data->param_service,
  374. BIO_PARSE_PRIO_HOST);
  375. if (hold_service != data->param_service)
  376. OPENSSL_free(hold_service);
  377. } else if (num == 1) {
  378. OPENSSL_free(data->param_service);
  379. data->param_service = BUF_strdup(ptr);
  380. } else if (num == 2) {
  381. const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
  382. if (ret) {
  383. data->param_hostname = BIO_ADDR_hostname_string(addr, 1);
  384. data->param_service = BIO_ADDR_service_string(addr, 1);
  385. BIO_ADDRINFO_free(data->addr_first);
  386. data->addr_first = NULL;
  387. data->addr_iter = NULL;
  388. }
  389. } else if (num == 3) {
  390. data->connect_family = *(int *)ptr;
  391. } else {
  392. ret = 0;
  393. }
  394. }
  395. break;
  396. case BIO_C_SET_NBIO:
  397. if (num != 0)
  398. data->connect_mode |= BIO_SOCK_NONBLOCK;
  399. else
  400. data->connect_mode &= ~BIO_SOCK_NONBLOCK;
  401. break;
  402. case BIO_C_SET_CONNECT_MODE:
  403. data->connect_mode = (int)num;
  404. break;
  405. case BIO_C_GET_FD:
  406. if (b->init) {
  407. ip = (int *)ptr;
  408. if (ip != NULL)
  409. *ip = b->num;
  410. ret = b->num;
  411. } else
  412. ret = -1;
  413. break;
  414. case BIO_CTRL_GET_CLOSE:
  415. ret = b->shutdown;
  416. break;
  417. case BIO_CTRL_SET_CLOSE:
  418. b->shutdown = (int)num;
  419. break;
  420. case BIO_CTRL_PENDING:
  421. case BIO_CTRL_WPENDING:
  422. ret = 0;
  423. break;
  424. case BIO_CTRL_FLUSH:
  425. break;
  426. case BIO_CTRL_DUP:
  427. {
  428. dbio = (BIO *)ptr;
  429. if (data->param_hostname)
  430. BIO_set_conn_hostname(dbio, data->param_hostname);
  431. if (data->param_service)
  432. BIO_set_conn_port(dbio, data->param_service);
  433. BIO_set_conn_ip_family(dbio, data->connect_family);
  434. BIO_set_conn_mode(dbio, data->connect_mode);
  435. /*
  436. * FIXME: the cast of the function seems unlikely to be a good
  437. * idea
  438. */
  439. (void)BIO_set_info_callback(dbio, data->info_callback);
  440. }
  441. break;
  442. case BIO_CTRL_SET_CALLBACK:
  443. ret = 0; /* use callback ctrl */
  444. break;
  445. case BIO_CTRL_GET_CALLBACK:
  446. {
  447. BIO_info_cb **fptr;
  448. fptr = (BIO_info_cb **)ptr;
  449. *fptr = data->info_callback;
  450. }
  451. break;
  452. default:
  453. ret = 0;
  454. break;
  455. }
  456. return ret;
  457. }
  458. static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  459. {
  460. long ret = 1;
  461. BIO_CONNECT *data;
  462. data = (BIO_CONNECT *)b->ptr;
  463. switch (cmd) {
  464. case BIO_CTRL_SET_CALLBACK:
  465. {
  466. data->info_callback = fp;
  467. }
  468. break;
  469. default:
  470. ret = 0;
  471. break;
  472. }
  473. return ret;
  474. }
  475. static int conn_puts(BIO *bp, const char *str)
  476. {
  477. int n, ret;
  478. n = strlen(str);
  479. ret = conn_write(bp, str, n);
  480. return ret;
  481. }
  482. BIO *BIO_new_connect(const char *str)
  483. {
  484. BIO *ret;
  485. ret = BIO_new(BIO_s_connect());
  486. if (ret == NULL)
  487. return NULL;
  488. if (BIO_set_conn_hostname(ret, str))
  489. return ret;
  490. BIO_free(ret);
  491. return NULL;
  492. }
  493. #endif