A Discrete-Event Network Simulator
API
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 
32 namespace ns3
33 {
34 
35 class Node;
36 class Packet;
37 class NetDevice;
38 class PacketSocketAddress;
39 
94 class PacketSocket : public Socket
95 {
96  public:
101  static TypeId GetTypeId();
102 
103  PacketSocket();
104  ~PacketSocket() override;
105 
110  void SetNode(Ptr<Node> node);
111 
112  SocketErrno GetErrno() const override;
113  SocketType GetSocketType() const override;
114  Ptr<Node> GetNode() const override;
122  int Bind() override;
130  int Bind6() override;
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:
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);
178  int DoBind(const PacketSocketAddress& address);
179 
185  uint32_t GetMinMtu(PacketSocketAddress ad) const;
186  void DoDispose() override;
187 
191  enum State
192  {
194  STATE_BOUND, // open and bound
195  STATE_CONNECTED, // open, bound and connected
197  };
198 
204  uint16_t m_protocol;
206  uint32_t m_device;
208 
209  std::queue<std::pair<Ptr<Packet>, Address>> m_deliveryQueue;
210  uint32_t m_rxAvailable;
211 
214 
215  // Socket options (attributes)
216  uint32_t m_rcvBufSize;
217 };
218 
223 class PacketSocketTag : public Tag
224 {
225  public:
229  PacketSocketTag();
244  void SetDestAddress(Address a);
249  Address GetDestAddress() const;
250 
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:
265 };
266 
271 class DeviceNameTag : public Tag
272 {
273  public:
277  DeviceNameTag();
282  void SetDeviceName(std::string n);
287  std::string GetDeviceName() const;
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;
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.
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
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.