Removing build warnings
[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 "IP_MONITOR"
38
39 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
40 {
41     if (desiredIndex < 0)
42     {
43         OIC_LOG_V(ERROR, TAG, "invalid index : %d", desiredIndex);
44         return NULL;
45     }
46
47     u_arraylist_t *iflist = u_arraylist_create();
48     if (!iflist)
49     {
50         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
51         return NULL;
52     }
53
54     struct ifaddrs *ifp = NULL;
55     if (-1 == getifaddrs(&ifp))
56     {
57         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
58         u_arraylist_destroy(iflist);
59         return NULL;
60     }
61     OIC_LOG(DEBUG, TAG, "Got ifaddrs");
62
63     struct ifaddrs *ifa = NULL;
64     for (ifa = ifp; ifa; ifa = ifa->ifa_next)
65     {
66         int family = ifa->ifa_addr->sa_family;
67         if ((ifa->ifa_flags & IFF_LOOPBACK) || (AF_INET != family && AF_INET6 != family))
68         {
69             continue;
70         }
71
72         int ifindex = if_nametoindex(ifa->ifa_name);
73         if (desiredIndex && (ifindex != desiredIndex))
74         {
75             continue;
76         }
77
78         int length = u_arraylist_length(iflist);
79         int already = false;
80         for (int i = length-1; i >= 0; i--)
81         {
82             CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i);
83
84             if (ifitem
85                 && (int)ifitem->index == ifindex
86                 && ifitem->family == (uint16_t)family)
87             {
88                 already = true;
89                 break;
90             }
91         }
92         if (already)
93         {
94             continue;
95         }
96
97         CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
98         if (!ifitem)
99         {
100             OIC_LOG(ERROR, TAG, "Malloc failed");
101             goto exit;
102         }
103
104         OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, ifa->ifa_name);
105         ifitem->index = ifindex;
106         ifitem->family = family;
107         ifitem->ipv4addr = ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
108         ifitem->flags = ifa->ifa_flags;
109
110         CAResult_t result = u_arraylist_add(iflist, ifitem);
111         if (CA_STATUS_OK != result)
112         {
113             OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
114             goto exit;
115         }
116
117         OIC_LOG_V(ERROR, TAG, "Added interface: %s (%d)", ifitem->name, family);
118     }
119
120     freeifaddrs(ifp);
121     return iflist;
122
123 exit:
124     freeifaddrs(ifp);
125     u_arraylist_destroy(iflist);
126     return NULL;
127 }