bss_acpt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* crypto/bio/bss_acpt.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #define USE_SOCKETS
  61. #include "cryptlib.h"
  62. #include <openssl/bio.h>
  63. #ifndef OPENSSL_NO_SOCK
  64. # ifdef OPENSSL_SYS_WIN16
  65. # define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
  66. # else
  67. # define SOCKET_PROTOCOL IPPROTO_TCP
  68. # endif
  69. # if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
  70. /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
  71. # undef FIONBIO
  72. # endif
  73. typedef struct bio_accept_st {
  74. int state;
  75. char *param_addr;
  76. int accept_sock;
  77. int accept_nbio;
  78. char *addr;
  79. int nbio;
  80. /*
  81. * If 0, it means normal, if 1, do a connect on bind failure, and if
  82. * there is no-one listening, bind with SO_REUSEADDR. If 2, always use
  83. * SO_REUSEADDR.
  84. */
  85. int bind_mode;
  86. BIO *bio_chain;
  87. } BIO_ACCEPT;
  88. static int acpt_write(BIO *h, const char *buf, int num);
  89. static int acpt_read(BIO *h, char *buf, int size);
  90. static int acpt_puts(BIO *h, const char *str);
  91. static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  92. static int acpt_new(BIO *h);
  93. static int acpt_free(BIO *data);
  94. static int acpt_state(BIO *b, BIO_ACCEPT *c);
  95. static void acpt_close_socket(BIO *data);
  96. static BIO_ACCEPT *BIO_ACCEPT_new(void);
  97. static void BIO_ACCEPT_free(BIO_ACCEPT *a);
  98. # define ACPT_S_BEFORE 1
  99. # define ACPT_S_GET_ACCEPT_SOCKET 2
  100. # define ACPT_S_OK 3
  101. static BIO_METHOD methods_acceptp = {
  102. BIO_TYPE_ACCEPT,
  103. "socket accept",
  104. acpt_write,
  105. acpt_read,
  106. acpt_puts,
  107. NULL, /* connect_gets, */
  108. acpt_ctrl,
  109. acpt_new,
  110. acpt_free,
  111. NULL,
  112. };
  113. BIO_METHOD *BIO_s_accept(void)
  114. {
  115. return (&methods_acceptp);
  116. }
  117. static int acpt_new(BIO *bi)
  118. {
  119. BIO_ACCEPT *ba;
  120. bi->init = 0;
  121. bi->num = INVALID_SOCKET;
  122. bi->flags = 0;
  123. if ((ba = BIO_ACCEPT_new()) == NULL)
  124. return (0);
  125. bi->ptr = (char *)ba;
  126. ba->state = ACPT_S_BEFORE;
  127. bi->shutdown = 1;
  128. return (1);
  129. }
  130. static BIO_ACCEPT *BIO_ACCEPT_new(void)
  131. {
  132. BIO_ACCEPT *ret;
  133. if ((ret = (BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
  134. return (NULL);
  135. memset(ret, 0, sizeof(BIO_ACCEPT));
  136. ret->accept_sock = INVALID_SOCKET;
  137. ret->bind_mode = BIO_BIND_NORMAL;
  138. return (ret);
  139. }
  140. static void BIO_ACCEPT_free(BIO_ACCEPT *a)
  141. {
  142. if (a == NULL)
  143. return;
  144. if (a->param_addr != NULL)
  145. OPENSSL_free(a->param_addr);
  146. if (a->addr != NULL)
  147. OPENSSL_free(a->addr);
  148. if (a->bio_chain != NULL)
  149. BIO_free(a->bio_chain);
  150. OPENSSL_free(a);
  151. }
  152. static void acpt_close_socket(BIO *bio)
  153. {
  154. BIO_ACCEPT *c;
  155. c = (BIO_ACCEPT *)bio->ptr;
  156. if (c->accept_sock != INVALID_SOCKET) {
  157. shutdown(c->accept_sock, 2);
  158. closesocket(c->accept_sock);
  159. c->accept_sock = INVALID_SOCKET;
  160. bio->num = INVALID_SOCKET;
  161. }
  162. }
  163. static int acpt_free(BIO *a)
  164. {
  165. BIO_ACCEPT *data;
  166. if (a == NULL)
  167. return (0);
  168. data = (BIO_ACCEPT *)a->ptr;
  169. if (a->shutdown) {
  170. acpt_close_socket(a);
  171. BIO_ACCEPT_free(data);
  172. a->ptr = NULL;
  173. a->flags = 0;
  174. a->init = 0;
  175. }
  176. return (1);
  177. }
  178. static int acpt_state(BIO *b, BIO_ACCEPT *c)
  179. {
  180. BIO *bio = NULL, *dbio;
  181. int s = -1;
  182. int i;
  183. again:
  184. switch (c->state) {
  185. case ACPT_S_BEFORE:
  186. if (c->param_addr == NULL) {
  187. BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_PORT_SPECIFIED);
  188. return (-1);
  189. }
  190. s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
  191. if (s == INVALID_SOCKET)
  192. return (-1);
  193. if (c->accept_nbio) {
  194. if (!BIO_socket_nbio(s, 1)) {
  195. closesocket(s);
  196. BIOerr(BIO_F_ACPT_STATE,
  197. BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
  198. return (-1);
  199. }
  200. }
  201. c->accept_sock = s;
  202. b->num = s;
  203. c->state = ACPT_S_GET_ACCEPT_SOCKET;
  204. return (1);
  205. /* break; */
  206. case ACPT_S_GET_ACCEPT_SOCKET:
  207. if (b->next_bio != NULL) {
  208. c->state = ACPT_S_OK;
  209. goto again;
  210. }
  211. BIO_clear_retry_flags(b);
  212. b->retry_reason = 0;
  213. i = BIO_accept(c->accept_sock, &(c->addr));
  214. /* -2 return means we should retry */
  215. if (i == -2) {
  216. BIO_set_retry_special(b);
  217. b->retry_reason = BIO_RR_ACCEPT;
  218. return -1;
  219. }
  220. if (i < 0)
  221. return (i);
  222. bio = BIO_new_socket(i, BIO_CLOSE);
  223. if (bio == NULL)
  224. goto err;
  225. BIO_set_callback(bio, BIO_get_callback(b));
  226. BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
  227. if (c->nbio) {
  228. if (!BIO_socket_nbio(i, 1)) {
  229. BIOerr(BIO_F_ACPT_STATE,
  230. BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
  231. goto err;
  232. }
  233. }
  234. /*
  235. * If the accept BIO has an bio_chain, we dup it and put the new
  236. * socket at the end.
  237. */
  238. if (c->bio_chain != NULL) {
  239. if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
  240. goto err;
  241. if (!BIO_push(dbio, bio))
  242. goto err;
  243. bio = dbio;
  244. }
  245. if (BIO_push(b, bio) == NULL)
  246. goto err;
  247. c->state = ACPT_S_OK;
  248. return (1);
  249. err:
  250. if (bio != NULL)
  251. BIO_free(bio);
  252. else if (s >= 0)
  253. closesocket(s);
  254. return (0);
  255. /* break; */
  256. case ACPT_S_OK:
  257. if (b->next_bio == NULL) {
  258. c->state = ACPT_S_GET_ACCEPT_SOCKET;
  259. goto again;
  260. }
  261. return (1);
  262. /* break; */
  263. default:
  264. return (0);
  265. /* break; */
  266. }
  267. }
  268. static int acpt_read(BIO *b, char *out, int outl)
  269. {
  270. int ret = 0;
  271. BIO_ACCEPT *data;
  272. BIO_clear_retry_flags(b);
  273. data = (BIO_ACCEPT *)b->ptr;
  274. while (b->next_bio == NULL) {
  275. ret = acpt_state(b, data);
  276. if (ret <= 0)
  277. return (ret);
  278. }
  279. ret = BIO_read(b->next_bio, out, outl);
  280. BIO_copy_next_retry(b);
  281. return (ret);
  282. }
  283. static int acpt_write(BIO *b, const char *in, int inl)
  284. {
  285. int ret;
  286. BIO_ACCEPT *data;
  287. BIO_clear_retry_flags(b);
  288. data = (BIO_ACCEPT *)b->ptr;
  289. while (b->next_bio == NULL) {
  290. ret = acpt_state(b, data);
  291. if (ret <= 0)
  292. return (ret);
  293. }
  294. ret = BIO_write(b->next_bio, in, inl);
  295. BIO_copy_next_retry(b);
  296. return (ret);
  297. }
  298. static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
  299. {
  300. int *ip;
  301. long ret = 1;
  302. BIO_ACCEPT *data;
  303. char **pp;
  304. data = (BIO_ACCEPT *)b->ptr;
  305. switch (cmd) {
  306. case BIO_CTRL_RESET:
  307. ret = 0;
  308. data->state = ACPT_S_BEFORE;
  309. acpt_close_socket(b);
  310. b->flags = 0;
  311. break;
  312. case BIO_C_DO_STATE_MACHINE:
  313. /* use this one to start the connection */
  314. ret = (long)acpt_state(b, data);
  315. break;
  316. case BIO_C_SET_ACCEPT:
  317. if (ptr != NULL) {
  318. if (num == 0) {
  319. b->init = 1;
  320. if (data->param_addr != NULL)
  321. OPENSSL_free(data->param_addr);
  322. data->param_addr = BUF_strdup(ptr);
  323. } else if (num == 1) {
  324. data->accept_nbio = (ptr != NULL);
  325. } else if (num == 2) {
  326. if (data->bio_chain != NULL)
  327. BIO_free(data->bio_chain);
  328. data->bio_chain = (BIO *)ptr;
  329. }
  330. }
  331. break;
  332. case BIO_C_SET_NBIO:
  333. data->nbio = (int)num;
  334. break;
  335. case BIO_C_SET_FD:
  336. b->init = 1;
  337. b->num = *((int *)ptr);
  338. data->accept_sock = b->num;
  339. data->state = ACPT_S_GET_ACCEPT_SOCKET;
  340. b->shutdown = (int)num;
  341. b->init = 1;
  342. break;
  343. case BIO_C_GET_FD:
  344. if (b->init) {
  345. ip = (int *)ptr;
  346. if (ip != NULL)
  347. *ip = data->accept_sock;
  348. ret = data->accept_sock;
  349. } else
  350. ret = -1;
  351. break;
  352. case BIO_C_GET_ACCEPT:
  353. if (b->init) {
  354. if (ptr != NULL) {
  355. pp = (char **)ptr;
  356. *pp = data->param_addr;
  357. } else
  358. ret = -1;
  359. } else
  360. ret = -1;
  361. break;
  362. case BIO_CTRL_GET_CLOSE:
  363. ret = b->shutdown;
  364. break;
  365. case BIO_CTRL_SET_CLOSE:
  366. b->shutdown = (int)num;
  367. break;
  368. case BIO_CTRL_PENDING:
  369. case BIO_CTRL_WPENDING:
  370. ret = 0;
  371. break;
  372. case BIO_CTRL_FLUSH:
  373. break;
  374. case BIO_C_SET_BIND_MODE:
  375. data->bind_mode = (int)num;
  376. break;
  377. case BIO_C_GET_BIND_MODE:
  378. ret = (long)data->bind_mode;
  379. break;
  380. case BIO_CTRL_DUP:
  381. /*- dbio=(BIO *)ptr;
  382. if (data->param_port) EAY EAY
  383. BIO_set_port(dbio,data->param_port);
  384. if (data->param_hostname)
  385. BIO_set_hostname(dbio,data->param_hostname);
  386. BIO_set_nbio(dbio,data->nbio); */
  387. break;
  388. default:
  389. ret = 0;
  390. break;
  391. }
  392. return (ret);
  393. }
  394. static int acpt_puts(BIO *bp, const char *str)
  395. {
  396. int n, ret;
  397. n = strlen(str);
  398. ret = acpt_write(bp, str, n);
  399. return (ret);
  400. }
  401. BIO *BIO_new_accept(const char *str)
  402. {
  403. BIO *ret;
  404. ret = BIO_new(BIO_s_accept());
  405. if (ret == NULL)
  406. return (NULL);
  407. if (BIO_set_accept_port(ret, str))
  408. return (ret);
  409. else {
  410. BIO_free(ret);
  411. return (NULL);
  412. }
  413. }
  414. #endif