NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RouteCache.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 "DSR.h"
16#include "List.h"
17/**
18This function adds a route to the route cache. This is the entire list of address from
19the device to the target in sequence
20*/
21int fn_NetSim_DSR_UpdateRouteCache(unsigned int length,
22 NETSIM_IPAddress* address,
23 double dTime)
24{
25 NETSIM_ID nDeviceId = pstruEventDetails->nDeviceId;
26 DSR_DEVICE_VAR* devVar = DSR_DEV_VAR(nDeviceId);
27 DSR_ROUTE_CACHE* cache = ROUTECACHE_ALLOC();
28 unsigned int count = 0;
29 while(count<length)
30 {
31 if(!IP_COMPARE(address[count],dsr_get_curr_ip()))
32 break;
33 count++;
34 }
35 count = length-count;
36 cache->nLength = count;
37 cache->dTimeOutTime = dTime+ROUTE_CACHE_TIMEOUT;
38 cache->address = calloc(count,sizeof* cache->address);
39 while(count--)
40 {
41 cache->address[count] = IP_COPY(address[--length]);
42 }
43 LIST_ADD_LAST(&devVar->pstruRouteCache,cache);
44 return 0;
45}
46/**
47This function searches the route cache for a route to the target from the device.
48If route is found, it returns the route.
49*/
50DSR_ROUTE_CACHE* fn_NetSim_DSR_FindCache(DSR_DEVICE_VAR* devVar,NETSIM_IPAddress address,double dTime)
51{
52 DSR_ROUTE_CACHE* cache=devVar->pstruRouteCache;
53 while(cache)
54 {
55 unsigned int nLoop;
56 if(cache->dTimeOutTime <= dTime)
57 {
58 DSR_ROUTE_CACHE* temp = cache;
59 cache = (DSR_ROUTE_CACHE*)LIST_NEXT(cache);
60 LIST_FREE((void**)&devVar->pstruRouteCache,temp);
61 continue; //cache expired
62 }
63 for(nLoop=0;nLoop<cache->nLength;nLoop++)
64 {
65 if(!IP_COMPARE(cache->address[nLoop],address))
66 {
67 cache->dTimeOutTime = dTime+ROUTE_CACHE_TIMEOUT;
68 return cache;
69 }
70 }
71 cache = LIST_NEXT(cache);
72 }
73 return NULL;
74}
75/**
76If the route cache contains the address in the addList, it returns a false.
77*/
78bool fn_NetSim_DSR_ValidateRouteCache(DSR_ROUTE_CACHE* cache,NETSIM_IPAddress* addList,int count)
79{
80 unsigned int loop;
81 int loop2;
82 for(loop=0;loop<cache->nLength;loop++)
83 {
84 for(loop2=0;loop2<count;loop2++)
85 {
86 if(!IP_COMPARE(cache->address[loop],addList[loop2]))
87 return false;
88 }
89 }
90 return true;
91}
92/**
93This function deletes an entry from the route cache.
94*/
95int fn_NetSim_DSR_DeleteEntryFromRouteCache(DSR_ROUTE_CACHE** ppcache,
96 NETSIM_IPAddress ip1,
97 NETSIM_IPAddress ip2)
98{
99 DSR_ROUTE_CACHE* cache = *ppcache;
100 while(cache)
101 {
102 unsigned int loop;
103 int flag = 0;
104 for(loop=0;loop<cache->nLength-1;loop++)
105 {
106 if(!IP_COMPARE(ip1,cache->address[loop]) &&
107 !IP_COMPARE(ip2,cache->address[loop+1]))
108
109 {
110 unsigned int loop2;
111 //cache found
112 for(loop2=0;loop2<cache->nLength;loop2++)
113 IP_FREE(cache->address[loop2]);
114 free(cache->address);
115 LIST_FREE((void**)ppcache,cache);
116 cache=*ppcache;
117 flag=1;
118 break;
119 }
120 }
121 if(flag)
122 continue;
123 cache = (DSR_ROUTE_CACHE*)LIST_NEXT(cache);
124 }
125 return 1;
126}
127
128
129
130
131
struct stru_DSR_RouteCache * pstruRouteCache
List of routes to particular destination.
Definition DSR.h:657
double dTimeOutTime
Definition DSR.h:574