1 /* crypto/bio/b_sock.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
67 /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
70 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
72 #define SOCKET_PROTOCOL IPPROTO_TCP
76 static int wsa_init_done=0;
79 unsigned long BIO_ghbn_hits=0L;
80 unsigned long BIO_ghbn_miss=0L;
83 static int get_ip(char *str,unsigned char *ip);
88 int BIO_get_host_ip(str,ip)
99 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
103 { /* do a gethostbyname */
104 if (!BIO_sock_init()) return(0);
106 he=BIO_gethostbyname(str);
109 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
113 /* cast to short because of win16 winsock definition */
114 if ((short)he->h_addrtype != AF_INET)
116 BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
120 ip[i]=he->h_addr_list[0][i];
125 int BIO_get_port(str,port_ptr)
134 BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED);
139 *port_ptr=(unsigned short)i;
142 s=getservbyname(str,"tcp");
145 if (strcmp(str,"http") == 0)
147 else if (strcmp(str,"telnet") == 0)
149 else if (strcmp(str,"socks") == 0)
151 else if (strcmp(str,"https") == 0)
153 else if (strcmp(str,"ssl") == 0)
155 else if (strcmp(str,"ftp") == 0)
157 else if (strcmp(str,"gopher") == 0)
160 else if (strcmp(str,"wais") == 0)
165 SYSerr(SYS_F_GETSERVBYNAME,errno);
170 *port_ptr=htons((unsigned short)s->s_port);
175 int BIO_sock_error(sock)
182 i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(char *)&j,&size);
190 static struct ghbn_cache_st
195 } ghbn_cache[GHBN_NUM];
197 struct hostent *BIO_gethostbyname(name)
202 unsigned long low= (unsigned long)-1;
204 CRYPTO_w_lock(CRYPTO_LOCK_BIO_GETHOSTBYNAME);
205 if (strlen(name) < 128)
207 for (i=0; i<GHBN_NUM; i++)
209 if (low > ghbn_cache[i].order)
211 low=ghbn_cache[i].order;
214 if (ghbn_cache[i].order > 0)
216 if (strncmp(name,ghbn_cache[i].name,128) == 0)
224 if (i == GHBN_NUM) /* no hit*/
227 ret=gethostbyname(name);
228 if (ret == NULL) return(NULL);
229 /* else add to cache */
230 strncpy(ghbn_cache[lowi].name,name,128);
231 memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent));
232 ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
237 ret= &(ghbn_cache[i].ent);
238 ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
240 CRYPTO_w_unlock(CRYPTO_LOCK_BIO_GETHOSTBYNAME);
247 static struct WSAData wsa_state;
254 signal(SIGINT,(void (*)(int))BIO_sock_cleanup);
257 memset(&wsa_state,0,sizeof(wsa_state));
258 if (WSAStartup(0x0101,&wsa_state)!=0)
260 err=WSAGetLastError();
261 SYSerr(SYS_F_WSASTARTUP,err);
262 BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
270 void BIO_sock_cleanup()
276 WSACancelBlockingCall();
282 int BIO_socket_ioctl(fd,type,arg)
290 i=ioctlsocket(fd,type,arg);
292 i=ioctl(fd,type,arg);
297 err=WSAGetLastError();
301 SYSerr(SYS_F_IOCTLSOCKET,err);
306 /* The reason I have implemented this instead of using sscanf is because
307 * Visual C 1.52c gives an unresolved external when linking a DLL :-( */
308 static int get_ip(str,ip)
315 tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
320 if ((c >= '0') && (c <= '9'))
323 tmp[num]=tmp[num]*10+c-'0';
324 if (tmp[num] > 255) return(-1);
333 else if ((num == 3) && ok)
345 int BIO_get_accept_socket(host)
349 struct sockaddr_in server;
356 if (!BIO_sock_init()) return(INVALID_SOCKET);
358 if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
382 if (!BIO_get_port(p,&port)) return(INVALID_SOCKET);
384 memset((char *)&server,0,sizeof(server));
385 server.sin_family=AF_INET;
386 server.sin_port=htons((unsigned short)port);
388 if (strcmp(h,"*") == 0)
389 server.sin_addr.s_addr=INADDR_ANY;
392 if (!BIO_get_host_ip(h,&(ip[0]))) return(INVALID_SOCKET);
394 ((unsigned long)ip[0]<<24L)|
395 ((unsigned long)ip[0]<<16L)|
396 ((unsigned long)ip[0]<< 8L)|
397 ((unsigned long)ip[0]);
398 server.sin_addr.s_addr=htonl(l);
401 s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
402 if (s == INVALID_SOCKET)
405 errno=WSAGetLastError();
407 SYSerr(SYS_F_SOCKET,errno);
408 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
411 if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
414 errno=WSAGetLastError();
416 SYSerr(SYS_F_BIND,errno);
417 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
420 if (listen(s,5) == -1)
423 errno=WSAGetLastError();
425 SYSerr(SYS_F_LISTEN,errno);
426 BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET);
431 if (str != NULL) Free(str);
432 if ((ret == 0) && (s != INVALID_SOCKET))
444 int BIO_accept(sock,addr)
448 int ret=INVALID_SOCKET;
449 static struct sockaddr_in from;
455 memset((char *)&from,0,sizeof(from));
457 ret=accept(sock,(struct sockaddr *)&from,&len);
458 if (ret == INVALID_SOCKET)
461 errno=WSAGetLastError();
463 SYSerr(SYS_F_ACCEPT,errno);
464 BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
468 if (addr == NULL) goto end;
470 l=ntohl(from.sin_addr.s_addr);
471 port=ntohs(from.sin_port);
474 if ((p=Malloc(24)) == NULL)
476 BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
481 sprintf(*addr,"%d.%d.%d.%d:%d",
482 (unsigned char)(l>>24L)&0xff,
483 (unsigned char)(l>>16L)&0xff,
484 (unsigned char)(l>> 8L)&0xff,
485 (unsigned char)(l )&0xff,
491 int BIO_set_tcp_ndelay(s,on)
496 #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
507 ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on));