/*
This file is part of GNUnet.
- (C) 2010 Christian Grothoff
+ (C) 2010, 2012 Christian Grothoff
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
*/
/**
- * @file vpn/gnunet-daemon-vpn.c
- * @brief the helper for various vpn-daemons. Opens a virtual network-interface,
+ * @file vpn/gnunet-helper-vpn.c
+ * @brief the helper for the VPN service. Opens a virtual network-interface,
* sends data received on the if to stdout, sends data received on stdin to the
* interface
* @author Philipp Tölke
+ * @author Christian Grothoff
+ *
+ * The following list of people have reviewed this code and considered
+ * it safe since the last modification (if you reviewed it, please
+ * have your name added to the list):
+ *
+ * - Philipp Tölke
*/
-#include <platform.h>
+#include "platform.h"
+#include <linux/if_tun.h>
-#include "gnunet-vpn-tun.h"
+/**
+ * Need 'struct GNUNET_MessageHeader'.
+ */
+#include "gnunet_crypto_lib.h"
#include "gnunet_common.h"
+
+/**
+ * Need VPN message types.
+ */
#include "gnunet_protocols.h"
-#include "gnunet-vpn-helper-p.h"
-#ifndef _LINUX_IN6_H
+/**
+ * Should we print (interesting|debug) messages that can happen during
+ * normal operation?
+ */
+#define DEBUG GNUNET_NO
-#define MAX_SIZE (65535 - sizeof(struct GNUNET_MessageHeader))
+/**
+ * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ */
+#define MAX_SIZE 65536
-// This is in linux/include/net/ipv6.h.
+#ifndef _LINUX_IN6_H
+/**
+ * This is in linux/include/net/ipv6.h, but not always exported...
+ */
struct in6_ifreq
{
struct in6_addr ifr6_addr;
uint32_t ifr6_prefixlen;
unsigned int ifr6_ifindex;
};
-
#endif
-static int running = 1;
-static void
-term (int sig)
+/**
+ * Creates a tun-interface called dev;
+ *
+ * @param dev is asumed to point to a char[IFNAMSIZ]
+ * if *dev == '\\0', uses the name supplied by the kernel;
+ * @return the fd to the tun or -1 on error
+ */
+static int
+init_tun (char *dev)
{
- fprintf (stderr, "Got SIGTERM...\n");
- if (sig == SIGTERM)
- running = 0;
+ struct ifreq ifr;
+ int fd;
+
+ if (NULL == dev)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
+ {
+ fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
+ strerror (errno));
+ return -1;
+ }
+
+ if (fd >= FD_SETSIZE)
+ {
+ fprintf (stderr, "File descriptor to large: %d", fd);
+ (void) close (fd);
+ return -1;
+ }
+
+ memset (&ifr, 0, sizeof (ifr));
+ ifr.ifr_flags = IFF_TUN;
+
+ if ('\0' != *dev)
+ strncpy (ifr.ifr_name, dev, IFNAMSIZ);
+
+ if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
+ {
+ fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
+ strerror (errno));
+ (void) close (fd);
+ return -1;
+ }
+ strcpy (dev, ifr.ifr_name);
+ return fd;
}
+
/**
* @brief Sets the IPv6-Address given in address on the interface dev
*
* @param prefix_len the length of the network-prefix
*/
static void
-set_address6 (char *dev, char *address, unsigned long prefix_len)
-{ /* {{{ */
- int fd = socket (AF_INET6, SOCK_DGRAM, 0);
-
- if (fd < 0)
- {
- fprintf (stderr, "error creating socket: %m\n");
- exit (1);
- }
-
+set_address6 (const char *dev, const char *address, unsigned long prefix_len)
+{
struct ifreq ifr;
struct in6_ifreq ifr6;
-
struct sockaddr_in6 sa6;
- memset (&sa6, 0, sizeof (struct sockaddr_in6));
+ int fd;
+ /*
+ * parse the new address
+ */
+ memset (&sa6, 0, sizeof (struct sockaddr_in6));
sa6.sin6_family = AF_INET6;
+ if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
+ {
+ fprintf (stderr, "Failed to parse address `%s': %s\n", address,
+ strerror (errno));
+ exit (1);
+ }
- int r = inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr);
- if (r < 0)
- {
- fprintf (stderr, "error at inet_pton: %m\n");
- exit (1);
- }
-
- memcpy ((char *) &ifr6.ifr6_addr, (char *) &sa6.sin6_addr,
- sizeof (struct in6_addr));
+ if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
+ {
+ fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
+ exit (1);
+ }
+ memset (&ifr, 0, sizeof (struct ifreq));
+ /*
+ * Get the index of the if
+ */
strncpy (ifr.ifr_name, dev, IFNAMSIZ);
+ if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
- if (ioctl (fd, SIOGIFINDEX, &ifr) < 0)
- {
- perror ("SIOGIFINDEX");
- }
-
+ memset (&ifr6, 0, sizeof (struct in6_ifreq));
+ ifr6.ifr6_addr = sa6.sin6_addr;
ifr6.ifr6_ifindex = ifr.ifr_ifindex;
ifr6.ifr6_prefixlen = prefix_len;
- if (ioctl (fd, SIOCSIFADDR, &ifr6) < 0)
- {
- perror ("SIOCSIFADDR");
- }
+ /*
+ * Set the address
+ */
+ if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
+ {
+ fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
- (void) ioctl (fd, SIOCGIFFLAGS, &ifr);
+ /*
+ * Get the flags
+ */
+ if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
+
+ /*
+ * Add the UP and RUNNING flags
+ */
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
- (void) ioctl (fd, SIOCSIFFLAGS, &ifr);
- close (fd);
-} /* }}} */
+ if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
+
+ if (0 != close (fd))
+ {
+ fprintf (stderr, "close failed: %s\n", strerror (errno));
+ exit (1);
+ }
+}
+
-static void
/**
* @brief Sets the IPv4-Address given in address on the interface dev
*
* @param address the IPv4-Address
* @param mask the netmask
*/
-set_address4 (char *dev, char *address, char *mask)
-{ /* {{{ */
- int fd = 0;
+static void
+set_address4 (const char *dev, const char *address, const char *mask)
+{
+ int fd;
struct sockaddr_in *addr;
struct ifreq ifr;
memset (&ifr, 0, sizeof (struct ifreq));
addr = (struct sockaddr_in *) &(ifr.ifr_addr);
- memset (addr, 0, sizeof (struct sockaddr_in));
addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = inet_addr (address);
- int r = inet_pton (AF_INET, address, &addr->sin_addr.s_addr);
- if (r < 0)
- {
- fprintf (stderr, "error at inet_pton: %m\n");
- exit (1);
- }
+ /*
+ * Parse the address
+ */
+ if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
+ {
+ fprintf (stderr, "Failed to parse address `%s': %s\n", address,
+ strerror (errno));
+ exit (1);
+ }
- fd = socket (PF_INET, SOCK_DGRAM, 0);
- if (fd < 0)
- {
- perror ("socket()");
- return;
- }
+ if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
+ {
+ fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
+ exit (1);
+ }
strncpy (ifr.ifr_name, dev, IFNAMSIZ);
- if (ioctl (fd, SIOCSIFADDR, &ifr) != 0)
- {
- perror ("SIOCSIFADDR");
- close (fd);
- return;
- }
+ /*
+ * Set the address
+ */
+ if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
+ /*
+ * Parse the netmask
+ */
addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
- r = inet_pton (AF_INET, mask, &addr->sin_addr.s_addr);
- if (r < 0)
- {
- fprintf (stderr, "error at inet_pton: %m\n");
- exit (1);
- }
+ if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
+ {
+ fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
- if (ioctl (fd, SIOCSIFNETMASK, &ifr) != 0)
- {
- perror ("SIOCSIFNETMASK");
- close (fd);
- return;
- }
+ /*
+ * Set the netmask
+ */
+ if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
+
+ /*
+ * Get the flags
+ */
+ if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
- (void) ioctl (fd, SIOCGIFFLAGS, &ifr);
+ /*
+ * Add the UP and RUNNING flags
+ */
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
- (void) ioctl (fd, SIOCSIFFLAGS, &ifr);
- close (fd);
-} /* }}} */
+ if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
+ {
+ fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+ strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
+
+ if (0 != close (fd))
+ {
+ fprintf (stderr, "close failed: %s\n", strerror (errno));
+ (void) close (fd);
+ exit (1);
+ }
+}
+
/**
- * @brief sets the socket to nonblocking
+ * Start forwarding to and from the tunnel.
*
- * @param fd the socket
+ * @param fd_tun tunnel FD
*/
static void
-setnonblocking (int fd)
-{ /*{{{ */
- int opts;
+run (int fd_tun)
+{
+ /*
+ * The buffer filled by reading from fd_tun
+ */
+ unsigned char buftun[MAX_SIZE];
+ ssize_t buftun_size = 0;
+ unsigned char *buftun_read = NULL;
+
+ /*
+ * The buffer filled by reading from stdin
+ */
+ unsigned char bufin[MAX_SIZE];
+ ssize_t bufin_size = 0;
+ size_t bufin_rpos = 0;
+ unsigned char *bufin_read = NULL;
+
+ fd_set fds_w;
+ fd_set fds_r;
+
+ /* read refers to reading from fd_tun, writing to stdout */
+ int read_open = 1;
+
+ /* write refers to reading from stdin, writing to fd_tun */
+ int write_open = 1;
- opts = fcntl (fd, F_GETFL);
- if (opts < 0)
+ while ((1 == read_open) || (1 == write_open))
+ {
+ FD_ZERO (&fds_w);
+ FD_ZERO (&fds_r);
+
+ /*
+ * We are supposed to read and the buffer is empty
+ * -> select on read from tun
+ */
+ if (read_open && (0 == buftun_size))
+ FD_SET (fd_tun, &fds_r);
+
+ /*
+ * We are supposed to read and the buffer is not empty
+ * -> select on write to stdout
+ */
+ if (read_open && (0 != buftun_size))
+ FD_SET (1, &fds_w);
+
+ /*
+ * We are supposed to write and the buffer is empty
+ * -> select on read from stdin
+ */
+ if (write_open && (NULL == bufin_read))
+ FD_SET (0, &fds_r);
+
+ /*
+ * We are supposed to write and the buffer is not empty
+ * -> select on write to tun
+ */
+ if (write_open && (NULL != bufin_read))
+ FD_SET (fd_tun, &fds_w);
+
+ int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
+
+ if (-1 == r)
{
- perror ("fcntl(F_GETFL)");
+ if (EINTR == errno)
+ continue;
+ fprintf (stderr, "select failed: %s\n", strerror (errno));
+ exit (1);
}
- opts = (opts | O_NONBLOCK);
- if (fcntl (fd, F_SETFL, opts) < 0)
+
+ if (r > 0)
{
- perror ("fcntl(F_SETFL)");
+ if (FD_ISSET (fd_tun, &fds_r))
+ {
+ buftun_size =
+ read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader),
+ MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
+ if (-1 == buftun_size)
+ {
+ fprintf (stderr, "read-error: %s\n", strerror (errno));
+ shutdown (fd_tun, SHUT_RD);
+ shutdown (1, SHUT_WR);
+ read_open = 0;
+ buftun_size = 0;
+ }
+ else if (0 == buftun_size)
+ {
+ fprintf (stderr, "EOF on tun\n");
+ shutdown (fd_tun, SHUT_RD);
+ shutdown (1, SHUT_WR);
+ read_open = 0;
+ buftun_size = 0;
+ }
+ else
+ {
+ buftun_read = buftun;
+ struct GNUNET_MessageHeader *hdr =
+ (struct GNUNET_MessageHeader *) buftun;
+ buftun_size += sizeof (struct GNUNET_MessageHeader);
+ hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
+ hdr->size = htons (buftun_size);
+ }
+ }
+ else if (FD_ISSET (1, &fds_w))
+ {
+ ssize_t written = write (1, buftun_read, buftun_size);
+
+ if (-1 == written)
+ {
+#if !DEBUG
+ if (errno != EPIPE)
+#endif
+ fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
+ shutdown (fd_tun, SHUT_RD);
+ shutdown (1, SHUT_WR);
+ read_open = 0;
+ buftun_size = 0;
+ }
+ else if (0 == written)
+ {
+ fprintf (stderr, "write returned 0!?\n");
+ exit (1);
+ }
+ else
+ {
+ buftun_size -= written;
+ buftun_read += written;
+ }
+ }
+
+ if (FD_ISSET (0, &fds_r))
+ {
+ bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
+ if (-1 == bufin_size)
+ {
+ fprintf (stderr, "read-error: %s\n", strerror (errno));
+ shutdown (0, SHUT_RD);
+ shutdown (fd_tun, SHUT_WR);
+ write_open = 0;
+ bufin_size = 0;
+ }
+ else if (0 == bufin_size)
+ {
+#if DEBUG
+ fprintf (stderr, "EOF on stdin\n");
+#endif
+ shutdown (0, SHUT_RD);
+ shutdown (fd_tun, SHUT_WR);
+ write_open = 0;
+ bufin_size = 0;
+ }
+ else
+ {
+ struct GNUNET_MessageHeader *hdr;
+
+PROCESS_BUFFER:
+ bufin_rpos += bufin_size;
+ if (bufin_rpos < sizeof (struct GNUNET_MessageHeader))
+ continue;
+ hdr = (struct GNUNET_MessageHeader *) bufin;
+ if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
+ {
+ fprintf (stderr, "protocol violation!\n");
+ exit (1);
+ }
+ if (ntohs (hdr->size) > bufin_rpos)
+ continue;
+ bufin_read = bufin + sizeof (struct GNUNET_MessageHeader);
+ bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader);
+ bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader);
+ }
+ }
+ else if (FD_ISSET (fd_tun, &fds_w))
+ {
+ ssize_t written = write (fd_tun, bufin_read, bufin_size);
+
+ if (-1 == written)
+ {
+ fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
+ shutdown (0, SHUT_RD);
+ shutdown (fd_tun, SHUT_WR);
+ write_open = 0;
+ bufin_size = 0;
+ }
+ else if (0 == written)
+ {
+ fprintf (stderr, "write returned 0!?\n");
+ exit (1);
+ }
+ else
+ {
+ bufin_size -= written;
+ bufin_read += written;
+ if (0 == bufin_size)
+ {
+ memmove (bufin, bufin_read, bufin_rpos);
+ bufin_read = NULL; /* start reading again */
+ bufin_size = 0;
+ goto PROCESS_BUFFER;
+ }
+ }
+ }
}
- return;
-} /*}}} */
+ }
+}
+
+/**
+ * Open VPN tunnel interface.
+ *
+ * @param argc must be 6
+ * @param argv 0: binary name (gnunet-helper-vpn)
+ * 1: tunnel interface name (gnunet-vpn)
+ * 2: IPv6 address (::1), "-" to disable
+ * 3: IPv6 netmask length in bits (64), ignored if #2 is "-"
+ * 4: IPv4 address (1.2.3.4), "-" to disable
+ * 5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
+ */
int
main (int argc, char **argv)
{
- unsigned char buf[MAX_SIZE];
-
char dev[IFNAMSIZ];
- memset (dev, 0, IFNAMSIZ);
+ int fd_tun;
+ int global_ret;
- signal (SIGTERM, &term);
+ if (6 != argc)
+ {
+ fprintf (stderr, "Fatal: must supply 5 arguments!\n");
+ return 1;
+ }
- int fd_tun = init_tun (dev);
+ strncpy (dev, argv[1], IFNAMSIZ);
+ dev[IFNAMSIZ - 1] = '\0';
- if (fd_tun < 0)
- {
- fprintf (stderr, "Could not initialize tun-interface: %s\n",
- strerror (errno));
- exit (1);
- }
+ if (-1 == (fd_tun = init_tun (dev)))
+ {
+ fprintf (stderr, "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
+ dev,
+ argv[2],
+ argv[3],
+ argv[4],
+ argv[5]);
+ return 1;
+ }
+ if (0 != strcmp (argv[2], "-"))
{
- // TODO: get this out of argv
- char address[] = "1234::1";
- unsigned long prefix_len = 16;
+ const char *address = argv[2];
+ long prefix_len = atol (argv[3]);
+
+ if ((prefix_len < 1) || (prefix_len > 127))
+ {
+ fprintf (stderr, "Fatal: prefix_len out of range\n");
+ return 1;
+ }
set_address6 (dev, address, prefix_len);
}
+ if (0 != strcmp (argv[4], "-"))
{
- char address[] = "10.10.10.1";
- char mask[] = "255.255.255.252";
+ const char *address = argv[4];
+ const char *mask = argv[5];
set_address4 (dev, address, mask);
}
uid_t uid = getuid ();
- if (setresuid (uid, uid, uid) != 0)
+#ifdef HAVE_SETRESUID
+ if (0 != setresuid (uid, uid, uid))
+ {
fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
+ global_ret = 2;
+ goto cleanup;
+ }
+#else
+ if (0 != (setuid (uid) | seteuid (uid)))
+ {
+ fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
+ global_ret = 2;
+ goto cleanup;
+ }
+#endif
- setnonblocking (0);
- setnonblocking (1);
- setnonblocking (fd_tun);
-
- fd_set fds_w;
- fd_set fds_r;
-
- int rea = 1;
- int wri = 1;
-
- int write_fd_possible = 0;
- int write_stdout_possible = 0;
-outer:
- while ((rea == 1 || wri == 1) && running == 1)
- {
- FD_ZERO (&fds_w);
- FD_ZERO (&fds_r);
-
- if (rea)
- {
- FD_SET (fd_tun, &fds_r);
- if (!write_stdout_possible)
- FD_SET (1, &fds_w);
- }
-
- if (wri)
- {
- FD_SET (0, &fds_r);
- if (!write_fd_possible)
- FD_SET (fd_tun, &fds_w);
- }
-
- int r = select (fd_tun + 1, &fds_r, &fds_w, (fd_set *) 0, 0);
-
- if (r > 0)
- {
- if (FD_ISSET (fd_tun, &fds_w))
- write_fd_possible = 1;
- if (FD_ISSET (1, &fds_w))
- write_stdout_possible = 1;
-
- if (FD_ISSET (0, &fds_r) && write_fd_possible)
- {
- write_fd_possible = 0;
- struct suid_packet *pkt = (struct suid_packet *) buf;
- r = read (0, buf, sizeof (struct GNUNET_MessageHeader));
- if (r <= 0)
- {
- fprintf (stderr, "read-error: %s\n", strerror (errno));
- shutdown (fd_tun, SHUT_WR);
- shutdown (0, SHUT_RD);
- wri = 0;
- goto outer;
- }
- if (pkt->hdr.type != ntohs (GNUNET_MESSAGE_TYPE_VPN_HELPER))
- abort ();
- while (r < ntohs (pkt->hdr.size))
- {
- int t = read (0, buf + r, ntohs (pkt->hdr.size) - r);
- if (r < 0)
- {
- fprintf (stderr, "read-error: %s\n", strerror (errno));
- shutdown (fd_tun, SHUT_WR);
- shutdown (0, SHUT_RD);
- wri = 0;
- goto outer;
- }
- r += t;
- }
- r = 0;
- while (r <
- ntohs (pkt->hdr.size) -
- sizeof (struct GNUNET_MessageHeader))
- {
- int t = write (fd_tun, pkt->data,
- ntohs (pkt->hdr.size) -
- sizeof (struct GNUNET_MessageHeader) - r);
- if (t < 0)
- {
- fprintf (stderr, "write-error 3: %s\n",
- strerror (errno));
- shutdown (fd_tun, SHUT_WR);
- shutdown (0, SHUT_RD);
- wri = 0;
- goto outer;
- }
- r += t;
- }
- }
- else if (write_stdout_possible && FD_ISSET (fd_tun, &fds_r))
- {
- write_stdout_possible = 0;
- r = read (fd_tun, buf, MAX_SIZE);
- if (r <= 0)
- {
- fprintf (stderr, "read-error: %s\n", strerror (errno));
- shutdown (fd_tun, SHUT_RD);
- shutdown (1, SHUT_WR);
- rea = 0;
- goto outer;
- }
- struct GNUNET_MessageHeader hdr = {.size =
- htons (r + sizeof (struct GNUNET_MessageHeader)),.type =
- htons (GNUNET_MESSAGE_TYPE_VPN_HELPER)
- };
- r = 0;
- while (r < sizeof (struct GNUNET_MessageHeader))
- {
- int t =
- write (1, &hdr, sizeof (struct GNUNET_MessageHeader) - r);
- if (t < 0)
- {
- fprintf (stderr, "write-error 2: %s\n",
- strerror (errno));
- shutdown (fd_tun, SHUT_RD);
- shutdown (1, SHUT_WR);
- rea = 0;
- goto outer;
- }
- r += t;
- }
- while (r < ntohs (hdr.size))
- {
- int t = write (1, buf, ntohs (hdr.size) - r);
- if (t < 0)
- {
- fprintf (stderr, "write-error 1: %s, written %d/%d\n",
- strerror (errno), r, ntohs (hdr.size));
- shutdown (fd_tun, SHUT_RD);
- shutdown (1, SHUT_WR);
- rea = 0;
- goto outer;
- }
- r += t;
- }
- }
- }
- }
-
+ if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
+ {
+ fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
+ strerror (errno));
+ /* no exit, we might as well die with SIGPIPE should it ever happen */
+ }
+ run (fd_tun);
+ global_ret = 0;
+ cleanup:
close (fd_tun);
-
- return 0;
+ return global_ret;
}