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_NEWLINK)
171 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
173 if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING)))
178 int ifiIndex = ifi->ifi_index;
180 iflist = CAIPGetInterfaceInformation(ifiIndex);
184 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
191 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
192 CATransportAdapter_t adapter)
194 if (!g_adapterCallbackList)
196 // Initialize Wifi service.
197 if (WIFI_ERROR_NONE != wifi_initialize())
199 OIC_LOG(ERROR, TAG, "wifi_initialize failed");
202 // Initialize Connections.
203 connection_error_e ret = connection_create(&connection);
204 if (CONNECTION_ERROR_NONE != ret)
206 OIC_LOG(ERROR, TAG, "connection_create failed");
207 return CA_STATUS_FAILED;
210 // Set callback for receiving state changes.
211 ret = connection_set_type_changed_cb(connection, CAIPConnectionStateChangedCb, NULL);
212 if (CONNECTION_ERROR_NONE != ret)
214 OIC_LOG(ERROR, TAG, "connection_set_type_changed_cb failed");
215 return CA_STATUS_FAILED;
219 OIC_LOG(DEBUG, TAG, "Initialize network monitoring successfully");
220 return CAIPSetNetworkMonitorCallback(callback, adapter);
223 CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
225 OIC_LOG(DEBUG, TAG, "IN");
227 CAIPUnSetNetworkMonitorCallback(adapter);
228 if (!g_adapterCallbackList)
230 // Deinitialize Wifi service.
231 if (WIFI_ERROR_NONE != wifi_deinitialize())
233 OIC_LOG(ERROR, TAG, "wifi_deinitialize failed");
236 // Reset callback for receiving state changes.
239 connection_error_e ret = connection_unset_type_changed_cb(connection);
240 if (CONNECTION_ERROR_NONE != ret)
242 OIC_LOG(ERROR, TAG, "connection_unset_type_changed_cb failed");
245 // Deinitialize Wifi service.
246 ret = connection_destroy(connection);
247 if (CONNECTION_ERROR_NONE != ret)
249 OIC_LOG(ERROR, TAG, "connection_destroy failed");
255 OIC_LOG(DEBUG, TAG, "Network monitoring terminated successfully");
260 * Used to send netlink query to kernel and recv response from kernel.
262 * @param[in] idx desired network interface index, 0 means all interfaces.
263 * @param[out] iflist linked list.
266 static bool CAIPGetAddrInfo(int idx, u_arraylist_t *iflist)
268 if ((idx < 0) || (iflist == NULL))
273 struct ifaddrs *ifp = NULL;
274 if (-1 == getifaddrs(&ifp))
276 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
280 struct ifaddrs *ifa = NULL;
281 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
288 int family = ifa->ifa_addr->sa_family;
289 if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
294 int ifindex = if_nametoindex(ifa->ifa_name);
295 if (idx && (ifindex != idx))
300 char ipaddr[MAX_ADDR_STR_SIZE_CA] = {0};
301 if (family == AF_INET6)
303 struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
304 inet_ntop(family, (void *)&(in6->sin6_addr), ipaddr, sizeof(ipaddr));
306 else if (family == AF_INET)
308 struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
309 inet_ntop(family, (void *)&(in->sin_addr), ipaddr, sizeof(ipaddr));
312 if ((strcmp(ipaddr, IFC_ADDR_LOOP_IPV4) == 0) ||
313 (strcmp(ipaddr, IFC_ADDR_LOOP_IPV6) == 0) ||
314 (strcmp(ifa->ifa_name, IFC_LABEL_LOOP) == 0))
316 OIC_LOG(DEBUG, TAG, "LOOPBACK continue!!!");
320 CAResult_t result = CAAddInterfaceItem(iflist, ifindex,
321 ifa->ifa_name, family,
322 ipaddr, ifa->ifa_flags);
323 if (CA_STATUS_OK != result)
325 OIC_LOG(ERROR, TAG, "CAAddInterfaceItem fail");
337 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
339 u_arraylist_t *iflist = u_arraylist_create();
342 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
346 if (!CAIPGetAddrInfo(desiredIndex, iflist))
354 u_arraylist_destroy(iflist);
358 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
359 char *name, int family, const char *addr, int flags)
361 CAInterface_t *ifitem = CANewInterfaceItem(index, name, family, addr, flags);
364 return CA_STATUS_FAILED;
366 bool result = u_arraylist_add(iflist, ifitem);
369 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
371 return CA_STATUS_FAILED;
377 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
378 const char *addr, int flags)
380 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
383 OIC_LOG(ERROR, TAG, "Malloc failed");
387 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, name);
388 ifitem->index = index;
389 ifitem->family = family;
390 OICStrcpy(ifitem->addr, sizeof(ifitem->addr), addr);
391 ifitem->flags = flags;
396 void CAIPConnectionStateChangedCb(connection_type_e type, void* userData)
400 case CONNECTION_TYPE_DISCONNECTED:
401 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_DISCONNECTED");
402 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
404 case CONNECTION_TYPE_ETHERNET:
405 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_ETHERNET");
406 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
408 case CONNECTION_TYPE_WIFI:
409 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_WIFI");
410 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
412 case CONNECTION_TYPE_CELLULAR:
413 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_CELLULAR");
414 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);