1 /* ****************************************************************
3 * Copyright 2015 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 ******************************************************************/
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"
31 * Logging tag for module name.
33 #define TAG "OIC_RM_TM"
36 * Tag for printing the Routing table.
38 #define RM_TAG "OIC_RM_RAP"
40 OCStackResult RTMEndpointInitialize(u_linklist_t **endpointTable)
42 OIC_LOG(DEBUG, TAG, "RTMEndpointInitialize IN");
43 if (NULL == *endpointTable)
45 *endpointTable = u_linklist_create();
46 if (NULL == *endpointTable)
48 OIC_LOG(ERROR, TAG, "Creating Routing Table failed");
49 return OC_STACK_ERROR;
52 OIC_LOG(DEBUG, TAG, "RTMEndpointInitialize OUT");
57 * Freeing every char pointer of endpoint entry here frees the table.
59 OCStackResult RTMFreeEndpointRouteTable(u_linklist_t **endpointTable)
61 OIC_LOG(DEBUG, TAG, "IN");
62 if (NULL == endpointTable || NULL == *endpointTable)
67 u_linklist_iterator_t *iterTable = NULL;
68 u_linklist_init_iterator(*endpointTable, &iterTable);
69 while (NULL != iterTable)
71 RTMEndpointEntry_t *hop = u_linklist_get_data(iterTable);
77 OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable);
78 if (OC_STACK_OK != ret)
80 OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed");
81 return OC_STACK_ERROR;
84 u_linklist_free(endpointTable);
85 OIC_LOG(DEBUG, TAG, "OUT");
90 * Freeing memory first and then Freeing linked list for gateway and endpoint.
92 OCStackResult RTMEndpointTerminate(u_linklist_t **endpointTable)
94 OIC_LOG(DEBUG, TAG, "IN");
96 OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable);
97 if (OC_STACK_OK != ret)
99 OIC_LOG(ERROR, TAG, "Deleting Endpoint Routing Table failed");
101 if (NULL != *endpointTable)
103 *endpointTable = NULL;
105 OIC_LOG(DEBUG, TAG, "OUT");
109 OCStackResult RTMAddEndpointEntry(uint16_t *endpointId, const CAEndpoint_t *destAddr,
110 u_linklist_t **endpointTable)
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)
118 *endpointTable = u_linklist_create();
119 if (NULL == *endpointTable)
121 OIC_LOG(ERROR, TAG, "u_linklist_create failed");
122 return OC_STACK_NO_MEMORY;
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)
131 RTMEndpointEntry_t *entry =
132 (RTMEndpointEntry_t *) u_linklist_get_data(iterTable);
134 if (NULL != entry && (0 == memcmp(destAddr->addr, entry->destIntfAddr.addr,
135 strlen(entry->destIntfAddr.addr)))
136 && destAddr->port == entry->destIntfAddr.port)
138 *endpointId = entry->endpointId;
139 OIC_LOG(ERROR, TAG, "Adding failed as Enpoint Entry Already present in Table");
140 return OC_STACK_DUPLICATE_REQUEST;
142 u_linklist_get_next(&iterTable);
146 RTMEndpointEntry_t *hopEntry = (RTMEndpointEntry_t *)OICCalloc(1, sizeof(RTMEndpointEntry_t));
148 if (NULL == hopEntry)
150 OIC_LOG(ERROR, TAG, "Malloc failed for hop entry");
151 return OC_STACK_ERROR;
154 hopEntry->endpointId = *endpointId;
155 hopEntry->destIntfAddr = *destAddr;
157 OCStackResult ret = u_linklist_add(*endpointTable, (void *)hopEntry);
158 if (OC_STACK_OK != ret)
160 OIC_LOG(ERROR, TAG, "Adding Enpoint Entry to Routing Table failed");
162 return OC_STACK_ERROR;
164 OIC_LOG(DEBUG, TAG, "OUT");
168 OCStackResult RTMRemoveEndpointEntry(uint16_t endpointId, u_linklist_t **endpointTable)
170 OIC_LOG(DEBUG, TAG, "IN");
171 RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable");
172 RM_NULL_CHECK_WITH_RET(*endpointTable, TAG, "*endpointTable");
174 u_linklist_iterator_t *iterTable = NULL;
175 u_linklist_init_iterator(*endpointTable, &iterTable);
176 while (NULL != iterTable)
178 RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable);
179 if (NULL != entry && endpointId == entry->endpointId)
181 OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable);
182 if (OC_STACK_OK != ret)
184 OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed");
185 return OC_STACK_ERROR;
191 u_linklist_get_next(&iterTable);
194 OIC_LOG(DEBUG, TAG, "OUT");
198 OCStackResult RTMRemoveEndpoints(u_linklist_t **endpointTable)
200 OIC_LOG(DEBUG, TAG, "IN");
201 if (NULL == endpointTable || NULL == *endpointTable)
203 OIC_LOG(DEBUG, TAG, "OUT");
207 OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable);
208 if (OC_STACK_OK != ret)
210 OIC_LOG(ERROR, TAG, "Freeing Endpoints failed");
211 return OC_STACK_ERROR;
213 OIC_LOG(DEBUG, TAG, "OUT");
217 CAEndpoint_t *RTMGetEndpointEntry(uint16_t endpointId, const u_linklist_t *endpointTable)
219 OIC_LOG(DEBUG, TAG, "IN");
220 if (NULL == endpointTable)
222 OIC_LOG(ERROR, TAG, "endpointTable is null");
226 u_linklist_iterator_t *iterTable = NULL;
227 u_linklist_init_iterator(endpointTable, &iterTable);
229 while (NULL != iterTable)
231 RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable);
232 if (NULL != entry && (endpointId == entry->endpointId))
234 OIC_LOG(DEBUG, TAG, "OUT");
235 return &(entry->destIntfAddr);
237 u_linklist_get_next(&iterTable);
239 OIC_LOG(DEBUG, TAG, "OUT");
243 void RTMEndpointPrintTable(const u_linklist_t *endpointTable)
245 RM_NULL_CHECK_VOID(endpointTable, TAG, "endpointTable");
247 OIC_LOG(DEBUG, RM_TAG, "=================Endpoint List table============================\n");
248 u_linklist_iterator_t *iterEndpointTable = NULL;
249 u_linklist_init_iterator(endpointTable, &iterEndpointTable);
251 // Iterate over endpoint list to find if already entry for gatewayid is present.
252 while (NULL != iterEndpointTable)
254 RTMEndpointEntry_t *hop =
255 (RTMEndpointEntry_t *) u_linklist_get_data(iterEndpointTable);
258 OIC_LOG(ERROR, RM_TAG, "Printing Table Failed");
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);
264 OIC_LOG(DEBUG, RM_TAG, "********************************************\n");
265 u_linklist_get_next(&iterEndpointTable);