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