bss_conn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. int (*info_callback) (const BIO *bio, int state, int ret);
  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, /* connect_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. int (*cb) (const BIO *, int, int) = 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. return (NULL);
  207. ret->state = BIO_CONN_S_BEFORE;
  208. ret->connect_family = BIO_FAMILY_IPANY;
  209. return (ret);
  210. }
  211. void BIO_CONNECT_free(BIO_CONNECT *a)
  212. {
  213. if (a == NULL)
  214. return;
  215. OPENSSL_free(a->param_hostname);
  216. OPENSSL_free(a->param_service);
  217. BIO_ADDRINFO_free(a->addr_first);
  218. OPENSSL_free(a);
  219. }
  220. const BIO_METHOD *BIO_s_connect(void)
  221. {
  222. return (&methods_connectp);
  223. }
  224. static int conn_new(BIO *bi)
  225. {
  226. bi->init = 0;
  227. bi->num = (int)INVALID_SOCKET;
  228. bi->flags = 0;
  229. if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
  230. return (0);
  231. else
  232. return (1);
  233. }
  234. static void conn_close_socket(BIO *bio)
  235. {
  236. BIO_CONNECT *c;
  237. c = (BIO_CONNECT *)bio->ptr;
  238. if (bio->num != (int)INVALID_SOCKET) {
  239. /* Only do a shutdown if things were established */
  240. if (c->state == BIO_CONN_S_OK)
  241. shutdown(bio->num, 2);
  242. BIO_closesocket(bio->num);
  243. bio->num = (int)INVALID_SOCKET;
  244. }
  245. }
  246. static int conn_free(BIO *a)
  247. {
  248. BIO_CONNECT *data;
  249. if (a == NULL)
  250. return (0);
  251. data = (BIO_CONNECT *)a->ptr;
  252. if (a->shutdown) {
  253. conn_close_socket(a);
  254. BIO_CONNECT_free(data);
  255. a->ptr = NULL;
  256. a->flags = 0;
  257. a->init = 0;
  258. }
  259. return (1);
  260. }
  261. static int conn_read(BIO *b, char *out, int outl)
  262. {
  263. int ret = 0;
  264. BIO_CONNECT *data;
  265. data = (BIO_CONNECT *)b->ptr;
  266. if (data->state != BIO_CONN_S_OK) {
  267. ret = conn_state(b, data);
  268. if (ret <= 0)
  269. return (ret);
  270. }
  271. if (out != NULL) {
  272. clear_socket_error();
  273. ret = readsocket(b->num, out, outl);
  274. BIO_clear_retry_flags(b);
  275. if (ret <= 0) {
  276. if (BIO_sock_should_retry(ret))
  277. BIO_set_retry_read(b);
  278. }
  279. }
  280. return (ret);
  281. }
  282. static int conn_write(BIO *b, const char *in, int inl)
  283. {
  284. int ret;
  285. BIO_CONNECT *data;
  286. data = (BIO_CONNECT *)b->ptr;
  287. if (data->state != BIO_CONN_S_OK) {
  288. ret = conn_state(b, data);
  289. if (ret <= 0)
  290. return (ret);
  291. }
  292. clear_socket_error();
  293. ret = writesocket(b->num, in, inl);
  294. BIO_clear_retry_flags(b);
  295. if (ret <= 0) {
  296. if (BIO_sock_should_retry(ret))
  297. BIO_set_retry_write(b);
  298. }
  299. return (ret);
  300. }
  301. static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
  302. {
  303. BIO *dbio;
  304. int *ip;
  305. const char **pptr = NULL;
  306. long ret = 1;
  307. BIO_CONNECT *data;
  308. data = (BIO_CONNECT *)b->ptr;
  309. switch (cmd) {
  310. case BIO_CTRL_RESET:
  311. ret = 0;
  312. data->state = BIO_CONN_S_BEFORE;
  313. conn_close_socket(b);
  314. BIO_ADDRINFO_free(data->addr_first);
  315. data->addr_first = NULL;
  316. b->flags = 0;
  317. break;
  318. case BIO_C_DO_STATE_MACHINE:
  319. /* use this one to start the connection */
  320. if (data->state != BIO_CONN_S_OK)
  321. ret = (long)conn_state(b, data);
  322. else
  323. ret = 1;
  324. break;
  325. case BIO_C_GET_CONNECT:
  326. if (ptr != NULL) {
  327. pptr = (const char **)ptr;
  328. if (num == 0) {
  329. *pptr = data->param_hostname;
  330. } else if (num == 1) {
  331. *pptr = data->param_service;
  332. } else if (num == 2) {
  333. *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
  334. } else if (num == 3) {
  335. switch (BIO_ADDRINFO_family(data->addr_iter)) {
  336. # ifdef AF_INET6
  337. case AF_INET6:
  338. ret = BIO_FAMILY_IPV6;
  339. break;
  340. # endif
  341. case AF_INET:
  342. ret = BIO_FAMILY_IPV4;
  343. break;
  344. case 0:
  345. ret = data->connect_family;
  346. break;
  347. default:
  348. ret = -1;
  349. break;
  350. }
  351. } else {
  352. ret = 0;
  353. }
  354. } else {
  355. ret = 0;
  356. }
  357. break;
  358. case BIO_C_SET_CONNECT:
  359. if (ptr != NULL) {
  360. b->init = 1;
  361. if (num == 0) {
  362. char *hold_service = data->param_service;
  363. /* We affect the hostname regardless. However, the input
  364. * string might contain a host:service spec, so we must
  365. * parse it, which might or might not affect the service
  366. */
  367. OPENSSL_free(data->param_hostname);
  368. data->param_hostname = NULL;
  369. ret = BIO_parse_hostserv(ptr,
  370. &data->param_hostname,
  371. &data->param_service,
  372. BIO_PARSE_PRIO_HOST);
  373. if (hold_service != data->param_service)
  374. OPENSSL_free(hold_service);
  375. } else if (num == 1) {
  376. OPENSSL_free(data->param_service);
  377. data->param_service = BUF_strdup(ptr);
  378. } else if (num == 2) {
  379. const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
  380. if (ret) {
  381. data->param_hostname = BIO_ADDR_hostname_string(addr, 1);
  382. data->param_service = BIO_ADDR_service_string(addr, 1);
  383. BIO_ADDRINFO_free(data->addr_first);
  384. data->addr_first = NULL;
  385. data->addr_iter = NULL;
  386. }
  387. } else if (num == 3) {
  388. data->connect_family = *(int *)ptr;
  389. } else {
  390. ret = 0;
  391. }
  392. }
  393. break;
  394. case BIO_C_SET_NBIO:
  395. if (num != 0)
  396. data->connect_mode |= BIO_SOCK_NONBLOCK;
  397. else
  398. data->connect_mode &= ~BIO_SOCK_NONBLOCK;
  399. break;
  400. case BIO_C_SET_CONNECT_MODE:
  401. data->connect_mode = (int)num;
  402. break;
  403. case BIO_C_GET_FD:
  404. if (b->init) {
  405. ip = (int *)ptr;
  406. if (ip != NULL)
  407. *ip = b->num;
  408. ret = b->num;
  409. } else
  410. ret = -1;
  411. break;
  412. case BIO_CTRL_GET_CLOSE:
  413. ret = b->shutdown;
  414. break;
  415. case BIO_CTRL_SET_CLOSE:
  416. b->shutdown = (int)num;
  417. break;
  418. case BIO_CTRL_PENDING:
  419. case BIO_CTRL_WPENDING:
  420. ret = 0;
  421. break;
  422. case BIO_CTRL_FLUSH:
  423. break;
  424. case BIO_CTRL_DUP:
  425. {
  426. dbio = (BIO *)ptr;
  427. if (data->param_hostname)
  428. BIO_set_conn_hostname(dbio, data->param_hostname);
  429. if (data->param_service)
  430. BIO_set_conn_port(dbio, data->param_service);
  431. BIO_set_conn_ip_family(dbio, data->connect_family);
  432. BIO_set_conn_mode(dbio, data->connect_mode);
  433. /*
  434. * FIXME: the cast of the function seems unlikely to be a good
  435. * idea
  436. */
  437. (void)BIO_set_info_callback(dbio,
  438. (bio_info_cb *)data->info_callback);
  439. }
  440. break;
  441. case BIO_CTRL_SET_CALLBACK:
  442. ret = 0; /* use callback ctrl */
  443. break;
  444. case BIO_CTRL_GET_CALLBACK:
  445. {
  446. int (**fptr) (const BIO *bio, int state, int xret);
  447. fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
  448. *fptr = data->info_callback;
  449. }
  450. break;
  451. default:
  452. ret = 0;
  453. break;
  454. }
  455. return (ret);
  456. }
  457. static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  458. {
  459. long ret = 1;
  460. BIO_CONNECT *data;
  461. data = (BIO_CONNECT *)b->ptr;
  462. switch (cmd) {
  463. case BIO_CTRL_SET_CALLBACK:
  464. {
  465. data->info_callback =
  466. (int (*)(const struct bio_st *, int, int))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