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>
32 #include "caadapterutils.h"
34 #include "oic_malloc.h"
35 #include "oic_string.h"
37 #define TAG "IP_MONITOR"
39 CAResult_t CAIPInitializeNetworkMonitor()
44 CAResult_t CAIPTerminateNetworkMonitor()
49 int CAGetPollingInterval(int interval)
54 CAInterface_t *CAFindInterfaceChange()
59 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
61 u_arraylist_t *iflist = u_arraylist_create();
64 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
68 struct ifaddrs *ifp = NULL;
69 if (-1 == getifaddrs(&ifp))
71 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
72 u_arraylist_destroy(iflist);
76 struct ifaddrs *ifa = NULL;
77 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
79 int family = ifa->ifa_addr->sa_family;
80 if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
85 int ifindex = if_nametoindex(ifa->ifa_name);
86 int length = u_arraylist_length(iflist);
88 for (int i = length-1; i >= 0; i--)
90 CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
91 if (ifitem->index == ifindex && ifitem->family == family)
102 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
105 OIC_LOG(ERROR, TAG, "Malloc failed");
109 OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
110 ifitem->index = ifindex;
111 ifitem->family = family;
112 ifitem->ipv4addr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
113 ifitem->flags = ifa->ifa_flags;
115 bool result = u_arraylist_add(iflist, ifitem);
118 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
128 u_arraylist_destroy(iflist);