NetSim Source Code Help
Loading...
Searching...
No Matches
IGMP_Router.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 "IGMP.h"
19
21 NETSIM_ID d,
22 bool* isAlreadyJoined)
23{
24 ptrIGMP_ROUTER_DB r = GET_IGMP_ROUTER(d)->database;
25
26 while (r)
27 {
28 if (!IP_COMPARE(r->group, addr))
29 {
30 *isAlreadyJoined = true;
31 return r;
32 }
33 r = LIST_NEXT(r);
34 }
35
36 *isAlreadyJoined = false;
38 r->group = IP_COPY(addr);
40
42
43 return r;
44}
45
47{
48 bool isAlreadyJoined = false;
51 if (!isAlreadyJoined)
52 {
53 print_igmp_log("Calling PIM to join group %s", group->str_ip);
55 }
56 return db;
57}
58
61{
62 ptrIGMP_ROUTER_DB db = GET_IGMP_ROUTER(d)->database;
63 while (db)
64 {
65 if (!IP_COMPARE(db->group, ip))
66 return db;
67 db = LIST_NEXT(db);
68 }
69 return NULL;
70}
71
74{
75 LIST_FREE(&GET_IGMP_ROUTER(d)->database, db);
76}
77
79{
80 NETSIM_ID i;
81 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
82 {
83 if (isBroadcastInterface(d, i + 1))
84 {
85 return true;
86 }
87 }
88 return false;
89}
90
92 NETSIM_ID ifid)
93{
94 UINT i;
95 for (i = 0; i < db->count; i++)
96 if (db->ifids[i] == ifid)
97 return true;
98 return false;
99}
100
102 NETSIM_ID ifid)
103{
104 UINT i;
105 for (i = 0; i < db->count; i++)
106 if (db->ifids[i] != ifid)
107 return true;
108 return false;
109}
110
112 NETSIM_ID d,
113 NETSIM_ID ifid)
114{
115 if (db->isRouteAlreadyAdded)
116 {
117 UINT c = db->count;
118 db->count++;
119 db->ifids = realloc(db->ifids, db->count * sizeof* db->ifids);
120 db->ifids[c] = ifid;
121
122 NETSIM_IPAddress* ip = calloc(db->count, sizeof* ip);
123 UINT i;
124 for (i = 0; i < db->count; i++)
125 ip[i] = DEVICE_NWADDRESS(d, db->ifids[i]);
126
128 db->group,
129 STR_TO_IP4("255.255.255.255"),
130 0,
131 db->group,
132 db->count,
133 ip,
134 db->ifids,
135 330);
136
137 free(ip);
138 }
139 else
140 {
142 db->group,
143 STR_TO_IP4("255.255.255.255"),
144 0,
145 db->group,
146 1,
147 &DEVICE_NWADDRESS(d, ifid),
148 &ifid,
149 330,
150 "Multicast");
151 db->isRouteAlreadyAdded = true;
152 db->ifids = (NETSIM_ID*)calloc(1, sizeof* db->ifids);
153 db->ifids[0] = ifid;
154 db->count = 1;
155 }
156}
157
159{
160 ptrIGMP_VAR var = GET_IGMP_VAR(d);
161
162 var->StartupQueryInterval = var->QueryInterval / 4;
163
165
167 {
168 NETSIM_IPAddress g = STR_TO_IP4("224.0.0.1");
169
170 print_igmp_log("\nRouter %d, Time 0.0: Joining multicast group %s.",
171 d,
172 g->str_ip);
173 print_igmp_log("Sending query msg after %0.0lf.",(double)IGMP_STARTUP_DELAY);
174
177 }
178}
179
181{
182 ptrIGMP_ROUTER_DB db = GET_IGMP_ROUTER(d)->database;
183 while (db)
184 {
185 if (!IP_COMPARE(db->group, ip))
186 {
188 return ACTION_DROP;
189
191 return ACTION_REROUTE;
192 else
193 return ACTION_DROP;
194 }
195 db = LIST_NEXT(db);
196 }
197 return ACTION_DROP;
198}
199
201{
203
205
206 if (isIPForSameDevice(src, d))
207 return false; // No processing required
208
209 print_igmp_log("\nRouter %d, Time %0.0lf: Query received for group address %s.",
210 d,
212 msg->GroupAddress->str_ip);
213
215
216 if (same->int_ip[0] > src->int_ip[0])
217 {
218 print_igmp_log("Received from lower ip address %s. Changing state to NonQuerier",
219 src->str_ip);
220
224
226 {
227 print_igmp_log("Starting Other querier present timer");
231 msg->GroupAddress,
233 }
234 }
235
236 return false;
237}
238
240{
244
248 {
249 // Start as querier
250 print_igmp_log("\nRouter %d, Time %0.0lf: Other querier present timer expire for group address %s. Starting as querier.",
253 db->group->str_ip);
254
257 db->group,
259 }
260 else
261 {
262 //Keep checking
263 print_igmp_log("\nRouter %d, Time %0.0lf: Query is already received for group address %s. Continuing as Non querier.",
266 db->group->str_ip);
267
271 }
272}
273
275 ptrIGMP_MSG msg,
276 NETSIM_ID d)
277{
278 print_igmp_log("\nRouter %d, Time %0.0lf: Report received for group address %s.",
279 d,
281 msg->GroupAddress->str_ip);
282
284
286
288 {
289 print_igmp_log("Router %d, Time %0.0lf: Starting group membership timer for group addr %s",
290 d,
292 msg->GroupAddress->str_ip);
295 msg->GroupAddress,
298 print_igmp_log("Starting query msg");
300 }
303
304 return false;
305}
306
308{
311
312 ptrIGMP_VAR igmp = GET_IGMP_VAR(d);
313
315
317 db->reportRcvTime +
319 {
320 // No member on this group.
321 print_igmp_log("\nRouter %d, Time %0.0lf: Group membership timer expires for group addr %s. Deleting from database.",
322 d,
324 group->str_ip);
326 }
327 else
328 {
329 //Keep checking
330 print_igmp_log("\nRouter %d, Time %0.0lf: Refreshing Group membership timer for group addr %s.",
331 d,
333 group->str_ip);
334
338 }
339}
340
342{
345 while (db)
346 {
347 LIST_FREE(&db, db);
348 }
349 free(r);
350 SET_IGMP_ROUTER(d, NULL);
351}
unsigned int NETSIM_ID
Definition: Animation.h:45
GROUP_MOBILITY * group
Definition: GroupMobility.c:30
void igmp_start_timer(NETSIM_ID d, IP_SUBEVENT sev, NETSIM_IPAddress addr, double time)
Definition: IGMP.c:123
void print_igmp_log(char *format,...)
Definition: IGMP.c:33
@ RouterState_Querier
Definition: IGMP.h:112
@ RouterState_NonQuerier
Definition: IGMP.h:113
#define IGMP_ROUTER_DB_ALLOC()
Definition: IGMP.h:134
#define IGMP_ROUTER_DB_ADD(d, db)
Definition: IGMP.h:135
#define isBroadcastInterface(d, i)
Definition: IGMP.h:49
#define GET_IGMP_ROUTER(d)
Definition: IGMP.h:175
#define IGMP_STARTUP_DELAY
Definition: IGMP.h:47
struct stru_router_database * ptrIGMP_ROUTER_DB
#define GET_IGMP_VAR(d)
Definition: IGMP.h:170
#define SET_IGMP_ROUTER(d, var)
Definition: IGMP.h:177
void send_query_msg(NETSIM_ID d, NETSIM_IPAddress group, double time)
Definition: IGMP_Msg.c:233
bool router_process_report(NetSim_PACKET *packet, ptrIGMP_MSG msg, NETSIM_ID d)
Definition: IGMP_Router.c:274
static void router_delete_multicast_db(NETSIM_ID d, ptrIGMP_ROUTER_DB db)
Definition: IGMP_Router.c:72
ptrIGMP_ROUTER_DB find_or_alloc_multicast_db_router(NETSIM_IPAddress addr, NETSIM_ID d, bool *isAlreadyJoined)
Definition: IGMP_Router.c:20
static bool isInterfacePresentInDatabase(ptrIGMP_ROUTER_DB db, NETSIM_ID ifid)
Definition: IGMP_Router.c:91
ptrIGMP_ROUTER_DB router_get_multicast_db(NETSIM_ID d, NETSIM_IPAddress ip)
Definition: IGMP_Router.c:59
IP_PROTOCOL_ACTION router_is_ip_present_in_db(NETSIM_ID d, NETSIM_IPAddress ip, NetSim_PACKET *packet)
Definition: IGMP_Router.c:180
bool router_process_query(NetSim_PACKET *packet, ptrIGMP_MSG msg, NETSIM_ID d)
Definition: IGMP_Router.c:200
void router_free(NETSIM_ID d)
Definition: IGMP_Router.c:341
void igmp_router_processOtherQuerierPresentTime()
Definition: IGMP_Router.c:239
void igmp_router_init(NETSIM_ID d)
Definition: IGMP_Router.c:158
static void router_add_ip_route(ptrIGMP_ROUTER_DB db, NETSIM_ID d, NETSIM_ID ifid)
Definition: IGMP_Router.c:111
static bool isOtherInterfacePresentInDatabase(ptrIGMP_ROUTER_DB db, NETSIM_ID ifid)
Definition: IGMP_Router.c:101
void igmp_router_ProcessGroupMembershipTimer()
Definition: IGMP_Router.c:307
static ptrIGMP_ROUTER_DB router_join_multicast_group(NETSIM_ID d, NETSIM_IPAddress group)
Definition: IGMP_Router.c:46
static bool isBroadcastInterfacePresent(NETSIM_ID d)
Definition: IGMP_Router.c:78
void pim_join_group(NETSIM_ID d, NETSIM_IPAddress group)
Definition: PIM_SM.c:228
IP_PROTOCOL_ACTION
Definition: IP.h:138
@ ACTION_DROP
Definition: IP.h:139
@ ACTION_REROUTE
Definition: IP.h:141
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)
@ EVENT_IGMP_OtherQuerierPresentTimer
Definition: IP.h:101
@ EVENT_IGMP_GroupMembershipTimer
Definition: IP.h:103
int iptable_change(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress subnet, UINT prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric)
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 LIST_NEXT(ls)
Definition: List.h:29
#define LIST_FREE(ls, mem)
Definition: List.h:32
#define realloc(p, s)
Definition: Memory.h:32
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
#define DEVICE(DeviceId)
Definition: Stack.h:769
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
bool isIPForSameDevice(NETSIM_IPAddress ip, NETSIM_ID d)
#define SECOND
Definition: Stack.h:60
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
NETSIM_IPAddress get_ip_from_same_subnet(NETSIM_ID d, NETSIM_IPAddress ip)
#define IP_WRAPPER_GET(DeviceId)
Definition: Stack.h:801
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
NETSIM_ID nDeviceId
Definition: Stack.h:750
NETSIM_ID nInterfaceId
Definition: Stack.h:751
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
NETSIM_IPAddress GroupAddress
Definition: IGMP.h:80
ptrIGMP_ROUTER_DB database
Definition: IGMP.h:140
double GroupMembershipInterval
Definition: IGMP.h:163
UINT RobustnessVar
Definition: IGMP.h:162
UINT QueryPresentInterval
Definition: IGMP.h:164
UINT StartupQueryInterval
Definition: IGMP.h:157
UINT QueryInterval
Definition: IGMP.h:161
UINT StartupQueryCount
Definition: IGMP.h:158
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54
unsigned int int_ip[4]
Definition: IP_Addressing.h:51
IGMP_ROUTER_STATE state
Definition: IGMP.h:119
double otherQuerierPresentTime
Definition: IGMP.h:122
bool isRouteAlreadyAdded
Definition: IGMP.h:129
bool isGroupMembershipTimerStarted
Definition: IGMP.h:126
double reportRcvTime
Definition: IGMP.h:127
NETSIM_ID * ifids
Definition: IGMP.h:130
bool isOtherquerierTimerStarted
Definition: IGMP.h:121
NETSIM_IPAddress group
Definition: IGMP.h:118