From 504a552c770f64dfa29ffc0b46e7b166718f84ec Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Thu, 30 Jun 2022 11:58:54 +0200 Subject: [PATCH] Windows needs special network init (#1034) --- tools/n2n-portfwd.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tools/n2n-portfwd.c b/tools/n2n-portfwd.c index 3620438..da19769 100644 --- a/tools/n2n-portfwd.c +++ b/tools/n2n-portfwd.c @@ -80,6 +80,7 @@ static void term_handler (int sig) { // ------------------------------------------------------------------------------------------------------- +// PLATFORM-DEPENDANT CODE SOCKET connect_to_management_port (n2n_portfwd_conf_t *ppp) { @@ -87,6 +88,23 @@ SOCKET connect_to_management_port (n2n_portfwd_conf_t *ppp) { SOCKET ret; struct sockaddr_in sock_addr; +#if defined(WIN32) + // Windows requires a call to WSAStartup() before it can work with sockets + WORD wVersionRequested; + WSADATA wsaData; + int err; + + // Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h + wVersionRequested = MAKEWORD(2, 2); + + err = WSAStartup(wVersionRequested, &wsaData); + if (err != 0) { + // tell the user that we could not find a usable Winsock DLL + traceEvent(TRACE_ERROR, "WSAStartup failed with error: %d\n", err); + return -1; + } +#endif + ret = socket (PF_INET, SOCK_DGRAM, 0); if((int)ret < 0) return -1;