Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / linux / caipnwmonitor.c
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 #include "caipinterface.h"
22
23 #include <sys/types.h>
24 #include <ifaddrs.h>
25 #include <net/if.h>
26 #include <sys/socket.h>
27 #include <netdb.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include "caadapterutils.h"
33 #include "logger.h"
34 #include "oic_malloc.h"
35 #include "oic_string.h"
36
37 #define TAG "OIC_CA_IP_MONITOR"
38
39 CAResult_t CAIPStartNetworkMonitor()
40 {
41     return CA_STATUS_OK;
42 }
43
44 CAResult_t CAIPStopNetworkMonitor()
45 {
46     return CA_STATUS_OK;
47 }
48
49 int CAGetPollingInterval(int interval)
50 {
51     return interval;
52 }
53
54 CAInterface_t *CAFindInterfaceChange()
55 {
56     return NULL;
57 }
58
59 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
60 {
61     if (desiredIndex < 0)
62     {
63         OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
64         return NULL;
65     }
66
67     u_arraylist_t *iflist = u_arraylist_create();
68     if (!iflist)
69     {
70         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
71         return NULL;
72     }
73
74     struct ifaddrs *ifp = NULL;
75     if (-1 == getifaddrs(&ifp))
76     {
77         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
78         u_arraylist_destroy(iflist);
79         return NULL;
80     }
81     OIC_LOG(DEBUG, TAG, "Got ifaddrs");
82
83     struct ifaddrs *ifa = NULL;
84     for (ifa = ifp; ifa; ifa = ifa->ifa_next)
85     {
86         if (!ifa->ifa_addr)
87         {
88             continue;
89         }
90         int family = ifa->ifa_addr->sa_family;
91         if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
92         {
93             continue;
94         }
95
96         int ifindex = if_nametoindex(ifa->ifa_name);
97         if (desiredIndex && (ifindex != desiredIndex))
98         {
99             continue;
100         }
101
102         int length = u_arraylist_length(iflist);
103         int already = false;
104         for (int i = length-1; i >= 0; i--)
105         {
106             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
107
108             if (ifitem
109                 && (int)ifitem->index == ifindex
110                 && ifitem->family == (uint16_t)family)
111             {
112                 already = true;
113                 break;
114             }
115         }
116         if (already)
117         {
118             continue;
119         }
120
121         CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
122         if (!ifitem)
123         {
124             OIC_LOG(ERROR, TAG, "Malloc failed");
125             goto exit;
126         }
127
128         OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
129         ifitem->index = ifindex;
130         ifitem->family = family;
131         ifitem->ipv4addr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
132         ifitem->flags = ifa->ifa_flags;
133
134         bool result = u_arraylist_add(iflist, ifitem);
135         if (!result)
136         {
137             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
138             goto exit;
139         }
140
141         OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, family);
142     }
143
144     freeifaddrs(ifp);
145     return iflist;
146
147 exit:
148     freeifaddrs(ifp);
149     u_arraylist_destroy(iflist);
150     return NULL;
151 }