device: Move device creation func into device.c
authorJukka Rissanen <jukka.rissanen@linux.intel.com>
Mon, 12 Nov 2012 12:07:19 +0000 (14:07 +0200)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Fri, 23 Nov 2012 10:58:50 +0000 (12:58 +0200)
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
include/inet.h
plugins/iwmxsdk.c
src/detect.c
src/device.c
src/inet.c

index 470059b106c425e38a360305843ff75de05c4eb8..d3746551a749d5879b91d5e421c0ff02ebc07b32 100644 (file)
@@ -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;
index 6bdcc91dd3a43aa65ed592a6ae9b6c9a78aa8b0b..8f7a35cb0b00e888103400fb302c1c4b1171b440 100644 (file)
@@ -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);
index 06de4c3d979312e6aea06ce9e1454abd5817f6ad..4aaf9f37d6d56faee6eac4b4f218a9791e5c5df5 100644 (file)
@@ -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);
index 94a113e518473f4535f57b1d40c09ffd1d6abe4c..44d17fd3961b955e8ff9ad866e6be5aea614fe70 100644 (file)
@@ -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;
 
index fc374f508311c7b35a111fb6693b77fb0b5200bf..5325ed52c0037b1d4afbea0272aa9d6b31f36024 100644 (file)
 
 #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;
index 7a0bc7eb8ca82742bba68507943a5d72bd4b3cac..23b02a24947b9e74645069245a7aa0357871d9f5 100644 (file)
@@ -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;