NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
SendBuffer.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* Author: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "List.h"
16#include "DSR.h"
17/**
18This function adds the packet to send buffer.
19*/
20bool fn_NetSim_DSR_AddToSendBuffer(DSR_SEND_BUFFER** sendBuffer,NetSim_PACKET* packet,double dTime)
21{
22 NETSIM_IPAddress dest = packet->pstruNetworkData->szDestIP;
23 DSR_SEND_BUFFER* temp = *sendBuffer;
24 bool flag=true;
25 while(temp)
26 {
27 if(!IP_COMPARE(dest,temp->target))
28 {
29 flag = false;
30 break;
31 }
32 temp = LIST_NEXT(temp);
33 }
34 if(!temp)
35 temp = (DSR_SEND_BUFFER*)SENDBUFFER_ALLOC();
36 if(!temp->packet)
37 {
38 temp->dTime = dTime;
39 temp->packet = packet;
40 temp->target = dest;
41 }
42 else
43 {
44 NetSim_PACKET* temp_packet=temp->packet;
45 while(temp_packet->pstruNextPacket)
46 temp_packet = temp_packet->pstruNextPacket;
47 temp_packet->pstruNextPacket = packet;
48 }
49 if(flag)
50 LIST_ADD_LAST((void**)sendBuffer,temp);
51 return flag;
52}
53/**
54This function Empties the send buffer.
55*/
56void fn_NetSim_DSR_EmptySendBuffer(NETSIM_IPAddress targetAddress,NETSIM_ID nDeviceId)
57{
58 DSR_SEND_BUFFER* sendBuffer=DSR_DEV_VAR(nDeviceId)->pstruSendBuffer;
59 while(sendBuffer)
60 {
61 if(!IP_COMPARE(sendBuffer->target,targetAddress))
62 {
63 while(sendBuffer->packet)
64 {
65 NetSim_PACKET* temp = sendBuffer->packet;
66 sendBuffer->packet = temp->pstruNextPacket;
67 temp->pstruNextPacket = NULL;
68 fn_NetSim_Packet_FreePacket(temp);
69 //Update the metrics
70 DSR_DEV_VAR(nDeviceId)->dsrMetrics.packetDropped++;
71 }
72 IP_FREE(sendBuffer->target);
73 LIST_FREE(&DSR_DEV_VAR(nDeviceId)->pstruSendBuffer,sendBuffer);
74 break;
75 }
76 sendBuffer = LIST_NEXT(sendBuffer);
77 }
78}
79/**
80This function checks if the route cache has the route to the target and transmits the packet
81if it has.
82*/
83void fn_NetSim_DSR_CheckSendBuffer(NETSIM_ID nDeviceId,double dTime)
84{
85 DSR_SEND_BUFFER* sendBuffer=DSR_DEV_VAR(nDeviceId)->pstruSendBuffer;
86 DSR_ROUTE_CACHE* routeCache=DSR_DEV_VAR(nDeviceId)->pstruRouteCache;
87 while(sendBuffer)
88 {
89 int ntxflag=0;
90 NETSIM_IPAddress target = sendBuffer->target;
91 while(routeCache)
92 {
93 unsigned int nLoop;
94 for(nLoop=0;nLoop<routeCache->nLength;nLoop++)
95 {
96 if(!IP_COMPARE(target,routeCache->address[nLoop]))
97 {
98 //Route found
99 DSR_TRANSMIT_SEND_BUFFER(sendBuffer,
100 nDeviceId,dTime);
101 ntxflag=1;
102 break;
103 }
104 }
105 routeCache = LIST_NEXT(routeCache);
106 }
107 if(ntxflag)
108 {
109 DSR_SEND_BUFFER* temp = sendBuffer;
110 sendBuffer=LIST_NEXT(sendBuffer);
111 DSR_EMPTY_SEND_BUFFER(temp->target,nDeviceId);
112 continue;
113 }
114 sendBuffer = LIST_NEXT(sendBuffer);
115 }
116}
117/**
118This function transmits the packets from sent buffer
119*/
120int fn_NetSim_DSR_TransmitPacketFromSendBuffer(DSR_SEND_BUFFER* sendBuffer,
121 NETSIM_ID nDeviceId,
122 double dTime)
123{
124 NetSim_EVENTDETAILS eventDetails;
125 memset(&eventDetails,0,sizeof*(&eventDetails));
126 while(sendBuffer->packet)
127 {
128 NetSim_PACKET* packet = sendBuffer->packet;
129 sendBuffer->packet = packet->pstruNextPacket;
130 packet->pstruNextPacket = NULL;
131 eventDetails.dEventTime = dTime;
132 eventDetails.dPacketSize = fnGetPacketSize(packet);
133 if(packet->pstruAppData)
134 {
135 eventDetails.nApplicationId = packet->pstruAppData->nApplicationId;
136 eventDetails.nSegmentId = packet->pstruAppData->nSegmentId;
137 }
138 else
139 {
140 eventDetails.nApplicationId = 0;
141 eventDetails.nSegmentId = 0;
142 }
143 eventDetails.nDeviceId = nDeviceId;
144 eventDetails.nDeviceType = NETWORK->ppstruDeviceList[nDeviceId-1]->nDeviceType;
145 eventDetails.nEventType = NETWORK_OUT_EVENT;
146 eventDetails.nInterfaceId = 1;
147 eventDetails.nPacketId = packet->nPacketId;
148 eventDetails.nProtocolId = fn_NetSim_Stack_GetNWProtocol(nDeviceId);
149 eventDetails.nSubEventType = 0;
150 eventDetails.pPacket = packet;
151 fnpAddEvent(&eventDetails);
152 }
153 return 0;
154}
NETSIM_IPAddress target
Target IP address.
Definition DSR.h:591
NetSim_PACKET * packet
List of packets to a particular Dest IP.
Definition DSR.h:593