NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RREQ.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
15#include "main.h"
16#include "AODV.h"
17#include "List.h"
18static unsigned int nRREQId=0;
19double fn_NetSim_AODV_AddTimeOut(NETSIM_IPAddress dest,NetSim_EVENTDETAILS* pstruEventDetails,double time);
20double fnGetTimeoutTime(int ttl,int count);
21int fn_NetSim_AODV_InsertInRREQSeenTable(AODV_DEVICE_VAR* devVar,
22 NETSIM_IPAddress orginator,
23 unsigned int nRREQId,
24 double dTime);
25int fnIncreaseTTL(AODV_DEVICE_VAR* devVar,NETSIM_IPAddress ip);
26int fn_NetSim_AODV_InterstInRREQSentTable(AODV_DEVICE_VAR* devVar,
27 NETSIM_IPAddress dest,
28 int ttl,
29 double dTimout);
30int fnDeleteRREQSentTable(AODV_DEVICE_VAR* devVar,AODV_RREQ_SENT_TABLE* table);
31bool fnCheckRREQSeenTable(AODV_DEVICE_VAR* devVar,AODV_RREQ* rreq);
32/**
33This function is used to genereate an AODV Route Request Packet. It Adds the RREQ entry in the
34RREQ sent table.
35*/
36NetSim_PACKET* fn_NetSim_AODV_GenerateRREQ(NetSim_EVENTDETAILS* pstruEventDetails)
37{
38 double time;
39 NETSIM_IPAddress Dest = pstruEventDetails->pPacket->pstruNetworkData->szDestIP;
40 unsigned int nDestSeq = fnFindSequenceNumber(AODV_DEV_VAR(pstruEventDetails->nDeviceId),Dest);
41 NetSim_PACKET* packet = fn_NetSim_AODV_GenerateCtrlPacket(pstruEventDetails->nDeviceId,
42 0,
43 0,
44 pstruEventDetails->dEventTime,
45 AODVctrlPacket_RREQ);
46 AODV_RREQ* rreq = (AODV_RREQ*)calloc(1,sizeof* rreq);
47 //Set the packet size
48 packet->pstruNetworkData->dPacketSize = AODV_RREQ_SIZE(pstruEventDetails->nDeviceId);
49 packet->pstruNetworkData->dOverhead = AODV_RREQ_SIZE(pstruEventDetails->nDeviceId);
50 packet->pstruNetworkData->Packet_RoutingProtocol = rreq;
51 rreq->DestinationIPAddress = IP_COPY(Dest);
52 rreq->DestinationSequenceNumber = nDestSeq;
53 rreq->HopCount = 0;
54 if(!nDestSeq)
55 rreq->JRGDU[4] = '1';
56 rreq->OriginatorIPAddress = aodv_get_curr_ip();
57 rreq->OriginatorSequenceNumber = ++AODV_DEV_VAR(pstruEventDetails->nDeviceId)->nSequenceNumber;
58 rreq->RREQID = ++nRREQId;
59 rreq->Type = 1;
60 rreq->LastAddress = IP_COPY(rreq->OriginatorIPAddress);
61 fn_NetSim_AODV_InsertInRREQSeenTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
63 nRREQId,
64 pstruEventDetails->dEventTime);
65 //Set the TTL
66 packet->pstruNetworkData->nTTL = fnIncreaseTTL(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
67 rreq->DestinationIPAddress)+1;
68 packet->pstruNetworkData->nTTL += 1;
69 time = fnGetTimeoutTime(packet->pstruNetworkData->nTTL-1,1);
70 //Add rreq timeout event
71 time = fn_NetSim_AODV_AddTimeOut(rreq->DestinationIPAddress,pstruEventDetails,time);
72
73 fn_NetSim_AODV_InterstInRREQSentTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
75 packet->pstruNetworkData->nTTL-1,
76 time);
77 //update the metrics
78 AODV_METRICS_VAR(pstruEventDetails->nDeviceId).rreqSent++;
79 return packet;
80}
81/**
82This function is used to find if a RREQ to the given ip is sent. This is searched in the
83RREQ_SENT_TABLE. If an entry is encountered, the corresponding table is returned. Else NULL
84is returned.
85*/
86AODV_RREQ_SENT_TABLE* fnFindSentTable(AODV_DEVICE_VAR* devVar,NETSIM_IPAddress ip)
87{
88 AODV_RREQ_SENT_TABLE* table = devVar->rreqSentTable;
89 while(table)
90 {
91 if(!IP_COMPARE(table->DestAddress,ip))
92 return table;
93 table=LIST_NEXT(table);
94 }
95 return NULL;
96}
97/**
98This functions inserts a RREQ querry in the RREQ sent table
99*/
100int fn_NetSim_AODV_InterstInRREQSentTable(AODV_DEVICE_VAR* devVar,
101 NETSIM_IPAddress dest,
102 int ttl,
103 double dTimout)
104{
105 AODV_RREQ_SENT_TABLE* table = fnFindSentTable(devVar,dest);
106 if(!table)
107 {
108 table = RREQSENTTABLE_ALLOC();
109 table->DestAddress = IP_COPY(dest);
110 table->times = 1;
111 LIST_ADD_LAST(&devVar->rreqSentTable,table);
112 }
113 if(ttl == AODV_NET_DIAMETER)
114 table->times += 1;
115 table->ttl = ttl;
116 table->dTimeoutTime = dTimout;
117 return 1;
118}
119/**
120This function increases the TTL of the RREQ sent table
121*/
122int fnIncreaseTTL(AODV_DEVICE_VAR* devVar,NETSIM_IPAddress ip)
123{
124 AODV_RREQ_SENT_TABLE* table = fnFindSentTable(devVar,ip);
125 if(!table)
126 return AODV_TTL_START;
127 if(table->ttl >= AODV_TTL_THRESHOLD)
128 return AODV_NET_DIAMETER;
129 if(table->ttl+AODV_TTL_INCREMENT > AODV_TTL_THRESHOLD)
130 return AODV_TTL_THRESHOLD;
131 return table->ttl+AODV_TTL_INCREMENT;
132
133}
134/**
135This function inserts the data of a RREQ in the RREQ seen table. It contains the
136originator IP address.
137*/
138int fn_NetSim_AODV_InsertInRREQSeenTable(AODV_DEVICE_VAR* devVar,
139 NETSIM_IPAddress orginator,
140 unsigned int nRREQId,
141 double dTime)
142{
143 AODV_RREQ_SEEN_TABLE* table = RREQSEENTABLE_ALLOC();
144 table->nRREQId = nRREQId;
145 table->OrginatingNode = IP_COPY(orginator);
146 table->time = dTime+AODV_PATH_DISCOVERY_TIME;
147 LIST_ADD_LAST(&devVar->rreqSeenTable,table);
148 return 1;
149}
150/**
151This function adds the RREQ timeout event.
152*/
153double fn_NetSim_AODV_AddTimeOut(NETSIM_IPAddress dest,NetSim_EVENTDETAILS* pstruEventDetails,double time)
154{
155 NetSim_EVENTDETAILS pevent;
156 pevent.dEventTime = pstruEventDetails->dEventTime+time;
157 pevent.dPacketSize = 0;
158 pevent.nApplicationId =0;
159 pevent.nDeviceId=pstruEventDetails->nDeviceId;
160 pevent.nDeviceType=pstruEventDetails->nDeviceType;
161 pevent.nInterfaceId = aodv_get_curr_if();
162 pevent.nEventType = TIMER_EVENT;
163 pevent.nPacketId =0;
164 pevent.nProtocolId = NW_PROTOCOL_AODV;
165 pevent.nSegmentId=0;
166 pevent.nSubEventType = AODVsubevent_RREQ_TIMEOUT;
167 pevent.pPacket=NULL;
168 pevent.szOtherDetails=IP_COPY(dest);
169 fnpAddEvent(&pevent);
170 return pevent.dEventTime;
171}
172/**
173This function is used to retry an AODV route request. If the table is NULL or if the table
174timeout is greater than present time or if the number of RREQ exceeds the RREQ_RETRY_LIMIT,
175then RREQ is not sent again.
176
177Else, a RREQ packet is generated and a NETWORK_OUT_EVENT is added.
178*/
179int fn_NetSim_AODV_RetryRREQ(NetSim_EVENTDETAILS* pstruEventDetails)
180{
181 unsigned int nDestSeq;
182 NetSim_PACKET* packet;
183 AODV_RREQ* rreq;
184 double time;
185 NETSIM_IPAddress Dest = pstruEventDetails->szOtherDetails;
186 AODV_RREQ_SENT_TABLE* table = fnFindSentTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
187 Dest);
188 if(!table)
189 {
190 IP_FREE(pstruEventDetails->szOtherDetails);
191 return 0; //RREP already received
192 }
193 if(table->dTimeoutTime > pstruEventDetails->dEventTime)
194 {
195 IP_FREE(pstruEventDetails->szOtherDetails);
196 return 0; //new RREQ sent
197 }
198 if(table->times > AODV_RREQ_RETRIES)
199 {
200 fnDeleteRREQSentTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),table);
201 fnEmptyFIFOBuffer(AODV_DEV_VAR(pstruEventDetails->nDeviceId),Dest);
202 IP_FREE(pstruEventDetails->szOtherDetails);
203 return 1; //MAX retries limit reaches
204 }
205
206 nDestSeq = fnFindSequenceNumber(AODV_DEV_VAR(pstruEventDetails->nDeviceId),Dest);
207 packet = fn_NetSim_AODV_GenerateCtrlPacket(pstruEventDetails->nDeviceId,
208 0,
209 0,
210 pstruEventDetails->dEventTime,
211 AODVctrlPacket_RREQ);
212 rreq = calloc(1,sizeof* rreq);
213
214 //Set the packet size
215 packet->pstruNetworkData->dPacketSize = AODV_RREQ_SIZE(pstruEventDetails->nDeviceId);
216 packet->pstruNetworkData->dOverhead = AODV_RREQ_SIZE(pstruEventDetails->nDeviceId);
217 packet->pstruNetworkData->Packet_RoutingProtocol = rreq;
218 rreq->DestinationIPAddress = IP_COPY(Dest);
219 rreq->DestinationSequenceNumber = nDestSeq;
220 rreq->HopCount = 0;
221 if(!nDestSeq)
222 rreq->JRGDU[4] = '1';
223 rreq->OriginatorIPAddress = aodv_get_curr_ip();
224 rreq->OriginatorSequenceNumber = ++AODV_DEV_VAR(pstruEventDetails->nDeviceId)->nSequenceNumber;
225 rreq->RREQID = ++nRREQId;
226 rreq->LastAddress = IP_COPY(rreq->OriginatorIPAddress);
227 rreq->Type = 1;
228 fn_NetSim_AODV_InsertInRREQSeenTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
230 nRREQId,
231 pstruEventDetails->dEventTime);
232 //Set the TTL
233 packet->pstruNetworkData->nTTL = fnIncreaseTTL(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
234 rreq->DestinationIPAddress)+1;
235 time = fnGetTimeoutTime(packet->pstruNetworkData->nTTL,table->times);
236 //Add rreq timeout event
237 time = fn_NetSim_AODV_AddTimeOut(rreq->DestinationIPAddress,pstruEventDetails,time);
238
239 fn_NetSim_AODV_InterstInRREQSentTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
241 packet->pstruNetworkData->nTTL-1,
242 time);
243 //Add Network out event to transmit RREQ
244 pstruEventDetails->dPacketSize = 0;
245 pstruEventDetails->nApplicationId = 0;
246 pstruEventDetails->nEventType = NETWORK_OUT_EVENT;
247 pstruEventDetails->nPacketId = 0;
248 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetNWProtocol(pstruEventDetails->nDeviceId);
249 pstruEventDetails->nSegmentId = 0;
250 pstruEventDetails->nSubEventType = 0;
251 pstruEventDetails->pPacket = packet;
252 if(pstruEventDetails->szOtherDetails)
253 IP_FREE(pstruEventDetails->szOtherDetails);
254 pstruEventDetails->szOtherDetails = NULL;
255 pstruEventDetails->pPacket->pstruNetworkData->szNextHopIp=NULL;
256 fnpAddEvent(pstruEventDetails);
257 //Update the metrics
258 AODV_METRICS_VAR(pstruEventDetails->nDeviceId).rreqSent++;
259 return 1;
260}
261/**
262This function returns the traversal time
263*/
264double fnGetTimeoutTime(int ttl,int count)
265{
266 if(ttl!=AODV_NET_DIAMETER)
267 return AODV_RING_TRAVERSAL_TIME(ttl);
268 return count*AODV_NET_TRAVERSAL_TIME;
269}
270/**
271This function deletes the RREQ snet table
272*/
273int fnDeleteRREQSentTable(AODV_DEVICE_VAR* devVar,AODV_RREQ_SENT_TABLE* table)
274{
275 LIST_FREE(&devVar->rreqSentTable,table);
276 return 1;
277}
278/**
279This function process the AODV RREQ. It checks for duplicate entry of the received RREQ packet.
280If the RREQ packet was received previously, it frees the packet.
281Else, it adds the RREQ in the AODV seen table, Inserts the reverse route in the Route table.
282If the device has the Route to the target, it generates a RREP.
283else it forwards the RREQ.
284*/
285int fn_NetSim_AODV_ProcessRREQ(NetSim_EVENTDETAILS* pstruEventDetails)
286{
287 NetSim_PACKET* packet = pstruEventDetails->pPacket;
288 AODV_RREQ* rreq=(AODV_RREQ*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
289 //Check for duplicate entry
290 if(fnCheckRREQSeenTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),rreq))
291 {
292 fn_NetSim_Packet_FreePacket(packet);
293 pstruEventDetails->pPacket=NULL;
294 return 0;//RREQ already received
295 }
296 //Insert in RREQ seen table
297 fn_NetSim_AODV_InsertInRREQSeenTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
299 rreq->RREQID,
300 pstruEventDetails->dEventTime);
301 //Add to precursos list
302 AODV_INSERT_PRECURSOR(rreq->LastAddress);
303 //Add reverse route in route table
304 if (!IP_COMPARE(aodv_get_curr_ip(), rreq->DestinationIPAddress))
305 sprintf(comment, "Received RREQ as destination");
306 else
307 sprintf(comment, "Received RREQ as intermediate");
308 AODV_INSERT_ROUTE_TABLE(rreq->OriginatorIPAddress,
310 rreq->HopCount,
311 rreq->LastAddress,
312 pstruEventDetails->dEventTime+2*AODV_NET_TRAVERSAL_TIME-2*rreq->HopCount*AODV_NODE_TRAVERSAL_TIME);
313 //Transmit fifo buffer
314 AODV_TRANSMIT_FIFO(AODV_DEV_VAR(pstruEventDetails->nDeviceId));
315 //Check for the destination
316 if(!IP_COMPARE(aodv_get_curr_ip(),rreq->DestinationIPAddress))
317 {
318 //Generate the route reply
319 AODV_GENERATE_RREP();
320 //Free the rreq packet
321 fn_NetSim_Packet_FreePacket(packet);
322 pstruEventDetails->pPacket=NULL;
323 }
324 else
325 {
326 if(AODV_CHECK_ROUTE_FOUND(rreq->DestinationIPAddress) &&
327 rreq->JRGDU[3] != '1' /* Destination only flag*/)
328 {
329 if(AODV_GENERATE_RREP_BY_IN())
330 {
331 fn_NetSim_Packet_FreePacket(packet);
332 pstruEventDetails->pPacket=NULL;
333 }
334 }
335 else
336 {
337 //Forward the rreq
338 AODV_FORWARD_RREQ();
339 }
340 }
341 return 1;
342}
343/**
344This function checks if the RREQ is there in the AODV seen table
345*/
346bool fnCheckRREQSeenTable(AODV_DEVICE_VAR* devVar,AODV_RREQ* rreq)
347{
348 AODV_RREQ_SEEN_TABLE* table = devVar->rreqSeenTable;
349 while(table)
350 {
351 if(!IP_COMPARE(table->OrginatingNode,rreq->OriginatorIPAddress) &&
352 table->nRREQId == rreq->RREQID)
353 return true;
354 table=LIST_NEXT(table);
355 }
356 return false;
357}
358/**
359This function forwards the RREQ. It creates, a control packet, adds the necessary header and
360adds the Network Out Event.
361*/
362int fn_NetSim_AODV_ForwardRREQ(NetSim_EVENTDETAILS* pstruEventDetails)
363{
364 unsigned int nSeq;
365 NetSim_EVENTDETAILS pevent;
366 AODV_RREQ* rreq = (AODV_RREQ*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
367 if(pstruEventDetails->pPacket->pstruNetworkData->nTTL == 1)
368 {
369 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
370 return 1;//Don't forward the RREQ
371 }
372 rreq->HopCount++;
373 nSeq = fnFindSequenceNumber(AODV_DEV_VAR(pstruEventDetails->nDeviceId),rreq->DestinationIPAddress);
374 if(nSeq > rreq->DestinationSequenceNumber)
375 rreq->DestinationSequenceNumber = nSeq;
377 rreq->JRGDU[4]='1';
378 if(rreq->LastAddress)
379 IP_FREE(rreq->LastAddress);
380 rreq->LastAddress = aodv_get_curr_ip();
381 pstruEventDetails->pPacket->nTransmitterId = pstruEventDetails->nDeviceId;
382 pstruEventDetails->pPacket->nReceiverId = 0;
383 //Add network out event to forward the route req
384 memcpy(&pevent,pstruEventDetails,sizeof pevent);
385 pevent.nEventType = NETWORK_OUT_EVENT;
386 pevent.nSubEventType = 0;
387 pstruEventDetails->pPacket->pstruNetworkData->szNextHopIp=NULL;
388 fnpAddEvent(&pevent);
389 //update the metrics
390 AODV_METRICS_VAR(pstruEventDetails->nDeviceId).rreqForwarded++;
391 return 1;
392}
393
394
unsigned int nRREQId
RREQ identification no.
Definition AODV.h:344
NETSIM_IPAddress OrginatingNode
IP address of a node originating RREQ.
Definition AODV.h:343
NETSIM_IPAddress DestAddress
Destination IP Address.
Definition AODV.h:351
int ttl
Time to live.
Definition AODV.h:352
unsigned int HopCount
Definition AODV.h:135
NETSIM_IPAddress DestinationIPAddress
Definition AODV.h:144
NETSIM_IPAddress OriginatorIPAddress
Definition AODV.h:149
unsigned int RREQID
Definition AODV.h:139
unsigned int OriginatorSequenceNumber
Definition AODV.h:150
unsigned int DestinationSequenceNumber
Definition AODV.h:145