[PATCH v5 03/42] device: Move device creation func into device.c

Jukka Rissanen jukka.rissanen at linux.intel.com
Mon Nov 12 04:07:19 PST 2012


The device creation function should be part of device.c and
not inet.c. After this change the inet.c can be used by
separate vpn daemon.
---
 include/device.h  |   1 +
 include/inet.h    |   1 -
 plugins/iwmxsdk.c |   4 +-
 src/detect.c      |   2 +-
 src/device.c      | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/inet.c        | 173 ----------------------------------------------------
 6 files changed, 182 insertions(+), 177 deletions(-)

diff --git a/include/device.h b/include/device.h
index 470059b..d374655 100644
--- a/include/device.h
+++ b/include/device.h
@@ -113,6 +113,7 @@ int connman_device_set_regdom(struct connman_device *device,
 						const char *alpha2);
 void connman_device_regdom_notify(struct connman_device *device,
 					int result, const char *alpha2);
+struct connman_device *connman_device_create_from_index(int index);
 
 struct connman_device_driver {
 	const char *name;
diff --git a/include/inet.h b/include/inet.h
index 6bdcc91..8f7a35c 100644
--- a/include/inet.h
+++ b/include/inet.h
@@ -41,7 +41,6 @@ short int connman_inet_ifflags(int index);
 int connman_inet_ifup(int index);
 int connman_inet_ifdown(int index);
 
-struct connman_device *connman_inet_create_device(int index);
 connman_bool_t connman_inet_is_cfg80211(int index);
 
 int connman_inet_set_address(int index, struct connman_ipaddress *ipaddress);
diff --git a/plugins/iwmxsdk.c b/plugins/iwmxsdk.c
index 06de4c3..4aaf9f3 100644
--- a/plugins/iwmxsdk.c
+++ b/plugins/iwmxsdk.c
@@ -672,7 +672,7 @@ error_scan:
  * Called through
  *
  * iwmx_sdk_dev_add
- *   connman_inet_create_device
+ *   connman_device_create_from_index
  *      connman_register
  *         iwmx_cm_probe()
  */
@@ -856,7 +856,7 @@ static void iwmx_sdk_dev_add(unsigned idx, unsigned api_idx, const char *name)
 		goto error_noifname;
 	}
 
-	wmxsdk->dev = connman_inet_create_device(ifindex);
+	wmxsdk->dev = connman_device_create_from_index(ifindex);
 	if (wmxsdk->dev == NULL) {
 		connman_error("wmxsdk: %s: failed to create connman_device\n",
 			      name);
diff --git a/src/detect.c b/src/detect.c
index 94a113e..44d17fd 100644
--- a/src/detect.c
+++ b/src/detect.c
@@ -71,7 +71,7 @@ static void detect_newlink(unsigned short type, int index,
 	if (device != NULL)
 		return;
 
-	device = connman_inet_create_device(index);
+	device = connman_device_create_from_index(index);
 	if (device == NULL)
 		return;
 
diff --git a/src/device.c b/src/device.c
index fc374f5..5325ed5 100644
--- a/src/device.c
+++ b/src/device.c
@@ -25,6 +25,11 @@
 
 #include <errno.h>
 #include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <net/ethernet.h>
+#include <net/if.h>
 
 #include "connman.h"
 
@@ -1132,6 +1137,179 @@ int __connman_device_request_hidden_scan(struct connman_device *device,
 					identity, passphrase, user_data);
 }
 
+static char *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 | SOCK_CLOEXEC, 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;
+
+	memcpy(&eth, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
+	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;
+}
+
+static char *index2addr(int index)
+{
+	struct ifreq ifr;
+	struct ether_addr eth;
+	char *str;
+	int sk, err;
+
+	if (index < 0)
+		return NULL;
+
+	sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 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;
+
+	str = malloc(18);
+	if (!str)
+		return NULL;
+
+	memcpy(&eth, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
+	snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
+						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;
+}
+
+struct connman_device *connman_device_create_from_index(int index)
+{
+	enum connman_device_type type;
+	struct connman_device *device;
+	char *devname, *ident = NULL;
+	char *addr = NULL, *name = NULL;
+
+	if (index < 0)
+		return NULL;
+
+	devname = connman_inet_ifname(index);
+	if (devname == NULL)
+		return NULL;
+
+	if (__connman_device_isfiltered(devname) == TRUE) {
+		connman_info("Ignoring interface %s (filtered)", devname);
+		g_free(devname);
+		return NULL;
+	}
+
+	type = __connman_rtnl_get_device_type(index);
+
+	switch (type) {
+	case CONNMAN_DEVICE_TYPE_UNKNOWN:
+		connman_info("Ignoring interface %s (type unknown)", devname);
+		g_free(devname);
+		return NULL;
+	case CONNMAN_DEVICE_TYPE_ETHERNET:
+	case CONNMAN_DEVICE_TYPE_GADGET:
+	case CONNMAN_DEVICE_TYPE_WIFI:
+	case CONNMAN_DEVICE_TYPE_WIMAX:
+		name = index2ident(index, "");
+		addr = index2addr(index);
+		break;
+	case CONNMAN_DEVICE_TYPE_BLUETOOTH:
+	case CONNMAN_DEVICE_TYPE_CELLULAR:
+	case CONNMAN_DEVICE_TYPE_GPS:
+	case CONNMAN_DEVICE_TYPE_VENDOR:
+		name = strdup(devname);
+		break;
+	}
+
+	device = connman_device_create(name, type);
+	if (device == NULL)
+		goto done;
+
+	switch (type) {
+	case CONNMAN_DEVICE_TYPE_UNKNOWN:
+	case CONNMAN_DEVICE_TYPE_VENDOR:
+	case CONNMAN_DEVICE_TYPE_GPS:
+		break;
+	case CONNMAN_DEVICE_TYPE_ETHERNET:
+	case CONNMAN_DEVICE_TYPE_GADGET:
+		ident = index2ident(index, NULL);
+		break;
+	case CONNMAN_DEVICE_TYPE_WIFI:
+	case CONNMAN_DEVICE_TYPE_WIMAX:
+		ident = index2ident(index, NULL);
+		break;
+	case CONNMAN_DEVICE_TYPE_BLUETOOTH:
+		break;
+	case CONNMAN_DEVICE_TYPE_CELLULAR:
+		ident = index2ident(index, NULL);
+		break;
+	}
+
+	connman_device_set_index(device, index);
+	connman_device_set_interface(device, devname);
+
+	if (ident != NULL) {
+		connman_device_set_ident(device, ident);
+		g_free(ident);
+	}
+
+	connman_device_set_string(device, "Address", addr);
+
+done:
+	g_free(devname);
+	g_free(name);
+	g_free(addr);
+
+	return device;
+}
+
 connman_bool_t __connman_device_isfiltered(const char *devname)
 {
 	char **pattern;
diff --git a/src/inet.c b/src/inet.c
index be69aca..ac45eb4 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -357,95 +357,6 @@ done:
 	return err;
 }
 
-static char *index2addr(int index)
-{
-	struct ifreq ifr;
-	struct ether_addr eth;
-	char *str;
-	int sk, err;
-
-	if (index < 0)
-		return NULL;
-
-	sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 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;
-
-	str = malloc(18);
-	if (!str)
-		return NULL;
-
-	memcpy(&eth, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
-	snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
-						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;
-}
-
-static char *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 | SOCK_CLOEXEC, 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;
-
-	memcpy(&eth, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
-	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;
-}
-
 connman_bool_t connman_inet_is_cfg80211(int index)
 {
 	connman_bool_t result = FALSE;
@@ -476,90 +387,6 @@ done:
 	return result;
 }
 
-struct connman_device *connman_inet_create_device(int index)
-{
-	enum connman_device_type type;
-	struct connman_device *device;
-	char *devname, *ident = NULL;
-	char *addr = NULL, *name = NULL;
-
-	if (index < 0)
-		return NULL;
-
-	devname = connman_inet_ifname(index);
-	if (devname == NULL)
-		return NULL;
-
-	if (__connman_device_isfiltered(devname) == TRUE) {
-		connman_info("Ignoring interface %s (filtered)", devname);
-		free(devname);
-		return NULL;
-	}
-
-	type = __connman_rtnl_get_device_type(index);
-
-	switch (type) {
-	case CONNMAN_DEVICE_TYPE_UNKNOWN:
-		connman_info("Ignoring interface %s (type unknown)", devname);
-		free(devname);
-		return NULL;
-	case CONNMAN_DEVICE_TYPE_ETHERNET:
-	case CONNMAN_DEVICE_TYPE_GADGET:
-	case CONNMAN_DEVICE_TYPE_WIFI:
-	case CONNMAN_DEVICE_TYPE_WIMAX:
-		name = index2ident(index, "");
-		addr = index2addr(index);
-		break;
-	case CONNMAN_DEVICE_TYPE_BLUETOOTH:
-	case CONNMAN_DEVICE_TYPE_CELLULAR:
-	case CONNMAN_DEVICE_TYPE_GPS:
-	case CONNMAN_DEVICE_TYPE_VENDOR:
-		name = strdup(devname);
-		break;
-	}
-
-	device = connman_device_create(name, type);
-	if (device == NULL)
-		goto done;
-
-	switch (type) {
-	case CONNMAN_DEVICE_TYPE_UNKNOWN:
-	case CONNMAN_DEVICE_TYPE_VENDOR:
-	case CONNMAN_DEVICE_TYPE_GPS:
-		break;
-	case CONNMAN_DEVICE_TYPE_ETHERNET:
-	case CONNMAN_DEVICE_TYPE_GADGET:
-		ident = index2ident(index, NULL);
-		break;
-	case CONNMAN_DEVICE_TYPE_WIFI:
-	case CONNMAN_DEVICE_TYPE_WIMAX:
-		ident = index2ident(index, NULL);
-		break;
-	case CONNMAN_DEVICE_TYPE_BLUETOOTH:
-		break;
-	case CONNMAN_DEVICE_TYPE_CELLULAR:
-		ident = index2ident(index, NULL);
-		break;
-	}
-
-	connman_device_set_index(device, index);
-	connman_device_set_interface(device, devname);
-
-	if (ident != NULL) {
-		connman_device_set_ident(device, ident);
-		free(ident);
-	}
-
-	connman_device_set_string(device, "Address", addr);
-
-done:
-	free(devname);
-	free(name);
-	free(addr);
-
-	return device;
-}
-
 struct in6_ifreq {
 	struct in6_addr ifr6_addr;
 	__u32 ifr6_prefixlen;
-- 
1.7.11.4




More information about the connman mailing list