1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "caipinterface.h"
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/select.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
38 #include <linux/netlink.h>
39 #include <linux/rtnetlink.h>
42 #include "caipnwmonitor.h"
44 #include "caadapterutils.h"
46 #include "oic_malloc.h"
47 #include "oic_string.h"
48 #include <coap/utlist.h>
50 #define TAG "OIC_CA_IP_MONITOR"
53 * Mutex for synchronizing access to cached interface and IP address information.
55 static oc_mutex g_networkMonitorContextMutex = NULL;
58 * Used to storing network interface.
60 static u_arraylist_t *g_netInterfaceList = NULL;
63 * Used to storing adapter changes callback interface.
65 static struct CAIPCBData_t *g_adapterCallbackList = NULL;
68 * Initialize the network interface monitoring list.
70 static CAResult_t CAIPInitializeNetworkMonitorList();
73 * Destroy the network interface monitoring list.
75 static void CAIPDestroyNetworkMonitorList();
78 * Compare the interface with the already added interface in list.
80 static bool CACmpNetworkList(uint32_t ifiindex);
83 * Add new network interface in list.
85 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem);
88 * Remove network interface from list.
90 static void CARemoveNetworkMonitorList(int ifiindex);
93 * Pass the changed network status through the stored callback.
95 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status);
98 * Create new interface item.
100 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
101 const char *addr, int flags);
103 static CAResult_t CAIPInitializeNetworkMonitorList()
105 if (!g_networkMonitorContextMutex)
107 g_networkMonitorContextMutex = oc_mutex_new();
108 if (!g_networkMonitorContextMutex)
110 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
111 return CA_STATUS_FAILED;
115 if (!g_netInterfaceList)
117 g_netInterfaceList = u_arraylist_create();
118 if (!g_netInterfaceList)
120 OIC_LOG(ERROR, TAG, "u_arraylist_create has failed");
121 CAIPDestroyNetworkMonitorList();
122 return CA_STATUS_FAILED;
128 static void CAIPDestroyNetworkMonitorList()
130 if (g_netInterfaceList)
132 u_arraylist_destroy(g_netInterfaceList);
133 g_netInterfaceList = NULL;
136 if (g_networkMonitorContextMutex)
138 oc_mutex_free(g_networkMonitorContextMutex);
139 g_networkMonitorContextMutex = NULL;
143 static bool CACmpNetworkList(uint32_t ifiindex)
145 if (!g_netInterfaceList)
147 OIC_LOG(ERROR, TAG, "g_netInterfaceList is NULL");
151 oc_mutex_lock(g_networkMonitorContextMutex);
153 uint32_t list_length = u_arraylist_length(g_netInterfaceList);
154 for (uint32_t list_index = 0; list_index < list_length; list_index++)
156 CAInterface_t *currItem = (CAInterface_t *) u_arraylist_get(g_netInterfaceList,
158 if (currItem->index == ifiindex)
160 oc_mutex_unlock(g_networkMonitorContextMutex);
164 oc_mutex_unlock(g_networkMonitorContextMutex);
168 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem)
170 VERIFY_NON_NULL(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
171 VERIFY_NON_NULL(ifitem, TAG, "ifitem is NULL");
173 oc_mutex_lock(g_networkMonitorContextMutex);
174 bool result = u_arraylist_add(g_netInterfaceList, (void *) ifitem);
177 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
178 oc_mutex_unlock(g_networkMonitorContextMutex);
179 return CA_STATUS_FAILED;
181 oc_mutex_unlock(g_networkMonitorContextMutex);
185 static void CARemoveNetworkMonitorList(int ifiindex)
187 VERIFY_NON_NULL_VOID(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
189 oc_mutex_lock(g_networkMonitorContextMutex);
191 uint32_t list_length = u_arraylist_length(g_netInterfaceList);
192 for (uint32_t list_index = 0; list_index < list_length; list_index++)
194 CAInterface_t *removedifitem = (CAInterface_t *) u_arraylist_get(
195 g_netInterfaceList, list_index);
196 if (removedifitem && ((int)removedifitem->index) == ifiindex)
198 if (u_arraylist_remove(g_netInterfaceList, list_index))
200 OICFree(removedifitem);
201 oc_mutex_unlock(g_networkMonitorContextMutex);
207 oc_mutex_unlock(g_networkMonitorContextMutex);
211 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
212 CATransportAdapter_t adapter)
214 CAResult_t res = CAIPInitializeNetworkMonitorList();
215 if (CA_STATUS_OK == res)
217 return CAIPSetNetworkMonitorCallback(callback, adapter);
222 CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
224 CAIPDestroyNetworkMonitorList();
225 return CAIPUnSetNetworkMonitorCallback(adapter);
228 int CAGetPollingInterval(int interval)
233 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status)
235 CAIPCBData_t *cbitem = NULL;
236 LL_FOREACH(g_adapterCallbackList, cbitem)
238 if (cbitem && cbitem->adapter)
240 cbitem->callback(cbitem->adapter, status);
245 CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback,
246 CATransportAdapter_t adapter)
250 OIC_LOG(ERROR, TAG, "callback is null");
251 return CA_STATUS_INVALID_PARAM;
254 CAIPCBData_t *cbitem = NULL;
255 LL_FOREACH(g_adapterCallbackList, cbitem)
257 if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback)
259 OIC_LOG(DEBUG, TAG, "this callback is already added");
264 cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem));
267 OIC_LOG(ERROR, TAG, "Malloc failed");
268 return CA_STATUS_FAILED;
271 cbitem->adapter = adapter;
272 cbitem->callback = callback;
273 LL_APPEND(g_adapterCallbackList, cbitem);
278 CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter)
280 CAIPCBData_t *cbitem = NULL;
281 CAIPCBData_t *tmpCbitem = NULL;
282 LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem)
284 if (cbitem && adapter == cbitem->adapter)
286 OIC_LOG(DEBUG, TAG, "remove specific callback");
287 LL_DELETE(g_adapterCallbackList, cbitem);
295 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
296 const char *addr, int flags)
298 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
301 OIC_LOG(ERROR, TAG, "Malloc failed");
305 OICStrcpy(ifitem->name, sizeof (ifitem->name), name);
306 ifitem->index = index;
307 ifitem->family = family;
308 OICStrcpy(ifitem->addr, sizeof (ifitem->addr), addr);
309 ifitem->flags = flags;
314 u_arraylist_t *CAFindInterfaceChange()
316 u_arraylist_t *iflist = NULL;
318 char buf[4096] = { 0 };
319 struct nlmsghdr *nh = NULL;
320 struct sockaddr_nl sa = { .nl_family = 0 };
321 struct iovec iov = { .iov_base = buf,
322 .iov_len = sizeof (buf) };
323 struct msghdr msg = { .msg_name = (void *)&sa,
324 .msg_namelen = sizeof (sa),
328 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
330 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
332 if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
337 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
340 OIC_LOG_V(ERROR, TAG, "ifi is NULL");
344 int ifiIndex = ifi->ifi_index;
346 if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
348 bool isFound = CACmpNetworkList(ifiIndex);
351 CARemoveNetworkMonitorList(ifiIndex);
352 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
357 iflist = CAIPGetInterfaceInformation(ifiIndex);
361 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
369 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
371 if (desiredIndex < 0)
373 OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
377 u_arraylist_t *iflist = u_arraylist_create();
380 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
384 struct ifaddrs *ifp = NULL;
385 if (-1 == getifaddrs(&ifp))
387 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
388 u_arraylist_destroy(iflist);
392 struct ifaddrs *ifa = NULL;
393 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
399 int family = ifa->ifa_addr->sa_family;
400 if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
405 int ifindex = if_nametoindex(ifa->ifa_name);
406 if (desiredIndex && (ifindex != desiredIndex))
411 int length = u_arraylist_length(iflist);
413 for (int i = length-1; i >= 0; i--)
415 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
418 && (int)ifitem->index == ifindex
419 && ifitem->family == (uint16_t)family)
430 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
433 OIC_LOG(ERROR, TAG, "Malloc failed");
437 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
438 ifitem->index = ifindex;
439 ifitem->family = family;
440 ifitem->flags = ifa->ifa_flags;
442 if (ifitem->family == AF_INET6)
444 struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
445 inet_ntop(ifitem->family, (void *)&(in6->sin6_addr), ifitem->addr,
446 sizeof(ifitem->addr));
448 else if (ifitem->family == AF_INET)
450 struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
451 inet_ntop(ifitem->family, (void *)&(in->sin_addr), ifitem->addr,
452 sizeof(ifitem->addr));
455 bool result = u_arraylist_add(iflist, ifitem);
458 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
462 bool isFound = CACmpNetworkList(ifitem->index);
465 CAInterface_t *newifitem = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
466 ifitem->addr, ifitem->flags);
467 CAResult_t ret = CAAddNetworkMonitorList(newifitem);
468 if (CA_STATUS_OK != ret)
473 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
474 OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
482 u_arraylist_destroy(iflist);