summaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2009-05-21 01:02:31 -0700
committerMarcel Holtmann <marcel@holtmann.org>2009-05-21 01:02:31 -0700
commit36b19e423a2d66c0c9d722d4ee2fc65c03163b43 (patch)
treea8e75b8c9b59b56397591f6cebde5d456d8fa30a /src/inet.c
parent91a77ed7265af5b9bf10784292945abb8db940ce (diff)
downloadconnman-36b19e423a2d66c0c9d722d4ee2fc65c03163b43.tar.gz
connman-36b19e423a2d66c0c9d722d4ee2fc65c03163b43.tar.bz2
connman-36b19e423a2d66c0c9d722d4ee2fc65c03163b43.zip
Export and use more generic INET helpers
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c111
1 files changed, 109 insertions, 2 deletions
diff --git a/src/inet.c b/src/inet.c
index e90656a6..56ccc59b 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -37,7 +37,32 @@
#include "connman.h"
-static char *index2name(int index)
+int connman_inet_ifindex(const char *name)
+{
+ struct ifreq ifr;
+ int sk, err;
+
+ if (name == NULL)
+ return -1;
+
+ sk = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sk < 0)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+
+ err = ioctl(sk, SIOCGIFINDEX, &ifr);
+
+ close(sk);
+
+ if (err < 0)
+ return -1;
+
+ return ifr.ifr_ifindex;
+}
+
+char *connman_inet_ifname(int index)
{
struct ifreq ifr;
int sk, err;
@@ -62,6 +87,88 @@ static char *index2name(int index)
return strdup(ifr.ifr_name);
}
+int connman_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 connman_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;
+}
+
static unsigned short index2type(int index)
{
struct ifreq ifr;
@@ -190,7 +297,7 @@ struct connman_device *connman_inet_create_device(int index)
if (index < 0)
return NULL;
- devname = index2name(index);
+ devname = connman_inet_ifname(index);
if (devname == NULL)
return NULL;