bss_acpt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. {
  75. int state;
  76. char *param_addr;
  77. int accept_sock;
  78. int accept_nbio;
  79. char *addr;
  80. int nbio;
  81. /* If 0, it means normal, if 1, do a connect on bind failure,
  82. * and if there is no-one listening, bind with SO_REUSEADDR.
  83. * If 2, always use SO_REUSEADDR. */
  84. int bind_mode;
  85. BIO *bio_chain;
  86. } BIO_ACCEPT;
  87. static int acpt_write(BIO *h, const char *buf, int num);
  88. static int acpt_read(BIO *h, char *buf, int size);
  89. static int acpt_puts(BIO *h, const char *str);
  90. static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  91. static int acpt_new(BIO *h);
  92. static int acpt_free(BIO *data);
  93. static int acpt_state(BIO *b, BIO_ACCEPT *c);
  94. static void acpt_close_socket(BIO *data);
  95. static BIO_ACCEPT *BIO_ACCEPT_new(void );
  96. static void BIO_ACCEPT_free(BIO_ACCEPT *a);
  97. #define ACPT_S_BEFORE 1
  98. #define ACPT_S_GET_ACCEPT_SOCKET 2
  99. #define ACPT_S_OK 3
  100. static BIO_METHOD methods_acceptp=
  101. {
  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) OPENSSL_free(a->param_addr);
  145. if (a->addr != NULL) OPENSSL_free(a->addr);
  146. if (a->bio_chain != NULL) BIO_free(a->bio_chain);
  147. OPENSSL_free(a);
  148. }
  149. static void acpt_close_socket(BIO *bio)
  150. {
  151. BIO_ACCEPT *c;
  152. c=(BIO_ACCEPT *)bio->ptr;
  153. if (c->accept_sock != INVALID_SOCKET)
  154. {
  155. shutdown(c->accept_sock,2);
  156. closesocket(c->accept_sock);
  157. c->accept_sock=INVALID_SOCKET;
  158. bio->num=INVALID_SOCKET;
  159. }
  160. }
  161. static int acpt_free(BIO *a)
  162. {
  163. BIO_ACCEPT *data;
  164. if (a == NULL) return(0);
  165. data=(BIO_ACCEPT *)a->ptr;
  166. if (a->shutdown)
  167. {
  168. acpt_close_socket(a);
  169. BIO_ACCEPT_free(data);
  170. a->ptr=NULL;
  171. a->flags=0;
  172. a->init=0;
  173. }
  174. return(1);
  175. }
  176. static int acpt_state(BIO *b, BIO_ACCEPT *c)
  177. {
  178. BIO *bio=NULL,*dbio;
  179. int s= -1;
  180. int i;
  181. again:
  182. switch (c->state)
  183. {
  184. case ACPT_S_BEFORE:
  185. if (c->param_addr == NULL)
  186. {
  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. {
  195. if (!BIO_socket_nbio(s,1))
  196. {
  197. closesocket(s);
  198. BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
  199. return(-1);
  200. }
  201. }
  202. c->accept_sock=s;
  203. b->num=s;
  204. c->state=ACPT_S_GET_ACCEPT_SOCKET;
  205. return(1);
  206. /* break; */
  207. case ACPT_S_GET_ACCEPT_SOCKET:
  208. if (b->next_bio != NULL)
  209. {
  210. c->state=ACPT_S_OK;
  211. goto again;
  212. }
  213. BIO_clear_retry_flags(b);
  214. b->retry_reason=0;
  215. i=BIO_accept(c->accept_sock,&(c->addr));
  216. /* -2 return means we should retry */
  217. if(i == -2)
  218. {
  219. BIO_set_retry_special(b);
  220. b->retry_reason=BIO_RR_ACCEPT;
  221. return -1;
  222. }
  223. if (i < 0) return(i);
  224. bio=BIO_new_socket(i,BIO_CLOSE);
  225. if (bio == NULL) goto err;
  226. BIO_set_callback(bio,BIO_get_callback(b));
  227. BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
  228. if (c->nbio)
  229. {
  230. if (!BIO_socket_nbio(i,1))
  231. {
  232. BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
  233. goto err;
  234. }
  235. }
  236. /* If the accept BIO has an bio_chain, we dup it and
  237. * put the new socket at the end. */
  238. if (c->bio_chain != NULL)
  239. {
  240. if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
  241. goto err;
  242. if (!BIO_push(dbio,bio)) goto err;
  243. bio=dbio;
  244. }
  245. if (BIO_push(b,bio) == NULL) goto err;
  246. c->state=ACPT_S_OK;
  247. return(1);
  248. err:
  249. if (bio != NULL)
  250. BIO_free(bio);
  251. else if (s >= 0)
  252. closesocket(s);
  253. return(0);
  254. /* break; */
  255. case ACPT_S_OK:
  256. if (b->next_bio == NULL)
  257. {
  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. {
  276. ret=acpt_state(b,data);
  277. if (ret <= 0) 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. {
  291. ret=acpt_state(b,data);
  292. if (ret <= 0) 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. BIO *dbio;
  301. int *ip;
  302. long ret=1;
  303. BIO_ACCEPT *data;
  304. char **pp;
  305. data=(BIO_ACCEPT *)b->ptr;
  306. switch (cmd)
  307. {
  308. case BIO_CTRL_RESET:
  309. ret=0;
  310. data->state=ACPT_S_BEFORE;
  311. acpt_close_socket(b);
  312. b->flags=0;
  313. break;
  314. case BIO_C_DO_STATE_MACHINE:
  315. /* use this one to start the connection */
  316. ret=(long)acpt_state(b,data);
  317. break;
  318. case BIO_C_SET_ACCEPT:
  319. if (ptr != NULL)
  320. {
  321. if (num == 0)
  322. {
  323. b->init=1;
  324. if (data->param_addr != NULL)
  325. OPENSSL_free(data->param_addr);
  326. data->param_addr=BUF_strdup(ptr);
  327. }
  328. else if (num == 1)
  329. {
  330. data->accept_nbio=(ptr != NULL);
  331. }
  332. else if (num == 2)
  333. {
  334. if (data->bio_chain != NULL)
  335. BIO_free(data->bio_chain);
  336. data->bio_chain=(BIO *)ptr;
  337. }
  338. }
  339. break;
  340. case BIO_C_SET_NBIO:
  341. data->nbio=(int)num;
  342. break;
  343. case BIO_C_SET_FD:
  344. b->init=1;
  345. b->num= *((int *)ptr);
  346. data->accept_sock=b->num;
  347. data->state=ACPT_S_GET_ACCEPT_SOCKET;
  348. b->shutdown=(int)num;
  349. b->init=1;
  350. break;
  351. case BIO_C_GET_FD:
  352. if (b->init)
  353. {
  354. ip=(int *)ptr;
  355. if (ip != NULL)
  356. *ip=data->accept_sock;
  357. ret=data->accept_sock;
  358. }
  359. else
  360. ret= -1;
  361. break;
  362. case BIO_C_GET_ACCEPT:
  363. if (b->init)
  364. {
  365. if (ptr != NULL)
  366. {
  367. pp=(char **)ptr;
  368. *pp=data->param_addr;
  369. }
  370. else
  371. ret= -1;
  372. }
  373. else
  374. ret= -1;
  375. break;
  376. case BIO_CTRL_GET_CLOSE:
  377. ret=b->shutdown;
  378. break;
  379. case BIO_CTRL_SET_CLOSE:
  380. b->shutdown=(int)num;
  381. break;
  382. case BIO_CTRL_PENDING:
  383. case BIO_CTRL_WPENDING:
  384. ret=0;
  385. break;
  386. case BIO_CTRL_FLUSH:
  387. break;
  388. case BIO_C_SET_BIND_MODE:
  389. data->bind_mode=(int)num;
  390. break;
  391. case BIO_C_GET_BIND_MODE:
  392. ret=(long)data->bind_mode;
  393. break;
  394. case BIO_CTRL_DUP:
  395. dbio=(BIO *)ptr;
  396. /* if (data->param_port) EAY EAY
  397. BIO_set_port(dbio,data->param_port);
  398. if (data->param_hostname)
  399. BIO_set_hostname(dbio,data->param_hostname);
  400. BIO_set_nbio(dbio,data->nbio); */
  401. break;
  402. default:
  403. ret=0;
  404. break;
  405. }
  406. return(ret);
  407. }
  408. static int acpt_puts(BIO *bp, const char *str)
  409. {
  410. int n,ret;
  411. n=strlen(str);
  412. ret=acpt_write(bp,str,n);
  413. return(ret);
  414. }
  415. BIO *BIO_new_accept(char *str)
  416. {
  417. BIO *ret;
  418. ret=BIO_new(BIO_s_accept());
  419. if (ret == NULL) return(NULL);
  420. if (BIO_set_accept_port(ret,str))
  421. return(ret);
  422. else
  423. {
  424. BIO_free(ret);
  425. return(NULL);
  426. }
  427. }
  428. #endif