NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
PIM_Neighbor.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
20ptrPIM_NEIGHBOR find_neighbor(NETSIM_ID d, NETSIM_IPAddress ip)
21{
22 ptrPIM_NEIGHBOR neigh = GET_PIM_VAR(d)->neighborList;
23 while (neigh)
24 {
25 //Look for primary address
26 if (!IP_COMPARE(neigh->neighborAddr, ip))
27 return neigh;
28
29 //Look for secondary address
30 UINT i;
31 for (i = 0; i < neigh->secondary_address_count; i++)
32 if (!IP_COMPARE(ip, neigh->secondary_address_list[i]))
33 return neigh;
34
35 PIM_NEIGHBOR_NEXT(neigh);
36 }
37 return NULL;
38}
39
40static void add_neighbor(NETSIM_ID d, ptrPIM_NEIGHBOR neigh)
41{
42 GET_PIM_VAR(d)->neighCount++;
43 PIM_NEIGHBOR_ADD(&GET_PIM_VAR(d)->neighborList, neigh);
44}
45
46ptrPIM_NEIGHBOR create_and_add_neighbor(NETSIM_ID d,
47 NETSIM_ID ifId,
48 NETSIM_IPAddress address)
49{
50 ptrPIM_NEIGHBOR neigh = PIM_NEIGHBOR_ALLOC();
51 neigh->incomingInterface = ifId;
52 neigh->neighborAddr = address;
53 add_neighbor(d, neigh);
54 return neigh;
55}
56
57static ptrPIM_NEIGHBOR* get_all_neigh_for_interface(NETSIM_ID d,
58 NETSIM_ID ifid,
59 UINT* c)
60{
61 UINT k = 0;
62 ptrPIM_NEIGHBOR* n;
63 ptrPIM_NEIGHBOR neigh = GET_PIM_VAR(d)->neighborList;
64 while (neigh)
65 {
66 if (neigh->incomingInterface == ifid)
67 {
68 if (k)
69 n = realloc(n, (k + 1) * sizeof* n);
70 else
71 n = calloc(1, sizeof* n);
72 n[k] = neigh;
73 k++;
74 }
75 PIM_NEIGHBOR_NEXT(neigh);
76 }
77 *c = k;
78 return n;
79}
80
81static bool dr_is_better(ptrPIM_NEIGHBOR a, ptrPIM_NEIGHBOR b)
82{
83 if (!a->dr_priority_present || !b->dr_priority_present)
84 {
85 return a->neighborAddr->str_ip > b->neighborAddr->str_ip;
86 }
87 else
88 {
89 return ((a->dr_priority > b->dr_priority) ||
90 (a->dr_priority == b->dr_priority &&
91 a->neighborAddr->str_ip > b->neighborAddr->str_ip));
92 }
93}
94
95void elect_DR(NETSIM_ID d, NETSIM_ID ifid)
96{
97 ptrPIM_VAR var = GET_PIM_VAR(d);
98 UINT c = 0;
99 ptrPIM_NEIGHBOR* neigh = get_all_neigh_for_interface(d, ifid, &c);
100 UINT i;
101
102 PIM_NEIGHBOR me;
103 memset(&me, 0, sizeof me);
104 me.neighborAddr = DEVICE_NWADDRESS(d, ifid);
105 ptrPIM_NEIGHBOR dr = &me;
106 for (i = 0; i < c; i++)
107 {
108 if (dr_is_better(neigh[i], dr))
109 dr = neigh[i];
110 }
111 var->DR[ifid - 1] = dr->neighborAddr;
112 free(neigh);
113}
114
115static bool lan_delay_enabled(NETSIM_ID d, NETSIM_ID I)
116{
117 UINT c = 0;
118 ptrPIM_NEIGHBOR* neigh = get_all_neigh_for_interface(d, I, &c);
119 UINT i;
120
121 for (i = 0; i < c; i++)
122 {
123 if (neigh[i]->lan_prune_delay_present == false)
124 {
125 free(neigh);
126 return false;
127 }
128 }
129 free(neigh);
130 return true;
131}
132
133time_interval Effective_Propagation_Delay(NETSIM_ID d,NETSIM_ID I)
134{
135 if (!lan_delay_enabled(d,I) == false)
136 return Propagation_delay_default;
137
138 time_interval delay = GET_PIM_VAR(d)->propagationDelay;
139 UINT c;
140 ptrPIM_NEIGHBOR* neigh = get_all_neigh_for_interface(d, I, &c);
141 UINT i;
142
143 for (i = 0; i < c; i++)
144 {
145 if (neigh[i]->propagation_delay > delay)
146 delay = neigh[i]->propagation_delay;
147 }
148 free(neigh);
149 return delay;
150}
151
152time_interval Effective_Override_Interval(NETSIM_ID d, NETSIM_ID I)
153{
154 if (!lan_delay_enabled(d, I) == false)
155 return t_override_default;
156
157 time_interval delay = GET_PIM_VAR(d)->overrideInterval;
158 UINT c;
159 ptrPIM_NEIGHBOR* neigh = get_all_neigh_for_interface(d, I, &c);
160 UINT i;
161
162 for (i = 0; i < c; i++)
163 {
164 if (neigh[i]->override_interval > delay)
165 delay = neigh[i]->override_interval;
166 }
167 free(neigh);
168 return delay;
169}
170
171bool Suppression_Enabled(NETSIM_ID d, NETSIM_ID I)
172{
173 if (lan_delay_enabled(d, I) == false)
174 return true;
175
176 UINT c;
177 ptrPIM_NEIGHBOR* neigh = get_all_neigh_for_interface(d, I, &c);
178 UINT i;
179
180 for (i = 0; i < c; i++)
181 {
182 if (neigh[i]->tracking_support == false)
183 {
184 free(neigh);
185 return true;
186 }
187 }
188 free(neigh);
189 return false;
190}