signed vs. unsigned.
[openssl.git] / apps / s_socket.c
index adaeef646a9e0663d4ac594c57287d75dbdb3e57..cf43301df2b5798740d5a6cbadd5f7bdce993c22 100644 (file)
 #include <errno.h>
 #include <signal.h>
 
+/* With IPv6, it looks like Digital has mixed up the proper order of
+   recursive header file inclusion, resulting in the compiler complaining
+   that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
+   is needed to have fileno() declared correctly...  So let's define u_int */
+#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
+#define __U_INT
+typedef unsigned int u_int;
+#endif
+
 #define USE_SOCKETS
 #define NON_MAIN
 #include "apps.h"
 #undef NON_MAIN
 #include "s_apps.h"
 #include <openssl/ssl.h>
-#include <openssl/e_os2.h>
 
-/* With IPv6, it looks like Digital has mixed up the proper order of
-   recursive header file inclusion, resulting in the compiler complaining
-   that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
-   is needed to have fileno() declared correctly...  So let's define u_int */
-#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
-#define __U_INT
-typedef unsigned int u_int;
+#ifdef FLAT_INC
+#include "e_os.h"
+#else
+#include "../e_os.h"
 #endif
 
+#ifndef OPENSSL_NO_SOCK
+
 static struct hostent *GetHostByName(char *name);
-#ifdef OPENSSL_SYS_WINDOWS
-static void sock_cleanup(void);
+#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
+static void ssl_sock_cleanup(void);
 #endif
-static int sock_init(void);
+static int ssl_sock_init(void);
 static int init_client_ip(int *sock,unsigned char ip[4], int port);
 static int init_server(int *sock, int port);
 static int init_server_long(int *sock, int port,char *ip);
@@ -97,6 +104,10 @@ static int host_ip(char *str, unsigned char ip[4]);
 #define SOCKET_PROTOCOL        IPPROTO_TCP
 #endif
 
+#ifdef OPENSSL_SYS_NETWARE
+static int wsa_init_done=0;
+#endif
+
 #ifdef OPENSSL_SYS_WINDOWS
 static struct WSAData wsa_state;
 static int wsa_init_done=0;
@@ -117,7 +128,7 @@ static LONG FAR PASCAL topHookProc(HWND hwnd, UINT message, WPARAM wParam,
                case WM_DESTROY:
                case WM_CLOSE:
                        SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopWndProc);
-                       sock_cleanup();
+                       ssl_sock_cleanup();
                        break;
                        }
                }
@@ -134,26 +145,42 @@ static BOOL CALLBACK enumproc(HWND hwnd,LPARAM lParam)
 #endif /* OPENSSL_SYS_WINDOWS */
 
 #ifdef OPENSSL_SYS_WINDOWS
-static void sock_cleanup(void)
+static void ssl_sock_cleanup(void)
        {
        if (wsa_init_done)
                {
                wsa_init_done=0;
+#ifndef OPENSSL_SYS_WINCE
                WSACancelBlockingCall();
+#endif
+               WSACleanup();
+               }
+       }
+#elif defined(OPENSSL_SYS_NETWARE)
+static void sock_cleanup(void)
+    {
+    if (wsa_init_done)
+        {
+        wsa_init_done=0;
                WSACleanup();
                }
        }
 #endif
 
-static int sock_init(void)
+static int ssl_sock_init(void)
        {
-#ifdef OPENSSL_SYS_WINDOWS
+#ifdef WATT32
+       extern int _watt_do_exit;
+       _watt_do_exit = 0;
+       if (sock_init())
+               return (0);
+#elif defined(OPENSSL_SYS_WINDOWS)
        if (!wsa_init_done)
                {
                int err;
          
 #ifdef SIGINT
-               signal(SIGINT,(void (*)(int))sock_cleanup);
+               signal(SIGINT,(void (*)(int))ssl_sock_cleanup);
 #endif
                wsa_init_done=1;
                memset(&wsa_state,0,sizeof(wsa_state));
@@ -172,6 +199,27 @@ static int sock_init(void)
                SetWindowLong(topWnd,GWL_WNDPROC,(LONG)lpTopHookProc);
 #endif /* OPENSSL_SYS_WIN16 */
                }
+#elif defined(OPENSSL_SYS_NETWARE)
+   WORD wVerReq;
+   WSADATA wsaData;
+   int err;
+
+   if (!wsa_init_done)
+      {
+   
+# ifdef SIGINT
+      signal(SIGINT,(void (*)(int))sock_cleanup);
+# endif
+
+      wsa_init_done=1;
+      wVerReq = MAKEWORD( 2, 0 );
+      err = WSAStartup(wVerReq,&wsaData);
+      if (err != 0)
+         {
+         BIO_printf(bio_err,"unable to start WINSOCK2, error code=%d\n",err);
+         return(0);
+         }
+      }
 #endif /* OPENSSL_SYS_WINDOWS */
        return(1);
        }
@@ -195,7 +243,7 @@ static int init_client_ip(int *sock, unsigned char ip[4], int port)
        struct sockaddr_in them;
        int s,i;
 
-       if (!sock_init()) return(0);
+       if (!ssl_sock_init()) return(0);
 
        memset((char *)&them,0,sizeof(them));
        them.sin_family=AF_INET;
@@ -222,7 +270,7 @@ static int init_client_ip(int *sock, unsigned char ip[4], int port)
        return(1);
        }
 
-int do_server(int port, int *ret, int (*cb)(), char *context)
+int do_server(int port, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), char *context)
        {
        int sock;
        char *name;
@@ -243,7 +291,7 @@ int do_server(int port, int *ret, int (*cb)(), char *context)
                        SHUTDOWN(accept_socket);
                        return(0);
                        }
-               i=(*cb)(name,sock, context);
+               i=(*cb)(name,sock, (unsigned char *)context);
                if (name != NULL) OPENSSL_free(name);
                SHUTDOWN2(sock);
                if (i < 0)
@@ -260,7 +308,7 @@ static int init_server_long(int *sock, int port, char *ip)
        struct sockaddr_in server;
        int s= -1,i;
 
-       if (!sock_init()) return(0);
+       if (!ssl_sock_init()) return(0);
 
        memset((char *)&server,0,sizeof(server));
        server.sin_family=AF_INET;
@@ -317,7 +365,7 @@ static int do_accept(int acc_sock, int *sock, char **host)
        int len;
 /*     struct linger ling; */
 
-       if (!sock_init()) return(0);
+       if (!ssl_sock_init()) return(0);
 
 #ifndef OPENSSL_SYS_WINDOWS
 redoit:
@@ -333,7 +381,7 @@ redoit:
        ret=accept(acc_sock,(struct sockaddr *)&from,(void *)&len);
        if (ret == INVALID_SOCKET)
                {
-#ifdef OPENSSL_SYS_WINDOWS
+#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_NETWARE)
                i=WSAGetLastError();
                BIO_printf(bio_err,"accept error %d\n",i);
 #else
@@ -380,7 +428,7 @@ redoit:
                        perror("OPENSSL_malloc");
                        return(0);
                        }
-               strcpy(*host,h1->h_name);
+               BUF_strlcpy(*host,h1->h_name,strlen(h1->h_name)+1);
 
                h2=GetHostByName(*host);
                if (h2 == NULL)
@@ -447,7 +495,7 @@ static int host_ip(char *str, unsigned char ip[4])
                { /* do a gethostbyname */
                struct hostent *he;
 
-               if (!sock_init()) return(0);
+               if (!ssl_sock_init()) return(0);
 
                he=GetHostByName(str);
                if (he == NULL)
@@ -528,9 +576,12 @@ static struct hostent *GetHostByName(char *name)
                ret=gethostbyname(name);
                if (ret == NULL) return(NULL);
                /* else add to cache */
-               strncpy(ghbn_cache[lowi].name,name,128);
-               memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent));
-               ghbn_cache[lowi].order=ghbn_miss+ghbn_hits;
+               if(strlen(name) < sizeof ghbn_cache[0].name)
+                       {
+                       strcpy(ghbn_cache[lowi].name,name);
+                       memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent));
+                       ghbn_cache[lowi].order=ghbn_miss+ghbn_hits;
+                       }
                return(ret);
                }
        else
@@ -541,3 +592,5 @@ static struct hostent *GetHostByName(char *name)
                return(ret);
                }
        }
+
+#endif