From 36b19e423a2d66c0c9d722d4ee2fc65c03163b43 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 21 May 2009 01:02:31 -0700 Subject: [PATCH] Export and use more generic INET helpers --- include/inet.h | 6 ++ plugins/Makefile.am | 22 +++--- plugins/bluetooth.c | 5 +- plugins/dhclient.c | 5 +- plugins/ethernet.c | 7 +- plugins/inet.c | 214 -------------------------------------------------- plugins/inet.h | 28 ------- plugins/supplicant.c | 6 +- plugins/udhcp.c | 6 +- plugins/wifi.c | 1 - src/inet.c | 111 +++++++++++++++++++++++++- 11 files changed, 139 insertions(+), 272 deletions(-) delete mode 100644 plugins/inet.c delete mode 100644 plugins/inet.h diff --git a/include/inet.h b/include/inet.h index 05d1705..3aacbdc 100644 --- a/include/inet.h +++ b/include/inet.h @@ -28,6 +28,12 @@ extern "C" { #include +extern int connman_inet_ifindex(const char *name); +extern char *connman_inet_ifname(int index); + +extern int connman_inet_ifup(int index); +extern int connman_inet_ifdown(int index); + extern struct connman_device *connman_inet_create_device(int index); #ifdef __cplusplus diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 1530c0d..c9bb900 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -19,20 +19,20 @@ endif if ETHERNET if ETHERNET_BUILTIN builtin_modules += ethernet -builtin_sources += ethernet.c inet.h inet.c +builtin_sources += ethernet.c else plugin_LTLIBRARIES += ethernet.la -ethernet_la_SOURCES = ethernet.c inet.h inet.c +ethernet_la_SOURCES = ethernet.c endif endif if WIFI if WIFI_BUILTIN builtin_modules += wifi -builtin_sources += wifi.c inet.h inet.c supplicant.h supplicant.c +builtin_sources += wifi.c supplicant.h supplicant.c else plugin_LTLIBRARIES += wifi.la -wifi_la_SOURCES = wifi.c inet.h inet.c supplicant.h supplicant.c +wifi_la_SOURCES = wifi.c supplicant.h supplicant.c wifi_la_LIBADD = @GDBUS_LIBS@ endif endif @@ -40,10 +40,10 @@ endif if BLUETOOTH if BLUETOOTH_BUILTIN builtin_modules += bluetooth -builtin_sources += bluetooth.c inet.h inet.c +builtin_sources += bluetooth.c else plugin_LTLIBRARIES += bluetooth.la -bluetooth_la_SOURCES = bluetooth.c inet.h inet.c +bluetooth_la_SOURCES = bluetooth.c bluetooth_la_LIBADD = @GDBUS_LIBS@ endif endif @@ -81,11 +81,11 @@ endif if UDHCP if UDHCP_BUILTIN builtin_modules += udhcp -builtin_sources += udhcp.c inet.h inet.c task.h task.c +builtin_sources += udhcp.c task.h task.c builtin_cflags += -DUDHCPC=\"@UDHCPC@\" else plugin_LTLIBRARIES += udhcp.la -udhcp_la_SOURCES = udhcp.c inet.h inet.c task.h task.c +udhcp_la_SOURCES = udhcp.c task.h task.c udhcp_la_CFLAGS = $(AM_CFLAGS) -DUDHCPC=\"@UDHCPC@\" \ -DSTATEDIR=\""$(statedir)"\" -DSCRIPTDIR=\""$(scriptdir)"\" endif @@ -94,11 +94,11 @@ endif if DHCLIENT if DHCLIENT_BUILTIN builtin_modules += dhclient -builtin_sources += dhclient.c inet.h inet.c +builtin_sources += dhclient.c builtin_cflags += -DDHCLIENT=\"@DHCLIENT@\" else plugin_LTLIBRARIES += dhclient.la -dhclient_la_SOURCES = dhclient.c inet.h inet.c +dhclient_la_SOURCES = dhclient.c dhclient_la_CFLAGS = $(AM_CFLAGS) -DDHCLIENT=\"@DHCLIENT@\" \ -DSTATEDIR=\""$(statedir)"\" -DSCRIPTDIR=\""$(scriptdir)"\" endif @@ -147,7 +147,7 @@ endif if IWMXSDK plugin_LTLIBRARIES += iwmxsdk.la -iwmxsdk_la_SOURCES = iwmxsdk.c inet.h inet.c +iwmxsdk_la_SOURCES = iwmxsdk.c iwmxsdk_la_LIBADD = @IWMXSDK_LIBS@ @GLIB_LIBS@ iwmxsdk_la_CFLAGS = $(AM_CFLAGS) @IWMXSDK_CFLAGS@ endif diff --git a/plugins/bluetooth.c b/plugins/bluetooth.c index 009c6c7..8279f61 100644 --- a/plugins/bluetooth.c +++ b/plugins/bluetooth.c @@ -31,11 +31,10 @@ #define CONNMAN_API_SUBJECT_TO_CHANGE #include #include +#include #include #include -#include "inet.h" - #define BLUEZ_SERVICE "org.bluez" #define BLUEZ_MANAGER_INTERFACE BLUEZ_SERVICE ".Manager" #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter" @@ -213,7 +212,7 @@ static void connect_reply(DBusPendingCall *call, void *user_data) data->interface = g_strdup(interface); - index = inet_name2index(interface); + index = connman_inet_ifindex(interface); connman_network_set_index(network, index); connman_network_set_connected(network, TRUE); diff --git a/plugins/dhclient.c b/plugins/dhclient.c index 0bf9ad1..432878a 100644 --- a/plugins/dhclient.c +++ b/plugins/dhclient.c @@ -30,11 +30,10 @@ #define CONNMAN_API_SUBJECT_TO_CHANGE #include #include +#include #include #include -#include "inet.h" - #define DHCLIENT_INTF "org.isc.dhclient" #define DHCLIENT_PATH "/org/isc/dhclient" @@ -145,7 +144,7 @@ static int dhclient_probe(struct connman_element *element) return -ENOMEM; task->ifindex = element->index; - task->ifname = inet_index2name(element->index); + task->ifname = connman_inet_ifname(element->index); task->element = element; if (task->ifname == NULL) { diff --git a/plugins/ethernet.c b/plugins/ethernet.c index e6d45ff..3013271 100644 --- a/plugins/ethernet.c +++ b/plugins/ethernet.c @@ -35,11 +35,10 @@ #define CONNMAN_API_SUBJECT_TO_CHANGE #include #include +#include #include #include -#include "inet.h" - struct ethernet_data { int index; unsigned flags; @@ -118,7 +117,7 @@ static int ethernet_enable(struct connman_device *device) DBG("device %p", device); - return inet_ifup(ethernet->index); + return connman_inet_ifup(ethernet->index); } static int ethernet_disable(struct connman_device *device) @@ -127,7 +126,7 @@ static int ethernet_disable(struct connman_device *device) DBG("device %p", device); - return inet_ifdown(ethernet->index); + return connman_inet_ifdown(ethernet->index); } static struct connman_device_driver ethernet_driver = { diff --git a/plugins/inet.c b/plugins/inet.c deleted file mode 100644 index b7d4a9f..0000000 --- a/plugins/inet.c +++ /dev/null @@ -1,214 +0,0 @@ -/* - * - * Connection Manager - * - * Copyright (C) 2007-2009 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 - -#include "inet.h" - -int inet_name2index(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 *inet_index2name(int index) -{ - struct ifreq ifr; - int sk, err; - - if (index < 0) - return NULL; - - sk = socket(PF_INET, SOCK_DGRAM, 0); - if (sk < 0) - return NULL; - - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_ifindex = index; - - err = ioctl(sk, SIOCGIFNAME, &ifr); - - close(sk); - - if (err < 0) - return NULL; - - return strdup(ifr.ifr_name); -} - -char *inet_index2ident(int index, const char *prefix) -{ - struct ifreq ifr; - struct ether_addr *eth; - char *str; - int sk, err, len; - - if (index < 0) - return NULL; - - sk = socket(PF_INET, SOCK_DGRAM, 0); - if (sk < 0) - return NULL; - - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_ifindex = index; - - err = ioctl(sk, SIOCGIFNAME, &ifr); - - if (err == 0) - err = ioctl(sk, SIOCGIFHWADDR, &ifr); - - close(sk); - - if (err < 0) - return NULL; - - len = prefix ? strlen(prefix) + 18 : 18; - - str = malloc(len); - if (!str) - return NULL; - - eth = (void *) &ifr.ifr_hwaddr.sa_data; - snprintf(str, len, "%s%02X_%02X_%02X_%02X_%02X_%02X", - prefix ? prefix : "", - eth->ether_addr_octet[0], - eth->ether_addr_octet[1], - eth->ether_addr_octet[2], - eth->ether_addr_octet[3], - eth->ether_addr_octet[4], - eth->ether_addr_octet[5]); - - return str; -} - -int 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 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; -} diff --git a/plugins/inet.h b/plugins/inet.h deleted file mode 100644 index af5799b..0000000 --- a/plugins/inet.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * Connection Manager - * - * Copyright (C) 2007-2009 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 - * - */ - -int inet_name2index(const char *name); - -char *inet_index2name(int index); -char *inet_index2ident(int index, const char *prefix); - -int inet_ifup(int index); -int inet_ifdown(int index); diff --git a/plugins/supplicant.c b/plugins/supplicant.c index 70af60a..192a016 100644 --- a/plugins/supplicant.c +++ b/plugins/supplicant.c @@ -34,10 +34,10 @@ #define CONNMAN_API_SUBJECT_TO_CHANGE #include #include +#include #include #include -#include "inet.h" #include "supplicant.h" #define TIMEOUT 5000 @@ -397,7 +397,7 @@ static void remove_interface_reply(DBusPendingCall *call, void *user_data) connman_device_unref(task->device); - inet_ifdown(task->ifindex); + connman_inet_ifdown(task->ifindex); free_task(task); @@ -1482,7 +1482,7 @@ int supplicant_start(struct connman_device *device) return -ENOMEM; task->ifindex = connman_device_get_index(device); - task->ifname = inet_index2name(task->ifindex); + task->ifname = connman_inet_ifname(task->ifindex); if (task->ifname == NULL) { g_free(task); diff --git a/plugins/udhcp.c b/plugins/udhcp.c index 36ea23f..6ee0ac8 100644 --- a/plugins/udhcp.c +++ b/plugins/udhcp.c @@ -29,10 +29,10 @@ #define CONNMAN_API_SUBJECT_TO_CHANGE #include #include +#include #include #include -#include "inet.h" #include "task.h" #define UDHCPC_INTF "net.busybox.udhcpc" @@ -49,7 +49,7 @@ static int udhcp_probe(struct connman_element *element) if (access(UDHCPC, X_OK) < 0) return -errno; - ifname = inet_index2name(element->index); + ifname = connman_inet_ifname(element->index); if (ifname == NULL) return -ENOMEM; @@ -118,7 +118,7 @@ static void udhcp_bound(DBusMessage *msg, gboolean renew) DBG("%s ==> address %s gateway %s", interface, address, gateway); - index = inet_name2index(interface); + index = connman_inet_ifindex(interface); if (index < 0) return; diff --git a/plugins/wifi.c b/plugins/wifi.c index 21b9208..08b3d48 100644 --- a/plugins/wifi.c +++ b/plugins/wifi.c @@ -33,7 +33,6 @@ #include #include -#include "inet.h" #include "supplicant.h" #define CLEANUP_TIMEOUT 8 /* in seconds */ diff --git a/src/inet.c b/src/inet.c index e90656a..56ccc59 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; -- 1.7.9.5