55#define DEBUG GNUNET_NO
62#if ! defined(__ANDROID__)
96 if (-1 == (fd = open (
"/dev/net/tun", O_RDWR)))
99 "Error opening `%s': %s\n",
105 if (fd >= FD_SETSIZE)
108 "File descriptor to large: %d",
114 memset (&ifr, 0,
sizeof(ifr));
115 ifr.ifr_flags = IFF_TUN;
118 strncpy (ifr.ifr_name,
127 "Error with ioctl on `%s': %s\n",
133 strcpy (dev, ifr.ifr_name);
148 unsigned long prefix_len)
152 struct sockaddr_in6 sa6;
158 memset (&sa6, 0,
sizeof(
struct sockaddr_in6));
159 sa6.sin6_family = AF_INET6;
160 if (1 != inet_pton (AF_INET6,
address, sa6.sin6_addr.s6_addr))
163 "Failed to parse IPv6 address `%s'\n",
168 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
171 "Error creating socket: %s\n",
176 memset (&ifr, 0,
sizeof(
struct ifreq));
180 strncpy (ifr.ifr_name,
188 "ioctl failed at %d: %s\n",
195 memset (&ifr6, 0,
sizeof(
struct in6_ifreq));
208 "ioctl failed at line %d: %s\n",
223 "ioctl failed at line %d: %s\n",
233 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
239 "ioctl failed at line %d: %s\n",
249 "close failed at line %d: %s\n",
270 struct sockaddr_in *addr;
273 memset (&ifr, 0,
sizeof(
struct ifreq));
274 addr = (
struct sockaddr_in *) &(ifr.ifr_addr);
275 addr->sin_family = AF_INET;
280 if (1 != inet_pton (AF_INET,
282 &addr->sin_addr.s_addr))
285 "Failed to parse IPv4 address `%s'\n",
290 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
293 "Error creating socket: %s\n",
298 strncpy (ifr.ifr_name, dev, IFNAMSIZ);
303 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
306 "ioctl failed at %d: %s\n",
316 addr = (
struct sockaddr_in *) &(ifr.ifr_netmask);
317 if (1 != inet_pton (AF_INET,
319 &addr->sin_addr.s_addr))
322 "Failed to parse IPv4 address mask `%s'\n",
331 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
334 "ioctl failed at line %d: %s\n",
344 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
347 "ioctl failed at line %d: %s\n",
357 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
358 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
361 "ioctl failed at line %d: %s\n",
371 "close failed at line %d: %s\n",
391 ssize_t buftun_size = 0;
392 unsigned char *buftun_read = NULL;
398 ssize_t bufin_size = 0;
399 size_t bufin_rpos = 0;
400 unsigned char *bufin_read = NULL;
412 while ((1 == read_open) && (1 == write_open))
421 if (read_open && (0 == buftun_size))
422 FD_SET (fd_tun, &fds_r);
428 if (read_open && (0 != buftun_size))
435 if (write_open && (NULL == bufin_read))
442 if (write_open && (NULL != bufin_read))
443 FD_SET (fd_tun, &fds_w);
445 r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
452 "select failed: %s\n",
459 if (FD_ISSET (fd_tun, &fds_r))
464 if (-1 == buftun_size)
469 shutdown (fd_tun, SHUT_RD);
470 shutdown (1, SHUT_WR);
474 else if (0 == buftun_size)
476 fprintf (stderr,
"EOF on tun\n");
477 shutdown (fd_tun, SHUT_RD);
478 shutdown (1, SHUT_WR);
486 buftun_read = buftun;
489 hdr->
size = htons (buftun_size);
492 else if (FD_ISSET (1, &fds_w))
494 ssize_t written = write (1,
504 "write-error to stdout: %s\n",
506 shutdown (fd_tun, SHUT_RD);
507 shutdown (1, SHUT_WR);
511 else if (0 == written)
514 "write returned 0!?\n");
519 buftun_size -= written;
520 buftun_read += written;
524 if (FD_ISSET (0, &fds_r))
526 bufin_size = read (0, bufin + bufin_rpos,
MAX_SIZE - bufin_rpos);
527 if (-1 == bufin_size)
532 shutdown (0, SHUT_RD);
533 shutdown (fd_tun, SHUT_WR);
537 else if (0 == bufin_size)
540 fprintf (stderr,
"EOF on stdin\n");
542 shutdown (0, SHUT_RD);
543 shutdown (fd_tun, SHUT_WR);
552 bufin_rpos += bufin_size;
559 "protocol violation!\n");
562 if (ntohs (hdr->
size) > bufin_rpos)
569 else if (FD_ISSET (fd_tun, &fds_w))
571 ssize_t written = write (fd_tun,
578 "write-error to tun: %s\n",
580 shutdown (0, SHUT_RD);
581 shutdown (fd_tun, SHUT_WR);
585 else if (0 == written)
587 fprintf (stderr,
"write returned 0!?\n");
592 bufin_size -= written;
593 bufin_read += written;
596 memmove (bufin, bufin_read, bufin_rpos);
628 fprintf (stderr,
"Fatal: must supply 5 arguments!\n");
635 dev[IFNAMSIZ - 1] =
'\0';
637 if (-1 == (fd_tun =
init_tun (dev)))
640 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
649 if (0 != strcmp (argv[2],
"-"))
652 long prefix_len = atol (argv[3]);
654 if ((prefix_len < 1) || (prefix_len > 127))
657 "Fatal: prefix_len out of range\n");
658 (void) close (fd_tun);
667 if (0 != strcmp (argv[4],
"-"))
670 const char *mask = argv[5];
677 uid_t uid = getuid ();
678 if (0 != setresuid (uid, uid, uid))
681 "Failed to setresuid: %s\n",
688 if (0 != (setuid (uid) | seteuid (uid)))
691 "Failed to setuid: %s\n",
698 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
701 "Failed to protect against SIGPIPE: %s\n",
708 (void) close (fd_tun);
static char * address
GNS address for this phone.
static int global_ret
Global status value.
static void cleanup(void *cls)
Disconnect and shutdown.
#define MAX_SIZE
Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
static void run(int fd_tun)
Start forwarding to and from the tunnel.
int main(int argc, char **argv)
Open VPN tunnel interface.
static int init_tun(char *dev)
Creates a tun-interface called dev;.
static void set_address4(const char *dev, const char *address, const char *mask)
Sets the IPv4-Address given in address on the interface dev.
static void set_address6(const char *dev, const char *address, unsigned long prefix_len)
Sets the IPv6-Address given in address on the interface dev.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Constants for network protocols.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_MESSAGE_TYPE_VPN_HELPER
Type of messages between the gnunet-vpn-helper and the daemon.
This is in linux/include/net/ipv6.h, but not always exported...
struct in6_addr ifr6_addr
unsigned int ifr6_ifindex