A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-l3-protocol.h
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
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: George F. Riley<riley@ece.gatech.edu>
19 //
20 
21 #ifndef IPV4_L3_PROTOCOL_H
22 #define IPV4_L3_PROTOCOL_H
23 
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"
37 
38 namespace ns3 {
39 
40 class Packet;
41 class NetDevice;
42 class Ipv4Interface;
43 class Ipv4Address;
44 class Ipv4Header;
45 class Ipv4RoutingTableEntry;
46 class Ipv4Route;
47 class Node;
48 class Socket;
49 class Ipv4RawSocketImpl;
50 class IpL4Protocol;
51 class Icmpv4L4Protocol;
52 
53 
77 class Ipv4L3Protocol : public Ipv4
78 {
79 public:
84  static TypeId GetTypeId (void);
85  static const uint16_t PROT_NUMBER;
86 
88  virtual ~Ipv4L3Protocol ();
89 
94  enum DropReason
95  {
102  };
103 
108  void SetNode (Ptr<Node> node);
109 
110  // functions defined in base class Ipv4
111 
112  void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
114 
116  void DeleteRawSocket (Ptr<Socket> socket);
117 
128  void Insert (Ptr<IpL4Protocol> protocol);
137  Ptr<IpL4Protocol> GetProtocol (int protocolNumber) const;
144  void Remove (Ptr<IpL4Protocol> protocol);
145 
152  void SetDefaultTtl (uint8_t ttl);
153 
167  void Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
168  const Address &to, NetDevice::PacketType packetType);
169 
180  void Send (Ptr<Packet> packet, Ipv4Address source,
181  Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
190  void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route);
191 
192  uint32_t AddInterface (Ptr<NetDevice> device);
198  Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
199  uint32_t GetNInterfaces (void) const;
200 
201  int32_t GetInterfaceForAddress (Ipv4Address addr) const;
202  int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
203  int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
204  bool IsDestinationAddress (Ipv4Address address, uint32_t iif) const;
205 
206  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
207  Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
208  uint32_t GetNAddresses (uint32_t interface) const;
209  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
210  bool RemoveAddress (uint32_t interface, Ipv4Address address);
213 
214 
215  void SetMetric (uint32_t i, uint16_t metric);
216  uint16_t GetMetric (uint32_t i) const;
217  uint16_t GetMtu (uint32_t i) const;
218  bool IsUp (uint32_t i) const;
219  void SetUp (uint32_t i);
220  void SetDown (uint32_t i);
221  bool IsForwarding (uint32_t i) const;
222  void SetForwarding (uint32_t i, bool val);
223 
224  Ptr<NetDevice> GetNetDevice (uint32_t i);
225 
226 protected:
227 
228  virtual void DoDispose (void);
233  virtual void NotifyNewAggregate ();
234 private:
236 
243 
251 
252  // class Ipv4 attributes
253  virtual void SetIpForward (bool forward);
254  virtual bool GetIpForward (void) const;
255  virtual void SetWeakEsModel (bool model);
256  virtual bool GetWeakEsModel (void) const;
257 
270  Ipv4Address source,
271  Ipv4Address destination,
272  uint8_t protocol,
273  uint16_t payloadSize,
274  uint8_t ttl,
275  uint8_t tos,
276  bool mayFragment);
277 
284  void
286  Ptr<Packet> packet,
287  Ipv4Header const &ipHeader);
288 
295  void
296  IpForward (Ptr<Ipv4Route> rtentry,
298  const Ipv4Header &header);
299 
306  void
309  const Ipv4Header &header);
310 
317  void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
318 
325  void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
326 
332  uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
333 
337  void SetupLoopback (void);
338 
343  Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
344 
351  bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
352 
359  void DoFragmentation (Ptr<Packet> packet, uint32_t outIfaceMtu, std::list<Ptr<Packet> >& listFragments);
360 
368  bool ProcessFragment (Ptr<Packet>& packet, Ipv4Header & ipHeader, uint32_t iif);
369 
376  void HandleFragmentsTimeout ( std::pair<uint64_t, uint32_t> key, Ipv4Header & ipHeader, uint32_t iif);
377 
381  typedef std::vector<Ptr<Ipv4Interface> > Ipv4InterfaceList;
385  typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
389  typedef std::list<Ptr<IpL4Protocol> > L4List_t;
390 
391  bool m_ipForward;
395  uint8_t m_defaultTos;
396  uint8_t m_defaultTtl;
397  uint16_t m_identification;
399 
406 
407  // The following two traces pass a packet with an IP header
412  // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
415 
417 
419 
424  class Fragments : public SimpleRefCount<Fragments>
425  {
426 public:
430  Fragments ();
431 
435  ~Fragments ();
436 
443  void AddFragment (Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
444 
449  bool IsEntire () const;
450 
455  Ptr<Packet> GetPacket () const;
456 
461  Ptr<Packet> GetPartialPacket () const;
462 
463 private:
468 
472  std::list<std::pair<Ptr<Packet>, uint16_t> > m_fragments;
473 
474  };
475 
477  typedef std::map< std::pair<uint64_t, uint32_t>, Ptr<Fragments> > MapFragments_t;
479  typedef std::map< std::pair<uint64_t, uint32_t>, EventId > MapFragmentsTimers_t;
480 
484 
485 };
486 
487 } // Namespace ns3
488 
489 #endif /* IPV4_L3_PROTOCOL_H */
bool m_weakEsModel
Weak ES model state.
void SetDefaultTtl(uint8_t ttl)
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
virtual void SetIpForward(bool forward)
Set or unset the IP forwarding state.
Ptr< Packet > GetPacket() const
Get the entire packet.
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...
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.
std::list< Ptr< IpL4Protocol > > L4List_t
Container of the IPv4 L4 instances.
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:210
forward calls to a chain of CallbackAn ns3::TracedCallback has almost exactly the same API as a norma...
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.
virtual bool GetWeakEsModel(void) const
Get the Weak Es Model status.
std::list< std::pair< Ptr< Packet >, uint16_t > > m_fragments
The current fragments.
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const
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...
uint32_t AddInterface(Ptr< NetDevice > device)
TracedCallback< const Ipv4Header &, Ptr< const Packet >, DropReason, Ptr< Ipv4 >, uint32_t > m_dropTrace
Trace of dropped packets.
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
Ptr< Ipv4RoutingProtocol > m_routingProtocol
Routing protocol associated with the stack.
void SetUp(uint32_t i)
uint8_t m_defaultTos
Default TOS.
L4List_t m_protocols
List of transport protocol.
a polymophic address class
Definition: address.h:86
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...
DropReason
Reason why a packet has been dropped.
Ipv4InterfaceList m_interfaces
List of IPv4 interfaces.
bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const
Determine whether address and interface corresponding to received packet can be accepted for local de...
Ptr< Packet > GetPartialPacket() const
Get the complete part of the packet.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_rxTrace
Trace of received packets.
Packet header for IPv4.
Definition: ipv4-header.h:31
void SetupLoopback(void)
Setup loopback interface.
Ptr< Icmpv4L4Protocol > GetIcmp(void) const
Get ICMPv4 protocol.
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.
bool IsEntire() const
If all fragments have been added.
void SetMetric(uint32_t i, uint16_t metric)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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.
bool IsUp(uint32_t i) const
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) ...
std::vector< Ptr< Ipv4Interface > > Ipv4InterfaceList
Container of the IPv4 Interfaces.
#define list
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
Remove the address at addressIndex on named interface.
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:75
bool m_ipForward
Forwarding packets (i.e.
void SetForwarding(uint32_t i, bool val)
std::list< Ptr< Ipv4RawSocketImpl > > SocketList
Container of the IPv4 Raw Sockets.
Implement the Ipv4 layer.
int32_t GetInterfaceForAddress(Ipv4Address addr) const
Return the interface number of the interface that has been assigned the specified IP address...
uint16_t m_identification
Identification.
void DoFragmentation(Ptr< Packet > packet, uint32_t outIfaceMtu, std::list< Ptr< Packet > > &listFragments)
Fragment a packet.
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.
Ptr< NetDevice > GetNetDevice(uint32_t i)
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.
void Remove(Ptr< IpL4Protocol > protocol)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
virtual void SetWeakEsModel(bool model)
Set or unset the Weak Es Model.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
a class to store IPv4 address information on an interface
an identifier for simulation events.
Definition: event-id.h:46
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)
Ptr< Ipv4Interface > GetInterface(uint32_t i) const
Get an interface.
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.
uint32_t GetNAddresses(uint32_t interface) const
MapFragmentsTimers_t m_fragmentsTimers
Expiration events.
uint16_t GetMtu(uint32_t i) const
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.
uint32_t GetNInterfaces(void) const
Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
Interface is down so can not send packet.
bool IsUnicast(Ipv4Address ad, Ipv4Mask interfaceMask) const
Check if an IPv4 address is unicast.
tuple address
Definition: first.py:37
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
Trace of unicast forwarded packets.
uint16_t GetMetric(uint32_t i) const
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:49
void SetDown(uint32_t i)
void HandleFragmentsTimeout(std::pair< uint64_t, uint32_t > key, Ipv4Header &ipHeader, uint32_t iif)
Process the timeout for packet fragments.
virtual bool GetIpForward(void) const
Get the IP forwarding state.
static const uint16_t PROT_NUMBER
Protocol number (0x0800)
bool m_moreFragment
True if other fragments will be sent.
bool IsForwarding(uint32_t i) const
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.