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_malloc.h"
26 #include "oic_string.h"
28 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
30 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
33 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
34 OICMalloc(sizeof(CALocalConnectivity_t));
37 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
40 memset(info, 0, sizeof(CALocalConnectivity_t));
43 if (address && strlen(address))
47 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
48 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
50 else if (CA_LE == type)
52 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
53 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
55 else if (CA_WIFI == type || CA_ETHERNET == type)
57 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
58 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
65 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(CALocalConnectivity_t *connectivity)
67 VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
69 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
70 OICMalloc(sizeof(CALocalConnectivity_t));
73 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
76 memset(info, 0, sizeof(CALocalConnectivity_t));
78 info->type = connectivity->type;
79 if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
81 strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
83 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
85 else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
87 strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
89 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
91 else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
92 && strlen(connectivity->addressInfo.IP.ipAddress))
94 strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
96 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
97 info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
100 info->isSecured = connectivity->isSecured;
104 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
108 OICFree(localEndpoint);
112 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
114 const char *resourceUri)
116 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
117 OICMalloc(sizeof(CARemoteEndpoint_t));
120 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
123 memset(info, 0, sizeof(CARemoteEndpoint_t));
125 info->connectivityType = type;
126 if (address && strlen(address))
130 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
131 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
133 else if (CA_LE == info->connectivityType)
135 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
136 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
138 else if (CA_WIFI == type || CA_ETHERNET == type)
140 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
141 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
145 if (resourceUri && strlen(resourceUri))
147 info->resourceUri = OICStrdup(resourceUri);
153 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
155 VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
157 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
158 OICMalloc(sizeof(CARemoteEndpoint_t));
161 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
164 memset(info, 0, sizeof(CARemoteEndpoint_t));
166 info->connectivityType = remoteEndpoint->connectivityType;
167 if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
169 strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
170 CA_MACADDR_SIZE - 1);
171 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
173 else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
175 strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
176 CA_MACADDR_SIZE - 1);
177 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
179 else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
180 && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
182 strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
184 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
185 info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
188 if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
190 info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
193 info->isSecured = remoteEndpoint->isSecured;
197 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
201 if (remoteEndpoint->resourceUri)
203 OICFree(remoteEndpoint->resourceUri);
206 OICFree(remoteEndpoint);
210 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
213 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
214 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
215 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
217 int32_t ipList1[8] = {0};
218 int32_t ipList2[8] = {0};
219 int32_t maskList[8] = {0};
221 /* Local Loopback Address */
222 if (0 == strncmp(ipAddress1, "127.", 4)
223 || 0 == strncmp(ipAddress2, "127.", 4))
228 char *ipAdrs1 = OICStrdup(ipAddress1);
231 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Failed to get dup string!");
236 int16_t lastDotIndex = 0;
237 int16_t ip1TokenCount = 0;
238 while ('\0' != ipAdrs1[index])
240 if ('.' == ipAdrs1[index])
242 ipAdrs1[index] = '\0';
243 ipList1[ip1TokenCount++] = atoi(ipAdrs1 + lastDotIndex);
244 lastDotIndex = index + 1;
249 ipList1[ip1TokenCount] = atoi(ipAdrs1 + lastDotIndex);
251 char *ipAdrs2 = OICStrdup(ipAddress2);
254 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Failed to get dup string!");
261 int16_t ip2TokenCount = 0;
262 while ('\0' != ipAdrs2[index])
264 if ('.' == ipAdrs2[index])
266 ipAdrs2[index] = '\0';
267 ipList2[ip2TokenCount++] = atoi(ipAdrs2 + lastDotIndex);
268 lastDotIndex = index + 1;
273 ipList2[ip2TokenCount] = atoi(ipAdrs2 + lastDotIndex);
275 char *nMask = OICStrdup(netMask);
278 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Failed to get dup string!");
286 int16_t maskTokenCount = 0;
287 while ('\0' != nMask[index])
289 if ('.' == nMask[index])
292 maskList[maskTokenCount++] = atoi(nMask + lastDotIndex);
293 lastDotIndex = index + 1;
298 maskList[maskTokenCount] = atoi(nMask + lastDotIndex);
304 if (ip1TokenCount < 3 || ip2TokenCount < 3 || maskTokenCount < 3)
306 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Address or mask is invalid!");
310 if (((ipList1[0]& maskList[0]) == (ipList2[0]& maskList[0]))
311 && ((ipList1[1]& maskList[1]) == (ipList2[1]& maskList[1]))
312 && ((ipList1[2]& maskList[2]) == (ipList2[2]& maskList[2]))
313 && ((ipList1[3]& maskList[3]) == (ipList2[3]& maskList[3])))