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"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
29 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
31 void CALogPDUData(coap_pdu_t *pdu)
33 VERIFY_NON_NULL_VOID(pdu, CA_ADAPTER_UTILS_TAG, "pdu");
34 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - payload : %s", pdu->data);
36 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - type : %d", pdu->hdr->type);
38 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - code : %d", pdu->hdr->code);
40 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - id : %d", pdu->hdr->id);
42 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - token : %s", pdu->hdr->token);
45 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address)
47 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
48 OICCalloc(1, sizeof(CALocalConnectivity_t));
51 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
56 if (address && strlen(address))
60 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
61 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
63 else if (CA_LE == type)
65 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
66 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
68 else if (CA_IPV4 == type)
70 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
71 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
73 else if (CA_IPV6 == type)
75 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
81 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
90 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity)
92 VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
94 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
95 OICCalloc(1, sizeof(CALocalConnectivity_t));
98 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
102 info->type = connectivity->type;
103 if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
105 strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
106 CA_MACADDR_SIZE - 1);
107 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
109 else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
111 strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
112 CA_MACADDR_SIZE - 1);
113 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
115 else if ((CA_IPV4 == info->type)
117 && strlen(connectivity->addressInfo.IP.ipAddress))
119 strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
121 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
122 info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
124 else if (CA_IPV6 == info->type)
126 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
132 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
137 info->isSecured = connectivity->isSecured;
141 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
143 OICFree(localEndpoint);
146 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
147 const char *resourceUri)
149 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
150 OICCalloc(1, sizeof(CARemoteEndpoint_t));
153 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
157 info->transportType = type;
158 if (address && strlen(address))
162 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
163 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
165 else if (CA_LE == type)
167 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
168 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
170 else if (CA_IPV4 == type)
172 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
173 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
175 else if (CA_IPV6 == type)
177 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
183 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
189 if (resourceUri && strlen(resourceUri))
191 info->resourceUri = OICStrdup(resourceUri);
197 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
199 VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
201 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
202 OICCalloc(1, sizeof(CARemoteEndpoint_t));
205 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
209 info->transportType = remoteEndpoint->transportType;
210 if (CA_EDR == info->transportType && ('\0' != remoteEndpoint->addressInfo.BT.btMacAddress[0]))
212 strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
213 CA_MACADDR_SIZE - 1);
214 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
216 else if (CA_LE == info->transportType
217 && ('\0' != remoteEndpoint->addressInfo.LE.leMacAddress[0]))
219 strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
220 CA_MACADDR_SIZE - 1);
221 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
223 else if ((CA_IPV4 == info->transportType)
224 && ('\0' != remoteEndpoint->addressInfo.IP.ipAddress[0]))
226 strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
228 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
229 info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
231 else if (CA_IPV6 == info->transportType)
233 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
237 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "Its not matching. May be multicast.");
240 //For Multicast, remote address will be null while resourceUri will have the service UUID
242 if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
244 info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
247 info->isSecured = remoteEndpoint->isSecured;
251 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
255 OICFree(remoteEndpoint->resourceUri);
256 OICFree(remoteEndpoint);
260 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
261 size_t ipAddrLen, uint16_t *port)
263 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
265 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
266 return CA_STATUS_INVALID_PARAM;
270 uint8_t dotCount = 0;
276 if (isdigit(*ipAddrStr))
278 if(index >= ipAddrLen)
280 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
281 return CA_STATUS_INVALID_PARAM;
284 ipAddr[index] += *ipAddrStr - '0';
286 else if (*ipAddrStr == '.')
299 if (*ipAddrStr == ':')
304 if (isdigit(*ipAddrStr))
307 *port += *ipAddrStr - '0';
321 return CA_STATUS_FAILED;
324 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
326 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
327 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
328 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
330 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
331 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
332 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
333 CAResult_t ret = CA_STATUS_OK;
335 /* Local Loopback Address */
336 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
341 uint16_t parsedPort = 0;
342 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
343 if (ret != CA_STATUS_OK)
345 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
349 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
350 if (ret != CA_STATUS_OK)
352 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
356 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
357 if (ret != CA_STATUS_OK)
359 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
363 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
364 == (ipList2[1] & maskList[1]))
365 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
366 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
370 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
371 const char *multicastAddress, uint16_t port)
373 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
374 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
375 VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
377 uint32_t listLength = u_arraylist_length(serverInfoList);
378 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
380 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
383 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
387 if (info->isMulticastServer && (strncmp(info->ipAddress, multicastAddress,
388 strlen(multicastAddress) == 0))
389 && (info->port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
391 return info->isServerStarted;
397 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
400 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
401 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
403 uint32_t listLength = u_arraylist_length(serverInfoList);
404 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
406 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
412 if (!info->isMulticastServer && (strncmp(info->ipAddress, ipAddress,
413 strlen(ipAddress)) == 0)
414 && (info->port == port))
416 return info->isServerStarted;
422 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
424 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
425 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
427 uint32_t listLength = u_arraylist_length(serverInfoList);
428 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
430 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
435 if ((strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0) &&
436 (info->isSecured == isSecured))
445 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
446 bool isSecured, bool isMulticast, CATransportType_t type)
448 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
449 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", -1);
451 uint32_t listLength = u_arraylist_length(serverInfoList);
453 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
455 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
461 if (!CAAdapterIsSameSubnet(info->ipAddress, ipAddress, info->subNetMask))
466 if (!info->isMulticastServer && (info->isSecured == isSecured))
468 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
469 "CAGetSocketFdForServer found socket [%d]", info->socketFd);
470 return info->socketFd;
475 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
476 "CAGetSocketFdForServer socket fd is not found");
480 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
482 VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
483 VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
485 CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
486 if (CA_STATUS_OK != result)
488 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
493 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
495 VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
497 uint32_t listLength = u_arraylist_length(serverInfoList);
498 for (uint32_t listIndex = 0; listIndex < listLength;)
500 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
507 if (info->socketFd == sockFd)
509 if (u_arraylist_remove(serverInfoList, listIndex))
516 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
527 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
529 uint32_t listLength = u_arraylist_length(infoList);
530 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
532 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
539 u_arraylist_free(&infoList);
542 void CAClearServerInfoList(u_arraylist_t *serverInfoList)
544 uint32_t listLength = u_arraylist_length(serverInfoList);
545 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
547 CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
554 u_arraylist_free(&serverInfoList);