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