From 1c74d1d740d16d2ede335693052b52342717b5d8 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 28 Dec 2008 03:29:31 +0100 Subject: [PATCH] Add helpers for ifup and ifdown --- plugins/inet.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/inet.h | 3 +++ 2 files changed, 85 insertions(+) diff --git a/plugins/inet.c b/plugins/inet.c index ab0fcdc..e3fedfb 100644 --- a/plugins/inet.c +++ b/plugins/inet.c @@ -105,3 +105,85 @@ char *inet_index2ident(int index, const char *prefix) return str; } + +int inet_ifup(int index) +{ + struct ifreq ifr; + int sk, err; + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return -errno; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = index; + + if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) { + err = -errno; + goto done; + } + + if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) { + err = -errno; + goto done; + } + + if (ifr.ifr_flags & IFF_UP) { + err = -EALREADY; + goto done; + } + + ifr.ifr_flags |= IFF_UP; + + if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) { + err = -errno; + goto done; + } + + err = 0; + +done: + close(sk); + + return err; +} + +int inet_ifdown(int index) +{ + struct ifreq ifr; + int sk, err; + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return -errno; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = index; + + if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) { + err = -errno; + goto done; + } + + if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) { + err = -errno; + goto done; + } + + if (!(ifr.ifr_flags & IFF_UP)) { + err = -EALREADY; + goto done; + } + + ifr.ifr_flags &= ~IFF_UP; + + if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) + err = -errno; + else + err = 0; + +done: + close(sk); + + return err; +} diff --git a/plugins/inet.h b/plugins/inet.h index afa8025..3246cc1 100644 --- a/plugins/inet.h +++ b/plugins/inet.h @@ -21,3 +21,6 @@ char *inet_index2name(int index); char *inet_index2ident(int index, const char *prefix); + +int inet_ifup(int index); +int inet_ifdown(int index); -- 2.7.4