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);
68 CAEndpoint_t *CAAdapterCreateEndpoint(CATransportFlags_t flags,
69 CATransportAdapter_t adapter,
73 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
76 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
82 OICStrcpy(info->addr, sizeof(info->addr), address);
83 info->addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
86 info->adapter = adapter;
92 CAEndpoint_t *CAAdapterCreateLocalEndpoint(CATransportFlags_t flags,
93 CATransportAdapter_t adapter, const char *address)
95 return CAAdapterCreateEndpoint(flags, adapter, address, 0);
98 CAResult_t CACreateEndpoint(CATransportFlags_t flags,
99 CATransportAdapter_t adapter,
102 CAEndpoint_t **object)
104 VERIFY_NON_NULL_RET(object, CA_ADAPTER_UTILS_TAG, "Endpoint is NULL", CA_STATUS_INVALID_PARAM);
105 CAEndpoint_t *endpoint = CAAdapterCreateEndpoint(flags, adapter, addr, port);
108 return CA_STATUS_FAILED;
115 CAEndpoint_t *CAAdapterCloneEndpoint(const CAEndpoint_t *endpoint)
117 VERIFY_NON_NULL_RET(endpoint, CA_ADAPTER_UTILS_TAG, "endpoint is NULL", NULL);
119 CAEndpoint_t *info = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
122 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
130 void CAAdapterFreeEndpoint(CAEndpoint_t *remoteEndpoint)
132 OICFree(remoteEndpoint);
135 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
136 size_t ipAddrLen, uint16_t *port)
138 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
140 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
141 return CA_STATUS_INVALID_PARAM;
145 uint8_t dotCount = 0;
151 if (isdigit(*ipAddrStr))
153 if(index >= ipAddrLen)
155 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
156 return CA_STATUS_INVALID_PARAM;
159 ipAddr[index] += *ipAddrStr - '0';
161 else if (*ipAddrStr == '.')
174 if (*ipAddrStr == ':')
179 if (isdigit(*ipAddrStr))
182 *port += *ipAddrStr - '0';
196 return CA_STATUS_FAILED;
199 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
201 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
202 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
203 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
205 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
206 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
207 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
208 CAResult_t ret = CA_STATUS_OK;
210 /* Local Loopback Address */
211 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
216 uint16_t parsedPort = 0;
217 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
218 if (ret != CA_STATUS_OK)
220 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
224 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
225 if (ret != CA_STATUS_OK)
227 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
231 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
232 if (ret != CA_STATUS_OK)
234 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
238 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
239 == (ipList2[1] & maskList[1]))
240 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
241 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
244 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
245 const char *multicastAddress, uint16_t port)
247 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
248 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
249 VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
251 uint32_t listLength = u_arraylist_length(serverInfoList);
252 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
254 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
257 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
261 if (info->isMulticastServer && (strncmp(info->endpoint.addr, multicastAddress,
262 strlen(multicastAddress)) == 0)
263 && (info->endpoint.port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
265 return info->isServerStarted;
271 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
274 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
275 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
277 uint32_t listLength = u_arraylist_length(serverInfoList);
278 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
280 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
286 if (!info->isMulticastServer && (strncmp(info->endpoint.addr, ipAddress,
287 strlen(ipAddress)) == 0)
288 && (info->endpoint.port == port))
290 return info->isServerStarted;
296 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
298 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
299 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
301 uint32_t listLength = u_arraylist_length(serverInfoList);
302 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
304 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
309 bool ifSecured = info->endpoint.flags & CA_SECURE;
310 if ((strncmp(info->endpoint.addr, ipAddress, strlen(ipAddress)) == 0) &&
311 (ifSecured == isSecured))
313 return info->endpoint.port;
320 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList,
321 bool isMulticast, const CAEndpoint_t *endpoint)
323 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
324 VERIFY_NON_NULL_RET(endpoint, CA_ADAPTER_UTILS_TAG, "endpoint is null", -1);
326 bool isSecured = (endpoint->flags & CA_SECURE) != 0;
328 uint32_t listLength = u_arraylist_length(serverInfoList);
330 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
332 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
338 if (!CAAdapterIsSameSubnet(info->endpoint.addr, endpoint->addr, info->subNetMask))
343 bool ifSecured = info->endpoint.flags & CA_SECURE;
344 if (!info->isMulticastServer && (ifSecured == isSecured))
346 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
347 "CAGetSocketFdForServer found socket [%d]", info->socketFd);
348 return info->socketFd;
353 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
354 "CAGetSocketFdForServer socket fd is not found");
358 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
360 VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
361 VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
363 CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
364 if (CA_STATUS_OK != result)
366 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
371 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
373 VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
375 uint32_t listLength = u_arraylist_length(serverInfoList);
376 for (uint32_t listIndex = 0; listIndex < listLength;)
378 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
385 if (info->socketFd == sockFd)
387 if (u_arraylist_remove(serverInfoList, listIndex))
394 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
405 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
407 uint32_t listLength = u_arraylist_length(infoList);
408 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
410 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
417 u_arraylist_free(&infoList);
420 void CAClearServerInfoList(u_arraylist_t *serverInfoList)
422 uint32_t listLength = u_arraylist_length(serverInfoList);
423 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
425 CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
432 u_arraylist_free(&serverInfoList);
437 * These two conversion functions return void because errors can't happen
438 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
440 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, char *host, uint16_t *port)
442 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
443 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
444 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
446 int r = getnameinfo((struct sockaddr *)sockAddr,
447 sizeof (struct sockaddr_storage),
448 host, CA_IPADDR_SIZE,
450 NI_NUMERICHOST|NI_NUMERICSERV);
455 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
456 "getaddrinfo failed: errno %s", strerror(errno));
460 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
461 "getaddrinfo failed: %s", gai_strerror(r));
465 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
468 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
470 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
471 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
473 struct addrinfo *addrs;
474 struct addrinfo hints = { 0 };
475 hints.ai_family = AF_UNSPEC;
476 hints.ai_socktype = SOCK_DGRAM;
477 hints.ai_flags = AI_NUMERICHOST;
479 int r = getaddrinfo(host, NULL, &hints, &addrs);
484 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
485 "getaddrinfo failed: errno %s", strerror(errno));
489 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
490 "getaddrinfo failed: %s", gai_strerror(r));
494 // assumption: in this case, getaddrinfo will only return one addrinfo
495 // or first is the one we want.
496 if (addrs[0].ai_family == AF_INET6)
498 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
499 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
503 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
504 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
508 #endif // WITH_ARDUINO
511 void CANativeJNISetContext(JNIEnv *env, jobject context)
513 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
517 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
521 g_Context = (*env)->NewGlobalRef(env, context);
524 void CANativeJNISetJavaVM(JavaVM *jvm)
526 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
530 jobject CANativeJNIGetContext()
535 JavaVM *CANativeJNIGetJavaVM()