src/node/node.h
author Florian Westphal <fw@strlen.de>
Wed Jul 15 18:46:14 2009 +0200 (2009-07-15)
changeset 4685 ae536d9e0d6d
parent 4558 31e9053749bb
permissions -rw-r--r--
nsc: move nsc glue code from nsc-tcp-l4-protocol to node/nsc-glue.cc.

known problems:
- sim_interface.h is duplicated
- nsc-glue.cc adds hooks in node.cc, "hijacks" incoming packets
- nsc-glue exports NSCs INetStack (instead of wrapping it completely)
- nsc-tcp-l4-protocol and nsc-tcp-socket-impl make calls into nsc-glue
- nsc-tcp-socket-impl should really be "nsc-socket-core" (or something
like that)

needs fixing on nsc side:
- no support for multiple interfaces yet (also not yet supported
on nsc side)
- nsc initialisation still tied to IP (Adding an Interface and assigning the
IP address is a single step; it should be separate)

maybe there is more.

There is a NSC_NEXT define in nsc-glue.h, its main purpose is to flag
the places where the NSC API needs to be adapted to support multiple
interfaces in nsc.
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2006 Georgia Tech Research Corporation, INRIA
     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  * Authors: George F. Riley<riley@ece.gatech.edu>
    19  *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    20  */
    21 #ifndef NODE_H
    22 #define NODE_H
    23 
    24 #include <vector>
    25 
    26 #include "ns3/object.h"
    27 #include "ns3/callback.h"
    28 #include "ns3/ptr.h"
    29 #include "ns3/net-device.h"
    30 
    31 struct INetStack;
    32 
    33 namespace ns3 {
    34 
    35 class Application;
    36 class Packet;
    37 class Address;
    38 class NscGlue;
    39 
    40 /**
    41  * \ingroup node
    42  *
    43  * \brief A network Node.
    44  *
    45  * This class holds together:
    46  *   - a list of NetDevice objects which represent the network interfaces
    47  *     of this node which are connected to other Node instances through
    48  *     Channel instances.
    49  *   - a list of Application objects which represent the userspace
    50  *     traffic generation applications which interact with the Node
    51  *     through the Socket API.
    52  *   - a node Id: a unique per-node identifier.
    53  *   - a system Id: a unique Id used for parallel simulations.
    54  *
    55  * Every Node created is added to the NodeList automatically.
    56  */
    57 class Node : public Object
    58 {
    59 public:
    60   static TypeId GetTypeId (void);
    61 
    62   /**
    63    * Must be invoked by subclasses only.
    64    */
    65   Node();
    66   /**
    67    * \param systemId a unique integer used for parallel simulations.
    68    *
    69    * Must be invoked by subclasses only.
    70    */
    71   Node(uint32_t systemId);
    72 
    73   virtual ~Node();
    74 
    75   /**
    76    * \returns the unique id of this node.
    77    * 
    78    * This unique id happens to be also the index of the Node into
    79    * the NodeList. 
    80    */
    81   uint32_t GetId (void) const;
    82 
    83   /**
    84    * \returns the system id for parallel simulations associated
    85    *          to this node.
    86    */
    87   uint32_t GetSystemId (void) const;
    88 
    89   /**
    90    * \param device NetDevice to associate to this node.
    91    * \returns the index of the NetDevice into the Node's list of
    92    *          NetDevice.
    93    *
    94    * Associate this device to this node.
    95    */
    96   uint32_t AddDevice (Ptr<NetDevice> device);
    97   /**
    98    * \param index the index of the requested NetDevice
    99    * \returns the requested NetDevice associated to this Node.
   100    *
   101    * The indexes used by the GetDevice method start at one and
   102    * end at GetNDevices ()
   103    */
   104   Ptr<NetDevice> GetDevice (uint32_t index) const;
   105   /**
   106    * \returns the number of NetDevice instances associated
   107    *          to this Node.
   108    */
   109   uint32_t GetNDevices (void) const;
   110 
   111   /**
   112    * \param application Application to associate to this node.
   113    * \returns the index of the Application within the Node's list
   114    *          of Application.
   115    *
   116    * Associated this Application to this Node. This method is called
   117    * automatically from Application::Application so the user
   118    * has little reasons to call this method directly.
   119    */
   120   uint32_t AddApplication (Ptr<Application> application);
   121   /**
   122    * \param index
   123    * \returns the application associated to this requested index
   124    *          within this Node.
   125    */
   126   Ptr<Application> GetApplication (uint32_t index) const;
   127 
   128   /**
   129    * \returns the number of applications associated to this Node.
   130    */
   131   uint32_t GetNApplications (void) const;
   132 
   133   /**
   134    * A protocol handler
   135    *
   136    * \param device a pointer to the net device which received the packet
   137    * \param packet the packet received
   138    * \param protocol the 16 bit protocol number associated with this packet.
   139    *        This protocol number is expected to be the same protocol number
   140    *        given to the Send method by the user on the sender side.
   141    * \param sender the address of the sender
   142    * \param receiver the address of the receiver; Note: this value is
   143    *                 only valid for promiscuous mode protocol
   144    *                 handlers.
   145    * \param packetType type of packet received
   146    *                   (broadcast/multicast/unicast/otherhost); Note:
   147    *                   this value is only valid for promiscuous mode
   148    *                   protocol handlers.
   149    */
   150   typedef Callback<void,Ptr<NetDevice>, Ptr<const Packet>,uint16_t,const Address &,
   151                    const Address &, NetDevice::PacketType> ProtocolHandler;
   152   /**
   153    * \param handler the handler to register
   154    * \param protocolType the type of protocol this handler is 
   155    *        interested in. This protocol type is a so-called
   156    *        EtherType, as registered here:
   157    *        http://standards.ieee.org/regauth/ethertype/eth.txt
   158    *        the value zero is interpreted as matching all
   159    *        protocols.
   160    * \param device the device attached to this handler. If the
   161    *        value is zero, the handler is attached to all
   162    *        devices on this node.
   163    * \param promiscuous whether to register a promiscuous mode handler
   164    */
   165   void RegisterProtocolHandler (ProtocolHandler handler, 
   166                                 uint16_t protocolType,
   167                                 Ptr<NetDevice> device,
   168                                 bool promiscuous=false);
   169   /**
   170    * \param handler the handler to unregister
   171    *
   172    * After this call returns, the input handler will never
   173    * be invoked anymore.
   174    */
   175   void UnregisterProtocolHandler (ProtocolHandler handler);
   176 
   177   void SetNscLibrary (const std::string &soname);
   178   std::string GetNscLibrary (void) const;
   179   INetStack* GetNscInetStack(void);
   180   void RegisterNscWakeupCallback (Callback<void>);
   181   
   182   /**
   183    * \returns true if checksums are enabled, false otherwise.
   184    */
   185   static bool ChecksumEnabled (void);
   186 
   187 protected:
   188   /**
   189    * The dispose method. Subclasses must override this method
   190    * and must chain up to it by calling Node::DoDispose at the
   191    * end of their own DoDispose method.
   192    */
   193   virtual void DoDispose (void);
   194 private:
   195 
   196   /**
   197    * \param device the device added to this Node.
   198    *
   199    * This method is invoked whenever a user calls Node::AddDevice.
   200    */
   201   virtual void NotifyDeviceAdded (Ptr<NetDevice> device);
   202 
   203   bool NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet>, uint16_t protocol, const Address &from);
   204   bool PromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet>, uint16_t protocol,
   205                                  const Address &from, const Address &to, NetDevice::PacketType packetType);
   206   bool ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet>, uint16_t protocol,
   207                           const Address &from, const Address &to, NetDevice::PacketType packetType, bool promisc);
   208 
   209   void Construct (void);
   210 
   211   struct ProtocolHandlerEntry {
   212     ProtocolHandler handler;
   213     Ptr<NetDevice> device;
   214     uint16_t protocol;
   215     bool promiscuous;
   216   };
   217   typedef std::vector<struct Node::ProtocolHandlerEntry> ProtocolHandlerList;
   218   uint32_t    m_id;         // Node id for this node
   219   uint32_t    m_sid;        // System id for this node
   220   std::vector<Ptr<NetDevice> > m_devices;
   221   std::vector<Ptr<Application> > m_applications;
   222   ProtocolHandlerList m_handlers;
   223 
   224   Ptr <NscGlue> m_nscGlue;
   225   bool m_useNsc;
   226 };
   227 
   228 } //namespace ns3
   229 
   230 #endif /* NODE_H */