Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / tizen / 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 "IP_MONITOR"
38
39 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
40 {
41     u_arraylist_t *iflist = u_arraylist_create();
42     if (!iflist)
43     {
44         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
45         return NULL;
46     }
47
48     struct ifaddrs *ifp = NULL;
49     if (-1 == getifaddrs(&ifp))
50     {
51         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
52         u_arraylist_destroy(iflist);
53         return NULL;
54     }
55
56     struct ifaddrs *ifa = NULL;
57     for (ifa = ifp; ifa; ifa = ifa->ifa_next)
58     {
59         int family = ifa->ifa_addr->sa_family;
60         if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
61         {
62             continue;
63         }
64
65         int ifindex = if_nametoindex(ifa->ifa_name);
66         int length = u_arraylist_length(iflist);
67         int already = false;
68         for (int i = length-1; i >= 0; i--)
69         {
70             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
71             if (ifitem->index == ifindex && ifitem->family == family)
72             {
73                 already = true;
74                 break;
75             }
76         }
77         if (already)
78         {
79             continue;
80         }
81
82         CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
83         if (!ifitem)
84         {
85             OIC_LOG(ERROR, TAG, "Malloc failed");
86             goto exit;
87         }
88
89         OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
90         ifitem->index = ifindex;
91         ifitem->family = family;
92         ifitem->ipv4addr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
93         ifitem->flags = ifa->ifa_flags;
94
95         CAResult_t result = u_arraylist_add(iflist, ifitem);
96         if (CA_STATUS_OK != result)
97         {
98             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
99             goto exit;
100         }
101     }
102
103     freeifaddrs(ifp);
104     return iflist;
105
106 exit:
107     freeifaddrs(ifp);
108     u_arraylist_destroy(iflist);
109     return NULL;
110 }