Workaround for some CMS signature formats.
[oweals/openssl.git] / crypto / bio / b_sock.c
index 8a69e99f82b2c411aab830bc778185c9b2faed0f..a026b3e0b02c0370f2be61f95a003afd6d70bc75 100644 (file)
@@ -233,13 +233,14 @@ int BIO_get_port(const char *str, unsigned short *port_ptr)
 int BIO_sock_error(int sock)
        {
        int j,i;
-       int size;
+       union { size_t s; int i; } size;
                 
 #if defined(OPENSSL_SYS_BEOS_R5)
        return 0;
 #endif
-                
-       size=sizeof(int);
+       /* heuristic way to adapt for platforms that expect 64-bit optlen */
+       size.s=0, size.i=sizeof(j);
        /* Note: under Windows the third parameter is of type (char *)
         * whereas under other systems it is (void *) if you don't have
         * a cast it will choke the compiler: if you do have a cast then
@@ -551,7 +552,30 @@ int BIO_socket_ioctl(int fd, long type, void *arg)
 #ifdef __DJGPP__
        i=ioctlsocket(fd,type,(char *)arg);
 #else
-       i=ioctlsocket(fd,type,arg);
+# if defined(OPENSSL_SYS_VMS)
+       /* 2011-02-18 SMS.
+        * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
+        * observe that all the consumers pass in an "unsigned long *",
+        * so we arrange a local copy with a short pointer, and use
+        * that, instead.
+        */
+#  if __INITIAL_POINTER_SIZE == 64
+#   define ARG arg_32p
+#   pragma pointer_size save
+#   pragma pointer_size 32
+       unsigned long arg_32;
+       unsigned long *arg_32p;
+#   pragma pointer_size restore
+       arg_32p = &arg_32;
+       arg_32 = *((unsigned long *) arg);
+#  else /* __INITIAL_POINTER_SIZE == 64 */
+#   define ARG arg
+#  endif /* __INITIAL_POINTER_SIZE == 64 [else] */
+# else /* defined(OPENSSL_SYS_VMS) */
+#  define ARG arg
+# endif /* defined(OPENSSL_SYS_VMS) [else] */
+
+       i=ioctlsocket(fd,type,ARG);
 #endif /* __DJGPP__ */
        if (i < 0)
                SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error());
@@ -606,7 +630,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
                struct sockaddr_in6 sa_in6;
 #endif
        } server,client;
-       int s=INVALID_SOCKET,cs;
+       int s=INVALID_SOCKET,cs,addrlen;
        unsigned char ip[4];
        unsigned short port;
        char *str=NULL,*e;
@@ -660,6 +684,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
         * note that commonly IPv6 wildchard socket can service
         * IPv4 connections just as well...  */
        memset(&hint,0,sizeof(hint));
+       hint.ai_flags = AI_PASSIVE;
        if (h)
                {
                if (strchr(h,':'))
@@ -672,13 +697,18 @@ int BIO_get_accept_socket(char *host, int bind_mode)
 #endif
                        }
                else if (h[0]=='*' && h[1]=='\0')
+                       {
+                       hint.ai_family = AF_INET;
                        h=NULL;
+                       }
                }
 
        if ((*p_getaddrinfo.f)(h,p,&hint,&res)) break;
 
-       memcpy(&server, res->ai_addr,
-               res->ai_addrlen<=sizeof(server)?res->ai_addrlen:sizeof(server));
+       addrlen = res->ai_addrlen<=sizeof(server) ?
+                       res->ai_addrlen :
+                       sizeof(server);
+       memcpy(&server, res->ai_addr, addrlen);
 
        (*p_freeaddrinfo.f)(res);
        goto again;
@@ -690,6 +720,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
        memset((char *)&server,0,sizeof(server));
        server.sa_in.sin_family=AF_INET;
        server.sa_in.sin_port=htons(port);
+       addrlen = sizeof(server.sa_in);
 
        if (h == NULL || strcmp(h,"*") == 0)
                server.sa_in.sin_addr.s_addr=INADDR_ANY;
@@ -723,12 +754,19 @@ again:
                bind_mode=BIO_BIND_NORMAL;
                }
 #endif
-       if (bind(s,&server.sa,sizeof(server)) == -1)
+       if (bind(s,&server.sa,addrlen) == -1)
                {
 #ifdef SO_REUSEADDR
                err_num=get_last_socket_error();
                if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
+#ifdef OPENSSL_SYS_WINDOWS
+                       /* Some versions of Windows define EADDRINUSE to
+                        * a dummy value.
+                        */
+                       (err_num == WSAEADDRINUSE))
+#else
                        (err_num == EADDRINUSE))
+#endif
                        {
                        client = server;
                        if (h == NULL || strcmp(h,"*") == 0)
@@ -751,7 +789,7 @@ again:
                        if (cs != INVALID_SOCKET)
                                {
                                int ii;
-                               ii=connect(cs,&client.sa,sizeof(client));
+                               ii=connect(cs,&client.sa,addrlen);
                                closesocket(cs);
                                if (ii == INVALID_SOCKET)
                                        {
@@ -923,7 +961,6 @@ int BIO_set_tcp_ndelay(int s, int on)
 #endif
        return(ret == 0);
        }
-#endif
 
 int BIO_socket_nbio(int s, int mode)
        {
@@ -936,3 +973,4 @@ int BIO_socket_nbio(int s, int mode)
 #endif
        return(ret == 0);
        }
+#endif