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) return NULL; if (__connman_device_isfiltered(devname)) { 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: 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 = g_strdup(devname); break; } device = connman_device_create(name, type); if (!device) 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: 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) { 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; }
static void cleanup_devices(void) { /* * Check what interfaces are currently up and if connman is * suppose to handle the interface, then cleanup the mess * related to that interface. There might be weird routes etc * that are related to that interface and that might confuse * connmand. So in this case we just turn the interface down * so that kernel removes routes/addresses automatically and * then proceed the startup. * * Note that this cleanup must be done before rtnl/detect code * has activated interface watches. */ char **interfaces; int i; interfaces = __connman_inet_get_running_interfaces(); if (!interfaces) return; for (i = 0; interfaces[i]; i++) { bool filtered; int index; struct sockaddr_in sin_addr, sin_mask; filtered = __connman_device_isfiltered(interfaces[i]); if (filtered) continue; if (!can_bring_down(interfaces[i])) continue; index = connman_inet_ifindex(interfaces[i]); if (index < 0) continue; if (!__connman_inet_get_address_netmask(index, &sin_addr, &sin_mask)) { char *address = g_strdup(inet_ntoa(sin_addr.sin_addr)); char *netmask = g_strdup(inet_ntoa(sin_mask.sin_addr)); if (__connman_config_address_provisioned(address, netmask)) { DBG("Skip %s which is already provisioned " "with %s/%s", interfaces[i], address, netmask); g_free(address); g_free(netmask); continue; } g_free(address); g_free(netmask); } DBG("cleaning up %s index %d", interfaces[i], index); connman_inet_ifdown(index); /* * ConnMan will turn the interface UP automatically so * no need to do it here. */ } g_strfreev(interfaces); }