Fix memory leak.
[openssl.git] / crypto / bio / b_sock.c
index 5ea621c0cf865c8202b5313ede04459fe9c8c44c..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());
@@ -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,7 +697,10 @@ 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;
@@ -731,7 +759,14 @@ again:
 #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)
@@ -926,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)
        {
@@ -939,3 +973,4 @@ int BIO_socket_nbio(int s, int mode)
 #endif
        return(ret == 0);
        }
+#endif