1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16 #ifndef NSC_TCP_SOCKET_IMPL_H
17 #define NSC_TCP_SOCKET_IMPL_H
18 
19 #include <stdint.h>
20 #include <queue>
21 #include <vector>
22 
23 #include "ns3/callback.h"
24 #include "ns3/traced-value.h"
25 #include "ns3/tcp-socket.h"
26 #include "ns3/ptr.h"
27 #include "ns3/ipv4-address.h"
28 #include "ns3/inet-socket-address.h"
29 #include "ns3/event-id.h"
30 #include "pending-data.h"
31 #include "ns3/sequence-number.h"
32 
33 struct INetStreamSocket;
34 
35 namespace ns3 {
36 
37 class Ipv4EndPoint;
38 class Node;
39 class Packet;
40 class NscTcpL4Protocol;
41 class TcpHeader;
42 
43 /**
44  * \ingroup socket
45  * \ingroup nsctcp
46  *
47  * \brief Socket logic for the NSC TCP sockets.
48  *
49  * Most of the TCP internal
50  * logic is handled by the NSC tcp library itself; this class maps ns3::Socket
51  * calls to the NSC TCP library.
52  */
53 class NscTcpSocketImpl : public TcpSocket
54 {
55 public:
56   /**
57    * \brief Get the type ID.
58    * \return the object TypeId
59    */
60   static TypeId GetTypeId (void);
61   /**
62    * Create an unbound tcp socket.
63    */
64   NscTcpSocketImpl ();
65 
66   /**
67    * Clone a TCP socket, for use upon receiving a connection request in LISTEN state
68    *
69    * \param sock the original Tcp Socket
70    */
71   NscTcpSocketImpl (const NscTcpSocketImpl& sock);
72   virtual ~NscTcpSocketImpl ();
73 
74   /**
75    * \brief Set the associated node.
76    * \param node the node
77    */
78   void SetNode (Ptr<Node> node);
79 
80   /**
81    * \brief Set the associated TCP L4 protocol.
82    * \param tcp the TCP L4 protocol
83    */
84   void SetTcp (Ptr<NscTcpL4Protocol> tcp);
85 
86   virtual enum SocketErrno GetErrno (void) const;
87   virtual enum SocketType GetSocketType (void) const;
88   virtual int GetPeerName (Address &address) const;
89   virtual Ptr<Node> GetNode (void) const;
90   virtual int Bind (void);
91   virtual int Bind6 (void);
92   virtual int Bind (const Address &address);
93   virtual int Close (void);
94   virtual int ShutdownSend (void);
95   virtual int ShutdownRecv (void);
96   virtual int Connect (const Address &address);
97   virtual int Listen (void);
98   virtual uint32_t GetTxAvailable (void) const;
99   virtual int Send (Ptr<Packet> p, uint32_t flags);
100   virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress);
101   virtual uint32_t GetRxAvailable (void) const;
102   virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags);
103   virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags,
104                                 Address &fromAddress);
105   virtual int GetSockName (Address &address) const;
106   virtual bool SetAllowBroadcast (bool allowBroadcast);
107   virtual bool GetAllowBroadcast () const;
108   virtual void BindToNetDevice (Ptr<NetDevice> netdevice); // NetDevice with my m_endPoint
109 
110 private:
111   /**
112    * \brief Called by NscTcpSocketImpl::ForwardUp()
113    *
114    * Actually performs the ForwardUp operations
115    */
116   void NSCWakeup (void);
117   /**
118    * \brief Tcp friend class.
119    * \relates Tcp
120    */
121   friend class Tcp;
122   // invoked by Tcp class
123   /**
124    * Finish the binding process
125    * \returns 0 on success, -1 on failure
126    */
127   int FinishBind (void);
128   /**
129    * \brief Called by the L3 protocol when it received a packet to pass on to TCP.
130    *
131    * \param p the incoming packet
132    * \param header the packet's IPv4 header
133    * \param port the incoming port
134    * \param incomingInterface the incoming interface
135    */
136   void ForwardUp (Ptr<Packet> p, Ipv4Header header, uint16_t port,
137                   Ptr<Ipv4Interface> incomingInterface);
138   /**
139    * \brief Kill this socket by zeroing its attributes (IPv4)
140    *
141    * This is a callback function configured to m_endpoint in
142    * SetupCallback(), invoked when the endpoint is destroyed.
143    */
144   void Destroy (void);
145   //methods for state
146   /**
147    * \brief Send all the pending data
148    * \returns true on success
149    */
150   bool SendPendingData (void);
151   /**
152    * \brief Read all the pending data
153    * \returns true on success
154    */
155   bool ReadPendingData (void);
156   /**
157    * \brief Accept an incoming connection
158    * \returns true on success
159    */
160   bool Accept (void);
161   /**
162    * \brief Complete the Fork operations (after a connection has been accepted)
163    */
164   void CompleteFork (void);
165 
166   /**
167    * \brief Called when a connection is in Established state
168    */
169   void ConnectionSucceeded ();
170 
171   // Manage data tx/rx
172   // \todo This should be virtual and overridden
173   /**
174    * \brief Copy self
175    * \returns a copy of self
176    */
177   Ptr<NscTcpSocketImpl> Copy ();
178 
179   // attribute related
180   virtual void SetSndBufSize (uint32_t size);
181   virtual uint32_t GetSndBufSize (void) const;
182   virtual void SetRcvBufSize (uint32_t size);
183   virtual uint32_t GetRcvBufSize (void) const;
184   virtual void SetSegSize (uint32_t size);
185   virtual uint32_t GetSegSize (void) const;
186   /**
187    * \brief Set the Advertised Window size
188    * \param window the window size
189    */
190   virtual void SetAdvWin (uint32_t window);
191   /**
192    * \brief Get the Advertised Window size
193    * \returns the window size
194    */
195   virtual uint32_t GetAdvWin (void) const;
196   virtual void SetInitialSSThresh (uint32_t threshold);
197   virtual uint32_t GetInitialSSThresh (void) const;
198   virtual void SetInitialCwnd (uint32_t cwnd);
199   virtual uint32_t GetInitialCwnd (void) const;
200   virtual void SetConnTimeout (Time timeout);
201   virtual Time GetConnTimeout (void) const;
202   virtual uint32_t GetSynRetries (void) const;
203   virtual void SetSynRetries (uint32_t count);
204   virtual void SetDataRetries (uint32_t retries);
205   virtual uint32_t GetDataRetries (void) const;
206   virtual void SetDelAckTimeout (Time timeout);
207   virtual Time GetDelAckTimeout (void) const;
208   virtual void SetDelAckMaxCount (uint32_t count);
209   virtual uint32_t GetDelAckMaxCount (void) const;
210   virtual void SetTcpNoDelay (bool noDelay);
211   virtual bool GetTcpNoDelay (void) const;
212   virtual void SetPersistTimeout (Time timeout);
213   virtual Time GetPersistTimeout (void) const;
214 
215   /**
216    * \brief Translate between a NSC error and a ns-3 error code
217    * \param err NSC error
218    * \returns ns-3 error code
219    */
220   enum Socket::SocketErrno GetNativeNs3Errno (int err) const;
221   uint32_t m_delAckMaxCount;  //!< Number of packet to fire an ACK before delay timeout
222   Time m_delAckTimeout;       //!< Time to delay an ACK
223   bool m_noDelay;             //!< Disable ACk delay
224 
225   Ipv4EndPoint *m_endPoint;     //!< the IPv4 endpoint
226   Ptr<Node> m_node;             //!< the associated node
227   Ptr<NscTcpL4Protocol> m_tcp;  //!< the associated TCP L4 protocol
228   Ipv4Address m_remoteAddress;  //!< peer IP address
229   uint16_t m_remotePort;        //!< peer port
230   //these two are so that the socket/endpoint cloning works
231   Ipv4Address m_localAddress;   //!< local address
232   uint16_t m_localPort;         //!< local port
233   InetSocketAddress m_peerAddress; //!< peer IP and port
234   mutable enum SocketErrno m_errno; //!< last error number
235   bool m_shutdownSend;          //!< Send no longer allowed
236   bool m_shutdownRecv;          //!< Receive no longer allowed
237   bool m_connected;             //!< Connection established
238 
239   //manage the state information
240   TracedValue<TcpStates_t> m_state; //!< state information
241   bool m_closeOnEmpty;              //!< true if socket will close when buffer is empty
242 
243   //needed to queue data when in SYN_SENT state
244   std::queue<Ptr<Packet> > m_txBuffer; //!< transmission buffer
245   uint32_t m_txBufferSize;             //!< transmission buffer size
246 
247   // Window management
248   uint32_t                       m_segmentSize;          //!< SegmentSize
249   uint32_t                       m_rxWindowSize;         //!< Receive window size
250   uint32_t                       m_advertisedWindowSize; //!< Window to advertise
251   TracedValue<uint32_t>          m_cWnd;                 //!< Congestion window
252   TracedValue<uint32_t>          m_ssThresh;             //!< Slow Start Threshold
253   uint32_t                       m_initialCWnd;          //!< Initial cWnd value
254   uint32_t                       m_initialSsThresh;      //!< Initial Slow Start Threshold
255 
256   // Round trip time estimation
257   Time m_lastMeasuredRtt; //!< Last measured RTT
258 
259   // Timer-related members
260   Time              m_cnTimeout;       //!< Timeout for connection retry
261   uint32_t          m_synRetries;      //!< Count of remaining connection retries
262   uint32_t          m_dataRetries;     //!< Count of remaining data retransmission attempts
263   Time              m_persistTimeout;  //!< Time between sending 1-byte probes
264 
265   // Temporary queue for delivering data to application
266   std::queue<Ptr<Packet> > m_deliveryQueue; //!< receive buffer
267   uint32_t m_rxAvailable;                   //!< receive buffer available size
268   INetStreamSocket* m_nscTcpSocket;         //!< the real NSC TCP socket
269 
270   // Attributes
271   uint32_t m_sndBufSize;   //!< buffer limit for the outgoing queue
272   uint32_t m_rcvBufSize;   //!< maximum receive socket buffer size
273 };
274 
275 } // namespace ns3
276 
277 #endif /* NSC_TCP_SOCKET_IMPL_H */
278