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 "routingutility.h"
24 #include "routingmanager.h"
25 #include "oic_malloc.h"
26 #include "include/logger.h"
29 * Logging tag for module name.
31 #define TAG "OIC_RM_UTIL"
34 * Tag for printing the logs of forwarding the packet.
36 #define RM_TAG "OIC_RM_RAP"
39 * Default routing option data length is 1 byte with any of the following values.
40 * 00 - source and destination address is not present.
41 * 01 - source and destination address is present with message type as ACK.
42 * 10 - source and destination address is present with message type as RESET.
43 * 11 - source and destination address is present with message type as NORMAL.
45 #define DEFAULT_ROUTE_OPTION_LEN 1
48 * Minimum routing option data length is
49 * Msg Type(1 bytes) + length of src address(1byte) + length of destination address(1byte) +
52 #define MIN_ROUTE_OPTION_LEN 5
55 * Acknowledge message type is denoted as 01000000
57 #define ACK_MESSAGE_TYPE (1 << 6)
60 * Reset message type is denoted as 10000000
62 #define RST_MESSAGE_TYPE (1 << 7)
65 * Normal message type is denoted as 11000000
67 #define NORMAL_MESSAGE_TYPE (3 << 6)
72 static OCMode g_rmStackMode = OC_CLIENT;
74 void RMSetStackMode(OCMode mode)
79 // destination and source are <GatewayId><ClientId> here, where ClientId is optional.
80 OCStackResult RMAddInfo(const char *destination, void *message, bool isRequest,
83 RM_NULL_CHECK_WITH_RET(message, TAG, "options");
85 CAHeaderOption_t **options = NULL;
86 uint8_t *numOptions = NULL;
90 CARequestInfo_t *requestMsg = message;
91 options = &(requestMsg->info.options);
92 RM_NULL_CHECK_WITH_RET(options, TAG, "options");
93 numOptions = &(requestMsg->info.numOptions);
94 RM_NULL_CHECK_WITH_RET(numOptions, TAG, "numOptions");
98 CAResponseInfo_t *respMsg = message;
99 if ('\0' == destination[0] && (CA_EMPTY == respMsg->result))
101 OIC_LOG(DEBUG, TAG, "Response is for an Endpoint, No need to add the routing Option");
104 options = &(respMsg->info.options);
105 RM_NULL_CHECK_WITH_RET(options, TAG, "options");
106 numOptions = &(respMsg->info.numOptions);
107 RM_NULL_CHECK_WITH_RET(numOptions, TAG, "numOptions");
111 CAHeaderOption_t *optionPtr = NULL;
114 RMGetRouteOptionIndex(*options, *numOptions, &index);
118 OIC_LOG(INFO, TAG, "Route option is present");
119 optionPtr = *options;
123 OIC_LOG(INFO, TAG, "Route option is not present");
125 optionPtr = OICCalloc((*numOptions + 1), sizeof(CAHeaderOption_t));
128 OIC_LOG(ERROR, TAG, "OICCalloc failed");
129 return OC_STACK_NO_MEMORY;
132 memcpy(optionPtr, *options, sizeof(CAHeaderOption_t) * (*numOptions));
135 OCStackResult res = OC_STACK_OK;
136 RMRouteOption_t routeOption = {.destGw = 0};
137 if (*numOptions != index)
139 OIC_LOG(INFO, TAG, "Route option is already present");
140 res = RMParseRouteOption(&optionPtr[index], &routeOption);
141 if (OC_STACK_OK != res)
143 OIC_LOG(ERROR, TAG, "RMParseRouteOption failed");
144 return OC_STACK_ERROR;
150 CAResponseInfo_t *respMsg = message;
151 if (CA_EMPTY == respMsg->result && CA_MSG_ACKNOWLEDGE == respMsg->info.type)
153 OIC_LOG(DEBUG, TAG, "CA_EMPTY WITH ACKNOWLEDGEMENT");
154 routeOption.msgType = ACK;
155 if (OC_SERVER == g_rmStackMode)
157 OIC_LOG(DEBUG, TAG, "This is server mode");
158 // Send the Empty message in the response with adding the MSGType in Route option.
159 respMsg->info.type = CA_MSG_NONCONFIRM;
160 respMsg->result = CA_CONTENT;
164 OIC_LOG(DEBUG, TAG, "Send a POST request");
171 else if (CA_EMPTY == respMsg->result && CA_MSG_RESET == respMsg->info.type)
173 OIC_LOG(DEBUG, TAG, "CA_EMPTY WITH RESET");
174 routeOption.msgType = RST;
175 respMsg->info.type = CA_MSG_NONCONFIRM;
176 respMsg->result = CA_CONTENT;
182 memcpy(&(routeOption.destGw), destination, sizeof(routeOption.destGw));
183 memcpy(&(routeOption.destEp), destination + sizeof(routeOption.destGw),
184 sizeof(routeOption.destEp));
187 #ifdef ROUTING_GATEWAY
188 // A gateway is supposed to add its ID as source.
189 uint32_t gatewayId = RMGetGatewayId();
192 memcpy(&(routeOption.srcGw), &gatewayId, sizeof(routeOption.srcGw));
195 if(!routeOption.destGw)
197 routeOption.mSeqNum = RMGetMcastSeqNumber();
201 res = RMCreateRouteOption(&routeOption, optionPtr + index);
202 if (OC_STACK_OK != res)
204 OIC_LOG(ERROR, TAG, "Creation of routing option failed");
209 if ((*numOptions) == index )
211 (*numOptions) = (*numOptions) + 1;
213 *options = optionPtr;
219 OCStackResult RMUpdateInfo(CAHeaderOption_t **options, uint8_t *numOptions,
220 CAEndpoint_t *endpoint)
222 RM_NULL_CHECK_WITH_RET(options, TAG, "options");
223 RM_NULL_CHECK_WITH_RET(*options, TAG, "invalid option");
224 RM_NULL_CHECK_WITH_RET(numOptions, TAG, "numOptions");
225 RM_NULL_CHECK_WITH_RET(endpoint, TAG, "endpoint");
227 if (0 >= *numOptions)
229 OIC_LOG(ERROR, TAG, "Invalid arguement: numOptions");
230 return OC_STACK_ERROR;
233 int8_t routeIndex = -1;
234 RMGetRouteOptionIndex(*options, *numOptions, &routeIndex);
236 if (-1 >= routeIndex)
238 OIC_LOG(DEBUG, TAG, "Nothing to remove.");
242 // Update Endpoint with source address from RM header option.
243 if (DEFAULT_ROUTE_OPTION_LEN < (*options + routeIndex)->optionLength )
246 uint16_t count = sizeof(dLen) + DEFAULT_ROUTE_OPTION_LEN;
247 memcpy(&dLen, (*options + routeIndex)->optionData + DEFAULT_ROUTE_OPTION_LEN, sizeof(dLen));
250 memcpy(&sLen, (*options + routeIndex)->optionData + count, sizeof(sLen));
251 count += sizeof(sLen);
254 memcpy(endpoint->routeData, (*options + routeIndex)->optionData + count,
256 OIC_LOG_V(DEBUG, TAG, "adding srcgid: %u in endpoint [%d]",
257 *((uint32_t *)endpoint->routeData), sLen);
259 count += GATEWAY_ID_LENGTH;
261 if (GATEWAY_ID_LENGTH < sLen)
263 memcpy(endpoint->routeData + GATEWAY_ID_LENGTH,
264 (*options + routeIndex)->optionData + count, ENDPOINT_ID_LENGTH);
265 OIC_LOG_V(DEBUG, TAG, "adding srceid: %u in endpoint",
266 *((uint16_t *)(endpoint->routeData + GATEWAY_ID_LENGTH)));
271 // Remove route option from header.
272 for (uint8_t i = routeIndex; i < (*numOptions)-1; i++)
274 memcpy((*options) + i, (*options)+i+1, sizeof(**options));
276 *numOptions = (*numOptions) - 1;
278 if (0 == *numOptions)
280 // Remove route option.
287 void RMGetRouteOptionIndex(const CAHeaderOption_t *options, uint8_t numOptions, int8_t *index)
289 RM_NULL_CHECK_VOID(index, TAG, "index");
293 OIC_LOG(INFO, TAG, "No existing options");
297 for (uint32_t i = 0; i < numOptions; i++)
299 OIC_LOG_V(DEBUG, TAG, "Request- optionID: %u", options[i].optionID);
300 if (RM_OPTION_MESSAGE_SWITCHING == options[i].optionID)
302 OIC_LOG_V(INFO, TAG, "Found Option at %d", i);
309 OCStackResult RMCreateRouteOption(const RMRouteOption_t *optValue, CAHeaderOption_t *options)
311 RM_NULL_CHECK_WITH_RET(optValue, RM_TAG, "optValue");
312 RM_NULL_CHECK_WITH_RET(options, RM_TAG, "options");
314 uint8_t dLen = (optValue->destGw ? GATEWAY_ID_LENGTH:0) +
315 (optValue->destEp ? ENDPOINT_ID_LENGTH:0);
316 uint8_t sLen = (optValue->srcGw ? GATEWAY_ID_LENGTH:0) +
317 (optValue->srcEp ? ENDPOINT_ID_LENGTH:0);
319 OIC_LOG_V(DEBUG, RM_TAG, "createoption dlen %u slen [%u]", dLen, sLen);
321 unsigned int totalLength = 0;
322 uint8_t *tempData = NULL;
324 if (0 == dLen && 0 == sLen)
326 OIC_LOG(DEBUG, RM_TAG, "Source and destination is not present");
327 totalLength = DEFAULT_ROUTE_OPTION_LEN;
328 tempData = OICCalloc(totalLength, sizeof(char));
329 if (NULL == tempData)
331 OIC_LOG(ERROR, RM_TAG, "Calloc failed");
332 return OC_STACK_NO_MEMORY;
335 if (ACK == optValue->msgType)
337 OIC_LOG(DEBUG, RM_TAG, "OptValue ACK Message Type");
338 memset(tempData, ACK_MESSAGE_TYPE, DEFAULT_ROUTE_OPTION_LEN);
340 else if (RST == optValue->msgType)
342 OIC_LOG(DEBUG, RM_TAG, "OptValue RST Message Type");
343 memset(tempData, RST_MESSAGE_TYPE, DEFAULT_ROUTE_OPTION_LEN);
347 OIC_LOG(DEBUG, RM_TAG, "OptValue NOR Message Type");
348 memset(tempData, NORMAL_MESSAGE_TYPE, DEFAULT_ROUTE_OPTION_LEN);
353 totalLength = MIN_ROUTE_OPTION_LEN + dLen + sLen;
354 tempData = OICCalloc(totalLength, sizeof(char));
355 if (NULL == tempData)
357 OIC_LOG(ERROR, RM_TAG, "Calloc failed");
358 return OC_STACK_NO_MEMORY;
361 if (ACK == optValue->msgType)
363 OIC_LOG(DEBUG, RM_TAG, "OptValue ACK Message Type");
364 memset(tempData, ACK_MESSAGE_TYPE, DEFAULT_ROUTE_OPTION_LEN);
366 else if (RST == optValue->msgType)
368 OIC_LOG(DEBUG, RM_TAG, "OptValue RST Message Type");
369 memset(tempData, RST_MESSAGE_TYPE, DEFAULT_ROUTE_OPTION_LEN);
373 OIC_LOG(DEBUG, RM_TAG, "OptValue NOR Message Type");
374 memset(tempData, NORMAL_MESSAGE_TYPE, DEFAULT_ROUTE_OPTION_LEN);
377 memcpy(tempData + DEFAULT_ROUTE_OPTION_LEN, &dLen, sizeof(dLen));
378 unsigned int count = sizeof(dLen) + DEFAULT_ROUTE_OPTION_LEN;
381 if (optValue->destGw)
383 memcpy(tempData + count, &(optValue->destGw), GATEWAY_ID_LENGTH);
384 count += GATEWAY_ID_LENGTH;
387 if (optValue->destEp)
389 memcpy(tempData + count, &(optValue->destEp), ENDPOINT_ID_LENGTH);
390 count += ENDPOINT_ID_LENGTH;
394 memcpy(tempData + count, &sLen, sizeof(sLen));
395 count += sizeof(sLen);
400 memcpy(tempData + count, &(optValue->srcGw), GATEWAY_ID_LENGTH);
401 count += GATEWAY_ID_LENGTH;
406 memcpy(tempData + count, &(optValue->srcEp), ENDPOINT_ID_LENGTH);
407 count += ENDPOINT_ID_LENGTH;
411 memcpy(tempData + count, &optValue->mSeqNum, sizeof(optValue->mSeqNum));
414 memcpy(options->optionData, tempData, totalLength);
416 options->optionID = RM_OPTION_MESSAGE_SWITCHING;
417 options->optionLength = totalLength;
419 OIC_LOG_V(INFO, RM_TAG, "Option Length is %d", options->optionLength);
425 OCStackResult RMParseRouteOption(const CAHeaderOption_t *options, RMRouteOption_t *optValue)
427 RM_NULL_CHECK_WITH_RET(options, RM_TAG, "options");
428 RM_NULL_CHECK_WITH_RET(optValue, RM_TAG, "optValue");
429 if (0 == options->optionLength)
431 OIC_LOG(ERROR, RM_TAG, "Option data is not present");
432 return OC_STACK_ERROR;
435 OIC_LOG_V(DEBUG, RM_TAG, "Option Length is %d", options->optionLength);
437 memcpy(&mType, options->optionData, sizeof(mType));
439 if (ACK_MESSAGE_TYPE == mType)
441 OIC_LOG(INFO, RM_TAG, "ACK_MESSAGE_TYPE");
442 optValue->msgType = ACK;
444 else if (RST_MESSAGE_TYPE == mType)
446 OIC_LOG(INFO, RM_TAG, "RST_MESSAGE_TYPE");
447 optValue->msgType = RST;
449 else if (NORMAL_MESSAGE_TYPE == mType)
451 OIC_LOG(INFO, RM_TAG, "NOR_MESSAGE_TYPE");
452 optValue->msgType = NOR;
455 if (DEFAULT_ROUTE_OPTION_LEN == options->optionLength)
457 OIC_LOG(INFO, RM_TAG, "No source and destination are present");
462 uint16_t count = DEFAULT_ROUTE_OPTION_LEN;
463 memcpy(&dLen, options->optionData + count, sizeof(dLen));
464 count += sizeof(dLen);
467 memcpy(&(optValue->destGw), options->optionData + count, GATEWAY_ID_LENGTH);
468 count += GATEWAY_ID_LENGTH;
470 if (GATEWAY_ID_LENGTH < dLen)
472 memcpy(&(optValue->destEp), options->optionData + count, ENDPOINT_ID_LENGTH);
473 count += ENDPOINT_ID_LENGTH;
478 memcpy(&sLen, options->optionData + count, sizeof(sLen));
479 count += sizeof(sLen);
482 memcpy(&(optValue->srcGw), options->optionData + count, GATEWAY_ID_LENGTH);
483 count += GATEWAY_ID_LENGTH;
485 if (GATEWAY_ID_LENGTH < sLen)
487 memcpy(&(optValue->srcEp), options->optionData + count, ENDPOINT_ID_LENGTH);
488 count += ENDPOINT_ID_LENGTH;
491 memcpy(&optValue->mSeqNum, options->optionData + count, sizeof(optValue->mSeqNum));
494 OIC_LOG_V(INFO, RM_TAG, "Option hopcount is %d", optValue->mSeqNum);
495 OIC_LOG_V(INFO, RM_TAG, "Option Sender Addr is [%u][%u]", optValue->srcGw, optValue->srcEp);
496 OIC_LOG_V(INFO, RM_TAG, "Option Dest Addr is [%u][%u]", optValue->destGw, optValue->destEp);
497 OIC_LOG_V(INFO, RM_TAG, "Message Type is [%u]", optValue->msgType);