From: Marcel Holtmann Date: Sun, 28 Dec 2008 02:29:31 +0000 (+0100) Subject: Add helpers for ifup and ifdown X-Git-Tag: 0.6~107 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1c74d1d740d16d2ede335693052b52342717b5d8;p=platform%2Fupstream%2Fconnman.git Add helpers for ifup and ifdown --- 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);