Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / linux / caipnwmonitor.c
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 #include "caipinterface.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/select.h>
29 #include <ifaddrs.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 #include <net/if.h>
35 #include <netdb.h>
36 #include <errno.h>
37
38 #ifdef __linux__
39 #include <linux/netlink.h>
40 #include <linux/rtnetlink.h>
41 #include <arpa/inet.h>
42 #include <netinet/in.h>
43 #endif
44
45 #include "camutex.h"
46 #include "caadapterutils.h"
47 #include "logger.h"
48 #include "oic_malloc.h"
49 #include "oic_string.h"
50
51 #define TAG "OIC_CA_IP_MONITOR"
52
53 /**
54  * Mutex for synchronizing access to cached interface and IP address information.
55  */
56 static ca_mutex g_networkMonitorContextMutex = NULL;
57
58 /**
59  * Used to storing network interface.
60  */
61 static u_arraylist_t *g_netInterfaceList = NULL;
62
63 static CAIPConnectionStateChangeCallback g_networkChangeCallback = NULL;
64
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);
69
70 static CAResult_t CAIPInitializeNetworkMonitorList()
71 {
72     if (!g_networkMonitorContextMutex)
73     {
74         g_networkMonitorContextMutex = ca_mutex_new();
75         if (!g_networkMonitorContextMutex)
76         {
77             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
78             return CA_STATUS_FAILED;
79         }
80     }
81
82     if (!g_netInterfaceList)
83     {
84         g_netInterfaceList = u_arraylist_create();
85         if (!g_netInterfaceList)
86         {
87             OIC_LOG(ERROR, TAG, "u_arraylist_create has failed");
88             CAIPDestroyNetworkMonitorList();
89             return CA_STATUS_FAILED;
90         }
91     }
92 }
93
94 static void CAIPDestroyNetworkMonitorList()
95 {
96     if (g_netInterfaceList)
97     {
98         u_arraylist_destroy(g_netInterfaceList);
99         g_netInterfaceList = NULL;
100     }
101
102     if (g_networkMonitorContextMutex)
103     {
104         ca_mutex_free(g_networkMonitorContextMutex);
105         g_networkMonitorContextMutex = NULL;
106     }
107 }
108
109 static bool CACmpNetworkList(uint32_t ifiindex)
110 {
111     if (!g_netInterfaceList)
112     {
113         OIC_LOG(ERROR, TAG, "g_netInterfaceList is NULL");
114         return false;
115     }
116
117     ca_mutex_lock(g_networkMonitorContextMutex);
118
119     uint32_t list_length = u_arraylist_length(g_netInterfaceList);
120     for (uint32_t list_index = 0; list_index < list_length; list_index++)
121     {
122         CAInterface_t *currItem = (CAInterface_t *) u_arraylist_get(g_netInterfaceList, list_index);
123         if (currItem->index == ifiindex)
124         {
125             ca_mutex_unlock(g_networkMonitorContextMutex);
126             return true;
127         }
128     }
129     ca_mutex_unlock(g_networkMonitorContextMutex);
130     return false;
131 }
132
133 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem)
134 {
135     VERIFY_NON_NULL(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
136     VERIFY_NON_NULL(ifitem, TAG, "ifitem is NULL");
137
138     ca_mutex_lock(g_networkMonitorContextMutex);
139     bool result = u_arraylist_add(g_netInterfaceList, (void *) ifitem);
140     if (!result)
141     {
142         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
143         ca_mutex_unlock(g_networkMonitorContextMutex);
144         return CA_STATUS_FAILED;
145     }
146     ca_mutex_unlock(g_networkMonitorContextMutex);
147     return CA_STATUS_OK;
148 }
149
150 static void CARemoveNetworkMonitorList(int ifiindex)
151 {
152     VERIFY_NON_NULL_VOID(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
153
154     ca_mutex_lock(g_networkMonitorContextMutex);
155
156     uint32_t list_length = u_arraylist_length(g_netInterfaceList);
157     for (uint32_t list_index = 0; list_index < list_length; list_index++)
158     {
159         CAInterface_t *removedifitem = (CAInterface_t *) u_arraylist_get(
160                 g_netInterfaceList, list_index);
161         if (removedifitem && removedifitem->index == ifiindex)
162         {
163             if (u_arraylist_remove(g_netInterfaceList, list_index))
164             {
165                 OICFree(removedifitem);
166                 ca_mutex_unlock(g_networkMonitorContextMutex);
167                 return;
168             }
169             continue;
170         }
171     }
172     ca_mutex_unlock(g_networkMonitorContextMutex);
173     return;
174 }
175
176 CAResult_t CAIPStartNetworkMonitor()
177 {
178     return CAIPInitializeNetworkMonitorList();
179 }
180
181 CAResult_t CAIPStopNetworkMonitor()
182 {
183     CAIPDestroyNetworkMonitorList();
184     return CA_STATUS_OK;
185 }
186
187 int CAGetPollingInterval(int interval)
188 {
189     return interval;
190 }
191
192 void CAIPSetNetworkMonitorCallback(CAIPConnectionStateChangeCallback callback)
193 {
194     g_networkChangeCallback = callback;
195 }
196
197 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
198                                          uint32_t addr, int flags)
199 {
200     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
201     if (!ifitem)
202     {
203         OIC_LOG(ERROR, TAG, "Malloc failed");
204         return NULL;
205     }
206
207     OICStrcpy(ifitem->name, sizeof (ifitem->name), name);
208     ifitem->index = index;
209     ifitem->family = family;
210     ifitem->ipv4addr = addr;
211     ifitem->flags = flags;
212
213     return ifitem;
214 }
215
216 CAInterface_t *CAFindInterfaceChange()
217 {
218     CAInterface_t *foundNewInterface = NULL;
219 #ifdef __linux__
220     char buf[4096];
221     struct nlmsghdr *nh;
222     struct sockaddr_nl sa;
223     struct iovec iov = { buf, sizeof (buf) };
224     struct msghdr msg = { (void *)&sa, sizeof (sa), &iov, 1, NULL, 0, 0 };
225
226     size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
227
228     for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
229     {
230         if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
231         {
232             continue;
233         }
234
235         struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
236
237         int ifiIndex = ifi->ifi_index;
238         u_arraylist_t *iflist = CAIPGetInterfaceInformation(ifiIndex);
239
240         if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING)))
241         {
242             bool isFound = CACmpNetworkList(ifiIndex);
243             if (isFound)
244             {
245                 CARemoveNetworkMonitorList(ifiIndex);
246                 if (g_networkChangeCallback)
247                 {
248                     g_networkChangeCallback(CA_ADAPTER_IP ,CA_INTERFACE_DOWN);
249                 }
250             }
251             continue;
252         }
253
254         if (!iflist)
255         {
256             OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
257             return NULL;
258         }
259
260         uint32_t listLength = u_arraylist_length(iflist);
261         for (uint32_t i = 0; i < listLength; i++)
262         {
263             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
264             if (!ifitem)
265             {
266                 continue;
267             }
268
269             if ((int)ifitem->index != ifiIndex)
270             {
271                 continue;
272             }
273
274             foundNewInterface = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
275                                                    ifitem->ipv4addr, ifitem->flags);
276             break;    // we found the one we were looking for
277         }
278         u_arraylist_destroy(iflist);
279     }
280 #endif
281     return foundNewInterface;
282 }
283
284 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
285 {
286     if (desiredIndex < 0)
287     {
288         OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
289         return NULL;
290     }
291
292     u_arraylist_t *iflist = u_arraylist_create();
293     if (!iflist)
294     {
295         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
296         return NULL;
297     }
298
299     struct ifaddrs *ifp = NULL;
300     if (-1 == getifaddrs(&ifp))
301     {
302         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
303         u_arraylist_destroy(iflist);
304         return NULL;
305     }
306     OIC_LOG(DEBUG, TAG, "Got ifaddrs");
307
308     struct ifaddrs *ifa = NULL;
309     for (ifa = ifp; ifa; ifa = ifa->ifa_next)
310     {
311         if (!ifa->ifa_addr)
312         {
313             continue;
314         }
315         int family = ifa->ifa_addr->sa_family;
316         if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
317         {
318             continue;
319         }
320
321         int ifindex = if_nametoindex(ifa->ifa_name);
322         if (desiredIndex && (ifindex != desiredIndex))
323         {
324             continue;
325         }
326
327         int length = u_arraylist_length(iflist);
328         int already = false;
329         for (int i = length-1; i >= 0; i--)
330         {
331             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
332
333             if (ifitem
334                 && (int)ifitem->index == ifindex
335                 && ifitem->family == (uint16_t)family)
336             {
337                 already = true;
338                 break;
339             }
340         }
341         if (already)
342         {
343             continue;
344         }
345
346         CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
347         if (!ifitem)
348         {
349             OIC_LOG(ERROR, TAG, "Malloc failed");
350             goto exit;
351         }
352
353         OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
354         ifitem->index = ifindex;
355         ifitem->family = family;
356         ifitem->ipv4addr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
357         ifitem->flags = ifa->ifa_flags;
358
359         bool result = u_arraylist_add(iflist, ifitem);
360         if (!result)
361         {
362             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
363             goto exit;
364         }
365
366         bool isFound = CACmpNetworkList(ifitem->index);
367         if (!isFound)
368         {
369             CAInterface_t *newifitem = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
370                                                           ifitem->ipv4addr, ifitem->flags);
371             CAResult_t ret = CAAddNetworkMonitorList(newifitem);
372             if (CA_STATUS_OK != ret)
373             {
374                 OICFree(newifitem);
375                 goto exit;
376             }
377             if (g_networkChangeCallback)
378             {
379                 g_networkChangeCallback(CA_ADAPTER_IP, CA_INTERFACE_UP);
380             }
381             OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
382         }
383     }
384     freeifaddrs(ifp);
385     return iflist;
386
387 exit:
388     freeifaddrs(ifp);
389     u_arraylist_destroy(iflist);
390     return NULL;
391 }