1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2007 INRIA
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;
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.
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
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
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"
35 class Ipv4RoutingProtocol;
43 * \brief Access to the Ipv4 forwarding table, interfaces, and configuration
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
55 * -# exporting Ipv4 configuration attributes
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)
65 * Ipv4 attributes such as whether IP forwarding is enabled and disabled
66 * are also stored in this class
68 * TO DO: Add API to allow access to the Ipv4 neighbor table
70 * \see Ipv4RoutingProtocol
71 * \see Ipv4InterfaceAddress
73 class Ipv4 : public Object
76 static TypeId GetTypeId (void);
81 * \brief Register a new routing protocol to be used by this Ipv4 stack
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.
87 * \param routingProtocol smart pointer to Ipv4RoutingProtocol object
89 virtual void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol) = 0;
92 * \brief Get the routing protocol to be used by this Ipv4 stack
94 * \returns smart pointer to Ipv4RoutingProtocol object, or null pointer if none
96 virtual Ptr<Ipv4RoutingProtocol> GetRoutingProtocol (void) const = 0;
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.
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.
107 virtual uint32_t AddInterface (Ptr<NetDevice> device) = 0;
110 * \returns the number of interfaces added by the user.
112 virtual uint32_t GetNInterfaces (void) const = 0;
115 * \brief Return the interface number of the interface that has been
116 * assigned the specified IP address.
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.
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.
128 virtual int32_t GetInterfaceForAddress (Ipv4Address address) const = 0;
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
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.
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.
146 virtual int32_t GetInterfaceForPrefix (Ipv4Address address,
147 Ipv4Mask mask) const = 0;
150 * \param interface The interface number of an Ipv4 interface.
151 * \returns The NetDevice associated with the Ipv4 interface number.
153 virtual Ptr<NetDevice> GetNetDevice (uint32_t interface) = 0;
156 * \param device The NetDevice for an Ipv4Interface
157 * \returns The interface number of an Ipv4 interface or -1 if not found.
159 virtual int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const = 0;
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
166 virtual bool AddAddress (uint32_t interface, Ipv4InterfaceAddress address) = 0;
169 * \param interface Interface number of an Ipv4 interface
170 * \returns the number of Ipv4InterfaceAddress entries for the interface.
172 virtual uint32_t GetNAddresses (uint32_t interface) const = 0;
175 * Because addresses can be removed, the addressIndex is not guaranteed
176 * to be static across calls to this method.
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
182 virtual Ipv4InterfaceAddress GetAddress (uint32_t interface, uint32_t addressIndex) const = 0;
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.
190 * \param interface Interface number of an Ipv4 interface
191 * \param addressIndex index of Ipv4InterfaceAddress to remove
192 * \returns true if the operation succeeded
194 virtual bool RemoveAddress (uint32_t interface, uint32_t addressIndex) = 0;
197 * \param interface The interface number of an Ipv4 interface
198 * \param metric routing metric (cost) associated to the underlying
201 virtual void SetMetric (uint32_t interface, uint16_t metric) = 0;
204 * \param interface The interface number of an Ipv4 interface
205 * \returns routing metric (cost) associated to the underlying
208 virtual uint16_t GetMetric (uint32_t interface) const = 0;
211 * \param interface Interface number of Ipv4 interface
212 * \returns the Maximum Transmission Unit (in bytes) associated
213 * to the underlying Ipv4 interface
215 virtual uint16_t GetMtu (uint32_t interface) const = 0;
218 * \param interface Interface number of Ipv4 interface
219 * \returns true if the underlying interface is in the "up" state,
222 virtual bool IsUp (uint32_t interface) const = 0;
225 * \param interface Interface number of Ipv4 interface
227 * Set the interface into the "up" state. In this state, it is
228 * considered valid during Ipv4 forwarding.
230 virtual void SetUp (uint32_t interface) = 0;
233 * \param interface Interface number of Ipv4 interface
235 * Set the interface into the "down" state. In this state, it is
236 * ignored during Ipv4 forwarding.
238 virtual void SetDown (uint32_t interface) = 0;
241 * \param interface Interface number of Ipv4 interface
242 * \returns true if IP forwarding enabled for input datagrams on this device
244 virtual bool IsForwarding (uint32_t interface) const = 0;
247 * \param interface Interface number of Ipv4 interface
248 * \param val Value to set the forwarding flag
250 * If set to true, IP forwarding is enabled for input datagrams on this device
252 virtual void SetForwarding (uint32_t interface, bool val) = 0;
254 static const uint32_t IF_ANY = 0xffffffff;
257 // Indirect the Ipv4 attributes through private pure virtual methods
258 virtual void SetIpForward (bool forward) = 0;
259 virtual bool GetIpForward (void) const = 0;