A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv4-l3-protocol.h
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:
80  static TypeId GetTypeId (void);
81  static const uint16_t PROT_NUMBER;
82 
84  virtual ~Ipv4L3Protocol ();
85 
90  enum DropReason
91  {
98  };
99 
100  void SetNode (Ptr<Node> node);
101 
102  // functions defined in base class Ipv4
103 
104  void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
106 
108  void DeleteRawSocket (Ptr<Socket> socket);
109 
120  void Insert (Ptr<IpL4Protocol> protocol);
129  Ptr<IpL4Protocol> GetProtocol (int protocolNumber) const;
136  void Remove (Ptr<IpL4Protocol> protocol);
137 
144  void SetDefaultTtl (uint8_t ttl);
145 
159  void Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
160  const Address &to, NetDevice::PacketType packetType);
161 
172  void Send (Ptr<Packet> packet, Ipv4Address source,
173  Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
182  void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route);
183 
184  uint32_t AddInterface (Ptr<NetDevice> device);
185  Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
186  uint32_t GetNInterfaces (void) const;
187 
188  int32_t GetInterfaceForAddress (Ipv4Address addr) const;
189  int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
190  int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
191  bool IsDestinationAddress (Ipv4Address address, uint32_t iif) const;
192 
193  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
194  Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
195  uint32_t GetNAddresses (uint32_t interface) const;
196  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
198  Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope);
199 
200 
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);
209 
210  Ptr<NetDevice> GetNetDevice (uint32_t i);
211 
212 protected:
213 
214  virtual void DoDispose (void);
219  virtual void NotifyNewAggregate ();
220 private:
221  friend class Ipv4L3ProtocolTestCase;
223  Ipv4L3Protocol &operator = (const Ipv4L3Protocol &);
224 
225  // class Ipv4 attributes
226  virtual void SetIpForward (bool forward);
227  virtual bool GetIpForward (void) const;
228  virtual void SetWeakEsModel (bool model);
229  virtual bool GetWeakEsModel (void) const;
230 
231  Ipv4Header BuildHeader (
232  Ipv4Address source,
233  Ipv4Address destination,
234  uint8_t protocol,
235  uint16_t payloadSize,
236  uint8_t ttl,
237  uint8_t tos,
238  bool mayFragment);
239 
240  void
241  SendRealOut (Ptr<Ipv4Route> route,
242  Ptr<Packet> packet,
243  Ipv4Header const &ipHeader);
244 
245  void
246  IpForward (Ptr<Ipv4Route> rtentry,
248  const Ipv4Header &header);
249 
250  void
251  IpMulticastForward (Ptr<Ipv4MulticastRoute> mrtentry,
253  const Ipv4Header &header);
254 
255  void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
256  void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
257 
258  uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
259  void SetupLoopback (void);
260 
265  Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
266  bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
267 
274  void DoFragmentation (Ptr<Packet> packet, uint32_t outIfaceMtu, std::list<Ptr<Packet> >& listFragments);
275 
283  bool ProcessFragment (Ptr<Packet>& packet, Ipv4Header & ipHeader, uint32_t iif);
284 
291  void HandleFragmentsTimeout ( std::pair<uint64_t, uint32_t> key, Ipv4Header & ipHeader, uint32_t iif);
292 
293  typedef std::vector<Ptr<Ipv4Interface> > Ipv4InterfaceList;
294  typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
295  typedef std::list<Ptr<IpL4Protocol> > L4List_t;
296 
297  bool m_ipForward;
298  bool m_weakEsModel;
299  L4List_t m_protocols;
300  Ipv4InterfaceList m_interfaces;
301  uint8_t m_defaultTos;
302  uint8_t m_defaultTtl;
303  uint16_t m_identification;
304  Ptr<Node> m_node;
305 
306  TracedCallback<const Ipv4Header &, Ptr<const Packet>, uint32_t> m_sendOutgoingTrace;
307  TracedCallback<const Ipv4Header &, Ptr<const Packet>, uint32_t> m_unicastForwardTrace;
308  TracedCallback<const Ipv4Header &, Ptr<const Packet>, uint32_t> m_localDeliverTrace;
309 
310  // The following two traces pass a packet with an IP header
311  TracedCallback<Ptr<const Packet>, Ptr<Ipv4>, uint32_t> m_txTrace;
312  TracedCallback<Ptr<const Packet>, Ptr<Ipv4>, uint32_t> m_rxTrace;
313  // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
315 
316  Ptr<Ipv4RoutingProtocol> m_routingProtocol;
317 
318  SocketList m_sockets;
319 
324  class Fragments : public SimpleRefCount<Fragments>
325  {
326 public:
330  Fragments ();
331 
335  ~Fragments ();
336 
343  void AddFragment (Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
344 
349  bool IsEntire () const;
350 
355  Ptr<Packet> GetPacket () const;
356 
361  Ptr<Packet> GetPartialPacket () const;
362 
363 private:
368 
372  std::list<std::pair<Ptr<Packet>, uint16_t> > m_fragments;
373 
374  };
375 
376  typedef std::map< std::pair<uint64_t, uint32_t>, Ptr<Fragments> > MapFragments_t;
377  typedef std::map< std::pair<uint64_t, uint32_t>, EventId > MapFragmentsTimers_t;
378 
382  MapFragments_t m_fragments;
383  Time m_fragmentExpirationTimeout;
384  MapFragmentsTimers_t m_fragmentsTimers;
385 
386 };
387 
388 } // Namespace ns3
389 
390 #endif /* IPV4_L3_PROTOCOL_H */
void SetDefaultTtl(uint8_t ttl)
keep track of time unit.
Definition: nstime.h:149
Ptr< Packet > GetPacket() const
Get the entire packet.
virtual void NotifyNewAggregate()
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::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)
void SetUp(uint32_t i)
a polymophic address class
Definition: address.h:86
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
DropReason
Reason why a packet has been dropped.
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.
Packet header for IPv4.
Definition: ipv4-header.h:31
Ptr< Icmpv4L4Protocol > GetIcmp(void) const
Get ICMPv4 protocol.
bool IsEntire() const
If all fragments have been added.
void SetMetric(uint32_t i, uint16_t metric)
virtual void DoDispose(void)
Ptr< Socket > CreateRawSocket(void)
Creates a raw socket.
void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)
bool IsUp(uint32_t i) const
MapFragments_t m_fragments
The hash of fragmented packets.
void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)
A Set of Fragment belonging to the same packet (src, dst, identification and proto) ...
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
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
void SetForwarding(uint32_t i, bool val)
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...
void DoFragmentation(Ptr< Packet > packet, uint32_t outIfaceMtu, std::list< Ptr< Packet > > &listFragments)
Fragment a packet.
Ptr< NetDevice > GetNetDevice(uint32_t i)
bool ProcessFragment(Ptr< Packet > &packet, Ipv4Header &ipHeader, uint32_t iif)
Process a packet fragment.
void Remove(Ptr< IpL4Protocol > protocol)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
a class to store IPv4 address information on an interface
an identifier for simulation events.
Definition: event-id.h:46
void DeleteRawSocket(Ptr< Socket > socket)
Deletes a particular raw socket.
bool AddAddress(uint32_t i, Ipv4InterfaceAddress address)
void Insert(Ptr< IpL4Protocol > protocol)
uint32_t GetNAddresses(uint32_t interface) const
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)
uint32_t GetNInterfaces(void) const
Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
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:44
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.
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.