src/node/ipv4.h
author Tom Henderson <tomh@tomh.org>
Tue Aug 11 21:29:18 2009 -0700 (2009-08-11)
changeset 4701 b57d5b1fe68e
parent 4607 0e15594f67f3
child 5760 4f08a0837018
permissions -rw-r--r--
fix Ipv4 doxygen error
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2007 INRIA
     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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 #ifndef IPV4_H
    21 #define IPV4_H
    22 
    23 #include <stdint.h>
    24 #include "ns3/object.h"
    25 #include "ns3/socket.h"
    26 #include "ns3/callback.h"
    27 #include "ipv4-address.h"
    28 #include "ipv4-interface-address.h"
    29 
    30 namespace ns3 {
    31 
    32 class Node;
    33 class NetDevice;
    34 class Packet;
    35 class Ipv4RoutingProtocol;
    36 
    37 /**
    38  * \ingroup node
    39  * \defgroup ipv4 Ipv4
    40  */
    41 /**
    42  * \ingroup ipv4
    43  * \brief Access to the Ipv4 forwarding table, interfaces, and configuration
    44  *
    45  * This class defines the API to manipulate the following aspects of
    46  * the Ipv4 implementation:  
    47  * -# set/get an Ipv4RoutingProtocol 
    48  * -# register a NetDevice for use by the Ipv4 layer (basically, to
    49  * create Ipv4-related state such as addressing and neighbor cache that 
    50  * is associated with a NetDevice)
    51  * -# manipulate the status of the NetDevice from the Ipv4 perspective, 
    52  * such as marking it as Up or Down, 
    53  * -# adding, deleting, and getting addresses associated to the Ipv4 
    54  * interfaces.
    55  * -# exporting Ipv4 configuration attributes
    56  * 
    57  * Each NetDevice has conceptually a single Ipv4 interface associated
    58  * with it (the corresponding structure in the Linux Ipv4 implementation
    59  * is struct in_device).  Each interface may have one or more Ipv4
    60  * addresses associated with it.  Each Ipv4 address may have different
    61  * subnet mask, scope, etc., so all of this per-address information 
    62  * is stored in an Ipv4InterfaceAddress class (the corresponding 
    63  * structure in Linux is struct in_ifaddr)
    64  *
    65  * Ipv4 attributes such as whether IP forwarding is enabled and disabled
    66  * are also stored in this class
    67  *
    68  * TO DO:  Add API to allow access to the Ipv4 neighbor table
    69  *
    70  * \see Ipv4RoutingProtocol
    71  * \see Ipv4InterfaceAddress
    72  */
    73 class Ipv4 : public Object
    74 {
    75 public:
    76   static TypeId GetTypeId (void);
    77   Ipv4 ();
    78   virtual ~Ipv4 ();
    79 
    80   /**
    81    * \brief Register a new routing protocol to be used by this Ipv4 stack
    82    *   
    83    * This call will replace any routing protocol that has been previously 
    84    * registered.  If you want to add multiple routing protocols, you must
    85    * add them to a Ipv4ListRoutingProtocol directly.
    86    * 
    87    * \param routingProtocol smart pointer to Ipv4RoutingProtocol object
    88    */
    89   virtual void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol) = 0;
    90 
    91   /**
    92    * \brief Get the routing protocol to be used by this Ipv4 stack
    93    * 
    94    * \returns smart pointer to Ipv4RoutingProtocol object, or null pointer if none
    95    */
    96   virtual Ptr<Ipv4RoutingProtocol> GetRoutingProtocol (void) const = 0;
    97 
    98   /**
    99    * \param device device to add to the list of Ipv4 interfaces
   100    *        which can be used as output interfaces during packet forwarding.
   101    * \returns the index of the Ipv4 interface added.
   102    *
   103    * Once a device has been added, it can never be removed: if you want
   104    * to disable it, you can invoke Ipv4::SetDown which will
   105    * make sure that it is never used during packet forwarding.
   106    */
   107   virtual uint32_t AddInterface (Ptr<NetDevice> device) = 0;
   108 
   109   /**
   110    * \returns the number of interfaces added by the user.
   111    */
   112   virtual uint32_t GetNInterfaces (void) const = 0;  
   113 
   114   /**
   115    * \brief Return the interface number of the interface that has been
   116    *        assigned the specified IP address.
   117    *
   118    * \param address The IP address being searched for
   119    * \returns The interface number of the Ipv4 interface with the given 
   120    *          address or -1 if not found.
   121    *
   122    * Each IP interface has one or more IP addresses associated with it.  
   123    * This method searches the list of interfaces for one that holds a
   124    * particular address.  This call takes an IP address as a parameter and
   125    * returns the interface number of the first interface that has been assigned
   126    * that address, or -1 if not found.  There must be an exact match.
   127    */
   128   virtual int32_t GetInterfaceForAddress (Ipv4Address address) const = 0;
   129 
   130   /**
   131    * \brief Return the interface number of first interface found that 
   132    *  has an Ipv4 address within the prefix specified by the input
   133    *  address and mask parameters
   134    *
   135    * \param address The IP address assigned to the interface of interest.
   136    * \param mask The IP prefix to use in the mask
   137    * \returns The interface number of the Ipv4 interface with the given 
   138    *          address or -1 if not found.
   139    *
   140    * Each IP interface has one or more IP addresses associated with it.  
   141    * This method searches the list of interfaces for the first one found
   142    * that holds an address that is included within the prefix 
   143    * formed by the input address and mask parameters.  The value -1 is
   144    * returned if no match is found.
   145    */
   146   virtual int32_t GetInterfaceForPrefix (Ipv4Address address,
   147     Ipv4Mask mask) const = 0;
   148 
   149   /**
   150    * \param interface The interface number of an Ipv4 interface.
   151    * \returns The NetDevice associated with the Ipv4 interface number.
   152    */
   153   virtual Ptr<NetDevice> GetNetDevice (uint32_t interface) = 0;
   154 
   155   /**
   156    * \param device The NetDevice for an Ipv4Interface
   157    * \returns The interface number of an Ipv4 interface or -1 if not found.
   158    */
   159   virtual int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const = 0;
   160 
   161   /**
   162    * \param interface Interface number of an Ipv4 interface
   163    * \param address Ipv4InterfaceAddress address to associate with the underlying Ipv4 interface
   164    * \returns true if the operation succeeded
   165    */
   166   virtual bool AddAddress (uint32_t interface, Ipv4InterfaceAddress address) = 0;
   167 
   168   /**
   169    * \param interface Interface number of an Ipv4 interface
   170    * \returns the number of Ipv4InterfaceAddress entries for the interface.
   171    */
   172   virtual uint32_t GetNAddresses (uint32_t interface) const = 0;
   173 
   174   /**
   175    * Because addresses can be removed, the addressIndex is not guaranteed
   176    * to be static across calls to this method.
   177    * 
   178    * \param interface Interface number of an Ipv4 interface
   179    * \param addressIndex index of Ipv4InterfaceAddress 
   180    * \returns the Ipv4InterfaceAddress associated to the interface and addresIndex
   181    */
   182   virtual Ipv4InterfaceAddress GetAddress (uint32_t interface, uint32_t addressIndex) const = 0;
   183 
   184   /**
   185    * Remove the address at addressIndex on named interface.  The addressIndex
   186    * for all higher indices will decrement by one after this method is called;
   187    * so, for example, to remove 5 addresses from an interface i, one could
   188    * call RemoveAddress (i, 0); 5 times.  
   189    * 
   190    * \param interface Interface number of an Ipv4 interface
   191    * \param addressIndex index of Ipv4InterfaceAddress to remove 
   192    * \returns true if the operation succeeded
   193    */
   194   virtual bool RemoveAddress (uint32_t interface, uint32_t addressIndex) = 0;
   195 
   196   /**
   197    * \param interface The interface number of an Ipv4 interface
   198    * \param metric routing metric (cost) associated to the underlying 
   199    *          Ipv4 interface
   200    */
   201   virtual void SetMetric (uint32_t interface, uint16_t metric) = 0;
   202 
   203   /**
   204    * \param interface The interface number of an Ipv4 interface
   205    * \returns routing metric (cost) associated to the underlying 
   206    *          Ipv4 interface
   207    */
   208   virtual uint16_t GetMetric (uint32_t interface) const = 0;
   209 
   210   /**
   211    * \param interface Interface number of Ipv4 interface
   212    * \returns the Maximum Transmission Unit (in bytes) associated
   213    *          to the underlying Ipv4 interface
   214    */
   215   virtual uint16_t GetMtu (uint32_t interface) const = 0;
   216 
   217   /**
   218    * \param interface Interface number of Ipv4 interface
   219    * \returns true if the underlying interface is in the "up" state,
   220    *          false otherwise.
   221    */
   222   virtual bool IsUp (uint32_t interface) const = 0;
   223 
   224   /**
   225    * \param interface Interface number of Ipv4 interface
   226    * 
   227    * Set the interface into the "up" state. In this state, it is
   228    * considered valid during Ipv4 forwarding.
   229    */
   230   virtual void SetUp (uint32_t interface) = 0;
   231 
   232   /**
   233    * \param interface Interface number of Ipv4 interface
   234    *
   235    * Set the interface into the "down" state. In this state, it is
   236    * ignored during Ipv4 forwarding.
   237    */
   238   virtual void SetDown (uint32_t interface) = 0;
   239 
   240   /**
   241    * \param interface Interface number of Ipv4 interface
   242    * \returns true if IP forwarding enabled for input datagrams on this device
   243    */
   244   virtual bool IsForwarding (uint32_t interface) const = 0;
   245 
   246   /**
   247    * \param interface Interface number of Ipv4 interface
   248    * \param val Value to set the forwarding flag
   249    * 
   250    * If set to true, IP forwarding is enabled for input datagrams on this device
   251    */
   252   virtual void SetForwarding (uint32_t interface, bool val) = 0;
   253 
   254   static const uint32_t IF_ANY = 0xffffffff;
   255 
   256 private:
   257   // Indirect the Ipv4 attributes through private pure virtual methods
   258   virtual void SetIpForward (bool forward) = 0;
   259   virtual bool GetIpForward (void) const = 0;
   260 };
   261 
   262 } // namespace ns3 
   263 
   264 #endif /* IPV4_H */