75 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
77 typedef unsigned int u_int;
94 #ifndef OPENSSL_NO_SOCK
96 #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
100 static struct hostent *GetHostByName(
char *name);
101 #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
102 static void ssl_sock_cleanup(
void);
104 static int ssl_sock_init(
void);
105 static int init_client_ip(
int *sock,
unsigned char ip[4],
int port,
int type);
106 static int init_server(
int *sock,
int port,
int type);
107 static int init_server_long(
int *sock,
int port,
char *ip,
int type);
108 static int do_accept(
int acc_sock,
int *sock,
char **host);
109 static int host_ip(
char *str,
unsigned char ip[4]);
111 #ifdef OPENSSL_SYS_WIN16
112 #define SOCKET_PROTOCOL 0
114 #define SOCKET_PROTOCOL IPPROTO_TCP
117 #if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
118 static int wsa_init_done=0;
121 #ifdef OPENSSL_SYS_WINDOWS
122 static struct WSAData wsa_state;
123 static int wsa_init_done=0;
125 #ifdef OPENSSL_SYS_WIN16
126 static HWND topWnd=0;
127 static FARPROC lpTopWndProc=NULL;
128 static FARPROC lpTopHookProc=NULL;
129 extern HINSTANCE _hInstance;
131 static LONG
FAR PASCAL topHookProc(HWND hwnd, UINT message, WPARAM wParam,
140 SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopWndProc);
145 return CallWindowProc(lpTopWndProc,hwnd,message,wParam,lParam);
148 static BOOL CALLBACK enumproc(HWND hwnd,LPARAM lParam)
157 #ifdef OPENSSL_SYS_WINDOWS
158 static void ssl_sock_cleanup(
void)
163 #ifndef OPENSSL_SYS_WINCE
164 WSACancelBlockingCall();
169 #elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
170 static void sock_cleanup(
void)
180 static int ssl_sock_init(
void)
183 extern int _watt_do_exit;
187 #elif defined(OPENSSL_SYS_WINDOWS)
193 signal(SIGINT,(
void (*)(
int))ssl_sock_cleanup);
196 memset(&wsa_state,0,
sizeof(wsa_state));
197 if (WSAStartup(0x0101,&wsa_state)!=0)
199 err=WSAGetLastError();
204 #ifdef OPENSSL_SYS_WIN16
205 EnumTaskWindows(GetCurrentTask(),enumproc,0L);
206 lpTopWndProc=(FARPROC)GetWindowLong(topWnd,GWL_WNDPROC);
207 lpTopHookProc=MakeProcInstance((FARPROC)topHookProc,_hInstance);
209 SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopHookProc);
212 #elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
221 signal(SIGINT,(
void (*)(
int))sock_cleanup);
225 wVerReq = MAKEWORD( 2, 0 );
226 err = WSAStartup(wVerReq,&wsaData);
241 memset(ip,
'\0',
sizeof ip);
242 if (!host_ip(host,&(ip[0])))
244 return init_client_ip(sock,ip,port,type);
247 static int init_client_ip(
int *sock,
unsigned char ip[4],
int port,
int type)
250 struct sockaddr_in them;
253 if (!ssl_sock_init())
return(0);
255 memset((
char *)&them,0,
sizeof(them));
256 them.sin_family=AF_INET;
257 them.sin_port=htons((
unsigned short)port);
259 ((
unsigned long)ip[0]<<24L)|
260 ((
unsigned long)ip[1]<<16L)|
261 ((
unsigned long)ip[2]<< 8L)|
262 ((
unsigned long)ip[3]);
263 them.sin_addr.s_addr=htonl(addr);
265 if (type == SOCK_STREAM)
268 s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
270 if (s == INVALID_SOCKET) { perror(
"socket");
return(0); }
272 #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
273 if (type == SOCK_STREAM)
276 i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(
char *)&i,
sizeof(i));
277 if (i < 0) { perror(
"keepalive");
return(0); }
281 if (connect(s,(
struct sockaddr *)&them,
sizeof(them)) == -1)
287 int do_server(
int port,
int type,
int *ret,
int (*
cb)(
char *hostname,
int s,
unsigned char *context),
unsigned char *context)
291 int accept_socket = 0;
294 if (!init_server(&accept_socket,port,type))
return(0);
303 if (type==SOCK_STREAM)
305 if (do_accept(accept_socket,&sock,&name) == 0)
307 SHUTDOWN(accept_socket);
312 sock = accept_socket;
313 i=(*cb)(name,sock, context);
315 if (type==SOCK_STREAM)
319 SHUTDOWN2(accept_socket);
325 static int init_server_long(
int *sock,
int port,
char *ip,
int type)
328 struct sockaddr_in server;
331 if (!ssl_sock_init())
return(0);
333 memset((
char *)&server,0,
sizeof(server));
334 server.sin_family=AF_INET;
335 server.sin_port=htons((
unsigned short)port);
337 server.sin_addr.s_addr=INADDR_ANY;
340 #ifndef BIT_FIELD_LIMITS
341 memcpy(&server.sin_addr.s_addr,ip,4);
343 memcpy(&server.sin_addr,ip,4);
346 if (type == SOCK_STREAM)
349 s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
351 if (s == INVALID_SOCKET)
goto err;
352 #if defined SOL_SOCKET && defined SO_REUSEADDR
355 setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
356 (
void *) &j,
sizeof j);
359 if (bind(s,(
struct sockaddr *)&server,
sizeof(server)) == -1)
361 #ifndef OPENSSL_SYS_WINDOWS
367 if (type==SOCK_STREAM && listen(s,128) == -1)
goto err;
371 if ((ret == 0) && (s != -1))
378 static int init_server(
int *sock,
int port,
int type)
380 return(init_server_long(sock, port, NULL, type));
383 static int do_accept(
int acc_sock,
int *sock,
char **host)
386 struct hostent *h1,*h2;
387 static struct sockaddr_in from;
391 if (!ssl_sock_init())
return(0);
393 #ifndef OPENSSL_SYS_WINDOWS
397 memset((
char *)&from,0,
sizeof(from));
404 ret=accept(acc_sock,(
struct sockaddr *)&from,(
void *)&len);
405 if (ret == INVALID_SOCKET)
407 #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
417 fprintf(stderr,
"errno=%d ",errno);
433 if (host == NULL)
goto end;
434 #ifndef BIT_FIELD_LIMITS
436 h1=gethostbyaddr((
char *)&from.sin_addr.s_addr,
437 sizeof(from.sin_addr.s_addr),AF_INET);
439 h1=gethostbyaddr((
char *)&from.sin_addr,
440 sizeof(
struct in_addr),AF_INET);
452 perror(
"OPENSSL_malloc");
455 BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
457 h2=GetHostByName(*host);
463 if (h2->h_addrtype != AF_INET)
488 if ((ip != NULL) && !host_ip(str,ip))
490 if (host_ptr != NULL) *host_ptr=
h;
499 static int host_ip(
char *str,
unsigned char ip[4])
504 if (sscanf(str,
"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
521 if (!ssl_sock_init())
return(0);
523 he=GetHostByName(str);
530 if ((
short)he->h_addrtype != AF_INET)
535 ip[0]=he->h_addr_list[0][0];
536 ip[1]=he->h_addr_list[0][1];
537 ip[2]=he->h_addr_list[0][2];
538 ip[3]=he->h_addr_list[0][3];
552 *port_ptr=(
unsigned short)i;
555 s=getservbyname(str,
"tcp");
561 *port_ptr=ntohs((
unsigned short)s->s_port);
567 static struct ghbn_cache_st
574 static unsigned long ghbn_hits=0L;
575 static unsigned long ghbn_miss=0L;
577 static struct hostent *GetHostByName(
char *name)
581 unsigned long low= (
unsigned long)-1;
585 if (low > ghbn_cache[i].order)
587 low=ghbn_cache[i].order;
590 if (ghbn_cache[i].order > 0)
592 if (strncmp(name,ghbn_cache[i].name,128) == 0)
599 ret=gethostbyname(name);
600 if (ret == NULL)
return(NULL);
602 if(strlen(name) <
sizeof ghbn_cache[0].name)
604 strcpy(ghbn_cache[lowi].name,name);
605 memcpy((
char *)&(ghbn_cache[lowi].ent),ret,
sizeof(
struct hostent));
606 ghbn_cache[lowi].order=ghbn_miss+ghbn_hits;
613 ret= &(ghbn_cache[i].ent);
614 ghbn_cache[i].order=ghbn_miss+ghbn_hits;