NetSim Source Code Help
Loading...
Searching...
No Matches
PIM_Route_Msg.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#include "PIM_Msg.h"
20
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 {
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
60{
61 bool isMsgArrivesFromBroadcastInterface = false;
62 int ret = -1;
66
67 if (!isMulticastIP(gip))
68 return ret--; //Not a multicast packet
69
71 ptrPIM_GROUP gr = pim_find_group(d, gip);
72 ptrPIM_VAR pim = GET_PIM_VAR(d);
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;
85 NETSIM_ID sid, sin;
86 sid = fn_NetSim_Stack_GetDeviceId_asIP(src, &sin);
87 dummy->pstruNetworkData->szDestIP = DEVICE_PUBLICIP(sid, sin);
89
91
92
93 if (!route)
94 fnNetSimError("Packet is reached to router without valid route to source\n");
95
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
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;
132 if (DEVICE_NWADDRESS(c, ci))
134
135 NetSim_EVENTDETAILS pevent;
136 memcpy(&pevent, pstruEventDetails, sizeof pevent);
137 pevent.dPacketSize = fpacket->pstruNetworkData->dPacketSize;
139 pevent.nInterfaceId = i + 1;
140 pevent.pPacket = fpacket;
141 fnpAddEvent(&pevent);
142 }
143 DROP:
146 return 0;
147}
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
#define IP_IS_IN_SAME_NETWORK_IPV4(ip1, ip2, subnet)
Definition: IP_Addressing.h:97
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
bool isMulticastIP(NETSIM_IPAddress ip)
#define c
#define UINT
Definition: Linux.h:38
#define fnNetSimError(x,...)
Definition: Linux.h:56
ptrPIM_GROUP pim_find_group(NETSIM_ID d, NETSIM_IPAddress addr)
Definition: PIM_Group.c:20
static bool isInPresentInRoute(NETSIM_ID d, NETSIM_ID in, ptrIP_FORWARD_ROUTE route)
Definition: PIM_Route_Msg.c:21
static bool isIamSrc(NetSim_PACKET *packet, NETSIM_ID d)
Definition: PIM_Route_Msg.c:32
#define isBroadcastInterface(d, i)
Definition: PIM_Route_Msg.c:43
int pim_route_msg()
Definition: PIM_Route_Msg.c:59
static bool isIamDR(NETSIM_ID d, NETSIM_ID in)
Definition: PIM_Route_Msg.c:44
#define GET_PIM_VAR(d)
Definition: PIM_SM.h:226
#define DEVICE(DeviceId)
Definition: Stack.h:769
NETSIM_ID fn_NetSim_Stack_GetConnectedDevice(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NETSIM_ID *nConnectedDevId, NETSIM_ID *nConnectedInterfaceId)
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
#define DEVICE_PUBLICIP(DeviceId, InterfaceId)
Definition: Stack.h:809
@ NETWORK_OUT_EVENT
Definition: Stack.h:108
@ NETWORK_LAYER
Definition: Stack.h:96
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
@ INTERFACE_WAN_ROUTER
Definition: Stack.h:254
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asIP(NETSIM_IPAddress ip, NETSIM_ID *nInterfaceId)
#define fn_NetSim_Packet_CopyPacket(pstruPacket)
Definition: main.h:182
#define fn_NetSim_Packet_CreatePacket(layer)
Definition: main.h:186
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
UINT count
Definition: IP.h:146
NETSIM_IPAddress * gateway
Definition: IP.h:148
EVENT_TYPE nEventType
Definition: Stack.h:747
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nDeviceId
Definition: Stack.h:750
NETSIM_ID nInterfaceId
Definition: Stack.h:751
NETSIM_IPAddress szGatewayIP
Definition: Packet.h:200
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
NETSIM_IPAddress * DR
Definition: PIM_SM.h:209