s_socket.c 13 KB

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