sl@0
|
1 |
/* apps/s_socket.c - socket-related functions used by s_client and s_server */
|
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 |
#include <stdio.h>
|
sl@0
|
60 |
#include <stdlib.h>
|
sl@0
|
61 |
#include <string.h>
|
sl@0
|
62 |
#include <errno.h>
|
sl@0
|
63 |
|
sl@0
|
64 |
|
sl@0
|
65 |
/* With IPv6, it looks like Digital has mixed up the proper order of
|
sl@0
|
66 |
recursive header file inclusion, resulting in the compiler complaining
|
sl@0
|
67 |
that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
|
sl@0
|
68 |
is needed to have fileno() declared correctly... So let's define u_int */
|
sl@0
|
69 |
#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
|
sl@0
|
70 |
#define __U_INT
|
sl@0
|
71 |
typedef unsigned int u_int;
|
sl@0
|
72 |
#endif
|
sl@0
|
73 |
|
sl@0
|
74 |
#define USE_SOCKETS
|
sl@0
|
75 |
#define NON_MAIN
|
sl@0
|
76 |
#include "apps.h"
|
sl@0
|
77 |
#undef USE_SOCKETS
|
sl@0
|
78 |
#undef NON_MAIN
|
sl@0
|
79 |
#include "s_apps.h"
|
sl@0
|
80 |
#include <openssl/ssl.h>
|
sl@0
|
81 |
|
sl@0
|
82 |
#ifdef FLAT_INC
|
sl@0
|
83 |
#include "e_os.h"
|
sl@0
|
84 |
#else
|
sl@0
|
85 |
#ifndef SYMBIAN
|
sl@0
|
86 |
#include "../e_os.h"
|
sl@0
|
87 |
#else
|
sl@0
|
88 |
#include "e_os.h"
|
sl@0
|
89 |
#endif
|
sl@0
|
90 |
#endif
|
sl@0
|
91 |
|
sl@0
|
92 |
|
sl@0
|
93 |
#ifndef OPENSSL_NO_SOCK
|
sl@0
|
94 |
|
sl@0
|
95 |
#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
|
sl@0
|
96 |
#include "netdb.h"
|
sl@0
|
97 |
#endif
|
sl@0
|
98 |
|
sl@0
|
99 |
static struct hostent *GetHostByName(char *name);
|
sl@0
|
100 |
#if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
|
sl@0
|
101 |
static void ssl_sock_cleanup(void);
|
sl@0
|
102 |
#endif
|
sl@0
|
103 |
static int ssl_sock_init(void);
|
sl@0
|
104 |
static int init_client_ip(int *sock,unsigned char ip[4], int port, int type);
|
sl@0
|
105 |
static int init_server(int *sock, int port, int type);
|
sl@0
|
106 |
static int init_server_long(int *sock, int port,char *ip, int type);
|
sl@0
|
107 |
static int do_accept(int acc_sock, int *sock, char **host);
|
sl@0
|
108 |
static int host_ip(char *str, unsigned char ip[4]);
|
sl@0
|
109 |
|
sl@0
|
110 |
#ifdef OPENSSL_SYS_WIN16
|
sl@0
|
111 |
#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
|
sl@0
|
112 |
#else
|
sl@0
|
113 |
#define SOCKET_PROTOCOL IPPROTO_TCP
|
sl@0
|
114 |
#endif
|
sl@0
|
115 |
|
sl@0
|
116 |
#if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
|
sl@0
|
117 |
static int wsa_init_done=0;
|
sl@0
|
118 |
#endif
|
sl@0
|
119 |
|
sl@0
|
120 |
#ifdef OPENSSL_SYS_WINDOWS
|
sl@0
|
121 |
static struct WSAData wsa_state;
|
sl@0
|
122 |
static int wsa_init_done=0;
|
sl@0
|
123 |
|
sl@0
|
124 |
#ifdef OPENSSL_SYS_WIN16
|
sl@0
|
125 |
static HWND topWnd=0;
|
sl@0
|
126 |
static FARPROC lpTopWndProc=NULL;
|
sl@0
|
127 |
static FARPROC lpTopHookProc=NULL;
|
sl@0
|
128 |
extern HINSTANCE _hInstance; /* nice global CRT provides */
|
sl@0
|
129 |
|
sl@0
|
130 |
|
sl@0
|
131 |
static LONG FAR PASCAL topHookProc(HWND hwnd, UINT message, WPARAM wParam,
|
sl@0
|
132 |
LPARAM lParam)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
if (hwnd == topWnd)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
switch(message)
|
sl@0
|
137 |
{
|
sl@0
|
138 |
case WM_DESTROY:
|
sl@0
|
139 |
case WM_CLOSE:
|
sl@0
|
140 |
SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopWndProc);
|
sl@0
|
141 |
ssl_sock_cleanup();
|
sl@0
|
142 |
break;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
}
|
sl@0
|
145 |
return CallWindowProc(lpTopWndProc,hwnd,message,wParam,lParam);
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
static BOOL CALLBACK enumproc(HWND hwnd,LPARAM lParam)
|
sl@0
|
149 |
{
|
sl@0
|
150 |
topWnd=hwnd;
|
sl@0
|
151 |
return(FALSE);
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
#endif /* OPENSSL_SYS_WIN32 */
|
sl@0
|
155 |
#endif /* OPENSSL_SYS_WINDOWS */
|
sl@0
|
156 |
|
sl@0
|
157 |
#ifdef OPENSSL_SYS_WINDOWS
|
sl@0
|
158 |
static void ssl_sock_cleanup(void)
|
sl@0
|
159 |
{
|
sl@0
|
160 |
if (wsa_init_done)
|
sl@0
|
161 |
{
|
sl@0
|
162 |
wsa_init_done=0;
|
sl@0
|
163 |
#ifndef OPENSSL_SYS_WINCE
|
sl@0
|
164 |
WSACancelBlockingCall();
|
sl@0
|
165 |
#endif
|
sl@0
|
166 |
WSACleanup();
|
sl@0
|
167 |
}
|
sl@0
|
168 |
}
|
sl@0
|
169 |
#elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
|
sl@0
|
170 |
static void sock_cleanup(void)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
if (wsa_init_done)
|
sl@0
|
173 |
{
|
sl@0
|
174 |
wsa_init_done=0;
|
sl@0
|
175 |
WSACleanup();
|
sl@0
|
176 |
}
|
sl@0
|
177 |
}
|
sl@0
|
178 |
#endif
|
sl@0
|
179 |
|
sl@0
|
180 |
static int ssl_sock_init(void)
|
sl@0
|
181 |
{
|
sl@0
|
182 |
#ifdef WATT32
|
sl@0
|
183 |
extern int _watt_do_exit;
|
sl@0
|
184 |
_watt_do_exit = 0;
|
sl@0
|
185 |
if (sock_init())
|
sl@0
|
186 |
return (0);
|
sl@0
|
187 |
#elif defined(OPENSSL_SYS_WINDOWS)
|
sl@0
|
188 |
if (!wsa_init_done)
|
sl@0
|
189 |
{
|
sl@0
|
190 |
int err;
|
sl@0
|
191 |
|
sl@0
|
192 |
#ifdef SIGINT
|
sl@0
|
193 |
// signal(SIGINT,(void (*)(int))ssl_sock_cleanup);
|
sl@0
|
194 |
#endif
|
sl@0
|
195 |
wsa_init_done=1;
|
sl@0
|
196 |
memset(&wsa_state,0,sizeof(wsa_state));
|
sl@0
|
197 |
if (WSAStartup(0x0101,&wsa_state)!=0)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
err=WSAGetLastError();
|
sl@0
|
200 |
BIO_printf(bio_err,"unable to start WINSOCK, error code=%d\n",err);
|
sl@0
|
201 |
return(0);
|
sl@0
|
202 |
}
|
sl@0
|
203 |
|
sl@0
|
204 |
#ifdef OPENSSL_SYS_WIN16
|
sl@0
|
205 |
EnumTaskWindows(GetCurrentTask(),enumproc,0L);
|
sl@0
|
206 |
lpTopWndProc=(FARPROC)GetWindowLong(topWnd,GWL_WNDPROC);
|
sl@0
|
207 |
lpTopHookProc=MakeProcInstance((FARPROC)topHookProc,_hInstance);
|
sl@0
|
208 |
|
sl@0
|
209 |
SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopHookProc);
|
sl@0
|
210 |
#endif /* OPENSSL_SYS_WIN16 */
|
sl@0
|
211 |
}
|
sl@0
|
212 |
#elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
|
sl@0
|
213 |
WORD wVerReq;
|
sl@0
|
214 |
WSADATA wsaData;
|
sl@0
|
215 |
int err;
|
sl@0
|
216 |
|
sl@0
|
217 |
if (!wsa_init_done)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
|
sl@0
|
220 |
# ifdef SIGINT
|
sl@0
|
221 |
// signal(SIGINT,(void (*)(int))sock_cleanup);
|
sl@0
|
222 |
# endif
|
sl@0
|
223 |
|
sl@0
|
224 |
wsa_init_done=1;
|
sl@0
|
225 |
wVerReq = MAKEWORD( 2, 0 );
|
sl@0
|
226 |
err = WSAStartup(wVerReq,&wsaData);
|
sl@0
|
227 |
if (err != 0)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
BIO_printf(bio_err,"unable to start WINSOCK2, error code=%d\n",err);
|
sl@0
|
230 |
return(0);
|
sl@0
|
231 |
}
|
sl@0
|
232 |
}
|
sl@0
|
233 |
#endif /* OPENSSL_SYS_WINDOWS */
|
sl@0
|
234 |
return(1);
|
sl@0
|
235 |
}
|
sl@0
|
236 |
|
sl@0
|
237 |
int init_client(int *sock, char *host, int port, int type)
|
sl@0
|
238 |
{
|
sl@0
|
239 |
unsigned char ip[4];
|
sl@0
|
240 |
short p=0;
|
sl@0
|
241 |
|
sl@0
|
242 |
if (!host_ip(host,&(ip[0])))
|
sl@0
|
243 |
{
|
sl@0
|
244 |
return(0);
|
sl@0
|
245 |
}
|
sl@0
|
246 |
if (p != 0) port=p;
|
sl@0
|
247 |
return(init_client_ip(sock,ip,port,type));
|
sl@0
|
248 |
}
|
sl@0
|
249 |
|
sl@0
|
250 |
static int init_client_ip(int *sock, unsigned char ip[4], int port, int type)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
unsigned long addr;
|
sl@0
|
253 |
struct sockaddr_in them;
|
sl@0
|
254 |
int s,i;
|
sl@0
|
255 |
|
sl@0
|
256 |
if (!ssl_sock_init()) return(0);
|
sl@0
|
257 |
|
sl@0
|
258 |
memset((char *)&them,0,sizeof(them));
|
sl@0
|
259 |
them.sin_family=AF_INET;
|
sl@0
|
260 |
them.sin_port=htons((unsigned short)port);
|
sl@0
|
261 |
addr=(unsigned long)
|
sl@0
|
262 |
((unsigned long)ip[0]<<24L)|
|
sl@0
|
263 |
((unsigned long)ip[1]<<16L)|
|
sl@0
|
264 |
((unsigned long)ip[2]<< 8L)|
|
sl@0
|
265 |
((unsigned long)ip[3]);
|
sl@0
|
266 |
them.sin_addr.s_addr=htonl(addr);
|
sl@0
|
267 |
|
sl@0
|
268 |
if (type == SOCK_STREAM)
|
sl@0
|
269 |
s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
|
sl@0
|
270 |
else /* ( type == SOCK_DGRAM) */
|
sl@0
|
271 |
s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
|
sl@0
|
272 |
|
sl@0
|
273 |
if (s == INVALID_SOCKET) { perror("socket"); return(0); }
|
sl@0
|
274 |
|
sl@0
|
275 |
#ifndef OPENSSL_SYS_MPE
|
sl@0
|
276 |
if (type == SOCK_STREAM)
|
sl@0
|
277 |
{
|
sl@0
|
278 |
i=0;
|
sl@0
|
279 |
i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
|
sl@0
|
280 |
if (i < 0) { perror("keepalive"); return(0); }
|
sl@0
|
281 |
}
|
sl@0
|
282 |
#endif
|
sl@0
|
283 |
|
sl@0
|
284 |
if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
|
sl@0
|
285 |
{ close(s); perror("connect"); return(0); }
|
sl@0
|
286 |
*sock=s;
|
sl@0
|
287 |
return(1);
|
sl@0
|
288 |
}
|
sl@0
|
289 |
|
sl@0
|
290 |
int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context)
|
sl@0
|
291 |
{
|
sl@0
|
292 |
int sock;
|
sl@0
|
293 |
char *name = NULL;
|
sl@0
|
294 |
int accept_socket;
|
sl@0
|
295 |
int i;
|
sl@0
|
296 |
|
sl@0
|
297 |
if (!init_server(&accept_socket,port,type)) return(0);
|
sl@0
|
298 |
|
sl@0
|
299 |
if (ret != NULL)
|
sl@0
|
300 |
{
|
sl@0
|
301 |
*ret=accept_socket;
|
sl@0
|
302 |
/* return(1);*/
|
sl@0
|
303 |
}
|
sl@0
|
304 |
for (;;)
|
sl@0
|
305 |
{
|
sl@0
|
306 |
if (type==SOCK_STREAM)
|
sl@0
|
307 |
{
|
sl@0
|
308 |
if (do_accept(accept_socket,&sock,&name) == 0)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
SHUTDOWN(accept_socket);
|
sl@0
|
311 |
return(0);
|
sl@0
|
312 |
}
|
sl@0
|
313 |
}
|
sl@0
|
314 |
else
|
sl@0
|
315 |
sock = accept_socket;
|
sl@0
|
316 |
i=(*cb)(name,sock, context);
|
sl@0
|
317 |
if (name != NULL) OPENSSL_free(name);
|
sl@0
|
318 |
if (type==SOCK_STREAM)
|
sl@0
|
319 |
SHUTDOWN2(sock);
|
sl@0
|
320 |
if (i < 0)
|
sl@0
|
321 |
{
|
sl@0
|
322 |
SHUTDOWN2(accept_socket);
|
sl@0
|
323 |
return(i);
|
sl@0
|
324 |
}
|
sl@0
|
325 |
}
|
sl@0
|
326 |
}
|
sl@0
|
327 |
|
sl@0
|
328 |
static int init_server_long(int *sock, int port, char *ip, int type)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
int ret=0;
|
sl@0
|
331 |
struct sockaddr_in server;
|
sl@0
|
332 |
int s= -1,i;
|
sl@0
|
333 |
|
sl@0
|
334 |
if (!ssl_sock_init()) return(0);
|
sl@0
|
335 |
|
sl@0
|
336 |
memset((char *)&server,0,sizeof(server));
|
sl@0
|
337 |
server.sin_family=AF_INET;
|
sl@0
|
338 |
server.sin_port=htons((unsigned short)port);
|
sl@0
|
339 |
if (ip == NULL)
|
sl@0
|
340 |
server.sin_addr.s_addr=INADDR_ANY;
|
sl@0
|
341 |
else
|
sl@0
|
342 |
/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
|
sl@0
|
343 |
#ifndef BIT_FIELD_LIMITS
|
sl@0
|
344 |
memcpy(&server.sin_addr.s_addr,ip,4);
|
sl@0
|
345 |
#else
|
sl@0
|
346 |
memcpy(&server.sin_addr,ip,4);
|
sl@0
|
347 |
#endif
|
sl@0
|
348 |
|
sl@0
|
349 |
if (type == SOCK_STREAM)
|
sl@0
|
350 |
s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
|
sl@0
|
351 |
else /* type == SOCK_DGRAM */
|
sl@0
|
352 |
s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
|
sl@0
|
353 |
|
sl@0
|
354 |
if (s == INVALID_SOCKET) goto err;
|
sl@0
|
355 |
#if defined SOL_SOCKET && defined SO_REUSEADDR
|
sl@0
|
356 |
{
|
sl@0
|
357 |
int j = 1;
|
sl@0
|
358 |
setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
|
sl@0
|
359 |
(void *) &j, sizeof j);
|
sl@0
|
360 |
}
|
sl@0
|
361 |
#endif
|
sl@0
|
362 |
if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
|
sl@0
|
363 |
{
|
sl@0
|
364 |
#ifndef OPENSSL_SYS_WINDOWS
|
sl@0
|
365 |
perror("bind");
|
sl@0
|
366 |
#endif
|
sl@0
|
367 |
goto err;
|
sl@0
|
368 |
}
|
sl@0
|
369 |
/* Make it 128 for linux */
|
sl@0
|
370 |
if (type==SOCK_STREAM && listen(s,128) == -1) goto err;
|
sl@0
|
371 |
i=0;
|
sl@0
|
372 |
*sock=s;
|
sl@0
|
373 |
ret=1;
|
sl@0
|
374 |
err:
|
sl@0
|
375 |
if ((ret == 0) && (s != -1))
|
sl@0
|
376 |
{
|
sl@0
|
377 |
SHUTDOWN(s);
|
sl@0
|
378 |
}
|
sl@0
|
379 |
return(ret);
|
sl@0
|
380 |
}
|
sl@0
|
381 |
|
sl@0
|
382 |
static int init_server(int *sock, int port, int type)
|
sl@0
|
383 |
{
|
sl@0
|
384 |
return(init_server_long(sock, port, NULL, type));
|
sl@0
|
385 |
}
|
sl@0
|
386 |
|
sl@0
|
387 |
static int do_accept(int acc_sock, int *sock, char **host)
|
sl@0
|
388 |
{
|
sl@0
|
389 |
int ret,i;
|
sl@0
|
390 |
struct hostent *h1,*h2;
|
sl@0
|
391 |
static struct sockaddr_in from;
|
sl@0
|
392 |
int len;
|
sl@0
|
393 |
/* struct linger ling; */
|
sl@0
|
394 |
|
sl@0
|
395 |
if (!ssl_sock_init()) return(0);
|
sl@0
|
396 |
|
sl@0
|
397 |
#ifndef OPENSSL_SYS_WINDOWS
|
sl@0
|
398 |
redoit:
|
sl@0
|
399 |
#endif
|
sl@0
|
400 |
|
sl@0
|
401 |
memset((char *)&from,0,sizeof(from));
|
sl@0
|
402 |
len=sizeof(from);
|
sl@0
|
403 |
/* Note: under VMS with SOCKETSHR the fourth parameter is currently
|
sl@0
|
404 |
* of type (int *) whereas under other systems it is (void *) if
|
sl@0
|
405 |
* you don't have a cast it will choke the compiler: if you do
|
sl@0
|
406 |
* have a cast then you can either go for (int *) or (void *).
|
sl@0
|
407 |
*/
|
sl@0
|
408 |
ret=accept(acc_sock,(struct sockaddr *)&from,(void *)&len);
|
sl@0
|
409 |
if (ret == INVALID_SOCKET)
|
sl@0
|
410 |
{
|
sl@0
|
411 |
#if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
|
sl@0
|
412 |
i=WSAGetLastError();
|
sl@0
|
413 |
BIO_printf(bio_err,"accept error %d\n",i);
|
sl@0
|
414 |
#else
|
sl@0
|
415 |
if (errno == EINTR)
|
sl@0
|
416 |
{
|
sl@0
|
417 |
/*check_timeout(); */
|
sl@0
|
418 |
goto redoit;
|
sl@0
|
419 |
}
|
sl@0
|
420 |
fprintf(stderr,"errno=%d ",errno);
|
sl@0
|
421 |
// perror("accept");
|
sl@0
|
422 |
|
sl@0
|
423 |
#endif
|
sl@0
|
424 |
return(0);
|
sl@0
|
425 |
}
|
sl@0
|
426 |
|
sl@0
|
427 |
/*
|
sl@0
|
428 |
ling.l_onoff=1;
|
sl@0
|
429 |
ling.l_linger=0;
|
sl@0
|
430 |
i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
|
sl@0
|
431 |
if (i < 0) { perror("linger"); return(0); }
|
sl@0
|
432 |
i=0;
|
sl@0
|
433 |
i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
|
sl@0
|
434 |
if (i < 0) { perror("keepalive"); return(0); }
|
sl@0
|
435 |
*/
|
sl@0
|
436 |
|
sl@0
|
437 |
if (host == NULL) goto end;
|
sl@0
|
438 |
#ifndef BIT_FIELD_LIMITS
|
sl@0
|
439 |
/* I should use WSAAsyncGetHostByName() under windows */
|
sl@0
|
440 |
h1=gethostbyaddr((char *)&from.sin_addr.s_addr,
|
sl@0
|
441 |
sizeof(from.sin_addr.s_addr),AF_INET);
|
sl@0
|
442 |
#else
|
sl@0
|
443 |
h1=gethostbyaddr((char *)&from.sin_addr,
|
sl@0
|
444 |
sizeof(struct in_addr),AF_INET);
|
sl@0
|
445 |
#endif
|
sl@0
|
446 |
if (h1 == NULL)
|
sl@0
|
447 |
{
|
sl@0
|
448 |
BIO_printf(bio_err,"bad gethostbyaddr\n");
|
sl@0
|
449 |
*host=NULL;
|
sl@0
|
450 |
/* return(0); */
|
sl@0
|
451 |
}
|
sl@0
|
452 |
else
|
sl@0
|
453 |
{
|
sl@0
|
454 |
if ((*host=(char *)OPENSSL_malloc(strlen(h1->h_name)+1)) == NULL)
|
sl@0
|
455 |
{
|
sl@0
|
456 |
perror("OPENSSL_malloc");
|
sl@0
|
457 |
return(0);
|
sl@0
|
458 |
}
|
sl@0
|
459 |
BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
|
sl@0
|
460 |
|
sl@0
|
461 |
h2=GetHostByName(*host);
|
sl@0
|
462 |
if (h2 == NULL)
|
sl@0
|
463 |
{
|
sl@0
|
464 |
BIO_printf(bio_err,"gethostbyname failure\n");
|
sl@0
|
465 |
return(0);
|
sl@0
|
466 |
}
|
sl@0
|
467 |
i=0;
|
sl@0
|
468 |
if (h2->h_addrtype != AF_INET)
|
sl@0
|
469 |
{
|
sl@0
|
470 |
BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
|
sl@0
|
471 |
return(0);
|
sl@0
|
472 |
}
|
sl@0
|
473 |
}
|
sl@0
|
474 |
end:
|
sl@0
|
475 |
*sock=ret;
|
sl@0
|
476 |
return(1);
|
sl@0
|
477 |
}
|
sl@0
|
478 |
|
sl@0
|
479 |
int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
|
sl@0
|
480 |
short *port_ptr)
|
sl@0
|
481 |
{
|
sl@0
|
482 |
char *h,*p;
|
sl@0
|
483 |
|
sl@0
|
484 |
h=str;
|
sl@0
|
485 |
p=strchr(str,':');
|
sl@0
|
486 |
if (p == NULL)
|
sl@0
|
487 |
{
|
sl@0
|
488 |
BIO_printf(bio_err,"no port defined\n");
|
sl@0
|
489 |
return(0);
|
sl@0
|
490 |
}
|
sl@0
|
491 |
*(p++)='\0';
|
sl@0
|
492 |
|
sl@0
|
493 |
if ((ip != NULL) && !host_ip(str,ip))
|
sl@0
|
494 |
goto err;
|
sl@0
|
495 |
if (host_ptr != NULL) *host_ptr=h;
|
sl@0
|
496 |
|
sl@0
|
497 |
if (!extract_port(p,port_ptr))
|
sl@0
|
498 |
goto err;
|
sl@0
|
499 |
return(1);
|
sl@0
|
500 |
err:
|
sl@0
|
501 |
return(0);
|
sl@0
|
502 |
}
|
sl@0
|
503 |
|
sl@0
|
504 |
static int host_ip(char *str, unsigned char ip[4])
|
sl@0
|
505 |
{
|
sl@0
|
506 |
unsigned int in[4];
|
sl@0
|
507 |
int i;
|
sl@0
|
508 |
|
sl@0
|
509 |
if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
|
sl@0
|
510 |
{
|
sl@0
|
511 |
for (i=0; i<4; i++)
|
sl@0
|
512 |
if (in[i] > 255)
|
sl@0
|
513 |
{
|
sl@0
|
514 |
BIO_printf(bio_err,"invalid IP address\n");
|
sl@0
|
515 |
goto err;
|
sl@0
|
516 |
}
|
sl@0
|
517 |
ip[0]=in[0];
|
sl@0
|
518 |
ip[1]=in[1];
|
sl@0
|
519 |
ip[2]=in[2];
|
sl@0
|
520 |
ip[3]=in[3];
|
sl@0
|
521 |
}
|
sl@0
|
522 |
else
|
sl@0
|
523 |
{ /* do a gethostbyname */
|
sl@0
|
524 |
struct hostent *he;
|
sl@0
|
525 |
|
sl@0
|
526 |
if (!ssl_sock_init()) return(0);
|
sl@0
|
527 |
|
sl@0
|
528 |
he=GetHostByName(str);
|
sl@0
|
529 |
if (he == NULL)
|
sl@0
|
530 |
{
|
sl@0
|
531 |
BIO_printf(bio_err,"gethostbyname failure\n");
|
sl@0
|
532 |
goto err;
|
sl@0
|
533 |
}
|
sl@0
|
534 |
/* cast to short because of win16 winsock definition */
|
sl@0
|
535 |
if ((short)he->h_addrtype != AF_INET)
|
sl@0
|
536 |
{
|
sl@0
|
537 |
BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
|
sl@0
|
538 |
return(0);
|
sl@0
|
539 |
}
|
sl@0
|
540 |
ip[0]=he->h_addr_list[0][0];
|
sl@0
|
541 |
ip[1]=he->h_addr_list[0][1];
|
sl@0
|
542 |
ip[2]=he->h_addr_list[0][2];
|
sl@0
|
543 |
ip[3]=he->h_addr_list[0][3];
|
sl@0
|
544 |
}
|
sl@0
|
545 |
return(1);
|
sl@0
|
546 |
err:
|
sl@0
|
547 |
return(0);
|
sl@0
|
548 |
}
|
sl@0
|
549 |
|
sl@0
|
550 |
int extract_port(char *str, short *port_ptr)
|
sl@0
|
551 |
{
|
sl@0
|
552 |
int i;
|
sl@0
|
553 |
struct servent *s;
|
sl@0
|
554 |
|
sl@0
|
555 |
i=atoi(str);
|
sl@0
|
556 |
if (i != 0)
|
sl@0
|
557 |
*port_ptr=(unsigned short)i;
|
sl@0
|
558 |
else
|
sl@0
|
559 |
{
|
sl@0
|
560 |
s=getservbyname(str,"tcp");
|
sl@0
|
561 |
if (s == NULL)
|
sl@0
|
562 |
{
|
sl@0
|
563 |
BIO_printf(bio_err,"getservbyname failure for %s\n",str);
|
sl@0
|
564 |
return(0);
|
sl@0
|
565 |
}
|
sl@0
|
566 |
*port_ptr=ntohs((unsigned short)s->s_port);
|
sl@0
|
567 |
}
|
sl@0
|
568 |
return(1);
|
sl@0
|
569 |
}
|
sl@0
|
570 |
|
sl@0
|
571 |
#define GHBN_NUM 4
|
sl@0
|
572 |
static struct ghbn_cache_st
|
sl@0
|
573 |
{
|
sl@0
|
574 |
char name[128];
|
sl@0
|
575 |
struct hostent ent;
|
sl@0
|
576 |
unsigned long order;
|
sl@0
|
577 |
} ghbn_cache[GHBN_NUM];
|
sl@0
|
578 |
|
sl@0
|
579 |
static unsigned long ghbn_hits=0L;
|
sl@0
|
580 |
static unsigned long ghbn_miss=0L;
|
sl@0
|
581 |
|
sl@0
|
582 |
static struct hostent *GetHostByName(char *name)
|
sl@0
|
583 |
{
|
sl@0
|
584 |
struct hostent *ret;
|
sl@0
|
585 |
int i,lowi=0;
|
sl@0
|
586 |
unsigned long low= (unsigned long)-1;
|
sl@0
|
587 |
|
sl@0
|
588 |
for (i=0; i<GHBN_NUM; i++)
|
sl@0
|
589 |
{
|
sl@0
|
590 |
if (low > ghbn_cache[i].order)
|
sl@0
|
591 |
{
|
sl@0
|
592 |
low=ghbn_cache[i].order;
|
sl@0
|
593 |
lowi=i;
|
sl@0
|
594 |
}
|
sl@0
|
595 |
if (ghbn_cache[i].order > 0)
|
sl@0
|
596 |
{
|
sl@0
|
597 |
if (strncmp(name,ghbn_cache[i].name,128) == 0)
|
sl@0
|
598 |
break;
|
sl@0
|
599 |
}
|
sl@0
|
600 |
}
|
sl@0
|
601 |
if (i == GHBN_NUM) /* no hit*/
|
sl@0
|
602 |
{
|
sl@0
|
603 |
ghbn_miss++;
|
sl@0
|
604 |
ret=gethostbyname(name);
|
sl@0
|
605 |
if (ret == NULL) return(NULL);
|
sl@0
|
606 |
/* else add to cache */
|
sl@0
|
607 |
if(strlen(name) < sizeof ghbn_cache[0].name)
|
sl@0
|
608 |
{
|
sl@0
|
609 |
strcpy(ghbn_cache[lowi].name,name);
|
sl@0
|
610 |
memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent));
|
sl@0
|
611 |
ghbn_cache[lowi].order=ghbn_miss+ghbn_hits;
|
sl@0
|
612 |
}
|
sl@0
|
613 |
return(ret);
|
sl@0
|
614 |
}
|
sl@0
|
615 |
else
|
sl@0
|
616 |
{
|
sl@0
|
617 |
ghbn_hits++;
|
sl@0
|
618 |
ret= &(ghbn_cache[i].ent);
|
sl@0
|
619 |
ghbn_cache[i].order=ghbn_miss+ghbn_hits;
|
sl@0
|
620 |
return(ret);
|
sl@0
|
621 |
}
|
sl@0
|
622 |
}
|
sl@0
|
623 |
|
sl@0
|
624 |
#endif
|