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