40a05524cb2b052fc794100598688c924044cf5a
[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     return CA_STATUS_OK;
93 }
94
95 static void CAIPDestroyNetworkMonitorList()
96 {
97     if (g_netInterfaceList)
98     {
99         u_arraylist_destroy(g_netInterfaceList);
100         g_netInterfaceList = NULL;
101     }
102
103     if (g_networkMonitorContextMutex)
104     {
105         ca_mutex_free(g_networkMonitorContextMutex);
106         g_networkMonitorContextMutex = NULL;
107     }
108 }
109
110 static bool CACmpNetworkList(uint32_t ifiindex)
111 {
112     if (!g_netInterfaceList)
113     {
114         OIC_LOG(ERROR, TAG, "g_netInterfaceList is NULL");
115         return false;
116     }
117
118     ca_mutex_lock(g_networkMonitorContextMutex);
119
120     uint32_t list_length = u_arraylist_length(g_netInterfaceList);
121     for (uint32_t list_index = 0; list_index < list_length; list_index++)
122     {
123         CAInterface_t *currItem = (CAInterface_t *) u_arraylist_get(g_netInterfaceList, list_index);
124         if (currItem->index == ifiindex)
125         {
126             ca_mutex_unlock(g_networkMonitorContextMutex);
127             return true;
128         }
129     }
130     ca_mutex_unlock(g_networkMonitorContextMutex);
131     return false;
132 }
133
134 static CAResult_t CAAddNetworkMonitorList(CAInterface_t *ifitem)
135 {
136     VERIFY_NON_NULL(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
137     VERIFY_NON_NULL(ifitem, TAG, "ifitem is NULL");
138
139     ca_mutex_lock(g_networkMonitorContextMutex);
140     bool result = u_arraylist_add(g_netInterfaceList, (void *) ifitem);
141     if (!result)
142     {
143         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
144         ca_mutex_unlock(g_networkMonitorContextMutex);
145         return CA_STATUS_FAILED;
146     }
147     ca_mutex_unlock(g_networkMonitorContextMutex);
148     return CA_STATUS_OK;
149 }
150
151 static void CARemoveNetworkMonitorList(int ifiindex)
152 {
153     VERIFY_NON_NULL_VOID(g_netInterfaceList, TAG, "g_netInterfaceList is NULL");
154
155     ca_mutex_lock(g_networkMonitorContextMutex);
156
157     uint32_t list_length = u_arraylist_length(g_netInterfaceList);
158     for (uint32_t list_index = 0; list_index < list_length; list_index++)
159     {
160         CAInterface_t *removedifitem = (CAInterface_t *) u_arraylist_get(
161                 g_netInterfaceList, list_index);
162         if (removedifitem && ((int)removedifitem->index) == ifiindex)
163         {
164             if (u_arraylist_remove(g_netInterfaceList, list_index))
165             {
166                 OICFree(removedifitem);
167                 ca_mutex_unlock(g_networkMonitorContextMutex);
168                 return;
169             }
170             continue;
171         }
172     }
173     ca_mutex_unlock(g_networkMonitorContextMutex);
174     return;
175 }
176
177 CAResult_t CAIPStartNetworkMonitor()
178 {
179     return CAIPInitializeNetworkMonitorList();
180 }
181
182 CAResult_t CAIPStopNetworkMonitor()
183 {
184     CAIPDestroyNetworkMonitorList();
185     return CA_STATUS_OK;
186 }
187
188 int CAGetPollingInterval(int interval)
189 {
190     return interval;
191 }
192
193 void CAIPSetNetworkMonitorCallback(CAIPConnectionStateChangeCallback callback)
194 {
195     g_networkChangeCallback = callback;
196 }
197
198 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
199                                          uint32_t addr, int flags)
200 {
201     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
202     if (!ifitem)
203     {
204         OIC_LOG(ERROR, TAG, "Malloc failed");
205         return NULL;
206     }
207
208     OICStrcpy(ifitem->name, sizeof (ifitem->name), name);
209     ifitem->index = index;
210     ifitem->family = family;
211     ifitem->ipv4addr = addr;
212     ifitem->flags = flags;
213
214     return ifitem;
215 }
216
217 CAInterface_t *CAFindInterfaceChange()
218 {
219     CAInterface_t *foundNewInterface = NULL;
220 #ifdef __linux__
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),
228                           .msg_iov = &iov,
229                           .msg_iovlen = 1 };
230
231     size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
232
233     for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
234     {
235         if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
236         {
237             continue;
238         }
239
240         struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
241
242         int ifiIndex = ifi->ifi_index;
243         u_arraylist_t *iflist = CAIPGetInterfaceInformation(ifiIndex);
244
245         if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING)))
246         {
247             bool isFound = CACmpNetworkList(ifiIndex);
248             if (isFound)
249             {
250                 CARemoveNetworkMonitorList(ifiIndex);
251                 if (g_networkChangeCallback)
252                 {
253                     g_networkChangeCallback(CA_ADAPTER_IP ,CA_INTERFACE_DOWN);
254                 }
255             }
256             continue;
257         }
258
259         if (!iflist)
260         {
261             OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
262             return NULL;
263         }
264
265         uint32_t listLength = u_arraylist_length(iflist);
266         for (uint32_t i = 0; i < listLength; i++)
267         {
268             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
269             if (!ifitem)
270             {
271                 continue;
272             }
273
274             if ((int)ifitem->index != ifiIndex)
275             {
276                 continue;
277             }
278
279             foundNewInterface = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
280                                                    ifitem->ipv4addr, ifitem->flags);
281             break;    // we found the one we were looking for
282         }
283         u_arraylist_destroy(iflist);
284     }
285 #endif
286     return foundNewInterface;
287 }
288
289 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
290 {
291     if (desiredIndex < 0)
292     {
293         OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
294         return NULL;
295     }
296
297     u_arraylist_t *iflist = u_arraylist_create();
298     if (!iflist)
299     {
300         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
301         return NULL;
302     }
303
304     struct ifaddrs *ifp = NULL;
305     if (-1 == getifaddrs(&ifp))
306     {
307         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
308         u_arraylist_destroy(iflist);
309         return NULL;
310     }
311     OIC_LOG(DEBUG, TAG, "Got ifaddrs");
312
313     struct ifaddrs *ifa = NULL;
314     for (ifa = ifp; ifa; ifa = ifa->ifa_next)
315     {
316         if (!ifa->ifa_addr)
317         {
318             continue;
319         }
320         int family = ifa->ifa_addr->sa_family;
321         if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
322         {
323             continue;
324         }
325
326         int ifindex = if_nametoindex(ifa->ifa_name);
327         if (desiredIndex && (ifindex != desiredIndex))
328         {
329             continue;
330         }
331
332         int length = u_arraylist_length(iflist);
333         int already = false;
334         for (int i = length-1; i >= 0; i--)
335         {
336             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
337
338             if (ifitem
339                 && (int)ifitem->index == ifindex
340                 && ifitem->family == (uint16_t)family)
341             {
342                 already = true;
343                 break;
344             }
345         }
346         if (already)
347         {
348             continue;
349         }
350
351         CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
352         if (!ifitem)
353         {
354             OIC_LOG(ERROR, TAG, "Malloc failed");
355             goto exit;
356         }
357
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;
363
364         bool result = u_arraylist_add(iflist, ifitem);
365         if (!result)
366         {
367             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
368             goto exit;
369         }
370
371         bool isFound = CACmpNetworkList(ifitem->index);
372         if (!isFound)
373         {
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)
378             {
379                 OICFree(newifitem);
380                 goto exit;
381             }
382             if (g_networkChangeCallback)
383             {
384                 g_networkChangeCallback(CA_ADAPTER_IP, CA_INTERFACE_UP);
385             }
386             OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
387         }
388     }
389     freeifaddrs(ifp);
390     return iflist;
391
392 exit:
393     freeifaddrs(ifp);
394     u_arraylist_destroy(iflist);
395     return NULL;
396 }