src/internet-stack/ipv4-static-routing-impl.h
changeset 4472 e20a31541404
child 4560 2f106fd728ab
equal deleted inserted replaced
4471:ef1730710767 4472:e20a31541404
       
     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 /**
       
    60  * \brief Add a host route to the static routing table.
       
    61  *
       
    62  * \param dest The Ipv4Address destination for this route.
       
    63  * \param nextHop The Ipv4Address of the next hop in the route.
       
    64  * \param interface The network interface index used to send packets to the
       
    65  * destination.
       
    66  *
       
    67  * \see Ipv4Address
       
    68  */
       
    69   void AddHostRouteTo (Ipv4Address dest, 
       
    70                        Ipv4Address nextHop, 
       
    71                        uint32_t interface);
       
    72 /**
       
    73  * \brief Add a host route to the static routing table.
       
    74  *
       
    75  * \param dest The Ipv4Address destination for this route.
       
    76  * \param interface The network interface index used to send packets to the
       
    77  * destination.
       
    78  *
       
    79  * \see Ipv4Address
       
    80  */
       
    81   void AddHostRouteTo (Ipv4Address dest, 
       
    82                        uint32_t interface);
       
    83 
       
    84 /**
       
    85  * \brief Add a network route to the static routing table.
       
    86  *
       
    87  * \param network The Ipv4Address network for this route.
       
    88  * \param networkMask The Ipv4Mask to extract the network.
       
    89  * \param nextHop The next hop in the route to the destination network.
       
    90  * \param interface The network interface index used to send packets to the
       
    91  * destination.
       
    92  *
       
    93  * \see Ipv4Address
       
    94  */
       
    95   void AddNetworkRouteTo (Ipv4Address network, 
       
    96                           Ipv4Mask networkMask, 
       
    97                           Ipv4Address nextHop, 
       
    98                           uint32_t interface);
       
    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 interface The network interface index used to send packets to the
       
   106  * destination.
       
   107  *
       
   108  * \see Ipv4Address
       
   109  */
       
   110   void AddNetworkRouteTo (Ipv4Address network, 
       
   111                           Ipv4Mask networkMask, 
       
   112                           uint32_t interface);
       
   113 
       
   114 /**
       
   115  * \brief Add a default route to the static routing table.
       
   116  *
       
   117  * This method tells the routing system what to do in the case where a specific
       
   118  * route to a destination is not found.  The system forwards packets to the
       
   119  * specified node in the hope that it knows better how to route the packet.
       
   120  * 
       
   121  * If the default route is set, it is returned as the selected route from 
       
   122  * LookupStatic irrespective of destination address if no specific route is
       
   123  * found.
       
   124  *
       
   125  * \param nextHop The Ipv4Address to send packets to in the hope that they
       
   126  * will be forwarded correctly.
       
   127  * \param interface The network interface index used to send packets.
       
   128  *
       
   129  * \see Ipv4Address
       
   130  * \see Ipv4StaticRouting::Lookup
       
   131  */
       
   132   void SetDefaultRoute (Ipv4Address nextHop, 
       
   133                         uint32_t interface);
       
   134 
       
   135 /**
       
   136  * \brief Get the number of individual unicast routes that have been added
       
   137  * to the routing table.
       
   138  *
       
   139  * \warning The default route counts as one of the routes.
       
   140  */
       
   141   uint32_t GetNRoutes (void);
       
   142 
       
   143 /**
       
   144  * \brief Get the default route from the static routing table.
       
   145  *
       
   146  * \return If the default route is set, a pointer to that Ipv4RoutingTableEntry is
       
   147  * returned, otherwise a zero pointer is returned.
       
   148  *
       
   149  * \see Ipv4RoutingTableEntry
       
   150  */
       
   151   Ipv4RoutingTableEntry GetDefaultRoute (void);
       
   152 
       
   153 /**
       
   154  * \brief Get a route from the static unicast routing table.
       
   155  *
       
   156  * Externally, the unicast static routing table appears simply as a table with
       
   157  * n entries.  The one sublety of note is that if a default route has been set
       
   158  * it will appear as the zeroth entry in the table.  This means that if you
       
   159  * add only a default route, the table will have one entry that can be accessed
       
   160  * either by explicity calling GetDefaultRoute () or by calling GetRoute (0).
       
   161  * 
       
   162  * Similarly, if the default route has been set, calling RemoveRoute (0) will
       
   163  * remove the default route.
       
   164  *
       
   165  * \param i The index (into the routing table) of the route to retrieve.  If
       
   166  * the default route has been set, it will occupy index zero.
       
   167  * \return If route is set, a pointer to that Ipv4RoutingTableEntry is returned, otherwise
       
   168  * a zero pointer is returned.
       
   169  *
       
   170  * \see Ipv4RoutingTableEntry
       
   171  * \see Ipv4StaticRouting::RemoveRoute
       
   172  */
       
   173   Ipv4RoutingTableEntry GetRoute (uint32_t i);
       
   174 
       
   175 /**
       
   176  * \brief Remove a route from the static unicast routing table.
       
   177  *
       
   178  * Externally, the unicast static routing table appears simply as a table with
       
   179  * n entries.  The one sublety of note is that if a default route has been set
       
   180  * it will appear as the zeroth entry in the table.  This means that if the
       
   181  * default route has been set, calling RemoveRoute (0) will remove the
       
   182  * default route.
       
   183  *
       
   184  * \param i The index (into the routing table) of the route to remove.  If
       
   185  * the default route has been set, it will occupy index zero.
       
   186  *
       
   187  * \see Ipv4RoutingTableEntry
       
   188  * \see Ipv4StaticRouting::GetRoute
       
   189  * \see Ipv4StaticRouting::AddRoute
       
   190  */
       
   191   void RemoveRoute (uint32_t i);
       
   192 
       
   193 /**
       
   194  * \brief Add a multicast route to the static routing table.
       
   195  *
       
   196  * A multicast route must specify an origin IP address, a multicast group and
       
   197  * an input network interface index as conditions and provide a vector of
       
   198  * output network interface indices over which packets matching the conditions
       
   199  * are sent.
       
   200  *
       
   201  * Typically there are two main types of multicast routes:  routes of the 
       
   202  * first kind are used during forwarding.  All of the conditions must be
       
   203  * exlicitly provided.  The second kind of routes are used to get packets off
       
   204  * of a local node.  The difference is in the input interface.  Routes for
       
   205  * forwarding will always have an explicit input interface specified.  Routes
       
   206  * off of a node will always set the input interface to a wildcard specified
       
   207  * by the index Ipv4RoutingProtocol::INTERFACE_ANY.
       
   208  *
       
   209  * For routes off of a local node wildcards may be used in the origin and
       
   210  * multicast group addresses.  The wildcard used for Ipv4Adresses is that 
       
   211  * address returned by Ipv4Address::GetAny () -- typically "0.0.0.0".  Usage
       
   212  * of a wildcard allows one to specify default behavior to varying degrees.
       
   213  *
       
   214  * For example, making the origin address a wildcard, but leaving the 
       
   215  * multicast group specific allows one (in the case of a node with multiple
       
   216  * interfaces) to create different routes using different output interfaces
       
   217  * for each multicast group.
       
   218  *
       
   219  * If the origin and multicast addresses are made wildcards, you have created
       
   220  * essentially a default multicast address that can forward to multiple 
       
   221  * interfaces.  Compare this to the actual default multicast address that is
       
   222  * limited to specifying a single output interface for compatibility with
       
   223  * existing functionality in other systems.
       
   224  * 
       
   225  * \param origin The Ipv4Address of the origin of packets for this route.  May
       
   226  * be Ipv4Address:GetAny for open groups.
       
   227  * \param group The Ipv4Address of the multicast group or this route.
       
   228  * \param inputInterface The input network interface index over which to 
       
   229  * expect packets destined for this route.  May be
       
   230  * Ipv4RoutingProtocol::INTERFACE_ANY for packets of local origin.
       
   231  * \param outputInterfaces A vector of network interface indices used to specify
       
   232  * how to send packets to the destination(s).
       
   233  *
       
   234  * \see Ipv4Address
       
   235  */
       
   236   void AddMulticastRoute (Ipv4Address origin,
       
   237                           Ipv4Address group,
       
   238                           uint32_t inputInterface,
       
   239                           std::vector<uint32_t> outputInterfaces);
       
   240 
       
   241 /**
       
   242  * \brief Add a default multicast route to the static routing table.
       
   243  *
       
   244  * This is the multicast equivalent of the unicast version SetDefaultRoute.
       
   245  * We tell the routing system what to do in the case where a specific route
       
   246  * to a destination multicast group is not found.  The system forwards 
       
   247  * packets out the specified interface in the hope that "something out there"
       
   248  * knows better how to route the packet.  This method is only used in 
       
   249  * initially sending packets off of a host.  The default multicast route is
       
   250  * not consulted during forwarding -- exact routes must be specified using
       
   251  * AddMulticastRoute for that case.
       
   252  *
       
   253  * Since we're basically sending packets to some entity we think may know
       
   254  * better what to do, we don't pay attention to "subtleties" like origin
       
   255  * address, nor do we worry about forwarding out multiple  interfaces.  If the
       
   256  * default multicast route is set, it is returned as the selected route from 
       
   257  * LookupStatic irrespective of origin or multicast group if another specific
       
   258  * route is not found.
       
   259  *
       
   260  * \param outputInterface The network interface index used to specify where
       
   261  * to send packets in the case of unknown routes.
       
   262  *
       
   263  * \see Ipv4Address
       
   264  */
       
   265   void SetDefaultMulticastRoute (uint32_t outputInterface);
       
   266 
       
   267 /**
       
   268  * \brief Get the number of individual multicast routes that have been added
       
   269  * to the routing table.
       
   270  *
       
   271  * \warning The default multicast route counts as one of the routes.
       
   272  */
       
   273   uint32_t GetNMulticastRoutes (void) const;
       
   274 
       
   275 /**
       
   276  * \brief Get a route from the static multicast routing table.
       
   277  *
       
   278  * Externally, the multicast static routing table appears simply as a table 
       
   279  * with n entries.  
       
   280  * 
       
   281  * \param i The index (into the routing table) of the multicast route to
       
   282  * retrieve.  
       
   283  * \return If route \e i is set, a pointer to that Ipv4MulticastRoutingTableEntry is
       
   284  * returned, otherwise a zero pointer is returned.
       
   285  *
       
   286  * \see Ipv4MulticastRoutingTableEntry
       
   287  * \see Ipv4StaticRouting::RemoveRoute
       
   288  */
       
   289   Ipv4MulticastRoutingTableEntry GetMulticastRoute (uint32_t i) const;
       
   290 
       
   291 /**
       
   292  * \brief Remove a route from the static multicast routing table.
       
   293  *
       
   294  * Externally, the multicast static routing table appears simply as a table 
       
   295  * with n entries.  
       
   296  * This method causes the multicast routing table to be searched for the first
       
   297  * route that matches the parameters and removes it.
       
   298  *
       
   299  * Wildcards may be provided to this function, but the wildcards are used to
       
   300  * exacly match wildcards in the routes (see AddMulticastRoute).  That is,
       
   301  * calling RemoveMulticastRoute with the origin set to "0.0.0.0" will not
       
   302  * remove routes with any address in the origin, but will only remove routes
       
   303  * with "0.0.0.0" set as the the origin.
       
   304  *
       
   305  * \param origin The IP address specified as the origin of packets for the
       
   306  * route.
       
   307  * \param group The IP address specified as the multicast group addres of
       
   308  * the route.
       
   309  * \param inputInterface The network interface index specified as the expected
       
   310  * input interface for the route.
       
   311  * \returns true if a route was found and removed, false otherwise.
       
   312  *
       
   313  * \see Ipv4MulticastRoutingTableEntry
       
   314  * \see Ipv4StaticRouting::AddMulticastRoute
       
   315  */
       
   316   bool RemoveMulticastRoute (Ipv4Address origin,
       
   317                              Ipv4Address group,
       
   318                              uint32_t inputInterface);
       
   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  *
       
   326  * \param index The index (into the multicast routing table) of the route to
       
   327  * remove.  
       
   328  *
       
   329  * \see Ipv4RoutingTableEntry
       
   330  * \see Ipv4StaticRouting::GetRoute
       
   331  * \see Ipv4StaticRouting::AddRoute
       
   332  */
       
   333   void RemoveMulticastRoute (uint32_t index);
       
   334 
       
   335   void SetNode (Ptr<Node> node);
       
   336   Ptr<Node> GetNode (void) const;
       
   337 
       
   338 protected:
       
   339   void DoDispose (void);
       
   340 
       
   341 private:
       
   342   typedef std::list<Ipv4RoutingTableEntry *> HostRoutes;
       
   343   typedef std::list<Ipv4RoutingTableEntry *>::const_iterator HostRoutesCI;
       
   344   typedef std::list<Ipv4RoutingTableEntry *>::iterator HostRoutesI;
       
   345   typedef std::list<Ipv4RoutingTableEntry *> NetworkRoutes;
       
   346   typedef std::list<Ipv4RoutingTableEntry *>::const_iterator NetworkRoutesCI;
       
   347   typedef std::list<Ipv4RoutingTableEntry *>::iterator NetworkRoutesI;
       
   348 
       
   349   typedef std::list<Ipv4MulticastRoutingTableEntry *> MulticastRoutes;
       
   350   typedef std::list<Ipv4MulticastRoutingTableEntry *>::const_iterator MulticastRoutesCI;
       
   351   typedef std::list<Ipv4MulticastRoutingTableEntry *>::iterator MulticastRoutesI;
       
   352   
       
   353   Ptr<Ipv4Route> LookupStatic (Ipv4Address dest);
       
   354   Ptr<Ipv4MulticastRoute> LookupStatic (Ipv4Address origin, Ipv4Address group,
       
   355                                     uint32_t interface);
       
   356 
       
   357   HostRoutes m_hostRoutes;
       
   358   NetworkRoutes m_networkRoutes;
       
   359   Ipv4RoutingTableEntry *m_defaultRoute;
       
   360   MulticastRoutes m_multicastRoutes;
       
   361 
       
   362   Ptr<Node> m_node;
       
   363 };
       
   364 
       
   365 } // Namespace ns3
       
   366 
       
   367 #endif /* IPV4_STATIC_ROUTING_IMPL_H */