NetSim Source Code Help
Loading...
Searching...
No Matches
RouteTable.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"
18/**
19This function adds the route in the Route table of the node. If the table is not there,
20it creates one with a certain timeout after which the entries are deleted.
21*/
23 unsigned int seqNumber,
24 unsigned int hopCount,
25 NETSIM_IPAddress nextHop,
26 double lifeTime,
28{
30 AODV_ROUTETABLE* table = devVar->routeTable;
31 while(table)
32 {
33 if(!IP_COMPARE(table->DestinationIPAddress,ip) &&
35 {
36 if(table->ValidDestinationSequenceNumberflag == false)
37 break;
38 if(table->DestinationSequenceNumber < seqNumber)
39 break;
40 if(table->DestinationSequenceNumber > seqNumber)
41 return 1;
42 if(hopCount+1<table->HopCount || table->routingFlags==AODV_RoutingFlag_InValid)
43 break;
44 else
45 return 1;
46 }
47 table = LIST_NEXT(table);
48 }
49 if(!table)
50 {
52 //Create new table
53 table = ROUTETABLE_ALLOC();
56 //Add an event for active route time out
58 pevent.dPacketSize = 0;
59 pevent.nApplicationId = 0;
62 pevent.nEventType = TIMER_EVENT;
64 pevent.nPacketId = 0;
67 pevent.nSegmentId = 0;
69 pevent.pPacket = NULL;
70 pevent.szOtherDetails = IP_COPY(ip);
71 fnpAddEvent(&pevent);
72 }
73 if(!table->DestinationIPAddress)
74 table->DestinationIPAddress = IP_COPY(ip);
75 table->DestinationSequenceNumber = seqNumber;
76 table->HopCount= hopCount;
77 table->Lifetime = max(table->Lifetime,lifeTime);
78 table->ListofPrecursors = devVar->precursorsList;
79 table->NetworkInterface = 1;
80 if(!table->NextHop)
81 table->NextHop = IP_COPY(nextHop);
85 return 1;
86}
87/**
88This function adds the given IP in the ADOV precursor list if not already present.
89*/
91{
92 int loop;
93 AODV_PRECURSORS_LIST* precursorsList = AODV_DEV_VAR(pstruEventDetails->nDeviceId)->precursorsList;
94 if(!precursorsList)
95 {
96 precursorsList = calloc(1,sizeof* precursorsList);
97 AODV_DEV_VAR(pstruEventDetails->nDeviceId)->precursorsList = precursorsList;
98 }
99 for(loop=0;loop<precursorsList->count;loop++)
100 {
101 if(!IP_COMPARE(precursorsList->list[loop],ip))
102 return 0; //ip already present in list
103 }
104 if(!precursorsList->count)
105 precursorsList->list = calloc(1,sizeof* precursorsList->list);
106 else
107 precursorsList->list = realloc(precursorsList->list,(precursorsList->count+1)*(sizeof* precursorsList->list));
108 precursorsList->list[precursorsList->count] = IP_COPY(ip);
109 precursorsList->count++;
110 return 1;
111}
112/**
113This function returns the next hop IP to the destination from the Route Table
114*/
116 NETSIM_IPAddress dest,
118{
119 AODV_ROUTETABLE* table=devVar->routeTable;
120 while(table)
121 {
122 if(!IP_COMPARE(table->DestinationIPAddress,dest) &&
124 return table->NextHop;
125 table = LIST_NEXT(table);
126 }
127 return NULL;
128}
129/**
130This function returns the Route table with the mentioned destination IP
131*/
133{
134 while(table)
135 {
136 if(!IP_COMPARE(table->DestinationIPAddress,dest))
137 {
138 return table;
139 }
140 table = LIST_NEXT(table);
141 }
142 return NULL;
143}
144/**
145This function updates the lifetime of the Route Table
146*/
148 double lifetime,
150{
152 while(table)
153 {
154 if(!IP_COMPARE(table->NextHop,ip) || !IP_COMPARE(table->DestinationIPAddress,ip))
155 {
156 table->Lifetime = max(table->Lifetime,lifetime);
157 }
158 table=LIST_NEXT(table);
159 }
160 return 1;
161}
162/**
163This function adds the timeout event of a Route Table which is equal to the table_LifeTime
164*/
166{
167 int flag = 0;
170 while(table)
171 {
172 if(!IP_COMPARE(table->DestinationIPAddress,dest))
173 {
174 if(table->Lifetime <= pstruEventDetails->dEventTime)
175 {
176 AODV_ROUTETABLE* temp = LIST_NEXT(table);
177 IP_FREE(table->DestinationIPAddress);
178 IP_FREE(table->NextHop);
180 table = temp;
181 continue;
182 }
183 else
184 {
185 //Add new time out event
186 pstruEventDetails->dEventTime = table->Lifetime;
189 flag = 1;
190 }
191 }
192 table=(AODV_ROUTETABLE*)LIST_NEXT(table);
193 }
194 if(!flag)
195 IP_FREE(dest);
196 return 1;
197}
#define AODV_ACTIVE_ROUTE_TIMEOUT
Definition: AODV.h:26
@ AODVsubevent_ACTIVE_ROUTE_TIMEOUT
Definition: AODV.h:87
@ AODV_RoutingFlag_InValid
Definition: AODV.h:287
@ AODV_RoutingFlag_Valid
Definition: AODV.h:286
#define ROUTETABLE_ALLOC()
Definition: AODV.h:418
#define AODV_DEV_VAR(devId)
Definition: AODV.h:413
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
struct stru_ip * NETSIM_IPAddress
Definition: IP_Addressing.h:32
void IP_FREE(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define max(a, b)
Definition: Linux.h:107
#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 realloc(p, s)
Definition: Memory.h:32
#define calloc(c, s)
Definition: Memory.h:29
int fn_NetSim_AODV_UpdateRouteTable(NETSIM_IPAddress ip, double lifetime, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteTable.c:147
AODV_ROUTETABLE * fnFindRouteTable(AODV_ROUTETABLE *table, NETSIM_IPAddress dest)
Definition: RouteTable.c:132
NETSIM_IPAddress fn_NetSim_AODV_FindNextHop(AODV_DEVICE_VAR *devVar, NETSIM_IPAddress dest, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteTable.c:115
int fn_NetSim_AODV_InsertInPrecursorsList(NETSIM_IPAddress ip, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteTable.c:90
int fn_NetSim_AODV_InsertInRouteTable(NETSIM_IPAddress ip, unsigned int seqNumber, unsigned int hopCount, NETSIM_IPAddress nextHop, double lifeTime, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteTable.c:22
int fn_NetSim_AODV_ActiveRouteTimeout(NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteTable.c:165
@ NW_PROTOCOL_AODV
Definition: Stack.h:197
@ TIMER_EVENT
Definition: Stack.h:114
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
AODV_PRECURSORS_LIST * precursorsList
Definition: AODV.h:384
AODV_ROUTETABLE * routeTable
Definition: AODV.h:381
NETSIM_IPAddress * list
Definition: AODV.h:293
bool ValidDestinationSequenceNumberflag
Definition: AODV.h:320
NETSIM_ID NetworkInterface
Definition: AODV.h:322
AODV_RoutingFlag routingFlags
Definition: AODV.h:321
unsigned int HopCount
Definition: AODV.h:323
NETSIM_IPAddress NextHop
Definition: AODV.h:324
NETSIM_IPAddress DestinationIPAddress
Definition: AODV.h:318
unsigned int DestinationSequenceNumber
Definition: AODV.h:319
AODV_PRECURSORS_LIST * ListofPrecursors
Definition: AODV.h:325
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
unsigned long long int nPrevEventId
Definition: Stack.h:759
NETSIM_ID nInterfaceId
Definition: Stack.h:751