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>
24 #include <sys/socket.h>
30 #include <arpa/inet.h>
33 #include "caadapterutils.h"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
37 #include "org_iotivity_ca_CaIpInterface.h"
39 #define TAG "OIC_CA_IP_MONITOR"
41 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
42 uint32_t addr, int flags);
44 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
45 const char *name, int family, uint32_t addr, int flags);
47 CAResult_t CAIPJniInit();
50 * destroy JNI interface.
51 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
53 static CAResult_t CAIPDestroyJniInterface();
55 #define MAX_INTERFACE_INFO_LENGTH 1024 // allows 32 interfaces from SIOCGIFCONF
57 CAResult_t CAIPStartNetworkMonitor()
62 CAResult_t CAIPStopNetworkMonitor()
64 return CAIPDestroyJniInterface();
67 int CAGetPollingInterval(int interval)
72 CAInterface_t *CAFindInterfaceChange()
74 char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 };
75 struct ifconf ifc = { .ifc_len = MAX_INTERFACE_INFO_LENGTH, .ifc_buf = buf };
77 int s = caglobals.ip.u6.fd != -1 ? caglobals.ip.u6.fd : caglobals.ip.u4.fd;
78 if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
80 OIC_LOG_V(ERROR, TAG, "SIOCGIFCONF failed: %s", strerror(errno));
84 CAInterface_t *foundNewInterface = NULL;
86 struct ifreq* ifr = ifc.ifc_req;
87 size_t interfaces = ifc.ifc_len / sizeof (ifc.ifc_req[0]);
88 size_t ifreqsize = ifc.ifc_len;
90 CAIfItem_t *previous = (CAIfItem_t *)OICMalloc(ifreqsize);
93 OIC_LOG(ERROR, TAG, "OICMalloc failed");
97 memcpy(previous, caglobals.ip.nm.ifItems, ifreqsize);
98 size_t numprevious = caglobals.ip.nm.numIfItems;
100 if (ifreqsize > caglobals.ip.nm.sizeIfItems)
103 CAIfItem_t *items = (CAIfItem_t *)OICRealloc(caglobals.ip.nm.ifItems, ifreqsize);
106 OIC_LOG(ERROR, TAG, "OICRealloc failed");
110 caglobals.ip.nm.ifItems = items;
111 caglobals.ip.nm.sizeIfItems = ifreqsize;
114 caglobals.ip.nm.numIfItems = 0;
115 for (size_t i = 0; i < interfaces; i++)
117 struct ifreq* item = &ifr[i];
118 char *name = item->ifr_name;
119 struct sockaddr_in *sa4 = (struct sockaddr_in *)&item->ifr_addr;
120 uint32_t ipv4addr = sa4->sin_addr.s_addr;
122 if (ioctl(s, SIOCGIFFLAGS, item) < 0)
124 OIC_LOG_V(ERROR, TAG, "SIOCGIFFLAGS failed: %s", strerror(errno));
127 int16_t flags = item->ifr_flags;
128 if ((flags & IFF_LOOPBACK) || !(flags & IFF_RUNNING))
132 if (ioctl(s, SIOCGIFINDEX, item) < 0)
134 OIC_LOG_V(ERROR, TAG, "SIOCGIFINDEX failed: %s", strerror(errno));
138 int ifIndex = item->ifr_ifindex;
139 caglobals.ip.nm.ifItems[i].ifIndex = ifIndex; // refill interface list
140 caglobals.ip.nm.numIfItems++;
142 if (foundNewInterface)
144 continue; // continue updating interface list
147 // see if this interface didn't previously exist
149 for (size_t j = 0; j < numprevious; j++)
151 if (ifIndex == previous[j].ifIndex)
159 OIC_LOG_V(INFO, TAG, "Interface found: %s", name);
163 foundNewInterface = CANewInterfaceItem(ifIndex, name, AF_INET, ipv4addr, flags);
167 return foundNewInterface;
170 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
172 u_arraylist_t *iflist = u_arraylist_create();
175 OIC_LOG_V(ERROR, TAG, "Failed to create iflist: %s", strerror(errno));
179 char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 };
180 struct ifconf ifc = { .ifc_len = MAX_INTERFACE_INFO_LENGTH, .ifc_buf = buf };
182 int s = caglobals.ip.u6.fd != -1 ? caglobals.ip.u6.fd : caglobals.ip.u4.fd;
183 if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
185 OIC_LOG_V(ERROR, TAG, "SIOCGIFCONF failed: %s", strerror(errno));
186 u_arraylist_destroy(iflist);
190 struct ifreq* ifr = ifc.ifc_req;
191 size_t interfaces = ifc.ifc_len / sizeof (ifc.ifc_req[0]);
192 size_t ifreqsize = ifc.ifc_len;
194 if (ifreqsize > caglobals.ip.nm.sizeIfItems)
196 CAIfItem_t *items = (CAIfItem_t *)OICRealloc(caglobals.ip.nm.ifItems, ifreqsize);
199 OIC_LOG(ERROR, TAG, "OICRealloc failed");
202 caglobals.ip.nm.ifItems = items;
203 caglobals.ip.nm.sizeIfItems = ifreqsize;
206 caglobals.ip.nm.numIfItems = 0;
207 for (size_t i = 0; i < interfaces; i++)
209 CAResult_t result = CA_STATUS_OK;
210 struct ifreq* item = &ifr[i];
211 char *name = item->ifr_name;
212 struct sockaddr_in *sa4 = (struct sockaddr_in *)&item->ifr_addr;
213 uint32_t ipv4addr = sa4->sin_addr.s_addr;
215 if (ioctl(s, SIOCGIFFLAGS, item) < 0)
217 OIC_LOG_V(ERROR, TAG, "SIOCGIFFLAGS failed: %s", strerror(errno));
220 int16_t flags = item->ifr_flags;
221 if ((flags & IFF_LOOPBACK) || !(flags & IFF_RUNNING))
225 if (ioctl(s, SIOCGIFINDEX, item) < 0)
227 OIC_LOG_V(ERROR, TAG, "SIOCGIFINDEX failed: %s", strerror(errno));
231 int ifindex = item->ifr_ifindex;
232 caglobals.ip.nm.ifItems[i].ifIndex = ifindex;
233 caglobals.ip.nm.numIfItems++;
235 if (desiredIndex && (ifindex != desiredIndex))
240 // Add IPv4 interface
241 result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET, ipv4addr, flags);
242 if (CA_STATUS_OK != result)
247 // Add IPv6 interface
248 result = CAAddInterfaceItem(iflist, ifindex, name, AF_INET6, ipv4addr, flags);
249 if (CA_STATUS_OK != result)
257 u_arraylist_destroy(iflist);
261 static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
262 const char *name, int family, uint32_t addr, int flags)
264 CAInterface_t *ifitem = CANewInterfaceItem(index, name, family, addr, flags);
267 return CA_STATUS_FAILED;
269 bool result = u_arraylist_add(iflist, ifitem);
272 OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
274 return CA_STATUS_FAILED;
280 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
281 uint32_t addr, int flags)
283 CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof (CAInterface_t));
286 OIC_LOG(ERROR, TAG, "Malloc failed");
290 OICStrcpy(ifitem->name, sizeof (ifitem->name), name);
291 ifitem->index = index;
292 ifitem->family = family;
293 ifitem->ipv4addr = addr;
294 ifitem->flags = flags;
299 CAResult_t CAIPJniInit()
301 OIC_LOG(DEBUG, TAG, "CAIPJniInit_IN");
303 JavaVM *jvm = CANativeJNIGetJavaVM();
306 OIC_LOG(ERROR, TAG, "Could not get JavaVM pointer");
307 return CA_STATUS_FAILED;
310 jobject context = CANativeJNIGetContext();
313 OIC_LOG(ERROR, TAG, "unable to get application context");
314 return CA_STATUS_FAILED;
318 if ((*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
320 OIC_LOG(ERROR, TAG, "Could not get JNIEnv pointer");
321 return CA_STATUS_FAILED;
324 jclass cls_Context = (*env)->FindClass(env, "android/content/Context");
327 OIC_LOG(ERROR, TAG, "Could not get context object class");
328 return CA_STATUS_FAILED;
331 jmethodID mid_getApplicationContext = (*env)->GetMethodID(env, cls_Context,
332 "getApplicationContext",
333 "()Landroid/content/Context;");
334 if (!mid_getApplicationContext)
336 OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method");
337 return CA_STATUS_FAILED;
340 jobject jApplicationContext = (*env)->CallObjectMethod(env, context,
341 mid_getApplicationContext);
342 if (!jApplicationContext)
344 OIC_LOG(ERROR, TAG, "Could not get application context");
345 return CA_STATUS_FAILED;
348 jclass cls_CaIpInterface = (*env)->FindClass(env, "org/iotivity/ca/CaIpInterface");
349 if (!cls_CaIpInterface)
351 OIC_LOG(ERROR, TAG, "Could not get CaIpInterface class");
352 return CA_STATUS_FAILED;
355 jmethodID mid_CaIpInterface_ctor = (*env)->GetMethodID(env, cls_CaIpInterface, "<init>",
356 "(Landroid/content/Context;)V");
357 if (!mid_CaIpInterface_ctor)
359 OIC_LOG(ERROR, TAG, "Could not get CaIpInterface constructor method");
360 return CA_STATUS_FAILED;
363 (*env)->NewObject(env, cls_CaIpInterface, mid_CaIpInterface_ctor, jApplicationContext);
364 OIC_LOG(DEBUG, TAG, "Create CaIpInterface instance, success");
366 OIC_LOG(DEBUG, TAG, "CAIPJniInit_OUT");
370 static CAResult_t CAIPDestroyJniInterface()
372 OIC_LOG(DEBUG, TAG, "CAIPDestroyJniInterface");
374 JavaVM *jvm = CANativeJNIGetJavaVM();
377 OIC_LOG(ERROR, TAG, "Could not get JavaVM pointer");
378 return CA_STATUS_FAILED;
381 bool isAttached = false;
383 jint res = (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6);
386 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
387 res = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
391 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
392 return CA_STATUS_FAILED;
397 jclass jni_IpInterface = (*env)->FindClass(env, "org/iotivity/ca/CaIpInterface");
398 if (!jni_IpInterface)
400 OIC_LOG(ERROR, TAG, "Could not get CaIpInterface class");
404 jmethodID jni_InterfaceDestroyMethod = (*env)->GetStaticMethodID(env, jni_IpInterface,
405 "destroyIpInterface",
407 if (!jni_InterfaceDestroyMethod)
409 OIC_LOG(ERROR, TAG, "Could not get CaIpInterface destroy method");
413 (*env)->CallStaticVoidMethod(env, jni_IpInterface, jni_InterfaceDestroyMethod);
415 if ((*env)->ExceptionCheck(env))
417 OIC_LOG(ERROR, TAG, "destroyIpInterface has failed");
418 (*env)->ExceptionDescribe(env);
419 (*env)->ExceptionClear(env);
423 OIC_LOG(DEBUG, TAG, "Destroy instance for CaIpInterface");
427 (*jvm)->DetachCurrentThread(jvm);
436 (*jvm)->DetachCurrentThread(jvm);
439 return CA_STATUS_FAILED;
442 JNIEXPORT void JNICALL
443 Java_org_iotivity_ca_CaIpInterface_caIpStateEnabled(JNIEnv *env, jclass class)
447 OIC_LOG(DEBUG, TAG, "caIpStateEnabled");
452 JNIEXPORT void JNICALL
453 Java_org_iotivity_ca_CaIpInterface_caIpStateDisabled(JNIEnv *env, jclass class)
457 OIC_LOG(DEBUG, TAG, "caIpStateDisabled");
459 u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
462 OIC_LOG_V(ERROR, TAG, "get interface info failed");
465 u_arraylist_destroy(iflist);