From c34723cfb980c6a8d957892cfd5466df44b50863 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 9 Nov 2015 17:10:29 +0100 Subject: Use a global ioctl socket Based-on-patch-by: Julian Kornberger --- src/fastd.c | 7 +++++++ src/fastd.h | 2 ++ src/iface.c | 38 ++++++-------------------------------- 3 files changed, 15 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/fastd.c b/src/fastd.c index 3d5b3d4..2d0b47a 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -173,6 +173,10 @@ static inline uint16_t get_bind_port(const fastd_bind_address_t *addr) { /** Initializes the configured sockets */ static void init_sockets(void) { + ctx.ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); + if (ctx.ioctl_sock < 0) + exit_errno("unable to create ioctl socket"); + ctx.socks = fastd_new_array(conf.n_bind_addrs, fastd_socket_t); size_t i; @@ -204,6 +208,9 @@ static void close_sockets(void) { fastd_socket_close(&ctx.socks[i]); free(ctx.socks); + + if (close(ctx.ioctl_sock)) + pr_error_errno("close"); } /** Calls the on-pre-up command */ diff --git a/src/fastd.h b/src/fastd.h index 1433509..7963505 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -325,6 +325,8 @@ struct fastd_context { int android_ctrl_sock_fd; /**< The unix domain socket for communicating with Android GUI */ #endif + int ioctl_sock; /**< The global ioctl socket */ + size_t n_socks; /**< The number of sockets in socks */ fastd_socket_t *socks; /**< Array of all sockets */ diff --git a/src/iface.c b/src/iface.c index 4010c16..74dda30 100644 --- a/src/iface.c +++ b/src/iface.c @@ -33,6 +33,7 @@ Management of the TUN/TAP interface */ +#include "fastd.h" #include "config.h" #include "peer.h" #include "poll.h" @@ -94,7 +95,6 @@ static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu); /** Opens the TUN/TAP device helper shared by Android and Linux targets */ static void open_iface_linux(fastd_iface_t *iface, const char *ifname, uint16_t mtu, const char *dev_name) { - int ctl_sock = -1; struct ifreq ifr = {}; iface->fd = FASTD_POLL_FD(POLL_TYPE_IFACE, open(dev_name, O_RDWR|O_NONBLOCK)); @@ -125,32 +125,20 @@ static void open_iface_linux(fastd_iface_t *iface, const char *ifname, uint16_t iface->name = fastd_strndup(ifr.ifr_name, IFNAMSIZ-1); - ctl_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (ctl_sock < 0) - exit_errno("socket"); - - if (ioctl(ctl_sock, SIOCGIFMTU, &ifr) < 0) + if (ioctl(ctx.ioctl_sock, SIOCGIFMTU, &ifr) < 0) exit_errno("SIOCGIFMTU ioctl failed"); if (ifr.ifr_mtu != mtu) { ifr.ifr_mtu = mtu; - if (ioctl(ctl_sock, SIOCSIFMTU, &ifr) < 0) { + if (ioctl(ctx.ioctl_sock, SIOCSIFMTU, &ifr) < 0) { pr_error_errno("unable to set TUN/TAP interface MTU: SIOCSIFMTU ioctl failed"); goto error; } } - if (close(ctl_sock)) - pr_error_errno("close"); - return; error: - if (ctl_sock >= 0) { - if (close(ctl_sock)) - pr_error_errno("close"); - } - close(iface->fd.fd); iface->fd.fd = -1; } @@ -290,12 +278,8 @@ static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu) { static void set_link0(fastd_iface_t *iface, bool set) { struct ifreq ifr = {}; - int ctl_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (ctl_sock < 0) - exit_errno("socket"); - strncpy(ifr.ifr_name, iface->name, IFNAMSIZ-1); - if (ioctl(ctl_sock, SIOCGIFFLAGS, &ifr) < 0) + if (ioctl(ctx.ioctl_sock, SIOCGIFFLAGS, &ifr) < 0) exit_errno("SIOCGIFFLAGS ioctl failed"); if (set) @@ -303,11 +287,8 @@ static void set_link0(fastd_iface_t *iface, bool set) { else ifr.ifr_flags &= ~IFF_LINK0; - if (ioctl(ctl_sock, SIOCSIFFLAGS, &ifr) < 0) + if (ioctl(ctx.ioctl_sock, SIOCSIFFLAGS, &ifr) < 0) exit_errno("SIOCSIFFLAGS ioctl failed"); - - if (close(ctl_sock)) - pr_error_errno("close"); } /** Sets up the TUN device */ @@ -386,18 +367,11 @@ static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu) { iface->name = fastd_strndup(ifname, IFNAMSIZ-1); - int ctl_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (ctl_sock < 0) - exit_errno("socket"); - struct ifreq ifr = {}; strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1); ifr.ifr_mtu = mtu; - if (ioctl(ctl_sock, SIOCSIFMTU, &ifr) < 0) + if (ioctl(ctx.ioctl_sock, SIOCSIFMTU, &ifr) < 0) exit_errno("SIOCSIFMTU ioctl failed"); - - if (close(ctl_sock)) - pr_error_errno("close"); } #else -- cgit v1.2.3