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"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
39 #define CA_ADAPTER_UTILS_TAG "OIC_CA_ADAP_UTILS"
44 * @brief pointer to store JavaVM
46 static JavaVM *g_jvm = NULL;
50 * @brief pointer to store context for android callback interface
52 static jobject g_Context = NULL;
56 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
57 size_t ipAddrLen, uint16_t *port)
59 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
61 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
62 return CA_STATUS_INVALID_PARAM;
72 if (isdigit(*ipAddrStr))
74 if(index >= ipAddrLen)
76 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
77 return CA_STATUS_INVALID_PARAM;
80 ipAddr[index] += *ipAddrStr - '0';
82 else if (*ipAddrStr == '.')
95 if (*ipAddrStr == ':')
100 if (isdigit(*ipAddrStr))
103 *port += *ipAddrStr - '0';
117 return CA_STATUS_FAILED;
120 #else // not with_arduino
122 * These two conversion functions return void because errors can't happen
123 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
125 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
126 char *host, uint16_t *port)
128 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
129 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
130 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
132 int r = getnameinfo((struct sockaddr *)sockAddr,
134 host, MAX_ADDR_STR_SIZE_CA,
136 NI_NUMERICHOST|NI_NUMERICSERV);
141 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
142 "getnameinfo failed: errno %s", strerror(errno));
146 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
147 "getnameinfo failed: %s", gai_strerror(r));
151 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
154 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
156 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
157 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
159 struct addrinfo *addrs;
160 struct addrinfo hints = { .ai_family = AF_UNSPEC,
161 .ai_socktype = SOCK_DGRAM,
162 .ai_flags = AI_NUMERICHOST };
164 int r = getaddrinfo(host, NULL, &hints, &addrs);
169 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
170 "getaddrinfo failed: errno %s", strerror(errno));
174 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
175 "getaddrinfo failed: %s", gai_strerror(r));
179 // assumption: in this case, getaddrinfo will only return one addrinfo
180 // or first is the one we want.
181 if (addrs[0].ai_family == AF_INET6)
183 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
184 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
188 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
189 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
193 #endif // WITH_ARDUINO
196 void CANativeJNISetContext(JNIEnv *env, jobject context)
198 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
202 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
206 g_Context = (*env)->NewGlobalRef(env, context);
209 void CANativeJNISetJavaVM(JavaVM *jvm)
211 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
215 jobject CANativeJNIGetContext()
220 JavaVM *CANativeJNIGetJavaVM()