NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RouteTable.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"
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*/
22
23static FILE* AODV_log_fp = NULL;
24
25int fn_NetSim_AODV_InsertInRouteTable(NETSIM_IPAddress ip,
26 unsigned int seqNumber,
27 unsigned int hopCount,
28 NETSIM_IPAddress nextHop,
29 double lifeTime,
30 NetSim_EVENTDETAILS* pstruEventDetails)
31{
32 AODV_DEVICE_VAR* devVar = AODV_DEV_VAR(pstruEventDetails->nDeviceId);
33 AODV_ROUTETABLE* table = devVar->routeTable;
34 double AODV_log_lifetime = 0;
35 bool flag = false;
36 while(table)
37 {
38 if(!IP_COMPARE(table->DestinationIPAddress,ip) /*&&
39 table->routingFlags == AODV_RoutingFlag_Valid*/)
40 {
41 if(table->ValidDestinationSequenceNumberflag == false)
42 break;
43 if(table->DestinationSequenceNumber < seqNumber)
44 break;
45 if(table->DestinationSequenceNumber > seqNumber)
46 return 1;
47 if(hopCount+1<table->HopCount || table->routingFlags==AODV_RoutingFlag_InValid)
48 break;
49 else
50 return 1;
51 }
52 table = LIST_NEXT(table);
53 }
54 if(!table)
55 {
56 NetSim_EVENTDETAILS pevent;
57 //Create new table
58 table = ROUTETABLE_ALLOC();
59 table->Lifetime = pstruEventDetails->dEventTime+AODV_ACTIVE_ROUTE_TIMEOUT;
60 LIST_ADD_LAST(&AODV_DEV_VAR(pstruEventDetails->nDeviceId)->routeTable,table);
61 //Add an event for active route time out
62 pevent.dEventTime = pstruEventDetails->dEventTime+AODV_ACTIVE_ROUTE_TIMEOUT;
63 pevent.dPacketSize = 0;
64 pevent.nApplicationId = 0;
65 pevent.nDeviceId = pstruEventDetails->nDeviceId;
66 pevent.nDeviceType = pstruEventDetails->nDeviceType;
67 pevent.nEventType = TIMER_EVENT;
68 pevent.nInterfaceId = pstruEventDetails->nInterfaceId;
69 pevent.nPacketId = 0;
70 pevent.nPrevEventId = pstruEventDetails->nPrevEventId;
71 pevent.nProtocolId = NW_PROTOCOL_AODV;
72 pevent.nSegmentId = 0;
73 pevent.nSubEventType = AODVsubevent_ACTIVE_ROUTE_TIMEOUT;
74 pevent.pPacket = NULL;
75 pevent.szOtherDetails = IP_COPY(ip);
76 fnpAddEvent(&pevent);
77 flag = true;
78 }
79 if(!table->DestinationIPAddress)
80 table->DestinationIPAddress = IP_COPY(ip);
81 table->DestinationSequenceNumber = seqNumber;
82 table->HopCount= hopCount;
83 AODV_log_lifetime = table->Lifetime;
84 table->Lifetime = max(table->Lifetime,lifeTime);
85 table->ListofPrecursors = devVar->precursorsList;
86 table->NetworkInterface = 1;
87 if (table->NextHop)
88 IP_FREE(table->NextHop);
89 table->NextHop = IP_COPY(nextHop);
90 table->routingFlags = AODV_RoutingFlag_Valid;
91 if(table->DestinationSequenceNumber)
92 table->ValidDestinationSequenceNumberflag = true;
93 //AODV DEVICE LOG
94 if (flag)
95 fn_NetSim_AODV_Route_Table_Log_Update(pstruEventDetails->nDeviceId, "New Entry");
96 else
97 if (AODV_log_lifetime < lifeTime)
98 fn_NetSim_AODV_Route_Table_Log_Update(pstruEventDetails->nDeviceId, "Lifetime extended");
99 return 1;
100}
101/**
102This function adds the given IP in the ADOV precursor list if not already present.
103*/
104int fn_NetSim_AODV_InsertInPrecursorsList(NETSIM_IPAddress ip,NetSim_EVENTDETAILS* pstruEventDetails)
105{
106 int loop;
107 AODV_PRECURSORS_LIST* precursorsList = AODV_DEV_VAR(pstruEventDetails->nDeviceId)->precursorsList;
108 if(!precursorsList)
109 {
110 precursorsList = calloc(1,sizeof* precursorsList);
111 AODV_DEV_VAR(pstruEventDetails->nDeviceId)->precursorsList = precursorsList;
112 }
113 for(loop=0;loop<precursorsList->count;loop++)
114 {
115 if(!IP_COMPARE(precursorsList->list[loop],ip))
116 return 0; //ip already present in list
117 }
118 if(!precursorsList->count)
119 precursorsList->list = calloc(1,sizeof* precursorsList->list);
120 else
121 precursorsList->list = realloc(precursorsList->list,(precursorsList->count+1)*(sizeof* precursorsList->list));
122 precursorsList->list[precursorsList->count] = IP_COPY(ip);
123 precursorsList->count++;
124 return 1;
125}
126/**
127This function returns the next hop IP to the destination from the Route Table
128*/
129NETSIM_IPAddress fn_NetSim_AODV_FindNextHop(AODV_DEVICE_VAR* devVar,
130 NETSIM_IPAddress dest,
131 NetSim_EVENTDETAILS* pstruEventDetails)
132{
133 AODV_ROUTETABLE* table=devVar->routeTable;
134 while(table)
135 {
136 if(!IP_COMPARE(table->DestinationIPAddress,dest) &&
137 table->routingFlags == AODV_RoutingFlag_Valid)
138 return table->NextHop;
139 table = LIST_NEXT(table);
140 }
141 return NULL;
142}
143/**
144This function returns the Route table with the mentioned destination IP
145*/
146AODV_ROUTETABLE* fnFindRouteTable(AODV_ROUTETABLE* table,NETSIM_IPAddress dest)
147{
148 while(table)
149 {
150 if(!IP_COMPARE(table->DestinationIPAddress,dest))
151 {
152 return table;
153 }
154 table = LIST_NEXT(table);
155 }
156 return NULL;
157}
158/**
159This function updates the lifetime of the Route Table
160*/
161int fn_NetSim_AODV_UpdateRouteTable(NETSIM_IPAddress ip,
162 double lifetime,
163 NetSim_EVENTDETAILS* pstruEventDetails)
164{
165 //AODV DEVICE LOG
166 double AODV_log_lifetime = 0;
167
168 AODV_ROUTETABLE* table = AODV_DEV_VAR(pstruEventDetails->nDeviceId)->routeTable;
169 while(table)
170 {
171 if(!IP_COMPARE(table->NextHop,ip) || !IP_COMPARE(table->DestinationIPAddress,ip))
172 {
173 //AODV DEVICE LOG
174 AODV_log_lifetime = table->Lifetime;
175
176 table->Lifetime = max(table->Lifetime,lifetime);
177 }
178 table=LIST_NEXT(table);
179 }
180 //AODV DEVICE LOG
181 if (AODV_log_lifetime < lifetime)
182 fn_NetSim_AODV_Route_Table_Log_Update(pstruEventDetails->nDeviceId, "Lifetime Extended");
183 return 1;
184}
185/**
186This function adds the timeout event of a Route Table which is equal to the table_LifeTime
187*/
188int fn_NetSim_AODV_ActiveRouteTimeout(NetSim_EVENTDETAILS* pstruEventDetails)
189{
190 int flag = 0;
191 NETSIM_IPAddress dest = (NETSIM_IPAddress)pstruEventDetails->szOtherDetails;
192 AODV_ROUTETABLE* table = AODV_DEV_VAR(pstruEventDetails->nDeviceId)->routeTable;
193 while(table)
194 {
195 if(!IP_COMPARE(table->DestinationIPAddress,dest))
196 {
197 if(table->Lifetime <= pstruEventDetails->dEventTime)
198 {
199 AODV_ROUTETABLE* temp = LIST_NEXT(table);
200 IP_FREE(table->DestinationIPAddress);
201 IP_FREE(table->NextHop);
202 LIST_FREE(&AODV_DEV_VAR(pstruEventDetails->nDeviceId)->routeTable,table);
203 table = temp;
204 continue;
205 }
206 else
207 {
208 //Add new time out event
209 pstruEventDetails->dEventTime = table->Lifetime;
210 pstruEventDetails->szOtherDetails = dest;
211 fnpAddEvent(pstruEventDetails);
212 flag = 1;
213 }
214 }
215 table=(AODV_ROUTETABLE*)LIST_NEXT(table);
216 }
217 //AODV DEVICE LOG
218 if(!flag)
219 {
220 sprintf(comment, "AODV active route timed out");
221 fn_NetSim_AODV_Route_Table_Log_Update(pstruEventDetails->nDeviceId, "Entry Deleted");
222 IP_FREE(dest);
223 }
224 return 1;
225}
226
227int fn_NetSim_AODV_Route_Table_Log_Init()
228{
229 if (!AODV_LOG_ENABLED())
230 return 0; // Skip logging if not enabled
231
232 char AODV_Log_filename[BUFSIZ];
233
234 sprintf(AODV_Log_filename, "%s\\AODV_Route_Table_log.csv", pszIOLogPath);
235
236 AODV_log_fp = fopen(AODV_Log_filename, "w");
237 if (!AODV_log_fp)
238 {
239 fnSystemError("Unable to open %s file", AODV_Log_filename);
240 perror(AODV_Log_filename);
241 }
242 else
243 {
244 fprintf(AODV_log_fp,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", "Time(MicroSeconds)", "Device Name", "DestinationIPAddress",
245 "DestinationSequenceNumber", "NetworkInterface", "HopCount", "NextHop",
246 "Lifetime(Microseconds)", "Event", "Comments");
247
248 fprintf(AODV_log_fp, "\n");
249 if (nDbgFlag) fflush(AODV_log_fp);
250 }
251}
252
253int fn_NetSim_AODV_Route_Table_Log_Update(NETSIM_ID devid, char* msg)
254
255{
256 if (!AODV_LOG_ENABLED())
257 return 0; // Skip logging if not enabled
258
259 if(AODV_log_fp)
260 {
261 AODV_ROUTETABLE* table = AODV_DEV_VAR(devid)->routeTable;
262 while (table)
263 {
264 (table->DestinationIPAddress)->str_ip;
265 fprintf(AODV_log_fp, "%lf,%s,%s, %d,%d,%d,%s,%f,%s,%s", pstruEventDetails->dEventTime,
266 DEVICE_NAME(devid), (table->DestinationIPAddress)->str_ip,
267 table->DestinationSequenceNumber, table->NetworkInterface, table->HopCount,
268 (table->NextHop)->str_ip, table->Lifetime, msg, comment);
269
270 fprintf(AODV_log_fp, "\n");
271 if (nDbgFlag) fflush(AODV_log_fp);
272
273 table = (AODV_ROUTETABLE*)LIST_NEXT(table);
274 }
275 }
276 return 0;
277
278}
279
280void fn_NetSim_AODV_Route_Table_Log_Finish()
281{
282 if (AODV_log_fp)
283 fclose(AODV_log_fp);
284}