From e6adbd6658b15bb8dd9504fec86424382b443ab4 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 23 Dec 2007 00:07:39 +0100 Subject: [PATCH] Add network interface helper functions --- plugins/80203.c | 22 ++++++++++- plugins/80211.c | 22 ++++++++++- plugins/Makefile.am | 4 +- plugins/net.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/net.h | 23 +++++++++++ 5 files changed, 172 insertions(+), 6 deletions(-) create mode 100644 plugins/net.c create mode 100644 plugins/net.h diff --git a/plugins/80203.c b/plugins/80203.c index 7921d9c..6c9f950 100644 --- a/plugins/80203.c +++ b/plugins/80203.c @@ -28,21 +28,39 @@ #include #include +#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 = { diff --git a/plugins/80211.c b/plugins/80211.c index eea416b..74438db 100644 --- a/plugins/80211.c +++ b/plugins/80211.c @@ -28,20 +28,38 @@ #include #include +#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 = { diff --git a/plugins/Makefile.am b/plugins/Makefile.am index aa9ed77..2d7d5df 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -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 index 0000000..627b4c5 --- /dev/null +++ b/plugins/net.c @@ -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 +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 index 0000000..10f46ff --- /dev/null +++ b/plugins/net.h @@ -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); -- 2.7.4