NetSim Source Code Help
Loading...
Searching...
No Matches
OSPF_DR.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#include "main.h"
15#include "NetSim_utility.h"
16#include "OSPF.h"
17#include "OSPF_enum.h"
18#include "OSPF_Msg.h"
19#include "OSPF_Neighbor.h"
20#include "OSPF_Interface.h"
21#include "OSPF_List.h"
22
23typedef struct
24{
32
34{
35 free(d);
36}
37
39 ptrOSPF_IF thisInterface,
40 ptrOSPFLIST eligibleRouterList)
41{
42 ptrdrEligibleRouter newRouter;
43
44 if (thisInterface->RouterPriority > 0)
45 {
46 newRouter = calloc(1, sizeof* newRouter);
47 newRouter->routerId = ospf->routerId;
48 newRouter->routerPriority = thisInterface->RouterPriority;
49 newRouter->routerAddr = thisInterface->IPIfAddr;
50 newRouter->DesignatedRouter = thisInterface->designaterRouterAddr;
51 newRouter->BackupDesignatedRouter = thisInterface->backupDesignaterRouterAddr;
52 ospf_list_add_mem(eligibleRouterList, newRouter);
53 }
54
55 ptrOSPF_NEIGHBOR neigh;
56 UINT i;
57 for (i = 0; i < thisInterface->neighborRouterCount; i++)
58 {
59 neigh = thisInterface->neighborRouterList[i];
60 if (neigh->state >= OSPFNEIGHSTATE_2Way &&
61 neigh->neighborPriority > 0)
62 {
63 newRouter = calloc(1, sizeof* newRouter);
64 newRouter->routerId = neigh->neighborId;
65 newRouter->routerPriority = neigh->neighborPriority;
66 newRouter->routerAddr = neigh->neighborIPAddr;
67 newRouter->DesignatedRouter = neigh->neighborDesignateRouter;
69 ospf_list_add_mem(eligibleRouterList, newRouter);
70 }
71 }
72}
73
75 ptrOSPF_IF thisInterface,
76 ptrdrEligibleRouter eligibleRouter)
77{
78 ptrdrEligibleRouter routerInfo;
79 void* pass = ospf_list_newIterator();
80 bool flag = false;
81 ptrdrEligibleRouter tempBDR = NULL;
82
83 while ((routerInfo = ospf_list_iterate_mem(eligibleRouter, pass)) != NULL)
84 {
85 // If the router declared itself to be DR
86 // it is not eligible to become BDR
87 if (!OSPFID_COMPARE(routerInfo->routerId,routerInfo->DesignatedRouter))
88 continue;
89 // If neighbor declared itself to be BDR
90 if (!OSPFID_COMPARE(routerInfo->routerId, routerInfo->BackupDesignatedRouter))
91 {
92 if (flag &&
93 tempBDR &&
94 (tempBDR->routerPriority > routerInfo->routerPriority ||
95 (tempBDR->routerPriority == routerInfo->routerPriority &&
96 tempBDR->routerId->int_ip > routerInfo->routerId->int_ip)))
97 {
98 // do nothing
99 }
100 else
101 {
102 tempBDR = routerInfo;
103 flag = TRUE;
104 }
105 }
106 else if (!flag)
107 {
108 if (tempBDR &&
109 (tempBDR->routerPriority > routerInfo->routerPriority ||
110 (tempBDR->routerPriority == routerInfo->routerPriority &&
111 tempBDR->routerId->int_ip > routerInfo->routerId->int_ip)))
112 {
113 // do nothing
114 }
115 else
116 {
117 tempBDR = routerInfo;
118 }
119 }
120 }
122
123 // Set BDR to this interface
124 if (tempBDR)
125 {
126 thisInterface->backupDesignaterRouter = tempBDR->routerId;
127 thisInterface->backupDesignaterRouterAddr = tempBDR->routerAddr;
128 }
129 else
130 {
131 thisInterface->backupDesignaterRouter = NULL;
132 thisInterface->backupDesignaterRouterAddr = NULL;
133 }
134}
135
137 ptrOSPF_IF thisInterface,
138 ptrdrEligibleRouter eligibleRouter)
139{
140 ptrdrEligibleRouter routerInfo;
141 void* pass = ospf_list_newIterator();
142 ptrdrEligibleRouter tempDR = NULL;
143
144 while ((routerInfo = ospf_list_iterate_mem(eligibleRouter, pass)) != NULL)
145 {
146 if (!OSPFID_COMPARE(routerInfo->routerAddr, routerInfo->DesignatedRouter))
147 {
148 if (tempDR &&
149 (tempDR->routerPriority > routerInfo->routerPriority ||
150 (tempDR->routerPriority == routerInfo->routerPriority &&
151 tempDR->routerId->int_ip > routerInfo->routerId->int_ip)))
152 {
153 // do nothing
154 }
155 else
156 {
157 tempDR = routerInfo;
158 }
159 }
160 }
162
163 // Set DR to this interface
164 if (tempDR)
165 {
166 thisInterface->designaterRouter = tempDR->routerId;
167 thisInterface->designaterRouterAddr = tempDR->routerAddr;
168 }
169 else
170 {
171 thisInterface->designaterRouter = thisInterface->backupDesignaterRouter;
172 thisInterface->designaterRouterAddr = thisInterface->backupDesignaterRouterAddr;
173 }
174
175 // Return new interface state
176 if (!OSPFID_COMPARE(thisInterface->designaterRouter, ospf->routerId))
177 {
178 return OSPFIFSTATE_DR;
179 }
180 else if (!OSPFID_COMPARE(thisInterface->backupDesignaterRouter, ospf->routerId))
181 {
182 return OSPFIFSTATE_BACKUP;
183 }
184 else
185 {
186 return OSPFIFSTATE_DROther;
187 }
188}
189
191 ptrOSPF_IF thisInterface)
192{
193 ptrOSPFLIST eligibleRoutersList = ospf_list_init(drEligibleRouter_free, NULL);
194 OSPFID oldDR;
195 OSPFID oldBDR;
196 OSPFIFSTATE oldState;
197 OSPFIFSTATE newState;
198
200 thisInterface,
201 eligibleRoutersList);
202
203 // RFC-2328, Section: 9.4.1
204 oldDR = thisInterface->designaterRouter;
205 oldBDR = thisInterface->backupDesignaterRouter;
206 oldState = thisInterface->State;
207
208 // RFC-2328, Section: 9.4.2 & 9.4.3
209 // First election of DR and BDR
210 ospf_DR_electBDR(ospf, thisInterface, eligibleRoutersList);
211 newState = ospf_DR_electDR(ospf, thisInterface, eligibleRoutersList);
212 ospf_list_delete_all(eligibleRoutersList);
213
214 // RFC-2328, Section: 9.4.4
215 if (newState != oldState &&
216 (newState != OSPFIFSTATE_DROther || oldState > OSPFIFSTATE_DROther))
217 {
218 eligibleRoutersList = ospf_list_init(drEligibleRouter_free, NULL);
220 thisInterface,
221 eligibleRoutersList);
222
223 ospf_DR_electBDR(ospf, thisInterface, eligibleRoutersList);
224 newState = ospf_DR_electDR(ospf, thisInterface, eligibleRoutersList);
225 ospf_list_delete_all(eligibleRoutersList);
226 }
227
228 print_ospf_log(OSPF_LOG, "Router %d declare DR = %s and BDR = %s\n"
229 "on interface %d at time %0.3lf",
230 ospf->myId,
231 thisInterface->designaterRouter->str_ip,
232 thisInterface->backupDesignaterRouter->str_ip,
233 thisInterface->id,
235
236
237 // RFC-2328, Section: 9.4.7
238 if (OSPFID_COMPARE(oldDR, thisInterface->designaterRouter) ||
239 OSPFID_COMPARE(oldBDR, thisInterface->backupDesignaterRouter))
240 {
241 UINT i;
242 for (i = 0; i < thisInterface->neighborRouterCount; i++)
243 {
244 ptrOSPF_NEIGHBOR neigh = thisInterface->neighborRouterList[i];
245 if (neigh->state == OSPFNEIGHSTATE_2Way)
246 {
248 ospf->myId,
249 thisInterface->id,
250 OSPF_Adjok,
251 NULL,
252 neigh);
253 }
254 }
255 }
256 return newState;
257}
#define UINT
Definition: Linux.h:38
#define TRUE
Definition: Linux.h:77
#define UINT16
Definition: Linux.h:33
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
#define OSPF_CURR_TIME()
Definition: OSPF.h:40
void print_ospf_log(OSPFLOGFLAG logFlag, char *format,...)
@ OSPF_LOG
Definition: OSPF.h:31
#define ospf_event_add(time, d, in, subevent, packet, eventdata)
Definition: OSPF.h:400
static void drEligibleRouter_free(ptrdrEligibleRouter d)
Definition: OSPF_DR.c:33
static OSPFIFSTATE ospf_DR_electDR(ptrOSPF_PDS ospf, ptrOSPF_IF thisInterface, ptrdrEligibleRouter eligibleRouter)
Definition: OSPF_DR.c:136
OSPFIFSTATE ospf_DR_election(ptrOSPF_PDS ospf, ptrOSPF_IF thisInterface)
Definition: OSPF_DR.c:190
static void ospf_DR_electBDR(ptrOSPF_PDS ospf, ptrOSPF_IF thisInterface, ptrdrEligibleRouter eligibleRouter)
Definition: OSPF_DR.c:74
static void ospf_DR_listEligibleRouter(ptrOSPF_PDS ospf, ptrOSPF_IF thisInterface, ptrOSPFLIST eligibleRouterList)
Definition: OSPF_DR.c:38
struct drEligibleRouter * ptrdrEligibleRouter
@ OSPFIFSTATE_DR
@ OSPFIFSTATE_BACKUP
@ OSPFIFSTATE_DROther
#define ospf_list_add_mem(list, mem)
Definition: OSPF_List.h:30
void ospf_list_deleteIterator(void *ite)
void ospf_list_delete_all(ptrOSPFLIST list)
void * ospf_list_iterate_mem(ptrOSPFLIST list, void *iterator)
ptrOSPFLIST ospf_list_init(void(*freeMem)(void *), void *(*copyMem)(void *))
void * ospf_list_newIterator()
@ OSPFNEIGHSTATE_2Way
Definition: OSPF_Neighbor.h:28
#define OSPFID_COMPARE
Definition: OSPF_Typedef.h:42
void * ptrOSPFLIST
Definition: OSPF_Typedef.h:50
enum enum_if_state OSPFIFSTATE
Definition: OSPF_Typedef.h:37
#define MILLISECOND
Definition: Stack.h:59
NETSIM_IPAddress routerAddr
Definition: OSPF_DR.c:27
OSPFID BackupDesignatedRouter
Definition: OSPF_DR.c:30
OSPFID DesignatedRouter
Definition: OSPF_DR.c:29
OSPFID routerId
Definition: OSPF_DR.c:25
UINT16 routerPriority
Definition: OSPF_DR.c:26
UINT routerOption
Definition: OSPF_DR.c:28
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54
unsigned int int_ip[4]
Definition: IP_Addressing.h:51
ptrOSPF_NEIGHBOR * neighborRouterList
OSPFID backupDesignaterRouter
NETSIM_IPAddress IPIfAddr
NETSIM_IPAddress designaterRouterAddr
NETSIM_IPAddress backupDesignaterRouterAddr
UINT8 RouterPriority
OSPFID designaterRouter
NETSIM_ID id
OSPFIFSTATE State
UINT neighborRouterCount
OSPFID neighborDesignateBackupRouter
Definition: OSPF_Neighbor.h:62
OSPFNEIGHSTATE state
Definition: OSPF_Neighbor.h:47
OSPFID neighborDesignateRouter
Definition: OSPF_Neighbor.h:61
NETSIM_IPAddress neighborIPAddr
Definition: OSPF_Neighbor.h:58
OSPFID routerId
Definition: OSPF.h:114
NETSIM_ID myId
Definition: OSPF.h:113