bss_conn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /* crypto/bio/bss_conn.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_connect_st
  74. {
  75. int state;
  76. char *param_hostname;
  77. char *param_port;
  78. int nbio;
  79. unsigned char ip[4];
  80. unsigned short port;
  81. struct sockaddr_in them;
  82. /* int socket; this will be kept in bio->num so that it is
  83. * compatible with the bss_sock bio */
  84. /* called when the connection is initially made
  85. * callback(BIO,state,ret); The callback should return
  86. * 'ret'. state is for compatibility with the ssl info_callback */
  87. int (*info_callback)(const BIO *bio,int state,int ret);
  88. } BIO_CONNECT;
  89. static int conn_write(BIO *h, const char *buf, int num);
  90. static int conn_read(BIO *h, char *buf, int size);
  91. static int conn_puts(BIO *h, const char *str);
  92. static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  93. static int conn_new(BIO *h);
  94. static int conn_free(BIO *data);
  95. static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
  96. static int conn_state(BIO *b, BIO_CONNECT *c);
  97. static void conn_close_socket(BIO *data);
  98. BIO_CONNECT *BIO_CONNECT_new(void );
  99. void BIO_CONNECT_free(BIO_CONNECT *a);
  100. static BIO_METHOD methods_connectp=
  101. {
  102. BIO_TYPE_CONNECT,
  103. "socket connect",
  104. conn_write,
  105. conn_read,
  106. conn_puts,
  107. NULL, /* connect_gets, */
  108. conn_ctrl,
  109. conn_new,
  110. conn_free,
  111. conn_callback_ctrl,
  112. };
  113. static int conn_state(BIO *b, BIO_CONNECT *c)
  114. {
  115. int ret= -1,i;
  116. unsigned long l;
  117. char *p,*q;
  118. int (*cb)(const BIO *,int,int)=NULL;
  119. if (c->info_callback != NULL)
  120. cb=c->info_callback;
  121. for (;;)
  122. {
  123. switch (c->state)
  124. {
  125. case BIO_CONN_S_BEFORE:
  126. p=c->param_hostname;
  127. if (p == NULL)
  128. {
  129. BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
  130. goto exit_loop;
  131. }
  132. for ( ; *p != '\0'; p++)
  133. {
  134. if ((*p == ':') || (*p == '/')) break;
  135. }
  136. i= *p;
  137. if ((i == ':') || (i == '/'))
  138. {
  139. *(p++)='\0';
  140. if (i == ':')
  141. {
  142. for (q=p; *q; q++)
  143. if (*q == '/')
  144. {
  145. *q='\0';
  146. break;
  147. }
  148. if (c->param_port != NULL)
  149. OPENSSL_free(c->param_port);
  150. c->param_port=BUF_strdup(p);
  151. }
  152. }
  153. if (c->param_port == NULL)
  154. {
  155. BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
  156. ERR_add_error_data(2,"host=",c->param_hostname);
  157. goto exit_loop;
  158. }
  159. c->state=BIO_CONN_S_GET_IP;
  160. break;
  161. case BIO_CONN_S_GET_IP:
  162. if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
  163. goto exit_loop;
  164. c->state=BIO_CONN_S_GET_PORT;
  165. break;
  166. case BIO_CONN_S_GET_PORT:
  167. if (c->param_port == NULL)
  168. {
  169. /* abort(); */
  170. goto exit_loop;
  171. }
  172. else if (BIO_get_port(c->param_port,&c->port) <= 0)
  173. goto exit_loop;
  174. c->state=BIO_CONN_S_CREATE_SOCKET;
  175. break;
  176. case BIO_CONN_S_CREATE_SOCKET:
  177. /* now setup address */
  178. memset((char *)&c->them,0,sizeof(c->them));
  179. c->them.sin_family=AF_INET;
  180. c->them.sin_port=htons((unsigned short)c->port);
  181. l=(unsigned long)
  182. ((unsigned long)c->ip[0]<<24L)|
  183. ((unsigned long)c->ip[1]<<16L)|
  184. ((unsigned long)c->ip[2]<< 8L)|
  185. ((unsigned long)c->ip[3]);
  186. c->them.sin_addr.s_addr=htonl(l);
  187. c->state=BIO_CONN_S_CREATE_SOCKET;
  188. ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
  189. if (ret == INVALID_SOCKET)
  190. {
  191. SYSerr(SYS_F_SOCKET,get_last_socket_error());
  192. ERR_add_error_data(4,"host=",c->param_hostname,
  193. ":",c->param_port);
  194. BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
  195. goto exit_loop;
  196. }
  197. b->num=ret;
  198. c->state=BIO_CONN_S_NBIO;
  199. break;
  200. case BIO_CONN_S_NBIO:
  201. if (c->nbio)
  202. {
  203. if (!BIO_socket_nbio(b->num,1))
  204. {
  205. BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
  206. ERR_add_error_data(4,"host=",
  207. c->param_hostname,
  208. ":",c->param_port);
  209. goto exit_loop;
  210. }
  211. }
  212. c->state=BIO_CONN_S_CONNECT;
  213. #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
  214. i=1;
  215. i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
  216. if (i < 0)
  217. {
  218. SYSerr(SYS_F_SOCKET,get_last_socket_error());
  219. ERR_add_error_data(4,"host=",c->param_hostname,
  220. ":",c->param_port);
  221. BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
  222. goto exit_loop;
  223. }
  224. #endif
  225. break;
  226. case BIO_CONN_S_CONNECT:
  227. BIO_clear_retry_flags(b);
  228. ret=connect(b->num,
  229. (struct sockaddr *)&c->them,
  230. sizeof(c->them));
  231. b->retry_reason=0;
  232. if (ret < 0)
  233. {
  234. if (BIO_sock_should_retry(ret))
  235. {
  236. BIO_set_retry_special(b);
  237. c->state=BIO_CONN_S_BLOCKED_CONNECT;
  238. b->retry_reason=BIO_RR_CONNECT;
  239. }
  240. else
  241. {
  242. SYSerr(SYS_F_CONNECT,get_last_socket_error());
  243. ERR_add_error_data(4,"host=",
  244. c->param_hostname,
  245. ":",c->param_port);
  246. BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
  247. }
  248. goto exit_loop;
  249. }
  250. else
  251. c->state=BIO_CONN_S_OK;
  252. break;
  253. case BIO_CONN_S_BLOCKED_CONNECT:
  254. i=BIO_sock_error(b->num);
  255. if (i)
  256. {
  257. BIO_clear_retry_flags(b);
  258. SYSerr(SYS_F_CONNECT,i);
  259. ERR_add_error_data(4,"host=",
  260. c->param_hostname,
  261. ":",c->param_port);
  262. BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
  263. ret=0;
  264. goto exit_loop;
  265. }
  266. else
  267. c->state=BIO_CONN_S_OK;
  268. break;
  269. case BIO_CONN_S_OK:
  270. ret=1;
  271. goto exit_loop;
  272. default:
  273. /* abort(); */
  274. goto exit_loop;
  275. }
  276. if (cb != NULL)
  277. {
  278. if (!(ret=cb((BIO *)b,c->state,ret)))
  279. goto end;
  280. }
  281. }
  282. /* Loop does not exit */
  283. exit_loop:
  284. if (cb != NULL)
  285. ret=cb((BIO *)b,c->state,ret);
  286. end:
  287. return(ret);
  288. }
  289. BIO_CONNECT *BIO_CONNECT_new(void)
  290. {
  291. BIO_CONNECT *ret;
  292. if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
  293. return(NULL);
  294. ret->state=BIO_CONN_S_BEFORE;
  295. ret->param_hostname=NULL;
  296. ret->param_port=NULL;
  297. ret->info_callback=NULL;
  298. ret->nbio=0;
  299. ret->ip[0]=0;
  300. ret->ip[1]=0;
  301. ret->ip[2]=0;
  302. ret->ip[3]=0;
  303. ret->port=0;
  304. memset((char *)&ret->them,0,sizeof(ret->them));
  305. return(ret);
  306. }
  307. void BIO_CONNECT_free(BIO_CONNECT *a)
  308. {
  309. if(a == NULL)
  310. return;
  311. if (a->param_hostname != NULL)
  312. OPENSSL_free(a->param_hostname);
  313. if (a->param_port != NULL)
  314. OPENSSL_free(a->param_port);
  315. OPENSSL_free(a);
  316. }
  317. BIO_METHOD *BIO_s_connect(void)
  318. {
  319. return(&methods_connectp);
  320. }
  321. static int conn_new(BIO *bi)
  322. {
  323. bi->init=0;
  324. bi->num=INVALID_SOCKET;
  325. bi->flags=0;
  326. if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
  327. return(0);
  328. else
  329. return(1);
  330. }
  331. static void conn_close_socket(BIO *bio)
  332. {
  333. BIO_CONNECT *c;
  334. c=(BIO_CONNECT *)bio->ptr;
  335. if (bio->num != INVALID_SOCKET)
  336. {
  337. /* Only do a shutdown if things were established */
  338. if (c->state == BIO_CONN_S_OK)
  339. shutdown(bio->num,2);
  340. closesocket(bio->num);
  341. bio->num=INVALID_SOCKET;
  342. }
  343. }
  344. static int conn_free(BIO *a)
  345. {
  346. BIO_CONNECT *data;
  347. if (a == NULL) return(0);
  348. data=(BIO_CONNECT *)a->ptr;
  349. if (a->shutdown)
  350. {
  351. conn_close_socket(a);
  352. BIO_CONNECT_free(data);
  353. a->ptr=NULL;
  354. a->flags=0;
  355. a->init=0;
  356. }
  357. return(1);
  358. }
  359. static int conn_read(BIO *b, char *out, int outl)
  360. {
  361. int ret=0;
  362. BIO_CONNECT *data;
  363. data=(BIO_CONNECT *)b->ptr;
  364. if (data->state != BIO_CONN_S_OK)
  365. {
  366. ret=conn_state(b,data);
  367. if (ret <= 0)
  368. return(ret);
  369. }
  370. if (out != NULL)
  371. {
  372. clear_socket_error();
  373. ret=readsocket(b->num,out,outl);
  374. BIO_clear_retry_flags(b);
  375. if (ret <= 0)
  376. {
  377. if (BIO_sock_should_retry(ret))
  378. BIO_set_retry_read(b);
  379. }
  380. }
  381. return(ret);
  382. }
  383. static int conn_write(BIO *b, const char *in, int inl)
  384. {
  385. int ret;
  386. BIO_CONNECT *data;
  387. data=(BIO_CONNECT *)b->ptr;
  388. if (data->state != BIO_CONN_S_OK)
  389. {
  390. ret=conn_state(b,data);
  391. if (ret <= 0) return(ret);
  392. }
  393. clear_socket_error();
  394. ret=writesocket(b->num,in,inl);
  395. BIO_clear_retry_flags(b);
  396. if (ret <= 0)
  397. {
  398. if (BIO_sock_should_retry(ret))
  399. BIO_set_retry_write(b);
  400. }
  401. return(ret);
  402. }
  403. static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
  404. {
  405. BIO *dbio;
  406. int *ip;
  407. const char **pptr;
  408. long ret=1;
  409. BIO_CONNECT *data;
  410. data=(BIO_CONNECT *)b->ptr;
  411. switch (cmd)
  412. {
  413. case BIO_CTRL_RESET:
  414. ret=0;
  415. data->state=BIO_CONN_S_BEFORE;
  416. conn_close_socket(b);
  417. b->flags=0;
  418. break;
  419. case BIO_C_DO_STATE_MACHINE:
  420. /* use this one to start the connection */
  421. if (data->state != BIO_CONN_S_OK)
  422. ret=(long)conn_state(b,data);
  423. else
  424. ret=1;
  425. break;
  426. case BIO_C_GET_CONNECT:
  427. if (ptr != NULL)
  428. {
  429. pptr=(const char **)ptr;
  430. if (num == 0)
  431. {
  432. *pptr=data->param_hostname;
  433. }
  434. else if (num == 1)
  435. {
  436. *pptr=data->param_port;
  437. }
  438. else if (num == 2)
  439. {
  440. *pptr= (char *)&(data->ip[0]);
  441. }
  442. else if (num == 3)
  443. {
  444. *((int *)ptr)=data->port;
  445. }
  446. if ((!b->init) || (ptr == NULL))
  447. *pptr="not initialized";
  448. ret=1;
  449. }
  450. break;
  451. case BIO_C_SET_CONNECT:
  452. if (ptr != NULL)
  453. {
  454. b->init=1;
  455. if (num == 0)
  456. {
  457. if (data->param_hostname != NULL)
  458. OPENSSL_free(data->param_hostname);
  459. data->param_hostname=BUF_strdup(ptr);
  460. }
  461. else if (num == 1)
  462. {
  463. if (data->param_port != NULL)
  464. OPENSSL_free(data->param_port);
  465. data->param_port=BUF_strdup(ptr);
  466. }
  467. else if (num == 2)
  468. {
  469. char buf[16];
  470. unsigned char *p = ptr;
  471. BIO_snprintf(buf,sizeof buf,"%d.%d.%d.%d",
  472. p[0],p[1],p[2],p[3]);
  473. if (data->param_hostname != NULL)
  474. OPENSSL_free(data->param_hostname);
  475. data->param_hostname=BUF_strdup(buf);
  476. memcpy(&(data->ip[0]),ptr,4);
  477. }
  478. else if (num == 3)
  479. {
  480. char buf[DECIMAL_SIZE(int)+1];
  481. BIO_snprintf(buf,sizeof buf,"%d",*(int *)ptr);
  482. if (data->param_port != NULL)
  483. OPENSSL_free(data->param_port);
  484. data->param_port=BUF_strdup(buf);
  485. data->port= *(int *)ptr;
  486. }
  487. }
  488. break;
  489. case BIO_C_SET_NBIO:
  490. data->nbio=(int)num;
  491. break;
  492. case BIO_C_GET_FD:
  493. if (b->init)
  494. {
  495. ip=(int *)ptr;
  496. if (ip != NULL)
  497. *ip=b->num;
  498. ret=b->num;
  499. }
  500. else
  501. ret= -1;
  502. break;
  503. case BIO_CTRL_GET_CLOSE:
  504. ret=b->shutdown;
  505. break;
  506. case BIO_CTRL_SET_CLOSE:
  507. b->shutdown=(int)num;
  508. break;
  509. case BIO_CTRL_PENDING:
  510. case BIO_CTRL_WPENDING:
  511. ret=0;
  512. break;
  513. case BIO_CTRL_FLUSH:
  514. break;
  515. case BIO_CTRL_DUP:
  516. {
  517. dbio=(BIO *)ptr;
  518. if (data->param_port)
  519. BIO_set_conn_port(dbio,data->param_port);
  520. if (data->param_hostname)
  521. BIO_set_conn_hostname(dbio,data->param_hostname);
  522. BIO_set_nbio(dbio,data->nbio);
  523. /* FIXME: the cast of the function seems unlikely to be a good idea */
  524. (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback);
  525. }
  526. break;
  527. case BIO_CTRL_SET_CALLBACK:
  528. {
  529. #if 0 /* FIXME: Should this be used? -- Richard Levitte */
  530. BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  531. ret = -1;
  532. #else
  533. ret=0;
  534. #endif
  535. }
  536. break;
  537. case BIO_CTRL_GET_CALLBACK:
  538. {
  539. int (**fptr)(const BIO *bio,int state,int xret);
  540. fptr=(int (**)(const BIO *bio,int state,int xret))ptr;
  541. *fptr=data->info_callback;
  542. }
  543. break;
  544. default:
  545. ret=0;
  546. break;
  547. }
  548. return(ret);
  549. }
  550. static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  551. {
  552. long ret=1;
  553. BIO_CONNECT *data;
  554. data=(BIO_CONNECT *)b->ptr;
  555. switch (cmd)
  556. {
  557. case BIO_CTRL_SET_CALLBACK:
  558. {
  559. data->info_callback=(int (*)(const struct bio_st *, int, int))fp;
  560. }
  561. break;
  562. default:
  563. ret=0;
  564. break;
  565. }
  566. return(ret);
  567. }
  568. static int conn_puts(BIO *bp, const char *str)
  569. {
  570. int n,ret;
  571. n=strlen(str);
  572. ret=conn_write(bp,str,n);
  573. return(ret);
  574. }
  575. BIO *BIO_new_connect(char *str)
  576. {
  577. BIO *ret;
  578. ret=BIO_new(BIO_s_connect());
  579. if (ret == NULL) return(NULL);
  580. if (BIO_set_conn_hostname(ret,str))
  581. return(ret);
  582. else
  583. {
  584. BIO_free(ret);
  585. return(NULL);
  586. }
  587. }
  588. #endif