-#ifdef WINDOWS
-static struct WSAData wsa_state;
-static int wsa_init_done=0;
-
-#ifdef WIN16
-static HWND topWnd=0;
-static FARPROC lpTopWndProc=NULL;
-static FARPROC lpTopHookProc=NULL;
-extern HINSTANCE _hInstance; /* nice global CRT provides */
-
-static LONG FAR PASCAL topHookProc(HWND hwnd, UINT message, WPARAM wParam,
- LPARAM lParam)
- {
- if (hwnd == topWnd)
- {
- switch(message)
- {
- case WM_DESTROY:
- case WM_CLOSE:
- SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopWndProc);
- sock_cleanup();
- break;
- }
- }
- return CallWindowProc(lpTopWndProc,hwnd,message,wParam,lParam);
- }
-
-static BOOL CALLBACK enumproc(HWND hwnd,LPARAM lParam)
- {
- topWnd=hwnd;
- return(FALSE);
- }
-
-#endif /* WIN32 */
-#endif /* WINDOWS */
-
-void sock_cleanup(void)
- {
-#ifdef WINDOWS
- if (wsa_init_done)
- {
- wsa_init_done=0;
- WSACancelBlockingCall();
- WSACleanup();
- }
-#endif
- }
-
-int sock_init(void)
- {
-#ifdef WINDOWS
- if (!wsa_init_done)
- {
- int err;
-
-#ifdef SIGINT
- signal(SIGINT,(void (*)(int))sock_cleanup);
-#endif
- wsa_init_done=1;
- memset(&wsa_state,0,sizeof(wsa_state));
- if (WSAStartup(0x0101,&wsa_state)!=0)
- {
- err=WSAGetLastError();
- BIO_printf(bio_err,"unable to start WINSOCK, error code=%d\n",err);
- return(0);
- }
-
-#ifdef WIN16
- EnumTaskWindows(GetCurrentTask(),enumproc,0L);
- lpTopWndProc=(FARPROC)GetWindowLong(topWnd,GWL_WNDPROC);
- lpTopHookProc=MakeProcInstance((FARPROC)topHookProc,_hInstance);
-
- SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopHookProc);
-#endif /* WIN16 */
- }
-#endif /* WINDOWS */
- return(1);
- }
-
-int init_client(int *sock, char *host, int port)
- {
- unsigned char ip[4];
- short p=0;
-
- if (!host_ip(host,&(ip[0])))
- {
- return(0);
- }
- if (p != 0) port=p;
- return(init_client_ip(sock,ip,port));
- }
-
-int init_client_ip(int *sock, unsigned char ip[4], int port)
- {
- unsigned long addr;
- struct sockaddr_in them;
- int s,i;
-
- if (!sock_init()) return(0);
-
- memset((char *)&them,0,sizeof(them));
- them.sin_family=AF_INET;
- them.sin_port=htons((unsigned short)port);
- addr=(unsigned long)
- ((unsigned long)ip[0]<<24L)|
- ((unsigned long)ip[1]<<16L)|
- ((unsigned long)ip[2]<< 8L)|
- ((unsigned long)ip[3]);
- them.sin_addr.s_addr=htonl(addr);
-
- s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
- if (s == INVALID_SOCKET) { perror("socket"); return(0); }
-
- i=0;
- i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
- if (i < 0) { perror("keepalive"); return(0); }
-
- if (connect(s,(struct sockaddr *)&them,sizeof(them)) == -1)
- { close(s); perror("connect"); return(0); }
- *sock=s;
- return(1);
- }
-
-int nbio_sock_error(int sock)
- {
- int j,i,size;
-
- size=sizeof(int);
- i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(char *)&j,&size);
- if (i < 0)
- return(1);
- else
- return(j);
- }
-
-int nbio_init_client_ip(int *sock, unsigned char ip[4], int port)
- {
- unsigned long addr;
- struct sockaddr_in them;
- int s,i;
-
- if (!sock_init()) return(0);
-
- memset((char *)&them,0,sizeof(them));
- them.sin_family=AF_INET;
- them.sin_port=htons((unsigned short)port);
- addr= (unsigned long)
- ((unsigned long)ip[0]<<24L)|
- ((unsigned long)ip[1]<<16L)|
- ((unsigned long)ip[2]<< 8L)|
- ((unsigned long)ip[3]);
- them.sin_addr.s_addr=htonl(addr);
-
- if (*sock <= 0)
- {
- unsigned long l=1;
-
- s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
- if (s == INVALID_SOCKET) { perror("socket"); return(0); }
-
- i=0;
- i=setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
- if (i < 0) { perror("keepalive"); return(0); }
- *sock=s;
-
-#ifdef FIONBIO
- BIO_socket_ioctl(s,FIONBIO,&l);
-#endif
- }
- else
- s= *sock;
-
- i=connect(s,(struct sockaddr *)&them,sizeof(them));
- if (i == INVALID_SOCKET)
- {
- if (BIO_sock_should_retry(i))
- return(-1);
- else
- return(0);
- }
- else
- return(1);
- }
-
-int do_server(int port, int *ret, int (*cb)(), char *context)
- {
- int sock;
- char *name;
- int accept_socket;
- int i;
-
- if (!init_server(&accept_socket,port)) return(0);
-
- if (ret != NULL)
- {
- *ret=accept_socket;
- /* return(1);*/
- }
- for (;;)
- {
- if (do_accept(accept_socket,&sock,&name) == 0)
- {
- SHUTDOWN(accept_socket);
- return(0);
- }
- i=(*cb)(name,sock, context);
- if (name != NULL) Free(name);
- SHUTDOWN2(sock);
- if (i < 0)
- {
- SHUTDOWN2(accept_socket);
- return(i);
- }
- }
- }
-
-int init_server_long(int *sock, int port, char *ip)
- {
- int ret=0;
- struct sockaddr_in server;
- int s= -1,i;
-
- if (!sock_init()) return(0);
-
- memset((char *)&server,0,sizeof(server));
- server.sin_family=AF_INET;
- server.sin_port=htons((unsigned short)port);
- if (ip == NULL)
- server.sin_addr.s_addr=INADDR_ANY;
- else
-/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
-#ifndef BIT_FIELD_LIMITS
- memcpy(&server.sin_addr.s_addr,ip,4);
-#else
- memcpy(&server.sin_addr,ip,4);
-#endif
- s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
-
- if (s == INVALID_SOCKET) goto err;
-#if defined SOL_SOCKET && defined SO_REUSEADDR
- {
- int j = 1;
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
- (const void *) &j, sizeof j);
- }
-#endif
- if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
- {
-#ifndef WINDOWS
- perror("bind");
-#endif
- goto err;
- }
- /* Make it 128 for linux */
- if (listen(s,128) == -1) goto err;
- i=0;
- *sock=s;
- ret=1;
-err:
- if ((ret == 0) && (s != -1))
- {
- SHUTDOWN(s);
- }
- return(ret);
- }
-
-int init_server(int *sock, int port)
- {
- return(init_server_long(sock, port, NULL));
- }
-
-int do_accept(int acc_sock, int *sock, char **host)
- {
- int ret,i;
- struct hostent *h1,*h2;
- static struct sockaddr_in from;
- int len;
-/* struct linger ling; */
-
- if (!sock_init()) return(0);
-
-#ifndef WINDOWS
-redoit:
-#endif