Add and use OPENSSL_zalloc
[openssl.git] / ssl / d1_lib.c
index eac271d27b0374eba366466e38352ab03d2bd74d..d3b582a98cc810ec39cbcb723e7616ace75b8892 100644 (file)
 
 #if defined(OPENSSL_SYS_VMS)
 # include <sys/timeb.h>
+#elif defined(OPENSSL_SYS_NETWARE) && !defined(_WINSOCK2API_)
+# include <sys/timeval.h>
+#elif defined(OPENSSL_SYS_VXWORKS)
+# include <sys/times.h>
+#elif !defined(OPENSSL_SYS_WIN32)
+# include <sys/time.h>
 #endif
 
 static void get_current_time(struct timeval *t);
 static int dtls1_set_handshake_header(SSL *s, int type, unsigned long len);
 static int dtls1_handshake_write(SSL *s);
-const char dtls1_version_str[] = "DTLSv1" OPENSSL_VERSION_PTEXT;
 int dtls1_listen(SSL *s, struct sockaddr *client);
 
 const SSL3_ENC_METHOD DTLSv1_enc_data = {
@@ -124,17 +129,16 @@ int dtls1_new(SSL *s)
 {
     DTLS1_STATE *d1;
 
-    if(!DTLS_RECORD_LAYER_new(&s->rlayer)) {
+    if (!DTLS_RECORD_LAYER_new(&s->rlayer)) {
         return 0;
     }
     
     if (!ssl3_new(s))
         return (0);
-    if ((d1 = OPENSSL_malloc(sizeof *d1)) == NULL) {
+    if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) {
         ssl3_free(s);
         return (0);
     }
-    memset(d1, 0, sizeof *d1);
 
     d1->buffered_messages = pqueue_new();
     d1->sent_messages = pqueue_new();
@@ -147,10 +151,8 @@ int dtls1_new(SSL *s)
     d1->mtu = 0;
 
     if (!d1->buffered_messages || !d1->sent_messages) {
-        if (d1->buffered_messages)
-            pqueue_free(d1->buffered_messages);
-        if (d1->sent_messages)
-            pqueue_free(d1->sent_messages);
+        pqueue_free(d1->buffered_messages);
+        pqueue_free(d1->sent_messages);
         OPENSSL_free(d1);
         ssl3_free(s);
         return (0);
@@ -211,7 +213,7 @@ void dtls1_clear(SSL *s)
 
         dtls1_clear_queues(s);
 
-        memset(s->d1, 0, sizeof(*(s->d1)));
+        memset(s->d1, 0, sizeof(*s->d1));
 
         if (s->server) {
             s->d1->cookie_len = sizeof(s->d1->cookie);
@@ -320,7 +322,7 @@ void dtls1_start_timer(SSL *s)
 #ifndef OPENSSL_NO_SCTP
     /* Disable timer for SCTP */
     if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
-        memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
+        memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
         return;
     }
 #endif
@@ -355,7 +357,7 @@ struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
     if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
         (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
          s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
-        memset(timeleft, 0, sizeof(struct timeval));
+        memset(timeleft, 0, sizeof(*timeleft));
         return timeleft;
     }
 
@@ -373,7 +375,7 @@ struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
      * because of small devergences with socket timeouts.
      */
     if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
-        memset(timeleft, 0, sizeof(struct timeval));
+        memset(timeleft, 0, sizeof(*timeleft));
     }
 
     return timeleft;
@@ -408,8 +410,8 @@ void dtls1_double_timeout(SSL *s)
 void dtls1_stop_timer(SSL *s)
 {
     /* Reset everything */
-    memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
-    memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
+    memset(&s->d1->timeout, 0, sizeof(s->d1->timeout));
+    memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
     s->d1->timeout_duration = 1;
     BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
              &(s->d1->next_timeout));
@@ -502,7 +504,7 @@ int dtls1_listen(SSL *s, struct sockaddr *client)
     int ret;
 
     /* Ensure there is no state left over from a previous invocation */
-    if(!SSL_clear(s))
+    if (!SSL_clear(s))
         return -1;
 
     SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
@@ -524,7 +526,7 @@ static int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len)
     s->init_off = 0;
     /* Buffer the message to handle re-xmits */
 
-    if(!dtls1_buffer_message(s, 0))
+    if (!dtls1_buffer_message(s, 0))
         return 0;
 
     return 1;