A Discrete-Event Network Simulator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 Georgia Tech Research Corporation
4  * Copyright (c) 2010 Adrian Sai-wah Tam
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
20  */
21 #ifndef TCP_SOCKET_BASE_H
22 #define TCP_SOCKET_BASE_H
24 #include <stdint.h>
25 #include <queue>
26 #include "ns3/callback.h"
27 #include "ns3/traced-value.h"
28 #include "ns3/tcp-socket.h"
29 #include "ns3/ptr.h"
30 #include "ns3/ipv4-address.h"
31 #include "ns3/ipv4-header.h"
32 #include "ns3/ipv4-interface.h"
33 #include "ns3/ipv6-header.h"
34 #include "ns3/ipv6-interface.h"
35 #include "ns3/event-id.h"
36 #include "tcp-tx-buffer.h"
37 #include "tcp-rx-buffer.h"
38 #include "rtt-estimator.h"
40 namespace ns3 {
42 class Ipv4EndPoint;
43 class Ipv6EndPoint;
44 class Node;
45 class Packet;
46 class TcpL4Protocol;
47 class TcpHeader;
62 class TcpSocketBase : public TcpSocket
63 {
64 public:
70  static TypeId GetTypeId (void);
74  TcpSocketBase (void);
81  TcpSocketBase (const TcpSocketBase& sock);
82  virtual ~TcpSocketBase (void);
84  // Set associated Node, TcpL4Protocol, RttEstimator to this socket
90  virtual void SetNode (Ptr<Node> node);
96  virtual void SetTcp (Ptr<TcpL4Protocol> tcp);
102  virtual void SetRtt (Ptr<RttEstimator> rtt);
104  // Necessary implementations of null functions from ns3::Socket
105  virtual enum SocketErrno GetErrno (void) const; // returns m_errno
106  virtual enum SocketType GetSocketType (void) const; // returns socket type
107  virtual Ptr<Node> GetNode (void) const; // returns m_node
108  virtual int Bind (void); // Bind a socket by setting up endpoint in TcpL4Protocol
109  virtual int Bind6 (void); // Bind a socket by setting up endpoint in TcpL4Protocol
110  virtual int Bind (const Address &address); // ... endpoint of specific addr or port
111  virtual int Connect (const Address &address); // Setup endpoint and call ProcessAction() to connect
112  virtual int Listen (void); // Verify the socket is in a correct state and call ProcessAction() to listen
113  virtual int Close (void); // Close by app: Kill socket upon tx buffer emptied
114  virtual int ShutdownSend (void); // Assert the m_shutdownSend flag to prevent send to network
115  virtual int ShutdownRecv (void); // Assert the m_shutdownRecv flag to prevent forward to app
116  virtual int Send (Ptr<Packet> p, uint32_t flags); // Call by app to send data to network
117  virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress); // Same as Send(), toAddress is insignificant
118  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags); // Return a packet to be forwarded to app
119  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, Address &fromAddress); // ... and write the remote address at fromAddress
120  virtual uint32_t GetTxAvailable (void) const; // Available Tx buffer size
121  virtual uint32_t GetRxAvailable (void) const; // Available-to-read data size, i.e. value of m_rxAvailable
122  virtual int GetSockName (Address &address) const; // Return local addr:port in address
123  virtual void BindToNetDevice (Ptr<NetDevice> netdevice); // NetDevice with my m_endPoint
125 protected:
126  // Implementing ns3::TcpSocket -- Attribute get/set
127  // inherited, no need to doc
129  virtual void SetSndBufSize (uint32_t size);
130  virtual uint32_t GetSndBufSize (void) const;
131  virtual void SetRcvBufSize (uint32_t size);
132  virtual uint32_t GetRcvBufSize (void) const;
133  virtual void SetSegSize (uint32_t size);
134  virtual uint32_t GetSegSize (void) const;
135  virtual void SetSSThresh (uint32_t threshold) = 0;
136  virtual uint32_t GetSSThresh (void) const = 0;
137  virtual void SetInitialCwnd (uint32_t cwnd) = 0;
138  virtual uint32_t GetInitialCwnd (void) const = 0;
139  virtual void SetConnTimeout (Time timeout);
140  virtual Time GetConnTimeout (void) const;
141  virtual void SetConnCount (uint32_t count);
142  virtual uint32_t GetConnCount (void) const;
143  virtual void SetDelAckTimeout (Time timeout);
144  virtual Time GetDelAckTimeout (void) const;
145  virtual void SetDelAckMaxCount (uint32_t count);
146  virtual uint32_t GetDelAckMaxCount (void) const;
147  virtual void SetTcpNoDelay (bool noDelay);
148  virtual bool GetTcpNoDelay (void) const;
149  virtual void SetPersistTimeout (Time timeout);
150  virtual Time GetPersistTimeout (void) const;
151  virtual bool SetAllowBroadcast (bool allowBroadcast);
152  virtual bool GetAllowBroadcast (void) const;
156  // Helper functions: Connection set up
163  int SetupCallback (void);
170  int DoConnect (void);
175  void ConnectionSucceeded (void);
182  int SetupEndpoint (void);
189  int SetupEndpoint6 (void);
203  void CompleteFork (Ptr<Packet> p, const TcpHeader& tcpHeader, const Address& fromAddress, const Address& toAddress);
207  // Helper functions: Transfer operation
217  void ForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface);
227  void ForwardUp6 (Ptr<Packet> packet, Ipv6Header header, uint16_t port, Ptr<Ipv6Interface> incomingInterface);
237  virtual void DoForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface); //Get a pkt from L3
246  virtual void DoForwardUp (Ptr<Packet> packet, Ipv6Header header, uint16_t port);
257  void ForwardIcmp (Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo);
268  void ForwardIcmp6 (Ipv6Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo);
278  bool SendPendingData (bool withAck = false);
289  uint32_t SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck);
296  void SendEmptyPacket (uint8_t flags);
301  void SendRST (void);
310  bool OutOfRange (SequenceNumber32 head, SequenceNumber32 tail) const;
313  // Helper functions: Connection close
320  int DoClose (void);
325  void CloseAndNotify (void);
333  void Destroy (void);
341  void Destroy6 (void);
346  void DeallocateEndPoint (void);
354  void PeerClose (Ptr<Packet> p, const TcpHeader& tcpHeader);
359  void DoPeerClose (void);
364  void CancelAllTimers (void);
369  void TimeWait (void);
371  // State transition functions
381  void ProcessEstablished (Ptr<Packet> packet, const TcpHeader& tcpHeader); // Received a packet upon ESTABLISHED state
391  void ProcessListen (Ptr<Packet> packet, const TcpHeader& tcpHeader,
392  const Address& fromAddress, const Address& toAddress);
400  void ProcessSynSent (Ptr<Packet> packet, const TcpHeader& tcpHeader);
410  void ProcessSynRcvd (Ptr<Packet> packet, const TcpHeader& tcpHeader,
411  const Address& fromAddress, const Address& toAddress);
419  void ProcessWait (Ptr<Packet> packet, const TcpHeader& tcpHeader);
427  void ProcessClosing (Ptr<Packet> packet, const TcpHeader& tcpHeader);
435  void ProcessLastAck (Ptr<Packet> packet, const TcpHeader& tcpHeader);
437  // Window management
443  virtual uint32_t UnAckDataCount (void);
449  virtual uint32_t BytesInFlight (void);
455  virtual uint32_t Window (void);
461  virtual uint32_t AvailableWindow (void);
467  virtual uint16_t AdvertisedWindowSize (void);
470  // Manage data tx/rx
476  virtual Ptr<TcpSocketBase> Fork (void) = 0;
483  virtual void ReceivedAck (Ptr<Packet> packet, const TcpHeader& tcpHeader);
490  virtual void ReceivedData (Ptr<Packet> packet, const TcpHeader& tcpHeader);
496  virtual void EstimateRtt (const TcpHeader& tcpHeader);
502  virtual void NewAck (SequenceNumber32 const& seq);
509  virtual void DupAck (const TcpHeader& tcpHeader, uint32_t count) = 0;
514  virtual void ReTxTimeout (void);
519  virtual void Retransmit (void);
524  virtual void DelAckTimeout (void);
529  virtual void LastAckTimeout (void);
534  virtual void PersistTimeout (void);
539  virtual void DoRetransmit (void);
545  virtual void ReadOptions (const TcpHeader& tcpHeader);
551  virtual void AddOptions (TcpHeader& tcpHeader);
554 protected:
555  // Counters and events
561  uint32_t m_dupAckCount;
562  uint32_t m_delAckCount;
563  uint32_t m_delAckMaxCount;
564  bool m_noDelay;
565  uint32_t m_cnCount;
566  uint32_t m_cnRetries;
573  // Connections to other layers of TCP/IP
583  // Rx and Tx buffer management
589  // State-related attributes
596  bool m_connected;
597  double m_msl;
599  // Window management
600  uint32_t m_segmentSize;
601  uint16_t m_maxWinSize;
603 };
605 } // namespace ns3
607 #endif /* TCP_SOCKET_BASE_H */
void PeerClose(Ptr< Packet > p, const TcpHeader &tcpHeader)
Received a FIN from peer, notify rx buffer.
void ProcessListen(Ptr< Packet > packet, const TcpHeader &tcpHeader, const Address &fromAddress, const Address &toAddress)
Received a packet upon LISTEN state.
virtual int Listen(void)
Listen for incoming connections.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
Doxygen introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
virtual int GetSockName(Address &address) const
Get socket address.
void ProcessWait(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received a packet upon CLOSE_WAIT, FIN_WAIT_1, FIN_WAIT_2.
virtual uint16_t AdvertisedWindowSize(void)
The amount of Rx window announced to the peer.
Callback template class.
Definition: callback.h:924
bool m_noDelay
Set to true to disable Nagle's algorithm.
virtual uint32_t GetSegSize(void) const
Get the segment size.
uint32_t SendDataPacket(SequenceNumber32 seq, uint32_t maxSize, bool withAck)
Extract at most maxSize bytes from the TxBuffer at sequence seq, add the TCP header, and send to TcpL4Protocol.
(abstract) base class of all TcpSockets
Definition: tcp-socket.h:64
Ipv4EndPoint * m_endPoint
the IPv4 endpoint
virtual ~TcpSocketBase(void)
TracedValue< Time > m_lastRtt
Last RTT sample collected.
virtual uint32_t GetInitialCwnd(void) const =0
Get the initial Congestion Window.
virtual int ShutdownRecv(void)
Create an unbound TCP socket.
EventId m_retxEvent
Retransmission event.
Ptr< Packet > Recv(void)
Read a single packet from the socket.
Definition: socket.cc:174
void ForwardUp6(Ptr< Packet > packet, Ipv6Header header, uint16_t port, Ptr< Ipv6Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
virtual void SetPersistTimeout(Time timeout)
Set the timout for persistent connection.
virtual int Send(Ptr< Packet > p, uint32_t flags)
Send data (or dummy data) to the remote host.
virtual bool GetAllowBroadcast(void) const
Query whether broadcast datagram transmissions are allowed.
EventId m_timewaitEvent
TIME_WAIT expiration event: Move this socket to CLOSED state.
static TypeId GetTypeId(void)
Get the type ID.
void SendRST(void)
Send reset and tear down this socket.
uint16_t m_maxWinSize
Maximum window size to advertise.
Callback< void, Ipv4Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback
ICMP callback.
void CancelAllTimers(void)
Cancel all timer when endpoint is deleted.
Time m_cnTimeout
Timeout for connection retry.
uint32_t m_segmentSize
Segment size.
EventId m_lastAckEvent
Last ACK timeout event.
void ConnectionSucceeded(void)
Schedule-friendly wrapper for Socket::NotifyConnectionSucceeded()
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
virtual void ReceivedAck(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received an ACK packet.
virtual void SetSSThresh(uint32_t threshold)=0
Set the Slow Start Threshold.
ns3::Time timeout
uint32_t m_cnCount
Count of remaining connection retries.
trace classes with value semantics
Definition: traced-value.h:55
virtual Time GetPersistTimeout(void) const
Get the timout for persistent connection.
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
void DoPeerClose(void)
FIN is in sequence, notify app and respond with a FIN.
virtual void PersistTimeout(void)
Send 1 byte probe to get an updated window size.
TracedValue< TcpStates_t > m_state
TCP state.
uint32_t m_delAckMaxCount
Number of packet to fire an ACK before delay timeout.
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
uint32_t m_cnRetries
Number of connection retries before giving up.
virtual uint32_t GetRcvBufSize(void) const
Get the receive buffer size.
uint32_t m_delAckCount
Delayed ACK counter.
virtual bool GetTcpNoDelay(void) const
Check if Nagle's algorithm is enabled or not.
void ForwardIcmp6(Ipv6Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Called by the L3 protocol when it received an ICMPv6 packet to pass on to TCP.
virtual int Bind(void)
Allocate a local IPv4 endpoint for this socket.
TcpTxBuffer m_txBuffer
Tx buffer.
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
Packet header for IPv4.
Definition: ipv4-header.h:31
virtual void ReadOptions(const TcpHeader &tcpHeader)
Read option from incoming packets.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)
Send data to a specified peer.
virtual void EstimateRtt(const TcpHeader &tcpHeader)
Take into account the packet for RTT estimation.
virtual uint32_t UnAckDataCount(void)
Return count of number of unacked bytes.
void ForwardUp(Ptr< Packet > packet, Ipv4Header header, uint16_t port, Ptr< Ipv4Interface > incomingInterface)
Called by the L3 protocol when it received a packet to pass on to TCP.
virtual uint32_t GetSndBufSize(void) const
Get the send buffer size.
virtual enum SocketType GetSocketType(void) const
virtual void SetInitialCwnd(uint32_t cwnd)=0
Set the initial Congestion Window.
Ipv6EndPoint * m_endPoint6
the IPv6 endpoint
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
bool OutOfRange(SequenceNumber32 head, SequenceNumber32 tail) const
Check if a sequence number range is within the rx window.
virtual bool SetAllowBroadcast(bool allowBroadcast)
Configure whether broadcast datagram transmissions are allowed.
virtual void SetTcpNoDelay(bool noDelay)
Enable/Disable Nagle's algorithm.
class for the reordering buffer that keeps the data from lower layer, i.e.
Definition: tcp-rx-buffer.h:40
virtual uint32_t GetConnCount(void) const
Get the number of connection retries before giving up.
class for keeping the data sent by the application to the TCP socket, i.e.
Definition: tcp-tx-buffer.h:40
enum SocketErrno m_errno
Socket error code.
A base class for implementation of a stream socket using TCP.
Ptr< RttEstimator > m_rtt
Round trip time estimator.
bool m_closeNotified
Told app to close socket.
void ProcessSynSent(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received a packet upon SYN_SENT.
int SetupCallback(void)
Common part of the two Bind(), i.e.
virtual void SetTcp(Ptr< TcpL4Protocol > tcp)
Set the associated TCP L4 protocol.
Ptr< TcpL4Protocol > m_tcp
the associated TCP L4 protocol
void ProcessSynRcvd(Ptr< Packet > packet, const TcpHeader &tcpHeader, const Address &fromAddress, const Address &toAddress)
Received a packet upon SYN_RCVD.
virtual int ShutdownSend(void)
virtual void SetDelAckMaxCount(uint32_t count)
Set the number of packet to fire an ACK before delay timeout.
int DoConnect(void)
Perform the real connection tasks: Send SYN if allowed, RST if invalid.
void CompleteFork(Ptr< Packet > p, const TcpHeader &tcpHeader, const Address &fromAddress, const Address &toAddress)
Complete a connection by forking the socket.
virtual void AddOptions(TcpHeader &tcpHeader)
Add option to outgoing packets.
uint32_t m_dupAckCount
Dupack counter.
Ptr< Node > m_node
the associated node
void TimeWait(void)
Move from CLOSING or FIN_WAIT_2 to TIME_WAIT state.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:43
virtual void SetNode(Ptr< Node > node)
Set the associated node.
virtual uint32_t BytesInFlight(void)
Return total bytes in flight.
void Destroy(void)
Kill this socket by zeroing its attributes (IPv4)
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
virtual int Close(void)
Close a socket.
virtual uint32_t AvailableWindow(void)
Return unfilled portion of window.
virtual Ptr< TcpSocketBase > Fork(void)=0
Call CopyObject<> to clone me.
virtual void DupAck(const TcpHeader &tcpHeader, uint32_t count)=0
Received dupack (duplicate ACK)
virtual void SetRtt(Ptr< RttEstimator > rtt)
Set the associated RTT estimator.
double m_msl
Max segment lifetime.
virtual void ReTxTimeout(void)
Call Retransmit() upon RTO event.
TracedValue< SequenceNumber32 > m_highTxMark
Highest seqno ever sent, regardless of ReTx.
void SendEmptyPacket(uint8_t flags)
Send a empty packet that carries a flag, e.g.
Time m_delAckTimeout
Time to delay an ACK.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)
Read a single packet from the socket and retrieve the sender address.
virtual void SetSndBufSize(uint32_t size)
Set the send buffer size.
virtual void SetDelAckTimeout(Time timeout)
Set the time to delay an ACK.
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
void DeallocateEndPoint(void)
Deallocate m_endPoint and m_endPoint6.
TcpRxBuffer m_rxBuffer
Rx buffer (reordering buffer)
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ProcessEstablished(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received a packet upon ESTABLISHED state.
bool m_connected
Connection established.
TracedValue< Time > m_rto
Retransmit timeout.
an identifier for simulation events.
Definition: event-id.h:46
int DoClose(void)
Close a socket by sending RST, FIN, or FIN+ACK, depend on the current state.
virtual void SetConnCount(uint32_t count)
Set the number of connection retries before giving up.
A representation of an internet IPv6 endpoint/connection.
virtual uint32_t Window(void)
Return the max possible number of unacked bytes.
virtual enum SocketErrno GetErrno(void) const
Get last error number.
void ProcessLastAck(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received a packet upon LAST_ACK.
virtual void SetSegSize(uint32_t size)
Set the segment size.
void ProcessClosing(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received a packet upon CLOSING.
virtual void ReceivedData(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Recv of a data, put into buffer, call L7 to get it if necessary.
void CloseAndNotify(void)
Peacefully close the socket by notifying the upper layer and deallocate end point.
Callback< void, Ipv6Address, uint8_t, uint8_t, uint8_t, uint32_t > m_icmpCallback6
ICMPv6 callback.
tuple address
Definition: first.py:37
virtual Ptr< Node > GetNode(void) const
Return the node this socket is associated with.
virtual void SetRcvBufSize(uint32_t size)
Set the receive buffer size.
bool m_shutdownRecv
Receive no longer allowed.
virtual Time GetDelAckTimeout(void) const
Get the time to delay an ACK.
virtual void DelAckTimeout(void)
Action upon delay ACK timeout, i.e.
virtual void LastAckTimeout(void)
Timeout at LAST_ACK, close the connection.
virtual void DoForwardUp(Ptr< Packet > packet, Ipv4Header header, uint16_t port, Ptr< Ipv4Interface > incomingInterface)
Called by TcpSocketBase::ForwardUp().
virtual int Bind6(void)
Allocate a local IPv6 endpoint for this socket.
Enumeration of the possible socket types.
Definition: socket.h:104
EventId m_delAckEvent
Delayed ACK timeout event.
a unique identifier for an interface.
Definition: type-id.h:49
bool m_shutdownSend
Send no longer allowed.
int SetupEndpoint6(void)
Configure the endpoint v6 to a local address.
bool SendPendingData(bool withAck=false)
Send as much pending data as possible according to the Tx window.
void ForwardIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo)
Called by the L3 protocol when it received an ICMP packet to pass on to TCP.
TracedValue< uint32_t > m_rWnd
Flow control window at remote side.
virtual void DoRetransmit(void)
Retransmit the oldest packet.
bool m_closeOnEmpty
Close socket upon tx buffer emptied.
A representation of an internet endpoint/connection.
virtual uint32_t GetDelAckMaxCount(void) const
Get the number of packet to fire an ACK before delay timeout.
virtual void SetConnTimeout(Time timeout)
Set the connection timeout.
EventId m_persistEvent
Persist event: Send 1 byte to probe for a non-zero Rx window.
int SetupEndpoint(void)
Configure the endpoint to a local address.
virtual Time GetConnTimeout(void) const
Get the connection timeout.
void Destroy6(void)
Kill this socket by zeroing its attributes (IPv6)
Time m_persistTimeout
Time between sending 1-byte probes.
virtual uint32_t GetTxAvailable(void) const
Returns the number of bytes which can be sent in a single call to Send.
virtual uint32_t GetSSThresh(void) const =0
Get the Slow Start Threshold.
virtual uint32_t GetRxAvailable(void) const
Return number of bytes which can be returned from one or multiple calls to Recv.