NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Application.c
1/************************************************************************************
2* Copyright (C) 2023 *
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* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24#include "main.h"
25#include "Application.h"
26#include "CoAP.h"
27
28void encryptBlock(uint8_t* data, uint32_t* len, uint32_t* key);
29
30/**
31This function is used to initialize the parameter for all the application based on
32the traffic type
33*/
34_declspec(dllexport) int fn_NetSim_Application_Init(struct stru_NetSim_Network *NETWORK_Formal,
35 NetSim_EVENTDETAILS *pstruEventDetails_Formal,
36 char *pszAppPath_Formal,
37 char *pszWritePath_Formal,
38 int nVersion_Type,
39 void **fnPointer)
40{
41 init_application_generation_log();
42 init_application_log();
43 update_latency();
44 return fn_NetSim_Application_Init_F();
45}
46
47/** This function is used to configure the applications like application id, source count, source id, destination count etc. */
48_declspec(dllexport) int fn_NetSim_Application_Configure(void** var)
49{
50 return fn_NetSim_Application_Configure_F(var);
51}
52
53/**
54This function is called by NetworkStack.dll, whenever the event gets triggered
55inside the NetworkStack.dll for applications. This is the main function for the application.
56It processes APPLICATION_OUT, APPLICATION_IN and TIMER events.
57 */
58_declspec (dllexport) int fn_NetSim_Application_Run()
59{
60 switch(pstruEventDetails->nEventType)
61 {
62 case APPLICATION_OUT_EVENT:
63 handle_app_out();
64 break;
65 case APPLICATION_IN_EVENT:
66 {
67 NetSim_PACKET* pstruPacket=pstruEventDetails->pPacket;
68 if(pstruPacket->nPacketType != PacketType_Control && pstruPacket->pstruAppData->nApplicationId &&
69 pstruPacket->nControlDataType/100 != PROTOCOL_APPLICATION)
70 {
71 ptrAPPLICATION_INFO pstruappinfo;
72 fnValidatePacket(pstruPacket);
73 pstruappinfo=applicationInfo[pstruPacket->pstruAppData->nApplicationId-1];
74 pstruPacket->pstruAppData->dEndTime = pstruEventDetails->dEventTime;
75 fn_NetSim_Application_Plot(pstruPacket);
76 appmetrics_dest_add(pstruappinfo, pstruPacket, pstruEventDetails->nDeviceId);
77 print_application_log(pstruappinfo, pstruPacket);
78 if(pstruappinfo->nAppType==TRAFFIC_PEER_TO_PEER && pstruPacket->pstruAppData->nAppEndFlag==1)
79 {
80 fn_NetSim_Application_P2P_MarkReceivedPacket(pstruappinfo,pstruPacket);
81 fn_NetSim_Application_P2P_SendNextPiece(pstruappinfo,get_first_dest_from_packet(pstruPacket),pstruEventDetails->dEventTime);
82 }
83 if(pstruappinfo->nAppType == TRAFFIC_EMULATION && pstruPacket->szPayload)
84 {
85 fn_NetSim_Dispatch_to_emulator(pstruPacket);
86 }
87 if (pstruappinfo->nAppType == TRAFFIC_BSM_APP)
88 {
89 process_saej2735_packet(pstruPacket);
90 }
91 //Delete the packet
92 fn_NetSim_Packet_FreePacket(pstruPacket);
93 }
94 // Here which type is placed is only getting processed next one is not getting processed
95 else if (pstruPacket->nControlDataType == packet_COAP_REQUEST)
96 {
97 ptrAPPLICATION_INFO pstruappinfo;
98 fnValidatePacket(pstruPacket);
99 pstruappinfo = applicationInfo[pstruPacket->pstruAppData->nApplicationId - 1];
100 pstruPacket->pstruAppData->dEndTime = pstruEventDetails->dEventTime;
101 //On receiving COAP Packet
102 appmetrics_dest_add(pstruappinfo, pstruPacket, pstruEventDetails->nDeviceId);
103 fn_NetSim_Application_Plot(pstruPacket);
104 fn_NetSim_Application_COAP_AppIn(pstruappinfo, pstruPacket);
105 //Delete the packet
106 fn_NetSim_Packet_FreePacket(pstruPacket);
107
108 }
109 else if(pstruPacket->nControlDataType == packet_HTTP_REQUEST)
110 {
111 ptrAPPLICATION_INFO pstruappinfo;
112 fnValidatePacket(pstruPacket);
113 pstruappinfo=applicationInfo[pstruPacket->pstruAppData->nApplicationId-1];
114 pstruPacket->pstruAppData->dEndTime = pstruEventDetails->dEventTime;
115 appmetrics_dest_add(pstruappinfo, pstruPacket, pstruEventDetails->nDeviceId);
116 fn_NetSim_Application_HTTP_ProcessRequest(pstruappinfo,pstruPacket);
117 }
118 }
119 break;
120 case TIMER_EVENT:
121 {
122 switch(pstruEventDetails->nSubEventType)
123 {
124 case event_APP_START:
125 //fn_NetSim_Call_LowerLayer(); //Not implemented now
126 switch(pstruEventDetails->pPacket->pstruAppData->nAppType)
127 {
128 case TRAFFIC_DATABASE:
129 case TRAFFIC_FTP:
130 case TRAFFIC_CUSTOM:
131 case TRAFFIC_DATA:
132 case TRAFFIC_VOICE:
133 case TRAFFIC_VIDEO:
134 case TRAFFIC_HTTP:
135 case TRAFFIC_INTERACTIVE_GAMING:
136 case TRAFFIC_EMAIL:
137 case TRAFFIC_CBR:
138 case TRAFFIC_SENSING:
139 case TRAFFIC_BSM_APP:
140 pstruEventDetails->nEventType=APPLICATION_OUT_EVENT;
141 pstruEventDetails->nSubEventType=0;
142 fnpAddEvent(pstruEventDetails);
143 break;
144 case TRAFFIC_COAP:
145 {
146
147 ptrAPPLICATION_INFO appInfo = (ptrAPPLICATION_INFO)pstruEventDetails->szOtherDetails;
148 APP_COAP_INFO* info = appInfo->appData;
149
150 pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;
151 pstruEventDetails->nSubEventType = 0;
152 /* modified code starts*/
153 if (info->pCOAPData->_RETRANSMIT < info->maxRetransmit && info->pCOAPData->Response_Received == 0) {
154
155 fnpAddEvent(pstruEventDetails);
156 info->pCOAPData->Response_Received = 0;
157 info->pCOAPData->RequestACK_Received = 0;
158 double temp = pstruEventDetails->dEventTime; // - (int)info->pageIAT + info->ackTimeOut*MILLISECOND;
159 fn_NetSim_Application_StartCOAPAPP(appInfo, temp);
160 info->pCOAPData->_RETRANSMIT += 1;
161
162
163 }
164 else {
165 info->pCOAPData->_RETRANSMIT = 1;
166 info->pCOAPData->Response_Received = 0;
167
168
169 }
170
171 pstruEventDetails->szOtherDetails = appInfo;
172 }
173 break;
174 case TRAFFIC_ERLANG_CALL:
175 {
176 ptrAPPLICATION_INFO appInfo=(ptrAPPLICATION_INFO)pstruEventDetails->szOtherDetails;
177 pstruEventDetails->nEventType=APPLICATION_OUT_EVENT;
178 pstruEventDetails->nSubEventType=0;
179 fnpAddEvent(pstruEventDetails);
180 pstruEventDetails->szOtherDetails=appInfo;
181 fn_NetSim_Application_ErlangCall_StartCall(pstruEventDetails);
182 }
183 break;
184
185 default:
186 fnNetSimError("Unknown application type %d in application_run.\n",pstruEventDetails->pPacket->pstruAppData->nAppType);
187 break;
188 }
189 break;
190 case event_APP_RESTART:
191 fn_NetSim_App_RestartApplication();
192 break;
193 case event_APP_END:
194 switch(applicationInfo[pstruEventDetails->nApplicationId-1]->nAppType)
195 {
196 case TRAFFIC_ERLANG_CALL:
197 fn_NetSim_Application_ErlangCall_EndCall(pstruEventDetails);
198 break;
199 }
200 break;
201 }
202 }
203 break;
204 default:
205 fnNetSimError("Unknown event type for Application");
206 break;
207 }
208 return 1;
209}
210/**
211This function is called by NetworkStack.dll, while writing the event trace
212to get the sub event as a string.
213*/
214_declspec (dllexport) char *fn_NetSim_Application_Trace(int nSubEvent)
215{
216 switch (nSubEvent)
217 {
218 case event_APP_START:
219 return "APP_START";
220 case event_APP_RESTART:
221 return "APP_RESTART";
222 case event_APP_END:
223 return "APP_END";
224 default:
225 return "APP_UNKNOWN_SUBEVENT";
226 }
227}
228/**
229This function is called by NetworkStack.dll, to free the application information from a packets.
230*/
231_declspec(dllexport) int fn_NetSim_Application_FreePacket(NetSim_PACKET* pstruPacket)
232{
233 return 1;
234}
235/**
236This function is called by NetworkStack.dll, to copy the application
237related information to a new packet
238*/
239_declspec(dllexport) int fn_NetSim_Application_CopyPacket(NetSim_PACKET* pstruDestPacket,NetSim_PACKET* pstruSrcPacket)
240{
241 return 1;
242}
243/**
244This function writes the Application metrics in Metrics.txt
245*/
246_declspec(dllexport) int fn_NetSim_Application_Metrics(PMETRICSWRITER metricsWriter)
247{
248 return fn_NetSim_Application_Metrics_F(metricsWriter);
249}
250
251/**
252This function is used to configure the packet trace
253*/
254_declspec(dllexport) char* fn_NetSim_Application_ConfigPacketTrace()
255{
256 return "";
257}
258/**
259This function is used to write the packet trace
260*/
261_declspec(dllexport) int fn_NetSim_Application_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
262{
263 return 1;
264}
265/**
266 This function is called by NetworkStack.dll, once simulation ends, to free the
267 allocated memory.
268*/
269_declspec(dllexport) int fn_NetSim_Application_Finish()
270{
271 unsigned int loop;
272 for(loop=0;loop<nApplicationCount;loop++)
273 {
274 free(applicationInfo[loop]->sourceList);
275 free(applicationInfo[loop]->destList);
276 if (applicationInfo[loop]->nAppType == TRAFFIC_VIDEO)
277 {
278 APP_VIDEO_INFO* info = applicationInfo[loop]->appData;
279 free(info->dPacketSizeArgs);
280 }
281 free((char*)(applicationInfo[loop]->appData));
282 free_app_metrics(applicationInfo[loop]);
283
284 while(applicationInfo[loop]->socketInfo)
285 LIST_FREE((void**)&applicationInfo[loop]->socketInfo, applicationInfo[loop]->socketInfo);
286 free(applicationInfo[loop]);
287 }
288 close_application_log();
289 close_application_generation_log();
290 free(applicationInfo);
291 return 1;
292}
293
294static PACKET_PRIORITY get_priority_based_on_qos(QUALITY_OF_SERVICE qos)
295{
296 switch (qos)
297 {
298 case QOS_UGS:
299 //case QOS_GBR:
300 return Priority_High;
301 case QOS_rtPS:
302 return Priority_Medium;
303 case QOS_ertPS:
304 return Priority_Normal;
305 case QOS_nrtPS:
306 return Priority_Low;
307 case QOS_BE:
308 //case QOS_NONGBR:
309 return Priority_Low;
310 default:
311 return Priority_Low;
312 }
313}
314/**
315This function is used to generate the packets for application
316*/
317NetSim_PACKET* fn_NetSim_Application_GeneratePacket(ptrAPPLICATION_INFO info,
318 double ldArrivalTime,
319 NETSIM_ID nSourceId,
320 UINT destCount,
321 NETSIM_ID* nDestination,
322 unsigned long long int nPacketId,
323 APPLICATION_TYPE nAppType,
324 QUALITY_OF_SERVICE nQOS,
325 unsigned int sourcePort,
326 unsigned int destPort)
327{
328 NetSim_PACKET* pstruPacket;
329 pstruPacket = fn_NetSim_Packet_CreatePacket(5);
330 pstruPacket->dEventTime = ldArrivalTime;
331
332 add_destlist_to_packet(pstruPacket, nDestination, destCount);
333
334 pstruPacket->nPacketId = nPacketId;
335 pstruPacket->nPacketStatus = PacketStatus_NoError;
336 pstruPacket->nPacketType = fn_NetSim_Application_GetPacketTypeBasedOnApplicationType(nAppType);
337 pstruPacket->pstruAppData->nAppType = nAppType;
338 pstruPacket->nPacketPriority = get_priority_based_on_qos(nQOS);
339 pstruPacket->nQOS = nQOS;
340 pstruPacket->nSourceId = nSourceId;
341 pstruPacket->pstruTransportData->nSourcePort = sourcePort;
342 pstruPacket->pstruTransportData->nDestinationPort = destPort;
343 pstruPacket->pstruAppData->nApplicationProtocol = PROTOCOL_APPLICATION;
344
345 if (NETWORK->ppstruDeviceList[nSourceId - 1]->pstruNetworkLayer && NETWORK->ppstruDeviceList[nSourceId - 1]->pstruNetworkLayer->ipVar)
346 {
347 //Add the source ip address
348 pstruPacket->pstruNetworkData->szSourceIP = IP_COPY(fn_NetSim_Stack_GetFirstIPAddressAsId(nSourceId, 0));
349 //Add the destination IP address
350
351 if (info->nTransmissionType == MULTICAST)
352 {
353 //Multicast
354 pstruPacket->pstruNetworkData->szDestIP = IP_COPY(info->multicastDestIP);
355 }
356 else if (info->nTransmissionType == UNICAST)
357 {
358 //UNICAST
359 pstruPacket->pstruNetworkData->szDestIP = IP_COPY(DNS_QUERY(nSourceId, nDestination[0]));
360 }
361 else if (info->nTransmissionType == BROADCAST)
362 {
363 //BROADCAST
364 NETSIM_IPAddress ip = pstruPacket->pstruNetworkData->szSourceIP;
365 if (ip->type == 4)
366 pstruPacket->pstruNetworkData->szDestIP = STR_TO_IP("255.255.255.255", 4);
367 else
368 pstruPacket->pstruNetworkData->szDestIP = STR_TO_IP("FF00:0:0:0:0:0:0:0", 6);
369 }
370 else
371 {
372 fnNetSimError("Unknown transmission type %d\n", info->nTransmissionType);
373 }
374 }
375 //Set TTL value
376 pstruPacket->pstruNetworkData->nTTL = MAX_TTL;
377 return pstruPacket;
378}
379
380/**
381This function is used to generate the next packet
382*/
383int fn_NetSim_Application_GenerateNextPacket(ptrAPPLICATION_INFO appInfo,
384 NETSIM_ID nSource,
385 UINT destCount,
386 NETSIM_ID* nDestination,
387 double time)
388{
389 if (appInfo->dEndTime <= time)
390 return 0;
391
392 if (appInfo->nAppState != appState_Started)
393 return 0;
394
395 switch (appInfo->nAppType)
396 {
397 case TRAFFIC_FTP:
398 case TRAFFIC_CUSTOM:
399 case TRAFFIC_DATA:
400 case TRAFFIC_CBR:
401 case TRAFFIC_SENSING:
402 {
403 double arrivalTime = 0;
404 double packetSize = 0;
405 fn_NetSim_TrafficGenerator_Custom((APP_DATA_INFO*)appInfo->appData,
406 &packetSize,
407 &arrivalTime,
408 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
409 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]),
410 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
411 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]));
412
413 if (appInfo->dEndTime <= time + arrivalTime)
414 return 0;
415
416 pstruEventDetails->dEventTime = time + arrivalTime;
417 pstruEventDetails->dPacketSize = (double)((int)packetSize);
418 pstruEventDetails->nApplicationId = appInfo->id;
419 pstruEventDetails->nDeviceId = nSource;
420 pstruEventDetails->nDeviceType = DEVICE_TYPE(nSource);
421 pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;
422 pstruEventDetails->nInterfaceId = 0;
423 pstruEventDetails->nProtocolId = PROTOCOL_APPLICATION;
424 pstruEventDetails->nSegmentId = 0;
425 pstruEventDetails->nSubEventType = 0;
426 pstruEventDetails->pPacket =
427 fn_NetSim_Application_GeneratePacket(appInfo,
428 pstruEventDetails->dEventTime,
429 nSource,
430 destCount,
431 nDestination,
432 ++appInfo->nPacketId,
433 appInfo->nAppType,
434 appInfo->qos,
435 appInfo->sourcePort,
436 appInfo->destPort);
437 pstruEventDetails->nPacketId = appInfo->nPacketId;
438 pstruEventDetails->szOtherDetails = appInfo;
439 fnpAddEvent(pstruEventDetails);
440 }
441 break;
442 case TRAFFIC_DATABASE:
443 break;
444 case TRAFFIC_HTTP:
445 break;
446 case TRAFFIC_INTERACTIVE_GAMING:
447 break;
448 case TRAFFIC_VIDEO:
449 {
450 double arrivalTime = 0;
451 double packetSize = 0;
452 fn_NetSim_TrafficGenerator_Video((APP_VIDEO_INFO*)appInfo->appData,
453 &packetSize,
454 &arrivalTime,
455 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
456 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]));
457 if (appInfo->dEndTime <= time + arrivalTime)
458 return 0;
459
460 pstruEventDetails->dEventTime = time + arrivalTime;
461 pstruEventDetails->dPacketSize = (double)((int)packetSize);
462 pstruEventDetails->nApplicationId = appInfo->id;
463 pstruEventDetails->nDeviceId = nSource;
464 pstruEventDetails->nDeviceType = DEVICE_TYPE(nSource);
465 pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;
466 pstruEventDetails->nInterfaceId = 0;
467 pstruEventDetails->nProtocolId = PROTOCOL_APPLICATION;
468 pstruEventDetails->nSegmentId = 0;
469 pstruEventDetails->nSubEventType = 0;
470 pstruEventDetails->pPacket =
471 fn_NetSim_Application_GeneratePacket(appInfo,
472 pstruEventDetails->dEventTime,
473 nSource,
474 destCount,
475 nDestination,
476 ++appInfo->nPacketId,
477 appInfo->nAppType,
478 appInfo->qos,
479 appInfo->sourcePort,
480 appInfo->destPort);
481 pstruEventDetails->nPacketId = appInfo->nPacketId;
482 pstruEventDetails->szOtherDetails = appInfo;
483 fnpAddEvent(pstruEventDetails);
484 }
485 break;
486 case TRAFFIC_VOICE:
487 {
488 double arrivalTime = 0;
489 double packetSize = 0;
490 fn_NetSim_TrafficGenerator_Voice((APP_VOICE_INFO*)appInfo->appData,
491 &packetSize,
492 &arrivalTime,
493 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
494 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]));
495
496 if (appInfo->dEndTime <= time + arrivalTime)
497 return 0;
498
499 pstruEventDetails->dEventTime = time + arrivalTime;
500 pstruEventDetails->dPacketSize = (double)((int)packetSize);
501 pstruEventDetails->nApplicationId = appInfo->id;
502 pstruEventDetails->nDeviceId = nSource;
503 pstruEventDetails->nDeviceType = DEVICE_TYPE(nSource);
504 pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;
505 pstruEventDetails->nInterfaceId = 0;
506 pstruEventDetails->nProtocolId = PROTOCOL_APPLICATION;
507 pstruEventDetails->nSegmentId = 0;
508 pstruEventDetails->nSubEventType = 0;
509 pstruEventDetails->pPacket =
510 fn_NetSim_Application_GeneratePacket(appInfo,
511 pstruEventDetails->dEventTime,
512 nSource,
513 destCount,
514 nDestination,
515 ++appInfo->nPacketId,
516 appInfo->nAppType,
517 appInfo->qos,
518 appInfo->sourcePort,
519 appInfo->destPort);
520 pstruEventDetails->nPacketId = appInfo->nPacketId;
521 pstruEventDetails->szOtherDetails = appInfo;
522 fnpAddEvent(pstruEventDetails);
523 }
524 break;
525 case TRAFFIC_ERLANG_CALL:
526 {
527 APP_CALL_INFO* info = (APP_CALL_INFO*)appInfo->appData;
528 NetSim_PACKET* packet = info->nextPacket[nSource - 1][nDestination[0] - 1];
529 double arrivalTime = 0;
530 double packetSize = 0;
531 if (info->nCallStatus[nSource - 1][nDestination[0] - 1] == 0 || packet == NULL)
532 return 0; //Call is ended
533 pstruEventDetails->dEventTime = packet->dEventTime;
534 pstruEventDetails->dPacketSize = fnGetPacketSize(packet);
535 pstruEventDetails->nApplicationId = appInfo->id;
536 pstruEventDetails->nDeviceId = nSource;
537 pstruEventDetails->nDeviceType = DEVICE_TYPE(nSource);
538 pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;
539 pstruEventDetails->nInterfaceId = 0;
540 pstruEventDetails->nProtocolId = PROTOCOL_APPLICATION;
541 pstruEventDetails->nSegmentId = 0;
542 pstruEventDetails->nSubEventType = 0;
543 pstruEventDetails->pPacket = packet;
544 pstruEventDetails->nPacketId = packet->nPacketId;
545 pstruEventDetails->szOtherDetails = appInfo;
546 if (info->nAppoutevent == NULL)
547 {
548 NETSIM_ID i;
549 info->nAppoutevent = (unsigned long long**)calloc(NETWORK->nDeviceCount, sizeof* info->nAppoutevent);
550 for (i = 0; i < NETWORK->nDeviceCount; i++)
551 info->nAppoutevent[i] = (unsigned long long*)calloc(NETWORK->nDeviceCount, sizeof* info->nAppoutevent[i]);
552 }
553 info->nAppoutevent[nSource - 1][nDestination[0] - 1] = fnpAddEvent(pstruEventDetails);
554 //Generate next packet
555 fn_NetSim_TrafficGenerator_Voice(&(info->VoiceInfo), &packetSize, &arrivalTime,
556 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
557 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]));
558 if (info->dCallEndTime[nSource - 1][nDestination[0] - 1] <= time + arrivalTime)
559 {
560 info->nextPacket[nSource - 1][nDestination[0] - 1] = NULL;
561 packet->pstruAppData->nAppEndFlag = 1;//last packet
562 return 0;
563 }
564 info->nextPacket[nSource - 1][nDestination[0] - 1] =
565 fn_NetSim_Application_GeneratePacket(appInfo,
566 pstruEventDetails->dEventTime + arrivalTime,
567 nSource,
568 destCount,
569 nDestination,
570 ++appInfo->nPacketId,
571 appInfo->nAppType,
572 appInfo->qos,
573 appInfo->sourcePort,
574 appInfo->destPort);
575 info->nextPacket[nSource - 1][nDestination[0] - 1]->pstruAppData->dPacketSize = packetSize;
576 info->nextPacket[nSource - 1][nDestination[0] - 1]->pstruAppData->dPayload = packetSize;
577 }
578 break;
579 case TRAFFIC_BSM_APP:
580 {
581 double arrivalTime = 0;
582 double packetSize = 0;
583 fn_NetSim_Application_BSM((PAPP_BSM_INFO)appInfo->appData,
584 &packetSize,
585 &arrivalTime,
586 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
587 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]),
588 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[0]),
589 &(NETWORK->ppstruDeviceList[nSource - 1]->ulSeed[1]));
590
591 if (appInfo->dEndTime <= time + arrivalTime)
592 return 0;
593
594 pstruEventDetails->dEventTime = time + arrivalTime;
595 pstruEventDetails->dPacketSize = (double)((int)packetSize);
596 pstruEventDetails->nApplicationId = appInfo->id;
597 pstruEventDetails->nDeviceId = nSource;
598 pstruEventDetails->nDeviceType = DEVICE_TYPE(nSource);
599 pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;
600 pstruEventDetails->nInterfaceId = 0;
601 pstruEventDetails->nProtocolId = PROTOCOL_APPLICATION;
602 pstruEventDetails->nSegmentId = 0;
603 pstruEventDetails->nSubEventType = 0;
604 pstruEventDetails->pPacket =
605 fn_NetSim_Application_GeneratePacket(appInfo,
606 pstruEventDetails->dEventTime,
607 nSource,
608 destCount,
609 nDestination,
610 ++appInfo->nPacketId,
611 appInfo->nAppType,
612 appInfo->qos,
613 appInfo->sourcePort,
614 appInfo->destPort);
615 pstruEventDetails->nPacketId = appInfo->nPacketId;
616 pstruEventDetails->szOtherDetails = appInfo;
617 fnpAddEvent(pstruEventDetails);
618 }
619 break;
620 default:
621 fnNetSimError("Unknown application type %d in Generate_next_packet.\n", appInfo->nAppType);
622 break;
623 }
624 return 1;
625}
626
627void copy_payload(UINT8 real[],NetSim_PACKET* packet,unsigned int* payload, ptrAPPLICATION_INFO info)
628{
629 u_short i;
630 uint32_t key = 16;
631 if (payload)
632 {
633 for (i = 0; i < *payload; i++)
634 {
635 if (info->encryption == Encryption_XOR)
636 real[i] = xor_encrypt('a' + i % 26, 16);
637 else
638 real[i] = 'a' + i % 26;
639 }
640 if (info->encryption == Encryption_TEA)
641 encryptBlock(real, payload, &key);
642 else if (info->encryption == Encryption_AES)
643 aes256(real,payload);
644 else if(info->encryption==Encryption_DES)
645 des(real,payload);
646 }
647}
648
649int fn_NetSim_Add_DummyPayload(NetSim_PACKET* packet, ptrAPPLICATION_INFO info)
650{
651 if (add_sae_j2735_payload(packet,info))
652 return 0; // Payload added by SAE J2735 Module
653
654 while(packet)
655 {
656 if(!packet->szPayload &&
657 packet->pstruAppData &&
658 packet->pstruAppData->dPacketSize &&
659 packet->pstruAppData->nAppType != TRAFFIC_EMULATION /* Don't set payload in emulation */ &&
660 wireshark_flag)
661 {
662 unsigned int size=(unsigned int)packet->pstruAppData->dPacketSize;
663 packet->szPayload = (PPACKET_INFO)calloc(1,sizeof* packet->szPayload);
664 copy_payload(packet->szPayload->packet,packet,&size,info);
665 packet->szPayload->packet_len = size;
666 packet->pstruAppData->dPacketSize = (double)size;
667 }
668 packet=packet->pstruNextPacket;
669 }
670 return 0;
671}
672
673double get_random_startupdelay()
674{
675 return RANDOM_STARTUP_DELAY*NETSIM_RAND_01();
676}
677
678void update_latency()
679{
680 for (int i = 0; i < NETWORK->nApplicationCount; i++)
681 {
682 applicationInfo[i]->uplink.prevPacketLatency = -1;
683 applicationInfo[i]->downlink.prevPacketLatency = -1;
684 // Application Generation Logs
685 applicationInfo[i]->ul.lastPacketSize = -1;
686 applicationInfo[i]->ul.lastPacketTime = -1;
687 applicationInfo[i]->ul.prevLastPacketTime = -1;
688 applicationInfo[i]->dl.lastPacketSize = -1;
689 applicationInfo[i]->dl.lastPacketTime = -1;
690 applicationInfo[i]->dl.prevLastPacketTime = -1;
691 }
692}