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 "CA_ADAPTER_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, char *host, uint16_t *port)
127 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
128 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
129 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
131 int r = getnameinfo((struct sockaddr *)sockAddr,
132 sizeof (struct sockaddr_storage),
133 host, MAX_ADDR_STR_SIZE_CA,
135 NI_NUMERICHOST|NI_NUMERICSERV);
140 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
141 "getaddrinfo failed: errno %s", strerror(errno));
145 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
146 "getaddrinfo failed: %s", gai_strerror(r));
150 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
153 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
155 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
156 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
158 struct addrinfo *addrs;
159 struct addrinfo hints = { .ai_family = AF_UNSPEC,
160 .ai_socktype = SOCK_DGRAM,
161 .ai_flags = AI_NUMERICHOST };
163 int r = getaddrinfo(host, NULL, &hints, &addrs);
168 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
169 "getaddrinfo failed: errno %s", strerror(errno));
173 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
174 "getaddrinfo failed: %s", gai_strerror(r));
178 // assumption: in this case, getaddrinfo will only return one addrinfo
179 // or first is the one we want.
180 if (addrs[0].ai_family == AF_INET6)
182 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
183 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
187 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
188 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
192 #endif // WITH_ARDUINO
195 void CANativeJNISetContext(JNIEnv *env, jobject context)
197 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
201 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
205 g_Context = (*env)->NewGlobalRef(env, context);
208 void CANativeJNISetJavaVM(JavaVM *jvm)
210 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
214 jobject CANativeJNIGetContext()
219 JavaVM *CANativeJNIGetJavaVM()