A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef IPV4_H
20#define IPV4_H
21
23#include "ipv4-route.h"
24
25#include "ns3/callback.h"
26#include "ns3/deprecated.h"
27#include "ns3/ipv4-address.h"
28#include "ns3/object.h"
29#include "ns3/socket.h"
30
31#include <stdint.h>
32
33namespace ns3
34{
35
36class Node;
37class NetDevice;
38class Packet;
39class Ipv4RoutingProtocol;
40class IpL4Protocol;
41class Ipv4Header;
42
43/**
44 * \ingroup internet
45 * \defgroup ipv4 IPv4 classes and sub-modules
46 */
47/**
48 * \ingroup ipv4
49 * \brief Access to the IPv4 forwarding table, interfaces, and configuration
50 *
51 * This class defines the API to manipulate the following aspects of
52 * the IPv4 implementation:
53 * -# set/get an Ipv4RoutingProtocol
54 * -# register a NetDevice for use by the Ipv4 layer (basically, to
55 * create Ipv4-related state such as addressing and neighbor cache that
56 * is associated with a NetDevice)
57 * -# manipulate the status of the NetDevice from the Ipv4 perspective,
58 * such as marking it as Up or Down,
59 * -# adding, deleting, and getting addresses associated to the Ipv4
60 * interfaces.
61 * -# exporting Ipv4 configuration attributes
62 *
63 * Each NetDevice has conceptually a single Ipv4 interface associated
64 * with it (the corresponding structure in the Linux Ipv4 implementation
65 * is struct in_device). Each interface may have one or more Ipv4
66 * addresses associated with it. Each Ipv4 address may have different
67 * subnet mask, scope, etc., so all of this per-address information
68 * is stored in an Ipv4InterfaceAddress class (the corresponding
69 * structure in Linux is struct in_ifaddr)
70 *
71 * Ipv4 attributes such as whether IP forwarding is enabled and disabled
72 * are also stored in this class
73 *
74 * TO DO: Add API to allow access to the Ipv4 neighbor table
75 *
76 * \see Ipv4RoutingProtocol
77 * \see Ipv4InterfaceAddress
78 */
79class Ipv4 : public Object
80{
81 public:
82 /**
83 * \brief Get the type ID.
84 * \return the object TypeId
85 */
86 static TypeId GetTypeId();
87 Ipv4();
88 ~Ipv4() override;
89
90 /**
91 * \brief Register a new routing protocol to be used by this Ipv4 stack
92 *
93 * This call will replace any routing protocol that has been previously
94 * registered. If you want to add multiple routing protocols, you must
95 * add them to a Ipv4ListRoutingProtocol directly.
96 *
97 * \param routingProtocol smart pointer to Ipv4RoutingProtocol object
98 */
99 virtual void SetRoutingProtocol(Ptr<Ipv4RoutingProtocol> routingProtocol) = 0;
100
101 /**
102 * \brief Get the routing protocol to be used by this Ipv4 stack
103 *
104 * \returns smart pointer to Ipv4RoutingProtocol object, or null pointer if none
105 */
107
108 /**
109 * \param device device to add to the list of Ipv4 interfaces
110 * which can be used as output interfaces during packet forwarding.
111 * \returns the index of the Ipv4 interface added.
112 *
113 * Once a device has been added, it can never be removed: if you want
114 * to disable it, you can invoke Ipv4::SetDown which will
115 * make sure that it is never used during packet forwarding.
116 */
118
119 /**
120 * \returns the number of interfaces added by the user.
121 */
122 virtual uint32_t GetNInterfaces() const = 0;
123
124 /**
125 * \brief Return the interface number of the interface that has been
126 * assigned the specified IP address.
127 *
128 * \param address The IP address being searched for
129 * \returns The interface number of the Ipv4 interface with the given
130 * address or -1 if not found.
131 *
132 * Each IP interface has one or more IP addresses associated with it.
133 * This method searches the list of interfaces for one that holds a
134 * particular address. This call takes an IP address as a parameter and
135 * returns the interface number of the first interface that has been assigned
136 * that address, or -1 if not found. There must be an exact match; this
137 * method will not match broadcast or multicast addresses.
138 */
139 virtual int32_t GetInterfaceForAddress(Ipv4Address address) const = 0;
140
141 /**
142 * \param packet packet to send
143 * \param source source address of packet
144 * \param destination address of packet
145 * \param protocol number of packet
146 * \param route route entry
147 *
148 * Higher-level layers call this method to send a packet
149 * down the stack to the MAC and PHY layers.
150 */
151 virtual void Send(Ptr<Packet> packet,
152 Ipv4Address source,
153 Ipv4Address destination,
154 uint8_t protocol,
155 Ptr<Ipv4Route> route) = 0;
156
157 /**
158 * \param packet packet to send
159 * \param ipHeader IP Header
160 * \param route route entry
161 *
162 * Higher-level layers call this method to send a packet with IPv4 Header
163 * (Intend to be used with IpHeaderInclude attribute.)
164 */
165 virtual void SendWithHeader(Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route) = 0;
166
167 /**
168 * \param protocol a template for the protocol to add to this L4 Demux.
169 *
170 * Invoke Copy on the input template to get a copy of the input
171 * protocol which can be used on the Node on which this L4 Demux
172 * is running. The new L4Protocol is registered internally as
173 * a working L4 Protocol and returned from this method.
174 * The caller does not get ownership of the returned pointer.
175 */
176 virtual void Insert(Ptr<IpL4Protocol> protocol) = 0;
177
178 /**
179 * \brief Add a L4 protocol to a specific interface.
180 *
181 * This may be called multiple times for multiple interfaces for the same
182 * protocol. To insert for all interfaces, use the separate
183 * Insert (Ptr<IpL4Protocol> protocol) method.
184 *
185 * Setting a protocol on a specific interface will overwrite the
186 * previously bound protocol.
187 *
188 * \param protocol L4 protocol.
189 * \param interfaceIndex interface index.
190 */
191 virtual void Insert(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) = 0;
192
193 /**
194 * \param protocol protocol to remove from this demux.
195 *
196 * The input value to this method should be the value
197 * returned from the Ipv4L4Protocol::Insert method.
198 */
199 virtual void Remove(Ptr<IpL4Protocol> protocol) = 0;
200
201 /**
202 * \brief Remove a L4 protocol from a specific interface.
203 * \param protocol L4 protocol to remove.
204 * \param interfaceIndex interface index.
205 */
206 virtual void Remove(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) = 0;
207
208 /**
209 * \brief Determine whether address and interface corresponding to
210 * received packet can be accepted for local delivery
211 *
212 * \param address The IP address being considered
213 * \param iif The incoming Ipv4 interface index
214 * \returns true if the address is associated with the interface index
215 *
216 * This method can be used to determine whether a received packet has
217 * an acceptable address for local delivery on the host. The address
218 * may be a unicast, multicast, or broadcast address. This method will
219 * return true if address is an exact match of a unicast address on
220 * one of the host's interfaces (see below), if address corresponds to
221 * a multicast group that the host has joined (and the incoming device
222 * is acceptable), or if address corresponds to a broadcast address.
223 *
224 * If the Ipv4 attribute StrongEndSystemModel is true, the address must match one assigned to
225 * the incoming device. If the attribute is false, the unicast address may match any of the Ipv4
226 * addresses on any interface.
227 */
228 virtual bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const = 0;
229
230 /**
231 * \brief Return the interface number of first interface found that
232 * has an Ipv4 address within the prefix specified by the input
233 * address and mask parameters
234 *
235 * \param address The IP address assigned to the interface of interest.
236 * \param mask The IP prefix to use in the mask
237 * \returns The interface number of the Ipv4 interface with the given
238 * address or -1 if not found.
239 *
240 * Each IP interface has one or more IP addresses associated with it.
241 * This method searches the list of interfaces for the first one found
242 * that holds an address that is included within the prefix
243 * formed by the input address and mask parameters. The value -1 is
244 * returned if no match is found.
245 */
246 virtual int32_t GetInterfaceForPrefix(Ipv4Address address, Ipv4Mask mask) const = 0;
247
248 /**
249 * \param interface The interface number of an Ipv4 interface.
250 * \returns The NetDevice associated with the Ipv4 interface number.
251 */
252 virtual Ptr<NetDevice> GetNetDevice(uint32_t interface) = 0;
253
254 /**
255 * \param device The NetDevice for an Ipv4Interface
256 * \returns The interface number of an Ipv4 interface or -1 if not found.
257 */
259
260 /**
261 * \param interface Interface number of an Ipv4 interface
262 * \param address Ipv4InterfaceAddress address to associate with the underlying Ipv4 interface
263 * \returns true if the operation succeeded
264 */
265 virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address) = 0;
266
267 /**
268 * \param interface Interface number of an Ipv4 interface
269 * \returns the number of Ipv4InterfaceAddress entries for the interface.
270 */
271 virtual uint32_t GetNAddresses(uint32_t interface) const = 0;
272
273 /**
274 * Because addresses can be removed, the addressIndex is not guaranteed
275 * to be static across calls to this method.
276 *
277 * \param interface Interface number of an Ipv4 interface
278 * \param addressIndex index of Ipv4InterfaceAddress
279 * \returns the Ipv4InterfaceAddress associated to the interface and addressIndex
280 */
281 virtual Ipv4InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const = 0;
282
283 /**
284 * Remove the address at addressIndex on named interface. The addressIndex
285 * for all higher indices will decrement by one after this method is called;
286 * so, for example, to remove 5 addresses from an interface i, one could
287 * call RemoveAddress (i, 0); 5 times.
288 *
289 * \param interface Interface number of an Ipv4 interface
290 * \param addressIndex index of Ipv4InterfaceAddress to remove
291 * \returns true if the operation succeeded
292 */
293 virtual bool RemoveAddress(uint32_t interface, uint32_t addressIndex) = 0;
294
295 /**
296 * \brief Remove the given address on named Ipv4 interface
297 *
298 * \param interface Interface number of an Ipv4 interface
299 * \param address The address to remove
300 * \returns true if the operation succeeded
301 */
302 virtual bool RemoveAddress(uint32_t interface, Ipv4Address address) = 0;
303
304 /**
305 * \brief Return the first primary source address with scope less than
306 * or equal to the requested scope, to use in sending a packet to
307 * destination dst out of the specified device.
308 *
309 * This method mirrors the behavior of Linux inet_select_addr() and is
310 * provided because interfaces may have multiple IP addresses configured
311 * on them with different scopes, and with a primary and secondary status.
312 * Secondary addresses are never returned.
313 * \see Ipv4InterfaceAddress
314 *
315 * If a non-zero device pointer is provided, the method first tries to
316 * return a primary address that is configured on that device, and whose
317 * subnet matches that of dst and whose scope is less than or equal to
318 * the requested scope. If a primary address does not match the
319 * subnet of dst but otherwise matches the scope, it is returned.
320 * If no such address on the device is found, the other devices are
321 * searched in order of their interface index, but not considering dst
322 * as a factor in the search. Because a loopback interface is typically
323 * the first one configured on a node, it will be the first alternate
324 * device to be tried. Addresses scoped at LINK scope are not returned
325 * in this phase.
326 *
327 * If no device pointer is provided, the same logic as above applies, only
328 * that there is no preferred device that is consulted first. This means
329 * that if the device pointer is null, input parameter dst will be ignored.
330 *
331 * If there are no possible addresses to return, a warning log message
332 * is issued and the all-zeroes address is returned.
333 *
334 * \param device output NetDevice (optionally provided, only to constrain the search)
335 * \param dst Destination address to match, if device is provided
336 * \param scope Scope of returned address must be less than or equal to this
337 * \returns the first primary Ipv4Address that meets the search criteria
338 */
341 Ipv4Address dst,
343
344 /**
345 * \param interface The interface number of an Ipv4 interface
346 * \param metric routing metric (cost) associated to the underlying
347 * Ipv4 interface
348 */
349 virtual void SetMetric(uint32_t interface, uint16_t metric) = 0;
350
351 /**
352 * \param interface The interface number of an Ipv4 interface
353 * \returns routing metric (cost) associated to the underlying
354 * Ipv4 interface
355 */
356 virtual uint16_t GetMetric(uint32_t interface) const = 0;
357
358 /**
359 * \param interface Interface number of Ipv4 interface
360 * \returns the Maximum Transmission Unit (in bytes) associated
361 * to the underlying Ipv4 interface
362 */
363 virtual uint16_t GetMtu(uint32_t interface) const = 0;
364
365 /**
366 * \param interface Interface number of Ipv4 interface
367 * \returns true if the underlying interface is in the "up" state,
368 * false otherwise.
369 */
370 virtual bool IsUp(uint32_t interface) const = 0;
371
372 /**
373 * \param interface Interface number of Ipv4 interface
374 *
375 * Set the interface into the "up" state. In this state, it is
376 * considered valid during Ipv4 forwarding.
377 */
378 virtual void SetUp(uint32_t interface) = 0;
379
380 /**
381 * \param interface Interface number of Ipv4 interface
382 *
383 * Set the interface into the "down" state. In this state, it is
384 * ignored during Ipv4 forwarding.
385 */
386 virtual void SetDown(uint32_t interface) = 0;
387
388 /**
389 * \param interface Interface number of Ipv4 interface
390 * \returns true if IP forwarding enabled for input datagrams on this device
391 */
392 virtual bool IsForwarding(uint32_t interface) const = 0;
393
394 /**
395 * \param interface Interface number of Ipv4 interface
396 * \param val Value to set the forwarding flag
397 *
398 * If set to true, IP forwarding is enabled for input datagrams on this device
399 */
400 virtual void SetForwarding(uint32_t interface, bool val) = 0;
401
402 /**
403 * \brief Choose the source address to use with destination address.
404 * \param interface interface index
405 * \param dest IPv4 destination address
406 * \return IPv4 source address to use
407 */
409
410 /**
411 * \param protocolNumber number of protocol to lookup
412 * in this L4 Demux
413 * \returns a matching L4 Protocol
414 *
415 * This method is typically called by lower layers
416 * to forward packets up the stack to the right protocol.
417 */
418 virtual Ptr<IpL4Protocol> GetProtocol(int protocolNumber) const = 0;
419
420 /**
421 * \brief Get L4 protocol by protocol number for the specified interface.
422 * \param protocolNumber protocol number
423 * \param interfaceIndex interface index, -1 means "any" interface.
424 * \return corresponding IpL4Protocol or 0 if not found
425 */
426 virtual Ptr<IpL4Protocol> GetProtocol(int protocolNumber, int32_t interfaceIndex) const = 0;
427
428 /**
429 * \brief Creates a raw socket
430 *
431 * \returns a smart pointer to the instantiated raw socket
432 */
434
435 /**
436 * \brief Deletes a particular raw socket
437 *
438 * \param socket Smart pointer to the raw socket to be deleted
439 */
440 virtual void DeleteRawSocket(Ptr<Socket> socket) = 0;
441
442 static const uint32_t IF_ANY = 0xffffffff; //!< interface wildcard, meaning any interface
443
444 private:
445 // Indirect the Ipv4 attributes through private pure virtual methods
446
447 /**
448 * \brief Set or unset the IP forwarding state
449 * \param forward the forwarding state
450 */
451 virtual void SetIpForward(bool forward) = 0;
452 /**
453 * \brief Get the IP forwarding state
454 * \returns true if IP is in forwarding state
455 */
456 virtual bool GetIpForward() const = 0;
457
458 /**
459 * \brief Get the Weak Es Model status
460 *
461 * RFC1122 term for whether host accepts datagram with a dest. address on another interface
462 * \returns true for Weak Es Model activated
463 * \deprecated Deprecated since ns-3.41. Use SetStrongEndSystemModel instead.
464 */
465 NS_DEPRECATED_3_41("Use GetStrongEndSystemModel instead")
466 virtual bool GetWeakEsModel() const = 0;
467
468 /**
469 * \brief Set or unset the Weak Es Model
470 *
471 * RFC1122 term for whether host accepts datagram with a dest. address on another interface
472 * \param model true for Weak Es Model
473 * \deprecated Deprecated since ns-3.41. Use SetStrongEndSystemModel instead.
474 */
476 virtual void SetWeakEsModel(bool model) = 0;
477
478 /**
479 * \brief Set or unset the Strong End System Model
480 *
481 * RFC1122 term for whether host rejects datagram with a dest. address on another interface
482 * \param model true for Strong End System Model
483 */
484 virtual void SetStrongEndSystemModel(bool model) = 0;
485 /**
486 * \brief Get the Strong End System Model status
487 *
488 * RFC1122 term for whether host rejects datagram with a dest. address on another interface
489 * \returns true for Strong End System Model activated
490 */
491 virtual bool GetStrongEndSystemModel() const = 0;
492};
493
494} // namespace ns3
495
496#endif /* IPV4_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
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
virtual int32_t GetInterfaceForAddress(Ipv4Address address) const =0
Return the interface number of the interface that has been assigned the specified IP address.
virtual bool GetIpForward() const =0
Get the IP forwarding state.
virtual bool GetStrongEndSystemModel() const =0
Get the Strong End System Model status.
virtual void SetIpForward(bool forward)=0
Set or unset the IP forwarding state.
virtual void SetStrongEndSystemModel(bool model)=0
Set or unset the Strong End System Model.
Ipv4()
Definition: ipv4.cc:76
virtual void DeleteRawSocket(Ptr< Socket > socket)=0
Deletes a particular raw socket.
virtual bool IsForwarding(uint32_t interface) const =0
virtual Ptr< Ipv4RoutingProtocol > GetRoutingProtocol() const =0
Get the routing protocol to be used by this Ipv4 stack.
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)=0
Register a new routing protocol to be used by this Ipv4 stack.
virtual void SetWeakEsModel(bool model)=0
Set or unset the Weak Es Model.
virtual void Insert(Ptr< IpL4Protocol > protocol)=0
virtual uint32_t GetNAddresses(uint32_t interface) const =0
virtual void Remove(Ptr< IpL4Protocol > protocol)=0
virtual bool RemoveAddress(uint32_t interface, Ipv4Address address)=0
Remove the given address on named Ipv4 interface.
virtual uint16_t GetMtu(uint32_t interface) const =0
virtual Ipv4InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
virtual bool GetWeakEsModel() const =0
Get the Weak Es Model status.
virtual void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)=0
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const =0
virtual void SetUp(uint32_t interface)=0
virtual Ipv4Address SelectSourceAddress(Ptr< const NetDevice > device, Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope)=0
Return the first primary source address with scope less than or equal to the requested scope,...
virtual Ptr< NetDevice > GetNetDevice(uint32_t interface)=0
virtual void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)=0
virtual bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const =0
Determine whether address and interface corresponding to received packet can be accepted for local de...
virtual void Insert(Ptr< IpL4Protocol > protocol, uint32_t interfaceIndex)=0
Add a L4 protocol to a specific interface.
static const uint32_t IF_ANY
interface wildcard, meaning any interface
Definition: ipv4.h:442
static TypeId GetTypeId()
Get the type ID.
Definition: ipv4.cc:36
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber, int32_t interfaceIndex) const =0
Get L4 protocol by protocol number for the specified interface.
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
virtual int32_t GetInterfaceForPrefix(Ipv4Address address, Ipv4Mask mask) const =0
Return the interface number of first interface found that has an Ipv4 address within the prefix speci...
virtual void SetDown(uint32_t interface)=0
virtual Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest)=0
Choose the source address to use with destination address.
virtual bool RemoveAddress(uint32_t interface, uint32_t addressIndex)=0
Remove the address at addressIndex on named interface.
virtual uint16_t GetMetric(uint32_t interface) const =0
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual uint32_t GetNInterfaces() const =0
virtual void Remove(Ptr< IpL4Protocol > protocol, uint32_t interfaceIndex)=0
Remove a L4 protocol from a specific interface.
virtual Ptr< Socket > CreateRawSocket()=0
Creates a raw socket.
virtual void SetForwarding(uint32_t interface, bool val)=0
virtual bool IsUp(uint32_t interface) const =0
~Ipv4() override
Definition: ipv4.cc:81
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
#define NS_DEPRECATED_3_41(msg)
Tag for things deprecated in version ns-3.41.
Definition: deprecated.h:109
Every class exported by the ns3 library is enclosed in the ns3 namespace.