NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Component 3/IP/Multicast.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 "IP.h"
17#include "List.h"
18#include "IGMP.h"
19
20static NETSIM_IPAddress ALL_IN_SUBNET;
21static NETSIM_IPAddress ALL_ROUTER_IN_SUBNET;
22static NETSIM_IPAddress ALL_PIM_ROUTER;
23static NETSIM_IPAddress ALL_SPF_ROUTERS;
24static NETSIM_IPAddress ALL_D_ROUTERS;
25
26void multicast_join_group()
27{
28 NETSIM_IPAddress ip = pstruEventDetails->szOtherDetails;
29
30 IP_DEVVAR* dev = GET_IP_DEVVAR(pstruEventDetails->nDeviceId);
31 if (!dev->isIGMPConfigured)
32 {
33 fnNetSimError("%s is called for device %d without IGMP enable. Please enable IGMP to run multicast\n",
34 __FUNCTION__,
35 pstruEventDetails->nDeviceId);
36 return;
37 }
38 //Call IPGMP to join group
39 igmp_host_join_group(pstruEventDetails->nDeviceId, ip);
40}
41
42static bool isOSPFPacket(NetSim_PACKET* packet)
43{
44 if (!ALL_SPF_ROUTERS)
45 {
46 ALL_SPF_ROUTERS = STR_TO_IP4("224.0.0.5");
47 ALL_D_ROUTERS = STR_TO_IP4("224.0.0.6");
48 }
49
50 NETSIM_IPAddress ip = packet->pstruNetworkData->szDestIP;
51 if (!IP_COMPARE(ip, ALL_SPF_ROUTERS))
52 return true;
53
54 if (!IP_COMPARE(ip, ALL_D_ROUTERS))
55 return true;
56
57 return false;
58}
59
60IP_PROTOCOL_ACTION check_ip_in_multicastgroup(NETSIM_IPAddress ip, NETSIM_ID d, NetSim_PACKET* packet)
61{
62 if (isRouter(d) && isOSPFPacket(packet))
63 return ACTION_MOVEUP;
64
65 if (isIGMPPacket(packet))
66 return ACTION_MOVEUP;
67
68 if (isPIMPacket(packet))
69 {
70 if (isHost(d))
71 return ACTION_DROP;
72 else if (isRouter(d))
73 return ACTION_MOVEUP;
74 }
75
76 if (isRouter(d))
77 return ACTION_REROUTE;
78 //return router_is_ip_present_in_db(d, ip, packet);
79 else if (isHost(d))
80 return host_is_ip_present_in_db(d, ip, packet);
81 else if (isL3Device(d))
82 return ACTION_REROUTE;
83 else
84 return ACTION_DROP; //Unknown type of device.
85}
86
87bool is_reserved_multicast_address(NETSIM_IPAddress ip)
88{
89 static bool init = false;
90 if (!init)
91 {
92 init = true;
93 ALL_PIM_ROUTER = STR_TO_IP4("224.0.0.13");
94 ALL_IN_SUBNET = STR_TO_IP4("224.0.0.1");
95 ALL_ROUTER_IN_SUBNET = STR_TO_IP4("224.0.0.2");
96 ALL_SPF_ROUTERS = STR_TO_IP4("224.0.0.5");
97 ALL_D_ROUTERS = STR_TO_IP4("224.0.0.6");
98 }
99
100 if (!IP_COMPARE(ip, ALL_IN_SUBNET ))
101 return true;
102
103 if (!IP_COMPARE(ip, ALL_PIM_ROUTER))
104 return true;
105
106 if (!IP_COMPARE(ip, ALL_ROUTER_IN_SUBNET))
107 return true;
108
109 if (!IP_COMPARE(ip, ALL_SPF_ROUTERS))
110 return true;
111
112 if (!IP_COMPARE(ip, ALL_D_ROUTERS))
113 return true;
114
115 return false;
116}
117
118IP_ROUTINGTABLE* tab = NULL;
119static bool isCorrectRouteForSubnet(IP_ROUTINGTABLE** table, NETSIM_IPAddress dest, NETSIM_IPAddress src)
120{
121 if (!tab)
122 tab = IPROUTINGTABLE_ALLOC();
123
124 if (IP_COMPARE((*table)->networkDestination, dest))
125 return false;
126
127 memcpy(tab, *table, sizeof* tab);
128 tab->interfaceCount = 0;
129
130 UINT k = 0;
131 UINT i;
132 for (i = 0; i < (*table)->interfaceCount; i++)
133 {
134 if (IP_IS_IN_SAME_NETWORK_IPV4(src, (*table)->Interface[i], (*table)->netMask))
135 {
136 tab->interfaceCount++;
137 tab->Interface[k] = (*table)->Interface[i];
138 tab->nInterfaceId[k] = (*table)->nInterfaceId[i];
139 k++;
140 }
141 }
142 if (k)
143 {
144 *table = tab;
145 return true;
146 }
147 else
148 {
149 *table = NULL;
150 return false;
151 }
152}
153
154bool isCorrectRoute(pptrIP_ROUTINGTABLE table, NETSIM_IPAddress dest, NETSIM_IPAddress src)
155{
156 if (!IP_COMPARE(dest, ALL_IN_SUBNET))
157 return isCorrectRouteForSubnet(table, dest, src);
158
159 if (!IP_COMPARE(dest, ALL_PIM_ROUTER))
160 return true;
161
162 return true;
163}
164