aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-helper-vpn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vpn/gnunet-helper-vpn.c')
-rw-r--r--src/vpn/gnunet-helper-vpn.c124
1 files changed, 94 insertions, 30 deletions
diff --git a/src/vpn/gnunet-helper-vpn.c b/src/vpn/gnunet-helper-vpn.c
index c2ebe6c7b..25a9492eb 100644
--- a/src/vpn/gnunet-helper-vpn.c
+++ b/src/vpn/gnunet-helper-vpn.c
@@ -26,36 +26,100 @@
26 * @author Philipp Tölke 26 * @author Philipp Tölke
27 */ 27 */
28#include <platform.h> 28#include <platform.h>
29#include <linux/if_tun.h>
29 30
30#include "gnunet-vpn-tun.h" 31/**
32 * Need 'struct GNUNET_MessageHeader'.
33 */
31#include "gnunet_common.h" 34#include "gnunet_common.h"
32#include "gnunet_protocols.h"
33#include "gnunet-vpn-helper-p.h"
34 35
35#ifndef _LINUX_IN6_H 36/**
37 * Need VPN message types.
38 */
39#include "gnunet_protocols.h"
36 40
37#define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader)) 41/**
42 * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
43 */
44#define MAX_SIZE 65536
38 45
39// This is in linux/include/net/ipv6.h. 46#ifndef _LINUX_IN6_H
40struct in6_ifreq 47/**
48 * This is in linux/include/net/ipv6.h, but not always exported...
49 */
50struct in6_ifreq
41{ 51{
42 struct in6_addr ifr6_addr; 52 struct in6_addr ifr6_addr;
43 uint32_t ifr6_prefixlen; 53 uint32_t ifr6_prefixlen;
44 unsigned int ifr6_ifindex; 54 unsigned int ifr6_ifindex;
45}; 55};
46
47#endif 56#endif
48 57
58
59struct suid_packet
60{
61 struct GNUNET_MessageHeader hdr;
62 unsigned char data[1];
63}
64
49static int running = 1; 65static int running = 1;
50 66
51static void 67static void
52term (int sig) 68term (int sig)
53{ 69{
54 fprintf (stderr, "Got SIGTERM...\n"); 70 fprintf (stderr,
71 "Got SIGTERM...\n");
55 if (sig == SIGTERM) 72 if (sig == SIGTERM)
56 running = 0; 73 running = 0;
57} 74}
58 75
76
77/**
78 * Creates a tun-interface called dev;
79 * @param dev is asumed to point to a char[IFNAMSIZ]
80 * if *dev == '\0', uses the name supplied by the kernel
81 * @return the fd to the tun or -1 on error
82 */
83static int
84init_tun (char *dev)
85{
86 struct ifreq ifr;
87 int fd;
88
89 if (NULL == dev)
90 {
91 errno = EINVAL;
92 return -1;
93 }
94
95 if (-1 == (fd = open("/dev/net/tun", O_RDWR)))
96 {
97 fprintf (stderr,
98 "Error opening `%s': %s\n",
99 "/dev/net/tun",
100 strerror(errno));
101 return -1;
102 }
103
104 memset(&ifr, 0, sizeof(ifr));
105 ifr.ifr_flags = IFF_TUN;
106
107 if ('\0' == *dev)
108 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
109
110 if (-1 == ioctl(fd, TUNSETIFF, (void *) &ifr))
111 {
112 fprintf (stderr,
113 "Error with ioctl on `%s': %s\n",
114 "/dev/net/tun",
115 strerror(errno));
116 close(fd);
117 return -1;
118 }
119 strcpy(dev, ifr.ifr_name);
120 return fd;
121}
122
59/** 123/**
60 * @brief Sets the IPv6-Address given in address on the interface dev 124 * @brief Sets the IPv6-Address given in address on the interface dev
61 * 125 *
@@ -65,7 +129,7 @@ term (int sig)
65 */ 129 */
66static void 130static void
67set_address6 (char *dev, char *address, unsigned long prefix_len) 131set_address6 (char *dev, char *address, unsigned long prefix_len)
68{ /* {{{ */ 132{
69 int fd = socket (AF_INET6, SOCK_DGRAM, 0); 133 int fd = socket (AF_INET6, SOCK_DGRAM, 0);
70 134
71 if (fd < 0) 135 if (fd < 0)
@@ -111,9 +175,9 @@ set_address6 (char *dev, char *address, unsigned long prefix_len)
111 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 175 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
112 (void) ioctl (fd, SIOCSIFFLAGS, &ifr); 176 (void) ioctl (fd, SIOCSIFFLAGS, &ifr);
113 close (fd); 177 close (fd);
114} /* }}} */ 178}
179
115 180
116static void
117/** 181/**
118 * @brief Sets the IPv4-Address given in address on the interface dev 182 * @brief Sets the IPv4-Address given in address on the interface dev
119 * 183 *
@@ -121,8 +185,9 @@ static void
121 * @param address the IPv4-Address 185 * @param address the IPv4-Address
122 * @param mask the netmask 186 * @param mask the netmask
123 */ 187 */
188static void
124set_address4 (char *dev, char *address, char *mask) 189set_address4 (char *dev, char *address, char *mask)
125{ /* {{{ */ 190{
126 int fd = 0; 191 int fd = 0;
127 struct sockaddr_in *addr; 192 struct sockaddr_in *addr;
128 struct ifreq ifr; 193 struct ifreq ifr;
@@ -175,7 +240,8 @@ set_address4 (char *dev, char *address, char *mask)
175 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 240 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
176 (void) ioctl (fd, SIOCSIFFLAGS, &ifr); 241 (void) ioctl (fd, SIOCSIFFLAGS, &ifr);
177 close (fd); 242 close (fd);
178} /* }}} */ 243}
244
179 245
180/** 246/**
181 * @brief sets the socket to nonblocking 247 * @brief sets the socket to nonblocking
@@ -186,22 +252,20 @@ static void
186setnonblocking (int fd) 252setnonblocking (int fd)
187{ /*{{{ */ 253{ /*{{{ */
188 int opts; 254 int opts;
255 opts = fcntl(fd,F_GETFL);
256 if (opts < 0) {
257 perror("fcntl(F_GETFL)");
258 }
259 opts = (opts | O_NONBLOCK);
260 if (fcntl(fd,F_SETFL,opts) < 0) {
261 perror("fcntl(F_SETFL)");
262 }
263 return;
264}
189 265
190 opts = fcntl (fd, F_GETFL);
191 if (opts < 0)
192 {
193 perror ("fcntl(F_GETFL)");
194 }
195 opts = (opts | O_NONBLOCK);
196 if (fcntl (fd, F_SETFL, opts) < 0)
197 {
198 perror ("fcntl(F_SETFL)");
199 }
200 return;
201} /*}}} */
202 266
203int 267int
204main (int argc, char **argv) 268main(int argc, char** argv)
205{ 269{
206 unsigned char buf[MAX_SIZE]; 270 unsigned char buf[MAX_SIZE];
207 271