A Discrete-Event Network Simulator
Go to the documentation of this file.
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
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 //
21 #ifndef IPV4_L3_PROTOCOL_H
22 #define IPV4_L3_PROTOCOL_H
24 #include <list>
25 #include <map>
26 #include <vector>
27 #include <stdint.h>
28 #include "ns3/ipv4-address.h"
29 #include "ns3/ptr.h"
30 #include "ns3/net-device.h"
31 #include "ns3/ipv4.h"
32 #include "ns3/traced-callback.h"
33 #include "ns3/ipv4-header.h"
34 #include "ns3/ipv4-routing-protocol.h"
35 #include "ns3/nstime.h"
36 #include "ns3/simulator.h"
40 namespace ns3 {
42 class Packet;
43 class NetDevice;
44 class Ipv4Interface;
45 class Ipv4Address;
46 class Ipv4Header;
47 class Ipv4RoutingTableEntry;
48 class Ipv4Route;
49 class Node;
50 class Socket;
51 class Ipv4RawSocketImpl;
52 class IpL4Protocol;
53 class Icmpv4L4Protocol;
80 class Ipv4L3Protocol : public Ipv4
81 {
82 public:
87  static TypeId GetTypeId (void);
88  static const uint16_t PROT_NUMBER;
91  virtual ~Ipv4L3Protocol ();
97  enum DropReason
98  {
105  };
111  void SetNode (Ptr<Node> node);
113  // functions defined in base class Ipv4
115  void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
119  void DeleteRawSocket (Ptr<Socket> socket);
121  virtual void Insert (Ptr<IpL4Protocol> protocol);
122  virtual void Insert (Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex);
124  virtual void Remove (Ptr<IpL4Protocol> protocol);
125  virtual void Remove (Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex);
127  virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber) const;
128  virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber, int32_t interfaceIndex) const;
130  virtual Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest);
138  void SetDefaultTtl (uint8_t ttl);
153  void Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
154  const Address &to, NetDevice::PacketType packetType);
166  void Send (Ptr<Packet> packet, Ipv4Address source,
167  Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
176  void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route);
178  uint32_t AddInterface (Ptr<NetDevice> device);
184  Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
185  uint32_t GetNInterfaces (void) const;
187  int32_t GetInterfaceForAddress (Ipv4Address addr) const;
188  int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
189  int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
190  bool IsDestinationAddress (Ipv4Address address, uint32_t iif) const;
192  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
193  Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
194  uint32_t GetNAddresses (uint32_t interface) const;
195  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
196  bool RemoveAddress (uint32_t interface, Ipv4Address address);
201  void SetMetric (uint32_t i, uint16_t metric);
202  uint16_t GetMetric (uint32_t i) const;
203  uint16_t GetMtu (uint32_t i) const;
204  bool IsUp (uint32_t i) const;
205  void SetUp (uint32_t i);
206  void SetDown (uint32_t i);
207  bool IsForwarding (uint32_t i) const;
208  void SetForwarding (uint32_t i, bool val);
210  Ptr<NetDevice> GetNetDevice (uint32_t i);
222  bool IsUnicast (Ipv4Address ad) const;
231  typedef void (* SentTracedCallback)
232  (const Ipv4Header & header, Ptr<const Packet> packet, uint32_t interface);
244  typedef void (* TxRxTracedCallback)
245  (Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
258  typedef void (* DropTracedCallback)
259  (const Ipv4Header & header, Ptr<const Packet> packet,
260  DropReason reason, Ptr<Ipv4> ipv4,
261  uint32_t interface);
263 protected:
265  virtual void DoDispose (void);
270  virtual void NotifyNewAggregate ();
271 private:
276  friend class ::Ipv4L3ProtocolTestCase;
293  // class Ipv4 attributes
294  virtual void SetIpForward (bool forward);
295  virtual bool GetIpForward (void) const;
296  virtual void SetWeakEsModel (bool model);
297  virtual bool GetWeakEsModel (void) const;
311  Ipv4Address source,
312  Ipv4Address destination,
313  uint8_t protocol,
314  uint16_t payloadSize,
315  uint8_t ttl,
316  uint8_t tos,
317  bool mayFragment);
325  void
327  Ptr<Packet> packet,
328  Ipv4Header const &ipHeader);
336  void
337  IpForward (Ptr<Ipv4Route> rtentry,
339  const Ipv4Header &header);
347  void
350  const Ipv4Header &header);
358  void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
366  void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
373  uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
378  void SetupLoopback (void);
384  Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
392  bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
397  typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
406  void DoFragmentation (Ptr<Packet> packet, const Ipv4Header& ipv4Header, uint32_t outIfaceMtu, std::list<Ipv4PayloadHeaderPair>& listFragments);
415  bool ProcessFragment (Ptr<Packet>& packet, Ipv4Header & ipHeader, uint32_t iif);
423  void HandleFragmentsTimeout ( std::pair<uint64_t, uint32_t> key, Ipv4Header & ipHeader, uint32_t iif);
435  void CallTxTrace (const Ipv4Header & ipHeader, Ptr<Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
440  typedef std::vector<Ptr<Ipv4Interface> > Ipv4InterfaceList;
444  typedef std::map<Ptr<const NetDevice>, uint32_t > Ipv4InterfaceReverseContainer;
448  typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
453  typedef std::pair<int, int32_t> L4ListKey_t;
458  typedef std::map<L4ListKey_t, Ptr<IpL4Protocol> > L4List_t;
460  bool m_ipForward;
465  uint8_t m_defaultTtl;
466  std::map<std::pair<uint64_t, uint8_t>, uint16_t> m_identification;
476  // The following two traces pass a packet with an IP header
485  // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
498  class Fragments : public SimpleRefCount<Fragments>
499  {
500 public:
504  Fragments ();
509  ~Fragments ();
517  void AddFragment (Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
523  bool IsEntire () const;
529  Ptr<Packet> GetPacket () const;
535  Ptr<Packet> GetPartialPacket () const;
537 private:
546  std::list<std::pair<Ptr<Packet>, uint16_t> > m_fragments;
548  };
551  typedef std::map< std::pair<uint64_t, uint32_t>, Ptr<Fragments> > MapFragments_t;
553  typedef std::map< std::pair<uint64_t, uint32_t>, EventId > MapFragmentsTimers_t;
559 };
561 } // Namespace ns3
563 #endif /* IPV4_L3_PROTOCOL_H */
virtual Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest)
Choose the source address to use with destination address.
bool m_weakEsModel
Weak ES model state.
void SetDefaultTtl(uint8_t ttl)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual void SetIpForward(bool forward)
Set or unset the IP forwarding state.
std::map< std::pair< uint64_t, uint32_t >, EventId > MapFragmentsTimers_t
Container of fragment timeout event, stored as pairs(src+dst addr, src+dst port) / EventId...
uint32_t GetNAddresses(uint32_t interface) const
bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const
Determine whether address and interface corresponding to received packet can be accepted for local de...
virtual void NotifyNewAggregate()
This function will notify other components connected to the node that a new stack member is now conne...
Time m_fragmentExpirationTimeout
Expiration timeout.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_sendOutgoingTrace
Trace of sent packets.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Forward calls to a chain of Callback.
std::map< std::pair< uint64_t, uint32_t >, Ptr< Fragments > > MapFragments_t
Container of fragments, stored as pairs(src+dst addr, src+dst port) / fragment.
std::list< std::pair< Ptr< Packet >, uint16_t > > m_fragments
The current fragments.
uint32_t AddInterface(Ptr< NetDevice > device)
TracedCallback< const Ipv4Header &, Ptr< const Packet >, DropReason, Ptr< Ipv4 >, uint32_t > m_dropTrace
Trace of dropped packets.
bool IsEntire() const
If all fragments have been added.
int32_t GetInterfaceForAddress(Ipv4Address addr) const
Return the interface number of the interface that has been assigned the specified IP address...
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
virtual bool GetIpForward(void) const
Get the IP forwarding state.
Ptr< Ipv4RoutingProtocol > m_routingProtocol
Routing protocol associated with the stack.
uint32_t GetNInterfaces(void) const
void SetUp(uint32_t i)
L4List_t m_protocols
List of transport protocol.
a polymophic address class
Definition: address.h:90
Ptr< Packet > GetPartialPacket() const
Get the complete part of the packet.
Reason why a packet has been dropped.
Ipv4InterfaceList m_interfaces
List of IPv4 interfaces.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_rxTrace
Trace of received packets.
Packet header for IPv4.
Definition: ipv4-header.h:33
Ptr< Icmpv4L4Protocol > GetIcmp(void) const
Get ICMPv4 protocol.
void SetupLoopback(void)
Setup loopback interface.
bool IsUnicast(Ipv4Address ad) const
Check if an IPv4 address is unicast according to the node.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_txTrace
Trace of transmitted packets.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_localDeliverTrace
Trace of locally delivered packets.
Ptr< Ipv4Interface > GetInterface(uint32_t i) const
Get an interface.
void SetMetric(uint32_t i, uint16_t metric)
virtual void DoDispose(void)
Destructor implementation.
void CallTxTrace(const Ipv4Header &ipHeader, Ptr< Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
Make a copy of the packet, add the header and invoke the TX trace callback.
virtual bool GetWeakEsModel(void) const
Get the Weak Es Model status.
Ptr< Socket > CreateRawSocket(void)
Creates a raw socket.
void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)
static TypeId GetTypeId(void)
Get the type ID.
MapFragments_t m_fragments
Fragmented packets.
void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)
void IpForward(Ptr< Ipv4Route > rtentry, Ptr< const Packet > p, const Ipv4Header &header)
Forward a packet.
A Set of Fragment belonging to the same packet (src, dst, identification and proto) ...
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
std::vector< Ptr< Ipv4Interface > > Ipv4InterfaceList
Container of the IPv4 Interfaces.
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
Remove the address at addressIndex on named interface.
int32_t GetInterfaceForPrefix(Ipv4Address addr, Ipv4Mask mask) const
Return the interface number of first interface found that has an Ipv4 address within the prefix speci...
void AddFragment(Ptr< Packet > fragment, uint16_t fragmentOffset, bool moreFragment)
Add a fragment.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
bool m_ipForward
Forwarding packets (i.e.
void SetForwarding(uint32_t i, bool val)
void DoFragmentation(Ptr< Packet > packet, const Ipv4Header &ipv4Header, uint32_t outIfaceMtu, std::list< Ipv4PayloadHeaderPair > &listFragments)
Fragment a packet.
std::list< Ptr< Ipv4RawSocketImpl > > SocketList
Container of the IPv4 Raw Sockets.
Implement the IPv4 layer.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: first.py:37
std::pair< int, int32_t > L4ListKey_t
Container of the IPv4 L4 keys: protocol number, interface index.
bool IsForwarding(uint32_t i) const
Ptr< Node > m_node
Node attached to stack.
void RouteInputError(Ptr< const Packet > p, const Ipv4Header &ipHeader, Socket::SocketErrno sockErrno)
Fallback when no route is found.
Ipv4Header BuildHeader(Ipv4Address source, Ipv4Address destination, uint8_t protocol, uint16_t payloadSize, uint8_t ttl, uint8_t tos, bool mayFragment)
Construct an IPv4 header.
void SendRealOut(Ptr< Ipv4Route > route, Ptr< Packet > packet, Ipv4Header const &ipHeader)
Send packet with route.
std::map< Ptr< const NetDevice >, uint32_t > Ipv4InterfaceReverseContainer
Container of NetDevices registered to IPv4 and their interface indexes.
Ptr< NetDevice > GetNetDevice(uint32_t i)
void(* SentTracedCallback)(const Ipv4Header &header, Ptr< const Packet > packet, uint32_t interface)
TracedCallback signature for packet send, forward, or local deliver events.
bool ProcessFragment(Ptr< Packet > &packet, Ipv4Header &ipHeader, uint32_t iif)
Process a packet fragment.
uint32_t AddIpv4Interface(Ptr< Ipv4Interface > interface)
Add an IPv4 interface to the stack.
uint16_t GetMetric(uint32_t i) const
virtual void Remove(Ptr< IpL4Protocol > protocol)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual void SetWeakEsModel(bool model)
Set or unset the Weak Es Model.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const
a class to store IPv4 address information on an interface
An identifier for simulation events.
Definition: event-id.h:53
Ptr< Packet > GetPacket() const
Get the entire packet.
SocketList m_sockets
List of IPv4 raw sockets.
void DeleteRawSocket(Ptr< Socket > socket)
Deletes a particular raw socket.
bool AddAddress(uint32_t i, Ipv4InterfaceAddress address)
virtual void Insert(Ptr< IpL4Protocol > protocol)
Ipv4L3Protocol & operator=(const Ipv4L3Protocol &)
Copy constructor.
void LocalDeliver(Ptr< const Packet > p, Ipv4Header const &ip, uint32_t iif)
Deliver a packet.
void IpMulticastForward(Ptr< Ipv4MulticastRoute > mrtentry, Ptr< const Packet > p, const Ipv4Header &header)
Forward a multicast packet.
MapFragmentsTimers_t m_fragmentsTimers
Expiration events.
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Lower layer calls this method after calling L3Demux::Lookup The ARP subclass needs to know from which...
uint8_t m_defaultTtl
Default TTL.
Interface is down so can not send packet.
uint16_t GetMtu(uint32_t i) const
void(* TxRxTracedCallback)(Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet transmission or reception events.
Packet types are used as they are in Linux.
Definition: net-device.h:296
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
Trace of unicast forwarded packets.
Ipv4InterfaceReverseContainer m_reverseInterfacesContainer
Container of NetDevice / Interface index associations.
std::map< L4ListKey_t, Ptr< IpL4Protocol > > L4List_t
Container of the IPv4 L4 instances.
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:58
std::map< std::pair< uint64_t, uint8_t >, uint16_t > m_identification
Identification (for each {src, dst, proto} tuple)
bool IsUp(uint32_t i) const
void SetDown(uint32_t i)
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
void HandleFragmentsTimeout(std::pair< uint64_t, uint32_t > key, Ipv4Header &ipHeader, uint32_t iif)
Process the timeout for packet fragments.
static const uint16_t PROT_NUMBER
Protocol number (0x0800)
bool m_moreFragment
True if other fragments will be sent.
void(* DropTracedCallback)(const Ipv4Header &header, Ptr< const Packet > packet, DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet drop events.
Ipv4Address SelectSourceAddress(Ptr< const NetDevice > device, Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope)
Return the first primary source address with scope less than or equal to the requested scope...
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.