22 #include "packet-socket.h"
23 #include "packet-socket-address.h"
26 #include "ns3/packet.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/trace-source-accessor.h"
36 NS_OBJECT_ENSURE_REGISTERED (PacketSocket);
39 PacketSocket::GetTypeId (
void)
41 static TypeId tid = TypeId (
"ns3::PacketSocket")
43 .AddConstructor<PacketSocket> ()
44 .AddTraceSource (
"Drop",
"Drop packet due to receive buffer overflow",
46 .AddAttribute (
"RcvBufSize",
47 "PacketSocket maximum receive buffer size (bytes)",
48 UintegerValue (131072),
49 MakeUintegerAccessor (&PacketSocket::m_rcvBufSize),
50 MakeUintegerChecker<uint32_t> ())
55 PacketSocket::PacketSocket () : m_rxAvailable (0)
59 m_shutdownSend =
false;
60 m_shutdownRecv =
false;
61 m_errno = ERROR_NOTERROR;
62 m_isSingleDevice =
false;
67 PacketSocket::SetNode (Ptr<Node> node)
73 PacketSocket::~PacketSocket ()
85 enum Socket::SocketErrno
92 enum Socket::SocketType
111 address.SetProtocol (0);
112 address.SetAllDevices ();
113 return DoBind (address);
129 m_errno = ERROR_INVAL;
140 if (m_state == STATE_BOUND ||
141 m_state == STATE_CONNECTED)
143 m_errno = ERROR_INVAL;
146 if (m_state == STATE_CLOSED)
148 m_errno = ERROR_BADF;
152 if (address.IsSingleDevice ())
154 dev = m_node->
GetDevice (address.GetSingleDevice ());
161 address.GetProtocol (), dev);
162 m_state = STATE_BOUND;
163 m_protocol = address.GetProtocol ();
164 m_isSingleDevice = address.IsSingleDevice ();
165 m_device = address.GetSingleDevice ();
166 m_boundnetdevice = dev;
174 if (m_state == STATE_CLOSED)
176 m_errno = ERROR_BADF;
179 m_shutdownSend =
true;
187 if (m_state == STATE_CLOSED)
189 m_errno = ERROR_BADF;
192 m_shutdownRecv =
true;
200 if (m_state == STATE_CLOSED)
202 m_errno = ERROR_BADF;
205 else if (m_state == STATE_BOUND || m_state == STATE_CONNECTED)
209 m_state = STATE_CLOSED;
210 m_shutdownSend =
true;
211 m_shutdownRecv =
true;
220 if (m_state == STATE_CLOSED)
222 m_errno = ERROR_BADF;
225 if (m_state == STATE_OPEN)
228 m_errno = ERROR_INVAL;
231 if (m_state == STATE_CONNECTED)
233 m_errno = ERROR_ISCONN;
238 m_errno = ERROR_AFNOSUPPORT;
242 m_state = STATE_CONNECTED;
243 NotifyConnectionSucceeded ();
246 NotifyConnectionFailed ();
253 m_errno = Socket::ERROR_OPNOTSUPP;
261 if (m_state == STATE_OPEN ||
262 m_state == STATE_BOUND)
264 m_errno = ERROR_NOTCONN;
267 return SendTo (p, flags, m_destAddr);
274 if (ad.IsSingleDevice ())
277 return device->GetMtu ();
281 uint32_t minMtu = 0xffff;
282 for (uint32_t i = 0; i < m_node->
GetNDevices (); i++)
284 Ptr<NetDevice> device = m_node->
GetDevice (i);
285 minMtu = std::min (minMtu, (uint32_t)device->GetMtu ());
295 if (m_state == STATE_CONNECTED)
298 return GetMinMtu (ad);
309 if (m_state == STATE_CLOSED)
312 m_errno = ERROR_BADF;
318 m_errno = ERROR_SHUTDOWN;
324 m_errno = ERROR_AFNOSUPPORT;
328 if (p->
GetSize () > GetMinMtu (ad))
330 m_errno = ERROR_MSGSIZE;
335 Address dest = ad.GetPhysicalAddress ();
336 if (ad.IsSingleDevice ())
339 if (!device->Send (p, dest, ad.GetProtocol ()))
347 for (uint32_t i = 0; i < m_node->
GetNDevices (); i++)
350 if (!device->Send (p, dest, ad.GetProtocol ()))
359 NotifyDataSent (p->
GetSize ());
366 m_errno = ERROR_INVAL;
377 uint16_t protocol,
const Address &from,
380 NS_LOG_FUNCTION (
this << device << packet << protocol << from << to << packetType);
385 PacketSocketAddress address;
386 address.SetPhysicalAddress (from);
387 address.SetSingleDevice (device->GetIfIndex ());
388 address.SetProtocol (protocol);
390 if ((m_rxAvailable + packet->
GetSize ()) <= m_rcvBufSize)
392 Ptr<Packet> copy = packet->
Copy ();
394 dnt.SetDeviceName (device->GetTypeId ().GetName ());
396 pst.SetPacketType (packetType);
397 pst.SetDestAddress (to);
398 SocketAddressTag tag;
399 tag.SetAddress (address);
401 copy->AddPacketTag (pst);
402 copy->AddPacketTag (dnt);
404 m_rxAvailable += packet->
GetSize ();
415 NS_LOG_WARN (
"No receive buffer space available. Drop.");
416 m_dropTrace (packet);
426 return m_rxAvailable;
441 m_rxAvailable -= p->
GetSize ();
461 fromAddress = tag.GetAddress ();
472 ad.SetProtocol (m_protocol);
473 if (m_isSingleDevice)
476 ad.SetPhysicalAddress (device->GetAddress ());
477 ad.SetSingleDevice (m_device);
481 ad.SetPhysicalAddress (
Address ());
542 PacketSocketTag::GetTypeId (
void)
546 .AddConstructor<PacketSocketTag> ()
575 os <<
"packetType=" << m_packetType;
589 if ( n.substr(0,5) ==
"ns3::" )
605 DeviceNameTag::GetTypeId (
void)
609 .AddConstructor<DeviceNameTag> ();
620 uint32_t s = 1 + m_deviceName.size();
626 const char *n = m_deviceName.c_str();
627 uint8_t l = (uint8_t) strlen (n);
632 i.
Write ( (uint8_t*) n , (uint32_t) l );
640 i.
Read ( (uint8_t* ) buf, (uint32_t) l);
641 m_deviceName = std::string (buf, l);
646 os <<
"DeviceName=" << m_deviceName;
void Write(const uint8_t *buffer, uint32_t size)
virtual enum SocketErrno GetErrno(void) const
#define NS_LOG_FUNCTION(parameters)
Address GetDestAddress(void) const
Ptr< Packet > Recv(void)
Read a single packet from the socket.
void AddPacketTag(const Tag &tag) const
uint64_t GetUid(void) const
std::queue< Ptr< Packet > > m_deliveryQueue
Default destination address.
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.
#define NS_ASSERT(condition)
static bool IsMatchingType(const Address &address)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void DoDispose(void)
uint32_t GetSize(void) const
virtual uint32_t GetSerializedSize(void) const
void SetPacketType(NetDevice::PacketType t)
virtual int Bind(void)
Allocate a local IPv4 endpoint for this socket.
virtual void Print(std::ostream &os) const
virtual int GetSockName(Address &address) const
void UnregisterProtocolHandler(ProtocolHandler handler)
TAG_BUFFER_INLINE uint8_t ReadU8(void)
a polymophic address class
virtual TypeId GetInstanceTypeId(void) const
virtual void Serialize(TagBuffer i) const
bool PeekPacketTag(Tag &tag) const
virtual bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
void SetDestAddress(Address a)
virtual int ShutdownRecv(void)
virtual void Deserialize(TagBuffer i)
virtual uint32_t GetSerializedSize(void) const
Ptr< NetDevice > GetDevice(uint32_t index) const
This class implements a tag that carries an address of a packet across the socket interface...
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
virtual Ptr< Node > GetNode(void) const
virtual int Listen(void)
Listen for incoming connections.
#define NS_LOG_LOGIC(msg)
void Deserialize(TagBuffer buffer)
uint32_t GetSerializedSize(void) const
uint32_t GetNDevices(void) const
Ptr< Packet > Copy(void) const
virtual uint32_t GetRxAvailable(void) const
tag a set of bytes in a packet
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
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.
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
virtual void Deserialize(TagBuffer i)
virtual int Close(void)
Close a socket.
#define PACKET_TAG_MAX_SIZE
Tag maximum size The maximum size (in bytes) of a Tag is stored in this constant. ...
void Serialize(TagBuffer buffer) const
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
NetDevice::PacketType GetPacketType(void) const
virtual void Print(std::ostream &os) const
static PacketSocketAddress ConvertFrom(const Address &address)
virtual TypeId GetInstanceTypeId(void) const
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
void Read(uint8_t *buffer, uint32_t size)
virtual int Bind6(void)
Allocate a local IPv6 endpoint for this socket.
std::string GetDeviceName(void) const
This class implements a tag that carries the dest address of a packet and the packet type...
virtual int ShutdownSend(void)
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
virtual enum SocketType GetSocketType(void) const
void SetDeviceName(std::string n)