X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Fip_adapter%2Ftizen%2Fcaipnwmonitor.c;h=079ecd53daf165ed2ef306e6adad95b09b14caa8;hb=d2d11fd812a38648d4c797eb7e5c872ba5961d41;hp=fa669497a4ac6ed20e5f9e09ceb5b6876ee9efbe;hpb=0a9584f9157485845c2b59b567e77b97e59f6c26;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c index fa66949..079ecd5 100644 --- a/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c @@ -18,8 +18,6 @@ * ******************************************************************/ -#include "caipinterface.h" - #include #include #include @@ -29,50 +27,129 @@ #include #include #include -#include #include #include #include #include +#include +#include "caipinterface.h" +#include "caipnwmonitor.h" #include "caadapterutils.h" #include "logger.h" #include "oic_malloc.h" #include "oic_string.h" +#include -#define TAG "IP_MONITOR" - +#define TAG "OIC_CA_IP_MONITOR" #define MAX_INTERFACE_INFO_LENGTH (1024) - #define NETLINK_MESSAGE_LENGTH (4096) - -static CAIPConnectionStateChangeCallback g_networkChangeCallback; - +#define IFC_LABEL_LOOP "lo" +#define IFC_ADDR_LOOP_IPV4 "127.0.0.1" +#define IFC_ADDR_LOOP_IPV6 "::1" + +/** + * Used to storing a connection handle for managing data connections. + */ +static connection_h connection = NULL; + +/** + * Used to storing adapter changes callback interface. + */ +static struct CAIPCBData_t *g_adapterCallbackList = NULL; + +/** + * Create new interface item. + */ static CAInterface_t *CANewInterfaceItem(int index, char *name, int family, const char *addr, int flags); +/** + * Add new network interface in list. + */ static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index, char *name, int family, const char *addr, int flags); -static void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap, - void *userData); - -static void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData); +/** + * Pass the changed network status through the stored callback. + */ +static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status); +/** + * Callback function to received connection state changes. + */ +static void CAIPConnectionStateChangedCb(connection_type_e type, void* userData); int CAGetPollingInterval(int interval) { return interval; } -void CAIPSetNetworkMonitorCallback(CAIPConnectionStateChangeCallback callback) +static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status) { - g_networkChangeCallback = callback; + CAIPCBData_t *cbitem = NULL; + LL_FOREACH(g_adapterCallbackList, cbitem) + { + if (cbitem && cbitem->adapter) + { + cbitem->callback(cbitem->adapter, status); + } + } +} + +CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback, + CATransportAdapter_t adapter) +{ + if (!callback) + { + OIC_LOG(ERROR, TAG, "callback is null"); + return CA_STATUS_INVALID_PARAM; + } + + CAIPCBData_t *cbitem = NULL; + LL_FOREACH(g_adapterCallbackList, cbitem) + { + if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback) + { + OIC_LOG(DEBUG, TAG, "this callback is already added"); + return CA_STATUS_OK; + } + } + + cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem)); + if (!cbitem) + { + OIC_LOG(ERROR, TAG, "Malloc failed"); + return CA_STATUS_FAILED; + } + + cbitem->adapter = adapter; + cbitem->callback = callback; + LL_APPEND(g_adapterCallbackList, cbitem); + + return CA_STATUS_OK; } -CAInterface_t *CAFindInterfaceChange() +CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter) { - CAInterface_t *foundNewInterface = NULL; + CAIPCBData_t *cbitem = NULL; + CAIPCBData_t *tmpCbitem = NULL; + LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem) + { + if (cbitem && adapter == cbitem->adapter) + { + OIC_LOG(DEBUG, TAG, "remove specific callback"); + LL_DELETE(g_adapterCallbackList, cbitem); + OICFree(cbitem); + return CA_STATUS_OK; + } + } + return CA_STATUS_OK; +} + +u_arraylist_t *CAFindInterfaceChange() +{ + u_arraylist_t *iflist = NULL; char buf[NETLINK_MESSAGE_LENGTH] = { 0 }; struct sockaddr_nl sa = { 0 }; struct iovec iov = { .iov_base = buf, @@ -82,194 +159,186 @@ CAInterface_t *CAFindInterfaceChange() .msg_iov = &iov, .msg_iovlen = 1 }; - size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0); + ssize_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0); for (struct nlmsghdr *nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) { - if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK) + if (nh != NULL && (nh->nlmsg_type != RTM_DELADDR && nh->nlmsg_type != RTM_NEWADDR)) { continue; } struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh); - - int ifiIndex = ifi->ifi_index; - u_arraylist_t *iflist = CAIPGetInterfaceInformation(ifiIndex); - - if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))) + if (!ifi) { continue; } - if (!iflist) + if (RTM_DELADDR == nh->nlmsg_type) { - OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno)); - return NULL; + CloseMulticastSocket(); } - uint32_t listLength = u_arraylist_length(iflist); - for (uint32_t i = 0; i < listLength; i++) + int ifiIndex = ifi->ifi_index; + iflist = CAIPGetInterfaceInformation(ifiIndex); + if (!iflist) { - CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i); - if (!ifitem) - { - continue; - } - - if ((int)ifitem->index != ifiIndex) - { - continue; - } - - foundNewInterface = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family, - ifitem->addr, ifitem->flags); - break; // we found the one we were looking for + OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno)); + return NULL; } - u_arraylist_destroy(iflist); + CreateMulticastSocket(); } - return foundNewInterface; + return iflist; } -CAResult_t CAIPStartNetworkMonitor() +CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback, + CATransportAdapter_t adapter) { - OIC_LOG(DEBUG, TAG, "IN"); - - // Initialize Wifi service - wifi_error_e ret = wifi_initialize(); - if (WIFI_ERROR_NONE != ret) - { - OIC_LOG(ERROR, TAG, "wifi_initialize failed"); - return CA_STATUS_FAILED; - } - - // Set callback for receiving state changes - ret = wifi_set_device_state_changed_cb(CAWIFIDeviceStateChangedCb, NULL); - if (WIFI_ERROR_NONE != ret) + if (!g_adapterCallbackList) { - OIC_LOG(ERROR, TAG, "wifi_set_device_state_changed_cb failed"); - return CA_STATUS_FAILED; - } + // Initialize Connections. + connection_error_e ret = connection_create(&connection); + if (CONNECTION_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "connection_create failed"); + return CA_STATUS_FAILED; + } - // Set callback for receiving connection state changes - ret = wifi_set_connection_state_changed_cb(CAWIFIConnectionStateChangedCb, NULL); - if (WIFI_ERROR_NONE != ret) - { - OIC_LOG(ERROR, TAG, "wifi_set_connection_state_changed_cb failed"); - return CA_STATUS_FAILED; + // Set callback for receiving state changes. + ret = connection_set_type_changed_cb(connection, CAIPConnectionStateChangedCb, NULL); + if (CONNECTION_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "connection_set_type_changed_cb failed"); + return CA_STATUS_FAILED; + } } - OIC_LOG(DEBUG, TAG, "OUT"); - return CA_STATUS_OK; + OIC_LOG(DEBUG, TAG, "Initialize network monitoring successfully"); + return CAIPSetNetworkMonitorCallback(callback, adapter); } -CAResult_t CAIPStopNetworkMonitor() +CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter) { OIC_LOG(DEBUG, TAG, "IN"); - // Reset callback for receiving state changes - wifi_error_e ret = wifi_unset_device_state_changed_cb(); - if (WIFI_ERROR_NONE != ret) + CAIPUnSetNetworkMonitorCallback(adapter); + if (!g_adapterCallbackList) { - OIC_LOG(ERROR, TAG, "wifi_unset_device_state_changed_cb failed"); - } - - // Reset callback for receiving connection state changes - ret = wifi_unset_connection_state_changed_cb(); - if (WIFI_ERROR_NONE != ret) - { - OIC_LOG(ERROR, TAG, "wifi_unset_connection_state_changed_cb failed"); - } + // Reset callback for receiving state changes. + if (connection) + { + connection_error_e ret = connection_unset_type_changed_cb(connection); + if (CONNECTION_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "connection_unset_type_changed_cb failed"); + } - // Deinitialize Wifi service - ret = wifi_deinitialize(); - if (WIFI_ERROR_NONE != ret) - { - OIC_LOG(ERROR, TAG, "wifi_deinitialize failed"); + // Deinitialize Wifi service. + ret = connection_destroy(connection); + if (CONNECTION_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "connection_destroy failed"); + } + connection = NULL; + } } - OIC_LOG(DEBUG, TAG, "OUT"); + OIC_LOG(DEBUG, TAG, "Network monitoring terminated successfully"); return CA_STATUS_OK; } -u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex) +/** + * Used to send netlink query to kernel and recv response from kernel. + * + * @param[in] idx desired network interface index, 0 means all interfaces. + * @param[out] iflist linked list. + * + */ +static bool CAIPGetAddrInfo(int idx, u_arraylist_t *iflist) { - u_arraylist_t *iflist = u_arraylist_create(); - if (!iflist) + if ((idx < 0) || (iflist == NULL)) { - OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno)); - return NULL; + return false; } - char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 }; - struct ifconf ifc = { .ifc_len = MAX_INTERFACE_INFO_LENGTH, .ifc_buf = buf }; - - int s = caglobals.ip.u6.fd != -1 ? caglobals.ip.u6.fd : caglobals.ip.u4.fd; - if (ioctl(s, SIOCGIFCONF, &ifc) < 0) + struct ifaddrs *ifp = NULL; + if (-1 == getifaddrs(&ifp)) { - OIC_LOG_V(ERROR, TAG, "SIOCGIFCONF failed: %s", strerror(errno)); - u_arraylist_destroy(iflist); - return NULL; + OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno)); + return false; } - struct ifreq* ifr = ifc.ifc_req; - size_t interfaces = ifc.ifc_len / sizeof (ifc.ifc_req[0]); - size_t ifreqsize = ifc.ifc_len; - - if (ifreqsize > caglobals.ip.nm.sizeIfItems) + struct ifaddrs *ifa = NULL; + for (ifa = ifp; ifa; ifa = ifa->ifa_next) { - CAIfItem_t *items = (CAIfItem_t *)OICRealloc(caglobals.ip.nm.ifItems, ifreqsize); - if (!items) + if (!ifa->ifa_addr) { - OIC_LOG(ERROR, TAG, "OICRealloc failed"); - goto exit; + continue; } - caglobals.ip.nm.ifItems = items; - caglobals.ip.nm.sizeIfItems = ifreqsize; - } - caglobals.ip.nm.numIfItems = 0; - for (size_t i = 0; i < interfaces; i++) - { - struct ifreq* item = &ifr[i]; - char *name = item->ifr_name; - - if (ioctl(s, SIOCGIFFLAGS, item) < 0) + int family = ifa->ifa_addr->sa_family; + if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family)) { - OIC_LOG_V(ERROR, TAG, "SIOCGIFFLAGS failed: %s", strerror(errno)); continue; } - int16_t flags = item->ifr_flags; - if ((flags & IFF_LOOPBACK) || !(flags & IFF_RUNNING)) + + int ifindex = if_nametoindex(ifa->ifa_name); + if (idx && (ifindex != idx)) { continue; } - if (ioctl(s, SIOCGIFINDEX, item) < 0) + + char ipaddr[MAX_ADDR_STR_SIZE_CA] = {0}; + if (family == AF_INET6) { - OIC_LOG_V(ERROR, TAG, "SIOCGIFINDEX failed: %s", strerror(errno)); - continue; + struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr; + inet_ntop(family, (void *)&(in6->sin6_addr), ipaddr, sizeof(ipaddr)); + } + else if (family == AF_INET) + { + struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr; + inet_ntop(family, (void *)&(in->sin_addr), ipaddr, sizeof(ipaddr)); } - int ifindex = item->ifr_ifindex; - caglobals.ip.nm.ifItems[i].ifIndex = ifindex; - caglobals.ip.nm.numIfItems++; - - if (desiredIndex && (ifindex != desiredIndex)) + if ((strcmp(ipaddr, IFC_ADDR_LOOP_IPV4) == 0) || + (strcmp(ipaddr, IFC_ADDR_LOOP_IPV6) == 0) || + (strcmp(ifa->ifa_name, IFC_LABEL_LOOP) == 0)) { + OIC_LOG(DEBUG, TAG, "LOOPBACK continue!!!"); continue; } - // Get address of network interface. - char addr[MAX_ADDR_STR_SIZE_CA] = { 0 }; - struct sockaddr_in *sa = (struct sockaddr_in *)&item->ifr_addr; - inet_ntop(AF_INET, (void *)&(sa->sin_addr), addr, sizeof(addr)); - - // Add IPv4 interface - CAResult_t result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET, addr, flags); + CAResult_t result = CAAddInterfaceItem(iflist, ifindex, + ifa->ifa_name, family, + ipaddr, ifa->ifa_flags); if (CA_STATUS_OK != result) { + OIC_LOG(ERROR, TAG, "CAAddInterfaceItem fail"); goto exit; } } + freeifaddrs(ifp); + return true; + +exit: + freeifaddrs(ifp); + return false; +} + +u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex) +{ + u_arraylist_t *iflist = u_arraylist_create(); + if (!iflist) + { + OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno)); + return NULL; + } + + if (!CAIPGetAddrInfo(desiredIndex, iflist)) + { + goto exit; + } + return iflist; exit: @@ -315,43 +384,29 @@ static CAInterface_t *CANewInterfaceItem(int index, char *name, int family, return ifitem; } -void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap, - void *userData) +void CAIPConnectionStateChangedCb(connection_type_e type, void* userData) { - OIC_LOG(DEBUG, TAG, "IN"); + (void)userData; - if (WIFI_CONNECTION_STATE_ASSOCIATION == state - || WIFI_CONNECTION_STATE_CONFIGURATION == state) + switch (type) { - OIC_LOG(DEBUG, TAG, "Connection is in Association State"); - return; + case CONNECTION_TYPE_DISCONNECTED: + OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_DISCONNECTED"); + CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN); + break; + case CONNECTION_TYPE_ETHERNET: + OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_ETHERNET"); + CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP); + break; + case CONNECTION_TYPE_WIFI: + OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_WIFI"); + CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP); + break; + case CONNECTION_TYPE_CELLULAR: + OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_CELLULAR"); + CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP); + break; + default: + break; } - - if (WIFI_CONNECTION_STATE_CONNECTED == state) - { - g_networkChangeCallback(CA_ADAPTER_IP, CA_INTERFACE_UP); - } - else - { - g_networkChangeCallback(CA_ADAPTER_IP, CA_INTERFACE_DOWN); - } - - OIC_LOG(DEBUG, TAG, "OUT"); -} - -void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - if (WIFI_DEVICE_STATE_ACTIVATED == state) - { - OIC_LOG(DEBUG, TAG, "Wifi is in Activated State"); - } - else - { - CAWIFIConnectionStateChangedCb(WIFI_CONNECTION_STATE_DISCONNECTED, NULL, NULL); - OIC_LOG(DEBUG, TAG, "Wifi is in Deactivated State"); - } - - OIC_LOG(DEBUG, TAG, "OUT"); }