adb: Win32: set socket buffer sizes properly

On Windows, adb_socket_setbufsize() was taking a file descriptor value
from the compatibility layer in sysdeps_win32.c (namely, an index into
the _win32_fhs array) and passing it to the Winsock setsockopt() call,
which wants a Winsock SOCKET handle. Basically, adb_socket_setbufsize()
was passing `fd` instead of `_fh_from_int(fd)->fh_socket`, resulting in
adb effectively setting a socket buffer size on a random socket in the
process.

The fix is to introduce adb_setsockopt() which just calls setsockopt()
on non-Win32, and which uses the Winsock SOCKET handle on Win32. The
change also moves Win32 disable_tcp_nagle() to a header and adds an
extra sanity check to adb_shutdown().

Change-Id: I4354e818d27538f7ff5b0e70b28bdb6300e1b98b
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
diff --git a/sysdeps_win32.c b/sysdeps_win32.c
index e69ec2b..f132b8c 100644
--- a/sysdeps_win32.c
+++ b/sysdeps_win32.c
@@ -440,7 +440,8 @@
 {
     FH   f = _fh_from_int(fd);
 
-    if (!f) {
+    if (!f || f->clazz != &_fh_socket_class) {
+        D("adb_shutdown: invalid fd %d\n", fd);
         return -1;
     }
 
@@ -471,6 +472,8 @@
 /**************************************************************************/
 /**************************************************************************/
 
+#undef setsockopt
+
 static void
 _socket_set_errno( void )
 {
@@ -786,15 +789,16 @@
 }
 
 
-void  disable_tcp_nagle(int fd)
+int  adb_setsockopt( int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen )
 {
     FH   fh = _fh_from_int(fd);
-    int  on = 1;
 
-    if ( !fh || fh->clazz != &_fh_socket_class )
-        return;
+    if ( !fh || fh->clazz != &_fh_socket_class ) {
+        D("adb_setsockopt: invalid fd %d\n", fd);
+        return -1;
+    }
 
-    setsockopt( fh->fh_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on) );
+    return setsockopt( fh->fh_socket, level, optname, optval, optlen );
 }
 
 /**************************************************************************/