A Discrete-Event Network Simulator
API
packet-socket.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Emmanuelle Laprise, 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: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>,
19  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  */
21 #ifndef PACKET_SOCKET_H
22 #define PACKET_SOCKET_H
23 
24 #include <stdint.h>
25 #include <queue>
26 #include "ns3/callback.h"
27 #include "ns3/traced-callback.h"
28 #include "ns3/ptr.h"
29 #include "ns3/socket.h"
30 #include "ns3/net-device.h"
31 
32 namespace ns3 {
33 
34 class Node;
35 class Packet;
36 class NetDevice;
37 class PacketSocketAddress;
38 
93 class PacketSocket : public Socket
94 {
95 public:
100  static TypeId GetTypeId (void);
101 
102  PacketSocket ();
103  virtual ~PacketSocket ();
104 
109  void SetNode (Ptr<Node> node);
110 
111  virtual enum SocketErrno GetErrno (void) const;
112  virtual enum SocketType GetSocketType (void) const;
113  virtual Ptr<Node> GetNode (void) const;
121  virtual int Bind (void);
129  virtual int Bind6 (void);
137  virtual int Bind (const Address & address);
138  virtual int Close (void);
139  virtual int ShutdownSend (void);
140  virtual int ShutdownRecv (void);
141  virtual int Connect (const Address &address);
142  virtual int Listen (void);
143  virtual uint32_t GetTxAvailable (void) const;
144  virtual int Send (Ptr<Packet> p, uint32_t flags);
145  virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress);
146  virtual uint32_t GetRxAvailable (void) const;
147  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags);
148  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,
149  Address &fromAddress);
150  virtual int GetSockName (Address &address) const;
151  virtual int GetPeerName (Address &address) const;
152  virtual bool SetAllowBroadcast (bool allowBroadcast);
153  virtual bool GetAllowBroadcast () const;
154 
155 private:
166  void ForwardUp (Ptr<NetDevice> device, Ptr<const Packet> packet,
167  uint16_t protocol, const Address &from, const Address &to,
168  NetDevice::PacketType packetType);
175  int DoBind (const PacketSocketAddress &address);
176 
182  uint32_t GetMinMtu (PacketSocketAddress ad) const;
183  virtual void DoDispose (void);
184 
188  enum State {
190  STATE_BOUND, // open and bound
191  STATE_CONNECTED, // open, bound and connected
193  };
194 
196  mutable enum SocketErrno m_errno;
199  enum State m_state;
200  uint16_t m_protocol;
202  uint32_t m_device;
204 
205  std::queue<std::pair<Ptr<Packet>, Address> > m_deliveryQueue;
206  uint32_t m_rxAvailable;
207 
210 
211  // Socket options (attributes)
212  uint32_t m_rcvBufSize;
213 
214 };
215 
219 class PacketSocketTag : public Tag
220 {
221 public:
225  PacketSocketTag ();
240  void SetDestAddress(Address a);
245  Address GetDestAddress (void) const;
246 
251  static TypeId GetTypeId (void);
252  virtual TypeId GetInstanceTypeId (void) const;
253  virtual uint32_t GetSerializedSize (void) const;
254  virtual void Serialize (TagBuffer i) const;
255  virtual void Deserialize (TagBuffer i);
256  virtual void Print (std::ostream &os) const;
257 
258 private:
261 };
265 class DeviceNameTag : public Tag
266 {
267 public:
271  DeviceNameTag ();
276  void SetDeviceName (std::string n);
281  std::string GetDeviceName (void) const;
286  static TypeId GetTypeId (void);
287  virtual TypeId GetInstanceTypeId (void) const;
288  virtual uint32_t GetSerializedSize (void) const;
289  virtual void Serialize (TagBuffer i) const;
290  virtual void Deserialize (TagBuffer i);
291  virtual void Print (std::ostream &os) const;
292 
293 private:
294  std::string m_deviceName;
295 };
296 
297 } // namespace ns3
298 
299 #endif /* PACKET_SOCKET_H */
300 
301 
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::DeviceNameTag::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition: packet-socket.cc:644
ns3::PacketSocketTag::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-socket.cc:566
ns3::PacketSocket::m_errno
enum SocketErrno m_errno
Socket error code.
Definition: packet-socket.h:196
ns3::PacketSocket::GetMinMtu
uint32_t GetMinMtu(PacketSocketAddress ad) const
Get the minimum MTU supported by the NetDevices bound to a specific address.
Definition: packet-socket.cc:273
ns3::PacketSocket::Close
virtual int Close(void)
Close a socket.
Definition: packet-socket.cc:199
ns3::PacketSocketTag::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: packet-socket.cc:576
ns3::PacketSocket::RecvFrom
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)
Read a single packet from the socket and retrieve the sender address.
Definition: packet-socket.cc:451
ns3::PacketSocket::GetRxAvailable
virtual uint32_t GetRxAvailable(void) const
Return number of bytes which can be returned from one or multiple calls to Recv.
Definition: packet-socket.cc:432
ns3::PacketSocket::m_shutdownRecv
bool m_shutdownRecv
Receive no longer allowed.
Definition: packet-socket.h:198
ns3::PacketSocketTag::Serialize
virtual void Serialize(TagBuffer i) const
Definition: packet-socket.cc:586
ns3::Socket::SocketErrno
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
ns3::PacketSocket::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-socket.cc:39
ns3::PacketSocketTag::Deserialize
virtual void Deserialize(TagBuffer i)
Definition: packet-socket.cc:592
ns3::PacketSocket::Listen
virtual int Listen(void)
Listen for incoming connections.
Definition: packet-socket.cc:252
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Socket::Recv
Ptr< Packet > Recv(void)
Read a single packet from the socket.
Definition: socket.cc:175
ns3::PacketSocket::STATE_CONNECTED
@ STATE_CONNECTED
Definition: packet-socket.h:191
ns3::PacketSocket::ShutdownRecv
virtual int ShutdownRecv(void)
Definition: packet-socket.cc:186
ns3::PacketSocketTag::PacketSocketTag
PacketSocketTag()
Create an empty PacketSocketTag.
Definition: packet-socket.cc:535
ns3::PacketSocket::STATE_BOUND
@ STATE_BOUND
Definition: packet-socket.h:190
ns3::PacketSocket::~PacketSocket
virtual ~PacketSocket()
Definition: packet-socket.cc:75
ns3::PacketSocketTag::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition: packet-socket.cc:581
ns3::DeviceNameTag::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: packet-socket.cc:630
ns3::PacketSocketAddress
an address for a packet socket
Definition: packet-socket-address.h:39
ns3::PacketSocket::m_node
Ptr< Node > m_node
the associated node
Definition: packet-socket.h:195
ns3::PacketSocket::GetErrno
virtual enum SocketErrno GetErrno(void) const
Get last error number.
Definition: packet-socket.cc:88
ns3::DeviceNameTag::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: packet-socket.cc:639
ns3::PacketSocket::m_state
enum State m_state
Socket state.
Definition: packet-socket.h:199
ns3::Ptr< Node >
ns3::PacketSocket::GetNode
virtual Ptr< Node > GetNode(void) const
Return the node this socket is associated with.
Definition: packet-socket.cc:102
ns3::PacketSocket::PacketSocket
PacketSocket()
Definition: packet-socket.cc:57
ns3::DeviceNameTag::Print
virtual void Print(std::ostream &os) const
Definition: packet-socket.cc:668
ns3::PacketSocket::State
State
States of the socket.
Definition: packet-socket.h:188
ns3::PacketSocket::SetNode
void SetNode(Ptr< Node > node)
Set the associated node.
Definition: packet-socket.cc:69
ns3::PacketSocket::Connect
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
Definition: packet-socket.cc:218
ns3::PacketSocketTag::Print
virtual void Print(std::ostream &os) const
Definition: packet-socket.cc:598
ns3::Socket
A low-level Socket API based loosely on the BSD Socket API.
Definition: socket.h:67
ns3::Tag
tag a set of bytes in a packet
Definition: tag.h:37
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::PacketSocketTag::GetPacketType
NetDevice::PacketType GetPacketType(void) const
Get the packet type.
Definition: packet-socket.cc:546
ns3::PacketSocket::m_device
uint32_t m_device
index of the bound NetDevice
Definition: packet-socket.h:202
ns3::PacketSocket::DoBind
int DoBind(const PacketSocketAddress &address)
Bind the socket to the NetDevice and register the protocol handler specified in the address.
Definition: packet-socket.cc:139
ns3::PacketSocket::m_deliveryQueue
std::queue< std::pair< Ptr< Packet >, Address > > m_deliveryQueue
Rx queue.
Definition: packet-socket.h:205
ns3::PacketSocket::GetSockName
virtual int GetSockName(Address &address) const
Get socket address.
Definition: packet-socket.cc:475
ns3::Socket::SocketType
SocketType
Enumeration of the possible socket types.
Definition: socket.h:104
first.address
address
Definition: first.py:44
ns3::PacketSocket::m_rcvBufSize
uint32_t m_rcvBufSize
Rx buffer size [Bytes].
Definition: packet-socket.h:212
ns3::PacketSocket::m_rxAvailable
uint32_t m_rxAvailable
Rx queue size [Bytes].
Definition: packet-socket.h:206
ns3::PacketSocket::GetTxAvailable
virtual uint32_t GetTxAvailable(void) const
Returns the number of bytes which can be sent in a single call to Send.
Definition: packet-socket.cc:294
ns3::PacketSocket::SendTo
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
Definition: packet-socket.cc:307
ns3::PacketSocket::SetAllowBroadcast
virtual bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
Definition: packet-socket.cc:514
ns3::PacketSocket
A PacketSocket is a link between an application and a net device.
Definition: packet-socket.h:94
ns3::PacketSocket::GetSocketType
virtual enum SocketType GetSocketType(void) const
Definition: packet-socket.cc:95
ns3::PacketSocket::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: packet-socket.cc:81
ns3::DeviceNameTag::Serialize
virtual void Serialize(TagBuffer i) const
Definition: packet-socket.cc:650
ns3::PacketSocket::Bind
virtual int Bind(void)
Bind the socket to the NetDevice and register the protocol handler.
Definition: packet-socket.cc:109
ns3::DeviceNameTag::m_deviceName
std::string m_deviceName
Device name.
Definition: packet-socket.h:294
ns3::PacketSocketTag::m_packetType
NetDevice::PacketType m_packetType
Packet type.
Definition: packet-socket.h:259
ns3::PacketSocket::m_destAddr
Address m_destAddr
Default destination address.
Definition: packet-socket.h:203
ns3::DeviceNameTag::Deserialize
virtual void Deserialize(TagBuffer i)
Definition: packet-socket.cc:659
ns3::PacketSocket::m_protocol
uint16_t m_protocol
Socket protocol.
Definition: packet-socket.h:200
ns3::PacketSocketTag::m_destAddr
Address m_destAddr
Destination address.
Definition: packet-socket.h:260
ns3::PacketSocket::Send
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
Definition: packet-socket.cc:260
ns3::PacketSocket::GetPeerName
virtual int GetPeerName(Address &address) const
Get the peer address of a connected socket.
Definition: packet-socket.cc:498
ns3::PacketSocket::ForwardUp
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.
Definition: packet-socket.cc:387
ns3::PacketSocket::ShutdownSend
virtual int ShutdownSend(void)
Definition: packet-socket.cc:173
ns3::PacketSocketTag::GetDestAddress
Address GetDestAddress(void) const
Get the destination address of the corresponding packet.
Definition: packet-socket.cc:558
ns3::PacketSocketTag::SetDestAddress
void SetDestAddress(Address a)
Set the destination address of the corresponding packet.
Definition: packet-socket.cc:552
ns3::PacketSocketTag
This class implements a tag that carries the dest address of a packet and the packet type.
Definition: packet-socket.h:220
ns3::DeviceNameTag::DeviceNameTag
DeviceNameTag()
Create an empty DeviceNameTag.
Definition: packet-socket.cc:607
ns3::NetDevice::PacketType
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition: traced-callback.h:53
ns3::PacketSocket::Bind6
virtual int Bind6(void)
Bind the socket to the NetDevice and register the protocol handler.
Definition: packet-socket.cc:119
ns3::PacketSocket::GetAllowBroadcast
virtual bool GetAllowBroadcast() const
Query whether broadcast datagram transmissions are allowed.
Definition: packet-socket.cc:525
ns3::PacketSocketTag::SetPacketType
void SetPacketType(NetDevice::PacketType t)
Set the packet type.
Definition: packet-socket.cc:540
ns3::PacketSocket::m_isSingleDevice
bool m_isSingleDevice
Is bound to a single netDevice.
Definition: packet-socket.h:201
ns3::PacketSocket::m_dropTrace
TracedCallback< Ptr< const Packet > > m_dropTrace
Traced callback: dropped packets.
Definition: packet-socket.h:209
ns3::TagBuffer
read and write tag data
Definition: tag-buffer.h:52
ns3::PacketSocket::STATE_CLOSED
@ STATE_CLOSED
Definition: packet-socket.h:192
ns3::DeviceNameTag::SetDeviceName
void SetDeviceName(std::string n)
Set the device name.
Definition: packet-socket.cc:612
ns3::DeviceNameTag
This class implements a tag that carries the ns3 device name from where a packet is coming.
Definition: packet-socket.h:266
ns3::PacketSocket::STATE_OPEN
@ STATE_OPEN
Definition: packet-socket.h:189
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
ns3::DeviceNameTag::GetDeviceName
std::string GetDeviceName(void) const
Get the device name from where the corresponding packet is coming.
Definition: packet-socket.cc:622
ns3::PacketSocket::m_shutdownSend
bool m_shutdownSend
Send no longer allowed.
Definition: packet-socket.h:197