NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
APP_OUT.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"
26static FILE* fpGenerationLogs = NULL;
27
28static void add_app_info_to_packet(NetSim_PACKET* packet, double time, double size, UINT appId)
29{
30 packet->pstruAppData->dArrivalTime = time;
31 packet->pstruAppData->dEndTime = time;
32 if (packet->nPacketType != PacketType_Control)
33 {
34 packet->pstruAppData->dOverhead = 0;
35 packet->pstruAppData->dPayload = size;
36 }
37 else
38 {
39 packet->pstruAppData->dPayload = 0;
40 packet->pstruAppData->dOverhead = size;
41 }
42 packet->pstruAppData->dPacketSize = packet->pstruAppData->dOverhead + packet->pstruAppData->dPayload;
43 packet->pstruAppData->dStartTime = ldEventTime;
44 packet->pstruAppData->nApplicationId = appId;
45}
46
47static void set_app_end_and_generate_next_packet(NetSim_PACKET* pstruPacket, ptrAPPLICATION_INFO appInfo,
48 UINT destCount, NETSIM_ID* dest)
49{
50 APPLICATION_TYPE nappType = pstruPacket->pstruAppData->nAppType;
51
52 if (nappType == TRAFFIC_FTP || nappType == TRAFFIC_DATABASE)
53 {
54 NetSim_PACKET* packet = pstruPacket;
55 while (packet->pstruNextPacket)
56 packet = packet->pstruNextPacket;
57 packet->pstruAppData->nAppEndFlag = 1;
58 fn_NetSim_Application_StartDataAPP(appInfo, pstruEventDetails->dEventTime);
59 }
60 else if (nappType == TRAFFIC_HTTP)
61 {
62 //Do nothing
63 NetSim_PACKET* packet = pstruPacket;
64 if (pstruPacket->pstruAppData->nAppEndFlag)
65 {
66 while (packet->pstruNextPacket)
67 {
68 packet->pstruAppData->nAppEndFlag = 0;
69 packet = packet->pstruNextPacket;
70 }
71 packet->pstruAppData->nAppEndFlag = 1;
72 }
73 }
74 else if (nappType == TRAFFIC_INTERACTIVE_GAMING)
75 {
76 if (get_first_dest_from_packet(pstruPacket) == appInfo->sourceList[0])
77 {
78 strcpy(pstruPacket->szPacketType, appInfo->name);
79 strcat(pstruPacket->szPacketType, "_UL");
80 fn_NetSim_Application_InteractiveGamingUL_GenerateNextPacket(appInfo, pstruPacket, ldEventTime);
81 }
82 else
83 {
84 strcpy(pstruPacket->szPacketType, appInfo->name);
85 strcat(pstruPacket->szPacketType, "_DL");
86 fn_NetSim_Application_InteractiveGamingDL_GenerateNextPacket(appInfo, pstruPacket, ldEventTime);
87 }
88 }
89 else if (nappType == TRAFFIC_COAP)
90 {
91 //DO nothing
92 }
93 else if (nappType == TRAFFIC_EMAIL)
94 {
95 appInfo = fn_NetSim_Application_Email_GenerateNextPacket((DETAIL*)appInfo,
96 pstruPacket->nSourceId,
97 get_first_dest_from_packet(pstruPacket),
98 pstruEventDetails->dEventTime);
99 }
100 else if (nappType == TRAFFIC_PEER_TO_PEER)
101 {
102 NetSim_PACKET* packet = pstruPacket;
103 while (packet->pstruNextPacket)
104 packet = packet->pstruNextPacket;
105 packet->pstruAppData->nAppEndFlag = 1;
106 }
107 else if (nappType == TRAFFIC_EMULATION)
108 {
109 //do nothing
110 }
111 else
112 {
113 fn_NetSim_Application_GenerateNextPacket(appInfo,
114 pstruPacket->nSourceId,
115 destCount,
116 dest,
117 pstruEventDetails->dEventTime);
118 }
119}
120
121static void send_to_transport_layer(ptrSOCKETINTERFACE s, ptrAPPLICATION_INFO appInfo,
122 NetSim_PACKET* packet, NETSIM_ID nDeviceId)
123{
124 packet->pstruTransportData->nTransportProtocol = appInfo->trxProtocol;
125 if (!fn_NetSim_Socket_GetBufferStatus(s))
126 {
127 //Socket buffer is NULL
128 //Create event for transport out
129 pstruEventDetails->dEventTime = ldEventTime;
130 pstruEventDetails->nEventType = TRANSPORT_OUT_EVENT;
131 pstruEventDetails->pPacket = NULL;
132 pstruEventDetails->nProtocolId = appInfo->trxProtocol;
133 pstruEventDetails->nSubEventType = 0;
134#pragma warning(disable:4312)
135 pstruEventDetails->szOtherDetails = (void*)s;
136#pragma warning(default:4312)
137 //Add Transport out event
138 fnpAddEvent(pstruEventDetails);
139 }
140
141 //Place the packet to socket buffer
142 fn_NetSim_Socket_PassPacketToInterface(nDeviceId, packet, s);
143}
144
145static void send_to_next_protocol(ptrSOCKETINTERFACE s, ptrAPPLICATION_INFO appInfo,
146 NetSim_PACKET* packet, NETSIM_ID nDeviceId)
147{
148 NetSim_EVENTDETAILS pevent;
149 while (packet)
150 {
151 memset(&pevent, 0, sizeof pevent);
152 pevent.dEventTime = ldEventTime;
153 pevent.dPacketSize = packet->pstruAppData->dPacketSize;
154 pevent.nApplicationId = packet->pstruAppData->nApplicationId;
155 pevent.nDeviceId = nDeviceId;
156 pevent.nDeviceType = DEVICE_TYPE(nDeviceId);
157 pevent.nEventType = APPLICATION_OUT_EVENT;
158 pevent.nPacketId = packet->nPacketId;
159 pevent.nProtocolId = appInfo->protocol;
160 pevent.nSegmentId = packet->pstruAppData->nSegmentId;
161 pevent.pPacket = packet;
162 pevent.szOtherDetails = (void*)s;
163 packet = packet->pstruNextPacket;
164 pevent.pPacket->pstruNextPacket = NULL;
165 fnpAddEvent(&pevent);
166 }
167}
168
169static void appout_send_packet(ptrSOCKETINTERFACE s, ptrAPPLICATION_INFO appInfo,
170 NetSim_PACKET* packet, NETSIM_ID nDeviceId)
171{
172 if (appInfo->protocol == APP_PROTOCOL_NULL)
173 send_to_transport_layer(s, appInfo, packet, nDeviceId);
174 else
175 send_to_next_protocol(s, appInfo, packet, nDeviceId);
176}
177
178static ptrAPPLICATION_INFO get_app_info(void* detail, APPLICATION_TYPE type)
179{
180 if (type == TRAFFIC_EMAIL)
181 return get_email_app_info(detail);
182
183 return detail;
184}
185
186void handle_app_out()
187{
188 void* otherDetails = pstruEventDetails->szOtherDetails;
189 double ldEventTime = pstruEventDetails->dEventTime;
190 NETSIM_ID nDeviceId = pstruEventDetails->nDeviceId;
191
192 NetSim_PACKET* pstruPacket = pstruEventDetails->pPacket;
193 if (!pstruPacket) return; //No packet to send
194
195 APPLICATION_TYPE nappType = pstruPacket->pstruAppData->nAppType;
196
197 UINT destCount;
198 NETSIM_ID* dest = get_dest_from_packet(pstruPacket, &destCount);
199
200 ptrSOCKETINTERFACE s;
201 s = fnGetSocket(pstruEventDetails->nApplicationId,
202 pstruPacket->nSourceId,
203 pstruPacket->pstruTransportData->nSourcePort,
204 pstruPacket->pstruTransportData->nDestinationPort);
205 if (!s)
206 {
207 fnNetSimError("Socket is NULL for application %d.\n",
208 pstruEventDetails->nApplicationId);
209 return;
210 }
211
212 //Initialize the application data
213 add_app_info_to_packet(pstruPacket, ldEventTime,
214 pstruEventDetails->dPacketSize, pstruEventDetails->nApplicationId);
215
216
217
218
219
220 ptrAPPLICATION_INFO appInfo = get_app_info(pstruEventDetails->szOtherDetails, nappType);
221 if (!appInfo)
222 {
223 fnNetSimError("Application info is NULL in application out event id = %lld\n", pstruEventDetails->nEventId);
224 return;
225 }
226
227 pstruPacket->pstruTransportData->nTransportProtocol = appInfo->trxProtocol;
228
229 //Fragment the packet
230 int nSegmentCount = 0;
231 double segmentsize = fn_NetSim_Stack_GetMSS(pstruPacket);
232 print_application_generation_log(appInfo, pstruPacket);
233 nSegmentCount = fn_NetSim_Stack_FragmentPacket(pstruPacket, (int)fn_NetSim_Stack_GetMSS(pstruPacket));
234
235
236 //Add the dummy payload to packet
237 fn_NetSim_Add_DummyPayload(pstruPacket, appInfo);
238
239 appmetrics_src_add(appInfo, pstruPacket);
240
241 appout_send_packet(s, appInfo, pstruPacket, nDeviceId);
242
243 set_app_end_and_generate_next_packet(pstruPacket, otherDetails, destCount, dest);
244
245}
246
247
248void init_application_generation_log()
249{
250 if (get_protocol_log_status("APPLICATION_GENERATION_LOG"))
251 {
252 char s[BUFSIZ];
253 sprintf(s, "%s\\%s", pszIOLogPath, "Application_Generation_Log.csv");
254 fpGenerationLogs = fopen(s, "w");
255 if (!fpGenerationLogs)
256 {
257 fnSystemError("Unable to open %s file", s);
258 perror(s);
259 }
260 else
261 {
262 fprintf(fpGenerationLogs, "%s,%s,%s,%s,%s,%s,%s,",
263 "Time(ms)","Application Name", "Application ID", "Source", "Destination", "Packet ID", "Generation Rate(Mbps)");
264
265 fprintf(fpGenerationLogs, "\n");
266 }
267 }
268}
269
270
271void print_application_generation_log(ptrAPPLICATION_INFO pstruappinfo, NetSim_PACKET* packet)
272{
273 if (fpGenerationLogs == NULL)
274 return;
275 double dPacketSize = packet->pstruAppData->dPacketSize;
276 NETSIM_ID nDestinationId = get_first_dest_from_packet(packet);
277 double generationRate = calculate_generation_rate(pstruappinfo, packet);
278 char DestinationName[BUFSIZ];
279 char Appname[BUFSIZ];
280 strcpy(Appname, pstruappinfo->name);
281
282 if (pstruappinfo->nAppType == TRAFFIC_EMAIL || pstruappinfo->nAppType == TRAFFIC_INTERACTIVE_GAMING || pstruappinfo->nAppType == TRAFFIC_HTTP)
283 {
284 if (get_first_dest_from_packet(packet) == pstruappinfo->sourceList[0]) // UL
285 {
286 strcat(Appname, "_Send"); // Send
287 }
288 else
289 strcat(Appname, "_Receive"); // Receive
290 }
291
292 if (nDestinationId)
293 strcpy(DestinationName, DEVICE_NAME(nDestinationId));
294 else
295 strcpy(DestinationName, "BROADCAST");
296
297 fprintf(fpGenerationLogs, "%lf,%s,%d,%s,",
298 ldEventTime/MILLISECOND,Appname, pstruappinfo->nConfigId, DEVICE_NAME(packet->nSourceId));
299
300 fprintf(fpGenerationLogs, "%s,%lld,%lf,",
301 DestinationName, packet->nPacketId, generationRate);
302
303 fprintf(fpGenerationLogs, "\n");
304}
305
306void close_application_generation_log()
307{
308 if (fpGenerationLogs)
309 fclose(fpGenerationLogs);
310}
311
312double calculate_generation_rate(ptrAPPLICATION_INFO pstruappinfo, NetSim_PACKET* packet)
313{
314 double genRate = -1;
315 double dPacketSize = packet->pstruAppData->dPacketSize;
316
317 if (get_first_dest_from_packet(packet) == pstruappinfo->sourceList[0]) //Sent
318 {
319 if (pstruappinfo->dl.lastPacketTime == -1 && ldEventTime == 0)
320 {
321 genRate = 0;
322 pstruappinfo->dl.lastPacketTime = ldEventTime;
323 pstruappinfo->dl.lastPacketSize = dPacketSize;
324 }
325 else if (pstruappinfo->dl.lastPacketTime == -1 && ldEventTime >= 0)
326 {
327 genRate = dPacketSize * 8 / (ldEventTime);
328 pstruappinfo->dl.lastPacketTime = ldEventTime;
329 pstruappinfo->dl.lastPacketSize = dPacketSize;
330 }
331
332 else if (ldEventTime > pstruappinfo->dl.lastPacketTime)
333 {
334 genRate = dPacketSize * 8 / (ldEventTime - pstruappinfo->dl.lastPacketTime);
335 pstruappinfo->dl.prevLastPacketTime = pstruappinfo->dl.lastPacketTime;
336 pstruappinfo->dl.lastPacketTime = ldEventTime;
337 pstruappinfo->dl.lastPacketSize = dPacketSize;
338 }
339
340 else if (ldEventTime == pstruappinfo->dl.lastPacketTime && ldEventTime != 0)
341 {
342 if (pstruappinfo->dl.prevLastPacketTime == -1)
343 {
344 pstruappinfo->dl.lastPacketSize += dPacketSize;
345 genRate = pstruappinfo->dl.lastPacketSize * 8 / (ldEventTime);
346 }
347 else
348 {
349 pstruappinfo->dl.lastPacketSize += dPacketSize;
350 genRate = pstruappinfo->dl.lastPacketSize * 8 / (ldEventTime - pstruappinfo->dl.lastPacketTime);
351 }
352 }
353
354 else
355 genRate = 0;
356 }
357
358 else // Receive
359 {
360 if (pstruappinfo->ul.lastPacketTime == -1 && ldEventTime == 0)
361 {
362 genRate = 0;
363 pstruappinfo->ul.lastPacketTime = ldEventTime;
364 pstruappinfo->ul.lastPacketSize = dPacketSize;
365 }
366 else if (pstruappinfo->ul.lastPacketTime == -1 && ldEventTime >= 0)
367 {
368 genRate = dPacketSize * 8 / (ldEventTime);
369 pstruappinfo->ul.lastPacketTime = ldEventTime;
370 pstruappinfo->ul.lastPacketSize = dPacketSize;
371 }
372
373 else if (ldEventTime > pstruappinfo->ul.lastPacketTime)
374 {
375 genRate = dPacketSize * 8 / (ldEventTime - pstruappinfo->ul.lastPacketTime);
376 pstruappinfo->ul.prevLastPacketTime = pstruappinfo->ul.lastPacketTime;
377 pstruappinfo->ul.lastPacketTime = ldEventTime;
378 pstruappinfo->ul.lastPacketSize = dPacketSize;
379 }
380
381 else if (ldEventTime == pstruappinfo->ul.lastPacketTime && ldEventTime != 0 )
382 {
383 if (pstruappinfo->ul.prevLastPacketTime == -1)
384 {
385 pstruappinfo->ul.lastPacketSize += dPacketSize;
386 genRate = pstruappinfo->ul.lastPacketSize * 8 / (ldEventTime);
387 }
388 else
389 {
390 pstruappinfo->ul.lastPacketSize += dPacketSize;
391 genRate = pstruappinfo->ul.lastPacketSize * 8 / (ldEventTime - pstruappinfo->ul.prevLastPacketTime);
392 }
393 }
394
395 else
396 genRate = 0;
397 }
398
399 return genRate;
400}