API change , Retransmission Callback on expiry , remove glib source for dynamic linking
[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
27 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
28
29 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
30         const char *address)
31 {
32     CALocalConnectivity_t *info = (CALocalConnectivity_t *)
33                                   OICMalloc(sizeof(CALocalConnectivity_t));
34     if (NULL == info)
35     {
36         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
37         return NULL;
38     }
39     memset(info, 0, sizeof(CALocalConnectivity_t));
40
41     info->type = type;
42     if (address && strlen(address))
43     {
44         if (CA_EDR == type)
45         {
46             strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
47             info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
48         }
49         else if (CA_LE == type)
50         {
51             strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
52             info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
53         }
54         else if (CA_WIFI == type || CA_ETHERNET == type)
55         {
56             strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
57             info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
58         }
59     }
60
61     return info;
62 }
63
64 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(CALocalConnectivity_t *connectivity)
65 {
66     VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
67
68     CALocalConnectivity_t *info = (CALocalConnectivity_t *)
69                                   OICMalloc(sizeof(CALocalConnectivity_t));
70     if (NULL == info)
71     {
72         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
73         return NULL;
74     }
75     memset(info, 0, sizeof(CALocalConnectivity_t));
76
77     info->type = connectivity->type;
78     if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
79     {
80         strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
81                 CA_MACADDR_SIZE - 1);
82         info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
83     }
84     else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
85     {
86         strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
87                 CA_MACADDR_SIZE - 1);
88         info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
89     }
90     else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
91              && strlen(connectivity->addressInfo.IP.ipAddress))
92     {
93         strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
94                 CA_IPADDR_SIZE - 1);
95         info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
96         info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
97     }
98
99     info->isSecured = connectivity->isSecured;
100     return info;
101 }
102
103 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
104 {
105     if (localEndpoint)
106     {
107         OICFree(localEndpoint);
108     }
109 }
110
111 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
112         const char *address,
113         const char *resourceUri)
114 {
115     CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
116                                OICMalloc(sizeof(CARemoteEndpoint_t));
117     if (NULL == info)
118     {
119         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
120         return NULL;
121     }
122     memset(info, 0, sizeof(CARemoteEndpoint_t));
123
124     info->connectivityType = type;
125     if (address && strlen(address))
126     {
127         if (CA_EDR == type)
128         {
129             strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
130             info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
131         }
132         else if (CA_LE == info->connectivityType)
133         {
134             strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
135             info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
136         }
137         else if (CA_WIFI == type || CA_ETHERNET == type)
138         {
139             strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
140             info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
141         }
142     }
143
144     if (resourceUri && strlen(resourceUri))
145     {
146         info->resourceUri = OICStrdup(resourceUri);
147     }
148
149     return info;
150 }
151
152 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
153 {
154     VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
155
156     CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
157                                OICMalloc(sizeof(CARemoteEndpoint_t));
158     if (NULL == info)
159     {
160         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
161         return NULL;
162     }
163     memset(info, 0, sizeof(CARemoteEndpoint_t));
164
165     info->connectivityType = remoteEndpoint->connectivityType;
166     if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
167     {
168         strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
169                 CA_MACADDR_SIZE - 1);
170         info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
171     }
172     else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
173     {
174         strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
175                 CA_MACADDR_SIZE - 1);
176         info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
177     }
178     else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
179              && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
180     {
181         strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
182                 CA_IPADDR_SIZE - 1);
183         info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
184         info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
185     }
186
187     if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
188     {
189         info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
190     }
191
192     info->isSecured = remoteEndpoint->isSecured;
193     return info;
194 }
195
196 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
197 {
198     if (remoteEndpoint)
199     {
200         if (remoteEndpoint->resourceUri)
201         {
202             OICFree(remoteEndpoint->resourceUri);
203         }
204
205         OICFree(remoteEndpoint);
206     }
207 }
208
209 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2,
210                            const char *netMask)
211 {
212     VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
213     VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
214     VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
215
216     int32_t ipList1[8] = {0};
217     int32_t ipList2[8] = {0};
218     int32_t maskList[8] = {0};
219
220     /* Local Loopback Address */
221     if (0 == strncmp(ipAddress1, "127.", 4)
222         || 0 == strncmp(ipAddress2, "127.", 4))
223     {
224         return true;
225     }
226
227     char *ipAdrs1 = strdup(ipAddress1);
228     int16_t index = 0;
229     int16_t lastDotIndex = 0;
230     int16_t ip1TokenCount = 0;
231     while ('\0' != ipAdrs1[index])
232     {
233         if ('.' == ipAdrs1[index])
234         {
235             ipAdrs1[index] = '\0';
236             ipList1[ip1TokenCount++] = atoi(ipAdrs1 + lastDotIndex);
237             lastDotIndex = index + 1;
238         }
239         index++;
240     }
241     // Add last touple
242     ipList1[ip1TokenCount] = atoi(ipAdrs1 + lastDotIndex);
243
244     char *ipAdrs2 = strdup(ipAddress2);
245     index = 0;
246     lastDotIndex = 0;
247     int16_t ip2TokenCount = 0;
248     while ('\0' != ipAdrs2[index])
249     {
250         if ('.' == ipAdrs2[index])
251         {
252             ipAdrs2[index] = '\0';
253             ipList2[ip2TokenCount++] = atoi(ipAdrs2 + lastDotIndex);
254             lastDotIndex = index + 1;
255         }
256         index++;
257     }
258     // Add last touple
259     ipList2[ip2TokenCount] = atoi(ipAdrs2 + lastDotIndex);
260
261     char *nMask = strdup(netMask);
262     index = 0;
263     lastDotIndex = 0;
264     int16_t maskTokenCount = 0;
265     while ('\0' != nMask[index])
266     {
267         if ('.' == nMask[index])
268         {
269             nMask[index] = '\0';
270             maskList[maskTokenCount++] = atoi(nMask + lastDotIndex);
271             lastDotIndex = index + 1;
272         }
273         index++;
274     }
275     // Add last touple
276     maskList[maskTokenCount] = atoi(nMask + lastDotIndex);
277
278     if (ip1TokenCount < 3 || ip2TokenCount < 3 || maskTokenCount < 3)
279     {
280         OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Address or mask is invalid!");
281         OICFree(ipAdrs1);
282         OICFree(ipAdrs2);
283         OICFree(nMask);
284         return false;
285     }
286
287     OICFree(ipAdrs1);
288     OICFree(ipAdrs2);
289     OICFree(nMask);
290
291     if (((ipList1[0]& maskList[0]) == (ipList2[0]& maskList[0]))
292         && ((ipList1[1]& maskList[1]) == (ipList2[1]& maskList[1]))
293         && ((ipList1[2]& maskList[2]) == (ipList2[2]& maskList[2]))
294         && ((ipList1[3]& maskList[3]) == (ipList2[3]& maskList[3])))
295     {
296         return true;
297     }
298     return false;
299 }