|
1 |
// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- |
2 |
// |
3 |
// Copyright (c) 2008 University of Washington |
4 |
// |
5 |
// This program is free software; you can redistribute it and/or modify |
6 |
// it under the terms of the GNU General Public License version 2 as |
7 |
// published by the Free Software Foundation; |
8 |
// |
9 |
// This program is distributed in the hope that it will be useful, |
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
// GNU General Public License for more details. |
13 |
// |
14 |
// You should have received a copy of the GNU General Public License |
15 |
// along with this program; if not, write to the Free Software |
16 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
// |
18 |
// |
19 |
|
20 |
#ifndef IPV4_GLOBAL_ROUTING_H |
21 |
#define IPV4_GLOBAL_ROUTING_H |
22 |
|
23 |
#include <list> |
24 |
#include <stdint.h> |
25 |
#include "ns3/ipv4-address.h" |
26 |
#include "ns3/ipv4-header.h" |
27 |
#include "ns3/ptr.h" |
28 |
#include "ns3/ipv4.h" |
29 |
|
30 |
namespace ns3 { |
31 |
|
32 |
class Packet; |
33 |
class NetDevice; |
34 |
class Ipv4Interface; |
35 |
class Ipv4Address; |
36 |
class Ipv4Header; |
37 |
class Ipv4Route; |
38 |
class Node; |
39 |
|
40 |
|
41 |
/** |
42 |
* @brief Global routing protocol for IP version 4 stacks. |
43 |
* |
44 |
* In ns-3 we have the concept of a pluggable routing protocol. Routing |
45 |
* protocols are added to a list maintained by the Ipv4L3Protocol. Every |
46 |
* stack gets one routing protocol for free -- the Ipv4StaticRouting routing |
47 |
* protocol is added in the constructor of the Ipv4L3Protocol (this is the |
48 |
* piece of code that implements the functionality of the IP layer). |
49 |
* |
50 |
* As an option to running a dynamic routing protocol, a GlobalRouteManager |
51 |
* object has been created to allow users to build routes for all participating |
52 |
* nodes. One can think of this object as a "routing oracle"; it has |
53 |
* an omniscient view of the topology, and can construct shortest path |
54 |
* routes between all pairs of nodes. These routes must be stored |
55 |
* somewhere in the node, so therefore this class Ipv4GlobalRouting |
56 |
* is used as one of the pluggable routing protocols. It is kept distinct |
57 |
* from Ipv4StaticRouting because these routes may be dynamically cleared |
58 |
* and rebuilt in the middle of the simulation, while manually entered |
59 |
* routes into the Ipv4StaticRouting may need to be kept distinct. |
60 |
* |
61 |
* This class deals with Ipv4 unicast routes only. |
62 |
* |
63 |
* @see Ipv4RoutingProtocol |
64 |
* @see GlobalRouteManager |
65 |
*/ |
66 |
class Ipv4GlobalRouting : public Ipv4RoutingProtocol |
67 |
{ |
68 |
public: |
69 |
static TypeId GetTypeId (void); |
70 |
/** |
71 |
* @brief Construct an empty Ipv4GlobalRouting routing protocol, |
72 |
* |
73 |
* The Ipv4GlobalRouting class supports host and network unicast routes. |
74 |
* This method initializes the lists containing these routes to empty. |
75 |
* |
76 |
* @see Ipv4GlobalRouting |
77 |
*/ |
78 |
Ipv4GlobalRouting (); |
79 |
|
80 |
/** |
81 |
* @brief Request that a check for a route bw performed and if a route is found |
82 |
* that the packet be sent on its way using the pre-packaged send callback. |
83 |
* |
84 |
* The source and destination IP addresses for the packet in question are found |
85 |
* in the provided Ipv4Header. There are two major processing forks depending |
86 |
* on the type of destination address. |
87 |
* |
88 |
* If the destination address is unicast then the routing table is consulted |
89 |
* for a route to the destination and if it is found, the routeReply callback |
90 |
* is executed to send the packet (with the found route). |
91 |
* |
92 |
* If the destination address is a multicast, then the method will return |
93 |
* false. |
94 |
* |
95 |
* @param ifIndex The network interface index over which the packed was |
96 |
* received. If the packet is from a local source, ifIndex will be set to |
97 |
* Ipv4RoutingProtocol::IF_INDEX_ANY. |
98 |
* @param ipHeader the Ipv4Header containing the source and destination IP |
99 |
* addresses for the packet. |
100 |
* @param packet The packet to be sent if a route is found. |
101 |
* @param routeReply A callback that packaged up the call to actually send the |
102 |
* packet. |
103 |
* @return Returns true if a route is found and the packet has been sent, |
104 |
* otherwise returns false indicating that the next routing protocol should |
105 |
* be consulted. |
106 |
* |
107 |
* @see Ipv4GlobalRouting |
108 |
* @see Ipv4RoutingProtocol |
109 |
*/ |
110 |
virtual bool RequestRoute (uint32_t ifIndex, |
111 |
Ipv4Header const &ipHeader, |
112 |
Ptr<Packet> packet, |
113 |
RouteReplyCallback routeReply); |
114 |
|
115 |
/** |
116 |
* @brief Check to see if we can determine the interface index that will be |
117 |
* used if a packet is sent to this destination. |
118 |
* |
119 |
* This method addresses a problem in the IP stack where a destination address |
120 |
* must be present and checksummed into the IP header before the actual |
121 |
* interface over which the packet is sent can be determined. The answer is |
122 |
* to implement a known and intentional cross-layer violation. This is the |
123 |
* endpoint of a call chain that started up quite high in the stack (sockets) |
124 |
* and has found its way down to the Ipv4L3Protocol which is consulting the |
125 |
* routing protocols for what they would do if presented with a packet of the |
126 |
* given destination. |
127 |
* |
128 |
* If there are multiple paths out of the node, the resolution is performed |
129 |
* by Ipv4L3Protocol::GetIfIndexforDestination which has access to more |
130 |
* contextual information that is useful for making a determination. |
131 |
* |
132 |
* This method will return false on a multicast address. |
133 |
* |
134 |
* @param destination The Ipv4Address if the destination of a hypothetical |
135 |
* packet. This may be a multicast group address. |
136 |
* @param ifIndex A reference to the interface index over which a packet |
137 |
* sent to this destination would be sent. |
138 |
* @return Returns true if a route is found to the destination that involves |
139 |
* a single output interface index, otherwise returns false indicating that |
140 |
* the next routing protocol should be consulted. |
141 |
* |
142 |
* @see Ipv4GlobalRouting |
143 |
* @see Ipv4RoutingProtocol |
144 |
* @see Ipv4L3Protocol |
145 |
*/ |
146 |
virtual bool RequestIfIndex (Ipv4Address destination, uint32_t& ifIndex); |
147 |
|
148 |
/** |
149 |
* @brief Add a host route to the global routing table. |
150 |
* |
151 |
* @param dest The Ipv4Address destination for this route. |
152 |
* @param nextHop The Ipv4Address of the next hop in the route. |
153 |
* @param interface The network interface index used to send packets to the |
154 |
* destination. |
155 |
* |
156 |
* @see Ipv4Address |
157 |
*/ |
158 |
void AddHostRouteTo (Ipv4Address dest, |
159 |
Ipv4Address nextHop, |
160 |
uint32_t interface); |
161 |
/** |
162 |
* @brief Add a host route to the global routing table. |
163 |
* |
164 |
* @param dest The Ipv4Address destination for this route. |
165 |
* @param interface The network interface index used to send packets to the |
166 |
* destination. |
167 |
* |
168 |
* @see Ipv4Address |
169 |
*/ |
170 |
void AddHostRouteTo (Ipv4Address dest, |
171 |
uint32_t interface); |
172 |
|
173 |
/** |
174 |
* @brief Add a network route to the global routing table. |
175 |
* |
176 |
* @param network The Ipv4Address network for this route. |
177 |
* @param networkMask The Ipv4Mask to extract the network. |
178 |
* @param nextHop The next hop in the route to the destination network. |
179 |
* @param interface The network interface index used to send packets to the |
180 |
* destination. |
181 |
* |
182 |
* @see Ipv4Address |
183 |
*/ |
184 |
void AddNetworkRouteTo (Ipv4Address network, |
185 |
Ipv4Mask networkMask, |
186 |
Ipv4Address nextHop, |
187 |
uint32_t interface); |
188 |
|
189 |
/** |
190 |
* @brief Add a network route to the global routing table. |
191 |
* |
192 |
* @param network The Ipv4Address network for this route. |
193 |
* @param networkMask The Ipv4Mask to extract the network. |
194 |
* @param interface The network interface index used to send packets to the |
195 |
* destination. |
196 |
* |
197 |
* @see Ipv4Address |
198 |
*/ |
199 |
void AddNetworkRouteTo (Ipv4Address network, |
200 |
Ipv4Mask networkMask, |
201 |
uint32_t interface); |
202 |
|
203 |
/** |
204 |
* @brief Get the number of individual unicast routes that have been added |
205 |
* to the routing table. |
206 |
* |
207 |
* @warning The default route counts as one of the routes. |
208 |
*/ |
209 |
uint32_t GetNRoutes (void); |
210 |
|
211 |
/** |
212 |
* @brief Get a route from the global unicast routing table. |
213 |
* |
214 |
* Externally, the unicast global routing table appears simply as a table with |
215 |
* n entries. The one sublety of note is that if a default route has been set |
216 |
* it will appear as the zeroth entry in the table. This means that if you |
217 |
* add only a default route, the table will have one entry that can be accessed |
218 |
* either by explicity calling GetDefaultRoute () or by calling GetRoute (0). |
219 |
* |
220 |
* Similarly, if the default route has been set, calling RemoveRoute (0) will |
221 |
* remove the default route. |
222 |
* |
223 |
* @param i The index (into the routing table) of the route to retrieve. If |
224 |
* the default route has been set, it will occupy index zero. |
225 |
* @return If route is set, a pointer to that Ipv4Route is returned, otherwise |
226 |
* a zero pointer is returned. |
227 |
* |
228 |
* @see Ipv4Route |
229 |
* @see Ipv4GlobalRouting::RemoveRoute |
230 |
*/ |
231 |
Ipv4Route *GetRoute (uint32_t i); |
232 |
|
233 |
/** |
234 |
* @brief Remove a route from the global unicast routing table. |
235 |
* |
236 |
* Externally, the unicast global routing table appears simply as a table with |
237 |
* n entries. The one sublety of note is that if a default route has been set |
238 |
* it will appear as the zeroth entry in the table. This means that if the |
239 |
* default route has been set, calling RemoveRoute (0) will remove the |
240 |
* default route. |
241 |
* |
242 |
* @param i The index (into the routing table) of the route to remove. If |
243 |
* the default route has been set, it will occupy index zero. |
244 |
* |
245 |
* @see Ipv4Route |
246 |
* @see Ipv4GlobalRouting::GetRoute |
247 |
* @see Ipv4GlobalRouting::AddRoute |
248 |
*/ |
249 |
void RemoveRoute (uint32_t i); |
250 |
|
251 |
protected: |
252 |
void DoDispose (void); |
253 |
|
254 |
private: |
255 |
typedef std::list<Ipv4Route *> HostRoutes; |
256 |
typedef std::list<Ipv4Route *>::const_iterator HostRoutesCI; |
257 |
typedef std::list<Ipv4Route *>::iterator HostRoutesI; |
258 |
typedef std::list<Ipv4Route *> NetworkRoutes; |
259 |
typedef std::list<Ipv4Route *>::const_iterator NetworkRoutesCI; |
260 |
typedef std::list<Ipv4Route *>::iterator NetworkRoutesI; |
261 |
|
262 |
Ipv4Route *LookupGlobal (Ipv4Address dest); |
263 |
|
264 |
HostRoutes m_hostRoutes; |
265 |
NetworkRoutes m_networkRoutes; |
266 |
}; |
267 |
|
268 |
} // Namespace ns3 |
269 |
|
270 |
#endif /* IPV4_GLOBAL_ROUTING_H */ |