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"
23 #include <sys/types.h>
26 #include <sys/socket.h>
31 #include <sys/ioctl.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
38 #include "caipnwmonitor.h"
39 #include "caadapterutils.h"
41 #include "oic_malloc.h"
42 #include "oic_string.h"
43 #include <coap/utlist.h>
45 #define TAG "IP_MONITOR"
47 #define MAX_INTERFACE_INFO_LENGTH (1024)
49 #define NETLINK_MESSAGE_LENGTH (4096)
52 * Used to storing adapter changes callback interface.
54 static struct CAIPCBData_t *g_adapterCallbackList = NULL;
57 * Create new interface item.
59 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
60 const char *addr, int flags);
63 * Add new network interface in list.
65 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
66 char *name, int family, const char *addr, int flags);
69 * Pass the changed network status through the stored callback.
71 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status);
74 * Callback function to received connection state changes.
76 static void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
80 * Callback function to received device state changes.
82 static void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData);
85 int CAGetPollingInterval(int interval)
90 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status)
92 CAIPCBData_t *cbitem = NULL;
93 LL_FOREACH(g_adapterCallbackList, cbitem)
95 if (cbitem && cbitem->adapter)
97 cbitem->callback(cbitem->adapter, status);
102 CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback,
103 CATransportAdapter_t adapter)
107 OIC_LOG(ERROR, TAG, "callback is null");
108 return CA_STATUS_INVALID_PARAM;
111 CAIPCBData_t *cbitem = NULL;
112 LL_FOREACH(g_adapterCallbackList, cbitem)
114 if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback)
116 OIC_LOG(DEBUG, TAG, "this callback is already added");
121 cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem));
124 OIC_LOG(ERROR, TAG, "Malloc failed");
125 return CA_STATUS_FAILED;
128 cbitem->adapter = adapter;
129 cbitem->callback = callback;
130 LL_APPEND(g_adapterCallbackList, cbitem);
135 CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter)
137 CAIPCBData_t *cbitem = NULL;
138 CAIPCBData_t *tmpCbitem = NULL;
139 LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem)
141 if (cbitem && adapter == cbitem->adapter)
143 OIC_LOG(DEBUG, TAG, "remove specific callback");
144 LL_DELETE(g_adapterCallbackList, cbitem);
152 CAInterface_t *CAFindInterfaceChange()
154 CAInterface_t *foundNewInterface = NULL;
155 char buf[NETLINK_MESSAGE_LENGTH] = { 0 };
156 struct sockaddr_nl sa = { 0 };
157 struct iovec iov = { .iov_base = buf,
158 .iov_len = sizeof (buf) };
159 struct msghdr msg = { .msg_name = (void *)&sa,
160 .msg_namelen = sizeof (sa),
164 size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
166 for (struct nlmsghdr *nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
168 if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
173 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
175 int ifiIndex = ifi->ifi_index;
176 u_arraylist_t *iflist = CAIPGetInterfaceInformation(ifiIndex);
178 if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING)))
185 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
189 uint32_t listLength = u_arraylist_length(iflist);
190 for (uint32_t i = 0; i < listLength; i++)
192 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
198 if ((int)ifitem->index != ifiIndex)
203 foundNewInterface = CANewInterfaceItem(ifitem->index, ifitem->name, ifitem->family,
204 ifitem->addr, ifitem->flags);
205 break; // we found the one we were looking for
207 u_arraylist_destroy(iflist);
209 return foundNewInterface;
212 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
213 CATransportAdapter_t adapter)
215 OIC_LOG(DEBUG, TAG, "IN");
217 if (!g_adapterCallbackList)
219 // Initialize Wifi service
220 wifi_error_e ret = wifi_initialize();
221 if (WIFI_ERROR_NONE != ret)
223 OIC_LOG(ERROR, TAG, "wifi_initialize failed");
224 return CA_STATUS_FAILED;
227 // Set callback for receiving state changes
228 ret = wifi_set_device_state_changed_cb(CAWIFIDeviceStateChangedCb, NULL);
229 if (WIFI_ERROR_NONE != ret)
231 OIC_LOG(ERROR, TAG, "wifi_set_device_state_changed_cb failed");
232 return CA_STATUS_FAILED;
235 // Set callback for receiving connection state changes
236 ret = wifi_set_connection_state_changed_cb(CAWIFIConnectionStateChangedCb, NULL);
237 if (WIFI_ERROR_NONE != ret)
239 OIC_LOG(ERROR, TAG, "wifi_set_connection_state_changed_cb failed");
240 return CA_STATUS_FAILED;
244 return CAIPSetNetworkMonitorCallback(callback, adapter);
247 CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
249 OIC_LOG(DEBUG, TAG, "IN");
251 CAIPUnSetNetworkMonitorCallback(adapter);
252 if (!g_adapterCallbackList)
254 // Reset callback for receiving state changes
255 wifi_error_e ret = wifi_unset_device_state_changed_cb();
256 if (WIFI_ERROR_NONE != ret)
258 OIC_LOG(ERROR, TAG, "wifi_unset_device_state_changed_cb failed");
261 // Reset callback for receiving connection state changes
262 ret = wifi_unset_connection_state_changed_cb();
263 if (WIFI_ERROR_NONE != ret)
265 OIC_LOG(ERROR, TAG, "wifi_unset_connection_state_changed_cb failed");
268 // Deinitialize Wifi service
269 ret = wifi_deinitialize();
270 if (WIFI_ERROR_NONE != ret)
272 OIC_LOG(ERROR, TAG, "wifi_deinitialize failed");
279 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
281 u_arraylist_t *iflist = u_arraylist_create();
284 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
288 char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 };
289 struct ifconf ifc = { .ifc_len = MAX_INTERFACE_INFO_LENGTH, .ifc_buf = buf };
291 int s = caglobals.ip.u6.fd != -1 ? caglobals.ip.u6.fd : caglobals.ip.u4.fd;
292 if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
294 OIC_LOG_V(ERROR, TAG, "SIOCGIFCONF failed: %s", strerror(errno));
295 u_arraylist_destroy(iflist);
299 struct ifreq* ifr = ifc.ifc_req;
300 size_t interfaces = ifc.ifc_len / sizeof (ifc.ifc_req[0]);
301 size_t ifreqsize = ifc.ifc_len;
303 if (ifreqsize > caglobals.ip.nm.sizeIfItems)
305 CAIfItem_t *items = (CAIfItem_t *)OICRealloc(caglobals.ip.nm.ifItems, ifreqsize);
308 OIC_LOG(ERROR, TAG, "OICRealloc failed");
311 caglobals.ip.nm.ifItems = items;
312 caglobals.ip.nm.sizeIfItems = ifreqsize;
315 caglobals.ip.nm.numIfItems = 0;
316 for (size_t i = 0; i < interfaces; i++)
318 struct ifreq* item = &ifr[i];
319 char *name = item->ifr_name;
321 if (ioctl(s, SIOCGIFFLAGS, item) < 0)
323 OIC_LOG_V(ERROR, TAG, "SIOCGIFFLAGS failed: %s", strerror(errno));
326 int16_t flags = item->ifr_flags;
327 if ((flags & IFF_LOOPBACK) || !(flags & IFF_RUNNING))
331 if (ioctl(s, SIOCGIFINDEX, item) < 0)
333 OIC_LOG_V(ERROR, TAG, "SIOCGIFINDEX failed: %s", strerror(errno));
337 int ifindex = item->ifr_ifindex;
338 caglobals.ip.nm.ifItems[i].ifIndex = ifindex;
339 caglobals.ip.nm.numIfItems++;
341 if (desiredIndex && (ifindex != desiredIndex))
346 // Get address of network interface.
347 char addr[MAX_ADDR_STR_SIZE_CA] = { 0 };
348 struct sockaddr_in *sa = (struct sockaddr_in *)&item->ifr_addr;
349 inet_ntop(AF_INET, (void *)&(sa->sin_addr), addr, sizeof(addr));
351 // Add IPv4 interface
352 CAResult_t result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET, addr, flags);
353 if (CA_STATUS_OK != result)
358 // Add IPv6 interface
359 result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET6, addr, flags);
360 if (CA_STATUS_OK != result)
368 u_arraylist_destroy(iflist);
372 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
373 char *name, int family, const char *addr, int flags)
375 CAInterface_t *ifitem = CANewInterfaceItem(index, name, family, addr, flags);
378 return CA_STATUS_FAILED;
380 bool result = u_arraylist_add(iflist, ifitem);
383 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
385 return CA_STATUS_FAILED;
391 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
392 const char *addr, int flags)
394 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
397 OIC_LOG(ERROR, TAG, "Malloc failed");
401 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, name);
402 ifitem->index = index;
403 ifitem->family = family;
404 OICStrcpy(ifitem->addr, sizeof(ifitem->addr), addr);
405 ifitem->flags = flags;
410 void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
413 OIC_LOG(DEBUG, TAG, "IN");
415 if (WIFI_CONNECTION_STATE_ASSOCIATION == state
416 || WIFI_CONNECTION_STATE_CONFIGURATION == state)
418 OIC_LOG(DEBUG, TAG, "Connection is in Association State");
422 if (WIFI_CONNECTION_STATE_CONNECTED == state)
424 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
428 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
431 OIC_LOG(DEBUG, TAG, "OUT");
434 void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData)
436 OIC_LOG(DEBUG, TAG, "IN");
438 if (WIFI_DEVICE_STATE_ACTIVATED == state)
440 OIC_LOG(DEBUG, TAG, "Wifi is in Activated State");
444 CAWIFIConnectionStateChangedCb(WIFI_CONNECTION_STATE_DISCONNECTED, NULL, NULL);
445 OIC_LOG(DEBUG, TAG, "Wifi is in Deactivated State");
448 OIC_LOG(DEBUG, TAG, "OUT");