summaryrefslogtreecommitdiff
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
parent91a77ed7265af5b9bf10784292945abb8db940ce (diff)
downloadconnman-36b19e423a2d66c0c9d722d4ee2fc65c03163b43.tar.gz
connman-36b19e423a2d66c0c9d722d4ee2fc65c03163b43.tar.bz2
connman-36b19e423a2d66c0c9d722d4ee2fc65c03163b43.zip
Export and use more generic INET helpers
-rw-r--r--include/inet.h6
-rw-r--r--plugins/Makefile.am22
-rw-r--r--plugins/bluetooth.c5
-rw-r--r--plugins/dhclient.c5
-rw-r--r--plugins/ethernet.c7
-rw-r--r--plugins/inet.c214
-rw-r--r--plugins/inet.h28
-rw-r--r--plugins/supplicant.c6
-rw-r--r--plugins/udhcp.c6
-rw-r--r--plugins/wifi.c1
-rw-r--r--src/inet.c111
11 files changed, 139 insertions, 272 deletions
diff --git a/include/inet.h b/include/inet.h
index 05d17059..3aacbdc2 100644
--- a/include/inet.h
+++ b/include/inet.h
@@ -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
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 1530c0dd..c9bb9001 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 009c6c7d..8279f619 100644
--- a/plugins/bluetooth.c
+++ b/plugins/bluetooth.c
@@ -31,11 +31,10 @@
#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);
diff --git a/plugins/dhclient.c b/plugins/dhclient.c
index 0bf9ad11..432878ad 100644
--- a/plugins/dhclient.c
+++ b/plugins/dhclient.c
@@ -30,11 +30,10 @@
#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) {
diff --git a/plugins/ethernet.c b/plugins/ethernet.c
index e6d45ff4..30132713 100644
--- a/plugins/ethernet.c
+++ b/plugins/ethernet.c
@@ -35,11 +35,10 @@
#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
index b7d4a9f0..00000000
--- 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 <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
index af5799be..00000000
--- 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 70af60a5..192a016b 100644
--- a/plugins/supplicant.c
+++ b/plugins/supplicant.c
@@ -34,10 +34,10 @@
#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);
diff --git a/plugins/udhcp.c b/plugins/udhcp.c
index 36ea23f0..6ee0ac8c 100644
--- a/plugins/udhcp.c
+++ b/plugins/udhcp.c
@@ -29,10 +29,10 @@
#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;
diff --git a/plugins/wifi.c b/plugins/wifi.c
index 21b92082..08b3d488 100644
--- a/plugins/wifi.c
+++ b/plugins/wifi.c
@@ -33,7 +33,6 @@
#include <connman/device.h>
#include <connman/log.h>
-#include "inet.h"
#include "supplicant.h"
#define CLEANUP_TIMEOUT 8 /* in seconds */
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;