b_sock.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /* crypto/bio/b_sock.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 <stdlib.h>
  60. #include <errno.h>
  61. #define USE_SOCKETS
  62. #include "cryptlib.h"
  63. #include <openssl/bio.h>
  64. #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
  65. #include <netdb.h>
  66. #if defined(NETWARE_CLIB)
  67. #include <sys/ioctl.h>
  68. NETDB_DEFINE_CONTEXT
  69. #endif
  70. #endif
  71. #ifndef OPENSSL_NO_SOCK
  72. #include <openssl/dso.h>
  73. #define SOCKET_PROTOCOL IPPROTO_TCP
  74. #ifdef SO_MAXCONN
  75. #define MAX_LISTEN SO_MAXCONN
  76. #elif defined(SOMAXCONN)
  77. #define MAX_LISTEN SOMAXCONN
  78. #else
  79. #define MAX_LISTEN 32
  80. #endif
  81. #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
  82. static int wsa_init_done=0;
  83. #endif
  84. /*
  85. * WSAAPI specifier is required to make indirect calls to run-time
  86. * linked WinSock 2 functions used in this module, to be specific
  87. * [get|free]addrinfo and getnameinfo. This is because WinSock uses
  88. * uses non-C calling convention, __stdcall vs. __cdecl, on x86
  89. * Windows. On non-WinSock platforms WSAAPI needs to be void.
  90. */
  91. #ifndef WSAAPI
  92. #define WSAAPI
  93. #endif
  94. #if 0
  95. static unsigned long BIO_ghbn_hits=0L;
  96. static unsigned long BIO_ghbn_miss=0L;
  97. #define GHBN_NUM 4
  98. static struct ghbn_cache_st
  99. {
  100. char name[129];
  101. struct hostent *ent;
  102. unsigned long order;
  103. } ghbn_cache[GHBN_NUM];
  104. #endif
  105. static int get_ip(const char *str,unsigned char *ip);
  106. #if 0
  107. static void ghbn_free(struct hostent *a);
  108. static struct hostent *ghbn_dup(struct hostent *a);
  109. #endif
  110. int BIO_get_host_ip(const char *str, unsigned char *ip)
  111. {
  112. int i;
  113. int err = 1;
  114. int locked = 0;
  115. struct hostent *he;
  116. i=get_ip(str,ip);
  117. if (i < 0)
  118. {
  119. BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
  120. goto err;
  121. }
  122. /* At this point, we have something that is most probably correct
  123. in some way, so let's init the socket. */
  124. if (BIO_sock_init() != 1)
  125. return 0; /* don't generate another error code here */
  126. /* If the string actually contained an IP address, we need not do
  127. anything more */
  128. if (i > 0) return(1);
  129. /* do a gethostbyname */
  130. CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
  131. locked = 1;
  132. he=BIO_gethostbyname(str);
  133. if (he == NULL)
  134. {
  135. BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
  136. goto err;
  137. }
  138. /* cast to short because of win16 winsock definition */
  139. if ((short)he->h_addrtype != AF_INET)
  140. {
  141. BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
  142. goto err;
  143. }
  144. for (i=0; i<4; i++)
  145. ip[i]=he->h_addr_list[0][i];
  146. err = 0;
  147. err:
  148. if (locked)
  149. CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
  150. if (err)
  151. {
  152. ERR_add_error_data(2,"host=",str);
  153. return 0;
  154. }
  155. else
  156. return 1;
  157. }
  158. int BIO_get_port(const char *str, unsigned short *port_ptr)
  159. {
  160. int i;
  161. struct servent *s;
  162. if (str == NULL)
  163. {
  164. BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED);
  165. return(0);
  166. }
  167. i=atoi(str);
  168. if (i != 0)
  169. *port_ptr=(unsigned short)i;
  170. else
  171. {
  172. CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
  173. /* Note: under VMS with SOCKETSHR, it seems like the first
  174. * parameter is 'char *', instead of 'const char *'
  175. */
  176. #ifndef CONST_STRICT
  177. s=getservbyname((char *)str,"tcp");
  178. #else
  179. s=getservbyname(str,"tcp");
  180. #endif
  181. if(s != NULL)
  182. *port_ptr=ntohs((unsigned short)s->s_port);
  183. CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
  184. if(s == NULL)
  185. {
  186. if (strcmp(str,"http") == 0)
  187. *port_ptr=80;
  188. else if (strcmp(str,"telnet") == 0)
  189. *port_ptr=23;
  190. else if (strcmp(str,"socks") == 0)
  191. *port_ptr=1080;
  192. else if (strcmp(str,"https") == 0)
  193. *port_ptr=443;
  194. else if (strcmp(str,"ssl") == 0)
  195. *port_ptr=443;
  196. else if (strcmp(str,"ftp") == 0)
  197. *port_ptr=21;
  198. else if (strcmp(str,"gopher") == 0)
  199. *port_ptr=70;
  200. #if 0
  201. else if (strcmp(str,"wais") == 0)
  202. *port_ptr=21;
  203. #endif
  204. else
  205. {
  206. SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error());
  207. ERR_add_error_data(3,"service='",str,"'");
  208. return(0);
  209. }
  210. }
  211. }
  212. return(1);
  213. }
  214. int BIO_sock_error(int sock)
  215. {
  216. int j,i;
  217. int size;
  218. #if defined(OPENSSL_SYS_BEOS_R5)
  219. return 0;
  220. #endif
  221. size=sizeof(int);
  222. /* Note: under Windows the third parameter is of type (char *)
  223. * whereas under other systems it is (void *) if you don't have
  224. * a cast it will choke the compiler: if you do have a cast then
  225. * you can either go for (char *) or (void *).
  226. */
  227. i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size);
  228. if (i < 0)
  229. return(1);
  230. else
  231. return(j);
  232. }
  233. #if 0
  234. long BIO_ghbn_ctrl(int cmd, int iarg, char *parg)
  235. {
  236. int i;
  237. char **p;
  238. switch (cmd)
  239. {
  240. case BIO_GHBN_CTRL_HITS:
  241. return(BIO_ghbn_hits);
  242. /* break; */
  243. case BIO_GHBN_CTRL_MISSES:
  244. return(BIO_ghbn_miss);
  245. /* break; */
  246. case BIO_GHBN_CTRL_CACHE_SIZE:
  247. return(GHBN_NUM);
  248. /* break; */
  249. case BIO_GHBN_CTRL_GET_ENTRY:
  250. if ((iarg >= 0) && (iarg <GHBN_NUM) &&
  251. (ghbn_cache[iarg].order > 0))
  252. {
  253. p=(char **)parg;
  254. if (p == NULL) return(0);
  255. *p=ghbn_cache[iarg].name;
  256. ghbn_cache[iarg].name[128]='\0';
  257. return(1);
  258. }
  259. return(0);
  260. /* break; */
  261. case BIO_GHBN_CTRL_FLUSH:
  262. for (i=0; i<GHBN_NUM; i++)
  263. ghbn_cache[i].order=0;
  264. break;
  265. default:
  266. return(0);
  267. }
  268. return(1);
  269. }
  270. #endif
  271. #if 0
  272. static struct hostent *ghbn_dup(struct hostent *a)
  273. {
  274. struct hostent *ret;
  275. int i,j;
  276. MemCheck_off();
  277. ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent));
  278. if (ret == NULL) return(NULL);
  279. memset(ret,0,sizeof(struct hostent));
  280. for (i=0; a->h_aliases[i] != NULL; i++)
  281. ;
  282. i++;
  283. ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *));
  284. if (ret->h_aliases == NULL)
  285. goto err;
  286. memset(ret->h_aliases, 0, i*sizeof(char *));
  287. for (i=0; a->h_addr_list[i] != NULL; i++)
  288. ;
  289. i++;
  290. ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *));
  291. if (ret->h_addr_list == NULL)
  292. goto err;
  293. memset(ret->h_addr_list, 0, i*sizeof(char *));
  294. j=strlen(a->h_name)+1;
  295. if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err;
  296. memcpy((char *)ret->h_name,a->h_name,j);
  297. for (i=0; a->h_aliases[i] != NULL; i++)
  298. {
  299. j=strlen(a->h_aliases[i])+1;
  300. if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err;
  301. memcpy(ret->h_aliases[i],a->h_aliases[i],j);
  302. }
  303. ret->h_length=a->h_length;
  304. ret->h_addrtype=a->h_addrtype;
  305. for (i=0; a->h_addr_list[i] != NULL; i++)
  306. {
  307. if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL)
  308. goto err;
  309. memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
  310. }
  311. if (0)
  312. {
  313. err:
  314. if (ret != NULL)
  315. ghbn_free(ret);
  316. ret=NULL;
  317. }
  318. MemCheck_on();
  319. return(ret);
  320. }
  321. static void ghbn_free(struct hostent *a)
  322. {
  323. int i;
  324. if(a == NULL)
  325. return;
  326. if (a->h_aliases != NULL)
  327. {
  328. for (i=0; a->h_aliases[i] != NULL; i++)
  329. OPENSSL_free(a->h_aliases[i]);
  330. OPENSSL_free(a->h_aliases);
  331. }
  332. if (a->h_addr_list != NULL)
  333. {
  334. for (i=0; a->h_addr_list[i] != NULL; i++)
  335. OPENSSL_free(a->h_addr_list[i]);
  336. OPENSSL_free(a->h_addr_list);
  337. }
  338. if (a->h_name != NULL) OPENSSL_free(a->h_name);
  339. OPENSSL_free(a);
  340. }
  341. #endif
  342. struct hostent *BIO_gethostbyname(const char *name)
  343. {
  344. #if 1
  345. /* Caching gethostbyname() results forever is wrong,
  346. * so we have to let the true gethostbyname() worry about this */
  347. #if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
  348. return gethostbyname((char*)name);
  349. #else
  350. return gethostbyname(name);
  351. #endif
  352. #else
  353. struct hostent *ret;
  354. int i,lowi=0,j;
  355. unsigned long low= (unsigned long)-1;
  356. # if 0
  357. /* It doesn't make sense to use locking here: The function interface
  358. * is not thread-safe, because threads can never be sure when
  359. * some other thread destroys the data they were given a pointer to.
  360. */
  361. CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
  362. # endif
  363. j=strlen(name);
  364. if (j < 128)
  365. {
  366. for (i=0; i<GHBN_NUM; i++)
  367. {
  368. if (low > ghbn_cache[i].order)
  369. {
  370. low=ghbn_cache[i].order;
  371. lowi=i;
  372. }
  373. if (ghbn_cache[i].order > 0)
  374. {
  375. if (strncmp(name,ghbn_cache[i].name,128) == 0)
  376. break;
  377. }
  378. }
  379. }
  380. else
  381. i=GHBN_NUM;
  382. if (i == GHBN_NUM) /* no hit*/
  383. {
  384. BIO_ghbn_miss++;
  385. /* Note: under VMS with SOCKETSHR, it seems like the first
  386. * parameter is 'char *', instead of 'const char *'
  387. */
  388. # ifndef CONST_STRICT
  389. ret=gethostbyname((char *)name);
  390. # else
  391. ret=gethostbyname(name);
  392. # endif
  393. if (ret == NULL)
  394. goto end;
  395. if (j > 128) /* too big to cache */
  396. {
  397. # if 0
  398. /* If we were trying to make this function thread-safe (which
  399. * is bound to fail), we'd have to give up in this case
  400. * (or allocate more memory). */
  401. ret = NULL;
  402. # endif
  403. goto end;
  404. }
  405. /* else add to cache */
  406. if (ghbn_cache[lowi].ent != NULL)
  407. ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */
  408. ghbn_cache[lowi].name[0] = '\0';
  409. if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL)
  410. {
  411. BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE);
  412. goto end;
  413. }
  414. strncpy(ghbn_cache[lowi].name,name,128);
  415. ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
  416. }
  417. else
  418. {
  419. BIO_ghbn_hits++;
  420. ret= ghbn_cache[i].ent;
  421. ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
  422. }
  423. end:
  424. # if 0
  425. CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
  426. # endif
  427. return(ret);
  428. #endif
  429. }
  430. int BIO_sock_init(void)
  431. {
  432. #ifdef OPENSSL_SYS_WINDOWS
  433. static struct WSAData wsa_state;
  434. if (!wsa_init_done)
  435. {
  436. int err;
  437. wsa_init_done=1;
  438. memset(&wsa_state,0,sizeof(wsa_state));
  439. /* Not making wsa_state available to the rest of the
  440. * code is formally wrong. But the structures we use
  441. * are [beleived to be] invariable among Winsock DLLs,
  442. * while API availability is [expected to be] probed
  443. * at run-time with DSO_global_lookup. */
  444. if (WSAStartup(0x0202,&wsa_state)!=0)
  445. {
  446. err=WSAGetLastError();
  447. SYSerr(SYS_F_WSASTARTUP,err);
  448. BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
  449. return(-1);
  450. }
  451. }
  452. #endif /* OPENSSL_SYS_WINDOWS */
  453. #ifdef WATT32
  454. extern int _watt_do_exit;
  455. _watt_do_exit = 0; /* don't make sock_init() call exit() */
  456. if (sock_init())
  457. return (-1);
  458. #endif
  459. #if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
  460. WORD wVerReq;
  461. WSADATA wsaData;
  462. int err;
  463. if (!wsa_init_done)
  464. {
  465. wsa_init_done=1;
  466. wVerReq = MAKEWORD( 2, 0 );
  467. err = WSAStartup(wVerReq,&wsaData);
  468. if (err != 0)
  469. {
  470. SYSerr(SYS_F_WSASTARTUP,err);
  471. BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
  472. return(-1);
  473. }
  474. }
  475. #endif
  476. return(1);
  477. }
  478. void BIO_sock_cleanup(void)
  479. {
  480. #ifdef OPENSSL_SYS_WINDOWS
  481. if (wsa_init_done)
  482. {
  483. wsa_init_done=0;
  484. #if 0 /* this call is claimed to be non-present in Winsock2 */
  485. WSACancelBlockingCall();
  486. #endif
  487. WSACleanup();
  488. }
  489. #elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
  490. if (wsa_init_done)
  491. {
  492. wsa_init_done=0;
  493. WSACleanup();
  494. }
  495. #endif
  496. }
  497. #if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
  498. int BIO_socket_ioctl(int fd, long type, void *arg)
  499. {
  500. int i;
  501. #ifdef __DJGPP__
  502. i=ioctlsocket(fd,type,(char *)arg);
  503. #else
  504. # if defined(OPENSSL_SYS_VMS)
  505. /* 2011-02-18 SMS.
  506. * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
  507. * observe that all the consumers pass in an "unsigned long *",
  508. * so we arrange a local copy with a short pointer, and use
  509. * that, instead.
  510. */
  511. # if __INITIAL_POINTER_SIZE == 64
  512. # define ARG arg_32p
  513. # pragma pointer_size save
  514. # pragma pointer_size 32
  515. unsigned long arg_32;
  516. unsigned long *arg_32p;
  517. # pragma pointer_size restore
  518. arg_32p = &arg_32;
  519. arg_32 = *((unsigned long *) arg);
  520. # else /* __INITIAL_POINTER_SIZE == 64 */
  521. # define ARG arg
  522. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  523. # else /* defined(OPENSSL_SYS_VMS) */
  524. # define ARG arg
  525. # endif /* defined(OPENSSL_SYS_VMS) [else] */
  526. i=ioctlsocket(fd,type,ARG);
  527. #endif /* __DJGPP__ */
  528. if (i < 0)
  529. SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
  530. return(i);
  531. }
  532. #endif /* __VMS_VER */
  533. /* The reason I have implemented this instead of using sscanf is because
  534. * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
  535. static int get_ip(const char *str, unsigned char ip[4])
  536. {
  537. unsigned int tmp[4];
  538. int num=0,c,ok=0;
  539. tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
  540. for (;;)
  541. {
  542. c= *(str++);
  543. if ((c >= '0') && (c <= '9'))
  544. {
  545. ok=1;
  546. tmp[num]=tmp[num]*10+c-'0';
  547. if (tmp[num] > 255) return(0);
  548. }
  549. else if (c == '.')
  550. {
  551. if (!ok) return(-1);
  552. if (num == 3) return(0);
  553. num++;
  554. ok=0;
  555. }
  556. else if (c == '\0' && (num == 3) && ok)
  557. break;
  558. else
  559. return(0);
  560. }
  561. ip[0]=tmp[0];
  562. ip[1]=tmp[1];
  563. ip[2]=tmp[2];
  564. ip[3]=tmp[3];
  565. return(1);
  566. }
  567. int BIO_get_accept_socket(char *host, int bind_mode)
  568. {
  569. int ret=0;
  570. union {
  571. struct sockaddr sa;
  572. struct sockaddr_in sa_in;
  573. #if OPENSSL_USE_IPV6
  574. struct sockaddr_in6 sa_in6;
  575. #endif
  576. } server,client;
  577. int s=INVALID_SOCKET,cs,addrlen;
  578. unsigned char ip[4];
  579. unsigned short port;
  580. char *str=NULL,*e;
  581. char *h,*p;
  582. unsigned long l;
  583. int err_num;
  584. if (BIO_sock_init() != 1) return(INVALID_SOCKET);
  585. if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
  586. h=p=NULL;
  587. h=str;
  588. for (e=str; *e; e++)
  589. {
  590. if (*e == ':')
  591. {
  592. p=e;
  593. }
  594. else if (*e == '/')
  595. {
  596. *e='\0';
  597. break;
  598. }
  599. }
  600. if (p) *p++='\0'; /* points at last ':', '::port' is special [see below] */
  601. else p=h,h=NULL;
  602. #ifdef EAI_FAMILY
  603. do {
  604. static union { void *p;
  605. int (WSAAPI *f)(const char *,const char *,
  606. const struct addrinfo *,
  607. struct addrinfo **);
  608. } p_getaddrinfo = {NULL};
  609. static union { void *p;
  610. void (WSAAPI *f)(struct addrinfo *);
  611. } p_freeaddrinfo = {NULL};
  612. struct addrinfo *res,hint;
  613. if (p_getaddrinfo.p==NULL)
  614. {
  615. if ((p_getaddrinfo.p=DSO_global_lookup("getaddrinfo"))==NULL ||
  616. (p_freeaddrinfo.p=DSO_global_lookup("freeaddrinfo"))==NULL)
  617. p_getaddrinfo.p=(void*)-1;
  618. }
  619. if (p_getaddrinfo.p==(void *)-1) break;
  620. /* '::port' enforces IPv6 wildcard listener. Some OSes,
  621. * e.g. Solaris, default to IPv6 without any hint. Also
  622. * note that commonly IPv6 wildchard socket can service
  623. * IPv4 connections just as well... */
  624. memset(&hint,0,sizeof(hint));
  625. hint.ai_flags = AI_PASSIVE;
  626. if (h)
  627. {
  628. if (strchr(h,':'))
  629. {
  630. if (h[1]=='\0') h=NULL;
  631. #if OPENSSL_USE_IPV6
  632. hint.ai_family = AF_INET6;
  633. #else
  634. h=NULL;
  635. #endif
  636. }
  637. else if (h[0]=='*' && h[1]=='\0')
  638. {
  639. hint.ai_family = AF_INET;
  640. h=NULL;
  641. }
  642. }
  643. if ((*p_getaddrinfo.f)(h,p,&hint,&res)) break;
  644. addrlen = res->ai_addrlen<=sizeof(server) ?
  645. res->ai_addrlen :
  646. sizeof(server);
  647. memcpy(&server, res->ai_addr, addrlen);
  648. (*p_freeaddrinfo.f)(res);
  649. goto again;
  650. } while (0);
  651. #endif
  652. if (!BIO_get_port(p,&port)) goto err;
  653. memset((char *)&server,0,sizeof(server));
  654. server.sa_in.sin_family=AF_INET;
  655. server.sa_in.sin_port=htons(port);
  656. addrlen = sizeof(server.sa_in);
  657. if (h == NULL || strcmp(h,"*") == 0)
  658. server.sa_in.sin_addr.s_addr=INADDR_ANY;
  659. else
  660. {
  661. if (!BIO_get_host_ip(h,&(ip[0]))) goto err;
  662. l=(unsigned long)
  663. ((unsigned long)ip[0]<<24L)|
  664. ((unsigned long)ip[1]<<16L)|
  665. ((unsigned long)ip[2]<< 8L)|
  666. ((unsigned long)ip[3]);
  667. server.sa_in.sin_addr.s_addr=htonl(l);
  668. }
  669. again:
  670. s=socket(server.sa.sa_family,SOCK_STREAM,SOCKET_PROTOCOL);
  671. if (s == INVALID_SOCKET)
  672. {
  673. SYSerr(SYS_F_SOCKET,get_last_socket_error());
  674. ERR_add_error_data(3,"port='",host,"'");
  675. BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET);
  676. goto err;
  677. }
  678. #ifdef SO_REUSEADDR
  679. if (bind_mode == BIO_BIND_REUSEADDR)
  680. {
  681. int i=1;
  682. ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i));
  683. bind_mode=BIO_BIND_NORMAL;
  684. }
  685. #endif
  686. if (bind(s,&server.sa,addrlen) == -1)
  687. {
  688. #ifdef SO_REUSEADDR
  689. err_num=get_last_socket_error();
  690. if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
  691. #ifdef OPENSSL_SYS_WINDOWS
  692. /* Some versions of Windows define EADDRINUSE to
  693. * a dummy value.
  694. */
  695. (err_num == WSAEADDRINUSE))
  696. #else
  697. (err_num == EADDRINUSE))
  698. #endif
  699. {
  700. client = server;
  701. if (h == NULL || strcmp(h,"*") == 0)
  702. {
  703. #if OPENSSL_USE_IPV6
  704. if (client.sa.sa_family == AF_INET6)
  705. {
  706. memset(&client.sa_in6.sin6_addr,0,sizeof(client.sa_in6.sin6_addr));
  707. client.sa_in6.sin6_addr.s6_addr[15]=1;
  708. }
  709. else
  710. #endif
  711. if (client.sa.sa_family == AF_INET)
  712. {
  713. client.sa_in.sin_addr.s_addr=htonl(0x7F000001);
  714. }
  715. else goto err;
  716. }
  717. cs=socket(client.sa.sa_family,SOCK_STREAM,SOCKET_PROTOCOL);
  718. if (cs != INVALID_SOCKET)
  719. {
  720. int ii;
  721. ii=connect(cs,&client.sa,addrlen);
  722. closesocket(cs);
  723. if (ii == INVALID_SOCKET)
  724. {
  725. bind_mode=BIO_BIND_REUSEADDR;
  726. closesocket(s);
  727. goto again;
  728. }
  729. /* else error */
  730. }
  731. /* else error */
  732. }
  733. #endif
  734. SYSerr(SYS_F_BIND,err_num);
  735. ERR_add_error_data(3,"port='",host,"'");
  736. BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
  737. goto err;
  738. }
  739. if (listen(s,MAX_LISTEN) == -1)
  740. {
  741. SYSerr(SYS_F_BIND,get_last_socket_error());
  742. ERR_add_error_data(3,"port='",host,"'");
  743. BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET);
  744. goto err;
  745. }
  746. ret=1;
  747. err:
  748. if (str != NULL) OPENSSL_free(str);
  749. if ((ret == 0) && (s != INVALID_SOCKET))
  750. {
  751. closesocket(s);
  752. s= INVALID_SOCKET;
  753. }
  754. return(s);
  755. }
  756. int BIO_accept(int sock, char **addr)
  757. {
  758. int ret=INVALID_SOCKET;
  759. unsigned long l;
  760. unsigned short port;
  761. char *p;
  762. struct {
  763. /*
  764. * As for following union. Trouble is that there are platforms
  765. * that have socklen_t and there are platforms that don't, on
  766. * some platforms socklen_t is int and on some size_t. So what
  767. * one can do? One can cook #ifdef spaghetti, which is nothing
  768. * but masochistic. Or one can do union between int and size_t.
  769. * One naturally does it primarily for 64-bit platforms where
  770. * sizeof(int) != sizeof(size_t). But would it work? Note that
  771. * if size_t member is initialized to 0, then later int member
  772. * assignment naturally does the job on little-endian platforms
  773. * regardless accept's expectations! What about big-endians?
  774. * If accept expects int*, then it works, and if size_t*, then
  775. * length value would appear as unreasonably large. But this
  776. * won't prevent it from filling in the address structure. The
  777. * trouble of course would be if accept returns more data than
  778. * actual buffer can accomodate and overwrite stack... That's
  779. * where early OPENSSL_assert comes into picture. Besides, the
  780. * only 64-bit big-endian platform found so far that expects
  781. * size_t* is HP-UX, where stack grows towards higher address.
  782. * <appro>
  783. */
  784. union { size_t s; int i; } len;
  785. union {
  786. struct sockaddr sa;
  787. struct sockaddr_in sa_in;
  788. #if OPENSSL_USE_IPV6
  789. struct sockaddr_in6 sa_in6;
  790. #endif
  791. } from;
  792. } sa;
  793. sa.len.s=0;
  794. sa.len.i=sizeof(sa.from);
  795. memset(&sa.from,0,sizeof(sa.from));
  796. ret=accept(sock,&sa.from.sa,(void *)&sa.len);
  797. if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
  798. {
  799. OPENSSL_assert(sa.len.s<=sizeof(sa.from));
  800. sa.len.i = (int)sa.len.s;
  801. /* use sa.len.i from this point */
  802. }
  803. if (ret == INVALID_SOCKET)
  804. {
  805. if(BIO_sock_should_retry(ret)) return -2;
  806. SYSerr(SYS_F_ACCEPT,get_last_socket_error());
  807. BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
  808. goto end;
  809. }
  810. if (addr == NULL) goto end;
  811. #ifdef EAI_FAMILY
  812. do {
  813. char h[NI_MAXHOST],s[NI_MAXSERV];
  814. size_t nl;
  815. static union { void *p;
  816. int (WSAAPI *f)(const struct sockaddr *,size_t/*socklen_t*/,
  817. char *,size_t,char *,size_t,int);
  818. } p_getnameinfo = {NULL};
  819. /* 2nd argument to getnameinfo is specified to
  820. * be socklen_t. Unfortunately there is a number
  821. * of environments where socklen_t is not defined.
  822. * As it's passed by value, it's safe to pass it
  823. * as size_t... <appro> */
  824. if (p_getnameinfo.p==NULL)
  825. {
  826. if ((p_getnameinfo.p=DSO_global_lookup("getnameinfo"))==NULL)
  827. p_getnameinfo.p=(void*)-1;
  828. }
  829. if (p_getnameinfo.p==(void *)-1) break;
  830. if ((*p_getnameinfo.f)(&sa.from.sa,sa.len.i,h,sizeof(h),s,sizeof(s),
  831. NI_NUMERICHOST|NI_NUMERICSERV)) break;
  832. nl = strlen(h)+strlen(s)+2;
  833. p = *addr;
  834. if (p) { *p = '\0'; p = OPENSSL_realloc(p,nl); }
  835. else { p = OPENSSL_malloc(nl); }
  836. if (p==NULL)
  837. {
  838. BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
  839. goto end;
  840. }
  841. *addr = p;
  842. BIO_snprintf(*addr,nl,"%s:%s",h,s);
  843. goto end;
  844. } while(0);
  845. #endif
  846. if (sa.from.sa.sa_family != AF_INET) goto end;
  847. l=ntohl(sa.from.sa_in.sin_addr.s_addr);
  848. port=ntohs(sa.from.sa_in.sin_port);
  849. if (*addr == NULL)
  850. {
  851. if ((p=OPENSSL_malloc(24)) == NULL)
  852. {
  853. BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
  854. goto end;
  855. }
  856. *addr=p;
  857. }
  858. BIO_snprintf(*addr,24,"%d.%d.%d.%d:%d",
  859. (unsigned char)(l>>24L)&0xff,
  860. (unsigned char)(l>>16L)&0xff,
  861. (unsigned char)(l>> 8L)&0xff,
  862. (unsigned char)(l )&0xff,
  863. port);
  864. end:
  865. return(ret);
  866. }
  867. int BIO_set_tcp_ndelay(int s, int on)
  868. {
  869. int ret=0;
  870. #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
  871. int opt;
  872. #ifdef SOL_TCP
  873. opt=SOL_TCP;
  874. #else
  875. #ifdef IPPROTO_TCP
  876. opt=IPPROTO_TCP;
  877. #endif
  878. #endif
  879. ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on));
  880. #endif
  881. return(ret == 0);
  882. }
  883. #endif
  884. int BIO_socket_nbio(int s, int mode)
  885. {
  886. int ret= -1;
  887. int l;
  888. l=mode;
  889. #ifdef FIONBIO
  890. ret=BIO_socket_ioctl(s,FIONBIO,&l);
  891. #endif
  892. return(ret == 0);
  893. }