Merge branch 'tizen' into tizen_5.5
[platform/upstream/iotivity.git] / resource / csdk / routing / src / routingtablemanager_endpoint.c
1 /* ****************************************************************
2  *
3  * Copyright 2015 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 <stdio.h>
22 #include <string.h>
23 #include "routingtablemanager_endpoint.h"
24 #include "routingtablemanager.h"
25 #include "routingutility.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
28 #include "include/logger.h"
29
30 /**
31  * Logging tag for module name.
32  */
33 #define TAG "OIC_RM_TM"
34
35 /**
36  * Tag for printing the Routing table.
37  */
38 #define RM_TAG "OIC_RM_RAP"
39
40 OCStackResult RTMEndpointInitialize(u_linklist_t **endpointTable)
41 {
42     OIC_LOG(DEBUG, TAG, "RTMEndpointInitialize IN");
43     if (NULL == *endpointTable)
44     {
45         *endpointTable = u_linklist_create();
46         if (NULL == *endpointTable)
47         {
48            OIC_LOG(ERROR, TAG, "Creating Routing Table failed");
49            return OC_STACK_ERROR;
50         }
51     }
52     OIC_LOG(DEBUG, TAG, "RTMEndpointInitialize OUT");
53     return OC_STACK_OK;
54 }
55
56 /*
57  * Freeing every char pointer of endpoint entry here frees the table.
58  */
59 OCStackResult RTMFreeEndpointRouteTable(u_linklist_t **endpointTable)
60 {
61     OIC_LOG(DEBUG, TAG, "IN");
62     if (NULL == endpointTable || NULL == *endpointTable)
63     {
64         return OC_STACK_OK;
65     }
66
67     u_linklist_iterator_t *iterTable = NULL;
68     u_linklist_init_iterator(*endpointTable, &iterTable);
69     while (NULL != iterTable)
70     {
71         RTMEndpointEntry_t *hop = u_linklist_get_data(iterTable);
72         if (NULL != hop)
73         {
74             OICFree(hop);
75         }
76
77         OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable);
78         if (OC_STACK_OK != ret)
79         {
80             OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed");
81             return OC_STACK_ERROR;
82         }
83     }
84     u_linklist_free(endpointTable);
85     OIC_LOG(DEBUG, TAG, "OUT");
86     return OC_STACK_OK;
87 }
88
89 /*
90  * Freeing memory first and then Freeing linked list for gateway and endpoint.
91  */
92 OCStackResult RTMEndpointTerminate(u_linklist_t **endpointTable)
93 {
94     OIC_LOG(DEBUG, TAG, "IN");
95
96     OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable);
97     if (OC_STACK_OK != ret)
98     {
99         OIC_LOG(ERROR, TAG, "Deleting Endpoint Routing Table failed");
100     }
101     if (NULL != *endpointTable)
102     {
103         *endpointTable = NULL;
104     }
105     OIC_LOG(DEBUG, TAG, "OUT");
106     return OC_STACK_OK;
107 }
108
109 OCStackResult RTMAddEndpointEntry(uint16_t *endpointId, const CAEndpoint_t *destAddr,
110                                   u_linklist_t **endpointTable)
111 {
112     OIC_LOG(DEBUG, TAG, "IN");
113     RM_NULL_CHECK_WITH_RET(endpointId, TAG, "endpointId");
114     RM_NULL_CHECK_WITH_RET(destAddr, TAG, "destAddr");
115     RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable");
116     if (NULL == *endpointTable)
117     {
118         *endpointTable = u_linklist_create();
119         if (NULL == *endpointTable)
120         {
121             OIC_LOG(ERROR, TAG, "u_linklist_create failed");
122             return OC_STACK_NO_MEMORY;
123         }
124     }
125
126     u_linklist_iterator_t *iterTable = NULL;
127     u_linklist_init_iterator(*endpointTable, &iterTable);
128     // Iterate over gateway list to find if already entry with this gatewayid is present.
129     while (NULL != iterTable)
130     {
131         RTMEndpointEntry_t *entry =
132             (RTMEndpointEntry_t *) u_linklist_get_data(iterTable);
133
134         if (NULL != entry && (0 == memcmp(destAddr->addr, entry->destIntfAddr.addr,
135                               strlen(entry->destIntfAddr.addr)))
136             && destAddr->port == entry->destIntfAddr.port)
137         {
138             *endpointId = entry->endpointId;
139             OIC_LOG(ERROR, TAG, "Adding failed as Enpoint Entry Already present in Table");
140             return OC_STACK_DUPLICATE_REQUEST;
141         }
142         u_linklist_get_next(&iterTable);
143     }
144
145     // Filling Entry.
146     RTMEndpointEntry_t *hopEntry = (RTMEndpointEntry_t *)OICCalloc(1, sizeof(RTMEndpointEntry_t));
147
148     if (NULL == hopEntry)
149     {
150        OIC_LOG(ERROR, TAG, "Malloc failed for hop entry");
151        return OC_STACK_ERROR;
152     }
153
154     hopEntry->endpointId = *endpointId;
155     hopEntry->destIntfAddr = *destAddr;
156
157     OCStackResult ret = u_linklist_add(*endpointTable, (void *)hopEntry);
158     if (OC_STACK_OK != ret)
159     {
160        OIC_LOG(ERROR, TAG, "Adding Enpoint Entry to Routing Table failed");
161        OICFree(hopEntry);
162        return OC_STACK_ERROR;
163     }
164     OIC_LOG(DEBUG, TAG, "OUT");
165     return OC_STACK_OK;
166 }
167
168 OCStackResult RTMRemoveEndpointEntry(uint16_t endpointId, u_linklist_t **endpointTable)
169 {
170     OIC_LOG(DEBUG, TAG, "IN");
171     RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable");
172     RM_NULL_CHECK_WITH_RET(*endpointTable, TAG, "*endpointTable");
173
174     u_linklist_iterator_t *iterTable = NULL;
175     u_linklist_init_iterator(*endpointTable, &iterTable);
176     while (NULL != iterTable)
177     {
178         RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable);
179         if (NULL !=  entry && endpointId == entry->endpointId)
180         {
181             OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable);
182             if (OC_STACK_OK != ret)
183             {
184                OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed");
185                return OC_STACK_ERROR;
186             }
187             OICFree(entry);
188         }
189         else
190         {
191             u_linklist_get_next(&iterTable);
192         }
193     }
194     OIC_LOG(DEBUG, TAG, "OUT");
195     return OC_STACK_OK;
196 }
197
198 OCStackResult RTMRemoveEndpoints(u_linklist_t **endpointTable)
199 {
200     OIC_LOG(DEBUG, TAG, "IN");
201     if (NULL == endpointTable || NULL == *endpointTable)
202     {
203         OIC_LOG(DEBUG, TAG, "OUT");
204         return OC_STACK_OK;
205     }
206
207     OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable);
208     if (OC_STACK_OK != ret)
209     {
210         OIC_LOG(ERROR, TAG, "Freeing Endpoints failed");
211         return OC_STACK_ERROR;
212     }
213     OIC_LOG(DEBUG, TAG, "OUT");
214     return OC_STACK_OK;
215 }
216
217 CAEndpoint_t *RTMGetEndpointEntry(uint16_t endpointId, const u_linklist_t *endpointTable)
218 {
219     OIC_LOG(DEBUG, TAG, "IN");
220     if (NULL == endpointTable)
221     {
222         OIC_LOG(ERROR, TAG, "endpointTable is null");
223         return NULL;
224     }
225
226     u_linklist_iterator_t *iterTable = NULL;
227     u_linklist_init_iterator(endpointTable, &iterTable);
228
229     while (NULL != iterTable)
230     {
231         RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable);
232         if (NULL != entry && (endpointId == entry->endpointId))
233         {
234             OIC_LOG(DEBUG, TAG, "OUT");
235             return &(entry->destIntfAddr);
236         }
237         u_linklist_get_next(&iterTable);
238     }
239     OIC_LOG(DEBUG, TAG, "OUT");
240     return NULL;
241 }
242
243 void RTMEndpointPrintTable(const u_linklist_t *endpointTable)
244 {
245     RM_NULL_CHECK_VOID(endpointTable, TAG, "endpointTable");
246
247     OIC_LOG(DEBUG, RM_TAG, "=================Endpoint List table============================\n");
248     u_linklist_iterator_t *iterEndpointTable = NULL;
249     u_linklist_init_iterator(endpointTable, &iterEndpointTable);
250
251     // Iterate over endpoint list to find if already entry for gatewayid is present.
252     while (NULL != iterEndpointTable)
253     {
254         RTMEndpointEntry_t *hop =
255             (RTMEndpointEntry_t *) u_linklist_get_data(iterEndpointTable);
256         if (NULL == hop)
257         {
258             OIC_LOG(ERROR, RM_TAG, "Printing Table Failed");
259             return;
260         }
261         OIC_LOG_V(DEBUG, RM_TAG, "EndpointId : %u\naddr : %s Port : %d Flags : %d",
262                   hop->endpointId, hop->destIntfAddr.addr, hop->destIntfAddr.port, hop->destIntfAddr.flags);
263
264         OIC_LOG(DEBUG, RM_TAG, "********************************************\n");
265         u_linklist_get_next(&iterEndpointTable);
266     }
267 }