Remove extraneous white space, and add some braces
[openssl.git] / ssl / d1_both.c
index 808d4d14eb4aaa10902ed445e3de1694b9f214f8..a2a39baa60cdcaadbe5f85ecd3ca44641c6e8728 100644 (file)
@@ -259,33 +259,12 @@ static int dtls1_query_mtu(SSL *s)
 int dtls1_do_write(SSL *s, int type)
        {
        int ret;
-       int curr_mtu;
-       unsigned int len, frag_off, mac_size, blocksize;
+       unsigned int curr_mtu;
+       int retry = 1;
+       unsigned int len, frag_off, mac_size, blocksize, used_len;
 
        if(!dtls1_query_mtu(s))
                return -1;
-#if 0 
-       mtu = s->d1->mtu;
-
-       fprintf(stderr, "using MTU = %d\n", mtu);
-
-       mtu -= (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
-
-       curr_mtu = mtu - BIO_wpending(SSL_get_wbio(s));
-
-       if ( curr_mtu > 0)
-               mtu = curr_mtu;
-       else if ( ( ret = BIO_flush(SSL_get_wbio(s))) <= 0)
-               return ret;
-
-       if ( BIO_wpending(SSL_get_wbio(s)) + s->init_num >= mtu)
-               {
-               ret = BIO_flush(SSL_get_wbio(s));
-               if ( ret <= 0)
-                       return ret;
-               mtu = s->d1->mtu - (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH);
-               }
-#endif
 
        OPENSSL_assert(s->d1->mtu >= dtls1_min_mtu(s));  /* should have something reasonable now */
 
@@ -310,10 +289,15 @@ int dtls1_do_write(SSL *s, int type)
                blocksize = 0;
 
        frag_off = 0;
-       while( s->init_num)
+       /* s->init_num shouldn't ever be < 0...but just in case */
+       while(s->init_num > 0)
                {
-               curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) - 
-                       DTLS1_RT_HEADER_LENGTH - mac_size - blocksize;
+               used_len = BIO_wpending(SSL_get_wbio(s)) +  DTLS1_RT_HEADER_LENGTH
+                       + mac_size + blocksize;
+               if(s->d1->mtu > used_len)
+                       curr_mtu = s->d1->mtu - used_len;
+               else
+                       curr_mtu = 0;
 
                if ( curr_mtu <= DTLS1_HM_HEADER_LENGTH)
                        {
@@ -321,15 +305,27 @@ int dtls1_do_write(SSL *s, int type)
                        ret = BIO_flush(SSL_get_wbio(s));
                        if ( ret <= 0)
                                return ret;
-                       curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH -
-                               mac_size - blocksize;
+                       used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;
+                       if(s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH)
+                               {
+                               curr_mtu = s->d1->mtu - used_len;
+                               }
+                       else
+                               {
+                               /* Shouldn't happen */
+                               return -1;
+                               }
                        }
 
-               if ( s->init_num > curr_mtu)
+               /* We just checked that s->init_num > 0 so this cast should be safe */
+               if (((unsigned int)s->init_num) > curr_mtu)
                        len = curr_mtu;
                else
                        len = s->init_num;
 
+               /* Shouldn't ever happen */
+               if(len > INT_MAX)
+                       len = INT_MAX;
 
                /* XDTLS: this function is too long.  split out the CCS part */
                if ( type == SSL3_RT_HANDSHAKE)
@@ -340,12 +336,17 @@ int dtls1_do_write(SSL *s, int type)
                                s->init_off -= DTLS1_HM_HEADER_LENGTH;
                                s->init_num += DTLS1_HM_HEADER_LENGTH;
 
-                               if ( s->init_num > curr_mtu)
+                               /* We just checked that s->init_num > 0 so this cast should be safe */
+                               if (((unsigned int)s->init_num) > curr_mtu)
                                        len = curr_mtu;
                                else
                                        len = s->init_num;
                                }
 
+                       /* Shouldn't ever happen */
+                       if(len > INT_MAX)
+                               len = INT_MAX;
+
                        if ( len < DTLS1_HM_HEADER_LENGTH )
                                {
                                /*
@@ -370,13 +371,15 @@ int dtls1_do_write(SSL *s, int type)
                         * is fine and wait for an alert to handle the
                         * retransmit 
                         */
-                       if ( BIO_ctrl(SSL_get_wbio(s),
+                       if ( retry && BIO_ctrl(SSL_get_wbio(s),
                                BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0 )
                                {
                                if(!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU))
                                        {
                                        if(!dtls1_query_mtu(s))
                                                return -1;
+                                       /* Have one more go */
+                                       retry = 0;
                                        }
                                else
                                        return -1;