b_sock.c 21 KB

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