s_socket.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /* apps/s_socket.c - socket-related functions used by s_client and s_server */
  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 <stdlib.h>
  60. #include <string.h>
  61. #include <errno.h>
  62. #include <signal.h>
  63. /* With IPv6, it looks like Digital has mixed up the proper order of
  64. recursive header file inclusion, resulting in the compiler complaining
  65. that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
  66. is needed to have fileno() declared correctly... So let's define u_int */
  67. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  68. #define __U_INT
  69. typedef unsigned int u_int;
  70. #endif
  71. #define USE_SOCKETS
  72. #define NON_MAIN
  73. #include "apps.h"
  74. #undef USE_SOCKETS
  75. #undef NON_MAIN
  76. #include "s_apps.h"
  77. #include <openssl/ssl.h>
  78. #ifdef FLAT_INC
  79. #include "e_os.h"
  80. #else
  81. #include "../e_os.h"
  82. #endif
  83. #ifndef OPENSSL_NO_SOCK
  84. static struct hostent *GetHostByName(char *name);
  85. #ifdef OPENSSL_SYS_WINDOWS
  86. static void ssl_sock_cleanup(void);
  87. #endif
  88. static int ssl_sock_init(void);
  89. static int init_client_ip(int *sock,unsigned char ip[4], int port);
  90. static int init_server(int *sock, int port);
  91. static int init_server_long(int *sock, int port,char *ip);
  92. static int do_accept(int acc_sock, int *sock, char **host);
  93. static int host_ip(char *str, unsigned char ip[4]);
  94. #ifdef OPENSSL_SYS_WIN16
  95. #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
  96. #else
  97. #define SOCKET_PROTOCOL IPPROTO_TCP
  98. #endif
  99. #ifdef OPENSSL_SYS_WINDOWS
  100. static struct WSAData wsa_state;
  101. static int wsa_init_done=0;
  102. #ifdef OPENSSL_SYS_WIN16
  103. static HWND topWnd=0;
  104. static FARPROC lpTopWndProc=NULL;
  105. static FARPROC lpTopHookProc=NULL;
  106. extern HINSTANCE _hInstance; /* nice global CRT provides */
  107. static LONG FAR PASCAL topHookProc(HWND hwnd, UINT message, WPARAM wParam,
  108. LPARAM lParam)
  109. {
  110. if (hwnd == topWnd)
  111. {
  112. switch(message)
  113. {
  114. case WM_DESTROY:
  115. case WM_CLOSE:
  116. SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopWndProc);
  117. ssl_sock_cleanup();
  118. break;
  119. }
  120. }
  121. return CallWindowProc(lpTopWndProc,hwnd,message,wParam,lParam);
  122. }
  123. static BOOL CALLBACK enumproc(HWND hwnd,LPARAM lParam)
  124. {
  125. topWnd=hwnd;
  126. return(FALSE);
  127. }
  128. #endif /* OPENSSL_SYS_WIN32 */
  129. #endif /* OPENSSL_SYS_WINDOWS */
  130. #ifdef OPENSSL_SYS_WINDOWS
  131. static void ssl_sock_cleanup(void)
  132. {
  133. if (wsa_init_done)
  134. {
  135. wsa_init_done=0;
  136. #ifndef OPENSSL_SYS_WINCE
  137. WSACancelBlockingCall();
  138. #endif
  139. WSACleanup();
  140. }
  141. }
  142. #endif
  143. static int ssl_sock_init(void)
  144. {
  145. #ifdef WATT32
  146. extern int _watt_do_exit;
  147. _watt_do_exit = 0;
  148. dbug_init();
  149. if (sock_init())
  150. return (0);
  151. #elif defined(OPENSSL_SYS_WINDOWS)
  152. if (!wsa_init_done)
  153. {
  154. int err;
  155. #ifdef SIGINT
  156. signal(SIGINT,(void (*)(int))ssl_sock_cleanup);
  157. #endif
  158. wsa_init_done=1;
  159. memset(&wsa_state,0,sizeof(wsa_state));
  160. if (WSAStartup(0x0101,&wsa_state)!=0)
  161. {
  162. err=WSAGetLastError();
  163. BIO_printf(bio_err,"unable to start WINSOCK, error code=%d\n",err);
  164. return(0);
  165. }
  166. #ifdef OPENSSL_SYS_WIN16
  167. EnumTaskWindows(GetCurrentTask(),enumproc,0L);
  168. lpTopWndProc=(FARPROC)GetWindowLong(topWnd,GWL_WNDPROC);
  169. lpTopHookProc=MakeProcInstance((FARPROC)topHookProc,_hInstance);
  170. SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopHookProc);
  171. #endif /* OPENSSL_SYS_WIN16 */
  172. }
  173. #endif /* OPENSSL_SYS_WINDOWS */
  174. return(1);
  175. }
  176. int init_client(int *sock, char *host, int port)
  177. {
  178. unsigned char ip[4];
  179. short p=0;
  180. if (!host_ip(host,&(ip[0])))
  181. {
  182. return(0);
  183. }
  184. if (p != 0) port=p;
  185. return(init_client_ip(sock,ip,port));
  186. }
  187. static int init_client_ip(int *sock, unsigned char ip[4], int port)
  188. {
  189. unsigned long addr;
  190. struct sockaddr_in them;
  191. int s,i;
  192. if (!ssl_sock_init()) return(0);
  193. memset((char *)&them,0,sizeof(them));
  194. them.sin_family=AF_INET;
  195. them.sin_port=htons((unsigned short)port);
  196. addr=(unsigned long)
  197. ((unsigned long)ip[0]<<24L)|
  198. ((unsigned long)ip[1]<<16L)|
  199. ((unsigned long)ip[2]<< 8L)|
  200. ((unsigned long)ip[3]);
  201. them.sin_addr.s_addr=htonl(addr);
  202. s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
  203. if (s == INVALID_SOCKET) { perror("socket"); return(0); }
  204. #ifndef OPENSSL_SYS_MPE
  205. i=0;
  206. i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
  207. if (i < 0) { perror("keepalive"); return(0); }
  208. #endif
  209. if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
  210. { close(s); perror("connect"); return(0); }
  211. *sock=s;
  212. return(1);
  213. }
  214. int do_server(int port, int *ret, int (*cb)(), char *context)
  215. {
  216. int sock;
  217. char *name;
  218. int accept_socket;
  219. int i;
  220. if (!init_server(&accept_socket,port)) return(0);
  221. if (ret != NULL)
  222. {
  223. *ret=accept_socket;
  224. /* return(1);*/
  225. }
  226. for (;;)
  227. {
  228. if (do_accept(accept_socket,&sock,&name) == 0)
  229. {
  230. SHUTDOWN(accept_socket);
  231. return(0);
  232. }
  233. i=(*cb)(name,sock, context);
  234. if (name != NULL) OPENSSL_free(name);
  235. SHUTDOWN2(sock);
  236. if (i < 0)
  237. {
  238. SHUTDOWN2(accept_socket);
  239. return(i);
  240. }
  241. }
  242. }
  243. static int init_server_long(int *sock, int port, char *ip)
  244. {
  245. int ret=0;
  246. struct sockaddr_in server;
  247. int s= -1,i;
  248. if (!ssl_sock_init()) return(0);
  249. memset((char *)&server,0,sizeof(server));
  250. server.sin_family=AF_INET;
  251. server.sin_port=htons((unsigned short)port);
  252. if (ip == NULL)
  253. server.sin_addr.s_addr=INADDR_ANY;
  254. else
  255. /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
  256. #ifndef BIT_FIELD_LIMITS
  257. memcpy(&server.sin_addr.s_addr,ip,4);
  258. #else
  259. memcpy(&server.sin_addr,ip,4);
  260. #endif
  261. s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
  262. if (s == INVALID_SOCKET) goto err;
  263. #if defined SOL_SOCKET && defined SO_REUSEADDR
  264. {
  265. int j = 1;
  266. setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  267. (void *) &j, sizeof j);
  268. }
  269. #endif
  270. if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
  271. {
  272. #ifndef OPENSSL_SYS_WINDOWS
  273. perror("bind");
  274. #endif
  275. goto err;
  276. }
  277. /* Make it 128 for linux */
  278. if (listen(s,128) == -1) goto err;
  279. i=0;
  280. *sock=s;
  281. ret=1;
  282. err:
  283. if ((ret == 0) && (s != -1))
  284. {
  285. SHUTDOWN(s);
  286. }
  287. return(ret);
  288. }
  289. static int init_server(int *sock, int port)
  290. {
  291. return(init_server_long(sock, port, NULL));
  292. }
  293. static int do_accept(int acc_sock, int *sock, char **host)
  294. {
  295. int ret,i;
  296. struct hostent *h1,*h2;
  297. static struct sockaddr_in from;
  298. int len;
  299. /* struct linger ling; */
  300. if (!ssl_sock_init()) return(0);
  301. #ifndef OPENSSL_SYS_WINDOWS
  302. redoit:
  303. #endif
  304. memset((char *)&from,0,sizeof(from));
  305. len=sizeof(from);
  306. /* Note: under VMS with SOCKETSHR the fourth parameter is currently
  307. * of type (int *) whereas under other systems it is (void *) if
  308. * you don't have a cast it will choke the compiler: if you do
  309. * have a cast then you can either go for (int *) or (void *).
  310. */
  311. ret=accept(acc_sock,(struct sockaddr *)&from,(void *)&len);
  312. if (ret == INVALID_SOCKET)
  313. {
  314. #ifdef OPENSSL_SYS_WINDOWS
  315. i=WSAGetLastError();
  316. BIO_printf(bio_err,"accept error %d\n",i);
  317. #else
  318. if (errno == EINTR)
  319. {
  320. /*check_timeout(); */
  321. goto redoit;
  322. }
  323. fprintf(stderr,"errno=%d ",errno);
  324. perror("accept");
  325. #endif
  326. return(0);
  327. }
  328. /*
  329. ling.l_onoff=1;
  330. ling.l_linger=0;
  331. i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
  332. if (i < 0) { perror("linger"); return(0); }
  333. i=0;
  334. i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
  335. if (i < 0) { perror("keepalive"); return(0); }
  336. */
  337. if (host == NULL) goto end;
  338. #ifndef BIT_FIELD_LIMITS
  339. /* I should use WSAAsyncGetHostByName() under windows */
  340. h1=gethostbyaddr((char *)&from.sin_addr.s_addr,
  341. sizeof(from.sin_addr.s_addr),AF_INET);
  342. #else
  343. h1=gethostbyaddr((char *)&from.sin_addr,
  344. sizeof(struct in_addr),AF_INET);
  345. #endif
  346. if (h1 == NULL)
  347. {
  348. BIO_printf(bio_err,"bad gethostbyaddr\n");
  349. *host=NULL;
  350. /* return(0); */
  351. }
  352. else
  353. {
  354. if ((*host=(char *)OPENSSL_malloc(strlen(h1->h_name)+1)) == NULL)
  355. {
  356. perror("OPENSSL_malloc");
  357. return(0);
  358. }
  359. strcpy(*host,h1->h_name);
  360. h2=GetHostByName(*host);
  361. if (h2 == NULL)
  362. {
  363. BIO_printf(bio_err,"gethostbyname failure\n");
  364. return(0);
  365. }
  366. i=0;
  367. if (h2->h_addrtype != AF_INET)
  368. {
  369. BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
  370. return(0);
  371. }
  372. }
  373. end:
  374. *sock=ret;
  375. return(1);
  376. }
  377. int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
  378. short *port_ptr)
  379. {
  380. char *h,*p;
  381. h=str;
  382. p=strchr(str,':');
  383. if (p == NULL)
  384. {
  385. BIO_printf(bio_err,"no port defined\n");
  386. return(0);
  387. }
  388. *(p++)='\0';
  389. if ((ip != NULL) && !host_ip(str,ip))
  390. goto err;
  391. if (host_ptr != NULL) *host_ptr=h;
  392. if (!extract_port(p,port_ptr))
  393. goto err;
  394. return(1);
  395. err:
  396. return(0);
  397. }
  398. static int host_ip(char *str, unsigned char ip[4])
  399. {
  400. unsigned int in[4];
  401. int i;
  402. if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
  403. {
  404. for (i=0; i<4; i++)
  405. if (in[i] > 255)
  406. {
  407. BIO_printf(bio_err,"invalid IP address\n");
  408. goto err;
  409. }
  410. ip[0]=in[0];
  411. ip[1]=in[1];
  412. ip[2]=in[2];
  413. ip[3]=in[3];
  414. }
  415. else
  416. { /* do a gethostbyname */
  417. struct hostent *he;
  418. if (!ssl_sock_init()) return(0);
  419. he=GetHostByName(str);
  420. if (he == NULL)
  421. {
  422. BIO_printf(bio_err,"gethostbyname failure\n");
  423. goto err;
  424. }
  425. /* cast to short because of win16 winsock definition */
  426. if ((short)he->h_addrtype != AF_INET)
  427. {
  428. BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
  429. return(0);
  430. }
  431. ip[0]=he->h_addr_list[0][0];
  432. ip[1]=he->h_addr_list[0][1];
  433. ip[2]=he->h_addr_list[0][2];
  434. ip[3]=he->h_addr_list[0][3];
  435. }
  436. return(1);
  437. err:
  438. return(0);
  439. }
  440. int extract_port(char *str, short *port_ptr)
  441. {
  442. int i;
  443. struct servent *s;
  444. i=atoi(str);
  445. if (i != 0)
  446. *port_ptr=(unsigned short)i;
  447. else
  448. {
  449. s=getservbyname(str,"tcp");
  450. if (s == NULL)
  451. {
  452. BIO_printf(bio_err,"getservbyname failure for %s\n",str);
  453. return(0);
  454. }
  455. *port_ptr=ntohs((unsigned short)s->s_port);
  456. }
  457. return(1);
  458. }
  459. #define GHBN_NUM 4
  460. static struct ghbn_cache_st
  461. {
  462. char name[128];
  463. struct hostent ent;
  464. unsigned long order;
  465. } ghbn_cache[GHBN_NUM];
  466. static unsigned long ghbn_hits=0L;
  467. static unsigned long ghbn_miss=0L;
  468. static struct hostent *GetHostByName(char *name)
  469. {
  470. struct hostent *ret;
  471. int i,lowi=0;
  472. unsigned long low= (unsigned long)-1;
  473. for (i=0; i<GHBN_NUM; i++)
  474. {
  475. if (low > ghbn_cache[i].order)
  476. {
  477. low=ghbn_cache[i].order;
  478. lowi=i;
  479. }
  480. if (ghbn_cache[i].order > 0)
  481. {
  482. if (strncmp(name,ghbn_cache[i].name,128) == 0)
  483. break;
  484. }
  485. }
  486. if (i == GHBN_NUM) /* no hit*/
  487. {
  488. ghbn_miss++;
  489. ret=gethostbyname(name);
  490. if (ret == NULL) return(NULL);
  491. /* else add to cache */
  492. if(strlen(name) < sizeof ghbn_cache[0].name)
  493. {
  494. strcpy(ghbn_cache[lowi].name,name);
  495. memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent));
  496. ghbn_cache[lowi].order=ghbn_miss+ghbn_hits;
  497. }
  498. return(ret);
  499. }
  500. else
  501. {
  502. ghbn_hits++;
  503. ret= &(ghbn_cache[i].ent);
  504. ghbn_cache[i].order=ghbn_miss+ghbn_hits;
  505. return(ret);
  506. }
  507. }
  508. #endif