From 6c6a470f8d4b2c5ec4725b13e7bfcf4ca0c04f41 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 14 Jan 2008 06:00:15 +0100 Subject: Add generic helpers for device start and shutdown --- src/connman.h | 2 ++ src/iface-inet.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) (limited to 'src') diff --git a/src/connman.h b/src/connman.h index 26931ff5..8756034c 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 664d2980..a09ba312 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; +} -- cgit v1.2.3