NetSim Source Code Help
Loading...
Searching...
No Matches
RREQ.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: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14
15#include "main.h"
16#include "AODV.h"
17#include "List.h"
18static unsigned int nRREQId=0;
20double fnGetTimeoutTime(int ttl,int count);
22 NETSIM_IPAddress orginator,
23 unsigned int nRREQId,
24 double dTime);
28 int ttl,
29 double dTimout);
32/**
33This function is used to genereate an AODV Route Request Packet. It Adds the RREQ entry in the
34RREQ sent table.
35*/
37{
38 double time;
40 unsigned int nDestSeq = fnFindSequenceNumber(AODV_DEV_VAR(pstruEventDetails->nDeviceId),Dest);
42 0,
43 0,
46 AODV_RREQ* rreq = (AODV_RREQ*)calloc(1,sizeof* rreq);
47 //Set the packet size
51 rreq->DestinationIPAddress = IP_COPY(Dest);
52 rreq->DestinationSequenceNumber = nDestSeq;
53 rreq->HopCount = 0;
54 if(!nDestSeq)
55 rreq->JRGDU[4] = '1';
58 rreq->RREQID = ++nRREQId;
59 rreq->Type = 1;
63 nRREQId,
65 //Set the TTL
67 rreq->DestinationIPAddress)+1;
68 packet->pstruNetworkData->nTTL += 1;
69 time = fnGetTimeoutTime(packet->pstruNetworkData->nTTL-1,1);
70 //Add rreq timeout event
72
75 packet->pstruNetworkData->nTTL-1,
76 time);
77 //update the metrics
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*/
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*/
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*/
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;
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*/
139 NETSIM_IPAddress orginator,
140 unsigned int nRREQId,
141 double dTime)
142{
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*/
154{
155 NetSim_EVENTDETAILS pevent;
157 pevent.dPacketSize = 0;
158 pevent.nApplicationId =0;
162 pevent.nEventType = TIMER_EVENT;
163 pevent.nPacketId =0;
165 pevent.nSegmentId=0;
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*/
180{
181 unsigned int nDestSeq;
182 NetSim_PACKET* packet;
183 AODV_RREQ* rreq;
184 double time;
187 Dest);
188 if(!table)
189 {
191 return 0; //RREP already received
192 }
194 {
196 return 0; //new RREQ sent
197 }
198 if(table->times > AODV_RREQ_RETRIES)
199 {
203 return 1; //MAX retries limit reaches
204 }
205
208 0,
209 0,
212 rreq = calloc(1,sizeof* rreq);
213
214 //Set the packet size
218 rreq->DestinationIPAddress = IP_COPY(Dest);
219 rreq->DestinationSequenceNumber = nDestSeq;
220 rreq->HopCount = 0;
221 if(!nDestSeq)
222 rreq->JRGDU[4] = '1';
225 rreq->RREQID = ++nRREQId;
227 rreq->Type = 1;
230 nRREQId,
232 //Set the TTL
234 rreq->DestinationIPAddress)+1;
235 time = fnGetTimeoutTime(packet->pstruNetworkData->nTTL,table->times);
236 //Add rreq timeout event
238
241 packet->pstruNetworkData->nTTL-1,
242 time);
243 //Add Network out event to transmit RREQ
251 pstruEventDetails->pPacket = packet;
257 //Update the metrics
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*/
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*/
286{
289 //Check for duplicate entry
291 {
294 return 0;//RREQ already received
295 }
296 //Insert in RREQ seen table
299 rreq->RREQID,
301 //Add to precursos list
303 //Add reverse route in route table
306 rreq->HopCount,
307 rreq->LastAddress,
309 //Transmit fifo buffer
311 //Check for the destination
313 {
314 //Generate the route reply
316 //Free the rreq packet
319 }
320 else
321 {
323 rreq->JRGDU[3] != '1' /* Destination only flag*/)
324 {
326 {
329 }
330 }
331 else
332 {
333 //Forward the rreq
335 }
336 }
337 return 1;
338}
339/**
340This function checks if the RREQ is there in the AODV seen table
341*/
343{
344 AODV_RREQ_SEEN_TABLE* table = devVar->rreqSeenTable;
345 while(table)
346 {
348 table->nRREQId == rreq->RREQID)
349 return true;
350 table=LIST_NEXT(table);
351 }
352 return false;
353}
354/**
355This function forwards the RREQ. It creates, a control packet, adds the necessary header and
356adds the Network Out Event.
357*/
359{
360 unsigned int nSeq;
361 NetSim_EVENTDETAILS pevent;
364 {
366 return 1;//Don't forward the RREQ
367 }
368 rreq->HopCount++;
370 if(nSeq > rreq->DestinationSequenceNumber)
371 rreq->DestinationSequenceNumber = nSeq;
373 rreq->JRGDU[4]='1';
374 if(rreq->LastAddress)
375 IP_FREE(rreq->LastAddress);
379 //Add network out event to forward the route req
380 memcpy(&pevent,pstruEventDetails,sizeof pevent);
382 pevent.nSubEventType = 0;
384 fnpAddEvent(&pevent);
385 //update the metrics
387 return 1;
388}
389
390
NETSIM_ID aodv_get_curr_if()
unsigned int fnFindSequenceNumber(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress ip)
NetSim_PACKET * fn_NetSim_AODV_GenerateCtrlPacket(NETSIM_ID src, NETSIM_ID dest, NETSIM_ID recv, double dTime, AODV_CONTROL_PACKET type)
@ AODVctrlPacket_RREQ
Definition: AODV.h:78
#define AODV_INSERT_ROUTE_TABLE(ip, seq, hopcount, nexthop, lifeTime)
Definition: AODV.h:397
@ AODVsubevent_RREQ_TIMEOUT
Definition: AODV.h:85
#define AODV_NET_DIAMETER
Definition: AODV.h:36
#define AODV_PATH_DISCOVERY_TIME
Definition: AODV.h:40
#define AODV_TTL_INCREMENT
Definition: AODV.h:47
#define AODV_GENERATE_RREP()
Definition: AODV.h:400
#define AODV_NET_TRAVERSAL_TIME
Definition: AODV.h:37
#define AODV_GENERATE_RREP_BY_IN()
Definition: AODV.h:405
#define AODV_INSERT_PRECURSOR(ip)
Definition: AODV.h:398
NETSIM_IPAddress aodv_get_curr_ip()
#define AODV_TRANSMIT_FIFO(devVar)
Definition: AODV.h:401
#define AODV_TTL_THRESHOLD
Definition: AODV.h:48
#define AODV_NODE_TRAVERSAL_TIME
Definition: AODV.h:39
#define RREQSENTTABLE_ALLOC()
Definition: AODV.h:417
#define AODV_METRICS_VAR(devId)
Definition: AODV.h:414
#define AODV_TTL_START
Definition: AODV.h:46
#define AODV_RREQ_SIZE(dev)
Definition: AODV.h:53
#define AODV_RING_TRAVERSAL_TIME(ttl)
Definition: AODV.h:42
#define AODV_CHECK_ROUTE_FOUND(destIP)
Definition: AODV.h:392
#define AODV_FORWARD_RREQ()
Definition: AODV.h:399
#define AODV_DEV_VAR(devId)
Definition: AODV.h:413
#define AODV_RREQ_RETRIES
Definition: AODV.h:43
#define RREQSEENTABLE_ALLOC()
Definition: AODV.h:416
int fnEmptyFIFOBuffer(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress dest)
Definition: FIFOBuffer.c:54
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
void IP_FREE(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define LIST_ADD_LAST(ls, mem)
Definition: List.h:30
#define LIST_NEXT(ls)
Definition: List.h:29
#define LIST_FREE(ls, mem)
Definition: List.h:32
#define calloc(c, s)
Definition: Memory.h:29
int fn_NetSim_AODV_InsertInRREQSeenTable(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress orginator, unsigned int nRREQId, double dTime)
Definition: RREQ.c:138
static unsigned int nRREQId
Definition: RREQ.c:18
AODV_RREQ_SENT_TABLE * fnFindSentTable(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress ip)
Definition: RREQ.c:86
int fnDeleteRREQSentTable(AODV_DEVICE_VAR *devVar, AODV_RREQ_SENT_TABLE *table)
Definition: RREQ.c:273
double fnGetTimeoutTime(int ttl, int count)
Definition: RREQ.c:264
int fnIncreaseTTL(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress ip)
Definition: RREQ.c:122
int fn_NetSim_AODV_ForwardRREQ(NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RREQ.c:358
int fn_NetSim_AODV_RetryRREQ(NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RREQ.c:179
bool fnCheckRREQSeenTable(AODV_DEVICE_VAR *devVar, AODV_RREQ *rreq)
Definition: RREQ.c:342
int fn_NetSim_AODV_InterstInRREQSentTable(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress dest, int ttl, double dTimout)
Definition: RREQ.c:100
NetSim_PACKET * fn_NetSim_AODV_GenerateRREQ(NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RREQ.c:36
int fn_NetSim_AODV_ProcessRREQ(NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RREQ.c:285
double fn_NetSim_AODV_AddTimeOut(NETSIM_IPAddress dest, NetSim_EVENTDETAILS *pstruEventDetails, double time)
Definition: RREQ.c:153
NETWORK_LAYER_PROTOCOL fn_NetSim_Stack_GetNWProtocol(NETSIM_ID nDeviceId)
@ NW_PROTOCOL_AODV
Definition: Stack.h:197
@ NETWORK_OUT_EVENT
Definition: Stack.h:108
@ TIMER_EVENT
Definition: Stack.h:114
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
AODV_RREQ_SEEN_TABLE * rreqSeenTable
Definition: AODV.h:382
AODV_RREQ_SENT_TABLE * rreqSentTable
Definition: AODV.h:383
Structure of RREQ Seen Table in which entry is made of the RREQ received.
Definition: AODV.h:341
unsigned int nRREQId
RREQ identification no.
Definition: AODV.h:343
NETSIM_IPAddress OrginatingNode
IP address of a node originating RREQ
Definition: AODV.h:342
Structure of RREQ sent Table in which entry of RREQ sent is made.
Definition: AODV.h:349
NETSIM_IPAddress DestAddress
Destination IP Address.
Definition: AODV.h:350
int ttl
Time to live.
Definition: AODV.h:351
unsigned int HopCount
Definition: AODV.h:134
NETSIM_IPAddress DestinationIPAddress
Definition: AODV.h:143
NETSIM_IPAddress OriginatorIPAddress
Definition: AODV.h:148
unsigned int RREQID
Definition: AODV.h:138
unsigned int OriginatorSequenceNumber
Definition: AODV.h:149
unsigned int DestinationSequenceNumber
Definition: AODV.h:144
unsigned int Type
Definition: AODV.h:113
NETSIM_IPAddress LastAddress
Definition: AODV.h:154
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
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
NETSIM_ID nReceiverId
Definition: Packet.h:266
NETSIM_ID nTransmitterId
Definition: Packet.h:265