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>
34 #include <net_connection.h>
36 #include "caipinterface.h"
37 #include "caipnwmonitor.h"
38 #include "caipnwmonitor_common.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"
46 #define MAX_INTERFACE_INFO_LENGTH (1024)
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 static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status)
86 CAIPPassNetworkChangesToAdapterInternal(status, g_adapterCallbackList, CA_IP_NW_COMMON_TIZEN);
89 CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback,
90 CATransportAdapter_t adapter)
92 return CAIPSetNetworkMonitorCallbackInternal(callback, adapter, g_adapterCallbackList);
95 CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter)
97 return CAIPUnSetNetworkMonitorCallbackInternal(adapter, g_adapterCallbackList);
100 u_arraylist_t *CAFindInterfaceChange()
102 u_arraylist_t *iflist = NULL;
103 char buf[NETLINK_MESSAGE_LENGTH] = { 0 };
104 struct sockaddr_nl sa = { 0 };
105 struct iovec iov = { .iov_base = buf,
106 .iov_len = sizeof (buf) };
107 struct msghdr msg = { .msg_name = (void *)&sa,
108 .msg_namelen = sizeof (sa),
112 ssize_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
114 for (struct nlmsghdr *nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
116 if (nh != NULL && (nh->nlmsg_type != RTM_DELADDR && nh->nlmsg_type != RTM_NEWADDR))
121 struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
127 if (RTM_DELADDR == nh->nlmsg_type)
129 CloseMulticastSocket();
132 int ifiIndex = ifi->ifi_index;
133 iflist = CAIPGetInterfaceInformation(ifiIndex);
136 OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
139 CreateMulticastSocket();
144 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
145 CATransportAdapter_t adapter)
147 if (!g_adapterCallbackList)
149 // Initialize Connections.
150 connection_error_e ret = connection_create(&connection);
151 if (CONNECTION_ERROR_NONE != ret)
153 OIC_LOG(ERROR, TAG, "connection_create failed");
154 return CA_STATUS_FAILED;
157 // Set callback for receiving state changes.
158 ret = connection_set_type_changed_cb(connection, CAIPConnectionStateChangedCb, NULL);
159 if (CONNECTION_ERROR_NONE != ret)
161 OIC_LOG(ERROR, TAG, "connection_set_type_changed_cb failed");
162 return CA_STATUS_FAILED;
166 OIC_LOG(DEBUG, TAG, "Initialize network monitoring successfully");
167 return CAIPSetNetworkMonitorCallback(callback, adapter);
170 CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
172 OIC_LOG(DEBUG, TAG, "IN");
174 CAIPUnSetNetworkMonitorCallback(adapter);
175 if (!g_adapterCallbackList)
177 // Reset callback for receiving state changes.
180 connection_error_e ret = connection_unset_type_changed_cb(connection);
181 if (CONNECTION_ERROR_NONE != ret)
183 OIC_LOG(ERROR, TAG, "connection_unset_type_changed_cb failed");
186 // Deinitialize Wifi service.
187 ret = connection_destroy(connection);
188 if (CONNECTION_ERROR_NONE != ret)
190 OIC_LOG(ERROR, TAG, "connection_destroy failed");
196 OIC_LOG(DEBUG, TAG, "Network monitoring terminated successfully");
201 * Used to send netlink query to kernel and recv response from kernel.
203 * @param[in] idx desired network interface index, 0 means all interfaces.
204 * @param[out] iflist linked list.
207 static bool CAIPGetAddrInfo(int idx, u_arraylist_t *iflist)
209 if ((idx < 0) || (iflist == NULL))
214 struct ifaddrs *ifp = NULL;
215 if (-1 == getifaddrs(&ifp))
217 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
221 struct ifaddrs *ifa = NULL;
222 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
229 int family = ifa->ifa_addr->sa_family;
230 if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
235 int ifindex = if_nametoindex(ifa->ifa_name);
236 if (idx && (ifindex != idx))
241 char ipaddr[MAX_ADDR_STR_SIZE_CA] = {0};
242 if (family == AF_INET6)
244 struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
245 inet_ntop(family, (void *)&(in6->sin6_addr), ipaddr, sizeof(ipaddr));
247 else if (family == AF_INET)
249 struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
250 inet_ntop(family, (void *)&(in->sin_addr), ipaddr, sizeof(ipaddr));
253 if ((strcmp(ipaddr, IFC_ADDR_LOOP_IPV4) == 0) ||
254 (strcmp(ipaddr, IFC_ADDR_LOOP_IPV6) == 0) ||
255 (strcmp(ifa->ifa_name, IFC_LABEL_LOOP) == 0))
257 OIC_LOG(DEBUG, TAG, "LOOPBACK continue!!!");
261 CAResult_t result = CAAddInterfaceItem(iflist, ifindex,
262 ifa->ifa_name, family,
263 ipaddr, ifa->ifa_flags);
264 if (CA_STATUS_OK != result)
266 OIC_LOG(ERROR, TAG, "CAAddInterfaceItem fail");
278 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
280 u_arraylist_t *iflist = u_arraylist_create();
283 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
287 if (!CAIPGetAddrInfo(desiredIndex, iflist))
295 u_arraylist_destroy(iflist);
299 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
300 char *name, int family, const char *addr, int flags)
302 CAInterface_t *ifitem = CANewInterfaceItem(index, name, family, addr, flags);
305 return CA_STATUS_FAILED;
307 bool result = u_arraylist_add(iflist, ifitem);
310 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
312 return CA_STATUS_FAILED;
318 static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
319 const char *addr, int flags)
321 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
324 OIC_LOG(ERROR, TAG, "Malloc failed");
328 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, name);
329 ifitem->index = index;
330 ifitem->family = family;
331 OICStrcpy(ifitem->addr, sizeof(ifitem->addr), addr);
332 ifitem->flags = flags;
337 void CAIPConnectionStateChangedCb(connection_type_e type, void* userData)
343 case CONNECTION_TYPE_DISCONNECTED:
344 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_DISCONNECTED");
345 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
347 case CONNECTION_TYPE_ETHERNET:
348 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_ETHERNET");
349 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
351 case CONNECTION_TYPE_WIFI:
352 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_WIFI");
353 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
355 case CONNECTION_TYPE_CELLULAR:
356 OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_CELLULAR");
357 CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);