aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-helper-vpn-windows.c
blob: 74e06430a036d584b0cd42849c1031ce17c5c435 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
     This file is part of GNUnet.
     (C) 2010, 2012 Christian Grothoff

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */

/**
 * @file vpn/gnunet-helper-vpn-windows.c
 * @brief the helper for the VPN service in win32 builds. 
 * Opens a virtual network-interface, sends data received on the if to stdout, 
 * sends data received on stdin to the interface
 * @author Christian M. Fuchs
 *
 * The following list of people have reviewed this code and considered
 * it safe since the last modification (if you reviewed it, please
 * have your name added to the list):
 *
 */

#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include "platform.h"

//#include <tchar.h>
//#include <stdlib.h>
//#include <regstr.h>
//#include <string.h>
//#include <malloc.h>
//#include <objbase.h>

/**
 * Need 'struct GNUNET_MessageHeader'.
 */
#include "gnunet_common.h"

/**
 * Need VPN message types.
 */
#include "gnunet_protocols.h"

/**
 * Should we print (interesting|debug) messages that can happen during
 * normal operation?
 */
#define DEBUG GNUNET_NO

/**
 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
 */
#define MAX_SIZE 65536

/** 
 * This is our own local instance of a virtual network interface
 * It is (somewhat) equivalent to using tun/tap in unixoid systems
 * 
 * Upon initialization, we create such an device node.
 * Upon termination, we remove it again.
 * 
 * If we crash this device might stay around.
 */
static HDEVINFO DeviceInfo = INVALID_HANDLE_VALUE;

/**
 * FIXME
 */
static SP_DEVINFO_DATA DeviceNode;

/**
 * FIXME
 */
static TCHAR class[128];

/**
 * FIXME
 */
static GUID guid;


/**
 * Creates a tun-interface called dev;
 *
 * @param dev is asumed to point to a char[IFNAMSIZ]
 *        if *dev == '\\0', uses the name supplied by the kernel;
 * @return the fd to the tun or -1 on error
 */
static int
init_tun (char *dev)
{
  int fd;

  if (NULL == dev)
    {
      errno = EINVAL;
      return -1;
    }

  /* Hello, I am a stub function! I did my job, yay me! */

  return fd;
}


/**
 * @brief Sets the IPv6-Address given in address on the interface dev
 *
 * @param dev the interface to configure
 * @param address the IPv6-Address
 * @param prefix_len the length of the network-prefix
 */
static void
set_address6 (const char *dev, const char *address, unsigned long prefix_len)
{
  int fd = -1;

  /*
   * parse the new address
   */

  /*
   * Get the index of the if
   */

  /*
   * Set the address
   */

  /*
   * Get the flags
   */


  /*
   * Add the UP and RUNNING flags
   */


  if (0 != close (fd))
    {
      fprintf (stderr, "close failed: %s\n", strerror (errno));
      exit (1);
    }
}

/**
 * @brief Sets the IPv4-Address given in address on the interface dev
 *
 * @param dev the interface to configure
 * @param address the IPv4-Address
 * @param mask the netmask
 */
static void
set_address4 (const char *dev, const char *address, const char *mask)
{
  int fd = -1;

  /*
   * Parse the address
   */

  /*
   * Set the address
   */

  /*
   * Parse the netmask
   */


  /*
   * Set the netmask
   */


  /*
   * Get the flags
   */


  /*
   * Add the UP and RUNNING flags
   */


  if (0 != close (fd))
    {
      fprintf (stderr, "close failed: %s\n", strerror (errno));
      (void) close (fd);
      exit (1);
    }
}


/**
 * FIXME.
 */
static boolean
setup_interface ()
{
  /*
   * where to find our inf-file. (+ the "full" path, after windows found")
   * 
   * We do not directly input all the props here, because openvpn will update
   * these details over time.
   */
  TCHAR InfFile[] = _T("tapw32.inf"); // FIXME: inline or #define, no 'variable'
  TCHAR hwid[] = _T("TAP0901");
  TCHAR InfFilePath[MAX_PATH];
  TCHAR hwIdList[LINE_LEN + 4];

#if DEAD
  /** 
   * Locate the inf-file, we need to store it somewhere where the system can
   * find it. A good choice would be CWD/PDW or %WINDIR$\system32\
   * 
   * TODO: Finde a more sane way to do this!
   */

  if (NULL == (InfFilePath = calloc (MAX_PATH, sizeof (TCHAR))))
      return FALSE;
#endif
  GetFullPathName (InfFile, MAX_PATH, InfFilePath, NULL);

#if DEAD
  /** 
   * Set the device's hardware ID and add it to a list.
   * This information will later on identify this device in registry. 
   * 
   * TODO: Currently we just use TAP0901 as HWID, 
   * but we might want to add additional information
   */
  hwIdList = calloc (LINE_LEN + 4, sizeof (TCHAR));
  if (hwIdList == NULL)
    {
      goto cleanup1;
    }
#endif
  strncpy (hwIdList, hwid, LINE_LEN);

  /** 
   * Bootstrap our device info using the drivers inf-file
   */
  if (!SetupDiGetINFClass (InfFilePath,
                           &guid,
                           class, sizeof (class) / sizeof (TCHAR),
                           NULL))
    {
      goto cleanup2;
    }

  /** 
   * Collect all the other needed information... 
   * let the system fill our this form 
   */
  DeviceInfo = SetupDiCreateDeviceInfoList (&guid, NULL);
  if (DeviceInfo == INVALID_HANDLE_VALUE)
    {
      goto cleanup3;
    }
  DeviceNode.cbSize = sizeof (SP_DEVINFO_DATA);
  if (!SetupDiCreateDeviceInfo (DeviceInfo,
                                class,
                                &guid,
                                NULL,
                                NULL,
                                DICD_GENERATE_ID,
                                &DeviceNode))
    {
      goto cleanup3;
    }

  /* Deploy all the information collected into the registry */
  if (!SetupDiSetDeviceRegistryProperty (DeviceInfo,
                                         &DeviceNode,
                                         SPDRP_HARDWAREID,
                                         (LPBYTE) hwIdList,
                                         (lstrlen (hwIdList) + 2) * sizeof (TCHAR)))
    {
      goto cleanup3;
    }
  /* Install our new class(=device) into the system */
  if (SetupDiCallClassInstaller (DIF_REGISTERDEVICE,
                                 DeviceInfo,
                                 &DeviceNode))
    {
      return TRUE;
    }

  //disabled for debug-reasons...
cleanup3:
  //GNUNET_free(DeviceInfo);
  ;
cleanup2:
  //GNUNET_free(hwIdList);
  ;
cleanup1:
  //GNUNET_free(InfFilePath);
  ;
  return FALSE;

}


static boolean
remove_interface ()
{
  SP_REMOVEDEVICE_PARAMS remove;

  remove.ClassInstallHeader.cbSize = sizeof (SP_CLASSINSTALL_HEADER);
  remove.HwProfile = 0;
  remove.Scope = DI_REMOVEDEVICE_GLOBAL;
  remove.ClassInstallHeader.InstallFunction = DIF_REMOVE;
  if (! SetupDiSetClassInstallParams (DeviceInfo,
				      (PSP_DEVINFO_DATA) &DeviceNode,
				      &remove.ClassInstallHeader,
				      sizeof (remove)))
    return FALSE;
  if (! SetupDiCallClassInstaller (DIF_REMOVE, 
				   DeviceInfo, 
				   (PSP_DEVINFO_DATA) &DeviceNode))
    return FALSE;
  return TRUE;
}


/**
 * Start forwarding to and from the tunnel.
 *
 * @param fd_tun tunnel FD
 */
static void
run (int fd_tun)
{
  /*
   * The buffer filled by reading from fd_tun
   */
  unsigned char buftun[MAX_SIZE];
  ssize_t buftun_size = 0;
  unsigned char *buftun_read = NULL;

  /*
   * The buffer filled by reading from stdin
   */
  unsigned char bufin[MAX_SIZE];
  ssize_t bufin_size = 0;
  size_t bufin_rpos = 0;
  unsigned char *bufin_read = NULL;
  /* Hello, I am a stub function! I did my job, yay me! */

}


/**
 * Open VPN tunnel interface.
 *
 * @param argc must be 6
 * @param argv 0: binary name (gnunet-helper-vpn)
 *             1: tunnel interface name (gnunet-vpn)
 *             2: IPv6 address (::1), "-" to disable
 *             3: IPv6 netmask length in bits (64), ignored if #2 is "-"
 *             4: IPv4 address (1.2.3.4), "-" to disable
 *             5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
 */
int
main (int argc, char **argv)
{
  //char dev[IFNAMSIZ];
  int fd_tun;
  int global_ret;

  if (6 != argc)
    {
      fprintf (stderr, "Fatal: must supply 5 arguments!\n");
      return 1;
    }

  /*
   * strncpy (dev, argv[1], IFNAMSIZ);
   * dev[IFNAMSIZ - 1] = '\0';
   */
  /*  if (-1 == (fd_tun = init_tun (dev)))
    {
      fprintf (stderr, "Fatal: could not initialize tun-interface  with IPv6 %s/%s and IPv4 %s/%s\n",
               dev,
               argv[2],
               argv[3],
               argv[4],
               argv[5]);
      return 1;
    }
   */

  if (0 != strcmp (argv[2], "-"))
    {
      const char *address = argv[2];
      long prefix_len = atol (argv[3]);

      if ((prefix_len < 1) || (prefix_len > 127))
        {
          fprintf (stderr, "Fatal: prefix_len out of range\n");
          return 1;
        }

      //set_address6 (dev, address, prefix_len);
    }

  if (0 != strcmp (argv[4], "-"))
    {
      const char *address = argv[4];
      const char *mask = argv[5];

      set_address4 (NULL, address, mask);
    }

  if (setup_interface ())
    {
      ;
    }

  /*
  uid_t uid = getuid ();
  if (0 != setresuid (uid, uid, uid))
  {
    fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
    global_ret = 2;
    goto cleanup;
  }
   */

  /*if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
  {
    fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
             strerror (errno));
    // no exit, we might as well die with SIGPIPE should it ever happen 
  }
   */
  //run (fd_tun);
  global_ret = 0;
cleanup:
  //close (fd_tun);
  return global_ret;
}