sl@0
|
1 |
/* crypto/bio/b_sock.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
|
sl@0
|
59 |
/*
|
sl@0
|
60 |
© Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
|
sl@0
|
61 |
*/
|
sl@0
|
62 |
|
sl@0
|
63 |
|
sl@0
|
64 |
#include <stdio.h>
|
sl@0
|
65 |
#include <stdlib.h>
|
sl@0
|
66 |
#include <errno.h>
|
sl@0
|
67 |
#define USE_SOCKETS
|
sl@0
|
68 |
#include "cryptlib.h"
|
sl@0
|
69 |
#include <openssl/bio.h>
|
sl@0
|
70 |
#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
|
sl@0
|
71 |
#include "netdb.h"
|
sl@0
|
72 |
#endif
|
sl@0
|
73 |
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
|
sl@0
|
74 |
#include "libcrypto_wsd_macros.h"
|
sl@0
|
75 |
#include "libcrypto_wsd.h"
|
sl@0
|
76 |
#endif
|
sl@0
|
77 |
|
sl@0
|
78 |
|
sl@0
|
79 |
#ifndef OPENSSL_NO_SOCK
|
sl@0
|
80 |
|
sl@0
|
81 |
#ifdef OPENSSL_SYS_WIN16
|
sl@0
|
82 |
#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
|
sl@0
|
83 |
#else
|
sl@0
|
84 |
#define SOCKET_PROTOCOL IPPROTO_TCP
|
sl@0
|
85 |
#endif
|
sl@0
|
86 |
|
sl@0
|
87 |
#ifdef SO_MAXCONN
|
sl@0
|
88 |
#define MAX_LISTEN SO_MAXCONN
|
sl@0
|
89 |
#elif defined(SOMAXCONN)
|
sl@0
|
90 |
#define MAX_LISTEN SOMAXCONN
|
sl@0
|
91 |
#else
|
sl@0
|
92 |
#define MAX_LISTEN 32
|
sl@0
|
93 |
#endif
|
sl@0
|
94 |
|
sl@0
|
95 |
#if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
|
sl@0
|
96 |
static int wsa_init_done=0;
|
sl@0
|
97 |
#endif
|
sl@0
|
98 |
|
sl@0
|
99 |
#if 0
|
sl@0
|
100 |
static unsigned long BIO_ghbn_hits=0L;
|
sl@0
|
101 |
static unsigned long BIO_ghbn_miss=0L;
|
sl@0
|
102 |
|
sl@0
|
103 |
#define GHBN_NUM 4
|
sl@0
|
104 |
static struct ghbn_cache_st
|
sl@0
|
105 |
{
|
sl@0
|
106 |
char name[129];
|
sl@0
|
107 |
struct hostent *ent;
|
sl@0
|
108 |
unsigned long order;
|
sl@0
|
109 |
} ghbn_cache[GHBN_NUM];
|
sl@0
|
110 |
#endif
|
sl@0
|
111 |
|
sl@0
|
112 |
static int get_ip(const char *str,unsigned char *ip);
|
sl@0
|
113 |
#if 0
|
sl@0
|
114 |
static void ghbn_free(struct hostent *a);
|
sl@0
|
115 |
static struct hostent *ghbn_dup(struct hostent *a);
|
sl@0
|
116 |
#endif
|
sl@0
|
117 |
EXPORT_C int BIO_get_host_ip(const char *str, unsigned char *ip)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
int i;
|
sl@0
|
120 |
int err = 1;
|
sl@0
|
121 |
int locked = 0;
|
sl@0
|
122 |
struct hostent *he;
|
sl@0
|
123 |
|
sl@0
|
124 |
i=get_ip(str,ip);
|
sl@0
|
125 |
if (i < 0)
|
sl@0
|
126 |
{
|
sl@0
|
127 |
BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS);
|
sl@0
|
128 |
goto err;
|
sl@0
|
129 |
}
|
sl@0
|
130 |
|
sl@0
|
131 |
/* At this point, we have something that is most probably correct
|
sl@0
|
132 |
in some way, so let's init the socket. */
|
sl@0
|
133 |
if (BIO_sock_init() != 1)
|
sl@0
|
134 |
return 0; /* don't generate another error code here */
|
sl@0
|
135 |
|
sl@0
|
136 |
/* If the string actually contained an IP address, we need not do
|
sl@0
|
137 |
anything more */
|
sl@0
|
138 |
if (i > 0) return(1);
|
sl@0
|
139 |
|
sl@0
|
140 |
/* do a gethostbyname */
|
sl@0
|
141 |
CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
|
sl@0
|
142 |
locked = 1;
|
sl@0
|
143 |
he=BIO_gethostbyname(str);
|
sl@0
|
144 |
if (he == NULL)
|
sl@0
|
145 |
{
|
sl@0
|
146 |
BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP);
|
sl@0
|
147 |
goto err;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
/* cast to short because of win16 winsock definition */
|
sl@0
|
151 |
if ((short)he->h_addrtype != AF_INET)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
|
sl@0
|
154 |
goto err;
|
sl@0
|
155 |
}
|
sl@0
|
156 |
for (i=0; i<4; i++)
|
sl@0
|
157 |
ip[i]=he->h_addr_list[0][i];
|
sl@0
|
158 |
err = 0;
|
sl@0
|
159 |
|
sl@0
|
160 |
err:
|
sl@0
|
161 |
if (locked)
|
sl@0
|
162 |
CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
|
sl@0
|
163 |
if (err)
|
sl@0
|
164 |
{
|
sl@0
|
165 |
ERR_add_error_data(2,"host=",str);
|
sl@0
|
166 |
return 0;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
else
|
sl@0
|
169 |
return 1;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
EXPORT_C int BIO_get_port(const char *str, unsigned short *port_ptr)
|
sl@0
|
173 |
{
|
sl@0
|
174 |
int i;
|
sl@0
|
175 |
struct servent *s;
|
sl@0
|
176 |
|
sl@0
|
177 |
if (str == NULL)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED);
|
sl@0
|
180 |
return(0);
|
sl@0
|
181 |
}
|
sl@0
|
182 |
i=atoi(str);
|
sl@0
|
183 |
if (i != 0)
|
sl@0
|
184 |
*port_ptr=(unsigned short)i;
|
sl@0
|
185 |
else
|
sl@0
|
186 |
{
|
sl@0
|
187 |
CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
|
sl@0
|
188 |
/* Note: under VMS with SOCKETSHR, it seems like the first
|
sl@0
|
189 |
* parameter is 'char *', instead of 'const char *'
|
sl@0
|
190 |
*/
|
sl@0
|
191 |
s=getservbyname(
|
sl@0
|
192 |
#ifndef CONST_STRICT
|
sl@0
|
193 |
(char *)
|
sl@0
|
194 |
#endif
|
sl@0
|
195 |
str,"tcp");
|
sl@0
|
196 |
if(s != NULL)
|
sl@0
|
197 |
*port_ptr=ntohs((unsigned short)s->s_port);
|
sl@0
|
198 |
CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
|
sl@0
|
199 |
if(s == NULL)
|
sl@0
|
200 |
{
|
sl@0
|
201 |
if (strcmp(str,"http") == 0)
|
sl@0
|
202 |
*port_ptr=80;
|
sl@0
|
203 |
else if (strcmp(str,"telnet") == 0)
|
sl@0
|
204 |
*port_ptr=23;
|
sl@0
|
205 |
else if (strcmp(str,"socks") == 0)
|
sl@0
|
206 |
*port_ptr=1080;
|
sl@0
|
207 |
else if (strcmp(str,"https") == 0)
|
sl@0
|
208 |
*port_ptr=443;
|
sl@0
|
209 |
else if (strcmp(str,"ssl") == 0)
|
sl@0
|
210 |
*port_ptr=443;
|
sl@0
|
211 |
else if (strcmp(str,"ftp") == 0)
|
sl@0
|
212 |
*port_ptr=21;
|
sl@0
|
213 |
else if (strcmp(str,"gopher") == 0)
|
sl@0
|
214 |
*port_ptr=70;
|
sl@0
|
215 |
#if 0
|
sl@0
|
216 |
else if (strcmp(str,"wais") == 0)
|
sl@0
|
217 |
*port_ptr=21;
|
sl@0
|
218 |
#endif
|
sl@0
|
219 |
else
|
sl@0
|
220 |
{
|
sl@0
|
221 |
SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error());
|
sl@0
|
222 |
ERR_add_error_data(3,"service='",str,"'");
|
sl@0
|
223 |
return(0);
|
sl@0
|
224 |
}
|
sl@0
|
225 |
}
|
sl@0
|
226 |
}
|
sl@0
|
227 |
return(1);
|
sl@0
|
228 |
}
|
sl@0
|
229 |
|
sl@0
|
230 |
EXPORT_C int BIO_sock_error(int sock)
|
sl@0
|
231 |
{
|
sl@0
|
232 |
int j,i;
|
sl@0
|
233 |
int size;
|
sl@0
|
234 |
|
sl@0
|
235 |
size=sizeof(int);
|
sl@0
|
236 |
/* Note: under Windows the third parameter is of type (char *)
|
sl@0
|
237 |
* whereas under other systems it is (void *) if you don't have
|
sl@0
|
238 |
* a cast it will choke the compiler: if you do have a cast then
|
sl@0
|
239 |
* you can either go for (char *) or (void *).
|
sl@0
|
240 |
*/
|
sl@0
|
241 |
i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size);
|
sl@0
|
242 |
if (i < 0)
|
sl@0
|
243 |
return(1);
|
sl@0
|
244 |
else
|
sl@0
|
245 |
return(j);
|
sl@0
|
246 |
}
|
sl@0
|
247 |
|
sl@0
|
248 |
#if 0
|
sl@0
|
249 |
long BIO_ghbn_ctrl(int cmd, int iarg, char *parg)
|
sl@0
|
250 |
{
|
sl@0
|
251 |
int i;
|
sl@0
|
252 |
char **p;
|
sl@0
|
253 |
|
sl@0
|
254 |
switch (cmd)
|
sl@0
|
255 |
{
|
sl@0
|
256 |
case BIO_GHBN_CTRL_HITS:
|
sl@0
|
257 |
return(BIO_ghbn_hits);
|
sl@0
|
258 |
/* break; */
|
sl@0
|
259 |
case BIO_GHBN_CTRL_MISSES:
|
sl@0
|
260 |
return(BIO_ghbn_miss);
|
sl@0
|
261 |
/* break; */
|
sl@0
|
262 |
case BIO_GHBN_CTRL_CACHE_SIZE:
|
sl@0
|
263 |
return(GHBN_NUM);
|
sl@0
|
264 |
/* break; */
|
sl@0
|
265 |
case BIO_GHBN_CTRL_GET_ENTRY:
|
sl@0
|
266 |
if ((iarg >= 0) && (iarg <GHBN_NUM) &&
|
sl@0
|
267 |
(ghbn_cache[iarg].order > 0))
|
sl@0
|
268 |
{
|
sl@0
|
269 |
p=(char **)parg;
|
sl@0
|
270 |
if (p == NULL) return(0);
|
sl@0
|
271 |
*p=ghbn_cache[iarg].name;
|
sl@0
|
272 |
ghbn_cache[iarg].name[128]='\0';
|
sl@0
|
273 |
return(1);
|
sl@0
|
274 |
}
|
sl@0
|
275 |
return(0);
|
sl@0
|
276 |
/* break; */
|
sl@0
|
277 |
case BIO_GHBN_CTRL_FLUSH:
|
sl@0
|
278 |
for (i=0; i<GHBN_NUM; i++)
|
sl@0
|
279 |
ghbn_cache[i].order=0;
|
sl@0
|
280 |
break;
|
sl@0
|
281 |
default:
|
sl@0
|
282 |
return(0);
|
sl@0
|
283 |
}
|
sl@0
|
284 |
return(1);
|
sl@0
|
285 |
}
|
sl@0
|
286 |
#endif
|
sl@0
|
287 |
|
sl@0
|
288 |
#if 0
|
sl@0
|
289 |
static struct hostent *ghbn_dup(struct hostent *a)
|
sl@0
|
290 |
{
|
sl@0
|
291 |
struct hostent *ret;
|
sl@0
|
292 |
int i,j;
|
sl@0
|
293 |
|
sl@0
|
294 |
MemCheck_off();
|
sl@0
|
295 |
ret=(struct hostent *)OPENSSL_malloc(sizeof(struct hostent));
|
sl@0
|
296 |
if (ret == NULL) return(NULL);
|
sl@0
|
297 |
memset(ret,0,sizeof(struct hostent));
|
sl@0
|
298 |
|
sl@0
|
299 |
for (i=0; a->h_aliases[i] != NULL; i++)
|
sl@0
|
300 |
;
|
sl@0
|
301 |
i++;
|
sl@0
|
302 |
ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *));
|
sl@0
|
303 |
if (ret->h_aliases == NULL)
|
sl@0
|
304 |
goto err;
|
sl@0
|
305 |
memset(ret->h_aliases, 0, i*sizeof(char *));
|
sl@0
|
306 |
|
sl@0
|
307 |
for (i=0; a->h_addr_list[i] != NULL; i++)
|
sl@0
|
308 |
;
|
sl@0
|
309 |
i++;
|
sl@0
|
310 |
ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *));
|
sl@0
|
311 |
if (ret->h_addr_list == NULL)
|
sl@0
|
312 |
goto err;
|
sl@0
|
313 |
memset(ret->h_addr_list, 0, i*sizeof(char *));
|
sl@0
|
314 |
|
sl@0
|
315 |
j=strlen(a->h_name)+1;
|
sl@0
|
316 |
if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err;
|
sl@0
|
317 |
memcpy((char *)ret->h_name,a->h_name,j);
|
sl@0
|
318 |
for (i=0; a->h_aliases[i] != NULL; i++)
|
sl@0
|
319 |
{
|
sl@0
|
320 |
j=strlen(a->h_aliases[i])+1;
|
sl@0
|
321 |
if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err;
|
sl@0
|
322 |
memcpy(ret->h_aliases[i],a->h_aliases[i],j);
|
sl@0
|
323 |
}
|
sl@0
|
324 |
ret->h_length=a->h_length;
|
sl@0
|
325 |
ret->h_addrtype=a->h_addrtype;
|
sl@0
|
326 |
for (i=0; a->h_addr_list[i] != NULL; i++)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL)
|
sl@0
|
329 |
goto err;
|
sl@0
|
330 |
memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length);
|
sl@0
|
331 |
}
|
sl@0
|
332 |
if (0)
|
sl@0
|
333 |
{
|
sl@0
|
334 |
err:
|
sl@0
|
335 |
if (ret != NULL)
|
sl@0
|
336 |
ghbn_free(ret);
|
sl@0
|
337 |
ret=NULL;
|
sl@0
|
338 |
}
|
sl@0
|
339 |
MemCheck_on();
|
sl@0
|
340 |
return(ret);
|
sl@0
|
341 |
}
|
sl@0
|
342 |
|
sl@0
|
343 |
static void ghbn_free(struct hostent *a)
|
sl@0
|
344 |
{
|
sl@0
|
345 |
int i;
|
sl@0
|
346 |
|
sl@0
|
347 |
if(a == NULL)
|
sl@0
|
348 |
return;
|
sl@0
|
349 |
|
sl@0
|
350 |
if (a->h_aliases != NULL)
|
sl@0
|
351 |
{
|
sl@0
|
352 |
for (i=0; a->h_aliases[i] != NULL; i++)
|
sl@0
|
353 |
OPENSSL_free(a->h_aliases[i]);
|
sl@0
|
354 |
OPENSSL_free(a->h_aliases);
|
sl@0
|
355 |
}
|
sl@0
|
356 |
if (a->h_addr_list != NULL)
|
sl@0
|
357 |
{
|
sl@0
|
358 |
for (i=0; a->h_addr_list[i] != NULL; i++)
|
sl@0
|
359 |
OPENSSL_free(a->h_addr_list[i]);
|
sl@0
|
360 |
OPENSSL_free(a->h_addr_list);
|
sl@0
|
361 |
}
|
sl@0
|
362 |
if (a->h_name != NULL) OPENSSL_free(a->h_name);
|
sl@0
|
363 |
OPENSSL_free(a);
|
sl@0
|
364 |
}
|
sl@0
|
365 |
|
sl@0
|
366 |
#endif
|
sl@0
|
367 |
|
sl@0
|
368 |
EXPORT_C struct hostent *BIO_gethostbyname(const char *name)
|
sl@0
|
369 |
{
|
sl@0
|
370 |
#if 1
|
sl@0
|
371 |
/* Caching gethostbyname() results forever is wrong,
|
sl@0
|
372 |
* so we have to let the true gethostbyname() worry about this */
|
sl@0
|
373 |
return gethostbyname(name);
|
sl@0
|
374 |
#else
|
sl@0
|
375 |
struct hostent *ret;
|
sl@0
|
376 |
int i,lowi=0,j;
|
sl@0
|
377 |
unsigned long low= (unsigned long)-1;
|
sl@0
|
378 |
|
sl@0
|
379 |
|
sl@0
|
380 |
# if 0
|
sl@0
|
381 |
/* It doesn't make sense to use locking here: The function interface
|
sl@0
|
382 |
* is not thread-safe, because threads can never be sure when
|
sl@0
|
383 |
* some other thread destroys the data they were given a pointer to.
|
sl@0
|
384 |
*/
|
sl@0
|
385 |
CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
|
sl@0
|
386 |
# endif
|
sl@0
|
387 |
j=strlen(name);
|
sl@0
|
388 |
if (j < 128)
|
sl@0
|
389 |
{
|
sl@0
|
390 |
for (i=0; i<GHBN_NUM; i++)
|
sl@0
|
391 |
{
|
sl@0
|
392 |
if (low > ghbn_cache[i].order)
|
sl@0
|
393 |
{
|
sl@0
|
394 |
low=ghbn_cache[i].order;
|
sl@0
|
395 |
lowi=i;
|
sl@0
|
396 |
}
|
sl@0
|
397 |
if (ghbn_cache[i].order > 0)
|
sl@0
|
398 |
{
|
sl@0
|
399 |
if (strncmp(name,ghbn_cache[i].name,128) == 0)
|
sl@0
|
400 |
break;
|
sl@0
|
401 |
}
|
sl@0
|
402 |
}
|
sl@0
|
403 |
}
|
sl@0
|
404 |
else
|
sl@0
|
405 |
i=GHBN_NUM;
|
sl@0
|
406 |
|
sl@0
|
407 |
if (i == GHBN_NUM) /* no hit*/
|
sl@0
|
408 |
{
|
sl@0
|
409 |
BIO_ghbn_miss++;
|
sl@0
|
410 |
/* Note: under VMS with SOCKETSHR, it seems like the first
|
sl@0
|
411 |
* parameter is 'char *', instead of 'const char *'
|
sl@0
|
412 |
*/
|
sl@0
|
413 |
ret=gethostbyname(
|
sl@0
|
414 |
# ifndef CONST_STRICT
|
sl@0
|
415 |
(char *)
|
sl@0
|
416 |
# endif
|
sl@0
|
417 |
name);
|
sl@0
|
418 |
|
sl@0
|
419 |
if (ret == NULL)
|
sl@0
|
420 |
goto end;
|
sl@0
|
421 |
if (j > 128) /* too big to cache */
|
sl@0
|
422 |
{
|
sl@0
|
423 |
# if 0
|
sl@0
|
424 |
/* If we were trying to make this function thread-safe (which
|
sl@0
|
425 |
* is bound to fail), we'd have to give up in this case
|
sl@0
|
426 |
* (or allocate more memory). */
|
sl@0
|
427 |
ret = NULL;
|
sl@0
|
428 |
# endif
|
sl@0
|
429 |
goto end;
|
sl@0
|
430 |
}
|
sl@0
|
431 |
|
sl@0
|
432 |
/* else add to cache */
|
sl@0
|
433 |
if (ghbn_cache[lowi].ent != NULL)
|
sl@0
|
434 |
ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */
|
sl@0
|
435 |
ghbn_cache[lowi].name[0] = '\0';
|
sl@0
|
436 |
|
sl@0
|
437 |
if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL)
|
sl@0
|
438 |
{
|
sl@0
|
439 |
BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE);
|
sl@0
|
440 |
goto end;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
strncpy(ghbn_cache[lowi].name,name,128);
|
sl@0
|
443 |
ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits;
|
sl@0
|
444 |
}
|
sl@0
|
445 |
else
|
sl@0
|
446 |
{
|
sl@0
|
447 |
BIO_ghbn_hits++;
|
sl@0
|
448 |
ret= ghbn_cache[i].ent;
|
sl@0
|
449 |
ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits;
|
sl@0
|
450 |
}
|
sl@0
|
451 |
end:
|
sl@0
|
452 |
# if 0
|
sl@0
|
453 |
CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
|
sl@0
|
454 |
# endif
|
sl@0
|
455 |
return(ret);
|
sl@0
|
456 |
#endif
|
sl@0
|
457 |
}
|
sl@0
|
458 |
|
sl@0
|
459 |
|
sl@0
|
460 |
EXPORT_C int BIO_sock_init(void)
|
sl@0
|
461 |
{
|
sl@0
|
462 |
#ifdef OPENSSL_SYS_WINDOWS
|
sl@0
|
463 |
static struct WSAData wsa_state;
|
sl@0
|
464 |
|
sl@0
|
465 |
if (!wsa_init_done)
|
sl@0
|
466 |
{
|
sl@0
|
467 |
int err;
|
sl@0
|
468 |
|
sl@0
|
469 |
wsa_init_done=1;
|
sl@0
|
470 |
memset(&wsa_state,0,sizeof(wsa_state));
|
sl@0
|
471 |
if (WSAStartup(0x0101,&wsa_state)!=0)
|
sl@0
|
472 |
{
|
sl@0
|
473 |
err=WSAGetLastError();
|
sl@0
|
474 |
SYSerr(SYS_F_WSASTARTUP,err);
|
sl@0
|
475 |
BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
|
sl@0
|
476 |
return(-1);
|
sl@0
|
477 |
}
|
sl@0
|
478 |
}
|
sl@0
|
479 |
#endif /* OPENSSL_SYS_WINDOWS */
|
sl@0
|
480 |
#ifdef WATT32
|
sl@0
|
481 |
extern int _watt_do_exit;
|
sl@0
|
482 |
_watt_do_exit = 0; /* don't make sock_init() call exit() */
|
sl@0
|
483 |
if (sock_init())
|
sl@0
|
484 |
return (-1);
|
sl@0
|
485 |
#endif
|
sl@0
|
486 |
|
sl@0
|
487 |
#if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
|
sl@0
|
488 |
WORD wVerReq;
|
sl@0
|
489 |
WSADATA wsaData;
|
sl@0
|
490 |
int err;
|
sl@0
|
491 |
|
sl@0
|
492 |
if (!wsa_init_done)
|
sl@0
|
493 |
{
|
sl@0
|
494 |
|
sl@0
|
495 |
|
sl@0
|
496 |
wsa_init_done=1;
|
sl@0
|
497 |
wVerReq = MAKEWORD( 2, 0 );
|
sl@0
|
498 |
err = WSAStartup(wVerReq,&wsaData);
|
sl@0
|
499 |
if (err != 0)
|
sl@0
|
500 |
{
|
sl@0
|
501 |
SYSerr(SYS_F_WSASTARTUP,err);
|
sl@0
|
502 |
BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP);
|
sl@0
|
503 |
return(-1);
|
sl@0
|
504 |
}
|
sl@0
|
505 |
}
|
sl@0
|
506 |
#endif
|
sl@0
|
507 |
|
sl@0
|
508 |
return(1);
|
sl@0
|
509 |
}
|
sl@0
|
510 |
|
sl@0
|
511 |
EXPORT_C void BIO_sock_cleanup(void)
|
sl@0
|
512 |
{
|
sl@0
|
513 |
#ifdef OPENSSL_SYS_WINDOWS
|
sl@0
|
514 |
if (wsa_init_done)
|
sl@0
|
515 |
{
|
sl@0
|
516 |
wsa_init_done=0;
|
sl@0
|
517 |
#ifndef OPENSSL_SYS_WINCE
|
sl@0
|
518 |
WSACancelBlockingCall();
|
sl@0
|
519 |
#endif
|
sl@0
|
520 |
WSACleanup();
|
sl@0
|
521 |
}
|
sl@0
|
522 |
#elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
|
sl@0
|
523 |
if (wsa_init_done)
|
sl@0
|
524 |
{
|
sl@0
|
525 |
wsa_init_done=0;
|
sl@0
|
526 |
WSACleanup();
|
sl@0
|
527 |
}
|
sl@0
|
528 |
#endif
|
sl@0
|
529 |
}
|
sl@0
|
530 |
|
sl@0
|
531 |
#if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
|
sl@0
|
532 |
|
sl@0
|
533 |
EXPORT_C int BIO_socket_ioctl(int fd, long type, void *arg)
|
sl@0
|
534 |
{
|
sl@0
|
535 |
int i;
|
sl@0
|
536 |
|
sl@0
|
537 |
#ifdef __DJGPP__
|
sl@0
|
538 |
i=ioctlsocket(fd,type,(char *)arg);
|
sl@0
|
539 |
#else
|
sl@0
|
540 |
i=ioctlsocket(fd,type,arg);
|
sl@0
|
541 |
#endif /* __DJGPP__ */
|
sl@0
|
542 |
if (i < 0)
|
sl@0
|
543 |
SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
|
sl@0
|
544 |
return(i);
|
sl@0
|
545 |
}
|
sl@0
|
546 |
#endif /* __VMS_VER */
|
sl@0
|
547 |
|
sl@0
|
548 |
/* The reason I have implemented this instead of using sscanf is because
|
sl@0
|
549 |
* Visual C 1.52c gives an unresolved external when linking a DLL :-( */
|
sl@0
|
550 |
static int get_ip(const char *str, unsigned char ip[4])
|
sl@0
|
551 |
{
|
sl@0
|
552 |
unsigned int tmp[4];
|
sl@0
|
553 |
int num=0,c,ok=0;
|
sl@0
|
554 |
|
sl@0
|
555 |
tmp[0]=tmp[1]=tmp[2]=tmp[3]=0;
|
sl@0
|
556 |
|
sl@0
|
557 |
for (;;)
|
sl@0
|
558 |
{
|
sl@0
|
559 |
c= *(str++);
|
sl@0
|
560 |
if ((c >= '0') && (c <= '9'))
|
sl@0
|
561 |
{
|
sl@0
|
562 |
ok=1;
|
sl@0
|
563 |
tmp[num]=tmp[num]*10+c-'0';
|
sl@0
|
564 |
if (tmp[num] > 255) return(0);
|
sl@0
|
565 |
}
|
sl@0
|
566 |
else if (c == '.')
|
sl@0
|
567 |
{
|
sl@0
|
568 |
if (!ok) return(-1);
|
sl@0
|
569 |
if (num == 3) return(0);
|
sl@0
|
570 |
num++;
|
sl@0
|
571 |
ok=0;
|
sl@0
|
572 |
}
|
sl@0
|
573 |
else if (c == '\0' && (num == 3) && ok)
|
sl@0
|
574 |
break;
|
sl@0
|
575 |
else
|
sl@0
|
576 |
return(0);
|
sl@0
|
577 |
}
|
sl@0
|
578 |
ip[0]=tmp[0];
|
sl@0
|
579 |
ip[1]=tmp[1];
|
sl@0
|
580 |
ip[2]=tmp[2];
|
sl@0
|
581 |
ip[3]=tmp[3];
|
sl@0
|
582 |
return(1);
|
sl@0
|
583 |
}
|
sl@0
|
584 |
|
sl@0
|
585 |
EXPORT_C int BIO_get_accept_socket(char *host, int bind_mode)
|
sl@0
|
586 |
{
|
sl@0
|
587 |
int ret=0;
|
sl@0
|
588 |
struct sockaddr_in server,client;
|
sl@0
|
589 |
int s=INVALID_SOCKET,cs;
|
sl@0
|
590 |
unsigned char ip[4];
|
sl@0
|
591 |
unsigned short port;
|
sl@0
|
592 |
char *str=NULL,*e;
|
sl@0
|
593 |
const char *h,*p;
|
sl@0
|
594 |
unsigned long l;
|
sl@0
|
595 |
int err_num;
|
sl@0
|
596 |
|
sl@0
|
597 |
if (BIO_sock_init() != 1) return(INVALID_SOCKET);
|
sl@0
|
598 |
|
sl@0
|
599 |
if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET);
|
sl@0
|
600 |
|
sl@0
|
601 |
h=p=NULL;
|
sl@0
|
602 |
h=str;
|
sl@0
|
603 |
for (e=str; *e; e++)
|
sl@0
|
604 |
{
|
sl@0
|
605 |
if (*e == ':')
|
sl@0
|
606 |
{
|
sl@0
|
607 |
p= &(e[1]);
|
sl@0
|
608 |
*e='\0';
|
sl@0
|
609 |
}
|
sl@0
|
610 |
else if (*e == '/')
|
sl@0
|
611 |
{
|
sl@0
|
612 |
*e='\0';
|
sl@0
|
613 |
break;
|
sl@0
|
614 |
}
|
sl@0
|
615 |
}
|
sl@0
|
616 |
|
sl@0
|
617 |
if (p == NULL)
|
sl@0
|
618 |
{
|
sl@0
|
619 |
p=h;
|
sl@0
|
620 |
h="*";
|
sl@0
|
621 |
}
|
sl@0
|
622 |
|
sl@0
|
623 |
if (!BIO_get_port(p,&port)) goto err;
|
sl@0
|
624 |
|
sl@0
|
625 |
memset((char *)&server,0,sizeof(server));
|
sl@0
|
626 |
server.sin_family=AF_INET;
|
sl@0
|
627 |
server.sin_port=htons(port);
|
sl@0
|
628 |
|
sl@0
|
629 |
if (strcmp(h,"*") == 0)
|
sl@0
|
630 |
server.sin_addr.s_addr=INADDR_ANY;
|
sl@0
|
631 |
else
|
sl@0
|
632 |
{
|
sl@0
|
633 |
if (!BIO_get_host_ip(h,&(ip[0]))) goto err;
|
sl@0
|
634 |
l=(unsigned long)
|
sl@0
|
635 |
((unsigned long)ip[0]<<24L)|
|
sl@0
|
636 |
((unsigned long)ip[1]<<16L)|
|
sl@0
|
637 |
((unsigned long)ip[2]<< 8L)|
|
sl@0
|
638 |
((unsigned long)ip[3]);
|
sl@0
|
639 |
server.sin_addr.s_addr=htonl(l);
|
sl@0
|
640 |
}
|
sl@0
|
641 |
|
sl@0
|
642 |
again:
|
sl@0
|
643 |
s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
|
sl@0
|
644 |
if (s == INVALID_SOCKET)
|
sl@0
|
645 |
{
|
sl@0
|
646 |
SYSerr(SYS_F_SOCKET,get_last_socket_error());
|
sl@0
|
647 |
ERR_add_error_data(3,"port='",host,"'");
|
sl@0
|
648 |
BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET);
|
sl@0
|
649 |
goto err;
|
sl@0
|
650 |
}
|
sl@0
|
651 |
|
sl@0
|
652 |
#ifdef SO_REUSEADDR
|
sl@0
|
653 |
if (bind_mode == BIO_BIND_REUSEADDR)
|
sl@0
|
654 |
{
|
sl@0
|
655 |
int i=1;
|
sl@0
|
656 |
|
sl@0
|
657 |
ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i));
|
sl@0
|
658 |
bind_mode=BIO_BIND_NORMAL;
|
sl@0
|
659 |
}
|
sl@0
|
660 |
#endif
|
sl@0
|
661 |
if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
|
sl@0
|
662 |
{
|
sl@0
|
663 |
#ifdef SO_REUSEADDR
|
sl@0
|
664 |
err_num=get_last_socket_error();
|
sl@0
|
665 |
if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
|
sl@0
|
666 |
(err_num == EADDRINUSE))
|
sl@0
|
667 |
{
|
sl@0
|
668 |
memcpy((char *)&client,(char *)&server,sizeof(server));
|
sl@0
|
669 |
if (strcmp(h,"*") == 0)
|
sl@0
|
670 |
client.sin_addr.s_addr=htonl(0x7F000001);
|
sl@0
|
671 |
cs=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
|
sl@0
|
672 |
if (cs != INVALID_SOCKET)
|
sl@0
|
673 |
{
|
sl@0
|
674 |
int ii;
|
sl@0
|
675 |
ii=connect(cs,(struct sockaddr *)&client,
|
sl@0
|
676 |
sizeof(client));
|
sl@0
|
677 |
closesocket(cs);
|
sl@0
|
678 |
if (ii == INVALID_SOCKET)
|
sl@0
|
679 |
{
|
sl@0
|
680 |
bind_mode=BIO_BIND_REUSEADDR;
|
sl@0
|
681 |
closesocket(s);
|
sl@0
|
682 |
goto again;
|
sl@0
|
683 |
}
|
sl@0
|
684 |
/* else error */
|
sl@0
|
685 |
}
|
sl@0
|
686 |
/* else error */
|
sl@0
|
687 |
}
|
sl@0
|
688 |
#endif
|
sl@0
|
689 |
SYSerr(SYS_F_BIND,err_num);
|
sl@0
|
690 |
ERR_add_error_data(3,"port='",host,"'");
|
sl@0
|
691 |
BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET);
|
sl@0
|
692 |
goto err;
|
sl@0
|
693 |
}
|
sl@0
|
694 |
if (listen(s,MAX_LISTEN) == -1)
|
sl@0
|
695 |
{
|
sl@0
|
696 |
SYSerr(SYS_F_BIND,get_last_socket_error());
|
sl@0
|
697 |
ERR_add_error_data(3,"port='",host,"'");
|
sl@0
|
698 |
BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET);
|
sl@0
|
699 |
goto err;
|
sl@0
|
700 |
}
|
sl@0
|
701 |
ret=1;
|
sl@0
|
702 |
err:
|
sl@0
|
703 |
if (str != NULL) OPENSSL_free(str);
|
sl@0
|
704 |
if ((ret == 0) && (s != INVALID_SOCKET))
|
sl@0
|
705 |
{
|
sl@0
|
706 |
closesocket(s);
|
sl@0
|
707 |
s= INVALID_SOCKET;
|
sl@0
|
708 |
}
|
sl@0
|
709 |
return(s);
|
sl@0
|
710 |
}
|
sl@0
|
711 |
#ifdef EMULATOR
|
sl@0
|
712 |
GET_STATIC_VAR_FROM_TLS(from,b_sock,struct sockaddr_in)
|
sl@0
|
713 |
#define from (*GET_WSD_VAR_NAME(from,b_sock, s)())
|
sl@0
|
714 |
#endif
|
sl@0
|
715 |
|
sl@0
|
716 |
EXPORT_C int BIO_accept(int sock, char **addr)
|
sl@0
|
717 |
{
|
sl@0
|
718 |
int ret=INVALID_SOCKET;
|
sl@0
|
719 |
#ifndef EMULATOR
|
sl@0
|
720 |
static struct sockaddr_in from;
|
sl@0
|
721 |
#endif
|
sl@0
|
722 |
unsigned long l;
|
sl@0
|
723 |
unsigned short port;
|
sl@0
|
724 |
int len;
|
sl@0
|
725 |
char *p;
|
sl@0
|
726 |
|
sl@0
|
727 |
memset((char *)&from,0,sizeof(from));
|
sl@0
|
728 |
len=sizeof(from);
|
sl@0
|
729 |
/* Note: under VMS with SOCKETSHR the fourth parameter is currently
|
sl@0
|
730 |
* of type (int *) whereas under other systems it is (void *) if
|
sl@0
|
731 |
* you don't have a cast it will choke the compiler: if you do
|
sl@0
|
732 |
* have a cast then you can either go for (int *) or (void *).
|
sl@0
|
733 |
*/
|
sl@0
|
734 |
ret=accept(sock,(struct sockaddr *)&from,(void *)&len);
|
sl@0
|
735 |
if (ret == INVALID_SOCKET)
|
sl@0
|
736 |
{
|
sl@0
|
737 |
if(BIO_sock_should_retry(ret)) return -2;
|
sl@0
|
738 |
SYSerr(SYS_F_ACCEPT,get_last_socket_error());
|
sl@0
|
739 |
BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR);
|
sl@0
|
740 |
goto end;
|
sl@0
|
741 |
}
|
sl@0
|
742 |
|
sl@0
|
743 |
if (addr == NULL) goto end;
|
sl@0
|
744 |
|
sl@0
|
745 |
l=ntohl(from.sin_addr.s_addr);
|
sl@0
|
746 |
port=ntohs(from.sin_port);
|
sl@0
|
747 |
if (*addr == NULL)
|
sl@0
|
748 |
{
|
sl@0
|
749 |
if ((p=OPENSSL_malloc(24)) == NULL)
|
sl@0
|
750 |
{
|
sl@0
|
751 |
BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE);
|
sl@0
|
752 |
goto end;
|
sl@0
|
753 |
}
|
sl@0
|
754 |
*addr=p;
|
sl@0
|
755 |
}
|
sl@0
|
756 |
BIO_snprintf(*addr,24,"%d.%d.%d.%d:%d",
|
sl@0
|
757 |
(unsigned char)(l>>24L)&0xff,
|
sl@0
|
758 |
(unsigned char)(l>>16L)&0xff,
|
sl@0
|
759 |
(unsigned char)(l>> 8L)&0xff,
|
sl@0
|
760 |
(unsigned char)(l )&0xff,
|
sl@0
|
761 |
port);
|
sl@0
|
762 |
end:
|
sl@0
|
763 |
return(ret);
|
sl@0
|
764 |
}
|
sl@0
|
765 |
|
sl@0
|
766 |
EXPORT_C int BIO_set_tcp_ndelay(int s, int on)
|
sl@0
|
767 |
{
|
sl@0
|
768 |
int ret=0;
|
sl@0
|
769 |
#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
|
sl@0
|
770 |
int opt;
|
sl@0
|
771 |
|
sl@0
|
772 |
#ifdef SOL_TCP
|
sl@0
|
773 |
opt=SOL_TCP;
|
sl@0
|
774 |
#else
|
sl@0
|
775 |
#ifdef IPPROTO_TCP
|
sl@0
|
776 |
opt=IPPROTO_TCP;
|
sl@0
|
777 |
#endif
|
sl@0
|
778 |
#endif
|
sl@0
|
779 |
|
sl@0
|
780 |
ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on));
|
sl@0
|
781 |
#endif
|
sl@0
|
782 |
return(ret == 0);
|
sl@0
|
783 |
}
|
sl@0
|
784 |
#endif
|
sl@0
|
785 |
|
sl@0
|
786 |
EXPORT_C int BIO_socket_nbio(int s, int mode)
|
sl@0
|
787 |
{
|
sl@0
|
788 |
int ret= -1;
|
sl@0
|
789 |
int l;
|
sl@0
|
790 |
|
sl@0
|
791 |
l=mode;
|
sl@0
|
792 |
#ifdef FIONBIO
|
sl@0
|
793 |
ret=BIO_socket_ioctl(s,FIONBIO,&l);
|
sl@0
|
794 |
#endif
|
sl@0
|
795 |
return(ret == 0);
|
sl@0
|
796 |
}
|