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;
55 void CALogPDUData(coap_pdu_t *pdu)
57 VERIFY_NON_NULL_VOID(pdu, CA_ADAPTER_UTILS_TAG, "pdu");
58 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - payload : %s", pdu->data);
60 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - type : %d", pdu->hdr->type);
62 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - code : %d", pdu->hdr->code);
64 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - id : %d", pdu->hdr->id);
66 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - token : %s", pdu->hdr->token);
69 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
70 size_t ipAddrLen, uint16_t *port)
72 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
74 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
75 return CA_STATUS_INVALID_PARAM;
85 if (isdigit(*ipAddrStr))
87 if(index >= ipAddrLen)
89 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
90 return CA_STATUS_INVALID_PARAM;
93 ipAddr[index] += *ipAddrStr - '0';
95 else if (*ipAddrStr == '.')
108 if (*ipAddrStr == ':')
113 if (isdigit(*ipAddrStr))
116 *port += *ipAddrStr - '0';
130 return CA_STATUS_FAILED;
133 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
135 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
136 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
137 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
139 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
140 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
141 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
142 CAResult_t ret = CA_STATUS_OK;
144 /* Local Loopback Address */
145 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
150 uint16_t parsedPort = 0;
151 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
152 if (ret != CA_STATUS_OK)
154 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
158 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
159 if (ret != CA_STATUS_OK)
161 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
165 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
166 if (ret != CA_STATUS_OK)
168 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
172 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
173 == (ipList2[1] & maskList[1]))
174 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
175 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
178 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
179 const char *multicastAddress, uint16_t port)
181 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
182 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
183 VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
185 uint32_t listLength = u_arraylist_length(serverInfoList);
186 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
188 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
191 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
195 if (info->isMulticastServer && (strncmp(info->endpoint.addr, multicastAddress,
196 strlen(multicastAddress)) == 0)
197 && (info->endpoint.port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
199 return info->isServerStarted;
205 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
208 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
209 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
211 uint32_t listLength = u_arraylist_length(serverInfoList);
212 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
214 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
220 if (!info->isMulticastServer && (strncmp(info->endpoint.addr, ipAddress,
221 strlen(ipAddress)) == 0)
222 && (info->endpoint.port == port))
224 return info->isServerStarted;
230 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
232 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
233 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
235 uint32_t listLength = u_arraylist_length(serverInfoList);
236 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
238 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
243 bool ifSecured = info->endpoint.flags & CA_SECURE;
244 if ((strncmp(info->endpoint.addr, ipAddress, strlen(ipAddress)) == 0) &&
245 (ifSecured == isSecured))
247 return info->endpoint.port;
254 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
255 bool isMulticast, const CAEndpoint_t *endpoint)
257 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
258 VERIFY_NON_NULL_RET(endpoint, CA_ADAPTER_UTILS_TAG, "endpoint is null", -1);
260 bool isSecured = (endpoint->flags & CA_SECURE) != 0;
262 uint32_t listLength = u_arraylist_length(serverInfoList);
264 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
266 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
272 if (!CAAdapterIsSameSubnet(info->endpoint.addr, endpoint->addr, info->subNetMask))
277 bool ifSecured = info->endpoint.flags & CA_SECURE;
278 if (!info->isMulticastServer && (ifSecured == isSecured))
280 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
281 "CAGetSocketFdForServer found socket [%d]", info->socketFd);
282 return info->socketFd;
287 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
288 "CAGetSocketFdForServer socket fd is not found");
292 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
294 VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
295 VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
297 CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
298 if (CA_STATUS_OK != result)
300 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
305 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
307 VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
309 uint32_t listLength = u_arraylist_length(serverInfoList);
310 for (uint32_t listIndex = 0; listIndex < listLength;)
312 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
319 if (info->socketFd == sockFd)
321 if (u_arraylist_remove(serverInfoList, listIndex))
328 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
339 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
341 uint32_t listLength = u_arraylist_length(infoList);
342 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
344 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
351 u_arraylist_free(&infoList);
354 void CAClearServerInfoList(u_arraylist_t *serverInfoList)
356 uint32_t listLength = u_arraylist_length(serverInfoList);
357 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
359 CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
366 u_arraylist_free(&serverInfoList);
371 * These two conversion functions return void because errors can't happen
372 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
374 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, char *host, uint16_t *port)
376 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
377 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
378 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
380 int r = getnameinfo((struct sockaddr *)sockAddr,
381 sizeof (struct sockaddr_storage),
382 host, CA_IPADDR_SIZE,
384 NI_NUMERICHOST|NI_NUMERICSERV);
389 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
390 "getaddrinfo failed: errno %s", strerror(errno));
394 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
395 "getaddrinfo failed: %s", gai_strerror(r));
399 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
402 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
404 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
405 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
407 struct addrinfo *addrs;
408 struct addrinfo hints = { 0 };
409 hints.ai_family = AF_UNSPEC;
410 hints.ai_socktype = SOCK_DGRAM;
411 hints.ai_flags = AI_NUMERICHOST;
413 int r = getaddrinfo(host, NULL, &hints, &addrs);
418 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
419 "getaddrinfo failed: errno %s", strerror(errno));
423 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
424 "getaddrinfo failed: %s", gai_strerror(r));
428 // assumption: in this case, getaddrinfo will only return one addrinfo
429 // or first is the one we want.
430 if (addrs[0].ai_family == AF_INET6)
432 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
433 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
437 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
438 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
442 #endif // WITH_ARDUINO
445 void CANativeJNISetContext(JNIEnv *env, jobject context)
447 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
451 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
455 g_Context = (*env)->NewGlobalRef(env, context);
458 void CANativeJNISetJavaVM(JavaVM *jvm)
460 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
464 jobject CANativeJNIGetContext()
469 JavaVM *CANativeJNIGetJavaVM()