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 "caadapterutils.h"
25 #include "oic_string.h"
26 #include "oic_malloc.h"
29 #if !defined(WITH_ARDUINO) && !defined(__msys_nt__)
30 #include <sys/socket.h>
31 #include <netinet/in.h>
35 #if defined(__msys_nt__)
44 #define CA_ADAPTER_UTILS_TAG "OIC_CA_ADAP_UTILS"
49 * @brief pointer to store JavaVM
51 static JavaVM *g_jvm = NULL;
55 * @brief pointer to store context for android callback interface
57 static jobject g_Context = NULL;
58 static jobject g_Activity = NULL;
62 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
63 size_t ipAddrLen, uint16_t *port)
65 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
67 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
68 return CA_STATUS_INVALID_PARAM;
78 if (isdigit(*ipAddrStr))
80 if(index >= ipAddrLen)
82 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
83 return CA_STATUS_INVALID_PARAM;
86 ipAddr[index] += *ipAddrStr - '0';
88 else if (*ipAddrStr == '.')
101 if (*ipAddrStr == ':')
106 if (isdigit(*ipAddrStr))
109 *port += *ipAddrStr - '0';
123 return CA_STATUS_FAILED;
126 #else // not with_arduino
128 * These two conversion functions return void because errors can't happen
129 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
131 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
132 char *host, uint16_t *port)
134 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
135 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
136 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
138 int r = getnameinfo((struct sockaddr *)sockAddr,
140 host, MAX_ADDR_STR_SIZE_CA,
142 NI_NUMERICHOST|NI_NUMERICSERV);
145 #if defined(EAI_SYSTEM)
148 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
149 "getnameinfo failed: errno %s", strerror(errno));
153 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
154 "getnameinfo failed: %s", gai_strerror(r));
156 #elif defined(__msys_nt__) || defined(_WIN32)
157 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
158 "getnameinfo failed: errno %i", WSAGetLastError());
160 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
161 "getnameinfo failed: %s", gai_strerror(r));
165 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
168 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
170 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
171 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
173 struct addrinfo *addrs;
174 struct addrinfo hints = { .ai_family = AF_UNSPEC,
175 .ai_socktype = SOCK_DGRAM,
176 .ai_flags = AI_NUMERICHOST };
178 int r = getaddrinfo(host, NULL, &hints, &addrs);
181 #if defined(EAI_SYSTEM)
184 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
185 "getaddrinfo failed: errno %s", strerror(errno));
189 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
190 "getaddrinfo failed: %s", gai_strerror(r));
192 #elif defined(__msys_nt__) || defined(_WIN32)
193 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
194 "getaddrinfo failed: errno %i", WSAGetLastError());
196 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
197 "getaddrinfo failed: %s", gai_strerror(r));
201 // assumption: in this case, getaddrinfo will only return one addrinfo
202 // or first is the one we want.
203 if (addrs[0].ai_family == AF_INET6)
205 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
206 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
210 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
211 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
215 #endif // WITH_ARDUINO
218 void CANativeJNISetContext(JNIEnv *env, jobject context)
220 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
224 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "context is null");
230 g_Context = (*env)->NewGlobalRef(env, context);
234 OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "context is already set");
238 void CANativeJNISetJavaVM(JavaVM *jvm)
240 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
244 jobject CANativeJNIGetContext()
249 JavaVM *CANativeJNIGetJavaVM()
254 void CANativeSetActivity(JNIEnv *env, jobject activity)
256 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeSetActivity");
260 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "activity is null");
266 g_Activity = (*env)->NewGlobalRef(env, activity);
270 OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "activity is already set");
274 jobject *CANativeGetActivity()
279 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
280 const char* methodName,
281 const char* methodFormat)
283 VERIFY_NON_NULL_RET(env, CA_ADAPTER_UTILS_TAG, "env", NULL);
284 VERIFY_NON_NULL_RET(className, CA_ADAPTER_UTILS_TAG, "className", NULL);
285 VERIFY_NON_NULL_RET(methodName, CA_ADAPTER_UTILS_TAG, "methodName", NULL);
286 VERIFY_NON_NULL_RET(methodFormat, CA_ADAPTER_UTILS_TAG, "methodFormat", NULL);
288 jclass jni_cid = (*env)->FindClass(env, className);
291 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className);
295 jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat);
298 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName);
299 (*env)->DeleteLocalRef(env, jni_cid);
303 (*env)->DeleteLocalRef(env, jni_cid);
307 void CADeleteGlobalReferences(JNIEnv *env)
311 (*env)->DeleteGlobalRef(env, g_Context);
317 (*env)->DeleteGlobalRef(env, g_Activity);