bss_conn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <errno.h>
  59. #define USE_SOCKETS
  60. #include "internal/cryptlib.h"
  61. #include <openssl/bio.h>
  62. #ifndef OPENSSL_NO_SOCK
  63. # if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  64. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  65. # undef FIONBIO
  66. # endif
  67. typedef struct bio_connect_st {
  68. int state;
  69. char *param_hostname;
  70. char *param_port;
  71. int nbio;
  72. unsigned char ip[4];
  73. unsigned short port;
  74. struct sockaddr_in them;
  75. /*
  76. * int socket; this will be kept in bio->num so that it is compatible
  77. * with the bss_sock bio
  78. */
  79. /*
  80. * called when the connection is initially made callback(BIO,state,ret);
  81. * The callback should return 'ret'. state is for compatibility with the
  82. * ssl info_callback
  83. */
  84. int (*info_callback) (const BIO *bio, int state, int ret);
  85. } BIO_CONNECT;
  86. static int conn_write(BIO *h, const char *buf, int num);
  87. static int conn_read(BIO *h, char *buf, int size);
  88. static int conn_puts(BIO *h, const char *str);
  89. static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  90. static int conn_new(BIO *h);
  91. static int conn_free(BIO *data);
  92. static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
  93. static int conn_state(BIO *b, BIO_CONNECT *c);
  94. static void conn_close_socket(BIO *data);
  95. BIO_CONNECT *BIO_CONNECT_new(void);
  96. void BIO_CONNECT_free(BIO_CONNECT *a);
  97. static BIO_METHOD methods_connectp = {
  98. BIO_TYPE_CONNECT,
  99. "socket connect",
  100. conn_write,
  101. conn_read,
  102. conn_puts,
  103. NULL, /* connect_gets, */
  104. conn_ctrl,
  105. conn_new,
  106. conn_free,
  107. conn_callback_ctrl,
  108. };
  109. static int conn_state(BIO *b, BIO_CONNECT *c)
  110. {
  111. int ret = -1, i;
  112. unsigned long l;
  113. char *p, *q;
  114. int (*cb) (const BIO *, int, int) = NULL;
  115. if (c->info_callback != NULL)
  116. cb = c->info_callback;
  117. for (;;) {
  118. switch (c->state) {
  119. case BIO_CONN_S_BEFORE:
  120. p = c->param_hostname;
  121. if (p == NULL) {
  122. BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_SPECIFIED);
  123. goto exit_loop;
  124. }
  125. for (; *p != '\0'; p++) {
  126. if ((*p == ':') || (*p == '/'))
  127. break;
  128. }
  129. i = *p;
  130. if ((i == ':') || (i == '/')) {
  131. *(p++) = '\0';
  132. if (i == ':') {
  133. for (q = p; *q; q++)
  134. if (*q == '/') {
  135. *q = '\0';
  136. break;
  137. }
  138. OPENSSL_free(c->param_port);
  139. c->param_port = OPENSSL_strdup(p);
  140. }
  141. }
  142. if (c->param_port == NULL) {
  143. BIOerr(BIO_F_CONN_STATE, BIO_R_NO_PORT_SPECIFIED);
  144. ERR_add_error_data(2, "host=", c->param_hostname);
  145. goto exit_loop;
  146. }
  147. c->state = BIO_CONN_S_GET_IP;
  148. break;
  149. case BIO_CONN_S_GET_IP:
  150. if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0)
  151. goto exit_loop;
  152. c->state = BIO_CONN_S_GET_PORT;
  153. break;
  154. case BIO_CONN_S_GET_PORT:
  155. if (c->param_port == NULL) {
  156. /* abort(); */
  157. goto exit_loop;
  158. } else if (BIO_get_port(c->param_port, &c->port) <= 0)
  159. goto exit_loop;
  160. c->state = BIO_CONN_S_CREATE_SOCKET;
  161. break;
  162. case BIO_CONN_S_CREATE_SOCKET:
  163. /* now setup address */
  164. memset(&c->them, 0, sizeof(c->them));
  165. c->them.sin_family = AF_INET;
  166. c->them.sin_port = htons((unsigned short)c->port);
  167. l = (unsigned long)
  168. ((unsigned long)c->ip[0] << 24L) |
  169. ((unsigned long)c->ip[1] << 16L) |
  170. ((unsigned long)c->ip[2] << 8L) | ((unsigned long)c->ip[3]);
  171. c->them.sin_addr.s_addr = htonl(l);
  172. c->state = BIO_CONN_S_CREATE_SOCKET;
  173. ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  174. if (ret == (int)INVALID_SOCKET) {
  175. SYSerr(SYS_F_SOCKET, get_last_socket_error());
  176. ERR_add_error_data(4, "host=", c->param_hostname,
  177. ":", c->param_port);
  178. BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
  179. goto exit_loop;
  180. }
  181. b->num = ret;
  182. c->state = BIO_CONN_S_NBIO;
  183. break;
  184. case BIO_CONN_S_NBIO:
  185. if (c->nbio) {
  186. if (!BIO_socket_nbio(b->num, 1)) {
  187. BIOerr(BIO_F_CONN_STATE, BIO_R_ERROR_SETTING_NBIO);
  188. ERR_add_error_data(4, "host=",
  189. c->param_hostname, ":", c->param_port);
  190. goto exit_loop;
  191. }
  192. }
  193. c->state = BIO_CONN_S_CONNECT;
  194. # if defined(SO_KEEPALIVE)
  195. i = 1;
  196. i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
  197. sizeof(i));
  198. if (i < 0) {
  199. SYSerr(SYS_F_SOCKET, get_last_socket_error());
  200. ERR_add_error_data(4, "host=", c->param_hostname,
  201. ":", c->param_port);
  202. BIOerr(BIO_F_CONN_STATE, BIO_R_KEEPALIVE);
  203. goto exit_loop;
  204. }
  205. # endif
  206. break;
  207. case BIO_CONN_S_CONNECT:
  208. BIO_clear_retry_flags(b);
  209. ret = connect(b->num,
  210. (struct sockaddr *)&c->them, sizeof(c->them));
  211. b->retry_reason = 0;
  212. if (ret < 0) {
  213. if (BIO_sock_should_retry(ret)) {
  214. BIO_set_retry_special(b);
  215. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  216. b->retry_reason = BIO_RR_CONNECT;
  217. } else {
  218. SYSerr(SYS_F_CONNECT, get_last_socket_error());
  219. ERR_add_error_data(4, "host=",
  220. c->param_hostname, ":", c->param_port);
  221. BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
  222. }
  223. goto exit_loop;
  224. } else
  225. c->state = BIO_CONN_S_OK;
  226. break;
  227. case BIO_CONN_S_BLOCKED_CONNECT:
  228. i = BIO_sock_error(b->num);
  229. if (i) {
  230. BIO_clear_retry_flags(b);
  231. SYSerr(SYS_F_CONNECT, i);
  232. ERR_add_error_data(4, "host=",
  233. c->param_hostname, ":", c->param_port);
  234. BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
  235. ret = 0;
  236. goto exit_loop;
  237. } else
  238. c->state = BIO_CONN_S_OK;
  239. break;
  240. case BIO_CONN_S_OK:
  241. ret = 1;
  242. goto exit_loop;
  243. default:
  244. /* abort(); */
  245. goto exit_loop;
  246. }
  247. if (cb != NULL) {
  248. if ((ret = cb((BIO *)b, c->state, ret)) == 0)
  249. goto end;
  250. }
  251. }
  252. /* Loop does not exit */
  253. exit_loop:
  254. if (cb != NULL)
  255. ret = cb((BIO *)b, c->state, ret);
  256. end:
  257. return (ret);
  258. }
  259. BIO_CONNECT *BIO_CONNECT_new(void)
  260. {
  261. BIO_CONNECT *ret;
  262. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  263. return (NULL);
  264. ret->state = BIO_CONN_S_BEFORE;
  265. ret->param_hostname = NULL;
  266. ret->param_port = NULL;
  267. ret->info_callback = NULL;
  268. return (ret);
  269. }
  270. void BIO_CONNECT_free(BIO_CONNECT *a)
  271. {
  272. if (a == NULL)
  273. return;
  274. OPENSSL_free(a->param_hostname);
  275. OPENSSL_free(a->param_port);
  276. OPENSSL_free(a);
  277. }
  278. BIO_METHOD *BIO_s_connect(void)
  279. {
  280. return (&methods_connectp);
  281. }
  282. static int conn_new(BIO *bi)
  283. {
  284. bi->init = 0;
  285. bi->num = (int)INVALID_SOCKET;
  286. bi->flags = 0;
  287. if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
  288. return (0);
  289. else
  290. return (1);
  291. }
  292. static void conn_close_socket(BIO *bio)
  293. {
  294. BIO_CONNECT *c;
  295. c = (BIO_CONNECT *)bio->ptr;
  296. if (bio->num != (int)INVALID_SOCKET) {
  297. /* Only do a shutdown if things were established */
  298. if (c->state == BIO_CONN_S_OK)
  299. shutdown(bio->num, 2);
  300. closesocket(bio->num);
  301. bio->num = (int)INVALID_SOCKET;
  302. }
  303. }
  304. static int conn_free(BIO *a)
  305. {
  306. BIO_CONNECT *data;
  307. if (a == NULL)
  308. return (0);
  309. data = (BIO_CONNECT *)a->ptr;
  310. if (a->shutdown) {
  311. conn_close_socket(a);
  312. BIO_CONNECT_free(data);
  313. a->ptr = NULL;
  314. a->flags = 0;
  315. a->init = 0;
  316. }
  317. return (1);
  318. }
  319. static int conn_read(BIO *b, char *out, int outl)
  320. {
  321. int ret = 0;
  322. BIO_CONNECT *data;
  323. data = (BIO_CONNECT *)b->ptr;
  324. if (data->state != BIO_CONN_S_OK) {
  325. ret = conn_state(b, data);
  326. if (ret <= 0)
  327. return (ret);
  328. }
  329. if (out != NULL) {
  330. clear_socket_error();
  331. ret = readsocket(b->num, out, outl);
  332. BIO_clear_retry_flags(b);
  333. if (ret <= 0) {
  334. if (BIO_sock_should_retry(ret))
  335. BIO_set_retry_read(b);
  336. }
  337. }
  338. return (ret);
  339. }
  340. static int conn_write(BIO *b, const char *in, int inl)
  341. {
  342. int ret;
  343. BIO_CONNECT *data;
  344. data = (BIO_CONNECT *)b->ptr;
  345. if (data->state != BIO_CONN_S_OK) {
  346. ret = conn_state(b, data);
  347. if (ret <= 0)
  348. return (ret);
  349. }
  350. clear_socket_error();
  351. ret = writesocket(b->num, in, inl);
  352. BIO_clear_retry_flags(b);
  353. if (ret <= 0) {
  354. if (BIO_sock_should_retry(ret))
  355. BIO_set_retry_write(b);
  356. }
  357. return (ret);
  358. }
  359. static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
  360. {
  361. BIO *dbio;
  362. int *ip;
  363. const char **pptr = NULL;
  364. long ret = 1;
  365. BIO_CONNECT *data;
  366. data = (BIO_CONNECT *)b->ptr;
  367. switch (cmd) {
  368. case BIO_CTRL_RESET:
  369. ret = 0;
  370. data->state = BIO_CONN_S_BEFORE;
  371. conn_close_socket(b);
  372. b->flags = 0;
  373. break;
  374. case BIO_C_DO_STATE_MACHINE:
  375. /* use this one to start the connection */
  376. if (data->state != BIO_CONN_S_OK)
  377. ret = (long)conn_state(b, data);
  378. else
  379. ret = 1;
  380. break;
  381. case BIO_C_GET_CONNECT:
  382. if (ptr != NULL) {
  383. pptr = (const char **)ptr;
  384. }
  385. if (b->init) {
  386. if (pptr != NULL) {
  387. ret = 1;
  388. if (num == 0) {
  389. *pptr = data->param_hostname;
  390. } else if (num == 1) {
  391. *pptr = data->param_port;
  392. } else if (num == 2) {
  393. *pptr = (char *)&(data->ip[0]);
  394. } else {
  395. ret = 0;
  396. }
  397. }
  398. if (num == 3) {
  399. ret = data->port;
  400. }
  401. } else {
  402. if (pptr != NULL)
  403. *pptr = "not initialized";
  404. ret = 0;
  405. }
  406. break;
  407. case BIO_C_SET_CONNECT:
  408. if (ptr != NULL) {
  409. b->init = 1;
  410. if (num == 0) {
  411. OPENSSL_free(data->param_hostname);
  412. data->param_hostname = OPENSSL_strdup(ptr);
  413. } else if (num == 1) {
  414. OPENSSL_free(data->param_port);
  415. data->param_port = OPENSSL_strdup(ptr);
  416. } else if (num == 2) {
  417. char buf[16];
  418. unsigned char *p = ptr;
  419. BIO_snprintf(buf, sizeof buf, "%d.%d.%d.%d",
  420. p[0], p[1], p[2], p[3]);
  421. OPENSSL_free(data->param_hostname);
  422. data->param_hostname = OPENSSL_strdup(buf);
  423. memcpy(&(data->ip[0]), ptr, 4);
  424. } else if (num == 3) {
  425. char buf[DECIMAL_SIZE(int) + 1];
  426. BIO_snprintf(buf, sizeof buf, "%d", *(int *)ptr);
  427. OPENSSL_free(data->param_port);
  428. data->param_port = OPENSSL_strdup(buf);
  429. data->port = *(int *)ptr;
  430. }
  431. }
  432. break;
  433. case BIO_C_SET_NBIO:
  434. data->nbio = (int)num;
  435. break;
  436. case BIO_C_GET_FD:
  437. if (b->init) {
  438. ip = (int *)ptr;
  439. if (ip != NULL)
  440. *ip = b->num;
  441. ret = b->num;
  442. } else
  443. ret = -1;
  444. break;
  445. case BIO_CTRL_GET_CLOSE:
  446. ret = b->shutdown;
  447. break;
  448. case BIO_CTRL_SET_CLOSE:
  449. b->shutdown = (int)num;
  450. break;
  451. case BIO_CTRL_PENDING:
  452. case BIO_CTRL_WPENDING:
  453. ret = 0;
  454. break;
  455. case BIO_CTRL_FLUSH:
  456. break;
  457. case BIO_CTRL_DUP:
  458. {
  459. dbio = (BIO *)ptr;
  460. if (data->param_port)
  461. BIO_set_conn_port(dbio, data->param_port);
  462. if (data->param_hostname)
  463. BIO_set_conn_hostname(dbio, data->param_hostname);
  464. BIO_set_nbio(dbio, data->nbio);
  465. /*
  466. * FIXME: the cast of the function seems unlikely to be a good
  467. * idea
  468. */
  469. (void)BIO_set_info_callback(dbio,
  470. (bio_info_cb *)data->info_callback);
  471. }
  472. break;
  473. case BIO_CTRL_SET_CALLBACK:
  474. {
  475. # if 0 /* FIXME: Should this be used? -- Richard
  476. * Levitte */
  477. BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  478. ret = -1;
  479. # else
  480. ret = 0;
  481. # endif
  482. }
  483. break;
  484. case BIO_CTRL_GET_CALLBACK:
  485. {
  486. int (**fptr) (const BIO *bio, int state, int xret);
  487. fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
  488. *fptr = data->info_callback;
  489. }
  490. break;
  491. default:
  492. ret = 0;
  493. break;
  494. }
  495. return (ret);
  496. }
  497. static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  498. {
  499. long ret = 1;
  500. BIO_CONNECT *data;
  501. data = (BIO_CONNECT *)b->ptr;
  502. switch (cmd) {
  503. case BIO_CTRL_SET_CALLBACK:
  504. {
  505. data->info_callback =
  506. (int (*)(const struct bio_st *, int, int))fp;
  507. }
  508. break;
  509. default:
  510. ret = 0;
  511. break;
  512. }
  513. return (ret);
  514. }
  515. static int conn_puts(BIO *bp, const char *str)
  516. {
  517. int n, ret;
  518. n = strlen(str);
  519. ret = conn_write(bp, str, n);
  520. return (ret);
  521. }
  522. BIO *BIO_new_connect(const char *str)
  523. {
  524. BIO *ret;
  525. ret = BIO_new(BIO_s_connect());
  526. if (ret == NULL)
  527. return (NULL);
  528. if (BIO_set_conn_hostname(ret, str))
  529. return (ret);
  530. BIO_free(ret);
  531. return (NULL);
  532. }
  533. #endif