NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
PIM_Route_Msg.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#include "PIM_Msg.h"
20
21static bool isInPresentInRoute(NETSIM_ID d, NETSIM_ID in, ptrIP_FORWARD_ROUTE route)
22{
23 UINT i;
24 for (i = 0; i < route->count; i++)
25 {
26 if (!IP_COMPARE(DEVICE_NWADDRESS(d, in), route->gateway[i]))
27 return true;
28 }
29 return false;
30}
31
32static bool isIamSrc(NetSim_PACKET* packet, NETSIM_ID d)
33{
34 NETSIM_ID i;
35 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
36 {
37 if (!IP_COMPARE(DEVICE_NWADDRESS(d, i + 1), packet->pstruNetworkData->szSourceIP))
38 return true;
39 }
40 return false;
41}
42
43#define isBroadcastInterface(d,i) (DEVICE_INTERFACE(d,i)->nInterfaceType != INTERFACE_WAN_ROUTER)
44static bool isIamDR(NETSIM_ID d, NETSIM_ID in)
45{
46 ptrPIM_VAR pim = GET_PIM_VAR(d);
47
48 if (isBroadcastInterface(d, in))
49 {
50 if (!pim->DR[in - 1] || !IP_COMPARE(pim->DR[in - 1],
51 DEVICE_NWADDRESS(d, in)))
52 return true;
53 else
54 return false;
55 }
56 return true;
57}
58
59int pim_route_msg()
60{
61 bool isMsgArrivesFromBroadcastInterface = false;
62 int ret = -1;
63 NETSIM_ID d = pstruEventDetails->nDeviceId;
64 NetSim_PACKET* packet = pstruEventDetails->pPacket;
65 NETSIM_IPAddress gip = packet->pstruNetworkData->szDestIP;
66
67 if (!isMulticastIP(gip))
68 return ret--; //Not a multicast packet
69
70 NETSIM_IPAddress src = packet->pstruNetworkData->szSourceIP;
71 ptrPIM_GROUP gr = pim_find_group(d, gip);
72 ptrPIM_VAR pim = GET_PIM_VAR(d);
73 NETSIM_ID in = pstruEventDetails->nInterfaceId;
74
75
76 if (!gr)
77 return ret--; //Group is not register.
78
79 if (isIamSrc(packet, d))
80 return ret--; // Let normal routing work
81
82 if (DEVICE_INTERFACE(d, in)->nInterfaceType != INTERFACE_WAN_ROUTER)
83 isMsgArrivesFromBroadcastInterface = true;
84 NetSim_PACKET* dummy = fn_NetSim_Packet_CreatePacket(NETWORK_LAYER);
85 NETSIM_ID sid, sin;
86 sid = fn_NetSim_Stack_GetDeviceId_asIP(src, &sin);
87 dummy->pstruNetworkData->szDestIP = DEVICE_PUBLICIP(sid, sin);
88 dummy->pstruNetworkData->szSourceIP = DEVICE_NWADDRESS(d, 1);
89
90 ptrIP_FORWARD_ROUTE route = fn_NetSim_IP_RoutePacket(dummy, d);
91
92
93 if (!route)
94 fnNetSimError("Packet is reached to router without valid route to source\n");
95
96 fn_NetSim_Packet_FreePacket(dummy);
97
98 if (!isMsgArrivesFromBroadcastInterface)
99 {
100 if (!isInPresentInRoute(d, in, route))
101 goto DROP; //Packet is arrived from non source interface.
102 }
103
104 NETSIM_ID i;
105 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
106 {
107 if (i + 1 == in)
108 continue; //Incoming interface
109
110 if (isBroadcastInterface(d, i + 1))
111 {
112 NETSIM_IPAddress subnet = DEVICE_INTERFACE(d, i + 1)->szSubnetMask;
113 if (IP_IS_IN_SAME_NETWORK_IPV4(src, DEVICE_NWADDRESS(d, i + 1), subnet))
114 continue;
115 }
116
117 if (isInPresentInRoute(d, i+1, route) && !isMsgArrivesFromBroadcastInterface)
118 continue;
119
120 if (!isIamDR(d, i + 1))
121 continue;
122
123
124 NetSim_PACKET* fpacket = fn_NetSim_Packet_CopyPacket(packet);
125 if (isBroadcastInterface(d, i + 1))
126 {
127 fpacket->pstruNetworkData->szNextHopIp = gip;
128 }
129 fpacket->pstruNetworkData->szGatewayIP = DEVICE_NWADDRESS(d, i + 1);
130 NETSIM_ID c, ci;
131 NETSIM_ID l = fn_NetSim_Stack_GetConnectedDevice(d, i + 1, &c, &ci);
132 if (DEVICE_NWADDRESS(c, ci))
133 fpacket->pstruNetworkData->szNextHopIp = DEVICE_NWADDRESS(c, ci);
134
135 NetSim_EVENTDETAILS pevent;
136 memcpy(&pevent, pstruEventDetails, sizeof pevent);
137 pevent.dPacketSize = fpacket->pstruNetworkData->dPacketSize;
138 pevent.nEventType = NETWORK_OUT_EVENT;
139 pevent.nInterfaceId = i + 1;
140 pevent.pPacket = fpacket;
141 fnpAddEvent(&pevent);
142 }
143 DROP:
144 fn_NetSim_Packet_FreePacket(packet);
145 pstruEventDetails->pPacket = NULL;
146 return 0;
147}