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>
25 #include <sys/socket.h>
31 #include <arpa/inet.h>
34 #include "caadapterutils.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
39 #define TAG "IP_MONITOR"
41 char *getHostIPAddress(const char *ifa_name) {
42 static char address[INET_ADDRSTRLEN] = {};
43 memset(&address, 0, INET_ADDRSTRLEN);
45 int sck, status, len = sizeof(ifr.ifr_name) - 1;
48 if ((sck = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
52 strncpy(ifr.ifr_name, ifa_name, len);
53 ifr.ifr_name[len] = '\0';
54 if ((status = ioctl(sck, SIOCGIFADDR, &ifr)) < 0) {
59 ip = inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr);
60 len = sizeof(address) - 1;
61 strncpy(address, ip, len);
66 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
68 u_arraylist_t *iflist = u_arraylist_create();
71 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
75 char* ipAddr = getHostIPAddress("wlan0");
78 OIC_LOG_V(ERROR, TAG, "Failed to get ifaddrs: %s", strerror(errno));
79 u_arraylist_destroy(iflist);
82 OIC_LOG_V(DEBUG, TAG, "Got ifaddrs:: %s", ipAddr);
84 struct in_addr inaddr;
85 inet_pton(AF_INET, ipAddr, &(inaddr));
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;
94 CAResult_t result = u_arraylist_add(iflist, ifitem);
95 if (CA_STATUS_OK != result)
97 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
101 OIC_LOG_V(ERROR, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
106 u_arraylist_destroy(iflist);