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(CAConnectivityType_t type,
48 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
49 OICCalloc(1, sizeof(CALocalConnectivity_t));
52 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
57 if (address && strlen(address))
61 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
62 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
64 else if (CA_LE == type)
66 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
67 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
69 else if (CA_WIFI == type || CA_ETHERNET == type)
71 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
72 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
76 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
85 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity)
87 VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
89 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
90 OICCalloc(1, sizeof(CALocalConnectivity_t));
93 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
97 info->type = connectivity->type;
98 if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
100 strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
101 CA_MACADDR_SIZE - 1);
102 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
104 else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
106 strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
107 CA_MACADDR_SIZE - 1);
108 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
110 else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
111 && strlen(connectivity->addressInfo.IP.ipAddress))
113 strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
115 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
116 info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
120 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
125 info->isSecured = connectivity->isSecured;
129 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
131 OICFree(localEndpoint);
134 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
136 const char *resourceUri)
138 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
139 OICCalloc(1, sizeof(CARemoteEndpoint_t));
142 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
146 info->connectivityType = type;
147 if (address && strlen(address))
151 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
152 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
154 else if (CA_LE == type)
156 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
157 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
159 else if (CA_WIFI == type || CA_ETHERNET == type)
161 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
162 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
166 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
172 if (resourceUri && strlen(resourceUri))
174 info->resourceUri = OICStrdup(resourceUri);
180 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
182 VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
184 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
185 OICCalloc(1, sizeof(CARemoteEndpoint_t));
188 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
192 info->connectivityType = remoteEndpoint->connectivityType;
193 if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
195 strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
196 CA_MACADDR_SIZE - 1);
197 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
199 else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
201 strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
202 CA_MACADDR_SIZE - 1);
203 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
205 else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
206 && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
208 strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
210 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
211 info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
215 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "Its not matching. May be multicast.");
218 //For Multicast, remote address will be null while resourceUri will have the service UUID
220 if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
222 info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
225 info->isSecured = remoteEndpoint->isSecured;
229 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
233 OICFree(remoteEndpoint->resourceUri);
234 OICFree(remoteEndpoint);
238 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
239 size_t ipAddrLen, uint16_t *port)
241 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
243 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
244 return CA_STATUS_INVALID_PARAM;
248 uint8_t dotCount = 0;
254 if (isdigit(*ipAddrStr))
256 if(index >= ipAddrLen)
258 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
259 return CA_STATUS_INVALID_PARAM;
262 ipAddr[index] += *ipAddrStr - '0';
264 else if (*ipAddrStr == '.')
277 if (*ipAddrStr == ':')
282 if (isdigit(*ipAddrStr))
285 *port += *ipAddrStr - '0';
299 return CA_STATUS_FAILED;
302 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
304 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
305 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
306 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
308 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
309 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
310 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
311 CAResult_t ret = CA_STATUS_OK;
313 /* Local Loopback Address */
314 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
319 uint16_t parsedPort = 0;
320 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
321 if (ret != CA_STATUS_OK)
323 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
327 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
328 if (ret != CA_STATUS_OK)
330 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
334 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
335 if (ret != CA_STATUS_OK)
337 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
341 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
342 == (ipList2[1] & maskList[1]))
343 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
344 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));