PR: 2047
[openssl.git] / crypto / bio / b_sock.c
index c8ca5923c114c4f8a16f0d25d181b1d692cc8a48..57aec21f7f5f510b78019e7d7e284ab3b35dce2f 100644 (file)
 #include "cryptlib.h"
 #include <openssl/bio.h>
 #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
-#include "netdb.h"
+#include <netdb.h>
+#if defined(NETWARE_CLIB)
+#include <sys/ioctl.h>
+NETDB_DEFINE_CONTEXT
+#endif
 #endif
 
 #ifndef OPENSSL_NO_SOCK
 static int wsa_init_done=0;
 #endif
 
-#if defined(OPENSSL_SYS_BEOS_BONE)             
-/* BONE's IP6 support is incomplete */
-#undef AF_INET6
-#endif
-
 #if 0
 static unsigned long BIO_ghbn_hits=0L;
 static unsigned long BIO_ghbn_miss=0L;
@@ -181,11 +180,11 @@ int BIO_get_port(const char *str, unsigned short *port_ptr)
                /* Note: under VMS with SOCKETSHR, it seems like the first
                 * parameter is 'char *', instead of 'const char *'
                 */
-               s=getservbyname(
 #ifndef CONST_STRICT
-                   (char *)
+               s=getservbyname((char *)str,"tcp");
+#else
+               s=getservbyname(str,"tcp");
 #endif
-                   str,"tcp");
                if(s != NULL)
                        *port_ptr=ntohs((unsigned short)s->s_port);
                CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
@@ -367,7 +366,11 @@ struct hostent *BIO_gethostbyname(const char *name)
 #if 1
        /* Caching gethostbyname() results forever is wrong,
         * so we have to let the true gethostbyname() worry about this */
+#if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
+       return gethostbyname((char*)name);
+#else
        return gethostbyname(name);
+#endif
 #else
        struct hostent *ret;
        int i,lowi=0,j;
@@ -407,11 +410,11 @@ struct hostent *BIO_gethostbyname(const char *name)
                /* Note: under VMS with SOCKETSHR, it seems like the first
                 * parameter is 'char *', instead of 'const char *'
                 */
-               ret=gethostbyname(
 #  ifndef CONST_STRICT
-                   (char *)
+               ret=gethostbyname((char *)name);
+#  else
+               ret=gethostbyname(name);
 #  endif
-                   name);
 
                if (ret == NULL)
                        goto end;
@@ -463,9 +466,6 @@ int BIO_sock_init(void)
                {
                int err;
          
-#ifdef SIGINT
-               signal(SIGINT,(void (*)(int))BIO_sock_cleanup);
-#endif
                wsa_init_done=1;
                memset(&wsa_state,0,sizeof(wsa_state));
                /* Not making wsa_state available to the rest of the
@@ -496,11 +496,6 @@ int BIO_sock_init(void)
 
     if (!wsa_init_done)
     {
-   
-# ifdef SIGINT
-        signal(SIGINT,(void (*)(int))BIO_sock_cleanup);
-# endif
-
         wsa_init_done=1;
         wVerReq = MAKEWORD( 2, 0 );
         err = WSAStartup(wVerReq,&wsaData);
@@ -522,7 +517,7 @@ void BIO_sock_cleanup(void)
        if (wsa_init_done)
                {
                wsa_init_done=0;
-#ifndef OPENSSL_SYS_WINCE
+#if 0          /* this call is claimed to be non-present in Winsock2 */
                WSACancelBlockingCall();
 #endif
                WSACleanup();
@@ -593,7 +588,13 @@ static int get_ip(const char *str, unsigned char ip[4])
 int BIO_get_accept_socket(char *host, int bind_mode)
        {
        int ret=0;
+#if OPENSSL_USE_IPV6
+# define ossl_sock_family(s) s.ss_family
+       struct sockaddr_storage server,client;
+#else
+# define ossl_sock_family(s) s.sa_family
        struct sockaddr server,client;
+#endif
        struct sockaddr_in *sa_in;
        int s=INVALID_SOCKET,cs;
        unsigned char ip[4];
@@ -654,7 +655,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
                if (strchr(h,':'))
                        {
                        if (h[1]=='\0') h=NULL;
-#ifdef AF_INET6
+#if OPENSSL_USE_IPV6
                        hint.ai_family = AF_INET6;
 #else
                        h=NULL;
@@ -665,7 +666,11 @@ int BIO_get_accept_socket(char *host, int bind_mode)
                }
 
        if ((*p_getaddrinfo.f)(h,p,&hint,&res)) break;
+#if OPENSSL_USE_IPV6
+       memcpy(&server, res->ai_addr, res->ai_addrlen);
+#else
        server = *res->ai_addr;
+#endif
        (*p_freeaddrinfo.f)(res);
        goto again;
        } while (0);
@@ -692,7 +697,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
                }
 
 again:
-       s=socket(server.sa_family,SOCK_STREAM,SOCKET_PROTOCOL);
+       s=socket(ossl_sock_family(server),SOCK_STREAM,SOCKET_PROTOCOL);
        if (s == INVALID_SOCKET)
                {
                SYSerr(SYS_F_SOCKET,get_last_socket_error());
@@ -710,7 +715,7 @@ again:
                bind_mode=BIO_BIND_NORMAL;
                }
 #endif
-       if (bind(s,&server,sizeof(server)) == -1)
+       if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1)
                {
 #ifdef SO_REUSEADDR
                err_num=get_last_socket_error();
@@ -720,8 +725,8 @@ again:
                        client = server;
                        if (h == NULL || strcmp(h,"*") == 0)
                                {
-#ifdef AF_INET6
-                               if (client.sa_family == AF_INET6)
+#if OPENSSL_USE_IPV6
+                               if (ossl_sock_family(client) == AF_INET6)
                                        {
                                        struct sockaddr_in6 *sin6 =
                                                (struct sockaddr_in6 *)&client;
@@ -730,15 +735,15 @@ again:
                                        }
                                else
 #endif
-                               if (client.sa_family == AF_INET)
+                               if (ossl_sock_family(client) == AF_INET)
                                        {
-                                       struct sockaddr_in *sin6 =
+                                       struct sockaddr_in *sin4 =
                                                (struct sockaddr_in *)&client;
-                                       sin6->sin_addr.s_addr=htonl(0x7F000001);
+                                       sin4->sin_addr.s_addr=htonl(0x7F000001);
                                        }
                                else    goto err;
                                }
-                       cs=socket(client.sa_family,SOCK_STREAM,SOCKET_PROTOCOL);
+                       cs=socket(ossl_sock_family(client),SOCK_STREAM,SOCKET_PROTOCOL);
                        if (cs != INVALID_SOCKET)
                                {
                                int ii;
@@ -808,18 +813,18 @@ int BIO_accept(int sock, char **addr)
        if (addr == NULL) goto end;
 
 #ifdef EAI_FAMILY
-# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_BEOS_BONE)
-#  define SOCKLEN_T size_t
-# else
-#  define SOCKLEN_T socklen_t
-#endif
        do {
        char   h[NI_MAXHOST],s[NI_MAXSERV];
        size_t nl;
        static union {  void *p;
-                       int (*f)(const struct sockaddr *,SOCKLEN_T,
+                       int (*f)(const struct sockaddr *,size_t/*socklen_t*/,
                                 char *,size_t,char *,size_t,int);
                        } p_getnameinfo = {NULL};
+                       /* 2nd argument to getnameinfo is specified to
+                        * be socklen_t. Unfortunately there is a number
+                        * of environments where socklen_t is not defined.
+                        * As it's passed by value, it's safe to pass it
+                        * as size_t... <appro> */
 
        if (p_getnameinfo.p==NULL)
                {