2 This file is part of GNUnet.
3 (C) 2010, 2012 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-helper-vpn-windows.c
23 * @brief the helper for the VPN service in win32 builds.
24 * Opens a virtual network-interface, sends data received on the if to stdout,
25 * sends data received on stdin to the interface
26 * @author Christian M. Fuchs
28 * The following list of people have reviewed this code and considered
29 * it safe since the last modification (if you reviewed it, please
30 * have your name added to the list):
44 //#include <objbase.h>
47 * Need 'struct GNUNET_MessageHeader'.
49 #include "gnunet_common.h"
52 * Need VPN message types.
54 #include "gnunet_protocols.h"
57 * Should we print (interesting|debug) messages that can happen during
60 #define DEBUG GNUNET_NO
63 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
65 #define MAX_SIZE 65536
68 * This is our own local instance of a virtual network interface
69 * It is (somewhat) equivalent to using tun/tap in unixoid systems
71 * Upon initialization, we create such an device node.
72 * Upon termination, we remove it again.
74 * If we crash this device might stay around.
76 static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE;
81 static SP_DEVINFO_DATA DeviceNode;
86 static TCHAR class[128];
95 * Creates a tun-interface called dev;
97 * @param dev is asumed to point to a char[IFNAMSIZ]
98 * if *dev == '\\0', uses the name supplied by the kernel;
99 * @return the fd to the tun or -1 on error
112 /* Hello, I am a stub function! I did my job, yay me! */
119 * @brief Sets the IPv6-Address given in address on the interface dev
121 * @param dev the interface to configure
122 * @param address the IPv6-Address
123 * @param prefix_len the length of the network-prefix
126 set_address6 (const char *dev, const char *address, unsigned long prefix_len)
131 * parse the new address
135 * Get the index of the if
148 * Add the UP and RUNNING flags
154 fprintf (stderr, "close failed: %s\n", strerror (errno));
160 * @brief Sets the IPv4-Address given in address on the interface dev
162 * @param dev the interface to configure
163 * @param address the IPv4-Address
164 * @param mask the netmask
167 set_address4 (const char *dev, const char *address, const char *mask)
195 * Add the UP and RUNNING flags
201 fprintf (stderr, "close failed: %s\n", strerror (errno));
215 * where to find our inf-file. (+ the "full" path, after windows found")
217 * We do not directly input all the props here, because openvpn will update
218 * these details over time.
220 TCHAR InfFile[] = _T("tapw32.inf"); // FIXME: inline or #define, no 'variable'
221 TCHAR hwid[] = _T("TAP0901");
222 TCHAR InfFilePath[MAX_PATH];
223 TCHAR hwIdList[LINE_LEN + 4];
227 * Locate the inf-file, we need to store it somewhere where the system can
228 * find it. A good choice would be CWD/PDW or %WINDIR$\system32\
230 * TODO: Finde a more sane way to do this!
233 if (NULL == (InfFilePath = calloc (MAX_PATH, sizeof (TCHAR))))
236 GetFullPathName (InfFile, MAX_PATH, InfFilePath, NULL);
240 * Set the device's hardware ID and add it to a list.
241 * This information will later on identify this device in registry.
243 * TODO: Currently we just use TAP0901 as HWID,
244 * but we might want to add additional information
246 hwIdList = calloc (LINE_LEN + 4, sizeof (TCHAR));
247 if (hwIdList == NULL)
252 strncpy (hwIdList, hwid, LINE_LEN);
255 * Bootstrap our device info using the drivers inf-file
257 if (!SetupDiGetINFClass (InfFilePath,
259 class, sizeof (class) / sizeof (TCHAR),
266 * Collect all the other needed information...
267 * let the system fill our this form
269 DeviceInfo = SetupDiCreateDeviceInfoList (&guid, NULL);
270 if (DeviceInfo == INVALID_HANDLE_VALUE)
274 DeviceNode.cbSize = sizeof (SP_DEVINFO_DATA);
275 if (!SetupDiCreateDeviceInfo (DeviceInfo,
286 /* Deploy all the information collected into the registry */
287 if (!SetupDiSetDeviceRegistryProperty (DeviceInfo,
291 (lstrlen (hwIdList) + 2) * sizeof (TCHAR)))
295 /* Install our new class(=device) into the system */
296 if (SetupDiCallClassInstaller (DIF_REGISTERDEVICE,
303 //disabled for debug-reasons...
305 //GNUNET_free(DeviceInfo);
308 //GNUNET_free(hwIdList);
311 //GNUNET_free(InfFilePath);
321 SP_REMOVEDEVICE_PARAMS remove;
323 remove.ClassInstallHeader.cbSize = sizeof (SP_CLASSINSTALL_HEADER);
324 remove.HwProfile = 0;
325 remove.Scope = DI_REMOVEDEVICE_GLOBAL;
326 remove.ClassInstallHeader.InstallFunction = DIF_REMOVE;
327 if (! SetupDiSetClassInstallParams (DeviceInfo,
328 (PSP_DEVINFO_DATA) &DeviceNode,
329 &remove.ClassInstallHeader,
332 if (! SetupDiCallClassInstaller (DIF_REMOVE,
334 (PSP_DEVINFO_DATA) &DeviceNode))
341 * Start forwarding to and from the tunnel.
343 * @param fd_tun tunnel FD
349 * The buffer filled by reading from fd_tun
351 unsigned char buftun[MAX_SIZE];
352 ssize_t buftun_size = 0;
353 unsigned char *buftun_read = NULL;
356 * The buffer filled by reading from stdin
358 unsigned char bufin[MAX_SIZE];
359 ssize_t bufin_size = 0;
360 size_t bufin_rpos = 0;
361 unsigned char *bufin_read = NULL;
362 /* Hello, I am a stub function! I did my job, yay me! */
368 * Open VPN tunnel interface.
370 * @param argc must be 6
371 * @param argv 0: binary name (gnunet-helper-vpn)
372 * 1: tunnel interface name (gnunet-vpn)
373 * 2: IPv6 address (::1), "-" to disable
374 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
375 * 4: IPv4 address (1.2.3.4), "-" to disable
376 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
379 main (int argc, char **argv)
381 //char dev[IFNAMSIZ];
387 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
392 * strncpy (dev, argv[1], IFNAMSIZ);
393 * dev[IFNAMSIZ - 1] = '\0';
395 /* if (-1 == (fd_tun = init_tun (dev)))
397 fprintf (stderr, "Fatal: could not initialize tun-interface with IPv6 %s/%s and IPv4 %s/%s\n",
407 if (0 != strcmp (argv[2], "-"))
409 const char *address = argv[2];
410 long prefix_len = atol (argv[3]);
412 if ((prefix_len < 1) || (prefix_len > 127))
414 fprintf (stderr, "Fatal: prefix_len out of range\n");
418 //set_address6 (dev, address, prefix_len);
421 if (0 != strcmp (argv[4], "-"))
423 const char *address = argv[4];
424 const char *mask = argv[5];
426 set_address4 (NULL, address, mask);
429 if (setup_interface ())
435 uid_t uid = getuid ();
436 if (0 != setresuid (uid, uid, uid))
438 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
444 /*if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
446 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
448 // no exit, we might as well die with SIGPIPE should it ever happen