NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
PIM_Route.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 "List.h"
17#include "IP.h"
18#include "PIM_SM.h"
19
20typedef struct stru_pim_route
21{
22 NETSIM_ID d;
23 UINT interfaceCount;
24 NETSIM_ID* ifList;
25 NETSIM_IPAddress dest;
26 ptrIP_ROUTINGTABLE table;
27 _ele* ele;
28}PIM_ROUTE, *ptrPIM_ROUTE;
29#define PIM_ROUTE_ALLOC() (ptrPIM_ROUTE)list_alloc(sizeof(PIM_ROUTE),offsetof(PIM_ROUTE,ele))
30#define PIM_ROUTE_ADD(ls,m) LIST_ADD_LAST((void**)ls,m)
31#define PIM_ROUTE_NEXT(m) (m = LIST_NEXT(m))
32static ptrPIM_ROUTE pimRoute = NULL;
33
34static ptrPIM_ROUTE find_route(NETSIM_ID d, NETSIM_ID i, NETSIM_IPAddress dest)
35{
36 ptrPIM_ROUTE r = pimRoute;
37 while (r)
38 {
39 bool fd = false;
40 bool fi = false;
41 if (r->d == d &&
42 !IP_COMPARE(r->dest, dest))
43 {
44 //Continue further check
45 }
46 else
47 {
48 PIM_ROUTE_NEXT(r);
49 continue;
50 }
51 if (i)
52 {
53 UINT k;
54 for (k = 0; k < r->interfaceCount; k++)
55 {
56 if (r->ifList[k] == i)
57 {
58 return r;
59 }
60 }
61 }
62 else
63 {
64 return r;
65 }
66
67 PIM_ROUTE_NEXT(r);
68 }
69 return NULL;
70}
71
72void pim_route_add(NETSIM_ID d,
73 NETSIM_ID i,
74 UINT metric,
75 NETSIM_IPAddress dest)
76{
77 ptrPIM_ROUTE r = find_route(d, i, dest);
78 if (r)
79 {
80 r->table->Metric = metric;
81 }
82 else
83 {
84 if (i)
85 r = find_route(d, 0, dest);
86 if (r)
87 {
88 //Add new interface only
89 UINT c = r->table->interfaceCount;
90 NETSIM_IPAddress* addr = r->table->Interface;
91 NETSIM_ID* id = r->table->nInterfaceId;
92 NETSIM_ID* rid = r->ifList;
93
94 addr = realloc(addr, (c + 1) * sizeof* addr);
95 addr[c] = IP_COPY(DEVICE_NWADDRESS(d, i));
96 r->table->Interface = addr;
97
98 id = realloc(id, (c + 1) * sizeof* id);
99 id[c] = i;
100 r->table->nInterfaceId = id;
101
102 rid = realloc(rid, (c + 1) * sizeof* rid);
103 rid[c] = i;
104 r->ifList = rid;
105
106 r->interfaceCount++;
107
108 r->table->interfaceCount++;
109
110 r->table->Metric = metric;
111 }
112 else
113 {
114 //Add new route
115 NETSIM_IPAddress* addr;
116 NETSIM_ID* id;
117 UINT c;
118
119 r = PIM_ROUTE_ALLOC();
120 PIM_ROUTE_ADD(&pimRoute, r);
121 r->d = d;
122 r->dest = dest;
123
124 if (i)
125 {
126 c = 1;
127 id = calloc(1, sizeof* id);
128 addr = calloc(1, sizeof* addr);
129 id[0] = i;
130 addr[0] = IP_COPY(DEVICE_NWADDRESS(d, i));
131 r->interfaceCount = 1;
132 r->ifList = calloc(1, sizeof* r->ifList);
133 r->ifList[0] = i;
134 }
135 else
136 {
137 c = DEVICE(d)->nNumOfInterface;
138 id = calloc(c, sizeof* id);
139 addr = calloc(c, sizeof* addr);
140 r->interfaceCount = c;
141 r->ifList = calloc(c, sizeof* r->ifList);
142 UINT k;
143 for (k = 0; k < c; k++)
144 {
145 id[k] = k + 1;
146 addr[k] = IP_COPY(DEVICE_NWADDRESS(d, k + 1));
147 r->ifList[k] = k + 1;
148 }
149 }
150
151 r->table = iptable_add(IP_WRAPPER_GET(d),
152 dest,
153 STR_TO_IP4("255.255.255.255"),
154 0,
155 NULL,
156 c,
157 addr,
158 id,
159 metric,
160 "PIM-MULTICAST");
161 }
162 }
163}
164
165NETSIM_IPAddress pimroute_find_nexthop(NETSIM_ID d, NETSIM_IPAddress dest)
166{
167 //Create dummy packet
168 NetSim_PACKET* packet = fn_NetSim_Packet_CreatePacket(NETWORK_LAYER);
169 packet->pstruNetworkData->szDestIP = dest;
170 packet->pstruNetworkData->szSourceIP = DEVICE_NWADDRESS(d, 1);
171
172 ptrIP_FORWARD_ROUTE route = fn_NetSim_IP_RoutePacket(packet, d);
173 NETSIM_IPAddress next = NULL;
174 if (!route->nextHop)
175 {
176 fnNetSimError("Unicast route is not found for dest %s from %s. Please check/enable routing protocol.",
177 dest->str_ip,
178 packet->pstruNetworkData->szSourceIP->str_ip);
179 }
180 else
181 {
182 next = route->nextHop[0];
183 }
184 free(route);
185 fn_NetSim_Packet_FreePacket(packet);
186 return next;
187}