A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-socket.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Emmanuelle Laprise, INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>,
7 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
8 */
9#ifndef PACKET_SOCKET_H
10#define PACKET_SOCKET_H
11
12#include "ns3/callback.h"
13#include "ns3/net-device.h"
14#include "ns3/ptr.h"
15#include "ns3/socket.h"
16#include "ns3/traced-callback.h"
17
18#include <queue>
19#include <stdint.h>
20
21namespace ns3
22{
23
24class Node;
25class Packet;
26class NetDevice;
27class PacketSocketAddress;
28
29/**
30 * \ingroup socket
31 *
32 * \brief A PacketSocket is a link between an application and a net device.
33 *
34 * A PacketSocket can be used to connect an application to a net
35 * device. The application provides the buffers of data, the socket
36 * converts them to a raw packet and the net device then adds the
37 * protocol specific headers and trailers. This socket type
38 * is very similar to the linux and BSD "packet" sockets.
39 *
40 * Here is a summary of the semantics of this class:
41 * - Bind: Bind uses only the protocol and device fields of the
42 * PacketSocketAddress. If none are provided, Bind uses
43 * zero for both, which means that the socket is bound
44 * to all protocols on all devices on the node.
45 *
46 * - Connect: uses only the protocol, device and "physical address"
47 * field of the PacketSocketAddress. It is used to set the default
48 * destination address for outgoing packets.
49 *
50 * - Send: send the input packet to the underlying NetDevices
51 * with the default destination address. The socket must
52 * be bound and connected.
53 *
54 * - SendTo: uses the protocol, device, and "physical address"
55 * fields of the PacketSocketAddress. The device value is
56 * used to specialize the packet transmission to a single
57 * device, the protocol value specifies the protocol of this
58 * packet only and the "physical address" field is used to override the
59 * default destination address. The socket must be bound.
60 *
61 * - Recv: The address represents the address of the packer originator.
62 * The fields "physical address", device, and protocol are filled.
63 *
64 * - Accept: not allowed
65 *
66 * - Listen: returns -1 (OPNOTSUPP)
67 *
68 * Socket data that is read from this socket using the methods returning
69 * an ns3::Packet object (i.e., Recv (), RecvMsg (), RecvFrom ()) will
70 * return Packet objects with three PacketTag objects attached.
71 * Applications may wish to read the extra out-of-band data provided in
72 * these tags, and may safely remove the tags from the Packet.
73 *
74 * - PacketSocketTag: contains destination address (type PacketSocketAddress)
75 * and packet type of the received packet
76 *
77 * - DeviceNameTag: contains the TypeId string of the relevant NetDevice
78 *
79 * \see class PacketSocketTag
80 * \see class DeviceNameTag
81 */
82
83class PacketSocket : public Socket
84{
85 public:
86 /**
87 * \brief Get the type ID.
88 * \return the object TypeId
89 */
90 static TypeId GetTypeId();
91
93 ~PacketSocket() override;
94
95 /**
96 * \brief Set the associated node.
97 * \param node the node
98 */
99 void SetNode(Ptr<Node> node);
100
101 SocketErrno GetErrno() const override;
102 SocketType GetSocketType() const override;
103 Ptr<Node> GetNode() const override;
104 /**
105 * \brief Bind the socket to the NetDevice and register the protocol handler.
106 *
107 * \warning this will actually bind protocol "0".
108 *
109 * \returns 0 on success, -1 on failure.
110 */
111 int Bind() override;
112 /**
113 * \brief Bind the socket to the NetDevice and register the protocol handler.
114 *
115 * \warning this will actually bind protocol "0".
116 *
117 * \returns 0 on success, -1 on failure.
118 */
119 int Bind6() override;
120 /**
121 * \brief Bind the socket to the NetDevice and register the
122 * protocol handler specified in the address.
123 *
124 * \param address the packet socket address
125 * \returns 0 on success, -1 on failure.
126 */
127 int Bind(const Address& address) override;
128 int Close() override;
129 int ShutdownSend() override;
130 int ShutdownRecv() override;
131 int Connect(const Address& address) override;
132 int Listen() override;
133 uint32_t GetTxAvailable() const override;
134 int Send(Ptr<Packet> p, uint32_t flags) override;
135 int SendTo(Ptr<Packet> p, uint32_t flags, const Address& toAddress) override;
136 uint32_t GetRxAvailable() const override;
137 Ptr<Packet> Recv(uint32_t maxSize, uint32_t flags) override;
138 Ptr<Packet> RecvFrom(uint32_t maxSize, uint32_t flags, Address& fromAddress) override;
139 int GetSockName(Address& address) const override;
140 int GetPeerName(Address& address) const override;
141 bool SetAllowBroadcast(bool allowBroadcast) override;
142 bool GetAllowBroadcast() const override;
143
144 private:
145 /**
146 * \brief Called by the L3 protocol when it received a packet to pass on to TCP.
147 *
148 * \param device the incoming NetDevice
149 * \param packet the incoming packet
150 * \param protocol the protocol
151 * \param from sender address
152 * \param to destination address
153 * \param packetType packet type
154 */
155 void ForwardUp(Ptr<NetDevice> device,
156 Ptr<const Packet> packet,
157 uint16_t protocol,
158 const Address& from,
159 const Address& to,
160 NetDevice::PacketType packetType);
161 /**
162 * \brief Bind the socket to the NetDevice and register the
163 * protocol handler specified in the address.
164 * \param address the packet socket address
165 * \returns 0 on success, -1 on failure.
166 */
167 int DoBind(const PacketSocketAddress& address);
168
169 /**
170 * \brief Get the minimum MTU supported by the NetDevices bound to a specific address
171 * \param ad the socket address to check for
172 * \returns The minimum MTU
173 */
175 void DoDispose() override;
176
177 /**
178 * \brief States of the socket
179 */
180 enum State
181 {
183 STATE_BOUND, // open and bound
184 STATE_CONNECTED, // open, bound and connected
186 };
187
188 Ptr<Node> m_node; //!< the associated node
189 mutable SocketErrno m_errno; //!< Socket error code
190 bool m_shutdownSend; //!< Send no longer allowed
191 bool m_shutdownRecv; //!< Receive no longer allowed
192 State m_state; //!< Socket state
193 uint16_t m_protocol; //!< Socket protocol
194 bool m_isSingleDevice; //!< Is bound to a single netDevice
195 uint32_t m_device; //!< index of the bound NetDevice
196 Address m_destAddr; //!< Default destination address
197
198 std::queue<std::pair<Ptr<Packet>, Address>> m_deliveryQueue; //!< Rx queue
199 uint32_t m_rxAvailable; //!< Rx queue size [Bytes]
200
201 /// Traced callback: dropped packets
203
204 // Socket options (attributes)
205 uint32_t m_rcvBufSize; //!< Rx buffer size [Bytes]
206};
207
208/**
209 * \brief This class implements a tag that carries the dest address of a packet and the packet
210 * type.
211 */
212class PacketSocketTag : public Tag
213{
214 public:
215 /**
216 * Create an empty PacketSocketTag
217 */
219 /**
220 * Set the packet type
221 * @param t the packet type of the corresponding packet
222 */
224 /**
225 * Get the packet type
226 * @return the packet type of the corresponding packet
227 */
229 /**
230 * Set the destination address of the corresponding packet
231 * @param a the destination address of the corresponding packet
232 */
233 void SetDestAddress(Address a);
234 /**
235 * Get the destination address of the corresponding packet
236 * @return the destination address of the corresponding packet
237 */
238 Address GetDestAddress() const;
239
240 /**
241 * \brief Get the type ID.
242 * \return the object TypeId
243 */
244 static TypeId GetTypeId();
245 TypeId GetInstanceTypeId() const override;
246 uint32_t GetSerializedSize() const override;
247 void Serialize(TagBuffer i) const override;
248 void Deserialize(TagBuffer i) override;
249 void Print(std::ostream& os) const override;
250
251 private:
253 Address m_destAddr; //!< Destination address
254};
255
256/**
257 * \brief This class implements a tag that carries the ns3 device name from where a packet is
258 * coming.
259 */
260class DeviceNameTag : public Tag
261{
262 public:
263 /**
264 * Create an empty DeviceNameTag
265 */
267 /**
268 * Set the device name
269 * @param n the device name from where the corresponding packet is coming.
270 */
271 void SetDeviceName(std::string n);
272 /**
273 * Get the device name from where the corresponding packet is coming.
274 * @return the device name from where the corresponding packet is coming.
275 */
276 std::string GetDeviceName() const;
277 /**
278 * \brief Get the type ID.
279 * \return the object TypeId
280 */
281 static TypeId GetTypeId();
282 TypeId GetInstanceTypeId() const override;
283 uint32_t GetSerializedSize() const override;
284 void Serialize(TagBuffer i) const override;
285 void Deserialize(TagBuffer i) override;
286 void Print(std::ostream& os) const override;
287
288 private:
289 std::string m_deviceName; //!< Device name
290};
291
292} // namespace ns3
293
294#endif /* PACKET_SOCKET_H */
a polymophic address class
Definition address.h:90
This class implements a tag that carries the ns3 device name from where a packet is coming.
DeviceNameTag()
Create an empty DeviceNameTag.
void Serialize(TagBuffer i) const override
static TypeId GetTypeId()
Get the type ID.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
std::string m_deviceName
Device name.
void SetDeviceName(std::string n)
Set the device name.
void Print(std::ostream &os) const override
uint32_t GetSerializedSize() const override
std::string GetDeviceName() const
Get the device name from where the corresponding packet is coming.
void Deserialize(TagBuffer i) override
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:289
an address for a packet socket
A PacketSocket is a link between an application and a net device.
Ptr< Node > m_node
the associated node
int Bind6() override
Bind the socket to the NetDevice and register the protocol handler.
int Send(Ptr< Packet > p, uint32_t flags) override
Send data (or dummy data) to the remote host.
int Close() override
Close a socket.
Address m_destAddr
Default destination address.
int Connect(const Address &address) override
Initiate a connection to a remote host.
State
States of the socket.
uint32_t m_rxAvailable
Rx queue size [Bytes].
int GetSockName(Address &address) const override
Get socket address.
Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress) override
Read a single packet from the socket and retrieve the sender address.
int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress) override
Send data to a specified peer.
std::queue< std::pair< Ptr< Packet >, Address > > m_deliveryQueue
Rx queue.
int GetPeerName(Address &address) const override
Get the peer address of a connected socket.
Ptr< Node > GetNode() const override
Return the node this socket is associated with.
int DoBind(const PacketSocketAddress &address)
Bind the socket to the NetDevice and register the protocol handler specified in the address.
int ShutdownSend() override
static TypeId GetTypeId()
Get the type ID.
bool SetAllowBroadcast(bool allowBroadcast) override
Configure whether broadcast datagram transmissions are allowed.
uint32_t m_rcvBufSize
Rx buffer size [Bytes].
void ForwardUp(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Called by the L3 protocol when it received a packet to pass on to TCP.
bool m_isSingleDevice
Is bound to a single netDevice.
uint32_t GetMinMtu(PacketSocketAddress ad) const
Get the minimum MTU supported by the NetDevices bound to a specific address.
int Bind() override
Bind the socket to the NetDevice and register the protocol handler.
bool GetAllowBroadcast() const override
Query whether broadcast datagram transmissions are allowed.
int Listen() override
Listen for incoming connections.
uint16_t m_protocol
Socket protocol.
SocketErrno m_errno
Socket error code.
~PacketSocket() override
void SetNode(Ptr< Node > node)
Set the associated node.
uint32_t GetTxAvailable() const override
Returns the number of bytes which can be sent in a single call to Send.
SocketType GetSocketType() const override
State m_state
Socket state.
bool m_shutdownSend
Send no longer allowed.
uint32_t m_device
index of the bound NetDevice
int ShutdownRecv() override
SocketErrno GetErrno() const override
Get last error number.
TracedCallback< Ptr< const Packet > > m_dropTrace
Traced callback: dropped packets.
uint32_t GetRxAvailable() const override
Return number of bytes which can be returned from one or multiple calls to Recv.
bool m_shutdownRecv
Receive no longer allowed.
void DoDispose() override
Destructor implementation.
This class implements a tag that carries the dest address of a packet and the packet type.
Address GetDestAddress() const
Get the destination address of the corresponding packet.
Address m_destAddr
Destination address.
uint32_t GetSerializedSize() const override
NetDevice::PacketType GetPacketType() const
Get the packet type.
void Print(std::ostream &os) const override
NetDevice::PacketType m_packetType
Packet type.
static TypeId GetTypeId()
Get the type ID.
PacketSocketTag()
Create an empty PacketSocketTag.
void Serialize(TagBuffer i) const override
void SetPacketType(NetDevice::PacketType t)
Set the packet type.
void Deserialize(TagBuffer i) override
void SetDestAddress(Address a)
Set the destination address of the corresponding packet.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Smart pointer class similar to boost::intrusive_ptr.
A low-level Socket API based loosely on the BSD Socket API.
Definition socket.h:57
Ptr< Packet > Recv()
Read a single packet from the socket.
Definition socket.cc:163
SocketType
Enumeration of the possible socket types.
Definition socket.h:96
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition socket.h:73
read and write tag data
Definition tag-buffer.h:41
tag a set of bytes in a packet
Definition tag.h:28
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.