NetSim Source Code Help
Loading...
Searching...
No Matches
PIM_Route.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
15#include "main.h"
16#include "List.h"
17#include "IP.h"
18#include "PIM_SM.h"
19
20typedef struct stru_pim_route
21{
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
35{
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 {
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
68 }
69 return NULL;
70}
71
73 NETSIM_ID i,
74 UINT metric,
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
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();
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
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
166{
167 //Create dummy packet
169 packet->pstruNetworkData->szDestIP = dest;
171
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,
179 }
180 else
181 {
182 next = route->nextHop[0];
183 }
184 free(route);
186 return next;
187}
unsigned int NETSIM_ID
Definition: Animation.h:45
ptrIP_FORWARD_ROUTE fn_NetSim_IP_RoutePacket(NetSim_PACKET *packet, NETSIM_ID dev)
Definition: IP_Routing.c:371
ptrIP_ROUTINGTABLE iptable_add(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress subnet, unsigned int prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric, char *type)
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define STR_TO_IP4(ipstr)
Definition: IP_Addressing.h:94
#define c
#define UINT
Definition: Linux.h:38
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define realloc(p, s)
Definition: Memory.h:32
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
static ptrPIM_ROUTE find_route(NETSIM_ID d, NETSIM_ID i, NETSIM_IPAddress dest)
Definition: PIM_Route.c:34
void pim_route_add(NETSIM_ID d, NETSIM_ID i, UINT metric, NETSIM_IPAddress dest)
Definition: PIM_Route.c:72
NETSIM_IPAddress pimroute_find_nexthop(NETSIM_ID d, NETSIM_IPAddress dest)
Definition: PIM_Route.c:165
#define PIM_ROUTE_ADD(ls, m)
Definition: PIM_Route.c:30
#define PIM_ROUTE_ALLOC()
Definition: PIM_Route.c:29
#define PIM_ROUTE_NEXT(m)
Definition: PIM_Route.c:31
static ptrPIM_ROUTE pimRoute
Definition: PIM_Route.c:32
struct stru_pim_route * ptrPIM_ROUTE
struct stru_pim_route PIM_ROUTE
#define DEVICE(DeviceId)
Definition: Stack.h:769
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
@ NETWORK_LAYER
Definition: Stack.h:96
#define IP_WRAPPER_GET(DeviceId)
Definition: Stack.h:801
#define fn_NetSim_Packet_CreatePacket(layer)
Definition: main.h:186
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
NETSIM_IPAddress * nextHop
Definition: IP.h:147
Definition: List.h:43
Structure to store ip routing table.
Definition: IP.h:157
NETSIM_ID * nInterfaceId
Definition: IP.h:170
NETSIM_IPAddress * Interface
Definition: IP.h:162
unsigned int Metric
Definition: IP.h:164
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54
NETSIM_ID d
Definition: PIM_Route.c:22
NETSIM_ID * ifList
Definition: PIM_Route.c:24
UINT interfaceCount
Definition: PIM_Route.c:23
NETSIM_IPAddress dest
Definition: PIM_Route.c:25
_ele * ele
Definition: PIM_Route.c:27
ptrIP_ROUTINGTABLE table
Definition: PIM_Route.c:26