Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / android / 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 <sys/socket.h>
26 #include <netdb.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30
31 #include <arpa/inet.h>
32 #include <linux/if.h>
33
34 #include "caadapterutils.h"
35 #include "logger.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
38
39 #define TAG "IP_MONITOR"
40
41 char *getHostIPAddress(const char *ifa_name) {
42     static char address[INET_ADDRSTRLEN] = {};
43     memset(&address, 0, INET_ADDRSTRLEN);
44     struct ifreq ifr;
45     int sck, status, len = sizeof(ifr.ifr_name) - 1;
46     char *ip;
47
48     if ((sck = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
49         return NULL;
50     }
51
52     strncpy(ifr.ifr_name, ifa_name, len);
53     ifr.ifr_name[len] = '\0';
54     if ((status = ioctl(sck, SIOCGIFADDR, &ifr)) < 0) {
55         close(sck);
56         return NULL;
57     }
58     close(sck);
59     ip = inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr);
60     len = sizeof(address) - 1;
61     strncpy(address, ip, len);
62     address[len] = '\0';
63     return address;
64 }
65
66 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
67 {
68     u_arraylist_t *iflist = u_arraylist_create();
69     if (!iflist)
70     {
71         OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
72         return NULL;
73     }
74
75     char* ipAddr = getHostIPAddress("wlan0");
76     if (NULL == ipAddr)
77     {
78         OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
79         u_arraylist_destroy(iflist);
80         return NULL;
81     } 
82     OIC_LOG_V(DEBUG, TAG, "Got ifaddrs:: %s", ipAddr);
83
84     struct in_addr inaddr;
85     inet_pton(AF_INET, ipAddr, &(inaddr));
86
87     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));;
88     OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, "wlan0");
89     ifitem->index = 0; //if_nametoindex("wlan0");
90     ifitem->family = AF_INET; //we support ipv4 only
91     ifitem->ipv4addr = inaddr.s_addr;
92     ifitem->flags = IFF_UP|IFF_RUNNING;
93
94     CAResult_t result = u_arraylist_add(iflist, ifitem);
95     if (CA_STATUS_OK != result)
96     {
97         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
98         goto exit;
99     }
100
101     OIC_LOG_V(ERROR, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
102
103     return iflist;
104
105 exit:
106     u_arraylist_destroy(iflist);
107     return NULL;
108 }
109