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 <sys/types.h>
24 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
35 #include <net_connection.h>
37 #include "caipinterface.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 "OIC_CA_IP_MONITOR"
47 #define NETLINK_MESSAGE_LENGTH (4096)
48 #define IFC_LABEL_LOOP "lo"
49 #define IFC_ADDR_LOOP_IPV4 "127.0.0.1"
50 #define IFC_ADDR_LOOP_IPV6 "::1"
53 * Used to storing a connection handle for managing data connections.
55 static connection_h connection = NULL;
58 * Used to storing adapter changes callback interface.
60 static struct CAIPCBData_t *g_adapterCallbackList = NULL;
63 * Create new interface item.
65 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
66 const char *addr, int flags);
69 * Add new network interface in list.
71 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
72 char *name, int family, const char *addr, int flags);
75 * Pass the changed network status through the stored callback.
77 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status);
80 * Callback function to received connection state changes.
82 static void CAIPConnectionStateChangedCb(connection_type_e type, void* userData);
84 int CAGetPollingInterval(int interval)
89 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status)
91 CAIPCBData_t *cbitem = NULL;
92 LL_FOREACH(g_adapterCallbackList, cbitem)
94 if (cbitem && cbitem->adapter)
96 cbitem->callback(cbitem->adapter, status);
101 CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback,
102 CATransportAdapter_t adapter)
106 OIC_LOG(ERROR, TAG, "callback is null");
107 return CA_STATUS_INVALID_PARAM;
110 CAIPCBData_t *cbitem = NULL;
111 LL_FOREACH(g_adapterCallbackList, cbitem)
113 if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback)
115 OIC_LOG(DEBUG, TAG, "this callback is already added");
120 cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem));
123 OIC_LOG(ERROR, TAG, "Malloc failed");
124 return CA_STATUS_FAILED;
127 cbitem->adapter = adapter;
128 cbitem->callback = callback;
129 LL_APPEND(g_adapterCallbackList, cbitem);
134 CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter)
136 CAIPCBData_t *cbitem = NULL;
137 CAIPCBData_t *tmpCbitem = NULL;
138 LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem)
140 if (cbitem && adapter == cbitem->adapter)
142 OIC_LOG(DEBUG, TAG, "remove specific callback");
143 LL_DELETE(g_adapterCallbackList, cbitem);
151 u_arraylist_t *CAFindInterfaceChange()
153 u_arraylist_t *iflist = NULL;
154 char buf[NETLINK_MESSAGE_LENGTH] = { 0 };
155 struct sockaddr_nl sa = { 0 };
156 struct iovec iov = { .iov_base = buf,
157 .iov_len = sizeof (buf) };
158 struct msghdr msg = { .msg_name = (void *)&sa,
159 .msg_namelen = sizeof (sa),
163 ssize_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
165 for (struct nlmsghdr *nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
167 if (nh != NULL && (nh->nlmsg_type != RTM_DELADDR && nh->nlmsg_type != RTM_NEWADDR))
171 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
177 int ifiIndex = ifi->ifi_index;
179 iflist = CAIPGetInterfaceInformation(ifiIndex);
182 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
189 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
190 CATransportAdapter_t adapter)
192 if (!g_adapterCallbackList)
194 // Initialize Wifi service.
195 if (WIFI_ERROR_NONE != wifi_initialize())
197 OIC_LOG(ERROR, TAG, "wifi_initialize failed");
200 // Initialize Connections.
201 connection_error_e ret = connection_create(&connection);
202 if (CONNECTION_ERROR_NONE != ret)
204 OIC_LOG(ERROR, TAG, "connection_create failed");
205 return CA_STATUS_FAILED;
208 // Set callback for receiving state changes.
209 ret = connection_set_type_changed_cb(connection, CAIPConnectionStateChangedCb, NULL);
210 if (CONNECTION_ERROR_NONE != ret)
212 OIC_LOG(ERROR, TAG, "connection_set_type_changed_cb failed");
213 return CA_STATUS_FAILED;
217 OIC_LOG(DEBUG, TAG, "Initialize network monitoring successfully");
218 return CAIPSetNetworkMonitorCallback(callback, adapter);
221 CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
223 OIC_LOG(DEBUG, TAG, "IN");
225 CAIPUnSetNetworkMonitorCallback(adapter);
226 if (!g_adapterCallbackList)
228 // Deinitialize Wifi service.
229 if (WIFI_ERROR_NONE != wifi_deinitialize())
231 OIC_LOG(ERROR, TAG, "wifi_deinitialize failed");
234 // Reset callback for receiving state changes.
237 connection_error_e ret = connection_unset_type_changed_cb(connection);
238 if (CONNECTION_ERROR_NONE != ret)
240 OIC_LOG(ERROR, TAG, "connection_unset_type_changed_cb failed");
243 // Deinitialize Wifi service.
244 ret = connection_destroy(connection);
245 if (CONNECTION_ERROR_NONE != ret)
247 OIC_LOG(ERROR, TAG, "connection_destroy failed");
253 OIC_LOG(DEBUG, TAG, "Network monitoring terminated successfully");
258 * Used to send netlink query to kernel and recv response from kernel.
260 * @param[in] idx desired network interface index, 0 means all interfaces.
261 * @param[out] iflist linked list.
264 static bool CAIPGetAddrInfo(int idx, u_arraylist_t *iflist)
266 if ((idx < 0) || (iflist == NULL))
271 struct ifaddrs *ifp = NULL;
272 if (-1 == getifaddrs(&ifp))
274 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
278 struct ifaddrs *ifa = NULL;
279 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
286 int family = ifa->ifa_addr->sa_family;
287 if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
292 int ifindex = if_nametoindex(ifa->ifa_name);
293 if (idx && (ifindex != idx))
298 char ipaddr[MAX_ADDR_STR_SIZE_CA] = {0};
299 if (family == AF_INET6)
301 struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
302 inet_ntop(family, (void *)&(in6->sin6_addr), ipaddr, sizeof(ipaddr));
304 else if (family == AF_INET)
306 struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
307 inet_ntop(family, (void *)&(in->sin_addr), ipaddr, sizeof(ipaddr));
310 if ((strcmp(ipaddr, IFC_ADDR_LOOP_IPV4) == 0) ||
311 (strcmp(ipaddr, IFC_ADDR_LOOP_IPV6) == 0) ||
312 (strcmp(ifa->ifa_name, IFC_LABEL_LOOP) == 0))
314 OIC_LOG(DEBUG, TAG, "LOOPBACK continue!!!");
318 CAResult_t result = CAAddInterfaceItem(iflist, ifindex,
319 ifa->ifa_name, family,
320 ipaddr, ifa->ifa_flags);
321 if (CA_STATUS_OK != result)
323 OIC_LOG(ERROR, TAG, "CAAddInterfaceItem fail");
335 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
337 u_arraylist_t *iflist = u_arraylist_create();
340 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
344 if (!CAIPGetAddrInfo(desiredIndex, iflist))
352 u_arraylist_destroy(iflist);
356 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
357 char *name, int family, const char *addr, int flags)
359 CAInterface_t *ifitem = CANewInterfaceItem(index, name, family, addr, flags);
362 return CA_STATUS_FAILED;
364 bool result = u_arraylist_add(iflist, ifitem);
367 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
369 return CA_STATUS_FAILED;
375 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
376 const char *addr, int flags)
378 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
381 OIC_LOG(ERROR, TAG, "Malloc failed");
385 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, name);
386 ifitem->index = index;
387 ifitem->family = family;
388 OICStrcpy(ifitem->addr, sizeof(ifitem->addr), addr);
389 ifitem->flags = flags;
394 void CAIPConnectionStateChangedCb(connection_type_e type, void* userData)
398 case CONNECTION_TYPE_DISCONNECTED:
399 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_DISCONNECTED");
400 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
402 case CONNECTION_TYPE_ETHERNET:
403 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_ETHERNET");
404 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
406 case CONNECTION_TYPE_WIFI:
407 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_WIFI");
408 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
410 case CONNECTION_TYPE_CELLULAR:
411 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_CELLULAR");
412 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);