Begin implementing the gnunet-vpn-helper
authorPhilipp Tölke <toelke@in.tum.de>
Tue, 20 Jul 2010 05:45:18 +0000 (05:45 +0000)
committerPhilipp Tölke <toelke@in.tum.de>
Tue, 20 Jul 2010 05:45:18 +0000 (05:45 +0000)
src/vpn/Makefile.am
src/vpn/gnunet-vpn-helper-p.h [new file with mode: 0644]
src/vpn/gnunet-vpn-helper.c [new file with mode: 0644]
src/vpn/tun.c

index 1fff59309175a9c958edc21f60d9f710d6aa8463..480d58145b894be9807ae3a5cf284b06404bbbb5 100644 (file)
@@ -23,12 +23,17 @@ bin_PROGRAMS = \
 
 
 gnunet_vpn_helper_SOURCES = \
- debug.c  debug.h \
- packet.h packet.c \
- pretty-print.c  pretty-print.h \
- tcp.c  tcp.h \
- test.c \
- tun.c  tun.h
+ gnunet-vpn-helper.c \
+ gnunet-vpn-helper-p.h \
+ tun.h tun.c
+
+# debug.c  debug.h \
+# packet.h packet.c \
+# pretty-print.c  pretty-print.h \
+# tcp.c  tcp.h \
+# test.c \
+# tun.c  tun.h \
+# udp.c udp.h
 
 gnunet_daemon_vpn_SOURCES = \
  gnunet-daemon-vpn.c
diff --git a/src/vpn/gnunet-vpn-helper-p.h b/src/vpn/gnunet-vpn-helper-p.h
new file mode 100644 (file)
index 0000000..d2fac59
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef GN_VPN_HELPER_P_H
+#define GN_VPN_HELPER_P_H
+
+struct suid_packet {
+       unsigned int size;
+       unsigned char data[1];
+};
+
+#endif
diff --git a/src/vpn/gnunet-vpn-helper.c b/src/vpn/gnunet-vpn-helper.c
new file mode 100644 (file)
index 0000000..66af3ca
--- /dev/null
@@ -0,0 +1,82 @@
+#define _GNU_SOURCE
+#include <arpa/inet.h>
+#include <linux/if.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+
+#include <string.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "gnunet-vpn-helper-p.h"
+#include "tun.h"
+
+#ifndef _LINUX_IN6_H
+// This is in linux/include/net/ipv6.h.
+
+struct in6_ifreq {
+    struct in6_addr ifr6_addr;
+    __u32 ifr6_prefixlen;
+    unsigned int ifr6_ifindex;
+};
+
+#endif
+
+static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */
+       int fd = socket(AF_INET6, SOCK_DGRAM, 0);
+
+       struct ifreq ifr;
+       struct in6_ifreq ifr6;
+
+       struct sockaddr_in6 sa6;
+       memset(&sa6, 0, sizeof(struct sockaddr_in6));
+
+       sa6.sin6_family = AF_INET6;
+
+       /* FIXME */ inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr);
+
+       memcpy((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr, sizeof(struct in6_addr));
+
+       strncpy(ifr.ifr_name, dev, IFNAMSIZ);
+
+       if (ioctl(fd, SIOGIFINDEX, &ifr) < 0) {
+               perror("SIOGIFINDEX");
+       }
+
+       ifr6.ifr6_ifindex = ifr.ifr_ifindex;
+       ifr6.ifr6_prefixlen = prefix_len;
+
+       if (ioctl(fd, SIOCSIFADDR, &ifr6) < 0) {
+               perror("SIOCSIFADDR");
+       }
+
+       /* FIXME */ ioctl(fd, SIOCGIFFLAGS, &ifr);
+       ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
+       /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
+} /* }}} */
+
+int main(int argc, char** argv) {
+       char dev[IFNAMSIZ];
+       memset(dev, 0, IFNAMSIZ);
+
+       int fd_tun = init_tun(dev);
+       fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun);
+
+       // TODO: get this out of argv
+       char address[] = "1234::1";
+       unsigned long prefix_len = 8;
+
+       set_address(dev, address, prefix_len);
+
+       uid_t uid = getuid ();
+       if (setresuid (uid, uid, uid) != 0 )
+               fprintf (stderr, "Failed to setresuid: %m\n");
+
+       // Wait
+       read(0, dev, 10);
+
+       return 0;
+}
index d3c38bb4d266c5d13e87311e7614857a69d73c62..e3854495ff1088f6a22688ca050a16c49afd5e5d 100644 (file)
 #include <errno.h>
 #include <stdlib.h>
 
-#include "debug.h"
-
 /**
  * Creates a tun-interface called dev;
+ * dev is asumed to point to a char[IFNAMSIZ]
  * if *dev == 0, uses the name supplied by the kernel
  * returns the fd to the tun or -1
  */
-int init_tun(char *dev) { /*{{{*/
+int init_tun(char *dev) {{{
+       if (!dev) {
+               errno = EINVAL;
+               return -1;
+       }
+
        struct ifreq ifr;
        int fd, err;
 
        if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
-               debug(1, 0, "opening /dev/net/tun: %s\n", strerror(errno));
+               fprintf(stderr, "opening /dev/net/tun: %m\n");
                return -1;
        }
 
        memset(&ifr, 0, sizeof(ifr));
 
-       ifr.ifr_flags = IFF_TUN; 
-       if(dev)
+       ifr.ifr_flags = IFF_TUN;
+
+       if (*dev)
                strncpy(ifr.ifr_name, dev, IFNAMSIZ);
 
        if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
                close(fd);
-               debug(1, 0, "ioctl'ing /dev/net/tun: %s\n", strerror(errno));
+               fprintf(stderr, "ioctl'ing /dev/net/tun: %m\n");
                return err;
        }
+
        strcpy(dev, ifr.ifr_name);
        return fd;
-} /*}}}*/
-
-void n2o(int fd) {
-       char buf[1024];
-       int r, w;
-       for(;;) {
-               r = read(fd, buf, 1024);
-               if (r < 0) {
-                       fprintf(stderr, "n2o read: %s\n", strerror(errno));
-                       exit(1);
-               }
-               if (r == 0) {
-                       close(fd);
-                       exit(0);
-               }
-               while (r > 0) {
-                       w = write(1, buf, r);
-                       if (w < 0) {
-                               fprintf(stderr, "n2o write: %s\n", strerror(errno));
-                               close(fd);
-                               exit(1);
-                       }
-                       r -= w;
-               }
-       }
-}
-
-void o2n(int fd) {
-       char buf[1024];
-       int r, w;
-       for(;;) {
-               r = read(0, buf, 1024);
-               if (r < 0) {
-                       fprintf(stderr, "o2n read: %s\n", strerror(errno));
-                       exit(1);
-               }
-               if (r == 0) {
-                       close(fd);
-                       exit(0);
-               }
-               while (r > 0) {
-                       w = write(fd, buf, r);
-                       if (w < 0) {
-                               fprintf(stderr, "o2n write: %s\n", strerror(errno));
-                               close(fd);
-                               exit(1);
-                       }
-                       r -= w;
-               }
-       }
-}
+}}}