15#include "NetSim_utility.h"
19#include "OSPF_Neighbor.h"
20#include "OSPF_Interface.h"
26 UINT16 routerPriority;
27 NETSIM_IPAddress routerAddr;
29 OSPFID DesignatedRouter;
30 OSPFID BackupDesignatedRouter;
33static void drEligibleRouter_free(ptrdrEligibleRouter d)
38static void ospf_DR_listEligibleRouter(ptrOSPF_PDS ospf,
39 ptrOSPF_IF thisInterface,
40 ptrOSPFLIST eligibleRouterList)
42 ptrdrEligibleRouter newRouter;
44 if (thisInterface->RouterPriority > 0)
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);
55 ptrOSPF_NEIGHBOR neigh;
57 for (i = 0; i < thisInterface->neighborRouterCount; i++)
59 neigh = thisInterface->neighborRouterList[i];
60 if (neigh->state >= OSPFNEIGHSTATE_2Way &&
61 neigh->neighborPriority > 0)
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;
68 newRouter->BackupDesignatedRouter = neigh->neighborDesignateBackupRouter;
69 ospf_list_add_mem(eligibleRouterList, newRouter);
74static void ospf_DR_electBDR(ptrOSPF_PDS ospf,
75 ptrOSPF_IF thisInterface,
76 ptrdrEligibleRouter eligibleRouter)
79 ptrdrEligibleRouter routerInfo;
80 void* pass = ospf_list_newIterator();
82 ptrdrEligibleRouter tempBDR = NULL;
84 while ((routerInfo = ospf_list_iterate_mem(eligibleRouter, pass)) != NULL)
88 if (!OSPFID_COMPARE(routerInfo->routerId,routerInfo->DesignatedRouter))
91 if (!OSPFID_COMPARE(routerInfo->routerId, routerInfo->BackupDesignatedRouter))
95 (tempBDR->routerPriority > routerInfo->routerPriority ||
96 (tempBDR->routerPriority == routerInfo->routerPriority &&
97 tempBDR->routerId->int_ip > routerInfo->routerId->int_ip)))
103 tempBDR = routerInfo;
110 (tempBDR->routerPriority > routerInfo->routerPriority ||
111 (tempBDR->routerPriority == routerInfo->routerPriority &&
112 tempBDR->routerId->int_ip > routerInfo->routerId->int_ip)))
118 tempBDR = routerInfo;
122 ospf_list_deleteIterator(pass);
127 thisInterface->backupDesignaterRouter = tempBDR->routerId;
128 thisInterface->backupDesignaterRouterAddr = tempBDR->routerAddr;
132 thisInterface->backupDesignaterRouter = NULL;
133 thisInterface->backupDesignaterRouterAddr = NULL;
137static OSPFIFSTATE ospf_DR_electDR(ptrOSPF_PDS ospf,
138 ptrOSPF_IF thisInterface,
139 ptrdrEligibleRouter eligibleRouter)
141 ptrdrEligibleRouter routerInfo;
142 void* pass = ospf_list_newIterator();
143 ptrdrEligibleRouter tempDR = NULL;
145 while ((routerInfo = ospf_list_iterate_mem(eligibleRouter, pass)) != NULL)
147 if (!OSPFID_COMPARE(routerInfo->routerAddr, routerInfo->DesignatedRouter))
150 (tempDR->routerPriority > routerInfo->routerPriority ||
151 (tempDR->routerPriority == routerInfo->routerPriority &&
152 tempDR->routerId->int_ip > routerInfo->routerId->int_ip)))
162 ospf_list_deleteIterator(pass);
167 thisInterface->designaterRouter = tempDR->routerId;
168 thisInterface->designaterRouterAddr = tempDR->routerAddr;
172 thisInterface->designaterRouter = thisInterface->backupDesignaterRouter;
173 thisInterface->designaterRouterAddr = thisInterface->backupDesignaterRouterAddr;
177 if (!OSPFID_COMPARE(thisInterface->designaterRouter, ospf->routerId))
179 return OSPFIFSTATE_DR;
181 else if (!OSPFID_COMPARE(thisInterface->backupDesignaterRouter, ospf->routerId))
183 return OSPFIFSTATE_BACKUP;
187 return OSPFIFSTATE_DROther;
191OSPFIFSTATE ospf_DR_election(ptrOSPF_PDS ospf,
192 ptrOSPF_IF thisInterface)
194 ptrOSPFLIST eligibleRoutersList = ospf_list_init(drEligibleRouter_free, NULL);
197 OSPFIFSTATE oldState;
198 OSPFIFSTATE newState;
200 ospf_DR_listEligibleRouter(ospf,
202 eligibleRoutersList);
205 oldDR = thisInterface->designaterRouter;
206 oldBDR = thisInterface->backupDesignaterRouter;
207 oldState = thisInterface->State;
211 ospf_DR_electBDR(ospf, thisInterface, eligibleRoutersList);
212 newState = ospf_DR_electDR(ospf, thisInterface, eligibleRoutersList);
213 ospf_list_delete_all(eligibleRoutersList);
216 if (newState != oldState &&
217 (newState != OSPFIFSTATE_DROther || oldState > OSPFIFSTATE_DROther))
219 eligibleRoutersList = ospf_list_init(drEligibleRouter_free, NULL);
220 ospf_DR_listEligibleRouter(ospf,
222 eligibleRoutersList);
224 ospf_DR_electBDR(ospf, thisInterface, eligibleRoutersList);
225 newState = ospf_DR_electDR(ospf, thisInterface, eligibleRoutersList);
226 ospf_list_delete_all(eligibleRoutersList);
229 print_ospf_log(OSPF_LOG,
"Router %d declare DR = %s and BDR = %s\n"
230 "on interface %d at time %0.3lf",
232 thisInterface->designaterRouter->str_ip,
233 thisInterface->backupDesignaterRouter->str_ip,
235 OSPF_CURR_TIME() / MILLISECOND);
239 if (OSPFID_COMPARE(oldDR, thisInterface->designaterRouter) ||
240 OSPFID_COMPARE(oldBDR, thisInterface->backupDesignaterRouter))
243 for (i = 0; i < thisInterface->neighborRouterCount; i++)
245 ptrOSPF_NEIGHBOR neigh = thisInterface->neighborRouterList[i];
246 if (neigh->state == OSPFNEIGHSTATE_2Way)
248 ospf_event_add(OSPF_CURR_TIME(),