src/internet-stack/ipv4-l3-protocol.h
author Qasim Javed <qasimj@gmail.com>
Thu Aug 06 01:55:49 2009 +0600 (2009-08-06)
changeset 4638 19aa5f9b4bdf
parent 4634 0417b4c836da
permissions -rw-r--r--
Source NAT working! Run (examples/netfilter-example.cc)
     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 <stdint.h>
    26 #include "ns3/ipv4-address.h"
    27 #include "ns3/ptr.h"
    28 #include "ns3/net-device.h"
    29 #include "ns3/ipv4.h"
    30 #include "ns3/traced-callback.h"
    31 #include "ns3/ipv4-header.h"
    32 #include "ns3/ipv4-routing-protocol.h"
    33 #include "ipv4-netfilter.h"
    34 
    35 namespace ns3 {
    36 
    37 class Packet;
    38 class NetDevice;
    39 class Ipv4Interface;
    40 class Ipv4Address;
    41 class Ipv4Header;
    42 class Ipv4RoutingTableEntry;
    43 class Ipv4Route;
    44 class Node;
    45 class Socket;
    46 class Ipv4RawSocketImpl;
    47 class Ipv4L4Protocol;
    48 class Ipv4L4Protocol;
    49 class Icmpv4L4Protocol;
    50 
    51 
    52 /**
    53  * \brief Implement the Ipv4 layer.
    54  * 
    55  * This is the actual implementation of IP.  It contains APIs to send and
    56  * receive packets at the IP layer, as well as APIs for IP routing.
    57  */
    58 class Ipv4L3Protocol : public Ipv4
    59 {
    60 public:
    61   static TypeId GetTypeId (void);
    62   static const uint16_t PROT_NUMBER;
    63 
    64   Ipv4L3Protocol();
    65   virtual ~Ipv4L3Protocol ();
    66 
    67   void SetNode (Ptr<Node> node);
    68 
    69   // functions defined in base class Ipv4
    70 
    71   void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
    72   Ptr<Ipv4RoutingProtocol> GetRoutingProtocol (void) const;
    73 
    74   Ptr<Socket> CreateRawSocket (void);
    75   void DeleteRawSocket (Ptr<Socket> socket);
    76 
    77   /**
    78    * \param protocol a template for the protocol to add to this L4 Demux.
    79    * \returns the L4Protocol effectively added.
    80    *
    81    * Invoke Copy on the input template to get a copy of the input
    82    * protocol which can be used on the Node on which this L4 Demux 
    83    * is running. The new L4Protocol is registered internally as
    84    * a working L4 Protocol and returned from this method.
    85    * The caller does not get ownership of the returned pointer.
    86    */
    87   void Insert(Ptr<Ipv4L4Protocol> protocol);
    88   /**
    89    * \param protocolNumber number of protocol to lookup
    90    *        in this L4 Demux
    91    * \returns a matching L4 Protocol
    92    *
    93    * This method is typically called by lower layers
    94    * to forward packets up the stack to the right protocol.
    95    * It is also called from NodeImpl::GetUdp for example.
    96    */
    97   Ptr<Ipv4L4Protocol> GetProtocol(int protocolNumber) const;
    98   /**
    99    * \param protocol protocol to remove from this demux.
   100    *
   101    * The input value to this method should be the value
   102    * returned from the Ipv4L4Protocol::Insert method.
   103    */
   104   void Remove (Ptr<Ipv4L4Protocol> protocol);
   105 
   106   /**
   107    * \param ttl default ttl to use
   108    *
   109    * When we need to send an ipv4 packet, we use this default
   110    * ttl value.
   111    */
   112   void SetDefaultTtl (uint8_t ttl);
   113 
   114   /**
   115    * Lower layer calls this method after calling L3Demux::Lookup
   116    * The ARP subclass needs to know from which NetDevice this
   117    * packet is coming to:
   118    *    - implement a per-NetDevice ARP cache
   119    *    - send back arp replies on the right device
   120    */
   121   void Receive( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
   122                 const Address &to, NetDevice::PacketType packetType);
   123 
   124   /**
   125    * \param packet packet to send
   126    * \param source source address of packet
   127    * \param destination address of packet
   128    * \param protocol number of packet
   129    * \param route route entry
   130    *
   131    * Higher-level layers call this method to send a packet
   132    * down the stack to the MAC and PHY layers.
   133    */
   134   void Send (Ptr<Packet> packet, Ipv4Address source, 
   135 	     Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
   136 
   137   uint32_t AddInterface (Ptr<NetDevice> device);
   138   Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
   139   uint32_t GetNInterfaces (void) const;
   140 
   141   int32_t GetInterfaceForAddress (Ipv4Address addr) const;
   142   int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
   143   int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
   144 
   145   bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
   146   Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
   147   uint32_t GetNAddresses (uint32_t interface) const;
   148   bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
   149 
   150   void SetMetric (uint32_t i, uint16_t metric);
   151   uint16_t GetMetric (uint32_t i) const;
   152   uint16_t GetMtu (uint32_t i) const;
   153   bool IsUp (uint32_t i) const;
   154   void SetUp (uint32_t i);
   155   void SetDown (uint32_t i);
   156   bool IsForwarding (uint32_t i) const;
   157   void SetForwarding (uint32_t i, bool val);
   158 
   159   Ptr<NetDevice> GetNetDevice (uint32_t i);
   160   Ipv4Netfilter* GetNetfilter();
   161 
   162 protected:
   163 
   164   virtual void DoDispose (void);
   165   /**
   166    * This function will notify other components connected to the node that a new stack member is now connected
   167    * This will be used to notify Layer 3 protocol of layer 4 protocol stack to connect them together.
   168    */
   169   virtual void NotifyNewAggregate ();
   170 private:
   171   friend class Ipv4L3ProtocolTest;
   172   Ipv4L3Protocol(const Ipv4L3Protocol &);
   173   Ipv4L3Protocol &operator = (const Ipv4L3Protocol &);
   174 
   175   virtual void SetIpForward (bool forward);
   176   virtual bool GetIpForward (void) const;
   177 
   178   Ipv4Header BuildHeader (
   179             Ipv4Address source,
   180             Ipv4Address destination,
   181             uint8_t protocol,
   182             uint16_t payloadSize,
   183             uint8_t ttl,
   184             bool mayFragment);
   185 
   186   void
   187   SendRealOut (Ptr<Ipv4Route> route,
   188                Ptr<Packet> packet,
   189                Ipv4Header const &ipHeader);
   190 
   191   void 
   192   IpForward (Ptr<Ipv4Route> rtentry, 
   193              Ptr<const Packet> p, 
   194              const Ipv4Header &header);
   195 
   196   void
   197   IpMulticastForward (Ptr<Ipv4MulticastRoute> mrtentry, 
   198                       Ptr<const Packet> p, 
   199                       const Ipv4Header &header);
   200 
   201   void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
   202   void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
   203 
   204   uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
   205   void SetupLoopback (void);
   206   Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
   207   bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
   208 
   209   typedef std::list<Ptr<Ipv4Interface> > Ipv4InterfaceList;
   210   typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
   211   typedef std::list<Ptr<Ipv4L4Protocol> > L4List_t;
   212 
   213   bool m_ipForward;
   214   L4List_t m_protocols;
   215   Ipv4InterfaceList m_interfaces;
   216   uint32_t m_nInterfaces;
   217   uint8_t m_defaultTtl;
   218   uint16_t m_identification;
   219   Ptr<Node> m_node;
   220   TracedCallback<Ptr<const Packet>, uint32_t> m_txTrace;
   221   TracedCallback<Ptr<const Packet>, uint32_t> m_rxTrace;
   222   TracedCallback<Ptr<const Packet> > m_dropTrace;
   223 
   224   Ptr<Ipv4RoutingProtocol> m_routingProtocol;
   225 
   226   SocketList m_sockets;
   227   Ipv4Netfilter netfilter;
   228 };
   229 
   230 } // Namespace ns3
   231 
   232 #endif /* IPV4_L3_PROTOCOL_H */