NetSim Source Code Help
Loading...
Searching...
No Matches
CoAP.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2020 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Siddharth Makadia *
12* *
13* ---------------------------------------------------------------------------------*/
14
15#include "Application.h"
16#include "CoAP.h"
17#include "main.h"
18#include "Stack.h"
19
20#define COAP_REQUEST_SIZE 12
21#define COAP_ACK_SIZE 8
22
23#define COAP_ACT_REQUIRED_DEFAULT TRUE
24#define COAP_MULTICAST_RESPONSE_DEFAULT TRUE
25#define COAP_VARIABLE_RESPONSE_TIME_DEFAULT 5
26#define COAP_PIGGYBACKED_TIME_DEFAULT 2000
27#define COAP_ACK_RESPONSE_TIME_DEFAULT 1000
28
29#define COAP_ACK_TIMEOUT_DEFAULT 2000 //in milliseconds
30#define COAP_ACK_RANDOM_FACTOR_DEFAULT 1.5
31#define COAP_MAX_RETRANSMIT_DEFAULT 4
32#define COAP_NSTART_DEFAULT 1
33#define COAP_DEFAULT_LEISURE_DEFAULT 5 //in seconds
34#define COAP_PROBING_RATE_DEFAULT 1 //in byte/second
35
36
38{
39 char* szVal;
40 APP_COAP_INFO* info = calloc(1, sizeof * info);
41 void* xmlChild;
42 appInfo->appData = info;
43 xmlChild = fn_NetSim_xmlGetChildElement(xmlNetSimNode, "COAP_REQUEST_INTERARRIVAL_TIME", 0);
44 if (xmlChild)
45 {
46 szVal = fn_NetSim_xmlConfig_GetVal(xmlChild, "DISTRIBUTION", 1);
47 if (szVal)
49 free(szVal);
50 szVal = fn_NetSim_xmlConfig_GetVal(xmlChild, "VALUE", 1);
51 if (szVal)
52 info->pageIAT = atof(szVal) * SECOND;
53 free(szVal);
54 }
55 else
56 return 0;
57 xmlChild = fn_NetSim_xmlGetChildElement(xmlNetSimNode, "PAGE_PROPERTY", 0);
58 if (xmlChild)
59 {
60 szVal = fn_NetSim_xmlConfig_GetVal(xmlChild, "PAGE_SIZE", 1);
61 if (szVal)
62 info->pageSize = atof(szVal);
63 free(szVal);
64 szVal = fn_NetSim_xmlConfig_GetVal(xmlChild, "PAGE_SIZE_DISTRIBUTION", 1);
65 if (szVal)
67 free(szVal);
68 }
69
70 xmlChild = fn_NetSim_xmlGetChildElement(xmlNetSimNode, "COAP_PROPERTY", 0);
71
72
73 if (xmlChild) {
74 getXmlVar(&info->ackRequired, ACT_REQUIRED, xmlChild, 1, _BOOL, COAP);
75 getXmlVar(&info->multicastResponse, MULTICAST_RESPONSE, xmlChild, 1, _BOOL, COAP);
76 getXmlVar(&info->variableResponseTime, VARIABLE_RESPONSE_TIME, xmlChild, 1, _INT, COAP);
77 getXmlVar(&info->piggybackedTime, PIGGYBACKED_TIME, xmlChild, 0, _INT, COAP);
78 getXmlVar(&info->ackResponseTime, ACK_RESPONSE_TIME, xmlChild, 0, _INT, COAP);
79 getXmlVar(&info->ackTimeOut, ACK_TIMEOUT, xmlChild, 0, _INT, COAP);
80 getXmlVar(&info->ackRandomFactor, ACK_RANDOM_FACTOR, xmlChild, 0, _DOUBLE, COAP);
81 getXmlVar(&info->maxRetransmit, MAX_RETRANSMIT, xmlChild, 0, _INT, COAP);
82 getXmlVar(&info->nstart, NSTART, xmlChild, 1, _INT, COAP);
83 getXmlVar(&info->defaultLeisure, DEFAULT_LEISURE, xmlChild, 1, _INT, COAP);
84 getXmlVar(&info->probingRate, PROBING_RATE, xmlChild, 1, _INT, COAP);
85
86 COAP_Data* pCOAPData = calloc(1, sizeof * pCOAPData);
87 info->pCOAPData = pCOAPData;
88
91 info->pCOAPData->Request_Received = 0;
93
94 info->pCOAPData->_RETRANSMIT = 0;
95 }
96 return 1;
97}
98
100
101 COAP_Header* pCOAPHeader = pstruPacket->pstruAppData->Packet_AppProtocol;
102 APP_COAP_INFO* info = pstruappinfo->appData;
103 COAP_Data* pCOAPData = info->pCOAPData;
104
105 //check for request Duplicate detection
106 if (pCOAPHeader->Code == REQUEST && pCOAPHeader->Type != ACKNOWLEDGEMENT) {
107 if (pCOAPData->Request_messageID == pCOAPHeader->MessageID && pCOAPData->Request_tokenValue == pCOAPHeader->tokenvalue) {
108 return;
109 }
110 else {
111 pCOAPData->Request_messageID = pCOAPHeader->MessageID;
112 pCOAPData->Request_tokenValue = pCOAPHeader->tokenvalue;
113 }
114 }
115 //check for response Duplicate detection
116 if (pCOAPHeader->Code == RESPONSE) {
117 if (pCOAPData->Response_messageID == pCOAPHeader->MessageID && pCOAPData->Response_tokenValue == pCOAPHeader->tokenvalue) {
118 return;
119 }
120 else {
121 pCOAPData->Response_messageID = pCOAPHeader->MessageID;
122 pCOAPData->Response_tokenValue = pCOAPHeader->tokenvalue;
123 }
124 }
125
126 //check of request
127 if (pCOAPHeader->Code == REQUEST) {
128
129 //check for ack and piggybacked condition
130 if (pCOAPHeader->Type == CONFIRMABLE && info->variableResponseTime > info->piggybackedTime) {
131
132 fn_NetSim_Application_COAP_Sent_ACK(pstruappinfo, pstruPacket);
133
134 }
135 //send response
136 if (pCOAPHeader->Type != ACKNOWLEDGEMENT) {
137
138 fn_NetSim_Application_COAP_ProcessRequest(pstruappinfo, pstruPacket);
139
140 }
141 }
142 //check for Response
143 if (pCOAPHeader->Code == RESPONSE) {
144 //check for Response and send ACK
145 if ((pCOAPHeader->Type == CONFIRMABLE || pCOAPHeader->Type == ACKNOWLEDGEMENT) && pCOAPHeader->Code == RESPONSE) {
146 fn_NetSim_Application_COAP_Sent_ACK(pstruappinfo, pstruPacket);
148 }
149 //Start the next timer event to sent the request packet again after receving the reponse and ACK
151
152 }
153
154
155 //check for Ack Packet
156 if (pCOAPHeader->Type == ACKNOWLEDGEMENT) {
157 if (!strcmp(pstruEventDetails->pPacket->szPacketType, "COAP_REQUEST") || !strcmp(pstruEventDetails->pPacket->szPacketType, "COAP_REQUEST_ACK"))
158 info->pCOAPData->Response_Received = 1;
159
160 }
161
162
163}
164
167 return 1;
168}
169
171
172 if (appInfo->dEndTime <= time)
173 return 0;
174
175 fnCreatePort(appInfo);
176
177 NETSIM_ID nSource = appInfo->sourceList[0];
178 NETSIM_ID nDestination = appInfo->destList[0];
179
180 //Create the socket buffer
181 fnCreateSocketBuffer(appInfo);
182 APP_COAP_INFO* info = appInfo->appData;
183
184 if (appInfo->nTransmissionType == MULTICAST)
185 {
186 fnNetSimError("In NETSIM, COAP application can't be configured as multicast.\n"
187 "Stopping application...\n");
188 return -1;
189 }
190
191 //Generate the app start event
192 fn_NetSim_Application_COAP_Genrate_RequestPacket(appInfo, nSource, nDestination, time, "COAP_REQUEST");
193
194 // Setting COAP application header
196
199 //fnpAddEvent(pstruEventDetails);
200
201
203
204 // Create a new Event for Stop and Wait method
205
206
207 return 0;
208}
209
211{
212 NETSIM_ID nSourceId = get_first_dest_from_packet(pstruPacket);
213 NETSIM_ID nDestinationId = pstruPacket->nSourceId;
214 APP_COAP_INFO* info = pstruappinfo->appData;
215 if (info->multicastResponse == FALSE && pstruappinfo->nTransmissionType == MULTICAST)
216 {
217 return 0;
218 }
219
221
222 //int ackreqired = pCOAPHeader->Type;
223
224
225 fn_NetSim_Application_COAP_Genrate_Packet(pstruappinfo, nSourceId, nDestinationId, 0.0, "COAP_RESPONSE");
226 int messageID = pCOAPHeader->MessageID;
227 int tokenValue = pCOAPHeader->tokenvalue;
228
229 //genrate and modify COAP header
231 //Piggybacked condition check
232 if (info->variableResponseTime < info->piggybackedTime && pCOAPHeaderNew->Type == CONFIRMABLE) {
233 pCOAPHeaderNew->Type = ACKNOWLEDGEMENT;
234 pCOAPHeaderNew->Code = RESPONSE;
235 pCOAPHeaderNew->MessageID = messageID;
236 pCOAPHeaderNew->tokenvalue = tokenValue;
237 }
238 else
239 {
240 //pCOAPHeaderNew->Type = CONFIRMABLE;
241 pCOAPHeaderNew->Code = RESPONSE;
242 pCOAPHeaderNew->MessageID = rand() % 65535;
243 pCOAPHeaderNew->tokenvalue = tokenValue;
244 }
245
249
250 return 1;
251}
252
253
255
256 NETSIM_ID nSourceId = get_first_dest_from_packet(pstruPacket);
257 NETSIM_ID nDestinationId = pstruPacket->nSourceId;
258 APP_COAP_INFO* info = pstruappinfo->appData;
259
261 int messageID = pCOAPHeader->MessageID;
262 //genrate and modify COAP header
264 if (pCOAPHeader->Code == REQUEST) {
265 fn_NetSim_Application_COAP_Genrate_Packet(pstruappinfo, nSourceId, nDestinationId, 0.0, "COAP_REQUEST_ACK");
266 }
267 else {
268 fn_NetSim_Application_COAP_Genrate_Packet(pstruappinfo, nSourceId, nDestinationId, 0.0, "COAP_RESPONSE_ACK");
269 }
270
271 pCOAPHeaderNew->Type = ACKNOWLEDGEMENT;
272 pCOAPHeaderNew->Code = REQUEST;
273 pCOAPHeaderNew->MessageID = messageID;
274 pCOAPHeaderNew->tokenvalue = 0;
275
278
280 return 1;
281}
282
283
284int fn_NetSim_Application_COAP_Genrate_RequestPacket(ptrAPPLICATION_INFO appInfo, NETSIM_ID nSource, NETSIM_ID nDestination, double time, char* PacketType) {
285 double arrivalTime; //interarrival time
286 APP_COAP_INFO* info = appInfo->appData;
287
288 do
289 {
291 &arrivalTime,
292 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
293 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]),
294 &(info->pageIAT));
295 } while (arrivalTime <= 0.0);
296
297 pstruEventDetails->dEventTime = time + arrivalTime;
300 pstruEventDetails->nDeviceId = nDestination;
310 nDestination,
311 1,
312 &nSource,
313 0,
314 appInfo->nAppType,
315 appInfo->qos,
316 appInfo->destPort,
317 appInfo->sourcePort);
320 strcpy(pstruEventDetails->pPacket->szPacketType, PacketType);
321
324 return 1;
325}
326
327int fn_NetSim_Application_COAP_Genrate_Packet(ptrAPPLICATION_INFO pstruappinfo, NETSIM_ID nSourceId, NETSIM_ID nDestinationId, double size, char* PacketType) {
328
329 APP_COAP_INFO* info = pstruappinfo->appData;
330 //COAP_Header* pCOAPHeader = pstruEventDetails->pPacket->pstruAppData->Packet_AppProtocol;
331 if (!strcmp(PacketType, "COAP_RESPONSE")) {
332 do
333 {
335 &(NETWORK->ppstruDeviceList[nSourceId - 1]->ulSeed[0]),
336 &(NETWORK->ppstruDeviceList[nSourceId - 1]->ulSeed[1]),
337 &(info->pageSize));
338 } while (size <= 0.0);
340 }
341 else {
342 double ackSize = COAP_ACK_SIZE;
343 do
344 {
345
347 &(NETWORK->ppstruDeviceList[nSourceId - 1]->ulSeed[0]),
348 &(NETWORK->ppstruDeviceList[nSourceId - 1]->ulSeed[1]),
349 &(ackSize));
350 } while (size <= 0.0);
352 }
353
356 pstruEventDetails->nPacketId = ++pstruappinfo->nPacketId;
362 nSourceId,
363 1,
364 &nDestinationId,
367 pstruappinfo->qos,
368 pstruappinfo->sourcePort,
369 pstruappinfo->destPort);
370 pstruEventDetails->szOtherDetails = pstruappinfo;
373 strcpy(pstruEventDetails->pPacket->szPacketType, PacketType);
376 return 1;
377}
378
380
381 COAP_Header* pCOAPHeader = calloc(1, sizeof * pCOAPHeader);
382 pCOAPHeader->Version = 1;
383 pCOAPHeader->Type = src->Type;
384 pCOAPHeader->TokenLength = src->TokenLength;
385 pCOAPHeader->Code = src->Code;
386 pCOAPHeader->MessageID = src->MessageID;
387 pCOAPHeader->tokenvalue = src->tokenvalue;
388
389 COAP_Options* pCOAPOptions;
390 pCOAPOptions = calloc(1, sizeof * pCOAPOptions);
391 pCOAPOptions->optionDelta = src->Options->optionDelta;
392 pCOAPOptions->optionLength = src->Options->optionLength;
393 pCOAPOptions->optionvalue = src->Options->optionvalue;
394 pCOAPHeader->Options = pCOAPOptions;
395 return pCOAPHeader;
396}
397
399 COAP_Header* pCOAPHeader = calloc(1, sizeof * pCOAPHeader);
400
401 pCOAPHeader->Version = 1;
402 pCOAPHeader->Type = (info->ackRequired == TRUE) ? CONFIRMABLE : NON_CONFIRMABLE;
403 pCOAPHeader->TokenLength = 2;
404 pCOAPHeader->Code = REQUEST;
405 pCOAPHeader->MessageID = rand() % 65535;
406 pCOAPHeader->tokenvalue = rand() % 4294967296;
407
408 COAP_Options* pCOAPOptions;
409 pCOAPOptions = calloc(1, sizeof * pCOAPOptions);
410 pCOAPOptions->optionDelta = 0;
411 pCOAPOptions->optionLength = 0;
412
413 // if required set nCOAPOptions->optionvalue
414
415 pCOAPHeader->Options = pCOAPOptions;
416
417 return pCOAPHeader;
418}
unsigned int NETSIM_ID
Definition: Animation.h:45
NetSim_PACKET * fn_NetSim_Application_GeneratePacket(ptrAPPLICATION_INFO info, double ldArrivalTime, NETSIM_ID nSourceId, UINT destCount, NETSIM_ID *nDestination, unsigned long long int nPacketId, APPLICATION_TYPE nAppType, QUALITY_OF_SERVICE nQOS, unsigned int sourcePort, unsigned int destPort)
Definition: Application.c:311
@ packet_COAP_REQUEST
Definition: Application.h:72
int fnCreateSocketBuffer(ptrAPPLICATION_INFO appInfo)
int fnDistribution(DISTRIBUTION nDistributionType, double *fDistOut, unsigned long *uSeed, unsigned long *uSeed1, double *args)
Definition: Distribution.c:17
void fnCreatePort(ptrAPPLICATION_INFO info)
@ event_APP_START
Definition: Application.h:54
#define COAP_REQUEST_SIZE
Definition: CoAP.c:20
int fn_NetSim_Application_COAP_Sent_ACK(ptrAPPLICATION_INFO pstruappinfo, NetSim_PACKET *pstruPacket)
Definition: CoAP.c:254
COAP_Header * fn_NetSim_Application_COAP_CopyHeader(COAP_Header *src)
Definition: CoAP.c:379
int fn_NetSim_Application_COAP_ProcessRequest(ptrAPPLICATION_INFO pstruappinfo, NetSim_PACKET *pstruPacket)
Definition: CoAP.c:210
int fn_NetSim_Application_StartCOAPAPP(ptrAPPLICATION_INFO appInfo, double time)
Definition: CoAP.c:170
int fn_NetSim_Application_COAP_Genrate_RequestPacket(ptrAPPLICATION_INFO appInfo, NETSIM_ID nSource, NETSIM_ID nDestination, double time, char *PacketType)
Definition: CoAP.c:284
COAP_Header * fn_NetSim_Application_COAP_GenrateHeader(APP_COAP_INFO *info)
Definition: CoAP.c:398
#define COAP_ACK_SIZE
Definition: CoAP.c:21
int fn_NetSim_Application_ConfigureCOAPTraffic(ptrAPPLICATION_INFO appInfo, void *xmlNetSimNode)
Definition: CoAP.c:37
int fn_NetSim_Application_COAP_Genrate_Packet(ptrAPPLICATION_INFO pstruappinfo, NETSIM_ID nSourceId, NETSIM_ID nDestinationId, double size, char *PacketType)
Definition: CoAP.c:327
int fn_NetSim_Application_COAP_Start(ptrAPPLICATION_INFO appInfo, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: CoAP.c:165
void fn_NetSim_Application_COAP_AppIn(ptrAPPLICATION_INFO pstruappinfo, NetSim_PACKET *pstruPacket)
Definition: CoAP.c:99
@ NON_CONFIRMABLE
Definition: CoAP.h:56
@ ACKNOWLEDGEMENT
Definition: CoAP.h:57
@ CONFIRMABLE
Definition: CoAP.h:55
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define TRUE
Definition: Linux.h:77
#define FALSE
Definition: Linux.h:76
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
@ PacketType_COAP
Definition: Packet.h:65
@ Segment_Unfragment
Definition: Packet.h:115
NETSIM_ID get_first_dest_from_packet(NetSim_PACKET *packet)
@ TRAFFIC_COAP
Definition: Packet.h:155
@ REQUEST
Definition: RIP.h:60
@ RESPONSE
Definition: RIP.h:61
#define PROTOCOL_APPLICATION
Definition: Stack.h:143
char * fn_NetSim_xmlConfig_GetVal(void *xmlNetSimNode, const char *szName, int flag)
#define getXmlVar(var, name, xmlNode, flag, type, protocol)
Definition: Stack.h:1046
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
@ _BOOL
Definition: Stack.h:1034
@ _INT
Definition: Stack.h:1030
@ _DOUBLE
Definition: Stack.h:1031
DISTRIBUTION fn_NetSim_Config_GetDistribution(const char *szDistribution)
#define MILLISECOND
Definition: Stack.h:59
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
#define SECOND
Definition: Stack.h:60
@ TIMER_EVENT
Definition: Stack.h:114
@ APPLICATION_OUT_EVENT
Definition: Stack.h:112
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
@ MULTICAST
Definition: Stack.h:365
void * fn_NetSim_xmlGetChildElement(void *xmlNetSimNode, const char *childName, int count)
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
Structure to store the COAP application information.
Definition: Application.h:253
DISTRIBUTION pageIATDistribution
Definition: Application.h:255
struct stru_NetSim_COAP_data * pCOAPData
Definition: Application.h:270
DISTRIBUTION pageSizeDistribution
Definition: Application.h:257
Structure to store application information.
Definition: Application.h:97
NETSIM_ID * destList
Definition: Application.h:106
APPLICATION_TYPE nAppType
Definition: Application.h:102
TRANSMISSION_TYPE nTransmissionType
Definition: Application.h:101
unsigned long long int nPacketId
Definition: Application.h:116
QUALITY_OF_SERVICE qos
Definition: Application.h:111
NETSIM_ID * sourceList
Definition: Application.h:105
unsigned int Version
Definition: CoAP.h:91
unsigned int tokenvalue
Definition: CoAP.h:96
unsigned int Code
Definition: CoAP.h:94
unsigned int MessageID
Definition: CoAP.h:95
struct stru_NetSim_COAP_Options * Options
Definition: CoAP.h:97
unsigned int Type
Definition: CoAP.h:92
unsigned int TokenLength
Definition: CoAP.h:93
unsigned int optionLength
Definition: CoAP.h:86
unsigned int optionDelta
Definition: CoAP.h:85
unsigned int Response_tokenValue
Definition: CoAP.h:73
unsigned int Request_tokenValue
Definition: CoAP.h:71
unsigned int Request_messageID
Definition: CoAP.h:70
unsigned int Response_Received
Definition: CoAP.h:75
unsigned int RequestACK_Received
Definition: CoAP.h:76
unsigned int Request_Received
Definition: CoAP.h:77
unsigned int ResponseACK_Received
Definition: CoAP.h:78
unsigned int Response_messageID
Definition: CoAP.h:72
unsigned long ulSeed[SEED_COUNT]
Definition: Stack.h:715
NETSIM_ID nApplicationId
Definition: Stack.h:752
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
long long int nPacketId
Definition: Stack.h:755
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
NETSIM_ID nInterfaceId
Definition: Stack.h:751
struct stru_NetSim_Device ** ppstruDeviceList
Definition: Stack.h:737
PACKET_FRAGMENT nPacketFragment
Definition: Packet.h:163
APPLICATION_LAYER_PROTOCOL nApplicationProtocol
Definition: Packet.h:172
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
char szPacketType[MAX_PACKET_TYPE_LEN]
Definition: Packet.h:271
unsigned int nControlDataType
Definition: Packet.h:258
PACKET_TYPE nPacketType
Definition: Packet.h:257
NETSIM_ID nSourceId
Definition: Packet.h:263