bss_dgram.c: fix bugs [from HEAD].
authorAndy Polyakov <appro@openssl.org>
Tue, 19 Jun 2012 12:50:09 +0000 (12:50 +0000)
committerAndy Polyakov <appro@openssl.org>
Tue, 19 Jun 2012 12:50:09 +0000 (12:50 +0000)
PR: 2833

crypto/bio/bss_dgram.c

index ad9c372d586fdded5661dcda9b64c193dd406fb2..9911df52ab81e323c3bb9dee734364f276fd2bc7 100644 (file)
@@ -186,7 +186,7 @@ static void dgram_adjust_rcv_timeout(BIO *b)
        {
 #if defined(SO_RCVTIMEO)
        bio_dgram_data *data = (bio_dgram_data *)b->ptr;
-       int sz = sizeof(int);
+       union { size_t s; int i; } sz = {0};
 
        /* Is a timer active? */
        if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
@@ -196,6 +196,8 @@ static void dgram_adjust_rcv_timeout(BIO *b)
                /* Read current socket timeout */
 #ifdef OPENSSL_SYS_WINDOWS
                int timeout;
+
+               sz.i = sizeof(timeout);
                if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
                                           (void*)&timeout, &sz) < 0)
                        { perror("getsockopt"); }
@@ -205,9 +207,12 @@ static void dgram_adjust_rcv_timeout(BIO *b)
                        data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
                        }
 #else
+               sz.i = sizeof(data->socket_timeout);
                if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
                                                &(data->socket_timeout), (void *)&sz) < 0)
                        { perror("getsockopt"); }
+               else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
+                       OPENSSL_assert(sz.s<=sizeof(data->socket_timeout));
 #endif
 
                /* Get current time */
@@ -376,11 +381,10 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
        int *ip;
        struct sockaddr *to = NULL;
        bio_dgram_data *data = NULL;
-#if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
-       long sockopt_val = 0;
-       unsigned int sockopt_len = 0;
-#endif
-#ifdef OPENSSL_SYS_LINUX
+       int sockopt_val = 0;
+#if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
+       socklen_t sockopt_len;  /* assume that system supporting IP_MTU is
+                                * modern enough to define socklen_t */
        socklen_t addr_len;
        union   {
                struct sockaddr sa;
@@ -462,7 +466,7 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                break;
                /* (Linux)kernel sets DF bit on outgoing IP packets */
        case BIO_CTRL_DGRAM_MTU_DISCOVER:
-#ifdef OPENSSL_SYS_LINUX
+#if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
                addr_len = (socklen_t)sizeof(addr);
                memset((void *)&addr, 0, sizeof(addr));
                if (getsockname(b->num, &addr.sa, &addr_len) < 0)
@@ -470,7 +474,6 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                        ret = 0;
                        break;
                        }
-               sockopt_len = sizeof(sockopt_val);
                switch (addr.sa.sa_family)
                        {
                case AF_INET:
@@ -479,7 +482,7 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                                &sockopt_val, sizeof(sockopt_val))) < 0)
                                perror("setsockopt");
                        break;
-#if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER)
+#if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
                case AF_INET6:
                        sockopt_val = IPV6_PMTUDISC_DO;
                        if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
@@ -496,7 +499,7 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                break;
 #endif
        case BIO_CTRL_DGRAM_QUERY_MTU:
-#ifdef OPENSSL_SYS_LINUX
+#if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
                addr_len = (socklen_t)sizeof(addr);
                memset((void *)&addr, 0, sizeof(addr));
                if (getsockname(b->num, &addr.sa, &addr_len) < 0)
@@ -658,10 +661,13 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
 #endif
                break;
        case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
-#ifdef OPENSSL_SYS_WINDOWS
                {
-               int timeout, sz = sizeof(timeout);
+               union { size_t s; int i; } sz = {0};
+#ifdef OPENSSL_SYS_WINDOWS
+               int timeout;
                struct timeval *tv = (struct timeval *)ptr;
+
+               sz.i = sizeof(timeout);
                if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
                        (void*)&timeout, &sz) < 0)
                        { perror("getsockopt"); ret = -1; }
@@ -671,12 +677,20 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                        tv->tv_usec = (timeout % 1000) * 1000;
                        ret = sizeof(*tv);
                        }
-               }
 #else
+               sz.i = sizeof(struct timeval);
                if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
-                       ptr, (void *)&ret) < 0)
+                       ptr, (void *)&sz) < 0)
                        { perror("getsockopt"); ret = -1; }
+               else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
+                       {
+                       OPENSSL_assert(sz.s<=sizeof(struct timeval));
+                       ret = (int)sz.s;
+                       }
+               else
+                       ret = sz.i;
 #endif
+               }
                break;
 #endif
 #if defined(SO_SNDTIMEO)
@@ -696,10 +710,13 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
 #endif
                break;
        case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
-#ifdef OPENSSL_SYS_WINDOWS
                {
-               int timeout, sz = sizeof(timeout);
+               union { size_t s; int i; } sz = {0};
+#ifdef OPENSSL_SYS_WINDOWS
+               int timeout;
                struct timeval *tv = (struct timeval *)ptr;
+
+               sz.i = sizeof(timeout);
                if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
                        (void*)&timeout, &sz) < 0)
                        { perror("getsockopt"); ret = -1; }
@@ -709,12 +726,20 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                        tv->tv_usec = (timeout % 1000) * 1000;
                        ret = sizeof(*tv);
                        }
-               }
 #else
+               sz.i = sizeof(struct timeval);
                if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 
-                       ptr, (void *)&ret) < 0)
+                       ptr, (void *)&sz) < 0)
                        { perror("getsockopt"); ret = -1; }
+               else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
+                       {
+                       OPENSSL_assert(sz.s<=sizeof(struct timeval));
+                       ret = (int)sz.s;
+                       }
+               else
+                       ret = sz.i;
 #endif
+               }
                break;
 #endif
        case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP: