Fixed klockwork memory leaks and modified the logs
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / caadapterutils.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "caadapterutils.h"
22
23 #include <string.h>
24
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27
28 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
29
30 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
31         const char *address)
32 {
33     CALocalConnectivity_t *info = (CALocalConnectivity_t *)
34                                   OICMalloc(sizeof(CALocalConnectivity_t));
35     if (NULL == info)
36     {
37         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
38         return NULL;
39     }
40     memset(info, 0, sizeof(CALocalConnectivity_t));
41
42     info->type = type;
43     if (address && strlen(address))
44     {
45         if (CA_EDR == type)
46         {
47             strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
48             info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
49         }
50         else if (CA_LE == type)
51         {
52             strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
53             info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
54         }
55         else if (CA_WIFI == type || CA_ETHERNET == type)
56         {
57             strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
58             info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
59         }
60     }
61
62     return info;
63 }
64
65 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(CALocalConnectivity_t *connectivity)
66 {
67     VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
68
69     CALocalConnectivity_t *info = (CALocalConnectivity_t *)
70                                   OICMalloc(sizeof(CALocalConnectivity_t));
71     if (NULL == info)
72     {
73         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
74         return NULL;
75     }
76     memset(info, 0, sizeof(CALocalConnectivity_t));
77
78     info->type = connectivity->type;
79     if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
80     {
81         strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
82                 CA_MACADDR_SIZE - 1);
83         info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
84     }
85     else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
86     {
87         strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
88                 CA_MACADDR_SIZE - 1);
89         info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
90     }
91     else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
92              && strlen(connectivity->addressInfo.IP.ipAddress))
93     {
94         strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
95                 CA_IPADDR_SIZE - 1);
96         info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
97         info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
98     }
99
100     info->isSecured = connectivity->isSecured;
101     return info;
102 }
103
104 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
105 {
106     if (localEndpoint)
107     {
108         OICFree(localEndpoint);
109     }
110 }
111
112 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
113         const char *address,
114         const char *resourceUri)
115 {
116     CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
117                                OICMalloc(sizeof(CARemoteEndpoint_t));
118     if (NULL == info)
119     {
120         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
121         return NULL;
122     }
123     memset(info, 0, sizeof(CARemoteEndpoint_t));
124
125     info->connectivityType = type;
126     if (address && strlen(address))
127     {
128         if (CA_EDR == type)
129         {
130             strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
131             info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
132         }
133         else if (CA_LE == info->connectivityType)
134         {
135             strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
136             info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
137         }
138         else if (CA_WIFI == type || CA_ETHERNET == type)
139         {
140             strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
141             info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
142         }
143     }
144
145     if (resourceUri && strlen(resourceUri))
146     {
147         info->resourceUri = OICStrdup(resourceUri);
148     }
149
150     return info;
151 }
152
153 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
154 {
155     VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
156
157     CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
158                                OICMalloc(sizeof(CARemoteEndpoint_t));
159     if (NULL == info)
160     {
161         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
162         return NULL;
163     }
164     memset(info, 0, sizeof(CARemoteEndpoint_t));
165
166     info->connectivityType = remoteEndpoint->connectivityType;
167     if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
168     {
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';
172     }
173     else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
174     {
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';
178     }
179     else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
180              && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
181     {
182         strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
183                 CA_IPADDR_SIZE - 1);
184         info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
185         info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
186     }
187
188     if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
189     {
190         info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
191     }
192
193     info->isSecured = remoteEndpoint->isSecured;
194     return info;
195 }
196
197 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
198 {
199     if (remoteEndpoint)
200     {
201         if (remoteEndpoint->resourceUri)
202         {
203             OICFree(remoteEndpoint->resourceUri);
204         }
205
206         OICFree(remoteEndpoint);
207     }
208 }
209
210 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
211                            const char *netMask)
212 {
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);
216
217     int32_t ipList1[8] = {0};
218     int32_t ipList2[8] = {0};
219     int32_t maskList[8] = {0};
220
221     /* Local Loopback Address */
222     if (0 == strncmp(ipAddress1, "127.", 4)
223         || 0 == strncmp(ipAddress2, "127.", 4))
224     {
225         return true;
226     }
227
228     char *ipAdrs1 = OICStrdup(ipAddress1);
229     if (!ipAdrs1)
230     {
231         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Failed to get dup string!");
232         return false;
233     }
234
235     int16_t index = 0;
236     int16_t lastDotIndex = 0;
237     int16_t ip1TokenCount = 0;
238     while ('\0' != ipAdrs1[index])
239     {
240         if ('.' == ipAdrs1[index])
241         {
242             ipAdrs1[index] = '\0';
243             ipList1[ip1TokenCount++] = atoi(ipAdrs1 + lastDotIndex);
244             lastDotIndex = index + 1;
245         }
246         index++;
247     }
248     // Add last touple
249     ipList1[ip1TokenCount] = atoi(ipAdrs1 + lastDotIndex);
250
251     char *ipAdrs2 = OICStrdup(ipAddress2);
252     if (!ipAdrs2)
253     {
254         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Failed to get dup string!");
255         OICFree(ipAdrs1);
256         return false;
257     }
258
259     index = 0;
260     lastDotIndex = 0;
261     int16_t ip2TokenCount = 0;
262     while ('\0' != ipAdrs2[index])
263     {
264         if ('.' == ipAdrs2[index])
265         {
266             ipAdrs2[index] = '\0';
267             ipList2[ip2TokenCount++] = atoi(ipAdrs2 + lastDotIndex);
268             lastDotIndex = index + 1;
269         }
270         index++;
271     }
272     // Add last touple
273     ipList2[ip2TokenCount] = atoi(ipAdrs2 + lastDotIndex);
274
275     char *nMask = OICStrdup(netMask);
276     if (!nMask)
277     {
278         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Failed to get dup string!");
279         OICFree(ipAdrs1);
280         OICFree(ipAdrs2);
281         return false;
282     }
283
284     index = 0;
285     lastDotIndex = 0;
286     int16_t maskTokenCount = 0;
287     while ('\0' != nMask[index])
288     {
289         if ('.' == nMask[index])
290         {
291             nMask[index] = '\0';
292             maskList[maskTokenCount++] = atoi(nMask + lastDotIndex);
293             lastDotIndex = index + 1;
294         }
295         index++;
296     }
297     // Add last touple
298     maskList[maskTokenCount] = atoi(nMask + lastDotIndex);
299
300     OICFree(ipAdrs1);
301     OICFree(ipAdrs2);
302     OICFree(nMask);
303
304     if (ip1TokenCount < 3 || ip2TokenCount < 3 || maskTokenCount < 3)
305     {
306         OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Address or mask is invalid!");
307         return false;
308     }
309
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])))
314     {
315         return true;
316     }
317     return false;
318 }