00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ns3/log.h"
00020 #include "ns3/object.h"
00021 #include "ipv4-global-routing.h"
00022 #include "ns3/packet.h"
00023 #include "ns3/node.h"
00024
00025 NS_LOG_COMPONENT_DEFINE ("Ipv4GlobalRouting");
00026
00027 namespace ns3 {
00028
00029 NS_OBJECT_ENSURE_REGISTERED (Ipv4GlobalRouting);
00030
00031 TypeId
00032 Ipv4GlobalRouting::GetTypeId (void)
00033 {
00034 static TypeId tid = TypeId ("ns3::Ipv4GlobalRouting")
00035 .SetParent<Object> ()
00036 ;
00037 return tid;
00038 }
00039
00040 Ipv4GlobalRouting::Ipv4GlobalRouting ()
00041 {
00042 NS_LOG_FUNCTION_NOARGS ();
00043 }
00044
00045 void
00046 Ipv4GlobalRouting::AddHostRouteTo (Ipv4Address dest,
00047 Ipv4Address nextHop,
00048 uint32_t interface)
00049 {
00050 NS_LOG_FUNCTION (dest << nextHop << interface);
00051 Ipv4Route *route = new Ipv4Route ();
00052 *route = Ipv4Route::CreateHostRouteTo (dest, nextHop, interface);
00053 m_hostRoutes.push_back (route);
00054 }
00055
00056 void
00057 Ipv4GlobalRouting::AddHostRouteTo (Ipv4Address dest,
00058 uint32_t interface)
00059 {
00060 NS_LOG_FUNCTION (dest << interface);
00061 Ipv4Route *route = new Ipv4Route ();
00062 *route = Ipv4Route::CreateHostRouteTo (dest, interface);
00063 m_hostRoutes.push_back (route);
00064 }
00065
00066 void
00067 Ipv4GlobalRouting::AddNetworkRouteTo (Ipv4Address network,
00068 Ipv4Mask networkMask,
00069 Ipv4Address nextHop,
00070 uint32_t interface)
00071 {
00072 NS_LOG_FUNCTION (network << networkMask << nextHop << interface);
00073 Ipv4Route *route = new Ipv4Route ();
00074 *route = Ipv4Route::CreateNetworkRouteTo (network,
00075 networkMask,
00076 nextHop,
00077 interface);
00078 m_networkRoutes.push_back (route);
00079 }
00080
00081 void
00082 Ipv4GlobalRouting::AddNetworkRouteTo (Ipv4Address network,
00083 Ipv4Mask networkMask,
00084 uint32_t interface)
00085 {
00086 NS_LOG_FUNCTION (network << networkMask << interface);
00087 Ipv4Route *route = new Ipv4Route ();
00088 *route = Ipv4Route::CreateNetworkRouteTo (network,
00089 networkMask,
00090 interface);
00091 m_networkRoutes.push_back (route);
00092 }
00093
00094 Ipv4Route *
00095 Ipv4GlobalRouting::LookupGlobal (Ipv4Address dest)
00096 {
00097 NS_LOG_FUNCTION_NOARGS ();
00098 for (HostRoutesCI i = m_hostRoutes.begin ();
00099 i != m_hostRoutes.end ();
00100 i++)
00101 {
00102 NS_ASSERT ((*i)->IsHost ());
00103 if ((*i)->GetDest ().IsEqual (dest))
00104 {
00105 NS_LOG_LOGIC ("Found global host route" << *i);
00106 return (*i);
00107 }
00108 }
00109 for (NetworkRoutesI j = m_networkRoutes.begin ();
00110 j != m_networkRoutes.end ();
00111 j++)
00112 {
00113 NS_ASSERT ((*j)->IsNetwork ());
00114 Ipv4Mask mask = (*j)->GetDestNetworkMask ();
00115 Ipv4Address entry = (*j)->GetDestNetwork ();
00116 if (mask.IsMatch (dest, entry))
00117 {
00118 NS_LOG_LOGIC ("Found global network route" << *j);
00119 return (*j);
00120 }
00121 }
00122 return 0;
00123 }
00124
00125 uint32_t
00126 Ipv4GlobalRouting::GetNRoutes (void)
00127 {
00128 NS_LOG_FUNCTION_NOARGS ();
00129 uint32_t n = 0;
00130 n += m_hostRoutes.size ();
00131 n += m_networkRoutes.size ();
00132 return n;
00133 }
00134
00135 Ipv4Route *
00136 Ipv4GlobalRouting::GetRoute (uint32_t index)
00137 {
00138 NS_LOG_FUNCTION (index);
00139 if (index < m_hostRoutes.size ())
00140 {
00141 uint32_t tmp = 0;
00142 for (HostRoutesCI i = m_hostRoutes.begin ();
00143 i != m_hostRoutes.end ();
00144 i++)
00145 {
00146 if (tmp == index)
00147 {
00148 return *i;
00149 }
00150 tmp++;
00151 }
00152 }
00153 index -= m_hostRoutes.size ();
00154 uint32_t tmp = 0;
00155 for (NetworkRoutesI j = m_networkRoutes.begin ();
00156 j != m_networkRoutes.end ();
00157 j++)
00158 {
00159 if (tmp == index)
00160 {
00161 return *j;
00162 }
00163 tmp++;
00164 }
00165 NS_ASSERT (false);
00166
00167 return 0;
00168 }
00169 void
00170 Ipv4GlobalRouting::RemoveRoute (uint32_t index)
00171 {
00172 NS_LOG_FUNCTION (index);
00173 if (index < m_hostRoutes.size ())
00174 {
00175 uint32_t tmp = 0;
00176 for (HostRoutesI i = m_hostRoutes.begin ();
00177 i != m_hostRoutes.end ();
00178 i++)
00179 {
00180 if (tmp == index)
00181 {
00182 NS_LOG_LOGIC ("Removing route " << index << "; size = " << m_hostRoutes.size());
00183 delete *i;
00184 m_hostRoutes.erase (i);
00185 NS_LOG_LOGIC ("Done removing host route " << index << "; host route remaining size = " << m_hostRoutes.size());
00186 return;
00187 }
00188 tmp++;
00189 }
00190 }
00191 index -= m_hostRoutes.size ();
00192 uint32_t tmp = 0;
00193 for (NetworkRoutesI j = m_networkRoutes.begin ();
00194 j != m_networkRoutes.end ();
00195 j++)
00196 {
00197 if (tmp == index)
00198 {
00199 NS_LOG_LOGIC ("Removing route " << index << "; size = " << m_networkRoutes.size());
00200 delete *j;
00201 m_networkRoutes.erase (j);
00202 NS_LOG_LOGIC ("Done removing network route " << index << "; network route remaining size = " << m_networkRoutes.size());
00203 return;
00204 }
00205 tmp++;
00206 }
00207 NS_ASSERT (false);
00208 }
00209
00210 bool
00211 Ipv4GlobalRouting::RequestRoute (
00212 uint32_t ifIndex,
00213 Ipv4Header const &ipHeader,
00214 Ptr<Packet> packet,
00215 RouteReplyCallback routeReply)
00216 {
00217 NS_LOG_FUNCTION (this << ifIndex << &ipHeader << packet << &routeReply);
00218
00219 NS_LOG_LOGIC ("source = " << ipHeader.GetSource ());
00220
00221 NS_LOG_LOGIC ("destination = " << ipHeader.GetDestination ());
00222
00223 if (ipHeader.GetDestination ().IsMulticast ())
00224 {
00225 NS_LOG_LOGIC ("Multicast destination-- returning false");
00226 return false;
00227 }
00228
00229
00230
00231 NS_LOG_LOGIC ("Unicast destination- looking up");
00232 Ipv4Route *route = LookupGlobal (ipHeader.GetDestination ());
00233 if (route != 0)
00234 {
00235 routeReply (true, *route, packet, ipHeader);
00236 return true;
00237 }
00238 else
00239 {
00240 return false;
00241
00242 }
00243 }
00244
00245 bool
00246 Ipv4GlobalRouting::RequestIfIndex (Ipv4Address destination, uint32_t& ifIndex)
00247 {
00248 NS_LOG_FUNCTION (this << destination << &ifIndex);
00249
00250
00251
00252
00253 if (destination.IsMulticast ())
00254 {
00255 NS_LOG_LOGIC ("Multicast destination-- returning false");
00256 return false;
00257 }
00258
00259
00260
00261 NS_LOG_LOGIC ("Unicast destination- looking up");
00262 Ipv4Route *route = LookupGlobal (destination);
00263 if (route)
00264 {
00265 ifIndex = route->GetInterface ();
00266 return true;
00267 }
00268 else
00269 {
00270 return false;
00271 }
00272 }
00273
00274 void
00275 Ipv4GlobalRouting::DoDispose (void)
00276 {
00277 NS_LOG_FUNCTION_NOARGS ();
00278 for (HostRoutesI i = m_hostRoutes.begin ();
00279 i != m_hostRoutes.end ();
00280 i = m_hostRoutes.erase (i))
00281 {
00282 delete (*i);
00283 }
00284 for (NetworkRoutesI j = m_networkRoutes.begin ();
00285 j != m_networkRoutes.end ();
00286 j = m_networkRoutes.erase (j))
00287 {
00288 delete (*j);
00289 }
00290 Ipv4RoutingProtocol::DoDispose ();
00291 }
00292
00293 }