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