1 // -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- |
|
2 // |
|
3 // Copyright (c) 2006 Georgia Tech Research Corporation |
|
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 // Author: George F. Riley<riley@ece.gatech.edu> |
|
19 // Gustavo Carneiro <gjc@inescporto.pt> |
|
20 // |
|
21 |
|
22 #ifndef IPV4_STATIC_ROUTING_IMPL_H |
|
23 #define IPV4_STATIC_ROUTING_IMPL_H |
|
24 |
|
25 #include <list> |
|
26 #include <stdint.h> |
|
27 #include "ns3/ipv4-address.h" |
|
28 #include "ns3/ipv4-header.h" |
|
29 #include "ns3/socket.h" |
|
30 #include "ns3/ptr.h" |
|
31 #include "ns3/ipv4.h" |
|
32 #include "ns3/ipv4-static-routing.h" |
|
33 |
|
34 namespace ns3 { |
|
35 |
|
36 class Packet; |
|
37 class NetDevice; |
|
38 class Ipv4Interface; |
|
39 class Ipv4Address; |
|
40 class Ipv4Header; |
|
41 class Ipv4RoutingTableEntry; |
|
42 class Ipv4MulticastRoutingTableEntry; |
|
43 class Node; |
|
44 |
|
45 class Ipv4StaticRoutingImpl : public Ipv4StaticRouting |
|
46 { |
|
47 public: |
|
48 static TypeId GetTypeId (void); |
|
49 |
|
50 Ipv4StaticRoutingImpl (); |
|
51 virtual ~Ipv4StaticRoutingImpl (); |
|
52 |
|
53 virtual Ptr<Ipv4Route> RouteOutput (const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr); |
|
54 |
|
55 virtual bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev, |
|
56 UnicastForwardCallback ucb, MulticastForwardCallback mcb, |
|
57 LocalDeliverCallback lcb, ErrorCallback ecb); |
|
58 |
|
59 virtual void NotifyInterfaceUp (uint32_t interface); |
|
60 virtual void NotifyInterfaceDown (uint32_t interface); |
|
61 virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address); |
|
62 virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address); |
|
63 virtual void SetIpv4 (Ptr<Ipv4> ipv4); |
|
64 |
|
65 |
|
66 /** |
|
67 * \brief Add a host route to the static routing table. |
|
68 * |
|
69 * \param dest The Ipv4Address destination for this route. |
|
70 * \param nextHop The Ipv4Address of the next hop in the route. |
|
71 * \param interface The network interface index used to send packets to the |
|
72 * destination. |
|
73 * |
|
74 * \see Ipv4Address |
|
75 */ |
|
76 void AddHostRouteTo (Ipv4Address dest, |
|
77 Ipv4Address nextHop, |
|
78 uint32_t interface); |
|
79 /** |
|
80 * \brief Add a host route to the static routing table. |
|
81 * |
|
82 * \param dest The Ipv4Address destination for this route. |
|
83 * \param interface The network interface index used to send packets to the |
|
84 * destination. |
|
85 * |
|
86 * \see Ipv4Address |
|
87 */ |
|
88 void AddHostRouteTo (Ipv4Address dest, |
|
89 uint32_t interface); |
|
90 |
|
91 /** |
|
92 * \brief Add a network route to the static routing table. |
|
93 * |
|
94 * \param network The Ipv4Address network for this route. |
|
95 * \param networkMask The Ipv4Mask to extract the network. |
|
96 * \param nextHop The next hop in the route to the destination network. |
|
97 * \param interface The network interface index used to send packets to the |
|
98 * destination. |
|
99 * |
|
100 * \see Ipv4Address |
|
101 */ |
|
102 void AddNetworkRouteTo (Ipv4Address network, |
|
103 Ipv4Mask networkMask, |
|
104 Ipv4Address nextHop, |
|
105 uint32_t interface); |
|
106 |
|
107 /** |
|
108 * \brief Add a network route to the static routing table. |
|
109 * |
|
110 * \param network The Ipv4Address network for this route. |
|
111 * \param networkMask The Ipv4Mask to extract the network. |
|
112 * \param interface The network interface index used to send packets to the |
|
113 * destination. |
|
114 * |
|
115 * \see Ipv4Address |
|
116 */ |
|
117 void AddNetworkRouteTo (Ipv4Address network, |
|
118 Ipv4Mask networkMask, |
|
119 uint32_t interface); |
|
120 |
|
121 /** |
|
122 * \brief Add a default route to the static routing table. |
|
123 * |
|
124 * This method tells the routing system what to do in the case where a specific |
|
125 * route to a destination is not found. The system forwards packets to the |
|
126 * specified node in the hope that it knows better how to route the packet. |
|
127 * |
|
128 * If the default route is set, it is returned as the selected route from |
|
129 * LookupStatic irrespective of destination address if no specific route is |
|
130 * found. |
|
131 * |
|
132 * \param nextHop The Ipv4Address to send packets to in the hope that they |
|
133 * will be forwarded correctly. |
|
134 * \param interface The network interface index used to send packets. |
|
135 * |
|
136 * \see Ipv4Address |
|
137 * \see Ipv4StaticRouting::Lookup |
|
138 */ |
|
139 void SetDefaultRoute (Ipv4Address nextHop, |
|
140 uint32_t interface); |
|
141 |
|
142 /** |
|
143 * \brief Get the number of individual unicast routes that have been added |
|
144 * to the routing table. |
|
145 * |
|
146 * \warning The default route counts as one of the routes. |
|
147 */ |
|
148 uint32_t GetNRoutes (void); |
|
149 |
|
150 /** |
|
151 * \brief Get the default route from the static routing table. |
|
152 * |
|
153 * \return If the default route is set, a pointer to that Ipv4RoutingTableEntry is |
|
154 * returned, otherwise a zero pointer is returned. |
|
155 * |
|
156 * \see Ipv4RoutingTableEntry |
|
157 */ |
|
158 Ipv4RoutingTableEntry GetDefaultRoute (void); |
|
159 |
|
160 /** |
|
161 * \brief Get a route from the static unicast routing table. |
|
162 * |
|
163 * Externally, the unicast static routing table appears simply as a table with |
|
164 * n entries. The one sublety of note is that if a default route has been set |
|
165 * it will appear as the zeroth entry in the table. This means that if you |
|
166 * add only a default route, the table will have one entry that can be accessed |
|
167 * either by explicity calling GetDefaultRoute () or by calling GetRoute (0). |
|
168 * |
|
169 * Similarly, if the default route has been set, calling RemoveRoute (0) will |
|
170 * remove the default route. |
|
171 * |
|
172 * \param i The index (into the routing table) of the route to retrieve. If |
|
173 * the default route has been set, it will occupy index zero. |
|
174 * \return If route is set, a pointer to that Ipv4RoutingTableEntry is returned, otherwise |
|
175 * a zero pointer is returned. |
|
176 * |
|
177 * \see Ipv4RoutingTableEntry |
|
178 * \see Ipv4StaticRouting::RemoveRoute |
|
179 */ |
|
180 Ipv4RoutingTableEntry GetRoute (uint32_t i); |
|
181 |
|
182 /** |
|
183 * \brief Remove a route from the static unicast routing table. |
|
184 * |
|
185 * Externally, the unicast static routing table appears simply as a table with |
|
186 * n entries. The one sublety of note is that if a default route has been set |
|
187 * it will appear as the zeroth entry in the table. This means that if the |
|
188 * default route has been set, calling RemoveRoute (0) will remove the |
|
189 * default route. |
|
190 * |
|
191 * \param i The index (into the routing table) of the route to remove. If |
|
192 * the default route has been set, it will occupy index zero. |
|
193 * |
|
194 * \see Ipv4RoutingTableEntry |
|
195 * \see Ipv4StaticRouting::GetRoute |
|
196 * \see Ipv4StaticRouting::AddRoute |
|
197 */ |
|
198 void RemoveRoute (uint32_t i); |
|
199 |
|
200 /** |
|
201 * \brief Add a multicast route to the static routing table. |
|
202 * |
|
203 * A multicast route must specify an origin IP address, a multicast group and |
|
204 * an input network interface index as conditions and provide a vector of |
|
205 * output network interface indices over which packets matching the conditions |
|
206 * are sent. |
|
207 * |
|
208 * Typically there are two main types of multicast routes: routes of the |
|
209 * first kind are used during forwarding. All of the conditions must be |
|
210 * exlicitly provided. The second kind of routes are used to get packets off |
|
211 * of a local node. The difference is in the input interface. Routes for |
|
212 * forwarding will always have an explicit input interface specified. Routes |
|
213 * off of a node will always set the input interface to a wildcard specified |
|
214 * by the index Ipv4RoutingProtocol::INTERFACE_ANY. |
|
215 * |
|
216 * For routes off of a local node wildcards may be used in the origin and |
|
217 * multicast group addresses. The wildcard used for Ipv4Adresses is that |
|
218 * address returned by Ipv4Address::GetAny () -- typically "0.0.0.0". Usage |
|
219 * of a wildcard allows one to specify default behavior to varying degrees. |
|
220 * |
|
221 * For example, making the origin address a wildcard, but leaving the |
|
222 * multicast group specific allows one (in the case of a node with multiple |
|
223 * interfaces) to create different routes using different output interfaces |
|
224 * for each multicast group. |
|
225 * |
|
226 * If the origin and multicast addresses are made wildcards, you have created |
|
227 * essentially a default multicast address that can forward to multiple |
|
228 * interfaces. Compare this to the actual default multicast address that is |
|
229 * limited to specifying a single output interface for compatibility with |
|
230 * existing functionality in other systems. |
|
231 * |
|
232 * \param origin The Ipv4Address of the origin of packets for this route. May |
|
233 * be Ipv4Address:GetAny for open groups. |
|
234 * \param group The Ipv4Address of the multicast group or this route. |
|
235 * \param inputInterface The input network interface index over which to |
|
236 * expect packets destined for this route. May be |
|
237 * Ipv4RoutingProtocol::INTERFACE_ANY for packets of local origin. |
|
238 * \param outputInterfaces A vector of network interface indices used to specify |
|
239 * how to send packets to the destination(s). |
|
240 * |
|
241 * \see Ipv4Address |
|
242 */ |
|
243 void AddMulticastRoute (Ipv4Address origin, |
|
244 Ipv4Address group, |
|
245 uint32_t inputInterface, |
|
246 std::vector<uint32_t> outputInterfaces); |
|
247 |
|
248 /** |
|
249 * \brief Add a default multicast route to the static routing table. |
|
250 * |
|
251 * This is the multicast equivalent of the unicast version SetDefaultRoute. |
|
252 * We tell the routing system what to do in the case where a specific route |
|
253 * to a destination multicast group is not found. The system forwards |
|
254 * packets out the specified interface in the hope that "something out there" |
|
255 * knows better how to route the packet. This method is only used in |
|
256 * initially sending packets off of a host. The default multicast route is |
|
257 * not consulted during forwarding -- exact routes must be specified using |
|
258 * AddMulticastRoute for that case. |
|
259 * |
|
260 * Since we're basically sending packets to some entity we think may know |
|
261 * better what to do, we don't pay attention to "subtleties" like origin |
|
262 * address, nor do we worry about forwarding out multiple interfaces. If the |
|
263 * default multicast route is set, it is returned as the selected route from |
|
264 * LookupStatic irrespective of origin or multicast group if another specific |
|
265 * route is not found. |
|
266 * |
|
267 * \param outputInterface The network interface index used to specify where |
|
268 * to send packets in the case of unknown routes. |
|
269 * |
|
270 * \see Ipv4Address |
|
271 */ |
|
272 void SetDefaultMulticastRoute (uint32_t outputInterface); |
|
273 |
|
274 /** |
|
275 * \brief Get the number of individual multicast routes that have been added |
|
276 * to the routing table. |
|
277 * |
|
278 * \warning The default multicast route counts as one of the routes. |
|
279 */ |
|
280 uint32_t GetNMulticastRoutes (void) const; |
|
281 |
|
282 /** |
|
283 * \brief Get a route from the static multicast routing table. |
|
284 * |
|
285 * Externally, the multicast static routing table appears simply as a table |
|
286 * with n entries. |
|
287 * |
|
288 * \param i The index (into the routing table) of the multicast route to |
|
289 * retrieve. |
|
290 * \return If route \e i is set, a pointer to that Ipv4MulticastRoutingTableEntry is |
|
291 * returned, otherwise a zero pointer is returned. |
|
292 * |
|
293 * \see Ipv4MulticastRoutingTableEntry |
|
294 * \see Ipv4StaticRouting::RemoveRoute |
|
295 */ |
|
296 Ipv4MulticastRoutingTableEntry GetMulticastRoute (uint32_t i) const; |
|
297 |
|
298 /** |
|
299 * \brief Remove a route from the static multicast routing table. |
|
300 * |
|
301 * Externally, the multicast static routing table appears simply as a table |
|
302 * with n entries. |
|
303 * This method causes the multicast routing table to be searched for the first |
|
304 * route that matches the parameters and removes it. |
|
305 * |
|
306 * Wildcards may be provided to this function, but the wildcards are used to |
|
307 * exacly match wildcards in the routes (see AddMulticastRoute). That is, |
|
308 * calling RemoveMulticastRoute with the origin set to "0.0.0.0" will not |
|
309 * remove routes with any address in the origin, but will only remove routes |
|
310 * with "0.0.0.0" set as the the origin. |
|
311 * |
|
312 * \param origin The IP address specified as the origin of packets for the |
|
313 * route. |
|
314 * \param group The IP address specified as the multicast group addres of |
|
315 * the route. |
|
316 * \param inputInterface The network interface index specified as the expected |
|
317 * input interface for the route. |
|
318 * \returns true if a route was found and removed, false otherwise. |
|
319 * |
|
320 * \see Ipv4MulticastRoutingTableEntry |
|
321 * \see Ipv4StaticRouting::AddMulticastRoute |
|
322 */ |
|
323 bool RemoveMulticastRoute (Ipv4Address origin, |
|
324 Ipv4Address group, |
|
325 uint32_t inputInterface); |
|
326 |
|
327 /** |
|
328 * \brief Remove a route from the static multicast routing table. |
|
329 * |
|
330 * Externally, the multicast static routing table appears simply as a table |
|
331 * with n entries. |
|
332 * |
|
333 * \param index The index (into the multicast routing table) of the route to |
|
334 * remove. |
|
335 * |
|
336 * \see Ipv4RoutingTableEntry |
|
337 * \see Ipv4StaticRouting::GetRoute |
|
338 * \see Ipv4StaticRouting::AddRoute |
|
339 */ |
|
340 void RemoveMulticastRoute (uint32_t index); |
|
341 |
|
342 protected: |
|
343 virtual void DoDispose (void); |
|
344 |
|
345 private: |
|
346 typedef std::list<Ipv4RoutingTableEntry *> HostRoutes; |
|
347 typedef std::list<Ipv4RoutingTableEntry *>::const_iterator HostRoutesCI; |
|
348 typedef std::list<Ipv4RoutingTableEntry *>::iterator HostRoutesI; |
|
349 typedef std::list<Ipv4RoutingTableEntry *> NetworkRoutes; |
|
350 typedef std::list<Ipv4RoutingTableEntry *>::const_iterator NetworkRoutesCI; |
|
351 typedef std::list<Ipv4RoutingTableEntry *>::iterator NetworkRoutesI; |
|
352 |
|
353 typedef std::list<Ipv4MulticastRoutingTableEntry *> MulticastRoutes; |
|
354 typedef std::list<Ipv4MulticastRoutingTableEntry *>::const_iterator MulticastRoutesCI; |
|
355 typedef std::list<Ipv4MulticastRoutingTableEntry *>::iterator MulticastRoutesI; |
|
356 |
|
357 Ptr<Ipv4Route> LookupStatic (Ipv4Address dest); |
|
358 Ptr<Ipv4MulticastRoute> LookupStatic (Ipv4Address origin, Ipv4Address group, |
|
359 uint32_t interface); |
|
360 |
|
361 Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest); |
|
362 |
|
363 HostRoutes m_hostRoutes; |
|
364 NetworkRoutes m_networkRoutes; |
|
365 Ipv4RoutingTableEntry *m_defaultRoute; |
|
366 MulticastRoutes m_multicastRoutes; |
|
367 |
|
368 Ptr<Ipv4> m_ipv4; |
|
369 }; |
|
370 |
|
371 } // Namespace ns3 |
|
372 |
|
373 #endif /* IPV4_STATIC_ROUTING_IMPL_H */ |
|