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 
78 class PacketSocket : public Socket
79 {
80 public:
85  static TypeId GetTypeId (void);
86 
87  PacketSocket ();
88  virtual ~PacketSocket ();
89 
94  void SetNode (Ptr<Node> node);
95 
96  virtual enum SocketErrno GetErrno (void) const;
97  virtual enum SocketType GetSocketType (void) const;
98  virtual Ptr<Node> GetNode (void) const;
106  virtual int Bind (void);
114  virtual int Bind6 (void);
122  virtual int Bind (const Address & address);
123  virtual int Close (void);
124  virtual int ShutdownSend (void);
125  virtual int ShutdownRecv (void);
126  virtual int Connect (const Address &address);
127  virtual int Listen (void);
128  virtual uint32_t GetTxAvailable (void) const;
129  virtual int Send (Ptr<Packet> p, uint32_t flags);
130  virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress);
131  virtual uint32_t GetRxAvailable (void) const;
132  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags);
133  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,
134  Address &fromAddress);
135  virtual int GetSockName (Address &address) const;
136  virtual int GetPeerName (Address &address) const;
137  virtual bool SetAllowBroadcast (bool allowBroadcast);
138  virtual bool GetAllowBroadcast () const;
139 
140 private:
151  void ForwardUp (Ptr<NetDevice> device, Ptr<const Packet> packet,
152  uint16_t protocol, const Address &from, const Address &to,
153  NetDevice::PacketType packetType);
160  int DoBind (const PacketSocketAddress &address);
161 
167  uint32_t GetMinMtu (PacketSocketAddress ad) const;
168  virtual void DoDispose (void);
169 
173  enum State {
175  STATE_BOUND, // open and bound
176  STATE_CONNECTED, // open, bound and connected
178  };
179 
181  mutable enum SocketErrno m_errno;
184  enum State m_state;
185  uint16_t m_protocol;
187  uint32_t m_device;
189 
190  std::queue<Ptr<Packet> > m_deliveryQueue;
191  uint32_t m_rxAvailable;
192 
195 
196  // Socket options (attributes)
197  uint32_t m_rcvBufSize;
198 
199 };
200 
204 class PacketSocketTag : public Tag
205 {
206 public:
210  PacketSocketTag ();
225  void SetDestAddress(Address a);
230  Address GetDestAddress (void) const;
231 
236  static TypeId GetTypeId (void);
237  virtual TypeId GetInstanceTypeId (void) const;
238  virtual uint32_t GetSerializedSize (void) const;
239  virtual void Serialize (TagBuffer i) const;
240  virtual void Deserialize (TagBuffer i);
241  virtual void Print (std::ostream &os) const;
242 
243 private:
246 };
250 class DeviceNameTag : public Tag
251 {
252 public:
256  DeviceNameTag ();
261  void SetDeviceName (std::string n);
266  std::string GetDeviceName (void) const;
271  static TypeId GetTypeId (void);
272  virtual TypeId GetInstanceTypeId (void) const;
273  virtual uint32_t GetSerializedSize (void) const;
274  virtual void Serialize (TagBuffer i) const;
275  virtual void Deserialize (TagBuffer i);
276  virtual void Print (std::ostream &os) const;
277 
278 private:
279  std::string m_deviceName;
280 };
281 
282 } // namespace ns3
283 
284 #endif /* PACKET_SOCKET_H */
285 
286 
static TypeId GetTypeId(void)
Get the type ID.
virtual enum SocketErrno GetErrno(void) const
Get last error number.
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
std::string m_deviceName
Device name.
Address GetDestAddress(void) const
Get the destination address of the corresponding packet.
Ptr< Packet > Recv(void)
Read a single packet from the socket.
Definition: socket.cc:175
NetDevice::PacketType m_packetType
Packet type.
Address m_destAddr
Default destination address.
Forward calls to a chain of Callback.
std::queue< Ptr< Packet > > m_deliveryQueue
Rx queue.
an address for a packet socket
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)
Read a single packet from the socket and retrieve the sender address.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:538
virtual void DoDispose(void)
Destructor implementation.
A PacketSocket is a link between an application and a net device.
Definition: packet-socket.h:78
virtual uint32_t GetSerializedSize(void) const
SocketType
Enumeration of the possible socket types.
Definition: socket.h:104
void SetPacketType(NetDevice::PacketType t)
Set the packet type.
virtual int Bind(void)
Bind the socket to the NetDevice and register the protocol handler.
virtual void Print(std::ostream &os) const
uint32_t m_rxAvailable
Rx queue size [Bytes].
virtual int GetSockName(Address &address) const
Get socket address.
a polymophic address class
Definition: address.h:90
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Ptr< Node > m_node
the associated node
virtual void Serialize(TagBuffer i) const
virtual int GetPeerName(Address &address) const
Get the peer address of a connected socket.
A low-level Socket API based loosely on the BSD Socket API.
Definition: socket.h:66
virtual bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
void SetDestAddress(Address a)
Set the destination address of the corresponding packet.
virtual int ShutdownRecv(void)
virtual void Deserialize(TagBuffer i)
enum State m_state
Socket state.
virtual uint32_t GetSerializedSize(void) const
static TypeId GetTypeId(void)
Get the type ID.
PacketSocketTag()
Create an empty PacketSocketTag.
bool m_isSingleDevice
Is bound to a single netDevice.
virtual Ptr< Node > GetNode(void) const
Return the node this socket is associated with.
virtual int Listen(void)
Listen for incoming connections.
State
States of the socket.
uint32_t m_device
index of the bound NetDevice
virtual uint32_t GetRxAvailable(void) const
Return number of bytes which can be returned from one or multiple calls to Recv.
tag a set of bytes in a packet
Definition: tag.h:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
uint32_t m_rcvBufSize
Rx buffer size [Bytes].
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
bool m_shutdownSend
Send no longer allowed.
This class implements a tag that carries the ns3 device name from where a packet is coming...
virtual bool GetAllowBroadcast() const
Query whether broadcast datagram transmissions are allowed.
enum SocketErrno m_errno
Socket error code.
virtual void Deserialize(TagBuffer i)
virtual int Close(void)
Close a socket.
uint16_t m_protocol
Socket protocol.
Address m_destAddr
Destination address.
bool m_shutdownRecv
Receive no longer allowed.
void SetNode(Ptr< Node > node)
Set the associated node.
static TypeId GetTypeId(void)
Get the type ID.
read and write tag data
Definition: tag-buffer.h:51
int DoBind(const PacketSocketAddress &address)
Bind the socket to the NetDevice and register the protocol handler specified in the address...
virtual uint32_t GetTxAvailable(void) const
Returns the number of bytes which can be sent in a single call to Send.
virtual void Serialize(TagBuffer i) const
virtual ~PacketSocket()
NetDevice::PacketType GetPacketType(void) const
Get the packet type.
virtual void Print(std::ostream &os) const
DeviceNameTag()
Create an empty DeviceNameTag.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
uint32_t GetMinMtu(PacketSocketAddress ad) const
Get the minimum MTU supported by the NetDevices bound to a specific address.
virtual int Bind6(void)
Bind the socket to the NetDevice and register the protocol handler.
std::string GetDeviceName(void) const
Get the device name from where the corresponding packet is coming.
tuple address
Definition: first.py:37
This class implements a tag that carries the dest address of a packet and the packet type...
TracedCallback< Ptr< const Packet > > m_dropTrace
Traced callback: dropped packets.
virtual int ShutdownSend(void)
a unique identifier for an interface.
Definition: type-id.h:58
virtual enum SocketType GetSocketType(void) const
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.
void SetDeviceName(std::string n)
Set the device name.