20 #define NS_LOG_APPEND_CONTEXT \ 21 if (m_node) { std::clog << Simulator::Now ().GetSeconds () << " [node " << m_node->GetId () << "] "; } 24 #include "ns3/inet-socket-address.h" 30 #include "ns3/simulation-singleton.h" 31 #include "ns3/simulator.h" 32 #include "ns3/packet.h" 33 #include "ns3/uinteger.h" 34 #include "ns3/trace-source-accessor.h" 39 #include <arpa/inet.h> 40 #include <netinet/in.h> 57 .SetGroupName (
"Internet")
58 .AddTraceSource (
"CongestionWindow",
59 "The TCP connection's congestion window",
61 "ns3::TracedValueCallback::Uint32")
62 .AddTraceSource (
"SlowStartThreshold",
63 "TCP slow start threshold (bytes)",
65 "ns3::TracedValueCallback::Uint32")
66 .AddTraceSource (
"State",
69 "ns3::TcpStatesTracedValueCallback")
80 m_peerAddress (
"0.0.0.0", 0),
81 m_errno (ERROR_NOTERROR),
82 m_shutdownSend (false),
83 m_shutdownRecv (false),
86 m_closeOnEmpty (false),
88 m_lastMeasuredRtt (
Seconds (0.0))
95 m_delAckMaxCount (sock.m_delAckMaxCount),
96 m_delAckTimeout (sock.m_delAckTimeout),
97 m_noDelay (sock.m_noDelay),
101 m_remoteAddress (sock.m_remoteAddress),
102 m_remotePort (sock.m_remotePort),
103 m_localAddress (sock.m_localAddress),
104 m_localPort (sock.m_localPort),
105 m_peerAddress (sock.m_peerAddress),
106 m_errno (sock.m_errno),
107 m_shutdownSend (sock.m_shutdownSend),
108 m_shutdownRecv (sock.m_shutdownRecv),
109 m_connected (sock.m_connected),
110 m_state (sock.m_state),
111 m_closeOnEmpty (sock.m_closeOnEmpty),
112 m_txBufferSize (sock.m_txBufferSize),
113 m_segmentSize (sock.m_segmentSize),
114 m_rxWindowSize (sock.m_rxWindowSize),
115 m_advertisedWindowSize (sock.m_advertisedWindowSize),
116 m_cWnd (sock.m_cWnd),
117 m_ssThresh (sock.m_ssThresh),
118 m_initialCWnd (sock.m_initialCWnd),
119 m_initialSsThresh (sock.m_initialSsThresh),
120 m_lastMeasuredRtt (
Seconds (0.0)),
121 m_cnTimeout (sock.m_cnTimeout),
122 m_synRetries (sock.m_synRetries),
125 m_sndBufSize (sock.m_sndBufSize)
231 NS_LOG_LOGIC (
"NscTcpSocketImpl: ERROR_AFNOSUPPORT - Bind6 not supported");
312 " deferring close, state " <<
m_state);
316 NS_LOG_LOGIC (
"NscTcp socket " <<
this <<
" calling disconnect(); moving to CLOSED");
340 std::ostringstream ss;
342 std::string ipstring = ss.str ();
395 return Send (p, flags);
460 if (m_deliveryQueue.empty () )
462 m_errno = ERROR_AGAIN;
465 Ptr<Packet> p = m_deliveryQueue.front ();
466 if (p->GetSize () <= maxSize)
468 m_deliveryQueue.pop ();
469 m_rxAvailable -= p->GetSize ();
473 m_errno = ERROR_AGAIN;
480 NscTcpSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags,
485 GetPeerName (fromAddress);
504 m_errno = ERROR_NOTCONN;
508 m_endPoint->GetPeerPort ());
513 NscTcpSocketImpl::GetRxAvailable (
void)
const 518 return m_rxAvailable;
528 void NscTcpSocketImpl::CompleteFork (
void)
535 struct sockaddr_in sin;
536 size_t sin_len =
sizeof(sin);
538 if (0 == m_nscTcpSocket->getpeername ((
struct sockaddr*) &sin, &sin_len)) {
539 m_remotePort = ntohs (sin.sin_port);
540 m_remoteAddress = Ipv4Address::Deserialize ((
const uint8_t*) &sin.sin_addr);
544 m_endPoint = m_tcp->Allocate ();
548 m_state = ESTABLISHED;
550 sin_len =
sizeof(sin);
552 if (0 == m_nscTcpSocket->getsockname ((
struct sockaddr *) &sin, &sin_len))
553 m_localAddress = Ipv4Address::Deserialize ((
const uint8_t*) &sin.sin_addr);
555 NS_LOG_LOGIC (
"NscTcpSocketImpl " <<
this <<
" accepted connection from " 556 << m_remoteAddress <<
":" << m_remotePort
557 <<
" to " << m_localAddress <<
":" << m_localPort);
562 NotifyNewConnectionCreated (
this, m_peerAddress);
565 void NscTcpSocketImpl::ConnectionSucceeded ()
569 struct sockaddr_in sin;
570 size_t sin_len =
sizeof(sin);
571 if (0 == m_nscTcpSocket->getsockname ((
struct sockaddr *) &sin, &sin_len)) {
572 m_localAddress = Ipv4Address::Deserialize ((
const uint8_t*)&sin.sin_addr);
573 m_localPort = ntohs (sin.sin_port);
576 NS_LOG_LOGIC (
"NscTcpSocketImpl " <<
this <<
" connected to " 577 << m_remoteAddress <<
":" << m_remotePort
578 <<
" from " << m_localAddress <<
":" << m_localPort);
579 NotifyConnectionSucceeded ();
583 bool NscTcpSocketImpl::Accept (
void)
585 if (m_state == CLOSED)
591 if (!m_nscTcpSocket->is_listening ())
596 int res = m_nscTcpSocket->
accept (&newsock);
609 newSock->m_nscTcpSocket = newsock;
612 Simulator::ScheduleNow (&NscTcpSocketImpl::CompleteFork, newSock);
616 bool NscTcpSocketImpl::ReadPendingData (
void)
618 if (m_state != ESTABLISHED)
623 uint8_t buffer[8192];
624 len =
sizeof(buffer);
625 m_errno = ERROR_NOTERROR;
626 err = m_nscTcpSocket->read_data (buffer, &len);
627 if (err == 0 && len == 0)
630 m_state = CLOSE_WAIT;
633 m_errno = GetNativeNs3Errno (err);
636 case ERROR_NOTERROR:
break;
637 case ERROR_AGAIN:
return false;
640 "during read_data, ns-3 errno set to" << m_errno);
647 m_deliveryQueue.push (p);
648 m_rxAvailable += p->
GetSize ();
654 bool NscTcpSocketImpl::SendPendingData (
void)
659 if (m_txBuffer.empty ())
665 size_t size, written = 0;
673 m_errno = ERROR_NOTERROR;
675 uint8_t *buf =
new uint8_t[size];
676 p->CopyData (buf, size);
677 ret = m_nscTcpSocket->send_data ((
const char *)buf, size);
686 NS_ASSERT (m_txBufferSize >= (
size_t)ret);
687 m_txBufferSize -= ret;
689 if ((
size_t)ret < size)
691 p->RemoveAtStart (ret);
697 if (m_txBuffer.empty ())
701 m_nscTcpSocket->disconnect ();
706 }
while ((
size_t) ret == size);
710 NS_LOG_DEBUG (
"Notifying data sent, remaining txbuffer size: " << m_txBufferSize);
711 Simulator::ScheduleNow (&NscTcpSocketImpl::NotifyDataSent,
this, ret);
716 NS_LOG_DEBUG (
"Not notifying data sent, return value " << ret);
723 return CopyObject<NscTcpSocketImpl> (
this);
727 NscTcpSocketImpl::SetSndBufSize (uint32_t size)
733 NscTcpSocketImpl::GetSndBufSize (
void)
const 739 NscTcpSocketImpl::SetRcvBufSize (uint32_t size)
745 NscTcpSocketImpl::GetRcvBufSize (
void)
const 751 NscTcpSocketImpl::SetSegSize (uint32_t size)
753 m_segmentSize = size;
757 NscTcpSocketImpl::GetSegSize (
void)
const 759 return m_segmentSize;
763 NscTcpSocketImpl::SetAdvWin (uint32_t
window)
765 m_advertisedWindowSize =
window;
769 NscTcpSocketImpl::GetAdvWin (
void)
const 771 return m_advertisedWindowSize;
775 NscTcpSocketImpl::SetInitialSSThresh (uint32_t threshold)
777 m_initialSsThresh = threshold;
781 NscTcpSocketImpl::GetInitialSSThresh (
void)
const 783 return m_initialSsThresh;
787 NscTcpSocketImpl::SetInitialCwnd (uint32_t cwnd)
789 m_initialCWnd = cwnd;
793 NscTcpSocketImpl::GetInitialCwnd (
void)
const 795 return m_initialCWnd;
805 NscTcpSocketImpl::GetConnTimeout (
void)
const 811 NscTcpSocketImpl::SetSynRetries (uint32_t count)
813 m_synRetries = count;
817 NscTcpSocketImpl::GetSynRetries (
void)
const 829 NscTcpSocketImpl::SetDataRetries (uint32_t retries)
832 m_dataRetries = retries;
836 NscTcpSocketImpl::GetDataRetries (
void)
const 839 return m_dataRetries;
843 NscTcpSocketImpl::GetDelAckTimeout (
void)
const 845 return m_delAckTimeout;
849 NscTcpSocketImpl::SetDelAckMaxCount (uint32_t count)
851 m_delAckMaxCount = count;
855 NscTcpSocketImpl::GetDelAckMaxCount (
void)
const 857 return m_delAckMaxCount;
861 NscTcpSocketImpl::SetTcpNoDelay (
bool noDelay)
867 NscTcpSocketImpl::GetTcpNoDelay (
void)
const 879 NscTcpSocketImpl::GetPersistTimeout (
void)
const 881 return m_persistTimeout;
885 NscTcpSocketImpl::GetNativeNs3Errno (
int error)
const 891 return ERROR_NOTERROR;
920 NscTcpSocketImpl::SetAllowBroadcast (
bool allowBroadcast)
930 NscTcpSocketImpl::GetAllowBroadcast ()
const
Ipv4EndPoint * m_endPoint
the IPv4 endpoint
Simulation virtual time values and global simulation resolution.
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual int ShutdownSend(void)
void Destroy(void)
Kill this socket by zeroing its attributes (IPv4)
(abstract) base class of all TcpSockets
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
bool ReadPendingData(void)
Read all the pending data.
uint32_t m_txBufferSize
transmission buffer size
Ipv4Address m_localAddress
local address
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
NscTcpSocketImpl()
Create an unbound tcp socket.
Ptr< Node > m_node
the associated node
void SetRxCallback(Callback< void, Ptr< Packet >, Ipv4Header, uint16_t, Ptr< Ipv4Interface > > callback)
Set the reception callback.
Ptr< Packet > Recv(void)
Read a single packet from the socket.
void SetDestroyCallback(Callback< void > callback)
Set the default destroy callback.
Ipv4Address m_remoteAddress
peer IP address
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
INetStreamSocket * m_nscTcpSocket
the real NSC TCP socket
TracedValue< TcpStates_t > m_state
state information
virtual int Bind6(void)
Allocate a local IPv6 endpoint for this socket.
uint32_t m_sndBufSize
buffer limit for the outgoing queue
void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
static TypeId GetTypeId(void)
Get the type ID.
nsc_errno
List of network stack errors that may happen in a simulation, and can be handled by the simulator in ...
bool m_shutdownSend
Send no longer allowed.
bool m_closeOnEmpty
true if socket will close when buffer is empty
virtual void connect(const char *, int)=0
Connect to a remote peer.
SocketErrno
Enumeration of the possible errors returned by a socket.
virtual enum SocketErrno GetErrno(void) const
Get last error number.
a polymophic address class
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound NetDevice, if any.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
uint32_t m_initialCWnd
Initial cWnd value.
void SetNode(Ptr< Node > node)
Set the associated node.
enum SocketErrno m_errno
last error number
virtual enum SocketType GetSocketType(void) const
uint32_t m_initialSsThresh
Initial Slow Start Threshold.
bool m_connected
Connection established.
Socket logic for the NSC TCP sockets.
bool Accept(void)
Accept an incoming connection.
virtual Ptr< Node > GetNode(void) const
Return the node this socket is associated with.
Ipv4Address GetLocalAddress(void)
Get the local address.
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
virtual int ShutdownRecv(void)
virtual bool is_connected()=0
Check the connection state.
virtual int Listen(void)
Listen for incoming connections.
virtual int Bind(void)
Allocate a local IPv4 endpoint for this socket.
uint16_t m_remotePort
peer port
virtual uint32_t GetTxAvailable(void) const
Returns the number of bytes which can be sent in a single call to Send.
Ptr< NscTcpL4Protocol > m_tcp
the associated TCP L4 protocol
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
uint16_t GetPort(void) const
TracedValue< uint32_t > m_cWnd
Congestion window.
void ForwardUp(Ptr< Packet > p, Ipv4Header header, uint16_t port, Ptr< Ipv4Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
Remote side has shutdown and is waiting for us to finish writing our data and to shutdown (we have to...
Sent a connection request, waiting for ack.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
Listening for a connection.
bool m_shutdownRecv
Receive no longer allowed.
uint32_t m_rxWindowSize
Receive window size.
void SetTcp(Ptr< NscTcpL4Protocol > tcp)
Set the associated TCP L4 protocol.
Ipv4 addresses are stored in host order in this class.
bool SendPendingData(void)
Send all the pending data.
int FinishBind(void)
Finish the binding process.
void ConnectionSucceeded()
Called when a connection is in Established state.
uint16_t GetLocalPort(void)
Get the local port.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
virtual int Close(void)
Close a socket.
virtual void listen(int)=0
Put the socket in Listening state on a port.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Time Seconds(double value)
Construct a Time in the indicated unit.
Struct interface to NSC Stream (i.e., TCP) Sockets.
virtual void disconnect()=0
Disconnect from a remote peer.
uint16_t m_localPort
local port
void Print(std::ostream &os) const
Print this address to the given output stream.
virtual int accept(INetStreamSocket **handler)=0
Accept an incoming connection.
std::queue< Ptr< Packet > > m_txBuffer
transmission buffer
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
uint32_t m_advertisedWindowSize
Window to advertise.
TracedValue< uint32_t > m_ssThresh
Slow Start Threshold.
SocketType
Enumeration of the possible socket types.
a unique identifier for an interface.
void NotifySend(uint32_t spaceAvailable)
Notify through the callback (if set) that some data have been sent.
uint32_t m_segmentSize
SegmentSize.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
static bool IsMatchingType(const Address &address)
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
Ipv4Address GetIpv4(void) const
virtual ~NscTcpSocketImpl()
void NSCWakeup(void)
Called by NscTcpSocketImpl::ForwardUp()