00001 // -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- 00002 // 00003 // Copyright (c) 2008 University of Washington 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License version 2 as 00007 // published by the Free Software Foundation; 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 // 00018 // 00019 00020 #ifndef IPV4_GLOBAL_ROUTING_H 00021 #define IPV4_GLOBAL_ROUTING_H 00022 00023 #include <list> 00024 #include <stdint.h> 00025 #include "ns3/ipv4-address.h" 00026 #include "ns3/ipv4-header.h" 00027 #include "ns3/ptr.h" 00028 #include "ns3/ipv4.h" 00029 00030 namespace ns3 { 00031 00032 class Packet; 00033 class NetDevice; 00034 class Ipv4Interface; 00035 class Ipv4Address; 00036 class Ipv4Header; 00037 class Ipv4Route; 00038 class Node; 00039 00040 00041 /** 00042 * @brief Global routing protocol for IP version 4 stacks. 00043 * 00044 * In ns-3 we have the concept of a pluggable routing protocol. Routing 00045 * protocols are added to a list maintained by the Ipv4L3Protocol. Every 00046 * stack gets one routing protocol for free -- the Ipv4StaticRouting routing 00047 * protocol is added in the constructor of the Ipv4L3Protocol (this is the 00048 * piece of code that implements the functionality of the IP layer). 00049 * 00050 * As an option to running a dynamic routing protocol, a GlobalRouteManager 00051 * object has been created to allow users to build routes for all participating 00052 * nodes. One can think of this object as a "routing oracle"; it has 00053 * an omniscient view of the topology, and can construct shortest path 00054 * routes between all pairs of nodes. These routes must be stored 00055 * somewhere in the node, so therefore this class Ipv4GlobalRouting 00056 * is used as one of the pluggable routing protocols. It is kept distinct 00057 * from Ipv4StaticRouting because these routes may be dynamically cleared 00058 * and rebuilt in the middle of the simulation, while manually entered 00059 * routes into the Ipv4StaticRouting may need to be kept distinct. 00060 * 00061 * This class deals with Ipv4 unicast routes only. 00062 * 00063 * @see Ipv4RoutingProtocol 00064 * @see GlobalRouteManager 00065 */ 00066 class Ipv4GlobalRouting : public Ipv4RoutingProtocol 00067 { 00068 public: 00069 static TypeId GetTypeId (void); 00070 /** 00071 * @brief Construct an empty Ipv4GlobalRouting routing protocol, 00072 * 00073 * The Ipv4GlobalRouting class supports host and network unicast routes. 00074 * This method initializes the lists containing these routes to empty. 00075 * 00076 * @see Ipv4GlobalRouting 00077 */ 00078 Ipv4GlobalRouting (); 00079 00080 /** 00081 * @brief Request that a check for a route bw performed and if a route is found 00082 * that the packet be sent on its way using the pre-packaged send callback. 00083 * 00084 * The source and destination IP addresses for the packet in question are found 00085 * in the provided Ipv4Header. There are two major processing forks depending 00086 * on the type of destination address. 00087 * 00088 * If the destination address is unicast then the routing table is consulted 00089 * for a route to the destination and if it is found, the routeReply callback 00090 * is executed to send the packet (with the found route). 00091 * 00092 * If the destination address is a multicast, then the method will return 00093 * false. 00094 * 00095 * @param ifIndex The network interface index over which the packed was 00096 * received. If the packet is from a local source, ifIndex will be set to 00097 * Ipv4RoutingProtocol::IF_INDEX_ANY. 00098 * @param ipHeader the Ipv4Header containing the source and destination IP 00099 * addresses for the packet. 00100 * @param packet The packet to be sent if a route is found. 00101 * @param routeReply A callback that packaged up the call to actually send the 00102 * packet. 00103 * @return Returns true if a route is found and the packet has been sent, 00104 * otherwise returns false indicating that the next routing protocol should 00105 * be consulted. 00106 * 00107 * @see Ipv4GlobalRouting 00108 * @see Ipv4RoutingProtocol 00109 */ 00110 virtual bool RequestRoute (uint32_t ifIndex, 00111 Ipv4Header const &ipHeader, 00112 Ptr<Packet> packet, 00113 RouteReplyCallback routeReply); 00114 00115 /** 00116 * @brief Check to see if we can determine the interface index that will be 00117 * used if a packet is sent to this destination. 00118 * 00119 * This method addresses a problem in the IP stack where a destination address 00120 * must be present and checksummed into the IP header before the actual 00121 * interface over which the packet is sent can be determined. The answer is 00122 * to implement a known and intentional cross-layer violation. This is the 00123 * endpoint of a call chain that started up quite high in the stack (sockets) 00124 * and has found its way down to the Ipv4L3Protocol which is consulting the 00125 * routing protocols for what they would do if presented with a packet of the 00126 * given destination. 00127 * 00128 * If there are multiple paths out of the node, the resolution is performed 00129 * by Ipv4L3Protocol::GetIfIndexforDestination which has access to more 00130 * contextual information that is useful for making a determination. 00131 * 00132 * This method will return false on a multicast address. 00133 * 00134 * @param destination The Ipv4Address if the destination of a hypothetical 00135 * packet. This may be a multicast group address. 00136 * @param ifIndex A reference to the interface index over which a packet 00137 * sent to this destination would be sent. 00138 * @return Returns true if a route is found to the destination that involves 00139 * a single output interface index, otherwise returns false indicating that 00140 * the next routing protocol should be consulted. 00141 * 00142 * @see Ipv4GlobalRouting 00143 * @see Ipv4RoutingProtocol 00144 * @see Ipv4L3Protocol 00145 */ 00146 virtual bool RequestIfIndex (Ipv4Address destination, uint32_t& ifIndex); 00147 00148 /** 00149 * @brief Add a host route to the global routing table. 00150 * 00151 * @param dest The Ipv4Address destination for this route. 00152 * @param nextHop The Ipv4Address of the next hop in the route. 00153 * @param interface The network interface index used to send packets to the 00154 * destination. 00155 * 00156 * @see Ipv4Address 00157 */ 00158 void AddHostRouteTo (Ipv4Address dest, 00159 Ipv4Address nextHop, 00160 uint32_t interface); 00161 /** 00162 * @brief Add a host route to the global routing table. 00163 * 00164 * @param dest The Ipv4Address destination for this route. 00165 * @param interface The network interface index used to send packets to the 00166 * destination. 00167 * 00168 * @see Ipv4Address 00169 */ 00170 void AddHostRouteTo (Ipv4Address dest, 00171 uint32_t interface); 00172 00173 /** 00174 * @brief Add a network route to the global routing table. 00175 * 00176 * @param network The Ipv4Address network for this route. 00177 * @param networkMask The Ipv4Mask to extract the network. 00178 * @param nextHop The next hop in the route to the destination network. 00179 * @param interface The network interface index used to send packets to the 00180 * destination. 00181 * 00182 * @see Ipv4Address 00183 */ 00184 void AddNetworkRouteTo (Ipv4Address network, 00185 Ipv4Mask networkMask, 00186 Ipv4Address nextHop, 00187 uint32_t interface); 00188 00189 /** 00190 * @brief Add a network route to the global routing table. 00191 * 00192 * @param network The Ipv4Address network for this route. 00193 * @param networkMask The Ipv4Mask to extract the network. 00194 * @param interface The network interface index used to send packets to the 00195 * destination. 00196 * 00197 * @see Ipv4Address 00198 */ 00199 void AddNetworkRouteTo (Ipv4Address network, 00200 Ipv4Mask networkMask, 00201 uint32_t interface); 00202 00203 /** 00204 * @brief Get the number of individual unicast routes that have been added 00205 * to the routing table. 00206 * 00207 * @warning The default route counts as one of the routes. 00208 */ 00209 uint32_t GetNRoutes (void); 00210 00211 /** 00212 * @brief Get a route from the global unicast routing table. 00213 * 00214 * Externally, the unicast global routing table appears simply as a table with 00215 * n entries. The one sublety of note is that if a default route has been set 00216 * it will appear as the zeroth entry in the table. This means that if you 00217 * add only a default route, the table will have one entry that can be accessed 00218 * either by explicity calling GetDefaultRoute () or by calling GetRoute (0). 00219 * 00220 * Similarly, if the default route has been set, calling RemoveRoute (0) will 00221 * remove the default route. 00222 * 00223 * @param i The index (into the routing table) of the route to retrieve. If 00224 * the default route has been set, it will occupy index zero. 00225 * @return If route is set, a pointer to that Ipv4Route is returned, otherwise 00226 * a zero pointer is returned. 00227 * 00228 * @see Ipv4Route 00229 * @see Ipv4GlobalRouting::RemoveRoute 00230 */ 00231 Ipv4Route *GetRoute (uint32_t i); 00232 00233 /** 00234 * @brief Remove a route from the global unicast routing table. 00235 * 00236 * Externally, the unicast global routing table appears simply as a table with 00237 * n entries. The one sublety of note is that if a default route has been set 00238 * it will appear as the zeroth entry in the table. This means that if the 00239 * default route has been set, calling RemoveRoute (0) will remove the 00240 * default route. 00241 * 00242 * @param i The index (into the routing table) of the route to remove. If 00243 * the default route has been set, it will occupy index zero. 00244 * 00245 * @see Ipv4Route 00246 * @see Ipv4GlobalRouting::GetRoute 00247 * @see Ipv4GlobalRouting::AddRoute 00248 */ 00249 void RemoveRoute (uint32_t i); 00250 00251 protected: 00252 void DoDispose (void); 00253 00254 private: 00255 typedef std::list<Ipv4Route *> HostRoutes; 00256 typedef std::list<Ipv4Route *>::const_iterator HostRoutesCI; 00257 typedef std::list<Ipv4Route *>::iterator HostRoutesI; 00258 typedef std::list<Ipv4Route *> NetworkRoutes; 00259 typedef std::list<Ipv4Route *>::const_iterator NetworkRoutesCI; 00260 typedef std::list<Ipv4Route *>::iterator NetworkRoutesI; 00261 00262 Ipv4Route *LookupGlobal (Ipv4Address dest); 00263 00264 HostRoutes m_hostRoutes; 00265 NetworkRoutes m_networkRoutes; 00266 }; 00267 00268 } // Namespace ns3 00269 00270 #endif /* IPV4_GLOBAL_ROUTING_H */