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):
37 #include <ddk/cfgmgr32.h>
39 #include "tap-windows.h"
43 * Need 'struct GNUNET_MessageHeader'.
45 #include "gnunet_common.h"
48 * Need VPN message types.
50 #include "gnunet_protocols.h"
53 * Should we print (interesting|debug) messages that can happen during
56 #define DEBUG GNUNET_NO
59 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
61 #define MAX_SIZE 65536
64 * Name or Path+Name of our driver in Unicode.
65 * The .sys and .cat files HAVE to be in the same location as this file!
67 #define INF_FILE "tapw32.inf"
70 * Hardware ID used in the inf-file.
71 * This might change over time, as openvpn advances their driver
73 #define HARDWARE_ID "TAP0901"
76 * Component ID if our driver
78 #define TAP_WIN_COMPONENT_ID "tap0901"
81 * Minimum major-id of the driver version we can work with
83 #define TAP_WIN_MIN_MAJOR 9
86 * Minimum minor-id of the driver version we can work with.
87 * v <= 7 has buggy IPv6.
88 * v == 8 is broken for small IPv4 Packets
90 #define TAP_WIN_MIN_MINOR 9
93 * Time in seconds to wait for our virtual device to go up after telling it to do so.
95 * openvpn doesn't specify a value, 4 seems sane for testing, even for openwrt
96 * (in fact, 4 was chosen by a fair dice roll...)
98 #define TAP32_POSTUP_WAITTIME 4
101 * Location of the network interface list resides in registry.
102 * TODO: is this fixed on all version of windows? Checked with XP and 7
104 #define INTERFACE_REGISTRY_LOCATION "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
107 * Our local process' PID. Used for creating a sufficiently unique additional
108 * hardware ID for our device.
110 static char secondary_hwid[LINE_LEN / 2];
113 * Device's visible Name, used to identify a network device in netsh.
114 * eg: "Local Area Connection 9"
116 static char device_visible_name[256];
119 * This is our own local instance of a virtual network interface
120 * It is (somewhat) equivalent to using tun/tap in unixoid systems
122 * Upon initialization, we create such an device node.
123 * Upon termination, we remove it again.
125 * If we crash this device might stay around.
127 static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE;
130 * Registry Key we hand over to windows to spawn a new virtual interface
132 static SP_DEVINFO_DATA DeviceNode;
135 * GUID of our virtual device in the form of
136 * {12345678-1234-1234-1234-123456789abc} - in hex
138 static char device_guid[256];
140 /* Overlapped IO Begins here (warning: nasty!) */
143 * A IO Object + read/writebuffer + buffer-size for windows asynchronous IO handling
150 BOOL path_open; // BOOL is winbool, NOT boolean!
154 OVERLAPPED overlapped;
156 unsigned char buffer[MAX_SIZE];
160 * Operlapped IO states for facility objects
162 #define IOSTATE_FAILED -1 /* overlapped I/O has failed, stop processing */
163 #define IOSTATE_READY 0 /* overlapped I/O is ready for work */
164 #define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
165 #define IOSTATE_WAITING 3 /* overlapped I/O has finished, but is waiting for it's write-partner */
170 * inet_pton() wrapper for WSAStringToAddress()
172 * this is needed as long as we support WinXP, because only Vista+ support
173 * inet_pton at all, and mingw does not yet offer inet_pton/ntop at all
175 * @param af - IN - the aftype this address is supposed to be (v4/v6)
176 * @param src - IN - the presentation form of the address
177 * @param dst - OUT - the numerical form of the address
178 * @return 0 on success, 1 on failure
181 inet_pton (int af, const char *src, void *dst)
183 struct sockaddr_storage addr;
184 int size = sizeof (addr);
185 char local_copy[INET6_ADDRSTRLEN + 1];
187 ZeroMemory (&addr, sizeof (addr));
188 /* stupid non-const API */
189 strncpy (local_copy, src, INET6_ADDRSTRLEN + 1);
190 local_copy[INET6_ADDRSTRLEN] = 0;
192 if (WSAStringToAddressA (local_copy, af, NULL, (struct sockaddr *) &addr, &size) == 0)
197 *(struct in_addr *) dst = ((struct sockaddr_in *) &addr)->sin_addr;
200 *(struct in6_addr *) dst = ((struct sockaddr_in6 *) &addr)->sin6_addr;
209 * Wrapper for executing a shellcommand in windows.
211 * @param command - the command + parameters to execute
212 * @return * exitcode of the program executed,
213 * * EINVAL (cmd/file not found)
214 * * EPIPE (could not read STDOUT)
217 execute_shellcommand (char * command)
221 if (NULL == command ||
222 NULL == (pipe = _popen (command, "rt")))
227 char output[LINE_LEN];
229 printf ("executed command: %s", command);
230 while (NULL != fgets (output, sizeof (output), pipe))
238 return _pclose (pipe);
242 * @brief Sets the IPv6-Address given in address on the interface dev
244 * @param address the IPv6-Address
245 * @param prefix_len the length of the network-prefix
248 set_address6 (const char *address, unsigned long prefix_len)
251 char command[LINE_LEN];
252 struct sockaddr_in6 sa6;
255 * parse the new address
257 memset (&sa6, 0, sizeof (struct sockaddr_in6));
258 sa6.sin6_family = AF_INET6;
259 if (1 != inet_pton (AF_INET6, address, &sa6.sin6_addr.s6_addr))
261 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
267 * prepare the command
269 snprintf (command, LINE_LEN,
270 "netsh interface ipv6 add address \"%s\" %s/%d",
271 device_visible_name, address, prefix_len);
275 ret = execute_shellcommand (command);
280 fprintf (stderr, "Setting IPv6 address failed: %s\n", strerror (ret));
281 exit (1); // FIXME: return error code, shut down interface / unload driver
286 * @brief Sets the IPv4-Address given in address on the interface dev
288 * @param dev the interface to configure
289 * @param address the IPv4-Address
290 * @param mask the netmask
293 set_address4 (const char *address, const char *mask)
296 char command[LINE_LEN];
298 struct sockaddr_in addr;
299 addr.sin_family = AF_INET;
304 if (1 != inet_pton (AF_INET, address, &addr.sin_addr.s_addr))
306 fprintf (stderr, "Failed to parse address `%s': %s\n", address,
312 * prepare the command
314 snprintf (command, LINE_LEN,
315 "netsh interface ipv4 add address \"%s\" %s %s",
316 device_visible_name, address, mask);
320 ret = execute_shellcommand (command);
325 fprintf (stderr, "Setting IPv4 address failed: %s\n", strerror (ret));
326 exit (1); // FIXME: return error code, shut down interface / unload driver
331 * Setup a new virtual interface to use for tunneling.
333 * @return: TRUE if setup was successful, else FALSE
339 * where to find our inf-file. (+ the "full" path, after windows found")
341 * We do not directly input all the props here, because openvpn will update
342 * these details over time.
344 char inf_file_path[MAX_PATH];
345 char hwidlist[LINE_LEN + 4];
346 char class_name[128];
351 * Set the device's hardware ID and add it to a list.
352 * This information will later on identify this device in registry.
354 * TODO: Currently we just use TAP0901 as HWID,
355 * but we might want to add additional information
357 strncpy (hwidlist, HARDWARE_ID, LINE_LEN);
359 * this is kind of over-complicated, but allows keeps things independent of
360 * how the openvpn-hwid is actually stored.
362 * A HWID list is double-\0 terminated and \0 separated
364 str_lenth = strlen (hwidlist) + 1;
365 strncpy (&hwidlist[str_lenth], secondary_hwid, LINE_LEN - str_lenth);
368 * Locate the inf-file, we need to store it somewhere where the system can
369 * find it. A good choice would be CWD/PDW or %WINDIR$\system32\
371 * TODO: How about win64 in the future?
372 * We need to use a different driver for amd64/i386 !
374 GetFullPathNameA (INF_FILE, MAX_PATH, inf_file_path, NULL);
377 * Bootstrap our device info using the drivers inf-file
379 if (!SetupDiGetINFClassA (inf_file_path,
381 class_name, sizeof (class_name) / sizeof (char),
386 * Collect all the other needed information...
387 * let the system fill our this form
389 DeviceInfo = SetupDiCreateDeviceInfoList (&class_guid, NULL);
390 if (DeviceInfo == INVALID_HANDLE_VALUE)
393 DeviceNode.cbSize = sizeof (SP_DEVINFO_DATA);
394 if (!SetupDiCreateDeviceInfoA (DeviceInfo,
403 /* Deploy all the information collected into the registry */
404 if (!SetupDiSetDeviceRegistryPropertyA (DeviceInfo,
408 (strlen (hwidlist) + 2) * sizeof (char)))
411 /* Install our new class(=device) into the system */
412 if (!SetupDiCallClassInstaller (DIF_REGISTERDEVICE,
421 * Remove our new virtual interface to use for tunneling.
422 * This function must be called AFTER setup_interface!
424 * @return: TRUE if destruction was successful, else FALSE
429 SP_REMOVEDEVICE_PARAMS remove;
431 if (INVALID_HANDLE_VALUE == DeviceInfo)
434 remove.ClassInstallHeader.cbSize = sizeof (SP_CLASSINSTALL_HEADER);
435 remove.HwProfile = 0;
436 remove.Scope = DI_REMOVEDEVICE_GLOBAL;
437 remove.ClassInstallHeader.InstallFunction = DIF_REMOVE;
439 * 1. Prepare our existing device information set, and place the
440 * uninstall related information into the structure
442 if (!SetupDiSetClassInstallParamsA (DeviceInfo,
443 (PSP_DEVINFO_DATA) & DeviceNode,
444 &remove.ClassInstallHeader,
448 * 2. Uninstall the virtual interface using the class installer
450 if (!SetupDiCallClassInstaller (DIF_REMOVE,
452 (PSP_DEVINFO_DATA) & DeviceNode))
455 SetupDiDestroyDeviceInfoList (DeviceInfo);
461 * Do all the lookup necessary to retrieve the inteface's actual name
464 * @return: TRUE if we were able to lookup the interface's name, else FALSE
467 resolve_interface_name ()
470 SP_DEVINFO_LIST_DETAIL_DATA device_details;
471 char pnp_instance_id [MAX_DEVICE_ID_LEN];
472 HKEY adapter_key_handle;
476 boolean retval = FALSE;
477 char adapter[] = INTERFACE_REGISTRY_LOCATION;
479 /* We can obtain the PNP instance ID from our setupapi handle */
480 device_details.cbSize = sizeof (device_details);
481 if (CR_SUCCESS != CM_Get_Device_ID_ExA (DeviceNode.DevInst,
482 (PCHAR) pnp_instance_id,
485 NULL)) //hMachine, we are local
488 /* Now we can use this ID to locate the correct networks interface in registry */
489 if (ERROR_SUCCESS != RegOpenKeyExA (
494 &adapter_key_handle))
497 /* Of course there is a multitude of entries here, with arbitrary names,
498 * thus we need to iterate through there.
502 char instance_key[256];
503 char query_key [256];
504 HKEY instance_key_handle;
505 char pnpinstanceid_name[] = "PnpInstanceID";
506 char pnpinstanceid_value[256];
507 char adaptername_name[] = "Name";
510 len = sizeof (adapter_key_handle);
511 /* optain a subkey of {4D36E972-E325-11CE-BFC1-08002BE10318} */
512 status = RegEnumKeyExA (
522 /* this may fail due to one of two reasons:
523 * we are at the end of the list*/
524 if (ERROR_NO_MORE_ITEMS == status)
526 // * we found a broken registry key, continue with the next key.
527 if (ERROR_SUCCESS != status)
530 /* prepare our new query string: */
531 snprintf (query_key, 256, "%s\\%s\\Connection",
532 INTERFACE_REGISTRY_LOCATION,
535 /* look inside instance_key\\Connection */
536 status = RegOpenKeyExA (
541 &instance_key_handle);
543 if (status != ERROR_SUCCESS)
546 /* now, read our PnpInstanceID */
547 len = sizeof (pnpinstanceid_value);
548 status = RegQueryValueExA (instance_key_handle,
550 NULL, //reserved, always NULL according to MSDN
552 (LPBYTE) pnpinstanceid_value,
555 if (status != ERROR_SUCCESS || data_type != REG_SZ)
558 /* compare the value we got to our devices PNPInstanceID*/
559 if (0 != strncmp (pnpinstanceid_value, pnp_instance_id,
560 sizeof (pnpinstanceid_value) / sizeof (char)))
563 len = sizeof (device_visible_name);
564 status = RegQueryValueExA (
567 NULL, //reserved, always NULL according to MSDN
569 (LPBYTE) device_visible_name,
572 if (status != ERROR_SUCCESS || data_type != REG_SZ)
576 * we have successfully found OUR instance,
577 * save the device GUID before exiting
580 strncpy (device_guid, instance_key, 256);
584 RegCloseKey (instance_key_handle);
589 RegCloseKey (adapter_key_handle);
595 check_tapw32_version (HANDLE handle)
600 memset (&(version), 0, sizeof (version));
603 if (DeviceIoControl (handle, TAP_WIN_IOCTL_GET_VERSION,
604 &version, sizeof (version),
605 &version, sizeof (version), &len, NULL))
608 fprintf (stderr, "TAP-Windows Driver Version %d.%d %s",
611 (version[2] ? "(DEBUG)" : ""));
615 if (version[0] != TAP_WIN_MIN_MAJOR || version[1] < TAP_WIN_MIN_MINOR)
617 fprintf (stderr, "ERROR: This version of gnunet requires a TAP-Windows driver that is at least version %d.%d!\n",
627 * Creates a tun-interface called dev;
629 * @return the fd to the tun or -1 on error
634 char device_path[256];
637 if (!setup_interface ())
640 return INVALID_HANDLE_VALUE;
643 if (!resolve_interface_name ())
646 return INVALID_HANDLE_VALUE;
649 /* Open Windows TAP-Windows adapter */
650 snprintf (device_path, sizeof (device_path), "%s%s%s",
655 handle = CreateFile (
657 GENERIC_READ | GENERIC_WRITE,
658 0, /* was: FILE_SHARE_READ */
661 FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
665 if (handle == INVALID_HANDLE_VALUE)
667 fprintf (stderr, "CreateFile failed on TAP device: %s\n", device_path);
671 /* get driver version info */
672 if (!check_tapw32_version (handle))
674 CloseHandle (handle);
675 return INVALID_HANDLE_VALUE;
678 /* TODO (opt?): get MTU-Size */
684 tun_up (HANDLE handle)
688 if (DeviceIoControl (handle, TAP_WIN_IOCTL_SET_MEDIA_STATUS,
689 &status, sizeof (status),
690 &status, sizeof (status), &len, NULL))
692 fprintf (stderr, "The TAP-Windows driver ignored our request to set the interface UP (TAP_WIN_IOCTL_SET_MEDIA_STATUS DeviceIoControl call)!\n");
696 /* Wait for the device to go UP, might take some time. */
697 Sleep ((TAP32_POSTUP_WAITTIME)*1000);
704 attempt_std_in (struct io_facility * std_in,
705 struct io_facility * tap_write)
708 // We could use PeekConsoleInput() or WaitForSingleObject()
709 // however, the interwebs states that WaitForSingleObject with filehandles
710 // might misbehave on some windows (unspecified which ones!).
711 // unfortunately, peekconsoleinput () just waits for KEYPRESS-event, which would never happen on a pipe or a file
714 // http://www.cplusplus.com/forum/windows/28837/
715 // http://stackoverflow.com/questions/4551644/using-overlapped-io-for-console-input
716 // http://cygwin.com/ml/cygwin/2012-05/msg00322.html
719 // http://stackoverflow.com/questions/3661106/overlapped-readfileex-on-child-process-redirected-stdout-never-fires
721 // we may read from STDIN, and no job was active
722 if (IOSTATE_READY == std_in->facility_state)
726 // we must complete a previous read from stdin, before doing more work
727 else if (IOSTATE_QUEUED == std_in->facility_state)
729 // there is some data to be read from STDIN!
730 /* if (PeekConsoleInput(stdin_handle,
731 &std_in->buffer[MAX_SIZE],
733 &std_in->buffer_size)){
738 // else { // nothing to do, try again next time }
745 attempt_tap_read (struct io_facility * tap_read,
746 struct io_facility * std_out)
749 if (IOSTATE_READY == tap_read->facility_state)
751 if (!ResetEvent (tap_read->overlapped.hEvent))
755 tap_read->status = ReadFile (tap_read->handle,
756 &tap_read->buffer[MAX_SIZE],
758 &tap_read->buffer_size,
759 &tap_read->overlapped);
761 /* Check how the task is handled */
762 if (tap_read->status)
763 {/* async event processed immediately*/
765 /* reset event manually*/
766 if (!SetEvent (tap_read->overlapped.hEvent))
769 /* we successfully read something from the TAP and now need to
770 * send it our via STDOUT. Is that possible at the moment? */
771 if (IOSTATE_READY == std_out->facility_state && 0 < tap_read->buffer_size)
772 { /* hand over this buffers content */
773 memcpy (std_out->buffer,
776 std_out->buffer_size = tap_read->buffer_size;
777 std_out->facility_state = IOSTATE_READY;
779 else if (0 < tap_read->buffer_size)
780 { /* If we have have read our buffer, wait for our write-partner*/
781 tap_read->facility_state = IOSTATE_WAITING;
782 // TODO: shall we attempt to fill our buffer or should we wait for our write-partner to finish?
785 else /* operation was either queued or failed*/
787 int err = GetLastError ();
788 if (ERROR_IO_PENDING == err)
789 { /* operation queued */
790 tap_read->facility_state = IOSTATE_QUEUED;
793 { /* error occurred, let the rest of the elements finish */
794 tap_read->path_open = FALSE;
795 tap_read->facility_state = IOSTATE_FAILED;
799 // We are queued and should check if the read has finished
800 else if (IOSTATE_QUEUED == tap_read->facility_state)
802 // there was an operation going on already, check if that has completed now.
803 tap_read->status = GetOverlappedResult (tap_read->handle,
804 &tap_read->overlapped,
805 &tap_read->buffer_size,
807 if (tap_read->status)
808 {/* successful return for a queued operation */
809 if (!ResetEvent (tap_read->overlapped.hEvent))
812 /* we successfully read something from the TAP and now need to
813 * send it our via STDOUT. Is that possible at the moment? */
814 if (IOSTATE_READY == std_out->facility_state && 0 < tap_read->buffer_size)
815 { /* hand over this buffers content */
816 memcpy (std_out->buffer,
819 std_out->buffer_size = tap_read->buffer_size;
820 std_out->facility_state = IOSTATE_READY;
821 tap_read->facility_state = IOSTATE_READY;
823 else if (0 < tap_read->buffer_size)
824 { /* If we have have read our buffer, wait for our write-partner*/
825 tap_read->facility_state = IOSTATE_WAITING;
826 // TODO: shall we attempt to fill our buffer or should we wait for our write-partner to finish?
830 { /* operation still pending/queued or failed? */
831 int err = GetLastError ();
832 if (ERROR_IO_INCOMPLETE != err && ERROR_IO_PENDING != err)
833 { /* error occurred, let the rest of the elements finish */
834 tap_read->path_open = FALSE;
835 tap_read->facility_state = IOSTATE_FAILED;
843 attempt_tap_write (struct io_facility * tap_write,
844 struct io_facility * std_in)
850 attempt_std_out (struct io_facility * std_out,
851 struct io_facility * tap_read)
857 * Initialize a overlapped structure
859 * @param elem the element to initilize
860 * @param initial_state the initial state for this instance
861 * @param signaled if the hEvent created should default to signaled or not
862 * @return true on success, else false
865 initialize_io_facility (struct io_facility * elem,
870 elem->path_open = TRUE;
871 elem->status = initial_state;
872 elem->handle_type = 0;
873 elem->handle = INVALID_HANDLE_VALUE;
874 elem->facility_state = 0;
875 elem->buffer_size = 0;
876 elem->overlapped.hEvent = CreateEvent (NULL, TRUE, signaled, NULL);
877 if (NULL == elem->overlapped.hEvent)
884 * Start forwarding to and from the tunnel.
886 * @param fd_tun tunnel FD
889 run (HANDLE tap_handle)
891 /* IO-Facility for reading from our virtual interface */
892 struct io_facility tap_read;
893 /* IO-Facility for writing to our virtual interface */
894 struct io_facility tap_write;
895 /* IO-Facility for reading from stdin */
896 struct io_facility std_in;
897 /* IO-Facility for writing to stdout */
898 struct io_facility std_out;
901 /* we do this HERE and not beforehand (in init_tun()), in contrast to openvpn
902 * to remove the need to flush the arp cache, handle DHCP and wrong IPs.
904 * DHCP and such are all features we will never use in gnunet afaik.
905 * But for openvpn those are essential.
907 if (!tun_up (tap_handle))
910 /* Initialize our overlapped IO structures*/
911 if (!(initialize_io_facility (&tap_read, TRUE, FALSE)
912 && initialize_io_facility (&tap_write, FALSE, TRUE)
913 && initialize_io_facility (&std_in, TRUE, FALSE)
914 && initialize_io_facility (&std_out, FALSE, TRUE)))
917 /* Handles for STDIN and STDOUT */
918 std_in.handle = GetStdHandle (STD_INPUT_HANDLE);
919 std_out.handle = GetStdHandle (STD_OUTPUT_HANDLE);
920 tap_read.handle = tap_handle;
921 tap_write.handle = tap_handle;
924 * Find out the types of our handles.
925 * This part is a problem, because in windows we need to handle files,
926 * pipes and the console differently.
928 std_in.handle_type = GetFileType (std_in.handle);
929 std_out.handle_type = GetFileType (std_out.handle);
930 /* the tap handle is always a file, but we still set this for consistency */
931 tap_read.handle_type = FILE_TYPE_DISK;
932 tap_write.handle_type = FILE_TYPE_DISK;
935 // Set Device to Subnet-Mode?
936 // do we really need tun.c:2925 ?
937 // Why does openvpn assign IPv4's there??? Foobar??
939 // Setup should be complete here.
940 // If something is missing, check init.c:3400+
943 // tunnel_point_to_point
946 while (std_in.path_open
948 || tap_read.path_open
949 || tap_write.path_open)
951 /* perform READ from stdin if possible */
952 if ((std_in.path_open && tap_write.path_open)
953 || IOSTATE_QUEUED == std_in.facility_state)
954 if (!attempt_std_in (&std_in, &tap_write))
957 /* perform READ from tap if possible */
958 if ((tap_read.path_open && std_out.path_open)
959 || IOSTATE_QUEUED == tap_read.facility_state)
960 if (!attempt_tap_read (&tap_read, &std_out))
963 /* perform WRITE to tap if possible */
964 if (IOSTATE_READY == tap_write.facility_state && tap_write.path_open)
965 if (!attempt_tap_write (&tap_write, &std_in))
968 /* perform WRITE to STDOUT if possible */
969 if (IOSTATE_READY == std_out.facility_state && std_out.path_open)
970 if (!attempt_std_out (&std_out, &tap_read))
973 // check if any path is blocked
981 * Open VPN tunnel interface.
983 * @param argc must be 6
984 * @param argv 0: binary name (gnunet-helper-vpn)
985 * 1: tunnel interface name (gnunet-vpn)
986 * 2: IPv6 address (::1), "-" to disable
987 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
988 * 4: IPv4 address (1.2.3.4), "-" to disable
989 * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
992 main (int argc, char **argv)
1000 fprintf (stderr, "Fatal: must supply 5 arguments!\n");
1004 strncpy (hwid, argv[1], LINE_LEN);
1005 hwid[LINE_LEN - 1] = '\0';
1008 * We use our PID for finding/resolving the control-panel name of our virtual
1009 * device. PIDs are (of course) unique at runtime, thus we can safely use it
1010 * as additional hardware-id for our device.
1012 snprintf (secondary_hwid, LINE_LEN / 2, "%s-%d",
1016 if (INVALID_HANDLE_VALUE == (handle = init_tun ()))
1018 fprintf (stderr, "Fatal: could not initialize virtual-interface %s with IPv6 %s/%s and IPv4 %s/%s\n",
1027 if (0 != strcmp (argv[2], "-"))
1029 const char *address = argv[2];
1030 long prefix_len = atol (argv[3]);
1032 if ((prefix_len < 1) || (prefix_len > 127))
1034 fprintf (stderr, "Fatal: prefix_len out of range\n");
1039 set_address6 (address, prefix_len);
1042 if (0 != strcmp (argv[4], "-"))
1044 const char *address = argv[4];
1045 const char *mask = argv[5];
1047 set_address4 (address, mask);
1051 // tap_allow_nonadmin_access
1057 remove_interface ();