From 6c6a470f8d4b2c5ec4725b13e7bfcf4ca0c04f41 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 14 Jan 2008 06:00:15 +0100 Subject: [PATCH] Add generic helpers for device start and shutdown --- src/connman.h | 2 ++ src/iface-inet.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/connman.h b/src/connman.h index 26931ff..8756034 100644 --- a/src/connman.h +++ b/src/connman.h @@ -57,6 +57,8 @@ void __connman_iface_list(DBusMessageIter *iter); int __connman_iface_create_identifier(struct connman_iface *iface); int __connman_iface_init_via_inet(struct connman_iface *iface); +int __connman_iface_up(struct connman_iface *iface); +int __connman_iface_down(struct connman_iface *iface); int __connman_iface_load(struct connman_iface *iface); int __connman_iface_store(struct connman_iface *iface); diff --git a/src/iface-inet.c b/src/iface-inet.c index 664d298..a09ba31 100644 --- a/src/iface-inet.c +++ b/src/iface-inet.c @@ -108,3 +108,87 @@ int __connman_iface_init_via_inet(struct connman_iface *iface) return 0; } + +int __connman_iface_up(struct connman_iface *iface) +{ + struct ifreq ifr; + int sk, err; + + DBG("iface %p", iface); + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return -errno; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = iface->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; +} + +int __connman_iface_down(struct connman_iface *iface) +{ + struct ifreq ifr; + int sk, err; + + DBG("iface %p", iface); + + sk = socket(PF_INET, SOCK_DGRAM, 0); + if (sk < 0) + return -errno; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = iface->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; +} -- 2.7.4