added log messages on ip adapter to improve debugging
[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/select.h>
28 #include <ifaddrs.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <net/if.h>
34 #include <netdb.h>
35 #include <errno.h>
36
37 #ifdef __linux__
38 #include <linux/netlink.h>
39 #include <linux/rtnetlink.h>
40 #endif
41
42 #include "caipnwmonitor.h"
43 #include "octhread.h"
44 #include "caadapterutils.h"
45 #include "logger.h"
46 #include "oic_malloc.h"
47 #include "oic_string.h"
48 #include <coap/utlist.h>
49
50 #define TAG "OIC_CA_IP_MONITOR"
51
52 /**
53  * Mutex for synchronizing access to cached interface and IP address information.
54  */
55 static oc_mutex g_networkMonitorContextMutex = NULL;
56
57 /**
58  * Used to storing network interface.
59  */
60 static u_arraylist_t *g_netInterfaceList = NULL;
61
62 /**
63  * Used to storing adapter changes callback interface.
64  */
65 static struct CAIPCBData_t *g_adapterCallbackList = NULL;
66
67 /**
68  * Initialize the network interface monitoring list.
69  */
70 static CAResult_t CAIPInitializeNetworkMonitorList();
71
72 /**
73  * Destroy the network interface monitoring list.
74  */
75 static void CAIPDestroyNetworkMonitorList();
76
77 /**
78  * Compare the interface with the already added interface in list.
79  */
80 static bool CACmpNetworkList(uint32_t ifiindex);
81
82 /**
83  * Add new network interface in list.
84  */
85 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem);
86
87 /**
88  * Remove network interface from list.
89  */
90 static void CARemoveNetworkMonitorList(int ifiindex);
91
92 /**
93  * Pass the changed network status through the stored callback.
94  */
95 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status);
96
97 /**
98  * Create new interface item.
99  */
100 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
101                                          const char *addr, int flags);
102
103 static CAResult_t CAIPInitializeNetworkMonitorList()
104 {
105     if (!g_networkMonitorContextMutex)
106     {
107         g_networkMonitorContextMutex = oc_mutex_new();
108         if (!g_networkMonitorContextMutex)
109         {
110             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
111             return CA_STATUS_FAILED;
112         }
113     }
114
115     if (!g_netInterfaceList)
116     {
117         g_netInterfaceList = u_arraylist_create();
118         if (!g_netInterfaceList)
119         {
120             OIC_LOG(ERROR, TAG, "u_arraylist_create has failed");
121             CAIPDestroyNetworkMonitorList();
122             return CA_STATUS_FAILED;
123         }
124     }
125     return CA_STATUS_OK;
126 }
127
128 static void CAIPDestroyNetworkMonitorList()
129 {
130     if (g_netInterfaceList)
131     {
132         u_arraylist_destroy(g_netInterfaceList);
133         g_netInterfaceList = NULL;
134     }
135
136     if (g_networkMonitorContextMutex)
137     {
138         oc_mutex_free(g_networkMonitorContextMutex);
139         g_networkMonitorContextMutex = NULL;
140     }
141 }
142
143 static bool CACmpNetworkList(uint32_t ifiindex)
144 {
145     if (!g_netInterfaceList)
146     {
147         OIC_LOG(ERROR, TAG, "g_netInterfaceList is NULL");
148         return false;
149     }
150
151     oc_mutex_lock(g_networkMonitorContextMutex);
152
153     uint32_t list_length = u_arraylist_length(g_netInterfaceList);
154     for (uint32_t list_index = 0; list_index < list_length; list_index++)
155     {
156         CAInterface_t *currItem = (CAInterface_t *) u_arraylist_get(g_netInterfaceList,
157                                                                     list_index);
158         if (currItem->index == ifiindex)
159         {
160             oc_mutex_unlock(g_networkMonitorContextMutex);
161             return true;
162         }
163     }
164     oc_mutex_unlock(g_networkMonitorContextMutex);
165     return false;
166 }
167
168 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem)
169 {
170     VERIFY_NON_NULL(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
171     VERIFY_NON_NULL(ifitem, TAG, "ifitem is NULL");
172
173     oc_mutex_lock(g_networkMonitorContextMutex);
174     bool result = u_arraylist_add(g_netInterfaceList, (void *) ifitem);
175     if (!result)
176     {
177         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
178         oc_mutex_unlock(g_networkMonitorContextMutex);
179         return CA_STATUS_FAILED;
180     }
181     oc_mutex_unlock(g_networkMonitorContextMutex);
182     return CA_STATUS_OK;
183 }
184
185 static void CARemoveNetworkMonitorList(int ifiindex)
186 {
187     VERIFY_NON_NULL_VOID(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
188
189     oc_mutex_lock(g_networkMonitorContextMutex);
190
191     uint32_t list_length = u_arraylist_length(g_netInterfaceList);
192     for (uint32_t list_index = 0; list_index < list_length; list_index++)
193     {
194         CAInterface_t *removedifitem = (CAInterface_t *) u_arraylist_get(
195                 g_netInterfaceList, list_index);
196         if (removedifitem && ((int)removedifitem->index) == ifiindex)
197         {
198             if (u_arraylist_remove(g_netInterfaceList, list_index))
199             {
200                 OICFree(removedifitem);
201                 oc_mutex_unlock(g_networkMonitorContextMutex);
202                 return;
203             }
204             continue;
205         }
206     }
207     oc_mutex_unlock(g_networkMonitorContextMutex);
208     return;
209 }
210
211 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
212                                    CATransportAdapter_t adapter)
213 {
214     CAResult_t res = CAIPInitializeNetworkMonitorList();
215     if (CA_STATUS_OK == res)
216     {
217         return CAIPSetNetworkMonitorCallback(callback, adapter);
218     }
219     return res;
220 }
221
222 CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
223 {
224     CAIPDestroyNetworkMonitorList();
225     return CAIPUnSetNetworkMonitorCallback(adapter);
226 }
227
228 int CAGetPollingInterval(int interval)
229 {
230     return interval;
231 }
232
233 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status)
234 {
235     CAIPCBData_t *cbitem = NULL;
236     LL_FOREACH(g_adapterCallbackList, cbitem)
237     {
238         if (cbitem && cbitem->adapter)
239         {
240             cbitem->callback(cbitem->adapter, status);
241         }
242     }
243 }
244
245 CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback,
246                                          CATransportAdapter_t adapter)
247 {
248     if (!callback)
249     {
250         OIC_LOG(ERROR, TAG, "callback is null");
251         return CA_STATUS_INVALID_PARAM;
252     }
253
254     CAIPCBData_t *cbitem = NULL;
255     LL_FOREACH(g_adapterCallbackList, cbitem)
256     {
257         if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback)
258         {
259             OIC_LOG(DEBUG, TAG, "this callback is already added");
260             return CA_STATUS_OK;
261         }
262     }
263
264     cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem));
265     if (!cbitem)
266     {
267         OIC_LOG(ERROR, TAG, "Malloc failed");
268         return CA_STATUS_FAILED;
269     }
270
271     cbitem->adapter = adapter;
272     cbitem->callback = callback;
273     LL_APPEND(g_adapterCallbackList, cbitem);
274
275     return CA_STATUS_OK;
276 }
277
278 CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter)
279 {
280     CAIPCBData_t *cbitem = NULL;
281     CAIPCBData_t *tmpCbitem = NULL;
282     LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem)
283     {
284         if (cbitem && adapter == cbitem->adapter)
285         {
286             OIC_LOG(DEBUG, TAG, "remove specific callback");
287             LL_DELETE(g_adapterCallbackList, cbitem);
288             OICFree(cbitem);
289             return CA_STATUS_OK;
290         }
291     }
292     return CA_STATUS_OK;
293 }
294
295 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
296                                          const char *addr, int flags)
297 {
298     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
299     if (!ifitem)
300     {
301         OIC_LOG(ERROR, TAG, "Malloc failed");
302         return NULL;
303     }
304
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;
310
311     return ifitem;
312 }
313
314 u_arraylist_t *CAFindInterfaceChange()
315 {
316     u_arraylist_t *iflist = NULL;
317 #ifdef __linux__
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),
325                           .msg_iov = &iov,
326                           .msg_iovlen = 1 };
327
328     size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
329
330     for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
331     {
332         if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
333         {
334             continue;
335         }
336
337         struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
338         if (!ifi)
339         {
340             OIC_LOG_V(ERROR, TAG, "ifi is NULL");
341             return NULL;
342         }
343
344         int ifiIndex = ifi->ifi_index;
345
346         if ((ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING))
347         {
348             bool isFound = CACmpNetworkList(ifiIndex);
349             if (isFound)
350             {
351                 CARemoveNetworkMonitorList(ifiIndex);
352                 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
353             }
354             continue;
355         }
356
357         iflist = CAIPGetInterfaceInformation(ifiIndex);
358
359         if (!iflist)
360         {
361             OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
362             return NULL;
363         }
364     }
365 #endif
366     return iflist;
367 }
368
369 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
370 {
371     if (desiredIndex < 0)
372     {
373         OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
374         return NULL;
375     }
376
377     u_arraylist_t *iflist = u_arraylist_create();
378     if (!iflist)
379     {
380         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
381         return NULL;
382     }
383
384     struct ifaddrs *ifp = NULL;
385     if (-1 == getifaddrs(&ifp))
386     {
387         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
388         u_arraylist_destroy(iflist);
389         return NULL;
390     }
391
392     struct ifaddrs *ifa = NULL;
393     for (ifa = ifp; ifa; ifa = ifa->ifa_next)
394     {
395         if (!ifa->ifa_addr)
396         {
397             continue;
398         }
399         int family = ifa->ifa_addr->sa_family;
400         if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
401         {
402             continue;
403         }
404
405         int ifindex = if_nametoindex(ifa->ifa_name);
406         if (desiredIndex && (ifindex != desiredIndex))
407         {
408             continue;
409         }
410
411         int length = u_arraylist_length(iflist);
412         int already = false;
413         for (int i = length-1; i >= 0; i--)
414         {
415             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
416
417             if (ifitem
418                 && (int)ifitem->index == ifindex
419                 && ifitem->family == (uint16_t)family)
420             {
421                 already = true;
422                 break;
423             }
424         }
425         if (already)
426         {
427             continue;
428         }
429
430         CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
431         if (!ifitem)
432         {
433             OIC_LOG(ERROR, TAG, "Malloc failed");
434             goto exit;
435         }
436
437         OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
438         ifitem->index = ifindex;
439         ifitem->family = family;
440         ifitem->flags = ifa->ifa_flags;
441
442         if (ifitem->family == AF_INET6)
443         {
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));
447         }
448         else if (ifitem->family == AF_INET)
449         {
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));
453         }
454
455         bool result = u_arraylist_add(iflist, ifitem);
456         if (!result)
457         {
458             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
459             goto exit;
460         }
461
462         bool isFound = CACmpNetworkList(ifitem->index);
463         if (!isFound)
464         {
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)
469             {
470                 OICFree(newifitem);
471                 goto exit;
472             }
473             CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
474             OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
475         }
476     }
477     freeifaddrs(ifp);
478     return iflist;
479
480 exit:
481     freeifaddrs(ifp);
482     u_arraylist_destroy(iflist);
483     return NULL;
484 }