NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
IERP_RoutingTable.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 "ZRP.h"
16#include "ZRP_Enum.h"
17
18int flushIerpTable(NODE_IERP* ierp)
19{
20 IERP_ROUTE_TABLE* table= ierp->routeTable;
21 while(table)
22 {
23 if(!table->flag)
24 {
25 LIST_FREE((void**)&ierp->routeTable,table);
26 table= ierp->routeTable;
27 continue;
28 }
29 table=(IERP_ROUTE_TABLE*)LIST_NEXT(table);
30 }
31 return 0;
32}
33ptrIP_ROUTINGTABLE findInIarpTable(ptrIP_ROUTINGTABLE table, NETSIM_IPAddress ip)
34{
35 while (table)
36 {
37 if (!IP_COMPARE(table->networkDestination, ip))
38 {
39 return table;
40 }
41 table = (ptrIP_ROUTINGTABLE)LIST_NEXT(table);
42 }
43 return NULL;
44}
45
46int addToIERPTableFromIARP(NODE_IERP* ierp,ptrIP_ROUTINGTABLE iarpTable)
47{
48 ptrIP_ROUTINGTABLE table= iarpTable;
49 while(iarpTable)
50 {
51 ptrIP_ROUTINGTABLE temp = iarpTable;
52 unsigned int i;
53 IERP_ROUTE_TABLE* entry = IERP_ROUTE_TABLE_ALLOC();
54 entry->DestAddr = IP_COPY(iarpTable->networkDestination);
55 entry->SubnetMask=IP_COPY(iarpTable->netMask);
56 entry->count = iarpTable->Metric-1;
57 if(entry->count)
58 entry->Route = (NETSIM_IPAddress*)calloc(entry->count,sizeof* entry->Route);
59 for(i=0;i<entry->count;i++)
60 {
61 entry->Route[i]=IP_COPY(iarpTable->gateway);
62 iarpTable=findInIarpTable(table,iarpTable->gateway);
63 }
64 LIST_ADD_LAST((void**)&ierp->routeTable,entry);
65 iarpTable=(ptrIP_ROUTINGTABLE)LIST_NEXT(temp);
66 }
67 return 0;
68}
69
70int extractRouteFromreply(IERP_PACKET* reply)
71{
72 unsigned int i;
73 unsigned int count=0;
74 NETSIM_IPAddress current = DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1);
75 NETSIM_IPAddress dest = reply->RouteSourceAddress;
76 NODE_ZRP* zrp=(NODE_ZRP*)DEVICE_NWROUTINGVAR(pstruEventDetails->nDeviceId);
77 NODE_IERP* ierp=(NODE_IERP*)zrp->ierp;
78 IERP_ROUTE_TABLE* table = ierp->routeTable;
79 IERP_ROUTE_TABLE* entry;
80 while(table)
81 {
82 if(!IP_COMPARE(table->DestAddr,dest))
83 return 1;//entry already present
84 table=(IERP_ROUTE_TABLE*)LIST_NEXT(table);
85 }
86 entry=IERP_ROUTE_TABLE_ALLOC();
87 entry->DestAddr=IP_COPY(dest);
88 entry->flag=true;
89 entry->SubnetMask=IP_COPY(DEVICE_INTERFACE(pstruEventDetails->nDeviceId,1)->szSubnetMask);
90 for(i=0;i<reply->IntermediateNodeCount;i++)
91 {
92 if(!IP_COMPARE(reply->IntermediateNodeAddress[i],current))
93 {
94 break;
95 }
96 count++;
97 }
98 entry->count=count;
99 entry->Route=(NETSIM_IPAddress*)calloc(max(count,1),sizeof* entry->Route);
100 for(i=0;i<count;i++)
101 entry->Route[i]=IP_COPY(reply->IntermediateNodeAddress[count-i-1]);
102 LIST_ADD_LAST((void**)&ierp->routeTable,entry);
103 return 0;
104}
105
106int flushIERPTableFromIARP(NODE_IERP* ierp,ptrIP_ROUTINGTABLE iarpTable)
107{
108 IERP_ROUTE_TABLE* table = ierp->routeTable;
109 ptrIP_ROUTINGTABLE temp;
110 while(table)
111 {
112 if(table->flag)
113 {
114 NETSIM_IPAddress nextHop=table->Route[0];
115 temp=iarpTable;
116 while(temp)
117 {
118 if(!IP_COMPARE(temp->networkDestination,nextHop) && temp->Metric==1)
119 break;
120 temp=(ptrIP_ROUTINGTABLE)LIST_NEXT(temp);
121 }
122 if(!temp)
123 {
124 //IARP entry deleted. IERP entry is invalid
125 LIST_FREE((void**)&ierp->routeTable,table);
126 table=ierp->routeTable;
127 continue;
128 }
129 }
130 table=(IERP_ROUTE_TABLE*)LIST_NEXT(table);
131 }
132 return 0;
133}