Clean up use of Windows Platform SDK headers.

Define WIN32_LEAN_AND_MEAN before including Windows Platform SDK
headers to preempt naming conflicts and to make the build faster. Avoid
including those headers in BoringSSL headers. Document that Platform
SDK 8.1 or later is required on Windows.

Change-Id: I907ada21dc722527ea37e839c71c5157455a7003
Reviewed-on: https://boringssl-review.googlesource.com/3100
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/BUILDING b/BUILDING
index 87e23c6..830f58b 100644
--- a/BUILDING
+++ b/BUILDING
@@ -14,8 +14,8 @@
   * On Windows only, Yasm[4] is required.
 
   * A C compiler is required. On Windows, MSVC 12 (Visual Studio 2013) or later
-    are supported. Recent versions of GCC and Clang should work on non-Windows
-    platforms, and maybe on Windows too.
+    with Platform SDK 8.1 or later are supported. Recent versions of GCC and
+    Clang should work on non-Windows platforms, and maybe on Windows too.
 
   * Bash is required for running some tests, but not for building.
 
diff --git a/crypto/bio/bio_test.c b/crypto/bio/bio_test.c
index db6fc02..57684df 100644
--- a/crypto/bio/bio_test.c
+++ b/crypto/bio/bio_test.c
@@ -26,6 +26,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 #else
+#define WIN32_LEAN_AND_MEAN
 #include <io.h>
 #include <WinSock2.h>
 #include <WS2tcpip.h>
diff --git a/crypto/bio/connect.c b/crypto/bio/connect.c
index 9bd87d4..55b9352 100644
--- a/crypto/bio/connect.c
+++ b/crypto/bio/connect.c
@@ -65,6 +65,10 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <unistd.h>
+#else
+#define WIN32_LEAN_AND_MEAN
+#include <WinSock2.h>
+#include <WS2tcpip.h>
 #endif
 
 #include <openssl/buf.h>
diff --git a/crypto/bio/fd.c b/crypto/bio/fd.c
index bc47e74..c5133d8 100644
--- a/crypto/bio/fd.c
+++ b/crypto/bio/fd.c
@@ -61,6 +61,7 @@
 #if !defined(OPENSSL_WINDOWS)
 #include <unistd.h>
 #else
+#define WIN32_LEAN_AND_MEAN
 #include <io.h>
 #include <Windows.h>
 #endif
diff --git a/crypto/bio/internal.h b/crypto/bio/internal.h
index ba28839..d9a34f1 100644
--- a/crypto/bio/internal.h
+++ b/crypto/bio/internal.h
@@ -67,8 +67,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #else
-#include <WinSock2.h>
-#include <WS2tcpip.h>
+typedef int socklen_t;
 #endif
 
 #if defined(__cplusplus)
diff --git a/crypto/bio/socket.c b/crypto/bio/socket.c
index 90ae14a..6d393a2 100644
--- a/crypto/bio/socket.c
+++ b/crypto/bio/socket.c
@@ -61,6 +61,9 @@
 
 #if !defined(OPENSSL_WINDOWS)
 #include <unistd.h>
+#else
+#define WIN32_LEAN_AND_MEAN
+#include <WinSock2.h>
 #endif
 
 #include "internal.h"
diff --git a/crypto/bio/socket_helper.c b/crypto/bio/socket_helper.c
index ba65a1a..801800a 100644
--- a/crypto/bio/socket_helper.c
+++ b/crypto/bio/socket_helper.c
@@ -24,9 +24,9 @@
 #include <netdb.h>
 #include <unistd.h>
 #else
+#define WIN32_LEAN_AND_MEAN
 #include <WinSock2.h>
 #include <WS2tcpip.h>
-typedef int socklen_t;
 #endif
 
 #include "internal.h"
diff --git a/crypto/directory_win.c b/crypto/directory_win.c
index 4956383..203d4a2 100644
--- a/crypto/directory_win.c
+++ b/crypto/directory_win.c
@@ -30,6 +30,7 @@
 
 #if defined(OPENSSL_WINDOWS)
 
+#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <tchar.h>
 #include <errno.h>
diff --git a/crypto/err/err.c b/crypto/err/err.c
index 892b2ac..5ee81ca 100644
--- a/crypto/err/err.c
+++ b/crypto/err/err.c
@@ -115,6 +115,7 @@
 #include <stdio.h>
 
 #if defined(OPENSSL_WINDOWS)
+#define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
 #endif
 
diff --git a/crypto/mem.c b/crypto/mem.c
index 11ef335..8341411 100644
--- a/crypto/mem.c
+++ b/crypto/mem.c
@@ -65,6 +65,7 @@
 #include <string.h>
 
 #if defined(OPENSSL_WINDOWS)
+#define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
 #else
 #include <strings.h>
diff --git a/crypto/rand/windows.c b/crypto/rand/windows.c
index ed6e5e9..86bea85 100644
--- a/crypto/rand/windows.c
+++ b/crypto/rand/windows.c
@@ -16,6 +16,7 @@
 
 #if defined(OPENSSL_WINDOWS)
 
+#define WIN32_LEAN_AND_MEAN
 #include <limits.h>
 #include <stdlib.h>
 #include <Windows.h>
diff --git a/crypto/thread.c b/crypto/thread.c
index 15e38bd..b1b4d00 100644
--- a/crypto/thread.c
+++ b/crypto/thread.c
@@ -59,6 +59,7 @@
 #include <errno.h>
 
 #if defined(OPENSSL_WINDOWS)
+#define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
 #endif
 
diff --git a/tool/speed.cc b/tool/speed.cc
index cfffcb0..8184339e 100644
--- a/tool/speed.cc
+++ b/tool/speed.cc
@@ -27,6 +27,7 @@
 #include <openssl/rsa.h>
 
 #if defined(OPENSSL_WINDOWS)
+#define WIN32_LEAN_AND_MEAN
 #include <Windows.h>
 #elif defined(OPENSSL_APPLE)
 #include <sys/time.h>
diff --git a/tool/transport_common.cc b/tool/transport_common.cc
index c2b3e0c..00b4a8b 100644
--- a/tool/transport_common.cc
+++ b/tool/transport_common.cc
@@ -35,7 +35,6 @@
 #include <io.h>
 #include <WinSock2.h>
 #include <WS2tcpip.h>
-typedef int socklen_t;
 typedef int ssize_t;
 #define read _read
 #define write _write