55 #define DEBUG GNUNET_NO
60 #define MAX_SIZE 65536
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;
411 while ((1 == read_open) && (1 == write_open))
420 if (read_open && (0 == buftun_size))
421 FD_SET (fd_tun, &fds_r);
427 if (read_open && (0 != buftun_size))
434 if (write_open && (NULL == bufin_read))
441 if (write_open && (NULL != bufin_read))
442 FD_SET (fd_tun, &fds_w);
444 int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
451 "select failed: %s\n",
458 if (FD_ISSET (fd_tun, &fds_r))
463 if (-1 == buftun_size)
468 shutdown (fd_tun, SHUT_RD);
469 shutdown (1, SHUT_WR);
473 else if (0 == buftun_size)
475 fprintf (stderr,
"EOF on tun\n");
476 shutdown (fd_tun, SHUT_RD);
477 shutdown (1, SHUT_WR);
483 buftun_read = buftun;
488 hdr->
size = htons (buftun_size);
491 else if (FD_ISSET (1, &fds_w))
493 ssize_t written = write (1,
503 "write-error to stdout: %s\n",
505 shutdown (fd_tun, SHUT_RD);
506 shutdown (1, SHUT_WR);
510 else if (0 == written)
513 "write returned 0!?\n");
518 buftun_size -= written;
519 buftun_read += written;
523 if (FD_ISSET (0, &fds_r))
525 bufin_size = read (0, bufin + bufin_rpos,
MAX_SIZE - bufin_rpos);
526 if (-1 == bufin_size)
531 shutdown (0, SHUT_RD);
532 shutdown (fd_tun, SHUT_WR);
536 else if (0 == bufin_size)
539 fprintf (stderr,
"EOF on stdin\n");
541 shutdown (0, SHUT_RD);
542 shutdown (fd_tun, SHUT_WR);
551 bufin_rpos += bufin_size;
558 "protocol violation!\n");
561 if (ntohs (hdr->
size) > bufin_rpos)
568 else if (FD_ISSET (fd_tun, &fds_w))
570 ssize_t written = write (fd_tun,
577 "write-error to tun: %s\n",
579 shutdown (0, SHUT_RD);
580 shutdown (fd_tun, SHUT_WR);
584 else if (0 == written)
586 fprintf (stderr,
"write returned 0!?\n");
591 bufin_size -= written;
592 bufin_read += written;
595 memmove (bufin, bufin_read, bufin_rpos);
627 fprintf (stderr,
"Fatal: must supply 5 arguments!\n");
634 dev[IFNAMSIZ - 1] =
'\0';
636 if (-1 == (fd_tun =
init_tun (dev)))
639 "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
648 if (0 != strcmp (argv[2],
"-"))
651 long prefix_len = atol (argv[3]);
653 if ((prefix_len < 1) || (prefix_len > 127))
656 "Fatal: prefix_len out of range\n");
657 (void) close (fd_tun);
666 if (0 != strcmp (argv[4],
"-"))
669 const char *mask = argv[5];
674 uid_t uid = getuid ();
675 #ifdef HAVE_SETRESUID
676 if (0 != setresuid (uid, uid, uid))
679 "Failed to setresuid: %s\n",
685 if (0 != (setuid (uid) | seteuid (uid)))
688 "Failed to setuid: %s\n",
695 if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
698 "Failed to protect against SIGPIPE: %s\n",
705 (void) close (fd_tun);
static char * address
GNS address for this phone.
static void cleanup(void *cls)
Function scheduled as very last function, cleans up after us.
#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.
static int global_ret
Return value from main.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Constants for network protocols.
#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