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/types.h>
28 #include <sys/select.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
39 #include <linux/netlink.h>
40 #include <linux/rtnetlink.h>
41 #include <arpa/inet.h>
42 #include <netinet/in.h>
46 #include "caadapterutils.h"
48 #include "oic_malloc.h"
49 #include "oic_string.h"
51 #define TAG "OIC_CA_IP_MONITOR"
54 * Mutex for synchronizing access to cached interface and IP address information.
56 static ca_mutex g_networkMonitorContextMutex = NULL;
59 * Used to storing network interface.
61 static u_arraylist_t *g_netInterfaceList = NULL;
63 static CAIPConnectionStateChangeCallback g_networkChangeCallback = NULL;
65 static CAResult_t CAIPInitializeNetworkMonitorList();
66 static void CAIPDestroyNetworkMonitorList();
67 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
68 uint32_t addr, int flags);
70 static CAResult_t CAIPInitializeNetworkMonitorList()
72 if (!g_networkMonitorContextMutex)
74 g_networkMonitorContextMutex = ca_mutex_new();
75 if (!g_networkMonitorContextMutex)
77 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
78 return CA_STATUS_FAILED;
82 if (!g_netInterfaceList)
84 g_netInterfaceList = u_arraylist_create();
85 if (!g_netInterfaceList)
87 OIC_LOG(ERROR, TAG, "u_arraylist_create has failed");
88 CAIPDestroyNetworkMonitorList();
89 return CA_STATUS_FAILED;
95 static void CAIPDestroyNetworkMonitorList()
97 if (g_netInterfaceList)
99 u_arraylist_destroy(g_netInterfaceList);
100 g_netInterfaceList = NULL;
103 if (g_networkMonitorContextMutex)
105 ca_mutex_free(g_networkMonitorContextMutex);
106 g_networkMonitorContextMutex = NULL;
110 static bool CACmpNetworkList(uint32_t ifiindex)
112 if (!g_netInterfaceList)
114 OIC_LOG(ERROR, TAG, "g_netInterfaceList is NULL");
118 ca_mutex_lock(g_networkMonitorContextMutex);
120 uint32_t list_length = u_arraylist_length(g_netInterfaceList);
121 for (uint32_t list_index = 0; list_index < list_length; list_index++)
123 CAInterface_t *currItem = (CAInterface_t *) u_arraylist_get(g_netInterfaceList, list_index);
124 if (currItem->index == ifiindex)
126 ca_mutex_unlock(g_networkMonitorContextMutex);
130 ca_mutex_unlock(g_networkMonitorContextMutex);
134 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem)
136 VERIFY_NON_NULL(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
137 VERIFY_NON_NULL(ifitem, TAG, "ifitem is NULL");
139 ca_mutex_lock(g_networkMonitorContextMutex);
140 bool result = u_arraylist_add(g_netInterfaceList, (void *) ifitem);
143 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
144 ca_mutex_unlock(g_networkMonitorContextMutex);
145 return CA_STATUS_FAILED;
147 ca_mutex_unlock(g_networkMonitorContextMutex);
151 static void CARemoveNetworkMonitorList(int ifiindex)
153 VERIFY_NON_NULL_VOID(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
155 ca_mutex_lock(g_networkMonitorContextMutex);
157 uint32_t list_length = u_arraylist_length(g_netInterfaceList);
158 for (uint32_t list_index = 0; list_index < list_length; list_index++)
160 CAInterface_t *removedifitem = (CAInterface_t *) u_arraylist_get(
161 g_netInterfaceList, list_index);
162 if (removedifitem && ((int)removedifitem->index) == ifiindex)
164 if (u_arraylist_remove(g_netInterfaceList, list_index))
166 OICFree(removedifitem);
167 ca_mutex_unlock(g_networkMonitorContextMutex);
173 ca_mutex_unlock(g_networkMonitorContextMutex);
177 CAResult_t CAIPStartNetworkMonitor()
179 return CAIPInitializeNetworkMonitorList();
182 CAResult_t CAIPStopNetworkMonitor()
184 CAIPDestroyNetworkMonitorList();
188 int CAGetPollingInterval(int interval)
193 void CAIPSetNetworkMonitorCallback(CAIPConnectionStateChangeCallback callback)
195 g_networkChangeCallback = callback;
198 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
199 uint32_t addr, int flags)
201 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
204 OIC_LOG(ERROR, TAG, "Malloc failed");
208 OICStrcpy(ifitem->name, sizeof (ifitem->name), name);
209 ifitem->index = index;
210 ifitem->family = family;
211 ifitem->ipv4addr = addr;
212 ifitem->flags = flags;
217 CAInterface_t *CAFindInterfaceChange()
219 CAInterface_t *foundNewInterface = NULL;
221 char buf[4096] = { 0 };
222 struct nlmsghdr *nh = NULL;
223 struct sockaddr_nl sa = { .nl_family = 0 };
224 struct iovec iov = { .iov_base = buf,
225 .iov_len = sizeof (buf) };
226 struct msghdr msg = { .msg_name = (void *)&sa,
227 .msg_namelen = sizeof (sa),
231 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
233 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
235 if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
240 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
242 int ifiIndex = ifi->ifi_index;
243 u_arraylist_t *iflist = CAIPGetInterfaceInformation(ifiIndex);
245 if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING)))
247 bool isFound = CACmpNetworkList(ifiIndex);
250 CARemoveNetworkMonitorList(ifiIndex);
251 if (g_networkChangeCallback)
253 g_networkChangeCallback(CA_ADAPTER_IP ,CA_INTERFACE_DOWN);
261 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
265 uint32_t listLength = u_arraylist_length(iflist);
266 for (uint32_t i = 0; i < listLength; i++)
268 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
274 if ((int)ifitem->index != ifiIndex)
279 foundNewInterface = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
280 ifitem->ipv4addr, ifitem->flags);
281 break; // we found the one we were looking for
283 u_arraylist_destroy(iflist);
286 return foundNewInterface;
289 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
291 if (desiredIndex < 0)
293 OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
297 u_arraylist_t *iflist = u_arraylist_create();
300 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
304 struct ifaddrs *ifp = NULL;
305 if (-1 == getifaddrs(&ifp))
307 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
308 u_arraylist_destroy(iflist);
311 OIC_LOG(DEBUG, TAG, "Got ifaddrs");
313 struct ifaddrs *ifa = NULL;
314 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
320 int family = ifa->ifa_addr->sa_family;
321 if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
326 int ifindex = if_nametoindex(ifa->ifa_name);
327 if (desiredIndex && (ifindex != desiredIndex))
332 int length = u_arraylist_length(iflist);
334 for (int i = length-1; i >= 0; i--)
336 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
339 && (int)ifitem->index == ifindex
340 && ifitem->family == (uint16_t)family)
351 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
354 OIC_LOG(ERROR, TAG, "Malloc failed");
358 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
359 ifitem->index = ifindex;
360 ifitem->family = family;
361 ifitem->ipv4addr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
362 ifitem->flags = ifa->ifa_flags;
364 bool result = u_arraylist_add(iflist, ifitem);
367 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
371 bool isFound = CACmpNetworkList(ifitem->index);
374 CAInterface_t *newifitem = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
375 ifitem->ipv4addr, ifitem->flags);
376 CAResult_t ret = CAAddNetworkMonitorList(newifitem);
377 if (CA_STATUS_OK != ret)
382 if (g_networkChangeCallback)
384 g_networkChangeCallback(CA_ADAPTER_IP, CA_INTERFACE_UP);
386 OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
394 u_arraylist_destroy(iflist);