A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-static-routing.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Georgia Tech Research Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: George F. Riley<riley@ece.gatech.edu>
18 * Gustavo Carneiro <gjc@inescporto.pt>
19 */
20
21#ifndef IPV4_STATIC_ROUTING_H
22#define IPV4_STATIC_ROUTING_H
23
24#include "ipv4-header.h"
26#include "ipv4.h"
27
28#include "ns3/ipv4-address.h"
29#include "ns3/ptr.h"
30#include "ns3/socket.h"
31
32#include <list>
33#include <stdint.h>
34#include <utility>
35
36namespace ns3
37{
38
39class Packet;
40class NetDevice;
41class Ipv4Interface;
42class Ipv4Address;
43class Ipv4Header;
44class Ipv4RoutingTableEntry;
45class Ipv4MulticastRoutingTableEntry;
46class Node;
47
48/**
49 * \ingroup ipv4Routing
50 *
51 * \brief Static routing protocol for IP version 4 stacks.
52 *
53 * This class provides a basic set of methods for inserting static
54 * unicast and multicast routes into the Ipv4 routing system.
55 * This particular protocol is designed to be inserted into an
56 * Ipv4ListRouting protocol but can be used also as a standalone
57 * protocol.
58 *
59 * The Ipv4StaticRouting class inherits from the abstract base class
60 * Ipv4RoutingProtocol that defines the interface methods that a routing
61 * protocol must support.
62 *
63 * \see Ipv4RoutingProtocol
64 * \see Ipv4ListRouting
65 * \see Ipv4ListRouting::AddRoutingProtocol
66 */
68{
69 public:
70 /**
71 * \brief The interface Id associated with this class.
72 * \return type identifier
73 */
74 static TypeId GetTypeId();
75
77 ~Ipv4StaticRouting() override;
78
80 const Ipv4Header& header,
82 Socket::SocketErrno& sockerr) override;
83
85 const Ipv4Header& header,
87 const UnicastForwardCallback& ucb,
88 const MulticastForwardCallback& mcb,
89 const LocalDeliverCallback& lcb,
90 const ErrorCallback& ecb) override;
91
92 void NotifyInterfaceUp(uint32_t interface) override;
93 void NotifyInterfaceDown(uint32_t interface) override;
94 void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override;
95 void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override;
96 void SetIpv4(Ptr<Ipv4> ipv4) override;
98 Time::Unit unit = Time::S) const override;
99
100 /**
101 * \brief Add a network route to the static routing table.
102 *
103 * \param network The Ipv4Address network for this route.
104 * \param networkMask The Ipv4Mask to extract the network.
105 * \param nextHop The next hop in the route to the destination network.
106 * \param interface The network interface index used to send packets to the
107 * destination.
108 * \param metric Metric of route in case of multiple routes to same destination
109 *
110 * \see Ipv4Address
111 */
112 void AddNetworkRouteTo(Ipv4Address network,
113 Ipv4Mask networkMask,
114 Ipv4Address nextHop,
115 uint32_t interface,
116 uint32_t metric = 0);
117
118 /**
119 * \brief Add a network route to the static routing table.
120 *
121 * \param network The Ipv4Address network for this route.
122 * \param networkMask The Ipv4Mask to extract the network.
123 * \param interface The network interface index used to send packets to the
124 * destination.
125 * \param metric Metric of route in case of multiple routes to same destination
126 *
127 * \see Ipv4Address
128 */
129 void AddNetworkRouteTo(Ipv4Address network,
130 Ipv4Mask networkMask,
131 uint32_t interface,
132 uint32_t metric = 0);
133
134 /**
135 * \brief Add a host route to the static routing table.
136 *
137 * \param dest The Ipv4Address destination for this route.
138 * \param nextHop The Ipv4Address of the next hop in the route.
139 * \param interface The network interface index used to send packets to the
140 * destination.
141 * \param metric Metric of route in case of multiple routes to same destination
142 *
143 * \see Ipv4Address
144 */
145 void AddHostRouteTo(Ipv4Address dest,
146 Ipv4Address nextHop,
147 uint32_t interface,
148 uint32_t metric = 0);
149 /**
150 * \brief Add a host route to the static routing table.
151 *
152 * \param dest The Ipv4Address destination for this route.
153 * \param interface The network interface index used to send packets to the
154 * destination.
155 * \param metric Metric of route in case of multiple routes to same destination
156 *
157 * \see Ipv4Address
158 */
159 void AddHostRouteTo(Ipv4Address dest, uint32_t interface, uint32_t metric = 0);
160 /**
161 * \brief Add a default route to the static routing table.
162 *
163 * This method tells the routing system what to do in the case where a specific
164 * route to a destination is not found. The system forwards packets to the
165 * specified node in the hope that it knows better how to route the packet.
166 *
167 * If the default route is set, it is returned as the selected route from
168 * LookupStatic irrespective of destination address if no specific route is
169 * found.
170 *
171 * \param nextHop The Ipv4Address to send packets to in the hope that they
172 * will be forwarded correctly.
173 * \param interface The network interface index used to send packets.
174 * \param metric Metric of route in case of multiple routes to same destination
175 *
176 * \see Ipv4Address
177 * \see Ipv4StaticRouting::Lookup
178 */
179 void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric = 0);
180
181 /**
182 * \brief Get the number of individual unicast routes that have been added
183 * to the routing table.
184 *
185 * \warning The default route counts as one of the routes.
186 * \return number of entries
187 */
188 uint32_t GetNRoutes() const;
189
190 /**
191 * \brief Get the default route with lowest metric from the static routing table.
192 *
193 * \return If the default route is set, a pointer to that Ipv4RoutingTableEntry is
194 * returned, otherwise an empty routing table entry is returned.
195 * If multiple default routes exist, the one with lowest metric is returned.
196 *
197 * \see Ipv4RoutingTableEntry
198 */
200
201 /**
202 * \brief Get a route from the static unicast routing table.
203 *
204 * Externally, the unicast static routing table appears simply as a table with
205 * n entries.
206 *
207 * \param i The index (into the routing table) of the route to retrieve.
208 * \return If route is set, a pointer to that Ipv4RoutingTableEntry is returned, otherwise
209 * a zero pointer is returned.
210 *
211 * \see Ipv4RoutingTableEntry
212 * \see Ipv4StaticRouting::RemoveRoute
213 */
215
216 /**
217 * \brief Get a metric for route from the static unicast routing table.
218 *
219 * \param index The index (into the routing table) of the route to retrieve.
220 * \return If route is set, the metric is returned. If not, an infinity metric (0xffffffff) is
221 * returned
222 *
223 */
224 uint32_t GetMetric(uint32_t index) const;
225
226 /**
227 * \brief Remove a route from the static unicast routing table.
228 *
229 * Externally, the unicast static routing table appears simply as a table with
230 * n entries.
231 *
232 * \param i The index (into the routing table) of the route to remove.
233 *
234 * \see Ipv4RoutingTableEntry
235 * \see Ipv4StaticRouting::GetRoute
236 * \see Ipv4StaticRouting::AddRoute
237 */
238 void RemoveRoute(uint32_t i);
239
240 /**
241 * \brief Add a multicast route to the static routing table.
242 *
243 * A multicast route must specify an origin IP address, a multicast group and
244 * an input network interface index as conditions and provide a vector of
245 * output network interface indices over which packets matching the conditions
246 * are sent.
247 *
248 * Typically there are two main types of multicast routes: routes used during
249 * forwarding, and routes used in the originator node.
250 * For forwarding, all of the conditions must be explicitly provided.
251 * For originator nodes, the route is equivalent to a unicast route, and
252 * must be added through `AddHostRouteTo`.
253 *
254 * \param origin The Ipv4Address of the origin of packets for this route. May
255 * be Ipv4Address:GetAny for open groups.
256 * \param group The Ipv4Address of the multicast group or this route.
257 * \param inputInterface The input network interface index over which to
258 * expect packets destined for this route.
259 * \param outputInterfaces A vector of network interface indexes used to specify
260 * how to send packets to the destination(s).
261 *
262 * \see Ipv4Address
263 */
264 void AddMulticastRoute(Ipv4Address origin,
265 Ipv4Address group,
266 uint32_t inputInterface,
267 std::vector<uint32_t> outputInterfaces);
268
269 /**
270 * \brief Add a default multicast route to the static routing table.
271 *
272 * This is the multicast equivalent of the unicast version SetDefaultRoute.
273 * We tell the routing system what to do in the case where a specific route
274 * to a destination multicast group is not found. The system forwards
275 * packets out the specified interface in the hope that "something out there"
276 * knows better how to route the packet. This method is only used in
277 * initially sending packets off of a host. The default multicast route is
278 * not consulted during forwarding -- exact routes must be specified using
279 * AddMulticastRoute for that case.
280 *
281 * Since we're basically sending packets to some entity we think may know
282 * better what to do, we don't pay attention to "subtleties" like origin
283 * address, nor do we worry about forwarding out multiple interfaces. If the
284 * default multicast route is set, it is returned as the selected route from
285 * LookupStatic irrespective of origin or multicast group if another specific
286 * route is not found.
287 *
288 * \param outputInterface The network interface index used to specify where
289 * to send packets in the case of unknown routes.
290 *
291 * \see Ipv4Address
292 */
293 void SetDefaultMulticastRoute(uint32_t outputInterface);
294
295 /**
296 * \brief Get the number of individual multicast routes that have been added
297 * to the routing table.
298 *
299 * \warning The default multicast route counts as one of the routes.
300 * \return number of entries
301 */
303
304 /**
305 * \brief Get a route from the static multicast routing table.
306 *
307 * Externally, the multicast static routing table appears simply as a table
308 * with n entries.
309 *
310 * \param i The index (into the routing table) of the multicast route to
311 * retrieve.
312 * \return If route \e i is set, a pointer to that Ipv4MulticastRoutingTableEntry is
313 * returned, otherwise a zero pointer is returned.
314 *
315 * \see Ipv4MulticastRoutingTableEntry
316 * \see Ipv4StaticRouting::RemoveRoute
317 */
319
320 /**
321 * \brief Remove a route from the static multicast routing table.
322 *
323 * Externally, the multicast static routing table appears simply as a table
324 * with n entries.
325 * This method causes the multicast routing table to be searched for the first
326 * route that matches the parameters and removes it.
327 *
328 * Wildcards may be provided to this function, but the wildcards are used to
329 * exactly match wildcards in the routes (see AddMulticastRoute). That is,
330 * calling RemoveMulticastRoute with the origin set to "0.0.0.0" will not
331 * remove routes with any address in the origin, but will only remove routes
332 * with "0.0.0.0" set as the the origin.
333 *
334 * \param origin The IP address specified as the origin of packets for the
335 * route.
336 * \param group The IP address specified as the multicast group address of
337 * the route.
338 * \param inputInterface The network interface index specified as the expected
339 * input interface for the route.
340 * \returns true if a route was found and removed, false otherwise.
341 *
342 * \see Ipv4MulticastRoutingTableEntry
343 * \see Ipv4StaticRouting::AddMulticastRoute
344 */
345 bool RemoveMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface);
346
347 /**
348 * \brief Remove a route from the static multicast routing table.
349 *
350 * Externally, the multicast static routing table appears simply as a table
351 * with n entries.
352 *
353 * \param index The index (into the multicast routing table) of the route to
354 * remove.
355 *
356 * \see Ipv4RoutingTableEntry
357 * \see Ipv4StaticRouting::GetRoute
358 * \see Ipv4StaticRouting::AddRoute
359 */
360 void RemoveMulticastRoute(uint32_t index);
361
362 protected:
363 void DoDispose() override;
364
365 private:
366 /// Container for the network routes
367 typedef std::list<std::pair<Ipv4RoutingTableEntry*, uint32_t>> NetworkRoutes;
368
369 /// Const Iterator for container for the network routes
370 typedef std::list<std::pair<Ipv4RoutingTableEntry*, uint32_t>>::const_iterator NetworkRoutesCI;
371
372 /// Iterator for container for the network routes
373 typedef std::list<std::pair<Ipv4RoutingTableEntry*, uint32_t>>::iterator NetworkRoutesI;
374
375 /// Container for the multicast routes
376 typedef std::list<Ipv4MulticastRoutingTableEntry*> MulticastRoutes;
377
378 /// Const Iterator for container for the multicast routes
379 typedef std::list<Ipv4MulticastRoutingTableEntry*>::const_iterator MulticastRoutesCI;
380
381 /// Iterator for container for the multicast routes
382 typedef std::list<Ipv4MulticastRoutingTableEntry*>::iterator MulticastRoutesI;
383
384 /**
385 * \brief Checks if a route is already present in the forwarding table.
386 * \param route route
387 * \param metric metric of route
388 * \return true if the route/metric is already in the forwarding table
389 */
390 bool LookupRoute(const Ipv4RoutingTableEntry& route, uint32_t metric);
391
392 /**
393 * \brief Lookup in the forwarding table for destination.
394 * \param dest destination address
395 * \param oif output interface if any (put 0 otherwise)
396 * \return Ipv4Route to route the packet to reach dest address
397 */
399
400 /**
401 * \brief Lookup in the multicast forwarding table for destination.
402 * \param origin source address
403 * \param group group multicast address
404 * \param interface interface index
405 * \return Ipv4MulticastRoute to route the packet to reach dest address
406 */
408
409 /**
410 * \brief the forwarding table for network.
411 */
413
414 /**
415 * \brief the forwarding table for multicast.
416 */
418
419 /**
420 * \brief Ipv4 reference.
421 */
423};
424
425} // Namespace ns3
426
427#endif /* IPV4_STATIC_ROUTING_H */
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
A record of an IPv4 multicast route for Ipv4GlobalRouting and Ipv4StaticRouting.
Abstract base class for IPv4 routing protocols.
A record of an IPv4 routing table entry for Ipv4GlobalRouting and Ipv4StaticRouting.
Static routing protocol for IP version 4 stacks.
void SetIpv4(Ptr< Ipv4 > ipv4) override
void NotifyInterfaceUp(uint32_t interface) override
void DoDispose() override
Destructor implementation.
Ipv4RoutingTableEntry GetDefaultRoute()
Get the default route with lowest metric from the static routing table.
void RemoveRoute(uint32_t i)
Remove a route from the static unicast routing table.
bool LookupRoute(const Ipv4RoutingTableEntry &route, uint32_t metric)
Checks if a route is already present in the forwarding table.
Ptr< Ipv4Route > RouteOutput(Ptr< Packet > p, const Ipv4Header &header, Ptr< NetDevice > oif, Socket::SocketErrno &sockerr) override
Query routing cache for an existing route, for an outbound packet.
void SetDefaultMulticastRoute(uint32_t outputInterface)
Add a default multicast route to the static routing table.
void AddMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface, std::vector< uint32_t > outputInterfaces)
Add a multicast route to the static routing table.
MulticastRoutes m_multicastRoutes
the forwarding table for multicast.
std::list< Ipv4MulticastRoutingTableEntry * >::const_iterator MulticastRoutesCI
Const Iterator for container for the multicast routes.
void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) override
uint32_t GetMetric(uint32_t index) const
Get a metric for route from the static unicast routing table.
std::list< std::pair< Ipv4RoutingTableEntry *, uint32_t > >::const_iterator NetworkRoutesCI
Const Iterator for container for the network routes.
NetworkRoutes m_networkRoutes
the forwarding table for network.
Ptr< Ipv4Route > LookupStatic(Ipv4Address dest, Ptr< NetDevice > oif=nullptr)
Lookup in the forwarding table for destination.
void PrintRoutingTable(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const override
Print the Routing Table entries.
void AddNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a network route to the static routing table.
uint32_t GetNMulticastRoutes() const
Get the number of individual multicast routes that have been added to the routing table.
Ipv4RoutingTableEntry GetRoute(uint32_t i) const
Get a route from the static unicast routing table.
std::list< std::pair< Ipv4RoutingTableEntry *, uint32_t > > NetworkRoutes
Container for the network routes.
void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) override
void NotifyInterfaceDown(uint32_t interface) override
bool RouteInput(Ptr< const Packet > p, const Ipv4Header &header, Ptr< const NetDevice > idev, const UnicastForwardCallback &ucb, const MulticastForwardCallback &mcb, const LocalDeliverCallback &lcb, const ErrorCallback &ecb) override
Route an input packet (to be forwarded or locally delivered)
static TypeId GetTypeId()
The interface Id associated with this class.
std::list< Ipv4MulticastRoutingTableEntry * > MulticastRoutes
Container for the multicast routes.
Ptr< Ipv4 > m_ipv4
Ipv4 reference.
Ipv4MulticastRoutingTableEntry GetMulticastRoute(uint32_t i) const
Get a route from the static multicast routing table.
void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a host route to the static routing table.
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
std::list< Ipv4MulticastRoutingTableEntry * >::iterator MulticastRoutesI
Iterator for container for the multicast routes.
std::list< std::pair< Ipv4RoutingTableEntry *, uint32_t > >::iterator NetworkRoutesI
Iterator for container for the network routes.
uint32_t GetNRoutes() const
Get the number of individual unicast routes that have been added to the routing table.
bool RemoveMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface)
Remove a route from the static multicast routing table.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:84
Unit
The unit to use to interpret a number representing time.
Definition: nstime.h:111
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.