#include "caipinterface.h"
+#include <stdio.h>
+#include <string.h>
#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/select.h>
#include <ifaddrs.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
#include <net/if.h>
-#include <sys/socket.h>
#include <netdb.h>
-#include <string.h>
#include <errno.h>
-#include <unistd.h>
+#ifdef __linux__
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#endif
+
+#include "octhread.h"
+#include "caipnwmonitor.h"
#include "caadapterutils.h"
-#include "camutex.h"
#include "logger.h"
#include "oic_malloc.h"
#include "oic_string.h"
+#include <coap/utlist.h>
-#define IP_MONITOR_TAG "IP_MONITOR"
+#define TAG "OIC_CA_IP_MONITOR"
/**
- * @var g_networkMonitorContextMutex
- * @brief Mutex for synchronizing access to cached interface and IP address information.
+ * Mutex for synchronizing access to cached interface and IP address information.
*/
-static ca_mutex g_networkMonitorContextMutex = NULL;
+static oc_mutex g_networkMonitorContextMutex = NULL;
/**
- * @var g_stopNetworkMonitor
- * @brief Used to stop the network monitor thread.
+ * Used to storing network interface.
*/
-static bool g_stopNetworkMonitor = false;
+static u_arraylist_t *g_netInterfaceList = NULL;
/**
- * @var g_stopNetworkMonitorMutex
- * @brief Mutex for synchronizing access to g_stopNetworkMonitor flag.
+ * Used to storing adapter changes callback interface.
*/
-static ca_mutex g_stopNetworkMonitorMutex = NULL;
+static struct CAIPCBData_t *g_adapterCallbackList = NULL;
/**
- * @struct CAIPNwMonitorContext
- * @brief Used for storing network monitor context information.
+ * Initialize the network interface monitoring list.
*/
-typedef struct
-{
- u_arraylist_t *netInterfaceList;
- ca_thread_pool_t threadPool;
- CANetworkStatus_t nwConnectivityStatus;
- CAIPConnectionStateChangeCallback networkChangeCb;
-} CAIPNetworkMonitorContext;
+static CAResult_t CAIPInitializeNetworkMonitorList();
/**
- * @var g_networkMonitorContext
- * @brief network monitor context.
+ * Destroy the network interface monitoring list.
*/
-static CAIPNetworkMonitorContext *g_networkMonitorContext = NULL;
-
-static void CAIPGetInterfaceInformation(u_arraylist_t **netInterfaceList)
-{
-
- VERIFY_NON_NULL_VOID(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null");
-
- struct ifaddrs *ifp = NULL;
- if (-1 == getifaddrs(&ifp))
- {
- OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get interface list!, Error code: %s",
- strerror(errno));
- return;
- }
-
- struct ifaddrs *ifa = NULL;
- for (ifa = ifp; ifa; ifa = ifa->ifa_next)
- {
- char interfaceAddress[CA_IPADDR_SIZE] = {0};
- char interfaceSubnetMask[CA_IPADDR_SIZE] = {0};
- socklen_t len = sizeof(struct sockaddr_in);
-
- if (!ifa->ifa_addr)
- {
- continue;
- }
-
- int type = ifa->ifa_addr->sa_family;
- if (ifa->ifa_flags & IFF_LOOPBACK
- || !((ifa->ifa_flags & IFF_UP) && (ifa->ifa_flags & IFF_RUNNING)))
- {
- continue;
- }
+static void CAIPDestroyNetworkMonitorList();
- if (AF_INET != type)
- {
- continue;
- }
-
- // get the interface ip address
- if (0 != getnameinfo(ifa->ifa_addr, len, interfaceAddress,
- sizeof(interfaceAddress), NULL, 0, NI_NUMERICHOST))
- {
- OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get IPAddress, Error code: %s",
- strerror(errno));
- continue;
- }
+/**
+ * Compare the interface with the already added interface in list.
+ */
+static bool CACmpNetworkList(uint32_t ifiindex);
- // get the interface subnet mask
- if (0 != getnameinfo(ifa->ifa_netmask, len, interfaceSubnetMask,
- sizeof(interfaceSubnetMask), NULL, 0, NI_NUMERICHOST))
- {
- OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get subnet mask, Error code: %s",
- strerror(errno));
- continue;
- }
+/**
+ * Add new network interface in list.
+ */
+static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem);
- CANetInfo_t *netInfo = (CANetInfo_t *)OICCalloc(1, sizeof(CANetInfo_t));
- if (!netInfo)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed");
- freeifaddrs(ifp);
- return;
- }
- // set interface name
- strncpy(netInfo->interfaceName, ifa->ifa_name, sizeof(netInfo->interfaceName) - 1);
- netInfo->interfaceName[sizeof(netInfo->interfaceName)-1] = '\0';
+/**
+ * Remove network interace from list.
+ */
+static void CARemoveNetworkMonitorList(int ifiindex);
- // set local ip address
- strncpy(netInfo->ipAddress, interfaceAddress, strlen(interfaceAddress));
+/**
+ * Pass the changed network status through the stored callback.
+ */
+static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status);
- // set subnet mask
- strncpy(netInfo->subnetMask, interfaceSubnetMask, strlen(interfaceSubnetMask));
+/**
+ * Create new interface item.
+ */
+static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
+ const char *addr, int flags);
- CAResult_t result = u_arraylist_add(*netInterfaceList, (void *)netInfo);
- if (CA_STATUS_OK != result)
+static CAResult_t CAIPInitializeNetworkMonitorList()
+{
+ if (!g_networkMonitorContextMutex)
+ {
+ g_networkMonitorContextMutex = oc_mutex_new();
+ if (!g_networkMonitorContextMutex)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!Thread exiting.");
- freeifaddrs(ifp);
- return;
+ OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
+ return CA_STATUS_FAILED;
}
}
- freeifaddrs(ifp);
-}
-
-static bool CACheckIsAnyInterfaceDown(const u_arraylist_t *netInterfaceList,
- const CANetInfo_t *info)
-{
- VERIFY_NON_NULL_RET(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null", false);
- VERIFY_NON_NULL_RET(info, IP_MONITOR_TAG, "info is null", false);
-
- uint32_t list_index = 0;
- uint32_t list_length = u_arraylist_length(netInterfaceList);
- for (list_index = 0; list_index < list_length; list_index++)
+ if (!g_netInterfaceList)
{
- CANetInfo_t *netInfo = (CANetInfo_t *)u_arraylist_get(netInterfaceList,
- list_index);
- if (!netInfo)
- {
- continue;
- }
- if (strncmp(netInfo->interfaceName, info->interfaceName, strlen(info->interfaceName)) == 0)
+ g_netInterfaceList = u_arraylist_create();
+ if (!g_netInterfaceList)
{
- return false;
+ OIC_LOG(ERROR, TAG, "u_arraylist_create has failed");
+ CAIPDestroyNetworkMonitorList();
+ return CA_STATUS_FAILED;
}
}
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Interface is down");
- return true;
+ return CA_STATUS_OK;
}
-static bool CACheckIsInterfaceInfoChanged(const CANetInfo_t *info)
+static void CAIPDestroyNetworkMonitorList()
{
- VERIFY_NON_NULL_RET(info, IP_MONITOR_TAG, "info is null", false);
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- uint32_t list_index = 0;
- uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- for (list_index = 0; list_index < list_length; list_index++)
+ if (g_netInterfaceList)
{
- CANetInfo_t *netInfo = (CANetInfo_t *)u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, list_index);
- if (!netInfo)
- {
- continue;
- }
- if (strncmp(netInfo->interfaceName, info->interfaceName, strlen(info->interfaceName)) == 0)
- {
- if (strncmp(netInfo->ipAddress, info->ipAddress, strlen(info->ipAddress)) == 0)
- {
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return false;
- }
- else
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network interface info changed");
- if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, list_index))
- {
- if (g_networkMonitorContext->networkChangeCb)
- {
- g_networkMonitorContext->networkChangeCb(netInfo->ipAddress,
- CA_INTERFACE_DOWN);
- }
- OICFree(netInfo);
- }
- else
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
- }
- break;
- }
- }
+ u_arraylist_destroy(g_netInterfaceList);
+ g_netInterfaceList = NULL;
}
- CANetInfo_t *newNetInfo = (CANetInfo_t *)OICMalloc(sizeof(CANetInfo_t));
- if (!newNetInfo)
+ if (g_networkMonitorContextMutex)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "newNetInfo malloc failed");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return false;
+ oc_mutex_free(g_networkMonitorContextMutex);
+ g_networkMonitorContextMutex = NULL;
}
- memcpy(newNetInfo, info, sizeof(*newNetInfo));
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "New Interface found");
+}
- CAResult_t result = u_arraylist_add(g_networkMonitorContext->netInterfaceList,
- (void *)newNetInfo);
- if (CA_STATUS_OK != result)
+static bool CACmpNetworkList(uint32_t ifiindex)
+{
+ if (!g_netInterfaceList)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
- OICFree(newNetInfo);
- ca_mutex_unlock(g_networkMonitorContextMutex);
+ OIC_LOG(ERROR, TAG, "g_netInterfaceList is NULL");
return false;
}
- ca_mutex_unlock(g_networkMonitorContextMutex);
- /*Callback will be unset only at the time of termination. By that time, all the threads will be
- stopped gracefully. This callback is properly protected*/
- if (g_networkMonitorContext->networkChangeCb)
+ oc_mutex_lock(g_networkMonitorContextMutex);
+
+ uint32_t list_length = u_arraylist_length(g_netInterfaceList);
+ for (uint32_t list_index = 0; list_index < list_length; list_index++)
{
- g_networkMonitorContext->networkChangeCb(newNetInfo->ipAddress, CA_INTERFACE_UP);
+ CAInterface_t *currItem = (CAInterface_t *) u_arraylist_get(g_netInterfaceList,
+ list_index);
+ if (currItem->index == ifiindex)
+ {
+ oc_mutex_unlock(g_networkMonitorContextMutex);
+ return true;
+ }
}
-
- return true;
+ oc_mutex_unlock(g_networkMonitorContextMutex);
+ return false;
}
-static void CANetworkMonitorThread(void *threadData)
+static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem)
{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
+ VERIFY_NON_NULL(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
+ VERIFY_NON_NULL(ifitem, TAG, "ifitem is NULL");
- while (!g_stopNetworkMonitor)
+ oc_mutex_lock(g_networkMonitorContextMutex);
+ bool result = u_arraylist_add(g_netInterfaceList, (void *) ifitem);
+ if (!result)
{
- u_arraylist_t *netInterfaceList = u_arraylist_create();
-
- VERIFY_NON_NULL_VOID(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null");
-
- CAIPGetInterfaceInformation(&netInterfaceList);
+ OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
+ oc_mutex_unlock(g_networkMonitorContextMutex);
+ return CA_STATUS_FAILED;
+ }
+ oc_mutex_unlock(g_networkMonitorContextMutex);
+ return CA_STATUS_OK;
+}
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext->netInterfaceList)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG,
- "u_arraylist_create failed. Network Monitor thread stopped");
- CAClearNetInterfaceInfoList(netInterfaceList);
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return;
- }
+static void CARemoveNetworkMonitorList(int ifiindex)
+{
+ VERIFY_NON_NULL_VOID(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
- uint32_t listIndex = 0;
- uint32_t listLength = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- for (listIndex = 0; listIndex < listLength;)
- {
- CANetInfo_t *info = (CANetInfo_t *)u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, listIndex);
- if (!info)
- {
- listIndex++;
- continue;
- }
+ oc_mutex_lock(g_networkMonitorContextMutex);
- bool ret = CACheckIsAnyInterfaceDown(netInterfaceList, info);
- if (ret)
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Interface is down");
- if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, listIndex))
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "u_arraylist_remove success");
- if (g_networkMonitorContext->networkChangeCb)
- {
- g_networkMonitorContext->networkChangeCb(info->ipAddress,
- CA_INTERFACE_DOWN);
- }
- OICFree(info);
- listLength--;
- }
- else
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
- break;
- }
- }
- else
- {
- listIndex++;
- }
- }
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- listLength = u_arraylist_length(netInterfaceList);
- for (listIndex = 0; listIndex < listLength; listIndex++)
+ uint32_t list_length = u_arraylist_length(g_netInterfaceList);
+ for (uint32_t list_index = 0; list_index < list_length; list_index++)
+ {
+ CAInterface_t *removedifitem = (CAInterface_t *) u_arraylist_get(
+ g_netInterfaceList, list_index);
+ if (removedifitem && ((int)removedifitem->index) == ifiindex)
{
- CANetInfo_t *info = (CANetInfo_t *)u_arraylist_get(netInterfaceList, listIndex);
- if (!info)
- {
- continue;
- }
- bool ret = CACheckIsInterfaceInfoChanged(info);
- if (ret)
+ if (u_arraylist_remove(g_netInterfaceList, list_index))
{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CACheckIsInterfaceInfoChanged true");
+ OICFree(removedifitem);
+ oc_mutex_unlock(g_networkMonitorContextMutex);
+ return;
}
+ continue;
}
- CAClearNetInterfaceInfoList(netInterfaceList);
- sleep(2); // To avoid maximum cpu usage.
}
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
+ oc_mutex_unlock(g_networkMonitorContextMutex);
+ return;
}
-static CAResult_t CAInitializeNetworkMonitorMutexes()
+CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
+ CATransportAdapter_t adapter)
{
- if (!g_networkMonitorContextMutex)
+ CAResult_t res = CAIPInitializeNetworkMonitorList();
+ if (CA_STATUS_OK == res)
{
- g_networkMonitorContextMutex = ca_mutex_new();
- if (!g_networkMonitorContextMutex)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContextMutex Malloc failed");
- return CA_MEMORY_ALLOC_FAILED;
- }
+ return CAIPSetNetworkMonitorCallback(callback, adapter);
}
+ return res;
+}
+
+CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
+{
+ CAIPDestroyNetworkMonitorList();
+ return CAIPUnSetNetworkMonitorCallback(adapter);
+}
- if (!g_stopNetworkMonitorMutex)
+int CAGetPollingInterval(int interval)
+{
+ return interval;
+}
+
+static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status)
+{
+ CAIPCBData_t *cbitem = NULL;
+ LL_FOREACH(g_adapterCallbackList, cbitem)
{
- g_stopNetworkMonitorMutex = ca_mutex_new();
- if (!g_stopNetworkMonitorMutex)
+ if (cbitem && cbitem->adapter)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_stopNetworkMonitorMutex Malloc failed");
- ca_mutex_free(g_networkMonitorContextMutex);
- return CA_MEMORY_ALLOC_FAILED;
+ cbitem->callback(cbitem->adapter, status);
+ CALogAdapterStateInfo(cbitem->adapter, status);
}
}
- return CA_STATUS_OK;
-}
-static void CADestroyNetworkMonitorMutexes()
-{
- ca_mutex_free(g_networkMonitorContextMutex);
- g_networkMonitorContextMutex = NULL;
-
- ca_mutex_free(g_stopNetworkMonitorMutex);
- g_stopNetworkMonitorMutex = NULL;
}
-CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
+CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback,
+ CATransportAdapter_t adapter)
{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- VERIFY_NON_NULL(threadPool, IP_MONITOR_TAG, "threadPool is null");
-
- CAResult_t ret = CAInitializeNetworkMonitorMutexes();
-
- if (CA_STATUS_OK != ret)
+ if (!callback)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "CAInitializeNetworkMonitorMutexes failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(ERROR, TAG, "callback is null");
+ return CA_STATUS_INVALID_PARAM;
}
- ca_mutex_lock(g_networkMonitorContextMutex);
-
- g_networkMonitorContext = (CAIPNetworkMonitorContext *)OICCalloc(1,
- sizeof(*g_networkMonitorContext));
- if (!g_networkMonitorContext)
+ CAIPCBData_t *cbitem = NULL;
+ LL_FOREACH(g_adapterCallbackList, cbitem)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext Malloc failed");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- CADestroyNetworkMonitorMutexes();
- return CA_MEMORY_ALLOC_FAILED;
+ if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback)
+ {
+ OIC_LOG(DEBUG, TAG, "this callback is already added");
+ return CA_STATUS_OK;
+ }
}
- g_networkMonitorContext->threadPool = threadPool;
- g_networkMonitorContext->netInterfaceList = u_arraylist_create();
- if (!g_networkMonitorContext->netInterfaceList)
+ cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem));
+ if (!cbitem)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_create failed");
- OICFree(g_networkMonitorContext);
- ca_mutex_unlock(g_networkMonitorContextMutex);
- CADestroyNetworkMonitorMutexes();
- return CA_MEMORY_ALLOC_FAILED;
+ OIC_LOG(ERROR, TAG, "Malloc failed");
+ return CA_STATUS_FAILED;
}
- CAIPGetInterfaceInformation(&g_networkMonitorContext->netInterfaceList);
+ cbitem->adapter = adapter;
+ cbitem->callback = callback;
+ LL_APPEND(g_adapterCallbackList, cbitem);
+ return CA_STATUS_OK;
+}
- if (u_arraylist_length(g_networkMonitorContext->netInterfaceList))
- {
- g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_UP;
- }
- else
+CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter)
+{
+ CAIPCBData_t *cbitem = NULL;
+ CAIPCBData_t *tmpCbitem = NULL;
+ LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem)
{
- g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
+ if (cbitem && adapter == cbitem->adapter)
+ {
+ OIC_LOG(DEBUG, TAG, "remove specific callback");
+ LL_DELETE(g_adapterCallbackList, cbitem);
+ OICFree(cbitem);
+ return CA_STATUS_OK;
+ }
}
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
return CA_STATUS_OK;
}
-void CAIPTerminateNetworkMonitor()
+static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
+ const char *addr, int flags)
{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- g_networkMonitorContext->threadPool = NULL;
-
- CAClearNetInterfaceInfoList(g_networkMonitorContext->netInterfaceList);
-
- g_networkMonitorContext->netInterfaceList = NULL;
- g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
- g_networkMonitorContext->networkChangeCb = NULL;
- g_networkMonitorContext->threadPool = NULL;
-
- OICFree(g_networkMonitorContext);
- g_networkMonitorContext = NULL;
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- ca_mutex_lock(g_stopNetworkMonitorMutex);
- g_stopNetworkMonitor = true;
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
+ CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
+ if (!ifitem)
+ {
+ OIC_LOG(ERROR, TAG, "Malloc failed");
+ return NULL;
+ }
- CADestroyNetworkMonitorMutexes();
+ OICStrcpy(ifitem->name, sizeof (ifitem->name), name);
+ ifitem->index = index;
+ ifitem->family = family;
+ OICStrcpy(ifitem->addr, sizeof (ifitem->addr), addr);
+ ifitem->flags = flags;
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
+ return ifitem;
}
-CAResult_t CAIPStartNetworkMonitor()
+u_arraylist_t *CAFindInterfaceChange()
{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- ca_mutex_lock(g_stopNetworkMonitorMutex);
-
- g_stopNetworkMonitor = false;
-
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
+ u_arraylist_t *iflist = NULL;
+#ifdef __linux__
+ char buf[4096] = { 0 };
+ struct nlmsghdr *nh = NULL;
+ struct sockaddr_nl sa = { .nl_family = 0 };
+ struct iovec iov = { .iov_base = buf,
+ .iov_len = sizeof (buf) };
+ struct msghdr msg = { .msg_name = (void *)&sa,
+ .msg_namelen = sizeof (sa),
+ .msg_iov = &iov,
+ .msg_iovlen = 1 };
+
+ ssize_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
+
+ for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
+ {
+ if (nh != NULL && (nh->nlmsg_type != RTM_DELADDR && nh->nlmsg_type != RTM_NEWADDR))
+ {
+ continue;
+ }
- ca_mutex_lock(g_networkMonitorContextMutex);
+ if (RTM_DELADDR == nh->nlmsg_type)
+ {
+ struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA (nh);
+ int ifiIndex = ifa->ifa_index;
+ bool isFound = CACmpNetworkList(ifiIndex);
+ if (isFound)
+ {
+ CARemoveNetworkMonitorList(ifiIndex);
+ CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
+ }
+ continue;
+ }
- if (!g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext is null");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_STATUS_FAILED;
- }
+ // Netlink message type is RTM_NEWADDR.
+ struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA (nh);
+ int ifiIndex = ifa->ifa_index;
- if (CA_STATUS_OK != ca_thread_pool_add_task(g_networkMonitorContext->threadPool,
- CANetworkMonitorThread, NULL))
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "[ThreadPool] thread_pool_add_task failed!");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_STATUS_FAILED;
+ iflist = CAIPGetInterfaceInformation(ifiIndex);
+ if (!iflist)
+ {
+ OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
+ return NULL;
+ }
}
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
+#endif
+ return iflist;
}
-CAResult_t CAIPStopNetworkMonitor()
+u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext)
+ if (desiredIndex < 0)
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext is null");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_STATUS_FAILED;
+ OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
+ return NULL;
}
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- ca_mutex_lock(g_stopNetworkMonitorMutex);
- if (!g_stopNetworkMonitor)
+ u_arraylist_t *iflist = u_arraylist_create();
+ if (!iflist)
{
- g_stopNetworkMonitor = true;
+ OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
+ return NULL;
}
- else
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStopNetworkMonitor, already stopped!");
- }
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "u_array_list is null");
- VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG,
- "g_networkMonitorContext is null");
- VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
- "g_networkMonitorContextMutex is null");
-
- // Get the interface and ipaddress information from cache
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext->netInterfaceList
- || !(u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
+ struct ifaddrs *ifp = NULL;
+ if (-1 == getifaddrs(&ifp))
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "Network not enabled");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_ADAPTER_NOT_ENABLED;
+ OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
+ u_arraylist_destroy(iflist);
+ return NULL;
}
- uint32_t list_index = 0;
- uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo list length [%d]",
- list_length);
- for (list_index = 0; list_index < list_length; list_index++)
+ struct ifaddrs *ifa = NULL;
+ for (ifa = ifp; ifa; ifa = ifa->ifa_next)
{
- CANetInfo_t *info = (CANetInfo_t *)u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, list_index);
- if (!info)
+ if (!ifa->ifa_addr)
{
continue;
}
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo ip [%s]",
- info->ipAddress);
- CANetInfo_t *newNetinfo = (CANetInfo_t *) OICMalloc(sizeof(CANetInfo_t));
- if (!newNetinfo)
+ int family = ifa->ifa_addr->sa_family;
+ if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed!");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_MEMORY_ALLOC_FAILED;
+ continue;
}
- memcpy(newNetinfo, info, sizeof(*info));
-
- CAResult_t result = u_arraylist_add(*netInterfaceList, (void *)newNetinfo);
- if (CA_STATUS_OK != result)
+ int ifindex = if_nametoindex(ifa->ifa_name);
+ if (desiredIndex && (ifindex != desiredIndex))
{
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_STATUS_FAILED;
+ continue;
}
- }
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- VERIFY_NON_NULL(subnetMask, IP_MONITOR_TAG, "subnet mask");
- VERIFY_NON_NULL(ipAddress, IP_MONITOR_TAG, "ipAddress is null");
- VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG,
- "g_networkMonitorContext is null");
- VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
- "g_networkMonitorContextMutex is null");
-
- // Get the interface and ipaddress information from cache
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext->netInterfaceList
- || (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network not enabled");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_ADAPTER_NOT_ENABLED;
- }
+ int length = u_arraylist_length(iflist);
+ int already = false;
+ for (int i = length-1; i >= 0; i--)
+ {
+ CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
- uint32_t list_index = 0;
- uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "list lenght [%d]", list_length);
- for (list_index = 0; list_index < list_length; list_index++)
- {
- CANetInfo_t *info = (CANetInfo_t *)u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, list_index);
- if (!info)
+ if (ifitem
+ && (int)ifitem->index == ifindex
+ && ifitem->family == (uint16_t)family)
+ {
+ already = true;
+ break;
+ }
+ }
+ if (already)
{
continue;
}
- if (strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0)
+ CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
+ if (!ifitem)
{
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG,
- "CAIPGetInterfaceSubnetMask subnetmask is %s", info->subnetMask);
- *subnetMask = OICStrdup(info->subnetMask);
- break;
+ OIC_LOG(ERROR, TAG, "Malloc failed");
+ goto exit;
}
- }
- ca_mutex_unlock(g_networkMonitorContextMutex);
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-bool CAIPIsConnected()
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
- if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "IP is not connected");
- return false;
- }
+ OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
+ ifitem->index = ifindex;
+ ifitem->family = family;
+ ifitem->flags = ifa->ifa_flags;
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList))
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "IP is not connected");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return false;
- }
- ca_mutex_unlock(g_networkMonitorContextMutex);
+ if (ifitem->family == AF_INET6)
+ {
+ struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
+ inet_ntop(ifitem->family, (void *)&(in6->sin6_addr), ifitem->addr,
+ sizeof(ifitem->addr));
+ }
+ else if (ifitem->family == AF_INET)
+ {
+ struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
+ inet_ntop(ifitem->family, (void *)&(in->sin_addr), ifitem->addr,
+ sizeof(ifitem->addr));
+ }
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return true;
-}
+ bool result = u_arraylist_add(iflist, ifitem);
+ if (!result)
+ {
+ OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
+ goto exit;
+ }
-void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
- if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "CAIPSetConnectionStateChangeCallback failed");
- return;
+ bool isFound = CACmpNetworkList(ifitem->index);
+ if (!isFound)
+ {
+ CAInterface_t *newifitem = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
+ ifitem->addr, ifitem->flags);
+ CAResult_t ret = CAAddNetworkMonitorList(newifitem);
+ if (CA_STATUS_OK != ret)
+ {
+ OICFree(newifitem);
+ goto exit;
+ }
+ CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
+ OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
+ }
}
- ca_mutex_lock(g_networkMonitorContextMutex);
-
- g_networkMonitorContext->networkChangeCb = callback;
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
+#ifndef __TIZENRT__
+ freeifaddrs(ifp);
+#endif
+ return iflist;
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
+exit:
+#ifndef __TIZENRT__
+ freeifaddrs(ifp);
+#endif
+ u_arraylist_destroy(iflist);
+ return NULL;
}
-