00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "olsr-routing-table.h"
00029 #include "ns3/packet.h"
00030 #include "ns3/ipv4-header.h"
00031 #include "ns3/log.h"
00032
00033 namespace ns3 { namespace olsr {
00034
00035 NS_LOG_COMPONENT_DEFINE ("OlsrRoutingTable");
00036
00037
00038
00039
00040 void
00041 RoutingTable::Clear ()
00042 {
00043 NS_LOG_FUNCTION_NOARGS ();
00044 m_table.clear ();
00045 }
00046
00047
00048
00049
00050
00051 void
00052 RoutingTable::RemoveEntry (Ipv4Address const &dest)
00053 {
00054 m_table.erase (dest);
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 bool
00064 RoutingTable::Lookup (Ipv4Address const &dest,
00065 RoutingTableEntry &outEntry) const
00066 {
00067
00068 std::map<Ipv4Address, RoutingTableEntry>::const_iterator it =
00069 m_table.find (dest);
00070
00071 if (it == m_table.end ())
00072 return false;
00073 outEntry = it->second;
00074 return true;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 bool
00094 RoutingTable::FindSendEntry (RoutingTableEntry const &entry,
00095 RoutingTableEntry &outEntry) const
00096 {
00097 outEntry = entry;
00098 while (outEntry.destAddr != outEntry.nextAddr)
00099 {
00100 if (not Lookup(outEntry.nextAddr, outEntry))
00101 return false;
00102 }
00103 return true;
00104 }
00105
00106
00107 bool
00108 RoutingTable::RequestRoute (uint32_t ifIndex,
00109 const Ipv4Header &ipHeader,
00110 Ptr<Packet> packet,
00111 RouteReplyCallback routeReply)
00112 {
00113 RoutingTableEntry entry1, entry2;
00114 if (Lookup (ipHeader.GetDestination (), entry1))
00115 {
00116 bool foundSendEntry = FindSendEntry (entry1, entry2);
00117 if (!foundSendEntry)
00118 NS_FATAL_ERROR ("FindSendEntry failure");
00119
00120 Ipv4Route route = Ipv4Route::CreateHostRouteTo
00121 (ipHeader.GetDestination (), entry2.nextAddr, entry2.interface);
00122
00123 NS_LOG_DEBUG ("Olsr node " << m_mainAddress
00124 << ": RouteRequest for dest=" << ipHeader.GetDestination ()
00125 << " --> nestHop=" << entry2.nextAddr
00126 << " interface=" << entry2.interface);
00127
00128 routeReply (true, route, packet, ipHeader);
00129 return true;
00130 }
00131 else
00132 {
00133 #ifdef NS3_LOG_ENABLE
00134 NS_LOG_DEBUG ("Olsr node " << m_mainAddress
00135 << ": RouteRequest for dest=" << ipHeader.GetDestination ()
00136 << " --> NOT FOUND; ** Dumping routing table...");
00137 for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator iter = m_table.begin ();
00138 iter != m_table.end (); iter++)
00139 {
00140 NS_LOG_DEBUG ("dest=" << iter->first << " --> next=" << iter->second.nextAddr
00141 << " via interface " << iter->second.interface);
00142 }
00143
00144 NS_LOG_DEBUG ("** Routing table dump end.");
00145 #endif
00146 return false;
00147 }
00148 }
00149
00150 bool
00151 RoutingTable::RequestIfIndex (Ipv4Address destination,
00152 uint32_t& ifIndex)
00153 {
00154 RoutingTableEntry entry1, entry2;
00155 if (Lookup (destination, entry1))
00156 {
00157 bool foundSendEntry = FindSendEntry (entry1, entry2);
00158 if (!foundSendEntry)
00159 NS_FATAL_ERROR ("FindSendEntry failure");
00160 ifIndex = entry2.interface;
00161 return true;
00162 }
00163 else
00164 {
00165 return false;
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 void
00181 RoutingTable::AddEntry (Ipv4Address const &dest,
00182 Ipv4Address const &next,
00183 uint32_t interface,
00184 uint32_t distance)
00185 {
00186 NS_LOG_FUNCTION (this << dest << next << interface << distance << m_mainAddress);
00187
00188 NS_ASSERT (distance > 0);
00189
00190
00191 RoutingTableEntry &entry = m_table[dest];
00192
00193 entry.destAddr = dest;
00194 entry.nextAddr = next;
00195 entry.interface = interface;
00196 entry.distance = distance;
00197 }
00198
00199 void
00200 RoutingTable::AddEntry (Ipv4Address const &dest,
00201 Ipv4Address const &next,
00202 Ipv4Address const &interfaceAddress,
00203 uint32_t distance)
00204 {
00205 NS_LOG_FUNCTION (this << dest << next << interfaceAddress << distance << m_mainAddress);
00206
00207 NS_ASSERT (distance > 0);
00208 NS_ASSERT (m_ipv4);
00209
00210 RoutingTableEntry entry;
00211 for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
00212 {
00213 if (m_ipv4->GetAddress (i) == interfaceAddress)
00214 {
00215 AddEntry (dest, next, i, distance);
00216 return;
00217 }
00218 }
00219 NS_ASSERT (false);
00220 AddEntry (dest, next, 0, distance);
00221 }
00222
00223 void
00224 RoutingTable::SetMainAddress (Ipv4Address mainAddress)
00225 {
00226 m_mainAddress = mainAddress;
00227 }
00228
00229 void
00230 RoutingTable::SetIpv4 (Ptr<Ipv4> ipv4)
00231 {
00232 m_ipv4 = ipv4;
00233 }
00234
00235 std::vector<RoutingTableEntry>
00236 RoutingTable::GetEntries () const
00237 {
00238 std::vector<RoutingTableEntry> retval;
00239 for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator iter = m_table.begin ();
00240 iter != m_table.end (); iter++)
00241 {
00242 retval.push_back (iter->second);
00243 }
00244 return retval;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 }};
00257
00258
00259
00260 #ifdef RUN_SELF_TESTS
00261
00262
00263 #include "ns3/test.h"
00264
00265
00266 namespace ns3 { namespace olsr {
00267
00268 class OlsrRoutingTableTest : public ns3::Test {
00269 private:
00270 public:
00271 OlsrRoutingTableTest ();
00272 virtual bool RunTests (void);
00273
00274
00275 };
00276
00277 OlsrRoutingTableTest::OlsrRoutingTableTest ()
00278 : ns3::Test ("OlsrRoutingTable")
00279 {}
00280
00281
00282 bool
00283 OlsrRoutingTableTest::RunTests (void)
00284 {
00285 bool result = true;
00286
00287 RoutingTable table;
00288
00289 table.AddEntry (Ipv4Address ("1.2.3.5"),
00290 Ipv4Address ("1.2.3.4"),
00291 0,
00292 1);
00293
00294 table.AddEntry (Ipv4Address ("1.2.3.4"),
00295 Ipv4Address ("1.2.3.4"),
00296 0,
00297 1);
00298
00299 RoutingTableEntry entry1;
00300 NS_TEST_ASSERT (table.Lookup (Ipv4Address ("1.2.3.5"), entry1));
00301 NS_TEST_ASSERT_EQUAL (entry1.destAddr, Ipv4Address ("1.2.3.5"));
00302 NS_TEST_ASSERT_EQUAL (entry1.nextAddr, Ipv4Address ("1.2.3.4"));
00303 NS_TEST_ASSERT_EQUAL (entry1.interface, 0);
00304 NS_TEST_ASSERT_EQUAL (entry1.distance, 1);
00305
00306 RoutingTableEntry entry2;
00307 NS_TEST_ASSERT (table.Lookup (Ipv4Address ("1.2.3.4"), entry2));
00308 NS_TEST_ASSERT_EQUAL (entry2.destAddr, Ipv4Address ("1.2.3.4"));
00309 NS_TEST_ASSERT_EQUAL (entry2.nextAddr, Ipv4Address ("1.2.3.4"));
00310 NS_TEST_ASSERT_EQUAL (entry2.interface, 0);
00311 NS_TEST_ASSERT_EQUAL (entry2.distance, 1);
00312
00313 RoutingTableEntry sendEntry;
00314 NS_TEST_ASSERT (table.FindSendEntry (entry1, sendEntry));
00315 NS_TEST_ASSERT_EQUAL (sendEntry.destAddr, Ipv4Address ("1.2.3.4"));
00316 NS_TEST_ASSERT_EQUAL (sendEntry.nextAddr, Ipv4Address ("1.2.3.4"));
00317 NS_TEST_ASSERT_EQUAL (sendEntry.interface, 0);
00318 NS_TEST_ASSERT_EQUAL (sendEntry.distance, 1);
00319
00320 table.RemoveEntry (Ipv4Address ("1.2.3.5"));
00321 RoutingTableEntry removedEntry;
00322 NS_TEST_ASSERT (not table.Lookup (Ipv4Address ("1.2.3.5"), removedEntry));
00323
00324 return result;
00325 }
00326
00327 static OlsrRoutingTableTest gOlsrRoutingTableTest;
00328
00329 }};
00330
00331
00332 #endif