NetSim Source Code Help
Loading...
Searching...
No Matches
RouteCache.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#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{
26 DSR_DEVICE_VAR* devVar = DSR_DEV_VAR(nDeviceId);
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*/
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*/
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*/
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
unsigned int NETSIM_ID
Definition: Animation.h:45
#define ROUTE_CACHE_TIMEOUT
Definition: DSR.h:49
#define DSR_DEV_VAR(dev)
Definition: DSR.h:772
#define ROUTECACHE_ALLOC()
Definition: DSR.h:779
NETSIM_IPAddress dsr_get_curr_ip()
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 free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
int fn_NetSim_DSR_UpdateRouteCache(unsigned int length, NETSIM_IPAddress *address, double dTime)
Definition: RouteCache.c:21
bool fn_NetSim_DSR_ValidateRouteCache(DSR_ROUTE_CACHE *cache, NETSIM_IPAddress *addList, int count)
Definition: RouteCache.c:78
DSR_ROUTE_CACHE * fn_NetSim_DSR_FindCache(DSR_DEVICE_VAR *devVar, NETSIM_IPAddress address, double dTime)
Definition: RouteCache.c:50
int fn_NetSim_DSR_DeleteEntryFromRouteCache(DSR_ROUTE_CACHE **ppcache, NETSIM_IPAddress ip1, NETSIM_IPAddress ip2)
Definition: RouteCache.c:95
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
struct stru_DSR_RouteCache * pstruRouteCache
List of routes to particular destination.
Definition: DSR.h:657
unsigned int nLength
Definition: DSR.h:582
NETSIM_IPAddress * address
Definition: DSR.h:583
double dTimeOutTime
Definition: DSR.h:574
NETSIM_ID nDeviceId
Definition: Stack.h:750