Remove OPENSSL_timeval.

With DTLSv1_get_timeout de-ctrl-ified, the type checker complains about
OPENSSL_timeval. Existing callers all use the real timeval.

Now that OPENSSL_timeval is not included in any public structs, simply
forward-declare timeval itself in ssl.h and pull in winsock2.h in internal
headers.

Change-Id: Ieaf110e141578488048c28cdadb14881301a2ce1
Reviewed-on: https://boringssl-review.googlesource.com/4682
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index 7c08adc..aadc52f 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -162,6 +162,11 @@
 /* wpa_supplicant expects to get the version functions from ssl.h */
 #include <openssl/crypto.h>
 
+/* Forward-declare struct timeval. On Windows, it is defined in winsock2.h and
+ * Windows headers define too many macros to be included in public headers.
+ * However, only a forward declaration is needed. */
+struct timeval;
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
@@ -449,17 +454,6 @@
   char extended_master_secret;
 };
 
-#if defined(OPENSSL_WINDOWS)
-/* Because of Windows header issues, we can't get the normal declaration of
- * timeval. */
-typedef struct OPENSSL_timeval_st {
-  long tv_sec;
-  long tv_usec;
-} OPENSSL_timeval;
-#else
-typedef struct timeval OPENSSL_timeval;
-#endif
-
 /* SSL_OP_LEGACY_SERVER_CONNECT allows initial connection to servers that don't
  * support RI */
 #define SSL_OP_LEGACY_SERVER_CONNECT 0x00000004L
@@ -1037,7 +1031,7 @@
 
   /* current_time_cb, if not NULL, is the function to use to get the current
    * time. It sets |*out_clock| to the current time. */
-  void (*current_time_cb)(const SSL *ssl, OPENSSL_timeval *out_clock);
+  void (*current_time_cb)(const SSL *ssl, struct timeval *out_clock);
 };
 
 OPENSSL_EXPORT LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx);
@@ -1634,7 +1628,7 @@
  *
  * NOTE: This function must be queried again whenever the handshake state
  * machine changes, including when |DTLSv1_handle_timeout| is called. */
-OPENSSL_EXPORT int DTLSv1_get_timeout(const SSL *ssl, OPENSSL_timeval *out);
+OPENSSL_EXPORT int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out);
 
 /* DTLSv1_handle_timeout is called when a DTLS handshake timeout expires. If no
  * timeout had expired, it returns 0. Otherwise, it retransmits the previous
diff --git a/ssl/d1_lib.c b/ssl/d1_lib.c
index e4809ca..e53156f 100644
--- a/ssl/d1_lib.c
+++ b/ssl/d1_lib.c
@@ -81,7 +81,7 @@
  * before failing the DTLS handshake. */
 #define DTLS1_MAX_TIMEOUTS                     12
 
-static void get_current_time(const SSL *ssl, OPENSSL_timeval *out_clock);
+static void get_current_time(const SSL *ssl, struct timeval *out_clock);
 
 int dtls1_new(SSL *s) {
   DTLS1_STATE *d1;
@@ -176,7 +176,7 @@
            &s->d1->next_timeout);
 }
 
-int DTLSv1_get_timeout(const SSL *ssl, OPENSSL_timeval *out) {
+int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
   if (!SSL_IS_DTLS(ssl)) {
     return 0;
   }
@@ -187,19 +187,19 @@
   }
 
   /* Get current time */
-  OPENSSL_timeval timenow;
+  struct timeval timenow;
   get_current_time(ssl, &timenow);
 
   /* If timer already expired, set remaining time to 0 */
   if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
       (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
        ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
-    memset(out, 0, sizeof(OPENSSL_timeval));
+    memset(out, 0, sizeof(struct timeval));
     return 1;
   }
 
   /* Calculate time left until timer expires */
-  memcpy(out, &ssl->d1->next_timeout, sizeof(OPENSSL_timeval));
+  memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval));
   out->tv_sec -= timenow.tv_sec;
   out->tv_usec -= timenow.tv_usec;
   if (out->tv_usec < 0) {
@@ -210,14 +210,14 @@
   /* If remaining time is less than 15 ms, set it to 0 to prevent issues
    * because of small devergences with socket timeouts. */
   if (out->tv_sec == 0 && out->tv_usec < 15000) {
-    memset(out, 0, sizeof(OPENSSL_timeval));
+    memset(out, 0, sizeof(struct timeval));
   }
 
   return 1;
 }
 
 int dtls1_is_timer_expired(SSL *s) {
-  OPENSSL_timeval timeleft;
+  struct timeval timeleft;
 
   /* Get time left until timeout, return false if no timer running */
   if (!DTLSv1_get_timeout(s, &timeleft)) {
@@ -244,7 +244,7 @@
 void dtls1_stop_timer(SSL *s) {
   /* Reset everything */
   s->d1->num_timeouts = 0;
-  memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
+  memset(&s->d1->next_timeout, 0, sizeof(struct timeval));
   s->d1->timeout_duration = 1;
   BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
            &s->d1->next_timeout);
@@ -294,7 +294,7 @@
   return dtls1_retransmit_buffered_messages(ssl);
 }
 
-static void get_current_time(const SSL *ssl, OPENSSL_timeval *out_clock) {
+static void get_current_time(const SSL *ssl, struct timeval *out_clock) {
   if (ssl->ctx->current_time_cb != NULL) {
     ssl->ctx->current_time_cb(ssl, out_clock);
     return;
diff --git a/ssl/internal.h b/ssl/internal.h
index ee76a73..1d8b2bf 100644
--- a/ssl/internal.h
+++ b/ssl/internal.h
@@ -149,6 +149,15 @@
 #include <openssl/ssl.h>
 #include <openssl/stack.h>
 
+#if defined(OPENSSL_WINDOWS)
+/* Windows defines struct timeval in winsock2.h. */
+#pragma warning(push, 3)
+#include <winsock2.h>
+#pragma warning(pop)
+#else
+#include <sys/types.h>
+#endif
+
 
 /* Cipher suites. */
 
@@ -739,9 +748,8 @@
   unsigned int num_timeouts;
 
   /* Indicates when the last handshake msg or heartbeat sent will
-   * timeout. Because of header issues on Windows, this cannot actually be a
-   * struct timeval. */
-  OPENSSL_timeval next_timeout;
+   * timeout. */
+  struct timeval next_timeout;
 
   /* Timeout duration */
   unsigned short timeout_duration;
diff --git a/ssl/test/bssl_shim.cc b/ssl/test/bssl_shim.cc
index 9750adb..1cf96f2 100644
--- a/ssl/test/bssl_shim.cc
+++ b/ssl/test/bssl_shim.cc
@@ -20,6 +20,7 @@
 #include <netinet/tcp.h>
 #include <signal.h>
 #include <sys/socket.h>
+#include <sys/types.h>
 #include <unistd.h>
 #else
 #include <io.h>
@@ -79,10 +80,10 @@
   // async_bio is async BIO which pauses reads and writes.
   BIO *async_bio = nullptr;
   // clock is the current time for the SSL connection.
-  OPENSSL_timeval clock;
+  timeval clock;
   // clock_delta is how far the clock advanced in the most recent failed
   // |BIO_read|.
-  OPENSSL_timeval clock_delta;
+  timeval clock_delta;
   ScopedEVP_PKEY channel_id;
   bool cert_ready = false;
   ScopedSSL_SESSION session;
@@ -285,7 +286,7 @@
   return config->psk.size();
 }
 
-static void CurrentTimeCallback(const SSL *ssl, OPENSSL_timeval *out_clock) {
+static void CurrentTimeCallback(const SSL *ssl, timeval *out_clock) {
   *out_clock = GetTestState(ssl)->clock;
 }
 
diff --git a/ssl/test/packeted_bio.cc b/ssl/test/packeted_bio.cc
index a2d1a5d..e831082 100644
--- a/ssl/test/packeted_bio.cc
+++ b/ssl/test/packeted_bio.cc
@@ -110,8 +110,7 @@
         (static_cast<uint64_t>(buf[6]) << 8) |
         static_cast<uint64_t>(buf[7]);
     timeout /= 1000;  // Convert nanoseconds to microseconds.
-    OPENSSL_timeval *out_timeout =
-        reinterpret_cast<OPENSSL_timeval *>(bio->ptr);
+    timeval *out_timeout = reinterpret_cast<timeval *>(bio->ptr);
     assert(out_timeout->tv_usec == 0);
     assert(out_timeout->tv_sec == 0);
     out_timeout->tv_usec = timeout % 1000000;
@@ -209,7 +208,7 @@
 
 }  // namespace
 
-ScopedBIO PacketedBioCreate(OPENSSL_timeval *out_timeout) {
+ScopedBIO PacketedBioCreate(timeval *out_timeout) {
   ScopedBIO bio(BIO_new(&g_packeted_bio_method));
   if (!bio) {
     return nullptr;
diff --git a/ssl/test/packeted_bio.h b/ssl/test/packeted_bio.h
index 7f58297..30697a5 100644
--- a/ssl/test/packeted_bio.h
+++ b/ssl/test/packeted_bio.h
@@ -15,11 +15,19 @@
 #ifndef HEADER_PACKETED_BIO
 #define HEADER_PACKETED_BIO
 
+#include <openssl/base.h>
 #include <openssl/bio.h>
-#include <openssl/ssl.h>
 
 #include "../../crypto/test/scoped_types.h"
 
+#if defined(OPENSSL_WINDOWS)
+#pragma warning(push, 3)
+#include <winsock2.h>
+#pragma warning(pop)
+#else
+#include <sys/types.h>
+#endif
+
 
 // PacketedBioCreate creates a filter BIO which implements a reliable in-order
 // blocking datagram socket. The resulting BIO, on |BIO_read|, may simulate a
@@ -30,7 +38,7 @@
 // Note: The read timeout simulation is intended to be used with the async BIO
 // wrapper. It doesn't simulate BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, used in DTLS's
 // blocking mode.
-ScopedBIO PacketedBioCreate(OPENSSL_timeval *out_timeout);
+ScopedBIO PacketedBioCreate(timeval *out_timeout);
 
 
 #endif  // HEADER_PACKETED_BIO