aboutsummaryrefslogtreecommitdiff
path: root/src/vpn
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2012-12-07 14:15:05 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2012-12-07 14:15:05 +0000
commit74a555104ed7288299e99549a73ba93eefb7c053 (patch)
treea15ad7d0980f99df2e2c929ba2236d2ec682aff1 /src/vpn
parent8329191b55a7cc8799bbd48acc94725de015bbd5 (diff)
downloadgnunet-74a555104ed7288299e99549a73ba93eefb7c053.tar.gz
gnunet-74a555104ed7288299e99549a73ba93eefb7c053.zip
solved fixme's, linked device handling functions to main program flow,
added some fixes here and there. added more comments to the code.
Diffstat (limited to 'src/vpn')
-rw-r--r--src/vpn/gnunet-helper-vpn-windows.c184
1 files changed, 90 insertions, 94 deletions
diff --git a/src/vpn/gnunet-helper-vpn-windows.c b/src/vpn/gnunet-helper-vpn-windows.c
index 74e06430a..c607d52e6 100644
--- a/src/vpn/gnunet-helper-vpn-windows.c
+++ b/src/vpn/gnunet-helper-vpn-windows.c
@@ -32,17 +32,11 @@
32 */ 32 */
33 33
34#include <stdio.h> 34#include <stdio.h>
35#include <tchar.h>
35#include <windows.h> 36#include <windows.h>
36#include <setupapi.h> 37#include <setupapi.h>
37#include "platform.h" 38#include "platform.h"
38 39
39//#include <tchar.h>
40//#include <stdlib.h>
41//#include <regstr.h>
42//#include <string.h>
43//#include <malloc.h>
44//#include <objbase.h>
45
46/** 40/**
47 * Need 'struct GNUNET_MessageHeader'. 41 * Need 'struct GNUNET_MessageHeader'.
48 */ 42 */
@@ -64,6 +58,18 @@
64 */ 58 */
65#define MAX_SIZE 65536 59#define MAX_SIZE 65536
66 60
61/**
62 * Name or Path+Name of our driver in Unicode.
63 * The .sys and .cat files HAVE to be in the same location as this file!
64 */
65#define INF_FILE _T("tapw32.inf")
66
67/**
68 * Hardware ID used in the inf-file.
69 * This might change over time, as openvpn advances their driver
70 */
71#define HARDWARE_ID _T("TAP0901")
72
67/** 73/**
68 * This is our own local instance of a virtual network interface 74 * This is our own local instance of a virtual network interface
69 * It is (somewhat) equivalent to using tun/tap in unixoid systems 75 * It is (somewhat) equivalent to using tun/tap in unixoid systems
@@ -76,54 +82,29 @@
76static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE; 82static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE;
77 83
78/** 84/**
79 * FIXME 85 * Registry Key we hand over to windows to spawn a new virtual interface
80 */ 86 */
81static SP_DEVINFO_DATA DeviceNode; 87static SP_DEVINFO_DATA DeviceNode;
82 88
83/** 89/**
84 * FIXME 90 * Class-tag of our virtual device
85 */ 91 */
86static TCHAR class[128]; 92static TCHAR class[128];
87 93
88/** 94/**
89 * FIXME 95 * GUID of our virtual device in the form of
96 * {12345678-1234-1234-1234-123456789abc} - in hex
90 */ 97 */
91static GUID guid; 98static GUID guid;
92 99
93
94/**
95 * Creates a tun-interface called dev;
96 *
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
100 */
101static int
102init_tun (char *dev)
103{
104 int fd;
105
106 if (NULL == dev)
107 {
108 errno = EINVAL;
109 return -1;
110 }
111
112 /* Hello, I am a stub function! I did my job, yay me! */
113
114 return fd;
115}
116
117
118/** 100/**
119 * @brief Sets the IPv6-Address given in address on the interface dev 101 * @brief Sets the IPv6-Address given in address on the interface dev
120 * 102 *
121 * @param dev the interface to configure
122 * @param address the IPv6-Address 103 * @param address the IPv6-Address
123 * @param prefix_len the length of the network-prefix 104 * @param prefix_len the length of the network-prefix
124 */ 105 */
125static void 106static void
126set_address6 (const char *dev, const char *address, unsigned long prefix_len) 107set_address6 (const char *address, unsigned long prefix_len)
127{ 108{
128 int fd = -1; 109 int fd = -1;
129 110
@@ -206,7 +187,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
206 187
207 188
208/** 189/**
209 * FIXME. 190 * Setup a new virtual interface to use for tunneling.
191 *
192 * @return: TRUE if setup was successful, else FALSE
210 */ 193 */
211static boolean 194static boolean
212setup_interface () 195setup_interface ()
@@ -217,25 +200,18 @@ setup_interface ()
217 * We do not directly input all the props here, because openvpn will update 200 * We do not directly input all the props here, because openvpn will update
218 * these details over time. 201 * these details over time.
219 */ 202 */
220 TCHAR InfFile[] = _T("tapw32.inf"); // FIXME: inline or #define, no 'variable'
221 TCHAR hwid[] = _T("TAP0901");
222 TCHAR InfFilePath[MAX_PATH]; 203 TCHAR InfFilePath[MAX_PATH];
223 TCHAR hwIdList[LINE_LEN + 4]; 204 TCHAR hwIdList[LINE_LEN + 4];
224 205
225#if DEAD
226 /** 206 /**
227 * Locate the inf-file, we need to store it somewhere where the system can 207 * 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\ 208 * find it. A good choice would be CWD/PDW or %WINDIR$\system32\
229 * 209 *
230 * TODO: Finde a more sane way to do this! 210 * TODO: Find a more sane way to do this!
211 * TODO: How about win64 in the future?
231 */ 212 */
213 GetFullPathName (INF_FILE, MAX_PATH, InfFilePath, NULL);
232 214
233 if (NULL == (InfFilePath = calloc (MAX_PATH, sizeof (TCHAR))))
234 return FALSE;
235#endif
236 GetFullPathName (InfFile, MAX_PATH, InfFilePath, NULL);
237
238#if DEAD
239 /** 215 /**
240 * Set the device's hardware ID and add it to a list. 216 * Set the device's hardware ID and add it to a list.
241 * This information will later on identify this device in registry. 217 * This information will later on identify this device in registry.
@@ -243,13 +219,7 @@ setup_interface ()
243 * TODO: Currently we just use TAP0901 as HWID, 219 * TODO: Currently we just use TAP0901 as HWID,
244 * but we might want to add additional information 220 * but we might want to add additional information
245 */ 221 */
246 hwIdList = calloc (LINE_LEN + 4, sizeof (TCHAR)); 222 strncpy (hwIdList, HARDWARE_ID, LINE_LEN);
247 if (hwIdList == NULL)
248 {
249 goto cleanup1;
250 }
251#endif
252 strncpy (hwIdList, hwid, LINE_LEN);
253 223
254 /** 224 /**
255 * Bootstrap our device info using the drivers inf-file 225 * Bootstrap our device info using the drivers inf-file
@@ -258,19 +228,16 @@ setup_interface ()
258 &guid, 228 &guid,
259 class, sizeof (class) / sizeof (TCHAR), 229 class, sizeof (class) / sizeof (TCHAR),
260 NULL)) 230 NULL))
261 { 231 return FALSE;
262 goto cleanup2; 232
263 }
264
265 /** 233 /**
266 * Collect all the other needed information... 234 * Collect all the other needed information...
267 * let the system fill our this form 235 * let the system fill our this form
268 */ 236 */
269 DeviceInfo = SetupDiCreateDeviceInfoList (&guid, NULL); 237 DeviceInfo = SetupDiCreateDeviceInfoList (&guid, NULL);
270 if (DeviceInfo == INVALID_HANDLE_VALUE) 238 if (DeviceInfo == INVALID_HANDLE_VALUE)
271 { 239 goto cleanup;
272 goto cleanup3; 240
273 }
274 DeviceNode.cbSize = sizeof (SP_DEVINFO_DATA); 241 DeviceNode.cbSize = sizeof (SP_DEVINFO_DATA);
275 if (!SetupDiCreateDeviceInfo (DeviceInfo, 242 if (!SetupDiCreateDeviceInfo (DeviceInfo,
276 class, 243 class,
@@ -279,56 +246,60 @@ setup_interface ()
279 NULL, 246 NULL,
280 DICD_GENERATE_ID, 247 DICD_GENERATE_ID,
281 &DeviceNode)) 248 &DeviceNode))
282 { 249 goto cleanup;
283 goto cleanup3; 250
284 }
285
286 /* Deploy all the information collected into the registry */ 251 /* Deploy all the information collected into the registry */
287 if (!SetupDiSetDeviceRegistryProperty (DeviceInfo, 252 if (!SetupDiSetDeviceRegistryProperty (DeviceInfo,
288 &DeviceNode, 253 &DeviceNode,
289 SPDRP_HARDWAREID, 254 SPDRP_HARDWAREID,
290 (LPBYTE) hwIdList, 255 (LPBYTE) hwIdList,
291 (lstrlen (hwIdList) + 2) * sizeof (TCHAR))) 256 (lstrlen (hwIdList) + 2) * sizeof (TCHAR)))
292 { 257 goto cleanup;
293 goto cleanup3; 258
294 }
295 /* Install our new class(=device) into the system */ 259 /* Install our new class(=device) into the system */
296 if (SetupDiCallClassInstaller (DIF_REGISTERDEVICE, 260 if (SetupDiCallClassInstaller (DIF_REGISTERDEVICE,
297 DeviceInfo, 261 DeviceInfo,
298 &DeviceNode)) 262 &DeviceNode))
299 {
300 return TRUE; 263 return TRUE;
301 } 264
302
303 //disabled for debug-reasons... 265 //disabled for debug-reasons...
304cleanup3: 266cleanup:
305 //GNUNET_free(DeviceInfo); 267// GNUNET_free(DeviceInfo);
306 ;
307cleanup2:
308 //GNUNET_free(hwIdList);
309 ;
310cleanup1:
311 //GNUNET_free(InfFilePath);
312 ;
313 return FALSE; 268 return FALSE;
314 269
315} 270}
316 271
317 272
273/**
274 * Remove our new virtual interface to use for tunneling.
275 * This function must be called AFTER setup_interface!
276 *
277 * @return: TRUE if destruction was successful, else FALSE
278 */
318static boolean 279static boolean
319remove_interface () 280remove_interface ()
320{ 281{
321 SP_REMOVEDEVICE_PARAMS remove; 282 SP_REMOVEDEVICE_PARAMS remove;
322 283
284 if (INVALID_HANDLE_VALUE == DeviceInfo)
285 return FALSE;
286
323 remove.ClassInstallHeader.cbSize = sizeof (SP_CLASSINSTALL_HEADER); 287 remove.ClassInstallHeader.cbSize = sizeof (SP_CLASSINSTALL_HEADER);
324 remove.HwProfile = 0; 288 remove.HwProfile = 0;
325 remove.Scope = DI_REMOVEDEVICE_GLOBAL; 289 remove.Scope = DI_REMOVEDEVICE_GLOBAL;
326 remove.ClassInstallHeader.InstallFunction = DIF_REMOVE; 290 remove.ClassInstallHeader.InstallFunction = DIF_REMOVE;
291 /*
292 * 1. Prepare our existing device information set, and place the
293 * uninstall related information into the structure
294 */
327 if (! SetupDiSetClassInstallParams (DeviceInfo, 295 if (! SetupDiSetClassInstallParams (DeviceInfo,
328 (PSP_DEVINFO_DATA) &DeviceNode, 296 (PSP_DEVINFO_DATA) &DeviceNode,
329 &remove.ClassInstallHeader, 297 &remove.ClassInstallHeader,
330 sizeof (remove))) 298 sizeof (remove)))
331 return FALSE; 299 return FALSE;
300 /*
301 * 2. Uninstall the virtual interface using the class installer
302 */
332 if (! SetupDiCallClassInstaller (DIF_REMOVE, 303 if (! SetupDiCallClassInstaller (DIF_REMOVE,
333 DeviceInfo, 304 DeviceInfo,
334 (PSP_DEVINFO_DATA) &DeviceNode)) 305 (PSP_DEVINFO_DATA) &DeviceNode))
@@ -336,6 +307,33 @@ remove_interface ()
336 return TRUE; 307 return TRUE;
337} 308}
338 309
310/**
311 * Creates a tun-interface called dev;
312 *
313 * @param hwid is asumed to point to a TCHAR[LINE_LEN]
314 * if *dev == '\\0', uses the name supplied by the kernel;
315 * @return the fd to the tun or -1 on error
316 */
317static int
318init_tun (TCHAR *hwid)
319{
320 int fd;
321
322 if (NULL == hwid)
323 {
324 errno = EINVAL;
325 return -1;
326 }
327
328 if (FALSE == setup_interface()){
329 errno = ENODEV;
330 return -1;
331 }
332
333
334
335 return fd;
336}
339 337
340/** 338/**
341 * Start forwarding to and from the tunnel. 339 * Start forwarding to and from the tunnel.
@@ -357,7 +355,7 @@ run (int fd_tun)
357 */ 355 */
358 unsigned char bufin[MAX_SIZE]; 356 unsigned char bufin[MAX_SIZE];
359 ssize_t bufin_size = 0; 357 ssize_t bufin_size = 0;
360 size_t bufin_rpos = 0; 358 ssize_t bufin_rpos = 0;
361 unsigned char *bufin_read = NULL; 359 unsigned char *bufin_read = NULL;
362 /* Hello, I am a stub function! I did my job, yay me! */ 360 /* Hello, I am a stub function! I did my job, yay me! */
363 361
@@ -369,7 +367,7 @@ run (int fd_tun)
369 * 367 *
370 * @param argc must be 6 368 * @param argc must be 6
371 * @param argv 0: binary name (gnunet-helper-vpn) 369 * @param argv 0: binary name (gnunet-helper-vpn)
372 * 1: tunnel interface name (gnunet-vpn) 370 * 1: tunnel interface name (gnunet-vpn) (unused, can be used as additional HWID in the future)
373 * 2: IPv6 address (::1), "-" to disable 371 * 2: IPv6 address (::1), "-" to disable
374 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-" 372 * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
375 * 4: IPv4 address (1.2.3.4), "-" to disable 373 * 4: IPv4 address (1.2.3.4), "-" to disable
@@ -378,7 +376,7 @@ run (int fd_tun)
378int 376int
379main (int argc, char **argv) 377main (int argc, char **argv)
380{ 378{
381 //char dev[IFNAMSIZ]; 379 TCHAR hwid[LINE_LEN];
382 int fd_tun; 380 int fd_tun;
383 int global_ret; 381 int global_ret;
384 382
@@ -388,21 +386,19 @@ main (int argc, char **argv)
388 return 1; 386 return 1;
389 } 387 }
390 388
391 /* 389 strncpy (hwid, argv[1], LINE_LEN);
392 * strncpy (dev, argv[1], IFNAMSIZ); 390 hwid[LINE_LEN - 1] = _T('\0');
393 * dev[IFNAMSIZ - 1] = '\0'; 391
394 */ 392 if (-1 == (fd_tun = init_tun (hwid)))
395 /* if (-1 == (fd_tun = init_tun (dev)))
396 { 393 {
397 fprintf (stderr, "Fatal: could not initialize tun-interface with IPv6 %s/%s and IPv4 %s/%s\n", 394 fprintf (stderr, "Fatal: could not initialize virtual-interface %s with IPv6 %s/%s and IPv4 %s/%s\n",
398 dev, 395 hwid,
399 argv[2], 396 argv[2],
400 argv[3], 397 argv[3],
401 argv[4], 398 argv[4],
402 argv[5]); 399 argv[5]);
403 return 1; 400 return 1;
404 } 401 }
405 */
406 402
407 if (0 != strcmp (argv[2], "-")) 403 if (0 != strcmp (argv[2], "-"))
408 { 404 {
@@ -415,7 +411,7 @@ main (int argc, char **argv)
415 return 1; 411 return 1;
416 } 412 }
417 413
418 //set_address6 (dev, address, prefix_len); 414 set_address6 (address, prefix_len);
419 } 415 }
420 416
421 if (0 != strcmp (argv[4], "-")) 417 if (0 != strcmp (argv[4], "-"))