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 namespace ns3 { 32 33 class Application; 34 class Packet; 35 class Address; 36 class Time; 37 38 39 /** 40 * \ingroup network 41 * 42 * \brief A network Node. 43 * 44 * This class holds together: 45 * - a list of NetDevice objects which represent the network interfaces 46 * of this node which are connected to other Node instances through 47 * Channel instances. 48 * - a list of Application objects which represent the userspace 49 * traffic generation applications which interact with the Node 50 * through the Socket API. 51 * - a node Id: a unique per-node identifier. 52 * - a system Id: a unique Id used for parallel simulations. 53 * 54 * Every Node created is added to the NodeList automatically. 55 */ 56 class Node : public Object 57 { 58 public: 59 /** 60 * \brief Get the type ID. 61 * \return the object TypeId 62 */ 63 static TypeId GetTypeId (void); 64 65 Node(); 66 /** 67 * \param systemId a unique integer used for parallel simulations. 68 */ 69 Node(uint32_t systemId); 70 71 virtual ~Node(); 72 73 /** 74 * \returns the unique id of this node. 75 * 76 * This unique id happens to be also the index of the Node into 77 * the NodeList. 78 */ 79 uint32_t GetId (void) const; 80 81 /** 82 * In the future, ns3 nodes may have clock that returned a local time 83 * different from the virtual time Simulator::Now(). 84 * This function is currently a placeholder to ease the development of this feature. 85 * For now, it is only an alias to Simulator::Now() 86 * 87 * \return The time as seen by this node 88 */ 89 Time GetLocalTime (void) const; 90 91 /** 92 * \returns the system id for parallel simulations associated 93 * to this node. 94 */ 95 uint32_t GetSystemId (void) const; 96 97 /** 98 * \brief Associate a NetDevice to this node. 99 * 100 * \param device NetDevice to associate to this node. 101 * \returns the index of the NetDevice into the Node's list of 102 * NetDevice. 103 */ 104 uint32_t AddDevice (Ptr<NetDevice> device); 105 /** 106 * \brief Retrieve the index-th NetDevice associated to this node. 107 * 108 * \param index the index of the requested NetDevice 109 * \returns the requested NetDevice. 110 */ 111 Ptr<NetDevice> GetDevice (uint32_t index) const; 112 /** 113 * \returns the number of NetDevice instances associated 114 * to this Node. 115 */ 116 uint32_t GetNDevices (void) const; 117 118 /** 119 * \brief Associate an Application to this Node. 120 * 121 * \param application Application to associate to this node. 122 * \returns the index of the Application within the Node's list 123 * of Application. 124 */ 125 uint32_t AddApplication (Ptr<Application> application); 126 /** 127 * \brief Retrieve the index-th Application associated to this node. 128 * 129 * \param index the index of the requested Application 130 * \returns the requested Application. 131 */ 132 Ptr<Application> GetApplication (uint32_t index) const; 133 134 /** 135 * \returns the number of Application instances associated to this Node. 136 */ 137 uint32_t GetNApplications (void) const; 138 139 /** 140 * A protocol handler 141 * 142 * \param device a pointer to the net device which received the packet 143 * \param packet the packet received 144 * \param protocol the 16 bit protocol number associated with this packet. 145 * This protocol number is expected to be the same protocol number 146 * given to the Send method by the user on the sender side. 147 * \param sender the address of the sender 148 * \param receiver the address of the receiver; Note: this value is 149 * only valid for promiscuous mode protocol 150 * handlers. Note: If the L2 protocol does not use L2 151 * addresses, the address reported here is the value of 152 * device->GetAddress(). 153 * \param packetType type of packet received 154 * (broadcast/multicast/unicast/otherhost); Note: 155 * this value is only valid for promiscuous mode 156 * protocol handlers. 157 */ 158 typedef Callback<void,Ptr<NetDevice>, Ptr<const Packet>,uint16_t,const Address &, 159 const Address &, NetDevice::PacketType> ProtocolHandler; 160 /** 161 * \param handler the handler to register 162 * \param protocolType the type of protocol this handler is 163 * interested in. This protocol type is a so-called 164 * EtherType, as registered here: 165 * http://standards.ieee.org/regauth/ethertype/eth.txt 166 * the value zero is interpreted as matching all 167 * protocols. 168 * \param device the device attached to this handler. If the 169 * value is zero, the handler is attached to all 170 * devices on this node. 171 * \param promiscuous whether to register a promiscuous mode handler 172 */ 173 void RegisterProtocolHandler (ProtocolHandler handler, 174 uint16_t protocolType, 175 Ptr<NetDevice> device, 176 bool promiscuous=false); 177 /** 178 * \param handler the handler to unregister 179 * 180 * After this call returns, the input handler will never 181 * be invoked anymore. 182 */ 183 void UnregisterProtocolHandler (ProtocolHandler handler); 184 185 /** 186 * A callback invoked whenever a device is added to a node. 187 */ 188 typedef Callback<void,Ptr<NetDevice> > DeviceAdditionListener; 189 /** 190 * \param listener the listener to add 191 * 192 * Add a new listener to the list of listeners for the device-added 193 * event. When a new listener is added, it is notified of the existence 194 * of all already-added devices to make discovery of devices easier. 195 */ 196 void RegisterDeviceAdditionListener (DeviceAdditionListener listener); 197 /** 198 * \param listener the listener to remove 199 * 200 * Remove an existing listener from the list of listeners for the 201 * device-added event. 202 */ 203 void UnregisterDeviceAdditionListener (DeviceAdditionListener listener); 204 205 206 207 /** 208 * \returns true if checksums are enabled, false otherwise. 209 */ 210 static bool ChecksumEnabled (void); 211 212 213 protected: 214 /** 215 * The dispose method. Subclasses must override this method 216 * and must chain up to it by calling Node::DoDispose at the 217 * end of their own DoDispose method. 218 */ 219 virtual void DoDispose (void); 220 virtual void DoInitialize (void); 221 private: 222 223 /** 224 * \brief Notifies all the DeviceAdditionListener about the new device added. 225 * \param device the added device to notify. 226 */ 227 void NotifyDeviceAdded (Ptr<NetDevice> device); 228 229 /** 230 * \brief Receive a packet from a device in non-promiscuous mode. 231 * \param device the device 232 * \param packet the packet 233 * \param protocol the protocol 234 * \param from the sender 235 * \returns true if the packet has been delivered to a protocol handler. 236 */ 237 bool NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, const Address &from); 238 /** 239 * \brief Receive a packet from a device in promiscuous mode. 240 * \param device the device 241 * \param packet the packet 242 * \param protocol the protocol 243 * \param from the sender 244 * \param to the destination 245 * \param packetType the packet type 246 * \returns true if the packet has been delivered to a protocol handler. 247 */ 248 bool PromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol, 249 const Address &from, const Address &to, NetDevice::PacketType packetType); 250 /** 251 * \brief Receive a packet from a device. 252 * \param device the device 253 * \param packet the packet 254 * \param protocol the protocol 255 * \param from the sender 256 * \param to the destination 257 * \param packetType the packet type 258 * \param promisc true if received in promiscuous mode 259 * \returns true if the packet has been delivered to a protocol handler. 260 */ 261 bool ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet>, uint16_t protocol, 262 const Address &from, const Address &to, NetDevice::PacketType packetType, bool promisc); 263 264 /** 265 * \brief Finish node's construction by setting the correct node ID. 266 */ 267 void Construct (void); 268 269 /** 270 * \brief Protocol handler entry. 271 * This structure is used to demultiplex all the protocols. 272 */ 273 struct ProtocolHandlerEntry { 274 ProtocolHandler handler; //!< the protocol handler 275 Ptr<NetDevice> device; //!< the NetDevice 276 uint16_t protocol; //!< the protocol number 277 bool promiscuous; //!< true if it is a promiscuous handler 278 }; 279 280 /// Typedef for protocol handlers container 281 typedef std::vector<struct Node::ProtocolHandlerEntry> ProtocolHandlerList; 282 /// Typedef for NetDevice addition listeners container 283 typedef std::vector<DeviceAdditionListener> DeviceAdditionListenerList; 284 285 uint32_t m_id; //!< Node id for this node 286 uint32_t m_sid; //!< System id for this node 287 std::vector<Ptr<NetDevice> > m_devices; //!< Devices associated to this node 288 std::vector<Ptr<Application> > m_applications; //!< Applications associated to this node 289 ProtocolHandlerList m_handlers; //!< Protocol handlers in the node 290 DeviceAdditionListenerList m_deviceAdditionListeners; //!< Device addition listeners in the node 291 }; 292 293 } // namespace ns3 294 295 #endif /* NODE_H */ 296