From: Philipp Tölke Date: Tue, 20 Jul 2010 05:45:18 +0000 (+0000) Subject: Begin implementing the gnunet-vpn-helper X-Git-Tag: initial-import-from-subversion-38251~20926 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=425e80a6d25faa505513cacd10a3667a48c62d1e;p=oweals%2Fgnunet.git Begin implementing the gnunet-vpn-helper --- diff --git a/src/vpn/Makefile.am b/src/vpn/Makefile.am index 1fff59309..480d58145 100644 --- a/src/vpn/Makefile.am +++ b/src/vpn/Makefile.am @@ -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 index 000000000..d2fac593a --- /dev/null +++ b/src/vpn/gnunet-vpn-helper-p.h @@ -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 index 000000000..66af3ca51 --- /dev/null +++ b/src/vpn/gnunet-vpn-helper.c @@ -0,0 +1,82 @@ +#define _GNU_SOURCE +#include +#include + +#include +#include +#include + +#include + +#include +#include + +#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; +} diff --git a/src/vpn/tun.c b/src/vpn/tun.c index d3c38bb4d..e3854495f 100644 --- a/src/vpn/tun.c +++ b/src/vpn/tun.c @@ -13,83 +13,39 @@ #include #include -#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; - } - } -} +}}}