From 2ad336f999781db211d92332e657398829c8799c Mon Sep 17 00:00:00 2001
From: Steven Murdoch <Steven.Murdoch@cl.cam.ac.uk>
Date: Fri, 19 Aug 2011 14:47:44 +0100
Subject: [PATCH] Link and build tor-fw-helper on Windows

- Update configure script to test for libminiupnpc along with the
  libws2_32 and libiphlpapi libraries required by libminiupnpc
- When building tor-fw-helper, link in libiphlpapi
- Link in libminiupnpc statically becasue I could not get the DLL
  to link properly
- Call WSAStartup before doing network operations
- Fix up a compiler warning about uninitialized backend_state

N.B. The changes to configure.in and Makefile.am will break on non-
Windows platforms.
---
 configure.in                                 |  2 +-
 src/tools/tor-fw-helper/Makefile.am          |  2 +-
 src/tools/tor-fw-helper/tor-fw-helper-upnp.c |  3 ++
 src/tools/tor-fw-helper/tor-fw-helper.c      | 34 +++++++++++++++++++-
 4 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/configure.in b/configure.in
index 4a89df6e04..1c45f186d5 100644
--- a/configure.in
+++ b/configure.in
@@ -559,7 +559,7 @@ dnl There are no packages for Debian or Redhat as of this patch
 
 if test "$upnp" = "true"; then
     AC_DEFINE(MINIUPNPC, 1, [Define to 1 if we are building with UPnP.])
-    TOR_SEARCH_LIBRARY(libminiupnpc, $trylibminiupnpcdir, [-lminiupnpc],
+    TOR_SEARCH_LIBRARY(libminiupnpc, $trylibminiupnpcdir, [-lminiupnpc -lws2_32 -liphlpapi],
         [#include <miniupnpc/miniwget.h>
          #include <miniupnpc/miniupnpc.h>
          #include <miniupnpc/upnpcommands.h>],
diff --git a/src/tools/tor-fw-helper/Makefile.am b/src/tools/tor-fw-helper/Makefile.am
index 77ff63fc36..39aabc7b20 100644
--- a/src/tools/tor-fw-helper/Makefile.am
+++ b/src/tools/tor-fw-helper/Makefile.am
@@ -25,7 +25,7 @@ endif
 
 if MINIUPNPC
 miniupnpc_ldflags = @TOR_LDFLAGS_libminiupnpc@
-miniupnpc_ldadd = -lminiupnpc -lm
+miniupnpc_ldadd = -lminiupnpc -lm -liphlpapi
 miniupnpc_cppflags = @TOR_CPPFLAGS_libminiupnpc@
 else
 miniupnpc_ldflags =
diff --git a/src/tools/tor-fw-helper/tor-fw-helper-upnp.c b/src/tools/tor-fw-helper/tor-fw-helper-upnp.c
index 18ca56394f..c4b14a84e2 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper-upnp.c
+++ b/src/tools/tor-fw-helper/tor-fw-helper-upnp.c
@@ -9,6 +9,9 @@
 
 #include "orconfig.h"
 #ifdef MINIUPNPC
+#ifdef MS_WINDOWS
+#define STATICLIB
+#endif
 #include <stdint.h>
 #include <string.h>
 #include <stdio.h>
diff --git a/src/tools/tor-fw-helper/tor-fw-helper.c b/src/tools/tor-fw-helper/tor-fw-helper.c
index 20d60d7ba6..cb8e0cd9eb 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper.c
+++ b/src/tools/tor-fw-helper/tor-fw-helper.c
@@ -13,6 +13,7 @@
  * later date.
  */
 
+#include "orconfig.h"
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -20,7 +21,10 @@
 #include <time.h>
 #include <string.h>
 
-#include "orconfig.h"
+#ifdef MS_WINDOWS
+#include <winsock2.h>
+#endif
+
 #include "tor-fw-helper.h"
 #ifdef NAT_PMP
 #include "tor-fw-helper-natpmp.h"
@@ -219,6 +223,29 @@ tor_fw_add_dir_port(tor_fw_options_t *tor_fw_options,
   }
 }
 
+/** Called before we make any calls to network-related functions.
+ * (Some operating systems require their network libraries to be
+ * initialized.) (from common/compat.c) */
+static int
+network_init(void)
+{
+#ifdef MS_WINDOWS
+  /* This silly exercise is necessary before windows will allow
+   * gethostbyname to work. */
+  WSADATA WSAData;
+  int r;
+  r = WSAStartup(0x101, &WSAData);
+  if (r) {
+    fprintf(stderr, "E: Error initializing Windows network layer - code was %d", r);
+    return -1;
+  }
+  /* WSAData.iMaxSockets might show the max sockets we're allowed to use.
+   * We might use it to complain if we're trying to be a server but have
+   * too few sockets available. */
+#endif
+  return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -229,6 +256,7 @@ main(int argc, char **argv)
   backends_t backend_state;
 
   memset(&tor_fw_options, 0, sizeof(tor_fw_options));
+  memset(&backend_state, 0, sizeof(backend_state));
 
   while (1) {
     int option_index = 0;
@@ -329,6 +357,10 @@ main(int argc, char **argv)
             tor_fw_options.public_dir_port);
   }
 
+  // Initialize networking
+  if (network_init())
+    exit(1);
+
   // Initalize the various fw-helper backend helpers
   r = init_backends(&tor_fw_options, &backend_state);
   if (r)
-- 
GitLab