Add network interface helper functions
authorMarcel Holtmann <marcel@holtmann.org>
Sat, 22 Dec 2007 23:07:39 +0000 (00:07 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Sat, 22 Dec 2007 23:07:39 +0000 (00:07 +0100)
plugins/80203.c
plugins/80211.c
plugins/Makefile.am
plugins/net.c [new file with mode: 0644]
plugins/net.h [new file with mode: 0644]

index 7921d9c..6c9f950 100644 (file)
 #include <connman/plugin.h>
 #include <connman/iface.h>
 
+#include "net.h"
+
 static int iface_probe(struct connman_iface *iface)
 {
-       printf("[802.03] probe interface %s\n", iface->udi);
+       char *ifname;
+
+       ifname = __net_ifname(iface->sysfs);
+       if (ifname == NULL)
+               return -1;
+
+       printf("[802.03] probe interface %s\n", ifname);
 
        iface->type = CONNMAN_IFACE_TYPE_80203;
 
        iface->flags = CONNMAN_IFACE_FLAGS_CARRIER_DETECT |
                                                CONNMAN_IFACE_FLAGS_IPV4;
 
+       __net_free(ifname);
+
        return 0;
 }
 
 static void iface_remove(struct connman_iface *iface)
 {
-       printf("[802.03] remove interface %s\n", iface->udi);
+       char *ifname;
+
+       ifname = __net_ifname(iface->sysfs);
+       if (ifname == NULL)
+               return;
+
+       printf("[802.03] remove interface %s\n", ifname);
+
+       __net_free(ifname);
 }
 
 static struct connman_iface_driver iface_driver = {
index eea416b..74438db 100644 (file)
 #include <connman/plugin.h>
 #include <connman/iface.h>
 
+#include "net.h"
+
 static int iface_probe(struct connman_iface *iface)
 {
-       printf("[802.11] probe interface %s\n", iface->udi);
+       char *ifname;
+
+       ifname = __net_ifname(iface->sysfs);
+       if (ifname == NULL)
+               return -1;
+
+       printf("[802.11] probe interface %s\n", ifname);
 
        iface->type = CONNMAN_IFACE_TYPE_80211;
 
        iface->flags = CONNMAN_IFACE_FLAGS_IPV4;
 
+       __net_free(ifname);
+
        return 0;
 }
 
 static void iface_remove(struct connman_iface *iface)
 {
-       printf("[802.11] remove interface %s\n", iface->udi);
+       char *ifname;
+
+       ifname = __net_ifname(iface->sysfs);
+       if (ifname == NULL)
+               return;
+
+       printf("[802.11] remove interface %s\n", ifname);
+
+       __net_free(ifname);
 }
 
 static struct connman_iface_driver iface_driver = {
index aa9ed77..2d7d5df 100644 (file)
@@ -3,9 +3,9 @@ plugindir = $(libdir)/connman/plugins
 
 plugin_LTLIBRARIES = libconnman-80203.la libconnman-80211.la
 
-libconnman_80203_la_SOURCES = 80203.c
+libconnman_80203_la_SOURCES = 80203.c net.h net.c
 
-libconnman_80211_la_SOURCES = 80211.c
+libconnman_80211_la_SOURCES = 80211.c net.h net.c
 
 AM_LDFLAGS = -module -avoid-version -export-symbols-regex connman_plugin_desc
 
diff --git a/plugins/net.c b/plugins/net.c
new file mode 100644 (file)
index 0000000..627b4c5
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+
+#include "net.h"
+
+static int __net_ifindex(const char *sysfs)
+{
+       char *pathname;
+       char buf[8];
+       size_t size;
+       ssize_t len;
+       int fd, val = -EIO;
+
+       if (sysfs == NULL)
+               return -1;
+
+       size = strlen(sysfs) + 9;
+
+       pathname = malloc(size);
+
+       sprintf(pathname, "%s/ifindex", sysfs);
+
+       fd = open(pathname, O_RDONLY);
+
+       free(pathname);
+
+       if (fd < 0)
+               return -errno;
+
+       memset(buf, 0, sizeof(buf));
+
+       len = read(fd, buf, sizeof(buf) - 1);
+       if (len < 0) {
+               val = -errno;
+               goto done;
+       }
+
+       val = atoi(buf);
+
+done:
+       close(fd);
+
+       return val;
+}
+
+char *__net_ifname(const char *sysfs)
+{
+       struct ifreq ifr;
+       int sk, err, ifindex;
+
+       ifindex = __net_ifindex(sysfs);
+       if (ifindex < 0)
+               return NULL;
+
+       sk = socket (PF_INET, SOCK_DGRAM, 0);
+       if (sk < 0)
+               return NULL;
+
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_ifindex = ifindex;
+
+       err = ioctl(sk, SIOCGIFNAME, &ifr);
+
+       close(sk);
+
+       if (err < 0)
+               return NULL;
+
+       return strdup(ifr.ifr_name);
+}
+
+void __net_free(void *ptr)
+{
+       if (ptr)
+               free(ptr);
+}
diff --git a/plugins/net.h b/plugins/net.h
new file mode 100644 (file)
index 0000000..10f46ff
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+char *__net_ifname(const char *sysfs);
+void __net_free(void *ptr);