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: Miralem Mehic <miralem.mehic@ieee.org> after
19 // George F. Riley<riley@ece.gatech.edu>
20 //
25 #include <list>
26 #include <map>
27 #include <vector>
28 #include <stdint.h>
29 #include "ns3/ipv4-address.h"
30 #include "ns3/ptr.h"
31 #include "ns3/net-device.h"
32 #include "ns3/ipv4.h"
33 #include "ns3/traced-callback.h"
34 #include "ns3/ipv4-header.h"
35 #include "ns3/ipv4-routing-protocol.h"
36 #include "ns3/nstime.h"
37 #include "ns3/simulator.h"
39 class VirtualIpv4L3ProtocolTestCase;
41 namespace ns3 {
43 class Packet;
44 class NetDevice;
45 class Ipv4Interface;
46 class Ipv4Address;
47 class Ipv4Header;
48 class Ipv4RoutingTableEntry;
49 class Ipv4Route;
50 class Node;
51 class Socket;
52 class Ipv4RawSocketImpl;
53 class IpL4Protocol;
54 class Icmpv4L4Protocol;
83 {
84 public:
89  static TypeId GetTypeId (void);
90  static const uint16_t PROT_NUMBER;
93  virtual ~VirtualIpv4L3Protocol ();
99  enum DropReason
100  {
107  };
113  void SetNode (Ptr<Node> node);
115  // functions defined in base class Ipv4
117  void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
121  void DeleteRawSocket (Ptr<Socket> socket);
123  virtual void Insert (Ptr<IpL4Protocol> protocol);
124  virtual void Insert (Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex);
126  virtual void Remove (Ptr<IpL4Protocol> protocol);
127  virtual void Remove (Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex);
129  virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber) const;
130  virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber, int32_t interfaceIndex) const;
132  virtual Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest);
140  void SetDefaultTtl (uint8_t ttl);
155  void Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
156  const Address &to, NetDevice::PacketType packetType);
168  void Send (Ptr<Packet> packet, Ipv4Address source,
169  Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
178  void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route);
180  uint32_t AddInterface (Ptr<NetDevice> device);
186  Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
187  uint32_t GetNInterfaces (void) const;
189  int32_t GetInterfaceForAddress (Ipv4Address addr) const;
190  int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
191  int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
192  bool IsDestinationAddress (Ipv4Address address, uint32_t iif) const;
194  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
195  Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
196  uint32_t GetNAddresses (uint32_t interface) const;
197  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
198  bool RemoveAddress (uint32_t interface, Ipv4Address address);
203  void SetMetric (uint32_t i, uint16_t metric);
204  uint16_t GetMetric (uint32_t i) const;
205  uint16_t GetMtu (uint32_t i) const;
206  bool IsUp (uint32_t i) const;
207  void SetUp (uint32_t i);
208  void SetDown (uint32_t i);
209  bool IsForwarding (uint32_t i) const;
210  void SetForwarding (uint32_t i, bool val);
212  Ptr<NetDevice> GetNetDevice (uint32_t i);
224  bool IsUnicast (Ipv4Address ad) const;
233  typedef void (* SentTracedCallback)
234  (const Ipv4Header & header, Ptr<const Packet> packet, uint32_t interface);
246  typedef void (* TxRxTracedCallback)
247  (Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
260  typedef void (* DropTracedCallback)
261  (const Ipv4Header & header, Ptr<const Packet> packet,
262  DropReason reason, Ptr<Ipv4> ipv4,
263  uint32_t interface);
265 protected:
267  virtual void DoDispose (void);
272  virtual void NotifyNewAggregate ();
273 private:
274  friend class ::VirtualIpv4L3ProtocolTestCase;
291  // class Ipv4 attributes
292  virtual void SetIpForward (bool forward);
293  virtual bool GetIpForward (void) const;
294  virtual void SetWeakEsModel (bool model);
295  virtual bool GetWeakEsModel (void) const;
309  Ipv4Address source,
310  Ipv4Address destination,
311  uint8_t protocol,
312  uint16_t payloadSize,
313  uint8_t ttl,
314  uint8_t tos,
315  bool mayFragment);
323  void
325  Ptr<Packet> packet,
326  Ipv4Header const &ipHeader);
334  void
335  IpForward (Ptr<Ipv4Route> rtentry,
337  const Ipv4Header &header);
345  void
348  const Ipv4Header &header);
356  void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
364  void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
371  uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
376  void SetupLoopback (void);
382  Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
390  bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
395  typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
404  void DoFragmentation (Ptr<Packet> packet, const Ipv4Header& ipv4Header, uint32_t outIfaceMtu, std::list<Ipv4PayloadHeaderPair>& listFragments);
413  bool ProcessFragment (Ptr<Packet>& packet, Ipv4Header & ipHeader, uint32_t iif);
421  void HandleFragmentsTimeout ( std::pair<uint64_t, uint32_t> key, Ipv4Header & ipHeader, uint32_t iif);
433  void CallTxTrace (const Ipv4Header & ipHeader, Ptr<Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
438  typedef std::vector<Ptr<Ipv4Interface> > Ipv4InterfaceList;
442  typedef std::map<Ptr<const NetDevice>, uint32_t > Ipv4InterfaceReverseContainer;
446  typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
451  typedef std::pair<int, int32_t> L4ListKey_t;
456  typedef std::map<L4ListKey_t, Ptr<IpL4Protocol> > L4List_t;
458  bool m_ipForward;
460  L4List_t m_protocols;
461  Ipv4InterfaceList m_interfaces;
462  Ipv4InterfaceReverseContainer m_reverseInterfacesContainer;
463  uint8_t m_defaultTtl;
464  std::map<std::pair<uint64_t, uint8_t>, uint16_t> m_identification;
474  // The following two traces pass a packet with an IP header
483  // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
491  SocketList m_sockets;
496  class Fragments : public SimpleRefCount<Fragments>
497  {
498 public:
502  Fragments ();
507  ~Fragments ();
515  void AddFragment (Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
521  bool IsEntire () const;
527  Ptr<Packet> GetPacket () const;
533  Ptr<Packet> GetPartialPacket () const;
535 private:
544  std::list<std::pair<Ptr<Packet>, uint16_t> > m_fragments;
546  };
549  typedef std::map< std::pair<uint64_t, uint32_t>, Ptr<Fragments> > MapFragments_t;
551  typedef std::map< std::pair<uint64_t, uint32_t>, EventId > MapFragmentsTimers_t;
553  MapFragments_t m_fragments;
555  MapFragmentsTimers_t m_fragmentsTimers;
557 };
559 } // Namespace ns3
561 #endif /* VIRTUAL_IPV4_L3_PROTOCOL_H */
void DoFragmentation(Ptr< Packet > packet, const Ipv4Header &ipv4Header, uint32_t outIfaceMtu, std::list< Ipv4PayloadHeaderPair > &listFragments)
Fragment a packet.
uint16_t GetMtu(uint32_t i) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SetMetric(uint32_t i, uint16_t metric)
void HandleFragmentsTimeout(std::pair< uint64_t, uint32_t > key, Ipv4Header &ipHeader, uint32_t iif)
Process the timeout for packet fragments.
Ptr< Ipv4Interface > GetInterface(uint32_t i) const
Get an interface.
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Ptr< Icmpv4L4Protocol > GetIcmp(void) const
Get ICMPv4 protocol.
Forward calls to a chain of Callback.
Ipv4InterfaceList m_interfaces
List of IPv4 interfaces.
std::vector< Ptr< Ipv4Interface > > Ipv4InterfaceList
Container of the IPv4 Interfaces.
static TypeId GetTypeId(void)
Get the type ID.
Ptr< Packet > GetPartialPacket() const
Get the complete part of the packet.
uint32_t GetNAddresses(uint32_t interface) const
void SetForwarding(uint32_t i, bool val)
bool AddAddress(uint32_t i, Ipv4InterfaceAddress address)
void DeleteRawSocket(Ptr< Socket > socket)
Deletes a particular raw socket.
void LocalDeliver(Ptr< const Packet > p, Ipv4Header const &ip, uint32_t iif)
Deliver a packet.
Ptr< NetDevice > GetNetDevice(uint32_t i)
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
MapFragmentsTimers_t m_fragmentsTimers
Expiration events.
Ptr< Socket > CreateRawSocket(void)
Creates a raw socket.
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.
Ptr< Node > m_node
Node attached to stack.
a polymophic address class
Definition: address.h:90
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.
void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)
virtual void SetIpForward(bool forward)
Set or unset the IP forwarding state.
Packet header for IPv4.
Definition: ipv4-header.h:33
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
virtual void Insert(Ptr< IpL4Protocol > protocol)
void(* DropTracedCallback)(const Ipv4Header &header, Ptr< const Packet > packet, DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet drop events.
bool IsForwarding(uint32_t i) const
void IpMulticastForward(Ptr< Ipv4MulticastRoute > mrtentry, Ptr< const Packet > p, const Ipv4Header &header)
Forward a multicast packet.
int32_t GetInterfaceForAddress(Ipv4Address addr) const
Return the interface number of the interface that has been assigned the specified IP address...
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...
Reason why a packet has been dropped.
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.
void(* SentTracedCallback)(const Ipv4Header &header, Ptr< const Packet > packet, uint32_t interface)
TracedCallback signature for packet send, forward, or local deliver events.
uint32_t AddIpv4Interface(Ptr< Ipv4Interface > interface)
Add an IPv4 interface to the stack.
Ptr< Ipv4RoutingProtocol > m_routingProtocol
Routing protocol associated with the stack.
Time m_fragmentExpirationTimeout
Expiration timeout.
void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)
MapFragments_t m_fragments
Fragmented packets.
std::map< std::pair< uint64_t, uint8_t >, uint16_t > m_identification
Identification (for each {src, dst, proto} tuple)
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
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...
bool IsUnicast(Ipv4Address ad) const
Check if an IPv4 address is unicast according to the node.
L4List_t m_protocols
List of transport protocol.
void IpForward(Ptr< Ipv4Route > rtentry, Ptr< const Packet > p, const Ipv4Header &header)
Forward a packet.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
Implement the IPv4 Overlay (virtual) layer.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
Trace of unicast forwarded packets.
Definition: first.py:37
bool IsEntire() const
If all fragments have been added.
Ptr< Packet > GetPacket() const
Get the entire packet.
std::map< L4ListKey_t, Ptr< IpL4Protocol > > L4List_t
Container of the IPv4 L4 instances.
bool ProcessFragment(Ptr< Packet > &packet, Ipv4Header &ipHeader, uint32_t iif)
Process a packet fragment.
bool m_ipForward
Forwarding packets (i.e.
virtual Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest)
Choose the source address to use with destination address.
std::map< Ptr< const NetDevice >, uint32_t > Ipv4InterfaceReverseContainer
Container of NetDevices registered to IPv4 and their interface indexes.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, DropReason, Ptr< Ipv4 >, uint32_t > m_dropTrace
Trace of dropped packets.
uint32_t AddInterface(Ptr< NetDevice > device)
void AddFragment(Ptr< Packet > fragment, uint16_t fragmentOffset, bool moreFragment)
Add a fragment.
bool m_weakEsModel
Weak ES model state.
virtual void SetWeakEsModel(bool model)
Set or unset the Weak Es Model.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_localDeliverTrace
Trace of locally delivered packets.
virtual void Remove(Ptr< IpL4Protocol > protocol)
std::list< std::pair< Ptr< Packet >, uint16_t > > m_fragments
The current fragments.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_sendOutgoingTrace
Trace of sent packets.
Interface is down so can not send packet.
std::pair< int, int32_t > L4ListKey_t
Container of the IPv4 L4 keys: protocol number, interface index.
A Set of Fragment belonging to the same packet (src, dst, identification and proto) ...
a class to store IPv4 address information on an interface
An identifier for simulation events.
Definition: event-id.h:53
virtual bool GetWeakEsModel(void) const
Get the Weak Es Model status.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_rxTrace
Trace of received packets.
static const uint16_t PROT_NUMBER
Protocol number (0x0800)
SocketList m_sockets
List of IPv4 raw sockets.
void SendRealOut(Ptr< Ipv4Route > route, Ptr< Packet > packet, Ipv4Header const &ipHeader)
Send packet with route.
bool m_moreFragment
True if other fragments will be sent.
Ipv4InterfaceReverseContainer m_reverseInterfacesContainer
Container of NetDevice / Interface index associations.
void RouteInputError(Ptr< const Packet > p, const Ipv4Header &ipHeader, Socket::SocketErrno sockErrno)
Fallback when no route is found.
Packet types are used as they are in Linux.
Definition: net-device.h:302
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
void SetupLoopback(void)
Setup loopback interface.
bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const
Determine whether address and interface corresponding to received packet can be accepted for local de...
VirtualIpv4L3Protocol & operator=(const VirtualIpv4L3Protocol &)
Copy constructor.
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...
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(* TxRxTracedCallback)(Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet transmission or reception events.
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
Remove the address at addressIndex on named interface.
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:58
virtual bool GetIpForward(void) const
Get the IP forwarding state.
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
Ipv4Header BuildHeader(Ipv4Address source, Ipv4Address destination, uint8_t protocol, uint16_t payloadSize, uint8_t ttl, uint8_t tos, bool mayFragment)
Construct an IPv4 header.
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...
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_txTrace
Trace of transmitted packets.
uint16_t GetMetric(uint32_t i) const
std::list< Ptr< Ipv4RawSocketImpl > > SocketList
Container of the IPv4 Raw Sockets.
virtual void NotifyNewAggregate()
This function will notify other components connected to the node that a new stack member is now conne...
virtual void DoDispose(void)
Destructor implementation.