]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/b_sock.c
Make sure we get the definition of OPENSSL_NO_SOCK.
[thirdparty/openssl.git] / crypto / bio / b_sock.c
CommitLineData
d02b48c6 1/* crypto/bio/b_sock.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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
d02b48c6 59#include <stdio.h>
58964a49 60#include <stdlib.h>
d02b48c6
RE
61#include <errno.h>
62#define USE_SOCKETS
63#include "cryptlib.h"
ec577822 64#include <openssl/bio.h>
d02b48c6 65
9ba4cc00
RL
66#ifndef OPENSSL_NO_SOCK
67
bc36ee62 68#ifdef OPENSSL_SYS_WIN16
d02b48c6
RE
69#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
70#else
71#define SOCKET_PROTOCOL IPPROTO_TCP
72#endif
73
58964a49 74#ifdef SO_MAXCONN
58964a49 75#define MAX_LISTEN SO_MAXCONN
ddab25a9
RL
76#elif defined(SOMAXCONN)
77#define MAX_LISTEN SOMAXCONN
58964a49
RE
78#else
79#define MAX_LISTEN 32
80#endif
81
bc36ee62 82#ifdef OPENSSL_SYS_WINDOWS
d02b48c6
RE
83static int wsa_init_done=0;
84#endif
85
54a656ef 86#if 0
58964a49
RE
87static unsigned long BIO_ghbn_hits=0L;
88static unsigned long BIO_ghbn_miss=0L;
89
90#define GHBN_NUM 4
91static struct ghbn_cache_st
92 {
93 char name[129];
94 struct hostent *ent;
95 unsigned long order;
96 } ghbn_cache[GHBN_NUM];
54a656ef 97#endif
d02b48c6 98
e778802f 99static int get_ip(const char *str,unsigned char *ip);
15863658 100#if 0
58964a49
RE
101static void ghbn_free(struct hostent *a);
102static struct hostent *ghbn_dup(struct hostent *a);
15863658 103#endif
6b691a5c 104int BIO_get_host_ip(const char *str, unsigned char *ip)
d02b48c6
RE
105 {
106 int i;
ba9f2808
BM
107 int err = 1;
108 int locked = 0;
d02b48c6
RE
109 struct hostent *he;
110
111 i=get_ip(str,ip);
d02b48c6
RE
112 if (i < 0)
113 {
114 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
ba9f2808 115 goto err;
d02b48c6 116 }
d02b48c6 117
173e243a
RL
118 /* At this point, we have something that is most probably correct
119 in some way, so let's init the socket. */
8c23788d
BM
120 if (BIO_sock_init() != 1)
121 return 0; /* don't generate another error code here */
d02b48c6 122
173e243a
RL
123 /* If the string actually contained an IP address, we need not do
124 anything more */
125 if (i > 0) return(1);
126
127 /* do a gethostbyname */
ba9f2808
BM
128 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
129 locked = 1;
130 he=BIO_gethostbyname(str);
131 if (he == NULL)
132 {
133 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
134 goto err;
d02b48c6 135 }
ba9f2808
BM
136
137 /* cast to short because of win16 winsock definition */
138 if ((short)he->h_addrtype != AF_INET)
139 {
140 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
141 goto err;
142 }
143 for (i=0; i<4; i++)
144 ip[i]=he->h_addr_list[0][i];
145 err = 0;
146
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;
d02b48c6
RE
157 }
158
6b691a5c 159int BIO_get_port(const char *str, unsigned short *port_ptr)
d02b48c6
RE
160 {
161 int i;
162 struct servent *s;
163
164 if (str == NULL)
165 {
166 BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED);
167 return(0);
168 }
169 i=atoi(str);
170 if (i != 0)
171 *port_ptr=(unsigned short)i;
172 else
173 {
2a82c7cf 174 CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
7256ce6a
RL
175 /* Note: under VMS with SOCKETSHR, it seems like the first
176 * parameter is 'char *', instead of 'const char *'
177 */
9d1a01be
UM
178 s=getservbyname(
179#ifndef CONST_STRICT
180 (char *)
181#endif
182 str,"tcp");
2a82c7cf
BM
183 if(s != NULL)
184 *port_ptr=ntohs((unsigned short)s->s_port);
185 CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
186 if(s == NULL)
d02b48c6
RE
187 {
188 if (strcmp(str,"http") == 0)
189 *port_ptr=80;
190 else if (strcmp(str,"telnet") == 0)
191 *port_ptr=23;
192 else if (strcmp(str,"socks") == 0)
193 *port_ptr=1080;
194 else if (strcmp(str,"https") == 0)
195 *port_ptr=443;
196 else if (strcmp(str,"ssl") == 0)
197 *port_ptr=443;
198 else if (strcmp(str,"ftp") == 0)
199 *port_ptr=21;
200 else if (strcmp(str,"gopher") == 0)
201 *port_ptr=70;
202#if 0
203 else if (strcmp(str,"wais") == 0)
204 *port_ptr=21;
205#endif
206 else
207 {
58964a49
RE
208 SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error());
209 ERR_add_error_data(3,"service='",str,"'");
d02b48c6
RE
210 return(0);
211 }
d02b48c6 212 }
d02b48c6
RE
213 }
214 return(1);
215 }
216
6b691a5c 217int BIO_sock_error(int sock)
d02b48c6 218 {
95dc05bc 219 int j,i;
61f5b6f3 220 int size;
d02b48c6
RE
221
222 size=sizeof(int);
a1e464f9
DSH
223 /* Note: under Windows the third parameter is of type (char *)
224 * whereas under other systems it is (void *) if you don't have
225 * a cast it will choke the compiler: if you do have a cast then
226 * you can either go for (char *) or (void *).
227 */
7d7d2cbc 228 i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size);
d02b48c6
RE
229 if (i < 0)
230 return(1);
231 else
232 return(j);
233 }
234
54a656ef 235#if 0
6b691a5c 236long BIO_ghbn_ctrl(int cmd, int iarg, char *parg)
d02b48c6 237 {
58964a49
RE
238 int i;
239 char **p;
240
241 switch (cmd)
242 {
243 case BIO_GHBN_CTRL_HITS:
244 return(BIO_ghbn_hits);
dfeab068 245 /* break; */
58964a49
RE
246 case BIO_GHBN_CTRL_MISSES:
247 return(BIO_ghbn_miss);
dfeab068 248 /* break; */
58964a49
RE
249 case BIO_GHBN_CTRL_CACHE_SIZE:
250 return(GHBN_NUM);
dfeab068 251 /* break; */
58964a49
RE
252 case BIO_GHBN_CTRL_GET_ENTRY:
253 if ((iarg >= 0) && (iarg <GHBN_NUM) &&
254 (ghbn_cache[iarg].order > 0))
255 {
256 p=(char **)parg;
257 if (p == NULL) return(0);
258 *p=ghbn_cache[iarg].name;
259 ghbn_cache[iarg].name[128]='\0';
260 return(1);
261 }
262 return(0);
dfeab068 263 /* break; */
58964a49
RE
264 case BIO_GHBN_CTRL_FLUSH:
265 for (i=0; i<GHBN_NUM; i++)
266 ghbn_cache[i].order=0;
267 break;
268 default:
269 return(0);
270 }
271 return(1);
272 }
54a656ef 273#endif
58964a49 274
15863658 275#if 0
6b691a5c 276static struct hostent *ghbn_dup(struct hostent *a)
58964a49
RE
277 {
278 struct hostent *ret;
279 int i,j;
280
dfeab068 281 MemCheck_off();
26a3a48d 282 ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent));
58964a49
RE
283 if (ret == NULL) return(NULL);
284 memset(ret,0,sizeof(struct hostent));
285
286 for (i=0; a->h_aliases[i] != NULL; i++)
287 ;
288 i++;
26a3a48d 289 ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *));
2a82c7cf
BM
290 if (ret->h_aliases == NULL)
291 goto err;
292 memset(ret->h_aliases, 0, i*sizeof(char *));
58964a49
RE
293
294 for (i=0; a->h_addr_list[i] != NULL; i++)
295 ;
296 i++;
26a3a48d 297 ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *));
2a82c7cf
BM
298 if (ret->h_addr_list == NULL)
299 goto err;
300 memset(ret->h_addr_list, 0, i*sizeof(char *));
58964a49
RE
301
302 j=strlen(a->h_name)+1;
26a3a48d 303 if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err;
690233bc 304 memcpy((char *)ret->h_name,a->h_name,j);
58964a49
RE
305 for (i=0; a->h_aliases[i] != NULL; i++)
306 {
307 j=strlen(a->h_aliases[i])+1;
26a3a48d 308 if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err;
690233bc 309 memcpy(ret->h_aliases[i],a->h_aliases[i],j);
58964a49
RE
310 }
311 ret->h_length=a->h_length;
312 ret->h_addrtype=a->h_addrtype;
313 for (i=0; a->h_addr_list[i] != NULL; i++)
314 {
26a3a48d 315 if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL)
58964a49
RE
316 goto err;
317 memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
318 }
dfeab068
RE
319 if (0)
320 {
58964a49 321err:
dfeab068
RE
322 if (ret != NULL)
323 ghbn_free(ret);
324 ret=NULL;
325 }
326 MemCheck_on();
327 return(ret);
58964a49
RE
328 }
329
6b691a5c 330static void ghbn_free(struct hostent *a)
58964a49
RE
331 {
332 int i;
333
e03ddfae
BL
334 if(a == NULL)
335 return;
336
58964a49
RE
337 if (a->h_aliases != NULL)
338 {
339 for (i=0; a->h_aliases[i] != NULL; i++)
26a3a48d
RL
340 OPENSSL_free(a->h_aliases[i]);
341 OPENSSL_free(a->h_aliases);
58964a49
RE
342 }
343 if (a->h_addr_list != NULL)
344 {
345 for (i=0; a->h_addr_list[i] != NULL; i++)
26a3a48d
RL
346 OPENSSL_free(a->h_addr_list[i]);
347 OPENSSL_free(a->h_addr_list);
58964a49 348 }
26a3a48d
RL
349 if (a->h_name != NULL) OPENSSL_free(a->h_name);
350 OPENSSL_free(a);
58964a49 351 }
d02b48c6 352
15863658
DSH
353#endif
354
6b691a5c 355struct hostent *BIO_gethostbyname(const char *name)
d02b48c6 356 {
c602e7f4
BM
357#if 1
358 /* Caching gethostbyname() results forever is wrong,
359 * so we have to let the true gethostbyname() worry about this */
360 return gethostbyname(name);
361#else
d02b48c6 362 struct hostent *ret;
58964a49 363 int i,lowi=0,j;
d02b48c6
RE
364 unsigned long low= (unsigned long)-1;
365
58964a49 366
c602e7f4
BM
367# if 0
368 /* It doesn't make sense to use locking here: The function interface
369 * is not thread-safe, because threads can never be sure when
370 * some other thread destroys the data they were given a pointer to.
371 */
2a82c7cf 372 CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
c602e7f4 373# endif
58964a49
RE
374 j=strlen(name);
375 if (j < 128)
d02b48c6
RE
376 {
377 for (i=0; i<GHBN_NUM; i++)
378 {
379 if (low > ghbn_cache[i].order)
380 {
381 low=ghbn_cache[i].order;
382 lowi=i;
383 }
384 if (ghbn_cache[i].order > 0)
385 {
386 if (strncmp(name,ghbn_cache[i].name,128) == 0)
387 break;
388 }
389 }
390 }
391 else
392 i=GHBN_NUM;
393
394 if (i == GHBN_NUM) /* no hit*/
395 {
396 BIO_ghbn_miss++;
7256ce6a
RL
397 /* Note: under VMS with SOCKETSHR, it seems like the first
398 * parameter is 'char *', instead of 'const char *'
399 */
9d1a01be 400 ret=gethostbyname(
c602e7f4 401# ifndef CONST_STRICT
9d1a01be 402 (char *)
c602e7f4 403# endif
9d1a01be 404 name);
58964a49 405
2a82c7cf
BM
406 if (ret == NULL)
407 goto end;
408 if (j > 128) /* too big to cache */
409 {
c602e7f4
BM
410# if 0
411 /* If we were trying to make this function thread-safe (which
412 * is bound to fail), we'd have to give up in this case
413 * (or allocate more memory). */
2a82c7cf 414 ret = NULL;
c602e7f4 415# endif
2a82c7cf
BM
416 goto end;
417 }
58964a49 418
d02b48c6 419 /* else add to cache */
58964a49 420 if (ghbn_cache[lowi].ent != NULL)
64a3b11b 421 ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */
2a82c7cf 422 ghbn_cache[lowi].name[0] = '\0';
58964a49 423
2a82c7cf
BM
424 if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL)
425 {
426 BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE);
427 goto end;
428 }
d02b48c6 429 strncpy(ghbn_cache[lowi].name,name,128);
d02b48c6
RE
430 ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
431 }
432 else
433 {
434 BIO_ghbn_hits++;
58964a49 435 ret= ghbn_cache[i].ent;
d02b48c6
RE
436 ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
437 }
2a82c7cf 438end:
c602e7f4 439# if 0
2a82c7cf 440 CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
c602e7f4 441# endif
d02b48c6 442 return(ret);
c602e7f4 443#endif
d02b48c6
RE
444 }
445
c602e7f4 446
6b691a5c 447int BIO_sock_init(void)
d02b48c6 448 {
bc36ee62 449#ifdef OPENSSL_SYS_WINDOWS
d02b48c6
RE
450 static struct WSAData wsa_state;
451
452 if (!wsa_init_done)
453 {
454 int err;
455
456#ifdef SIGINT
457 signal(SIGINT,(void (*)(int))BIO_sock_cleanup);
458#endif
459 wsa_init_done=1;
460 memset(&wsa_state,0,sizeof(wsa_state));
461 if (WSAStartup(0x0101,&wsa_state)!=0)
462 {
463 err=WSAGetLastError();
464 SYSerr(SYS_F_WSASTARTUP,err);
465 BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
466 return(-1);
467 }
468 }
bc36ee62 469#endif /* OPENSSL_SYS_WINDOWS */
8d6e6048
RL
470#ifdef WATT32
471 extern int _watt_do_exit;
472 _watt_do_exit = 0; /* don't make sock_init() call exit() */
473 if (sock_init())
474 return (-1);
475#endif
d02b48c6
RE
476 return(1);
477 }
478
6b691a5c 479void BIO_sock_cleanup(void)
d02b48c6 480 {
bc36ee62 481#ifdef OPENSSL_SYS_WINDOWS
d02b48c6
RE
482 if (wsa_init_done)
483 {
484 wsa_init_done=0;
0bf23d9b 485#ifndef OPENSSL_SYS_WINCE
d02b48c6 486 WSACancelBlockingCall();
0bf23d9b 487#endif
d02b48c6
RE
488 WSACleanup();
489 }
490#endif
491 }
492
bc36ee62 493#if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
7d7d2cbc 494
c029841e 495int BIO_socket_ioctl(int fd, long type, void *arg)
d02b48c6 496 {
58964a49 497 int i;
d02b48c6 498
451dc18f
RL
499#ifdef __DJGPP__
500 i=ioctlsocket(fd,type,(char *)arg);
501#else
d02b48c6 502 i=ioctlsocket(fd,type,arg);
451dc18f 503#endif /* __DJGPP__ */
d02b48c6 504 if (i < 0)
58964a49 505 SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
d02b48c6
RE
506 return(i);
507 }
7d7d2cbc 508#endif /* __VMS_VER */
d02b48c6
RE
509
510/* The reason I have implemented this instead of using sscanf is because
511 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
6b691a5c 512static int get_ip(const char *str, unsigned char ip[4])
d02b48c6
RE
513 {
514 unsigned int tmp[4];
515 int num=0,c,ok=0;
516
517 tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
518
519 for (;;)
520 {
521 c= *(str++);
522 if ((c >= '0') && (c <= '9'))
523 {
524 ok=1;
525 tmp[num]=tmp[num]*10+c-'0';
b1460627 526 if (tmp[num] > 255) return(0);
d02b48c6
RE
527 }
528 else if (c == '.')
529 {
530 if (!ok) return(-1);
b1460627 531 if (num == 3) return(0);
d02b48c6
RE
532 num++;
533 ok=0;
534 }
b1460627 535 else if (c == '\0' && (num == 3) && ok)
d02b48c6
RE
536 break;
537 else
538 return(0);
539 }
540 ip[0]=tmp[0];
541 ip[1]=tmp[1];
542 ip[2]=tmp[2];
543 ip[3]=tmp[3];
544 return(1);
545 }
546
6b691a5c 547int BIO_get_accept_socket(char *host, int bind_mode)
d02b48c6
RE
548 {
549 int ret=0;
dfeab068 550 struct sockaddr_in server,client;
e24e4065 551 int s=INVALID_SOCKET,cs;
d02b48c6 552 unsigned char ip[4];
def9f431 553 unsigned short port;
e24e4065 554 char *str=NULL,*e;
e778802f 555 const char *h,*p;
d02b48c6 556 unsigned long l;
dfeab068 557 int err_num;
d02b48c6 558
8c23788d 559 if (BIO_sock_init() != 1) return(INVALID_SOCKET);
d02b48c6
RE
560
561 if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
562
563 h=p=NULL;
564 h=str;
565 for (e=str; *e; e++)
566 {
567 if (*e == ':')
568 {
569 p= &(e[1]);
570 *e='\0';
571 }
572 else if (*e == '/')
573 {
574 *e='\0';
575 break;
576 }
577 }
578
579 if (p == NULL)
580 {
581 p=h;
582 h="*";
583 }
584
e24e4065 585 if (!BIO_get_port(p,&port)) goto err;
d02b48c6
RE
586
587 memset((char *)&server,0,sizeof(server));
588 server.sin_family=AF_INET;
def9f431 589 server.sin_port=htons(port);
d02b48c6
RE
590
591 if (strcmp(h,"*") == 0)
592 server.sin_addr.s_addr=INADDR_ANY;
593 else
594 {
e24e4065 595 if (!BIO_get_host_ip(h,&(ip[0]))) goto err;
d02b48c6
RE
596 l=(unsigned long)
597 ((unsigned long)ip[0]<<24L)|
dfeab068
RE
598 ((unsigned long)ip[1]<<16L)|
599 ((unsigned long)ip[2]<< 8L)|
600 ((unsigned long)ip[3]);
d02b48c6
RE
601 server.sin_addr.s_addr=htonl(l);
602 }
603
dfeab068 604again:
d02b48c6
RE
605 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
606 if (s == INVALID_SOCKET)
607 {
58964a49
RE
608 SYSerr(SYS_F_SOCKET,get_last_socket_error());
609 ERR_add_error_data(3,"port='",host,"'");
610 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET);
d02b48c6
RE
611 goto err;
612 }
dfeab068
RE
613
614#ifdef SO_REUSEADDR
615 if (bind_mode == BIO_BIND_REUSEADDR)
616 {
617 int i=1;
618
619 ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i));
620 bind_mode=BIO_BIND_NORMAL;
621 }
622#endif
d02b48c6
RE
623 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
624 {
dfeab068
RE
625#ifdef SO_REUSEADDR
626 err_num=get_last_socket_error();
627 if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
628 (err_num == EADDRINUSE))
629 {
630 memcpy((char *)&client,(char *)&server,sizeof(server));
631 if (strcmp(h,"*") == 0)
632 client.sin_addr.s_addr=htonl(0x7F000001);
633 cs=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
634 if (cs != INVALID_SOCKET)
635 {
636 int ii;
637 ii=connect(cs,(struct sockaddr *)&client,
638 sizeof(client));
639 closesocket(cs);
640 if (ii == INVALID_SOCKET)
641 {
642 bind_mode=BIO_BIND_REUSEADDR;
643 closesocket(s);
644 goto again;
645 }
646 /* else error */
647 }
648 /* else error */
649 }
650#endif
651 SYSerr(SYS_F_BIND,err_num);
58964a49 652 ERR_add_error_data(3,"port='",host,"'");
d02b48c6
RE
653 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
654 goto err;
655 }
58964a49 656 if (listen(s,MAX_LISTEN) == -1)
d02b48c6 657 {
58964a49
RE
658 SYSerr(SYS_F_BIND,get_last_socket_error());
659 ERR_add_error_data(3,"port='",host,"'");
d02b48c6
RE
660 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET);
661 goto err;
662 }
663 ret=1;
664err:
26a3a48d 665 if (str != NULL) OPENSSL_free(str);
d02b48c6
RE
666 if ((ret == 0) && (s != INVALID_SOCKET))
667 {
d02b48c6 668 closesocket(s);
d02b48c6
RE
669 s= INVALID_SOCKET;
670 }
671 return(s);
672 }
673
6b691a5c 674int BIO_accept(int sock, char **addr)
d02b48c6
RE
675 {
676 int ret=INVALID_SOCKET;
677 static struct sockaddr_in from;
678 unsigned long l;
def9f431 679 unsigned short port;
61f5b6f3 680 int len;
d02b48c6
RE
681 char *p;
682
683 memset((char *)&from,0,sizeof(from));
684 len=sizeof(from);
75e0770d 685 /* Note: under VMS with SOCKETSHR the fourth parameter is currently
7d7d2cbc
UM
686 * of type (int *) whereas under other systems it is (void *) if
687 * you don't have a cast it will choke the compiler: if you do
688 * have a cast then you can either go for (int *) or (void *).
689 */
690 ret=accept(sock,(struct sockaddr *)&from,(void *)&len);
d02b48c6
RE
691 if (ret == INVALID_SOCKET)
692 {
924046ce 693 if(BIO_sock_should_retry(ret)) return -2;
58964a49 694 SYSerr(SYS_F_ACCEPT,get_last_socket_error());
d02b48c6
RE
695 BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
696 goto end;
697 }
698
699 if (addr == NULL) goto end;
700
701 l=ntohl(from.sin_addr.s_addr);
702 port=ntohs(from.sin_port);
703 if (*addr == NULL)
704 {
26a3a48d 705 if ((p=OPENSSL_malloc(24)) == NULL)
d02b48c6
RE
706 {
707 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
708 goto end;
709 }
710 *addr=p;
711 }
712 sprintf(*addr,"%d.%d.%d.%d:%d",
713 (unsigned char)(l>>24L)&0xff,
714 (unsigned char)(l>>16L)&0xff,
715 (unsigned char)(l>> 8L)&0xff,
716 (unsigned char)(l )&0xff,
717 port);
718end:
719 return(ret);
720 }
721
6b691a5c 722int BIO_set_tcp_ndelay(int s, int on)
d02b48c6
RE
723 {
724 int ret=0;
725#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
726 int opt;
727
728#ifdef SOL_TCP
729 opt=SOL_TCP;
730#else
731#ifdef IPPROTO_TCP
732 opt=IPPROTO_TCP;
733#endif
734#endif
735
736 ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on));
737#endif
738 return(ret == 0);
739 }
740#endif
741
6b691a5c 742int BIO_socket_nbio(int s, int mode)
dfeab068
RE
743 {
744 int ret= -1;
c029841e 745 int l;
dfeab068
RE
746
747 l=mode;
748#ifdef FIONBIO
749 ret=BIO_socket_ioctl(s,FIONBIO,&l);
750#endif
751 return(ret == 0);
752 }