Export and use more generic INET helpers
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 21 May 2009 08:02:31 +0000 (01:02 -0700)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 21 May 2009 08:02:31 +0000 (01:02 -0700)
include/inet.h
plugins/Makefile.am
plugins/bluetooth.c
plugins/dhclient.c
plugins/ethernet.c
plugins/inet.c [deleted file]
plugins/inet.h [deleted file]
plugins/supplicant.c
plugins/udhcp.c
plugins/wifi.c
src/inet.c

index 05d1705..3aacbdc 100644 (file)
@@ -28,6 +28,12 @@ extern "C" {
 
 #include <connman/device.h>
 
+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
index 1530c0d..c9bb900 100644 (file)
@@ -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
index 009c6c7..8279f61 100644 (file)
 #define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
 #include <connman/device.h>
+#include <connman/inet.h>
 #include <connman/dbus.h>
 #include <connman/log.h>
 
-#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);
index 0bf9ad1..432878a 100644 (file)
 #define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
 #include <connman/driver.h>
+#include <connman/inet.h>
 #include <connman/dbus.h>
 #include <connman/log.h>
 
-#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) {
index e6d45ff..3013271 100644 (file)
 #define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
 #include <connman/device.h>
+#include <connman/inet.h>
 #include <connman/rtnl.h>
 #include <connman/log.h>
 
-#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 (file)
index b7d4a9f..0000000
+++ /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 <config.h>
-#endif
-
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <net/if.h>
-#include <net/ethernet.h>
-
-#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 (file)
index af5799b..0000000
+++ /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);
index 70af60a..192a016 100644 (file)
 #define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/device.h>
 #include <connman/option.h>
+#include <connman/inet.h>
 #include <connman/dbus.h>
 #include <connman/log.h>
 
-#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);
index 36ea23f..6ee0ac8 100644 (file)
 #define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/plugin.h>
 #include <connman/driver.h>
+#include <connman/inet.h>
 #include <connman/dbus.h>
 #include <connman/log.h>
 
-#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;
 
index 21b9208..08b3d48 100644 (file)
@@ -33,7 +33,6 @@
 #include <connman/device.h>
 #include <connman/log.h>
 
-#include "inet.h"
 #include "supplicant.h"
 
 #define CLEANUP_TIMEOUT   8    /* in seconds */
index e90656a..56ccc59 100644 (file)
 
 #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;