akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <list> |
| 2 | #include <utility> |
| 3 | #include <algorithm> |
| 4 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 5 | #include "nlsr.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 6 | #include "name-prefix-table.hpp" |
| 7 | #include "name-prefix-table-entry.hpp" |
| 8 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 9 | |
| 10 | |
| 11 | namespace nlsr { |
| 12 | |
| 13 | using namespace std; |
| 14 | |
| 15 | static bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 16 | npteCompare(NamePrefixTableEntry& npte, const string& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 17 | { |
| 18 | return npte.getNamePrefix() == name; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 24 | NamePrefixTable::addEntry(const string& name, RoutingTableEntry& rte, Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 26 | std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(), |
| 27 | m_table.end(), bind(&npteCompare, _1, name)); |
| 28 | if (it == m_table.end()) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | { |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 30 | NamePrefixTableEntry newEntry(name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | newEntry.addRoutingTableEntry(rte); |
| 32 | newEntry.generateNhlfromRteList(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 33 | newEntry.getNexthopList().sort(); |
| 34 | m_table.push_back(newEntry); |
| 35 | if (rte.getNexthopList().getSize() > 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 37 | pnlsr.getFib().update(pnlsr, name, newEntry.getNexthopList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | else |
| 41 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 42 | if (rte.getNexthopList().getSize() > 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | { |
| 44 | (*it).addRoutingTableEntry(rte); |
| 45 | (*it).generateNhlfromRteList(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 46 | (*it).getNexthopList().sort(); |
| 47 | pnlsr.getFib().update(pnlsr, name, (*it).getNexthopList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 48 | } |
| 49 | else |
| 50 | { |
| 51 | (*it).resetRteListNextHop(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 52 | (*it).getNexthopList().reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 53 | pnlsr.getFib().remove(pnlsr, name); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 59 | NamePrefixTable::removeEntry(const string& name, RoutingTableEntry& rte, Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 61 | std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(), |
| 62 | m_table.end(), bind(&npteCompare, _1, name)); |
| 63 | if (it != m_table.end()) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 64 | { |
| 65 | string destRouter = rte.getDestination(); |
| 66 | (*it).removeRoutingTableEntry(rte); |
| 67 | if (((*it).getRteListSize() == 0) && |
| 68 | (!pnlsr.getLsdb().doesLsaExist(destRouter + "/1", 1)) && |
| 69 | (!pnlsr.getLsdb().doesLsaExist(destRouter + "/2", 2)) && |
| 70 | (!pnlsr.getLsdb().doesLsaExist(destRouter + "/3", 3))) |
| 71 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 72 | m_table.erase(it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 73 | pnlsr.getFib().remove(pnlsr, name); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | (*it).generateNhlfromRteList(); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 78 | pnlsr.getFib().update(pnlsr, name, (*it).getNexthopList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 85 | NamePrefixTable::addEntry(const string& name, const string& destRouter, Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 87 | RoutingTableEntry* rteCheck = |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 89 | if (rteCheck != 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 91 | addEntry(name, *(rteCheck) , pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | } |
| 93 | else |
| 94 | { |
| 95 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 96 | addEntry(name, rte, pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 101 | NamePrefixTable::removeEntry(const string& name, const string& destRouter, Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 103 | RoutingTableEntry* rteCheck = |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | pnlsr.getRoutingTable().findRoutingTableEntry(destRouter); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 105 | if (rteCheck != 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 107 | removeEntry(name, *(rteCheck), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
| 111 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 112 | removeEntry(name, rte, pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 117 | NamePrefixTable::updateWithNewRoute(Nlsr& pnlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 119 | for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin(); |
| 120 | it != m_table.end(); ++it) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | { |
| 122 | std::list<RoutingTableEntry> rteList = (*it).getRteList(); |
| 123 | for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin(); |
| 124 | rteit != rteList.end(); ++rteit) |
| 125 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 126 | RoutingTableEntry* rteCheck = |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination()); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 128 | if (rteCheck != 0) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 130 | addEntry((*it).getNamePrefix(), *(rteCheck), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
| 134 | RoutingTableEntry rte((*rteit).getDestination()); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 135 | addEntry((*it).getNamePrefix(), rte, pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 142 | NamePrefixTable::print() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | { |
| 144 | std::cout << "----------------NPT----------------------" << std::endl; |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame^] | 145 | for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin(); |
| 146 | it != m_table.end(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | ++it) |
| 148 | { |
| 149 | cout << (*it) << endl; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | } //namespace nlsr |