Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / tizen / 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 <sys/types.h>
24 #include <ifaddrs.h>
25 #include <net/if.h>
26 #include <sys/socket.h>
27 #include <netdb.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/ioctl.h>
32 #include <wifi.h>
33
34 #include "caadapterutils.h"
35 #include "logger.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
38
39 #define TAG "IP_MONITOR"
40 #define MAX_INTERFACE_INFO_LENGTH (1024)
41
42 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
43                                          uint32_t addr, int flags);
44
45 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
46                                      char *name, int family, uint32_t addr, int flags);
47
48 static void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
49                                            void *userData);
50
51 static void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData);
52
53
54 int CAGetPollingInterval(int interval)
55 {
56     return interval;
57 }
58
59 CAInterface_t *CAFindInterfaceChange()
60 {
61     char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 };
62     struct ifconf ifc  = { .ifc_len = MAX_INTERFACE_INFO_LENGTH, .ifc_buf = buf };
63
64     int s = caglobals.ip.u6.fd != -1 ? caglobals.ip.u6.fd : caglobals.ip.u4.fd;
65     if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
66     {
67         OIC_LOG_V(ERROR, TAG, "SIOCGIFCONF failed: %s", strerror(errno));
68         return NULL;
69     }
70
71     CAInterface_t *foundNewInterface = NULL;
72
73     struct ifreq* ifr = ifc.ifc_req;
74     size_t interfaces = ifc.ifc_len / sizeof (ifc.ifc_req[0]);
75     size_t ifreqsize = ifc.ifc_len;
76
77     CAIfItem_t *previous = (CAIfItem_t *)OICMalloc(ifreqsize);
78     if (!previous)
79     {
80         OIC_LOG(ERROR, TAG, "OICMalloc failed");
81         return NULL;
82     }
83
84     memcpy(previous, caglobals.ip.nm.ifItems, ifreqsize);
85     size_t numprevious = caglobals.ip.nm.numIfItems;
86
87     if (ifreqsize > caglobals.ip.nm.sizeIfItems)
88     {
89
90         CAIfItem_t *items = (CAIfItem_t *)OICRealloc(caglobals.ip.nm.ifItems, ifreqsize);
91         if (!items)
92         {
93             OIC_LOG(ERROR, TAG, "OICRealloc failed");
94             OICFree(previous);
95             return NULL;
96         }
97         caglobals.ip.nm.ifItems = items;
98         caglobals.ip.nm.sizeIfItems = ifreqsize;
99     }
100
101     caglobals.ip.nm.numIfItems = 0;
102     for (size_t i = 0; i < interfaces; i++)
103     {
104         struct ifreq* item = &ifr[i];
105         char *name = item->ifr_name;
106         struct sockaddr_in *sa4 = (struct sockaddr_in *)&item->ifr_addr;
107         uint32_t ipv4addr = sa4->sin_addr.s_addr;
108
109         if (ioctl(s, SIOCGIFFLAGS, item) < 0)
110         {
111             OIC_LOG_V(ERROR, TAG, "SIOCGIFFLAGS failed: %s", strerror(errno));
112             continue;
113         }
114         int16_t flags = item->ifr_flags;
115         if ((flags & IFF_LOOPBACK) || !(flags & IFF_RUNNING))
116         {
117             continue;
118         }
119         if (ioctl(s, SIOCGIFINDEX, item) < 0)
120         {
121             OIC_LOG_V(ERROR, TAG, "SIOCGIFINDEX failed: %s", strerror(errno));
122             continue;
123         }
124
125         int ifIndex = item->ifr_ifindex;
126         caglobals.ip.nm.ifItems[i].ifIndex = ifIndex;  // refill interface list
127         caglobals.ip.nm.numIfItems++;
128
129         if (foundNewInterface)
130         {
131             continue;   // continue updating interface list
132         }
133
134         // see if this interface didn't previously exist
135         bool found = false;
136         for (size_t j = 0; j < numprevious; j++)
137         {
138             if (ifIndex == previous[j].ifIndex)
139             {
140                 found = true;
141                 break;
142             }
143         }
144         if (found)
145         {
146             OIC_LOG_V(INFO, TAG, "Interface found: %s", name);
147             continue;
148         }
149
150         foundNewInterface = CANewInterfaceItem(ifIndex, name, AF_INET, ipv4addr, flags);
151     }
152
153     OICFree(previous);
154     return foundNewInterface;
155 }
156
157 CAResult_t CAIPStartNetworkMonitor()
158 {
159     OIC_LOG(DEBUG, TAG, "IN");
160
161      // Initialize Wifi service
162     wifi_error_e ret = wifi_initialize();
163     if (WIFI_ERROR_NONE != ret)
164     {
165         OIC_LOG(ERROR, TAG, "wifi_initialize failed");
166         return CA_STATUS_FAILED;
167     }
168
169     // Set callback for receiving state changes
170     ret = wifi_set_device_state_changed_cb(CAWIFIDeviceStateChangedCb, NULL);
171     if (WIFI_ERROR_NONE != ret)
172     {
173         OIC_LOG(ERROR, TAG, "wifi_set_device_state_changed_cb failed");
174         return CA_STATUS_FAILED;
175     }
176
177     // Set callback for receiving connection state changes
178     ret = wifi_set_connection_state_changed_cb(CAWIFIConnectionStateChangedCb, NULL);
179     if (WIFI_ERROR_NONE != ret)
180     {
181         OIC_LOG(ERROR, TAG, "wifi_set_connection_state_changed_cb failed");
182         return CA_STATUS_FAILED;
183     }
184
185     OIC_LOG(DEBUG, TAG, "OUT");
186     return CA_STATUS_OK;
187 }
188
189 CAResult_t CAIPStopNetworkMonitor()
190 {
191     OIC_LOG(DEBUG, TAG, "IN");
192
193      // Reset callback for receiving state changes
194     wifi_error_e ret = wifi_unset_device_state_changed_cb();
195     if (WIFI_ERROR_NONE != ret)
196     {
197         OIC_LOG(ERROR, TAG, "wifi_unset_device_state_changed_cb failed");
198     }
199
200     // Reset callback for receiving connection state changes
201     ret = wifi_unset_connection_state_changed_cb();
202     if (WIFI_ERROR_NONE != ret)
203     {
204         OIC_LOG(ERROR, TAG, "wifi_unset_connection_state_changed_cb failed");
205     }
206
207     // Deinitialize Wifi service
208     ret = wifi_deinitialize();
209     if (WIFI_ERROR_NONE != ret)
210     {
211         OIC_LOG(ERROR, TAG, "wifi_deinitialize failed");
212     }
213
214     OIC_LOG(DEBUG, TAG, "OUT");
215     return CA_STATUS_OK;
216 }
217
218 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
219 {
220     u_arraylist_t *iflist = u_arraylist_create();
221     if (!iflist)
222     {
223         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
224         return NULL;
225     }
226
227     char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 };
228     struct ifconf ifc = { .ifc_len = MAX_INTERFACE_INFO_LENGTH, .ifc_buf = buf };
229
230     int s = caglobals.ip.u6.fd != -1 ? caglobals.ip.u6.fd : caglobals.ip.u4.fd;
231     if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
232     {
233         OIC_LOG_V(ERROR, TAG, "SIOCGIFCONF failed: %s", strerror(errno));
234         u_arraylist_destroy(iflist);
235         return NULL;
236     }
237
238     struct ifreq* ifr = ifc.ifc_req;
239     size_t interfaces = ifc.ifc_len / sizeof (ifc.ifc_req[0]);
240     size_t ifreqsize = ifc.ifc_len;
241
242     if (ifreqsize > caglobals.ip.nm.sizeIfItems)
243     {
244         CAIfItem_t *items = (CAIfItem_t *)OICRealloc(caglobals.ip.nm.ifItems, ifreqsize);
245         if (!items)
246         {
247             OIC_LOG(ERROR, TAG, "OICRealloc failed");
248             goto exit;
249         }
250         caglobals.ip.nm.ifItems = items;
251         caglobals.ip.nm.sizeIfItems = ifreqsize;
252     }
253
254     caglobals.ip.nm.numIfItems = 0;
255     for (size_t i = 0; i < interfaces; i++)
256     {
257         struct ifreq* item = &ifr[i];
258         char *name = item->ifr_name;
259         struct sockaddr_in *sa4 = (struct sockaddr_in *)&item->ifr_addr;
260         uint32_t ipv4addr = sa4->sin_addr.s_addr;
261
262         if (ioctl(s, SIOCGIFFLAGS, item) < 0)
263         {
264             OIC_LOG_V(ERROR, TAG, "SIOCGIFFLAGS failed: %s", strerror(errno));
265             continue;
266         }
267         int16_t flags = item->ifr_flags;
268         if ((flags & IFF_LOOPBACK) || !(flags & IFF_RUNNING))
269         {
270             continue;
271         }
272         if (ioctl(s, SIOCGIFINDEX, item) < 0)
273         {
274             OIC_LOG_V(ERROR, TAG, "SIOCGIFINDEX failed: %s", strerror(errno));
275             continue;
276         }
277
278         int ifindex = item->ifr_ifindex;
279         caglobals.ip.nm.ifItems[i].ifIndex = ifindex;
280         caglobals.ip.nm.numIfItems++;
281
282         if (desiredIndex && (ifindex != desiredIndex))
283         {
284             continue;
285         }
286
287         // Add IPv4 interface
288         CAResult_t result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET, ipv4addr, flags);
289         if (CA_STATUS_OK != result)
290         {
291             goto exit;
292         }
293
294         // Add IPv6 interface
295         result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET6, ipv4addr, flags);
296         if (CA_STATUS_OK != result)
297         {
298             goto exit;
299         }
300     }
301     return iflist;
302
303 exit:
304     u_arraylist_destroy(iflist);
305     return NULL;
306 }
307
308 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
309                                      char *name, int family, uint32_t addr, int flags)
310 {
311     CAInterface_t *ifitem = CANewInterfaceItem(index, name, family, addr, flags);
312     if (!ifitem)
313     {
314         return CA_STATUS_FAILED;
315     }
316     bool result = u_arraylist_add(iflist, ifitem);
317     if (!result)
318     {
319         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
320         OICFree(ifitem);
321         return CA_STATUS_FAILED;
322     }
323
324     return CA_STATUS_OK;
325 }
326
327 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
328                                          uint32_t addr, int flags)
329 {
330     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
331     if (!ifitem)
332     {
333         OIC_LOG(ERROR, TAG, "Malloc failed");
334         return NULL;
335     }
336
337     OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, name);
338     ifitem->index = index;
339     ifitem->family = family;
340     ifitem->ipv4addr = addr;
341     ifitem->flags = flags;
342
343     return ifitem;
344 }
345
346 void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
347                                     void *userData)
348 {
349     OIC_LOG(DEBUG, TAG, "IN");
350
351     if (WIFI_CONNECTION_STATE_ASSOCIATION == state
352         || WIFI_CONNECTION_STATE_CONFIGURATION == state)
353     {
354         OIC_LOG(DEBUG, TAG, "Connection is in Association State");
355         return;
356     }
357
358     if (WIFI_CONNECTION_STATE_CONNECTED == state)
359     {
360         CAWakeUpForChange();
361     }
362     else
363     {
364         u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
365         if (!iflist)
366         {
367             OIC_LOG_V(ERROR, TAG, "get interface info failed");
368             return;
369         }
370         u_arraylist_destroy(iflist);
371     }
372
373     OIC_LOG(DEBUG, TAG, "OUT");
374 }
375
376 void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData)
377 {
378     OIC_LOG(DEBUG, TAG, "IN");
379
380     if (WIFI_DEVICE_STATE_ACTIVATED == state)
381     {
382         OIC_LOG(DEBUG, TAG, "Wifi is in Activated State");
383     }
384     else
385     {
386         CAWIFIConnectionStateChangedCb(WIFI_CONNECTION_STATE_DISCONNECTED, NULL, NULL);
387         OIC_LOG(DEBUG, TAG, "Wifi is in Deactivated State");
388     }
389
390     OIC_LOG(DEBUG, TAG, "OUT");
391 }