author | Adrian S Tam <adrian.sw.tam@gmail.com> |
Wed, 07 Dec 2011 11:22:10 -0500 | |
changeset 7619 | b4dee6307aa7 |
parent 7611 | d462369b70f1 |
child 7622 | f2e5d5201044 |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7256
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
6694 | 2 |
/* |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
3 |
* Copyright (c) 2007 Georgia Tech Research Corporation |
6694 | 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 |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
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 |
||
22 |
#define NS_LOG_APPEND_CONTEXT \ |
|
23 |
if (m_node) { std::clog << Simulator::Now ().GetSeconds () << " [node " << m_node->GetId () << "] "; } |
|
24 |
||
25 |
#include "ns3/abort.h" |
|
26 |
#include "ns3/node.h" |
|
27 |
#include "ns3/inet-socket-address.h" |
|
28 |
#include "ns3/log.h" |
|
29 |
#include "ns3/ipv4.h" |
|
30 |
#include "ns3/ipv4-interface-address.h" |
|
31 |
#include "ns3/ipv4-route.h" |
|
32 |
#include "ns3/ipv4-routing-protocol.h" |
|
33 |
#include "ns3/simulation-singleton.h" |
|
34 |
#include "ns3/simulator.h" |
|
35 |
#include "ns3/packet.h" |
|
36 |
#include "ns3/uinteger.h" |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
37 |
#include "ns3/double.h" |
6694 | 38 |
#include "ns3/trace-source-accessor.h" |
39 |
#include "tcp-socket-base.h" |
|
40 |
#include "tcp-l4-protocol.h" |
|
41 |
#include "ipv4-end-point.h" |
|
42 |
#include "tcp-header.h" |
|
43 |
#include "rtt-estimator.h" |
|
44 |
||
45 |
#include <algorithm> |
|
46 |
||
47 |
NS_LOG_COMPONENT_DEFINE ("TcpSocketBase"); |
|
48 |
||
49 |
namespace ns3 { |
|
50 |
||
51 |
NS_OBJECT_ENSURE_REGISTERED (TcpSocketBase); |
|
52 |
||
53 |
TypeId |
|
54 |
TcpSocketBase::GetTypeId (void) |
|
55 |
{ |
|
56 |
static TypeId tid = TypeId ("ns3::TcpSocketBase") |
|
57 |
.SetParent<TcpSocket> () |
|
58 |
// .AddAttribute ("TcpState", "State in TCP state machine", |
|
59 |
// TypeId::ATTR_GET, |
|
60 |
// EnumValue (CLOSED), |
|
61 |
// MakeEnumAccessor (&TcpSocketBase::m_state), |
|
62 |
// MakeEnumChecker (CLOSED, "Closed")) |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
63 |
.AddAttribute ("MaxSegLifetime", |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
64 |
"Maximum segment lifetime in seconds, use for TIME_WAIT state transition to CLOSED state", |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
65 |
DoubleValue (120), /* RFC793 says MSL=2 minutes*/ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
66 |
MakeDoubleAccessor (&TcpSocketBase::m_msl), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
67 |
MakeDoubleChecker<double> (0)) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
68 |
.AddAttribute ("MaxWindowSize", "Max size of advertised window", |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
69 |
UintegerValue (65535), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
70 |
MakeUintegerAccessor (&TcpSocketBase::m_maxWinSize), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
71 |
MakeUintegerChecker<uint16_t> ()) |
6694 | 72 |
.AddTraceSource ("RTO", |
73 |
"Retransmission timeout", |
|
74 |
MakeTraceSourceAccessor (&TcpSocketBase::m_rto)) |
|
75 |
.AddTraceSource ("RTT", |
|
76 |
"Last RTT sample", |
|
77 |
MakeTraceSourceAccessor (&TcpSocketBase::m_lastRtt)) |
|
78 |
.AddTraceSource ("NextTxSequence", |
|
79 |
"Next sequence number to send (SND.NXT)", |
|
80 |
MakeTraceSourceAccessor (&TcpSocketBase::m_nextTxSequence)) |
|
81 |
.AddTraceSource ("HighestSequence", |
|
82 |
"Highest sequence number ever sent in socket's life time", |
|
83 |
MakeTraceSourceAccessor (&TcpSocketBase::m_highTxMark)) |
|
84 |
.AddTraceSource ("State", |
|
85 |
"TCP state", |
|
86 |
MakeTraceSourceAccessor (&TcpSocketBase::m_state)) |
|
87 |
.AddTraceSource ("RWND", |
|
88 |
"Remote side's flow control window", |
|
89 |
MakeTraceSourceAccessor (&TcpSocketBase::m_rWnd)) |
|
90 |
; |
|
91 |
return tid; |
|
92 |
} |
|
93 |
||
94 |
TcpSocketBase::TcpSocketBase (void) |
|
95 |
: m_dupAckCount (0), |
|
96 |
m_delAckCount (0), |
|
97 |
m_endPoint (0), |
|
98 |
m_node (0), |
|
99 |
m_tcp (0), |
|
100 |
m_rtt (0), |
|
101 |
m_nextTxSequence (0), // Change this for non-zero initial sequence number |
|
102 |
m_highTxMark (0), |
|
103 |
m_rxBuffer (0), |
|
104 |
m_txBuffer (0), |
|
105 |
m_state (CLOSED), |
|
106 |
m_errno (ERROR_NOTERROR), |
|
107 |
m_closeNotified (false), |
|
108 |
m_closeOnEmpty (false), |
|
109 |
m_shutdownSend (false), |
|
110 |
m_shutdownRecv (false), |
|
111 |
m_connected (false), |
|
112 |
m_segmentSize (0), // For attribute initialization consistency (quiet valgrind) |
|
113 |
m_rWnd (0) |
|
114 |
{ |
|
115 |
NS_LOG_FUNCTION (this); |
|
116 |
} |
|
117 |
||
118 |
TcpSocketBase::TcpSocketBase (const TcpSocketBase& sock) |
|
119 |
: TcpSocket (sock), //copy object::m_tid and socket::callbacks |
|
120 |
m_dupAckCount (sock.m_dupAckCount), |
|
121 |
m_delAckCount (0), |
|
122 |
m_delAckMaxCount (sock.m_delAckMaxCount), |
|
7619
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
123 |
m_noDelay (sock.m_noDelay), |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
124 |
m_cnRetries (sock.m_cnRetries), |
6694 | 125 |
m_delAckTimeout (sock.m_delAckTimeout), |
126 |
m_persistTimeout (sock.m_persistTimeout), |
|
127 |
m_cnTimeout (sock.m_cnTimeout), |
|
128 |
m_endPoint (0), |
|
129 |
m_node (sock.m_node), |
|
130 |
m_tcp (sock.m_tcp), |
|
131 |
m_rtt (0), |
|
132 |
m_nextTxSequence (sock.m_nextTxSequence), |
|
133 |
m_highTxMark (sock.m_highTxMark), |
|
134 |
m_rxBuffer (sock.m_rxBuffer), |
|
135 |
m_txBuffer (sock.m_txBuffer), |
|
136 |
m_state (sock.m_state), |
|
137 |
m_errno (sock.m_errno), |
|
138 |
m_closeNotified (sock.m_closeNotified), |
|
139 |
m_closeOnEmpty (sock.m_closeOnEmpty), |
|
140 |
m_shutdownSend (sock.m_shutdownSend), |
|
141 |
m_shutdownRecv (sock.m_shutdownRecv), |
|
142 |
m_connected (sock.m_connected), |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
143 |
m_msl (sock.m_msl), |
6694 | 144 |
m_segmentSize (sock.m_segmentSize), |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
145 |
m_maxWinSize (sock.m_maxWinSize), |
6694 | 146 |
m_rWnd (sock.m_rWnd) |
147 |
{ |
|
148 |
NS_LOG_FUNCTION (this); |
|
149 |
NS_LOG_LOGIC ("Invoked the copy constructor"); |
|
150 |
// Copy the rtt estimator if it is set |
|
151 |
if (sock.m_rtt) |
|
152 |
{ |
|
153 |
m_rtt = sock.m_rtt->Copy (); |
|
154 |
} |
|
155 |
// Reset all callbacks to null |
|
156 |
Callback<void, Ptr< Socket > > vPS = MakeNullCallback<void, Ptr<Socket> > (); |
|
157 |
Callback<void, Ptr<Socket>, const Address &> vPSA = MakeNullCallback<void, Ptr<Socket>, const Address &> (); |
|
158 |
Callback<void, Ptr<Socket>, uint32_t> vPSUI = MakeNullCallback<void, Ptr<Socket>, uint32_t> (); |
|
159 |
SetConnectCallback (vPS, vPS); |
|
160 |
SetDataSentCallback (vPSUI); |
|
161 |
SetSendCallback (vPSUI); |
|
162 |
SetRecvCallback (vPS); |
|
163 |
} |
|
164 |
||
165 |
TcpSocketBase::~TcpSocketBase (void) |
|
166 |
{ |
|
167 |
NS_LOG_FUNCTION (this); |
|
168 |
m_node = 0; |
|
169 |
if (m_endPoint != 0) |
|
170 |
{ |
|
171 |
NS_ASSERT (m_tcp != 0); |
|
172 |
/* |
|
173 |
* Upon Bind, an Ipv4Endpoint is allocated and set to m_endPoint, and |
|
174 |
* DestroyCallback is set to TcpSocketBase::Destroy. If we called |
|
175 |
* m_tcp->DeAllocate, it wil destroy its Ipv4EndpointDemux::DeAllocate, |
|
176 |
* which in turn destroys my m_endPoint, and in turn invokes |
|
177 |
* TcpSocketBase::Destroy to nullify m_node, m_endPoint, and m_tcp. |
|
178 |
*/ |
|
179 |
NS_ASSERT (m_endPoint != 0); |
|
180 |
m_tcp->DeAllocate (m_endPoint); |
|
181 |
NS_ASSERT (m_endPoint == 0); |
|
182 |
} |
|
183 |
m_tcp = 0; |
|
184 |
CancelAllTimers (); |
|
185 |
} |
|
186 |
||
187 |
/** Associate a node with this TCP socket */ |
|
188 |
void |
|
189 |
TcpSocketBase::SetNode (Ptr<Node> node) |
|
190 |
{ |
|
191 |
m_node = node; |
|
192 |
} |
|
193 |
||
194 |
/** Associate the L4 protocol (e.g. mux/demux) with this socket */ |
|
195 |
void |
|
196 |
TcpSocketBase::SetTcp (Ptr<TcpL4Protocol> tcp) |
|
197 |
{ |
|
198 |
m_tcp = tcp; |
|
199 |
} |
|
200 |
||
201 |
/** Set an RTT estimator with this socket */ |
|
202 |
void |
|
203 |
TcpSocketBase::SetRtt (Ptr<RttEstimator> rtt) |
|
204 |
{ |
|
205 |
m_rtt = rtt; |
|
206 |
} |
|
207 |
||
208 |
/** Inherit from Socket class: Returns error code */ |
|
209 |
enum Socket::SocketErrno |
|
210 |
TcpSocketBase::GetErrno (void) const |
|
211 |
{ |
|
212 |
return m_errno; |
|
213 |
} |
|
214 |
||
215 |
/** Inherit from Socket class: Returns socket type, NS3_SOCK_STREAM */ |
|
216 |
enum Socket::SocketType |
|
217 |
TcpSocketBase::GetSocketType (void) const |
|
218 |
{ |
|
219 |
return NS3_SOCK_STREAM; |
|
220 |
} |
|
221 |
||
222 |
/** Inherit from Socket class: Returns associated node */ |
|
223 |
Ptr<Node> |
|
224 |
TcpSocketBase::GetNode (void) const |
|
225 |
{ |
|
226 |
NS_LOG_FUNCTION_NOARGS (); |
|
227 |
return m_node; |
|
228 |
} |
|
229 |
||
230 |
/** Inherit from Socket class: Bind socket to an end-point in TcpL4Protocol */ |
|
231 |
int |
|
232 |
TcpSocketBase::Bind (void) |
|
233 |
{ |
|
234 |
NS_LOG_FUNCTION_NOARGS (); |
|
235 |
m_endPoint = m_tcp->Allocate (); |
|
7441
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
236 |
if (0 == m_endPoint) |
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
237 |
{ |
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
238 |
m_errno = ERROR_ADDRNOTAVAIL; |
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
239 |
return -1; |
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
240 |
} |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
241 |
m_tcp->m_sockets.push_back (this); |
6694 | 242 |
return SetupCallback (); |
243 |
} |
|
244 |
||
245 |
/** Inherit from Socket class: Bind socket (with specific address) to an end-point in TcpL4Protocol */ |
|
246 |
int |
|
247 |
TcpSocketBase::Bind (const Address &address) |
|
248 |
{ |
|
249 |
NS_LOG_FUNCTION (this << address); |
|
250 |
if (!InetSocketAddress::IsMatchingType (address)) |
|
251 |
{ |
|
252 |
m_errno = ERROR_INVAL; |
|
253 |
return -1; |
|
254 |
} |
|
255 |
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); |
|
256 |
Ipv4Address ipv4 = transport.GetIpv4 (); |
|
257 |
uint16_t port = transport.GetPort (); |
|
258 |
if (ipv4 == Ipv4Address::GetAny () && port == 0) |
|
259 |
{ |
|
260 |
m_endPoint = m_tcp->Allocate (); |
|
7441
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
261 |
if (0 == m_endPoint) |
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
262 |
{ |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
263 |
m_errno = ERROR_ADDRNOTAVAIL; |
7441
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
264 |
return -1; |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
265 |
} |
6694 | 266 |
} |
267 |
else if (ipv4 == Ipv4Address::GetAny () && port != 0) |
|
268 |
{ |
|
269 |
m_endPoint = m_tcp->Allocate (port); |
|
7440
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
270 |
if (0 == m_endPoint) |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
271 |
{ |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
272 |
m_errno = ERROR_ADDRINUSE; |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
273 |
return -1; |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
274 |
} |
6694 | 275 |
} |
276 |
else if (ipv4 != Ipv4Address::GetAny () && port == 0) |
|
277 |
{ |
|
278 |
m_endPoint = m_tcp->Allocate (ipv4); |
|
7441
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
279 |
if (0 == m_endPoint) |
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
280 |
{ |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
281 |
m_errno = ERROR_ADDRNOTAVAIL; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
282 |
return -1; |
7441
bf446d9feecc
Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
John Abraham <john.abraham@gatech.edu>
parents:
7440
diff
changeset
|
283 |
} |
6694 | 284 |
} |
285 |
else if (ipv4 != Ipv4Address::GetAny () && port != 0) |
|
286 |
{ |
|
287 |
m_endPoint = m_tcp->Allocate (ipv4, port); |
|
7440
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
288 |
if (0 == m_endPoint) |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
289 |
{ |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
290 |
m_errno = ERROR_ADDRINUSE; |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
291 |
return -1; |
979d65377d5f
Bug 1164 - IPV4 TCP Bind an already used port failed but without setting errno
John Abraham <john.abraham@gatech.edu>
parents:
7385
diff
changeset
|
292 |
} |
6694 | 293 |
} |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
294 |
m_tcp->m_sockets.push_back (this); |
6694 | 295 |
NS_LOG_LOGIC ("TcpSocketBase " << this << " got an endpoint: " << m_endPoint); |
296 |
||
297 |
return SetupCallback (); |
|
298 |
} |
|
299 |
||
300 |
/** Inherit from Socket class: Initiate connection to a remote address:port */ |
|
301 |
int |
|
302 |
TcpSocketBase::Connect (const Address & address) |
|
303 |
{ |
|
304 |
NS_LOG_FUNCTION (this << address); |
|
305 |
||
306 |
// If haven't do so, Bind() this socket first |
|
307 |
if (m_endPoint == 0) |
|
308 |
{ |
|
309 |
if (Bind () == -1) |
|
310 |
{ |
|
311 |
NS_ASSERT (m_endPoint == 0); |
|
312 |
return -1; // Bind() failed |
|
313 |
} |
|
314 |
NS_ASSERT (m_endPoint != 0); |
|
315 |
} |
|
316 |
||
317 |
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); |
|
318 |
m_endPoint->SetPeer (transport.GetIpv4 (), transport.GetPort ()); |
|
319 |
||
320 |
// Get the appropriate local address and port number from the routing protocol and set up endpoint |
|
321 |
if (SetupEndpoint () != 0) |
|
322 |
{ // Route to destination does not exist |
|
323 |
return -1; |
|
324 |
} |
|
325 |
||
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
326 |
// Re-initialize parameters in case this socket is being reused after CLOSE |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
327 |
m_rtt->Reset (); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
328 |
m_cnCount = m_cnRetries; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
329 |
|
6694 | 330 |
// DoConnect() will do state-checking and send a SYN packet |
331 |
return DoConnect (); |
|
332 |
} |
|
333 |
||
334 |
/** Inherit from Socket class: Listen on the endpoint for an incoming connection */ |
|
335 |
int |
|
336 |
TcpSocketBase::Listen (void) |
|
337 |
{ |
|
338 |
NS_LOG_FUNCTION (this); |
|
339 |
// Linux quits EINVAL if we're not in CLOSED state, so match what they do |
|
340 |
if (m_state != CLOSED) |
|
341 |
{ |
|
342 |
m_errno = ERROR_INVAL; |
|
343 |
return -1; |
|
344 |
} |
|
345 |
// In other cases, set the state to LISTEN and done |
|
346 |
NS_LOG_INFO ("CLOSED -> LISTEN"); |
|
347 |
m_state = LISTEN; |
|
348 |
return 0; |
|
349 |
} |
|
350 |
||
351 |
/** Inherit from Socket class: Kill this socket and signal the peer (if any) */ |
|
352 |
int |
|
353 |
TcpSocketBase::Close (void) |
|
354 |
{ |
|
355 |
NS_LOG_FUNCTION (this); |
|
356 |
// First we check to see if there is any unread rx data |
|
357 |
// Bug number 426 claims we should send reset in this case. |
|
358 |
if (m_rxBuffer.Size () != 0) |
|
359 |
{ |
|
360 |
SendRST (); |
|
361 |
return 0; |
|
362 |
} |
|
363 |
if (m_txBuffer.SizeFromSequence (m_nextTxSequence) > 0) |
|
364 |
{ // App close with pending data must wait until all data transmitted |
|
365 |
if (m_closeOnEmpty == false) |
|
366 |
{ |
|
367 |
m_closeOnEmpty = true; |
|
368 |
NS_LOG_INFO ("Socket " << this << " deferring close, state " << TcpStateName[m_state]); |
|
369 |
} |
|
370 |
return 0; |
|
371 |
} |
|
372 |
return DoClose (); |
|
373 |
} |
|
374 |
||
375 |
/** Inherit from Socket class: Signal a termination of send */ |
|
376 |
int |
|
377 |
TcpSocketBase::ShutdownSend (void) |
|
378 |
{ |
|
379 |
NS_LOG_FUNCTION (this); |
|
380 |
m_shutdownSend = true; |
|
381 |
return 0; |
|
382 |
} |
|
383 |
||
384 |
/** Inherit from Socket class: Signal a termination of receive */ |
|
385 |
int |
|
386 |
TcpSocketBase::ShutdownRecv (void) |
|
387 |
{ |
|
388 |
NS_LOG_FUNCTION (this); |
|
389 |
m_shutdownRecv = true; |
|
390 |
return 0; |
|
391 |
} |
|
392 |
||
393 |
/** Inherit from Socket class: Send a packet. Parameter flags is not used. |
|
394 |
Packet has no TCP header. Invoked by upper-layer application */ |
|
395 |
int |
|
396 |
TcpSocketBase::Send (Ptr<Packet> p, uint32_t flags) |
|
397 |
{ |
|
398 |
NS_LOG_FUNCTION (this << p); |
|
399 |
NS_ABORT_MSG_IF (flags, "use of flags is not supported in TcpSocketBase::Send()"); |
|
400 |
if (m_state == ESTABLISHED || m_state == SYN_SENT || m_state == CLOSE_WAIT) |
|
401 |
{ |
|
402 |
// Store the packet into Tx buffer |
|
403 |
if (!m_txBuffer.Add (p)) |
|
404 |
{ // TxBuffer overflow, send failed |
|
405 |
m_errno = ERROR_MSGSIZE; |
|
406 |
return -1; |
|
407 |
} |
|
408 |
// Submit the data to lower layers |
|
409 |
NS_LOG_LOGIC ("txBufSize=" << m_txBuffer.Size () << " state " << TcpStateName[m_state]); |
|
410 |
if (m_state == ESTABLISHED || m_state == CLOSE_WAIT) |
|
411 |
{ // Try to send the data out |
|
412 |
SendPendingData (m_connected); |
|
413 |
} |
|
414 |
return p->GetSize (); |
|
415 |
} |
|
416 |
else |
|
417 |
{ // Connection not established yet |
|
418 |
m_errno = ERROR_NOTCONN; |
|
419 |
return -1; // Send failure |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
/** Inherit from Socket class: In TcpSocketBase, it is same as Send() call */ |
|
424 |
int |
|
425 |
TcpSocketBase::SendTo (Ptr<Packet> p, uint32_t flags, const Address &address) |
|
426 |
{ |
|
427 |
return Send (p, flags); // SendTo() and Send() are the same |
|
428 |
} |
|
429 |
||
430 |
/** Inherit from Socket class: Return data to upper-layer application. Parameter flags |
|
431 |
is not used. Data is returned as a packet of size no larger than maxSize */ |
|
432 |
Ptr<Packet> |
|
433 |
TcpSocketBase::Recv (uint32_t maxSize, uint32_t flags) |
|
434 |
{ |
|
435 |
NS_LOG_FUNCTION (this); |
|
436 |
NS_ABORT_MSG_IF (flags, "use of flags is not supported in TcpSocketBase::Recv()"); |
|
437 |
if (m_rxBuffer.Size () == 0 && m_state == CLOSE_WAIT) |
|
438 |
{ |
|
439 |
return Create<Packet> (); // Send EOF on connection close |
|
440 |
} |
|
441 |
Ptr<Packet> outPacket = m_rxBuffer.Extract (maxSize); |
|
442 |
if (outPacket != 0 && outPacket->GetSize () != 0) |
|
443 |
{ |
|
444 |
SocketAddressTag tag; |
|
445 |
tag.SetAddress (InetSocketAddress (m_endPoint->GetPeerAddress (), m_endPoint->GetPeerPort ())); |
|
446 |
outPacket->AddPacketTag (tag); |
|
447 |
} |
|
448 |
return outPacket; |
|
449 |
} |
|
450 |
||
451 |
/** Inherit from Socket class: Recv and return the remote's address */ |
|
452 |
Ptr<Packet> |
|
453 |
TcpSocketBase::RecvFrom (uint32_t maxSize, uint32_t flags, Address &fromAddress) |
|
454 |
{ |
|
455 |
NS_LOG_FUNCTION (this << maxSize << flags); |
|
456 |
Ptr<Packet> packet = Recv (maxSize, flags); |
|
457 |
// Null packet means no data to read, and an empty packet indicates EOF |
|
458 |
if (packet != 0 && packet->GetSize () != 0) |
|
459 |
{ |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
460 |
if (m_endPoint != 0) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
461 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
462 |
fromAddress = InetSocketAddress (m_endPoint->GetPeerAddress (), m_endPoint->GetPeerPort ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
463 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
464 |
else |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
465 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
466 |
fromAddress = InetSocketAddress (Ipv4Address::GetZero (), 0); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
467 |
} |
6694 | 468 |
} |
469 |
return packet; |
|
470 |
} |
|
471 |
||
472 |
/** Inherit from Socket class: Get the max number of bytes an app can send */ |
|
473 |
uint32_t |
|
474 |
TcpSocketBase::GetTxAvailable (void) const |
|
475 |
{ |
|
476 |
NS_LOG_FUNCTION (this); |
|
477 |
return m_txBuffer.Available (); |
|
478 |
} |
|
479 |
||
480 |
/** Inherit from Socket class: Get the max number of bytes an app can read */ |
|
481 |
uint32_t |
|
482 |
TcpSocketBase::GetRxAvailable (void) const |
|
483 |
{ |
|
484 |
NS_LOG_FUNCTION (this); |
|
485 |
return m_rxBuffer.Available (); |
|
486 |
} |
|
487 |
||
488 |
/** Inherit from Socket class: Return local address:port */ |
|
489 |
int |
|
490 |
TcpSocketBase::GetSockName (Address &address) const |
|
491 |
{ |
|
492 |
NS_LOG_FUNCTION (this); |
|
493 |
if (m_endPoint != 0) |
|
494 |
{ |
|
495 |
address = InetSocketAddress (m_endPoint->GetLocalAddress (), m_endPoint->GetLocalPort ()); |
|
496 |
} |
|
497 |
else |
|
498 |
{ // It is possible to call this method on a socket without a name |
|
499 |
// in which case, behavior is unspecified |
|
500 |
address = InetSocketAddress (Ipv4Address::GetZero (), 0); |
|
501 |
} |
|
502 |
return 0; |
|
503 |
} |
|
504 |
||
505 |
/** Inherit from Socket class: Bind this socket to the specified NetDevice */ |
|
506 |
void |
|
507 |
TcpSocketBase::BindToNetDevice (Ptr<NetDevice> netdevice) |
|
508 |
{ |
|
509 |
NS_LOG_FUNCTION (netdevice); |
|
510 |
Socket::BindToNetDevice (netdevice); // Includes sanity check |
|
511 |
if (m_endPoint == 0) |
|
512 |
{ |
|
513 |
if (Bind () == -1) |
|
514 |
{ |
|
515 |
NS_ASSERT (m_endPoint == 0); |
|
516 |
return; |
|
517 |
} |
|
518 |
NS_ASSERT (m_endPoint != 0); |
|
519 |
} |
|
520 |
m_endPoint->BindToNetDevice (netdevice); |
|
521 |
return; |
|
522 |
} |
|
523 |
||
524 |
/** Clean up after Bind. Set up callback functions in the end-point. */ |
|
525 |
int |
|
526 |
TcpSocketBase::SetupCallback (void) |
|
527 |
{ |
|
528 |
NS_LOG_FUNCTION (this); |
|
529 |
if (m_endPoint == 0) |
|
530 |
{ |
|
531 |
return -1; |
|
532 |
} |
|
533 |
m_endPoint->SetRxCallback (MakeCallback (&TcpSocketBase::ForwardUp, Ptr<TcpSocketBase> (this))); |
|
534 |
m_endPoint->SetDestroyCallback (MakeCallback (&TcpSocketBase::Destroy, Ptr<TcpSocketBase> (this))); |
|
535 |
return 0; |
|
536 |
} |
|
537 |
||
538 |
/** Perform the real connection tasks: Send SYN if allowed, RST if invalid */ |
|
539 |
int |
|
540 |
TcpSocketBase::DoConnect (void) |
|
541 |
{ |
|
542 |
NS_LOG_FUNCTION (this); |
|
543 |
||
544 |
// A new connection is allowed only if this socket does not have a connection |
|
545 |
if (m_state == CLOSED || m_state == LISTEN || m_state == SYN_SENT || m_state == LAST_ACK || m_state == CLOSE_WAIT) |
|
546 |
{ // send a SYN packet and change state into SYN_SENT |
|
547 |
SendEmptyPacket (TcpHeader::SYN); |
|
548 |
NS_LOG_INFO (TcpStateName[m_state] << " -> SYN_SENT"); |
|
549 |
m_state = SYN_SENT; |
|
550 |
} |
|
551 |
else if (m_state != TIME_WAIT) |
|
552 |
{ // In states SYN_RCVD, ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, and CLOSING, an connection |
|
553 |
// exists. We send RST, tear down everything, and close this socket. |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
554 |
SendRST (); |
6694 | 555 |
CloseAndNotify (); |
556 |
} |
|
557 |
return 0; |
|
558 |
} |
|
559 |
||
560 |
/** Do the action to close the socket. Usually send a packet with appropriate |
|
561 |
flags depended on the current m_state. */ |
|
562 |
int |
|
563 |
TcpSocketBase::DoClose (void) |
|
564 |
{ |
|
565 |
NS_LOG_FUNCTION (this); |
|
566 |
switch (m_state) |
|
567 |
{ |
|
568 |
case SYN_RCVD: |
|
569 |
case ESTABLISHED: |
|
570 |
// send FIN to close the peer |
|
571 |
SendEmptyPacket (TcpHeader::FIN); |
|
572 |
NS_LOG_INFO ("ESTABLISHED -> FIN_WAIT_1"); |
|
573 |
m_state = FIN_WAIT_1; |
|
574 |
break; |
|
575 |
case CLOSE_WAIT: |
|
576 |
// send FIN+ACK to close the peer |
|
577 |
SendEmptyPacket (TcpHeader::FIN | TcpHeader::ACK); |
|
578 |
NS_LOG_INFO ("CLOSE_WAIT -> LAST_ACK"); |
|
579 |
m_state = LAST_ACK; |
|
580 |
break; |
|
581 |
case SYN_SENT: |
|
582 |
case CLOSING: |
|
583 |
// Send RST if application closes in SYN_SENT and CLOSING |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
584 |
SendRST (); |
6694 | 585 |
CloseAndNotify (); |
586 |
break; |
|
587 |
case LISTEN: |
|
588 |
case LAST_ACK: |
|
589 |
// In these three states, move to CLOSED and tear down the end point |
|
590 |
CloseAndNotify (); |
|
591 |
break; |
|
592 |
case CLOSED: |
|
593 |
case FIN_WAIT_1: |
|
594 |
case FIN_WAIT_2: |
|
595 |
case TIME_WAIT: |
|
596 |
default: /* mute compiler */ |
|
597 |
// Do nothing in these four states |
|
598 |
break; |
|
599 |
} |
|
600 |
return 0; |
|
601 |
} |
|
602 |
||
603 |
/** Peacefully close the socket by notifying the upper layer and deallocate end point */ |
|
604 |
void |
|
605 |
TcpSocketBase::CloseAndNotify (void) |
|
606 |
{ |
|
607 |
NS_LOG_FUNCTION (this); |
|
608 |
||
609 |
if (!m_closeNotified) NotifyNormalClose (); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
610 |
if (m_state != TIME_WAIT) DeallocateEndPoint (); |
6694 | 611 |
m_closeNotified = true; |
612 |
NS_LOG_INFO (TcpStateName[m_state] << " -> CLOSED"); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
613 |
CancelAllTimers (); |
6694 | 614 |
m_state = CLOSED; |
615 |
} |
|
616 |
||
617 |
||
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
618 |
/** Tell if a sequence number range is out side the range that my rx buffer can |
6694 | 619 |
accpet */ |
620 |
bool |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
621 |
TcpSocketBase::OutOfRange (SequenceNumber32 head, SequenceNumber32 tail) const |
6694 | 622 |
{ |
623 |
if (m_state == LISTEN || m_state == SYN_SENT || m_state == SYN_RCVD) |
|
624 |
{ // Rx buffer in these states are not initialized. |
|
625 |
return false; |
|
626 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
627 |
if (m_state == LAST_ACK || m_state == CLOSING || m_state == CLOSE_WAIT) |
6694 | 628 |
{ // In LAST_ACK and CLOSING states, it only wait for an ACK and the |
629 |
// sequence number must equals to m_rxBuffer.NextRxSequence () |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
630 |
return (m_rxBuffer.NextRxSequence () != head); |
6694 | 631 |
}; |
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
7045
diff
changeset
|
632 |
|
6694 | 633 |
// In all other cases, check if the sequence number is in range |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
634 |
return (tail < m_rxBuffer.NextRxSequence () || m_rxBuffer.MaxRxSequence () <= head); |
6694 | 635 |
} |
636 |
||
637 |
/** Function called by the L3 protocol when it received a packet to pass on to |
|
638 |
the TCP. This function is registered as the "RxCallback" function in |
|
639 |
SetupCallback(), which invoked by Bind(), and CompleteFork() */ |
|
640 |
void |
|
641 |
TcpSocketBase::ForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, |
|
642 |
Ptr<Ipv4Interface> incomingInterface) |
|
643 |
{ |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
644 |
DoForwardUp (packet, header, port, incomingInterface); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
645 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
646 |
|
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
647 |
/** The real function to handle the incoming packet from lower layers. This is |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
648 |
wrapped by ForwardUp() so that this function can be overloaded by daughter |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
649 |
classes. */ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
650 |
void |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
651 |
TcpSocketBase::DoForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
652 |
Ptr<Ipv4Interface> incomingInterface) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
653 |
{ |
6694 | 654 |
NS_LOG_LOGIC ("Socket " << this << " forward up " << |
655 |
m_endPoint->GetPeerAddress () << |
|
656 |
":" << m_endPoint->GetPeerPort () << |
|
657 |
" to " << m_endPoint->GetLocalAddress () << |
|
658 |
":" << m_endPoint->GetLocalPort ()); |
|
659 |
Address fromAddress = InetSocketAddress (header.GetSource (), port); |
|
660 |
Address toAddress = InetSocketAddress (header.GetDestination (), m_endPoint->GetLocalPort ()); |
|
661 |
||
662 |
// Peel off TCP header and do validity checking |
|
663 |
TcpHeader tcpHeader; |
|
664 |
packet->RemoveHeader (tcpHeader); |
|
665 |
if (tcpHeader.GetFlags () & TcpHeader::ACK) |
|
666 |
{ |
|
667 |
EstimateRtt (tcpHeader); |
|
668 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
669 |
ReadOptions (tcpHeader); |
6694 | 670 |
|
671 |
// Update Rx window size, i.e. the flow control window |
|
672 |
if (m_rWnd.Get () == 0 && tcpHeader.GetWindowSize () != 0) |
|
673 |
{ // persist probes end |
|
674 |
NS_LOG_LOGIC (this << " Leaving zerowindow persist state"); |
|
675 |
m_persistEvent.Cancel (); |
|
676 |
} |
|
677 |
m_rWnd = tcpHeader.GetWindowSize (); |
|
678 |
||
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
679 |
// Discard fully out of range packets |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
680 |
if (OutOfRange (tcpHeader.GetSequenceNumber (), tcpHeader.GetSequenceNumber () + packet->GetSize ())) |
6694 | 681 |
{ |
682 |
NS_LOG_LOGIC ("At state " << TcpStateName[m_state] << |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
683 |
" received packet of seq [" << tcpHeader.GetSequenceNumber () << |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
684 |
":" << tcpHeader.GetSequenceNumber () + packet->GetSize() << |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
685 |
") out of range [" << m_rxBuffer.NextRxSequence () << ":" << |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
686 |
m_rxBuffer.MaxRxSequence () << ")"); |
7610
0c91f9d21f89
Send ACK to some out-of-order packets (fixes bug 1112 and part of 1256)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
687 |
// Acknowledgement should be sent for all unacceptable packets (RFC793, p.69) |
0c91f9d21f89
Send ACK to some out-of-order packets (fixes bug 1112 and part of 1256)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
688 |
if (m_state == ESTABLISHED && !(tcpHeader.GetFlags () & TcpHeader::RST)) |
0c91f9d21f89
Send ACK to some out-of-order packets (fixes bug 1112 and part of 1256)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
689 |
{ |
0c91f9d21f89
Send ACK to some out-of-order packets (fixes bug 1112 and part of 1256)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
690 |
SendEmptyPacket (TcpHeader::ACK); |
0c91f9d21f89
Send ACK to some out-of-order packets (fixes bug 1112 and part of 1256)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
691 |
} |
6694 | 692 |
return; |
693 |
} |
|
694 |
||
695 |
// TCP state machine code in different process functions |
|
696 |
// C.f.: tcp_rcv_state_process() in tcp_input.c in Linux kernel |
|
697 |
switch (m_state) |
|
698 |
{ |
|
699 |
case ESTABLISHED: |
|
700 |
ProcessEstablished (packet, tcpHeader); |
|
701 |
break; |
|
702 |
case LISTEN: |
|
703 |
ProcessListen (packet, tcpHeader, fromAddress, toAddress); |
|
704 |
break; |
|
705 |
case TIME_WAIT: |
|
706 |
// Do nothing |
|
707 |
break; |
|
708 |
case CLOSED: |
|
709 |
// Send RST if the incoming packet is not a RST |
|
710 |
if ((tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG)) != TcpHeader::RST) |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
711 |
{ // Since m_endPoint is not configured yet, we cannot use SendRST here |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
712 |
TcpHeader h; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
713 |
h.SetFlags (TcpHeader::RST); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
714 |
h.SetSequenceNumber (m_nextTxSequence); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
715 |
h.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
716 |
h.SetSourcePort (tcpHeader.GetDestinationPort ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
717 |
h.SetDestinationPort (tcpHeader.GetSourcePort ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
718 |
h.SetWindowSize (AdvertisedWindowSize ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
719 |
AddOptions (h); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
720 |
m_tcp->SendPacket (Create<Packet> (), h, header.GetDestination (), header.GetSource (), m_boundnetdevice); |
6694 | 721 |
} |
722 |
break; |
|
723 |
case SYN_SENT: |
|
724 |
ProcessSynSent (packet, tcpHeader); |
|
725 |
break; |
|
726 |
case SYN_RCVD: |
|
727 |
ProcessSynRcvd (packet, tcpHeader, fromAddress, toAddress); |
|
728 |
break; |
|
729 |
case FIN_WAIT_1: |
|
730 |
case FIN_WAIT_2: |
|
731 |
case CLOSE_WAIT: |
|
732 |
ProcessWait (packet, tcpHeader); |
|
733 |
break; |
|
734 |
case CLOSING: |
|
735 |
ProcessClosing (packet, tcpHeader); |
|
736 |
break; |
|
737 |
case LAST_ACK: |
|
738 |
ProcessLastAck (packet, tcpHeader); |
|
739 |
break; |
|
740 |
default: // mute compiler |
|
741 |
break; |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
/** Received a packet upon ESTABLISHED state. This function is mimicking the |
|
746 |
role of tcp_rcv_established() in tcp_input.c in Linux kernel. */ |
|
747 |
void |
|
748 |
TcpSocketBase::ProcessEstablished (Ptr<Packet> packet, const TcpHeader& tcpHeader) |
|
749 |
{ |
|
750 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
751 |
||
752 |
// Extract the flags. PSH and URG are not honoured. |
|
753 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
754 |
||
755 |
// Different flags are different events |
|
756 |
if (tcpflags == TcpHeader::ACK) |
|
757 |
{ |
|
758 |
ReceivedAck (packet, tcpHeader); |
|
759 |
} |
|
760 |
else if (tcpflags == TcpHeader::SYN) |
|
761 |
{ // Received SYN, old NS-3 behaviour is to set state to SYN_RCVD and |
|
762 |
// respond with a SYN+ACK. But it is not a legal state transition as of |
|
763 |
// RFC793. Thus this is ignored. |
|
764 |
} |
|
765 |
else if (tcpflags == (TcpHeader::SYN | TcpHeader::ACK)) |
|
766 |
{ // No action for received SYN+ACK, it is probably a duplicated packet |
|
767 |
} |
|
768 |
else if (tcpflags == TcpHeader::FIN || tcpflags == (TcpHeader::FIN | TcpHeader::ACK)) |
|
769 |
{ // Received FIN or FIN+ACK, bring down this socket nicely |
|
770 |
PeerClose (packet, tcpHeader); |
|
771 |
} |
|
772 |
else if (tcpflags == 0) |
|
773 |
{ // No flags means there is only data |
|
774 |
ReceivedData (packet, tcpHeader); |
|
775 |
if (m_rxBuffer.Finished ()) |
|
776 |
{ |
|
777 |
PeerClose (packet, tcpHeader); |
|
778 |
} |
|
779 |
} |
|
780 |
else |
|
781 |
{ // Received RST or the TCP flags is invalid, in either case, terminate this socket |
|
782 |
if (tcpflags != TcpHeader::RST) |
|
783 |
{ // this must be an invalid flag, send reset |
|
784 |
NS_LOG_LOGIC ("Illegal flag " << tcpflags << " received. Reset packet is sent."); |
|
785 |
SendRST (); |
|
786 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
787 |
CloseAndNotify (); |
6694 | 788 |
} |
789 |
} |
|
790 |
||
791 |
/** Process the newly received ACK */ |
|
792 |
void |
|
793 |
TcpSocketBase::ReceivedAck (Ptr<Packet> packet, const TcpHeader& tcpHeader) |
|
794 |
{ |
|
795 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
796 |
||
797 |
// Received ACK. Compare the ACK number against highest unacked seqno |
|
798 |
if (0 == (tcpHeader.GetFlags () & TcpHeader::ACK)) |
|
799 |
{ // Ignore if no ACK flag |
|
800 |
} |
|
801 |
else if (tcpHeader.GetAckNumber () < m_txBuffer.HeadSequence ()) |
|
802 |
{ // Case 1: Old ACK, ignored. |
|
803 |
NS_LOG_LOGIC ("Ignored ack of " << tcpHeader.GetAckNumber ()); |
|
804 |
} |
|
805 |
else if (tcpHeader.GetAckNumber () == m_txBuffer.HeadSequence ()) |
|
806 |
{ // Case 2: Potentially a duplicated ACK |
|
807 |
if (tcpHeader.GetAckNumber () < m_nextTxSequence) |
|
808 |
{ |
|
809 |
NS_LOG_LOGIC ("Dupack of " << tcpHeader.GetAckNumber ()); |
|
810 |
DupAck (tcpHeader, ++m_dupAckCount); |
|
811 |
} |
|
812 |
// otherwise, the ACK is precisely equal to the nextTxSequence |
|
813 |
NS_ASSERT (tcpHeader.GetAckNumber () <= m_nextTxSequence); |
|
814 |
} |
|
815 |
else if (tcpHeader.GetAckNumber () > m_txBuffer.HeadSequence ()) |
|
816 |
{ // Case 3: New ACK, reset m_dupAckCount and update m_txBuffer |
|
817 |
NS_LOG_LOGIC ("New ack of " << tcpHeader.GetAckNumber ()); |
|
818 |
NewAck (tcpHeader.GetAckNumber ()); |
|
819 |
m_dupAckCount = 0; |
|
820 |
} |
|
821 |
// If there is any data piggybacked, store it into m_rxBuffer |
|
822 |
if (packet->GetSize () > 0) |
|
823 |
{ |
|
824 |
ReceivedData (packet, tcpHeader); |
|
825 |
} |
|
826 |
} |
|
827 |
||
828 |
/** Received a packet upon LISTEN state. */ |
|
829 |
void |
|
830 |
TcpSocketBase::ProcessListen (Ptr<Packet> packet, const TcpHeader& tcpHeader, |
|
831 |
const Address& fromAddress, const Address& toAddress) |
|
832 |
{ |
|
833 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
834 |
||
835 |
// Extract the flags. PSH and URG are not honoured. |
|
836 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
837 |
||
838 |
// Fork a socket if received a SYN. Do nothing otherwise. |
|
839 |
// C.f.: the LISTEN part in tcp_v4_do_rcv() in tcp_ipv4.c in Linux kernel |
|
840 |
if (tcpflags != TcpHeader::SYN) return; |
|
841 |
||
842 |
// Call socket's notify function to let the server app know we got a SYN |
|
843 |
// If the server app refuses the connection, do nothing |
|
844 |
if (!NotifyConnectionRequest (fromAddress)) return; |
|
845 |
// Clone the socket, simulate fork |
|
846 |
Ptr<TcpSocketBase> newSock = Fork (); |
|
847 |
NS_LOG_LOGIC ("Cloned a TcpSocketBase " << newSock); |
|
848 |
Simulator::ScheduleNow (&TcpSocketBase::CompleteFork, newSock, |
|
849 |
packet, tcpHeader, fromAddress, toAddress); |
|
850 |
} |
|
851 |
||
852 |
/** Received a packet upon SYN_SENT */ |
|
853 |
void |
|
854 |
TcpSocketBase::ProcessSynSent (Ptr<Packet> packet, const TcpHeader& tcpHeader) |
|
855 |
{ |
|
856 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
857 |
||
858 |
// Extract the flags. PSH and URG are not honoured. |
|
859 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
860 |
||
861 |
if (tcpflags == 0) |
|
862 |
{ // Bare data, accept it and move to ESTABLISHED state. This is not a normal behaviour. Remove this? |
|
863 |
NS_LOG_INFO ("SYN_SENT -> ESTABLISHED"); |
|
864 |
m_state = ESTABLISHED; |
|
865 |
m_connected = true; |
|
866 |
m_retxEvent.Cancel (); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
867 |
m_delAckCount = m_delAckMaxCount; |
6694 | 868 |
ReceivedData (packet, tcpHeader); |
869 |
Simulator::ScheduleNow (&TcpSocketBase::ConnectionSucceeded, this); |
|
870 |
} |
|
871 |
else if (tcpflags == TcpHeader::ACK) |
|
872 |
{ // Ignore ACK in SYN_SENT |
|
873 |
} |
|
874 |
else if (tcpflags == TcpHeader::SYN) |
|
875 |
{ // Received SYN, move to SYN_RCVD state and respond with SYN+ACK |
|
876 |
NS_LOG_INFO ("SYN_SENT -> SYN_RCVD"); |
|
877 |
m_state = SYN_RCVD; |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
878 |
m_cnCount = m_cnRetries; |
6694 | 879 |
m_rxBuffer.SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
880 |
SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
|
881 |
} |
|
882 |
else if (tcpflags == (TcpHeader::SYN | TcpHeader::ACK) |
|
883 |
&& m_nextTxSequence + SequenceNumber32 (1) == tcpHeader.GetAckNumber ()) |
|
884 |
{ // Handshake completed |
|
885 |
NS_LOG_INFO ("SYN_SENT -> ESTABLISHED"); |
|
886 |
m_state = ESTABLISHED; |
|
887 |
m_connected = true; |
|
888 |
m_retxEvent.Cancel (); |
|
889 |
m_rxBuffer.SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
|
890 |
m_highTxMark = ++m_nextTxSequence; |
|
891 |
m_txBuffer.SetHeadSequence (m_nextTxSequence); |
|
892 |
SendEmptyPacket (TcpHeader::ACK); |
|
893 |
SendPendingData (m_connected); |
|
894 |
Simulator::ScheduleNow (&TcpSocketBase::ConnectionSucceeded, this); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
895 |
// Always respond to first data packet to speed up the connection. |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
896 |
// Remove to get the behaviour of old NS-3 code. |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
897 |
m_delAckCount = m_delAckMaxCount; |
6694 | 898 |
} |
899 |
else |
|
900 |
{ // Other in-sequence input |
|
901 |
if (tcpflags != TcpHeader::RST) |
|
902 |
{ // When (1) rx of FIN+ACK; (2) rx of FIN; (3) rx of bad flags |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
903 |
NS_LOG_LOGIC ("Illegal flag " << std::hex << static_cast<uint32_t>(tcpflags) << std::dec << " received. Reset packet is sent."); |
6694 | 904 |
SendRST (); |
905 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
906 |
CloseAndNotify (); |
6694 | 907 |
} |
908 |
} |
|
909 |
||
910 |
/** Received a packet upon SYN_RCVD */ |
|
911 |
void |
|
912 |
TcpSocketBase::ProcessSynRcvd (Ptr<Packet> packet, const TcpHeader& tcpHeader, |
|
913 |
const Address& fromAddress, const Address& toAddress) |
|
914 |
{ |
|
915 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
916 |
||
917 |
// Extract the flags. PSH and URG are not honoured. |
|
918 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
919 |
||
920 |
if (tcpflags == 0 || |
|
921 |
(tcpflags == TcpHeader::ACK |
|
922 |
&& m_nextTxSequence + SequenceNumber32 (1) == tcpHeader.GetAckNumber ())) |
|
923 |
{ // If it is bare data, accept it and move to ESTABLISHED state. This is |
|
924 |
// possibly due to ACK lost in 3WHS. If in-sequence ACK is received, the |
|
925 |
// handshake is completed nicely. |
|
926 |
NS_LOG_INFO ("SYN_RCVD -> ESTABLISHED"); |
|
927 |
m_state = ESTABLISHED; |
|
928 |
m_connected = true; |
|
929 |
m_retxEvent.Cancel (); |
|
930 |
m_highTxMark = ++m_nextTxSequence; |
|
931 |
m_txBuffer.SetHeadSequence (m_nextTxSequence); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
932 |
m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
933 |
InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
6694 | 934 |
// Always respond to first data packet to speed up the connection. |
935 |
// Remove to get the behaviour of old NS-3 code. |
|
936 |
m_delAckCount = m_delAckMaxCount; |
|
937 |
ReceivedAck (packet, tcpHeader); |
|
938 |
NotifyNewConnectionCreated (this, fromAddress); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
939 |
// As this connection is established, the socket is available to send data now |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
940 |
if (GetTxAvailable () > 0) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
941 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
942 |
NotifySend (GetTxAvailable ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
943 |
} |
6694 | 944 |
} |
945 |
else if (tcpflags == TcpHeader::SYN) |
|
946 |
{ // Probably the peer lost my SYN+ACK |
|
947 |
m_rxBuffer.SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
|
948 |
SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
|
949 |
} |
|
950 |
else if (tcpflags == (TcpHeader::FIN | TcpHeader::ACK)) |
|
951 |
{ |
|
952 |
if (tcpHeader.GetSequenceNumber () == m_rxBuffer.NextRxSequence ()) |
|
953 |
{ // In-sequence FIN before connection complete. Set up connection and close. |
|
954 |
m_connected = true; |
|
955 |
m_retxEvent.Cancel (); |
|
956 |
m_highTxMark = ++m_nextTxSequence; |
|
957 |
m_txBuffer.SetHeadSequence (m_nextTxSequence); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
958 |
m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
959 |
InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
6694 | 960 |
PeerClose (packet, tcpHeader); |
961 |
} |
|
962 |
} |
|
963 |
else |
|
964 |
{ // Other in-sequence input |
|
965 |
if (tcpflags != TcpHeader::RST) |
|
966 |
{ // When (1) rx of SYN+ACK; (2) rx of FIN; (3) rx of bad flags |
|
967 |
NS_LOG_LOGIC ("Illegal flag " << tcpflags << " received. Reset packet is sent."); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
968 |
m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
969 |
InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
6694 | 970 |
SendRST (); |
971 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
972 |
CloseAndNotify (); |
6694 | 973 |
} |
974 |
} |
|
975 |
||
976 |
/** Received a packet upon CLOSE_WAIT, FIN_WAIT_1, or FIN_WAIT_2 states */ |
|
977 |
void |
|
978 |
TcpSocketBase::ProcessWait (Ptr<Packet> packet, const TcpHeader& tcpHeader) |
|
979 |
{ |
|
980 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
981 |
||
982 |
// Extract the flags. PSH and URG are not honoured. |
|
983 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
984 |
||
985 |
if (packet->GetSize () > 0) |
|
986 |
{ // Bare data, accept it |
|
987 |
ReceivedData (packet, tcpHeader); |
|
988 |
} |
|
989 |
else if (tcpflags == TcpHeader::ACK) |
|
990 |
{ // Process the ACK, and if in FIN_WAIT_1, conditionally move to FIN_WAIT_2 |
|
991 |
ReceivedAck (packet, tcpHeader); |
|
992 |
if (m_state == FIN_WAIT_1 && m_txBuffer.Size () == 0 && |
|
993 |
tcpHeader.GetAckNumber () == m_highTxMark + SequenceNumber32 (1)) |
|
994 |
{ // This ACK corresponds to the FIN sent |
|
995 |
NS_LOG_INFO ("FIN_WAIT_1 -> FIN_WAIT_2"); |
|
996 |
m_state = FIN_WAIT_2; |
|
997 |
} |
|
998 |
} |
|
999 |
else if (tcpflags == TcpHeader::FIN || tcpflags == (TcpHeader::FIN | TcpHeader::ACK)) |
|
1000 |
{ // Got FIN, respond with ACK and move to next state |
|
1001 |
if (tcpflags & TcpHeader::ACK) |
|
1002 |
{ // Process the ACK first |
|
1003 |
ReceivedAck (packet, tcpHeader); |
|
1004 |
} |
|
1005 |
m_rxBuffer.SetFinSequence (tcpHeader.GetSequenceNumber ()); |
|
1006 |
} |
|
1007 |
else if (tcpflags == TcpHeader::SYN || tcpflags == (TcpHeader::SYN | TcpHeader::ACK)) |
|
1008 |
{ // Duplicated SYN or SYN+ACK, possibly due to spurious retransmission |
|
1009 |
return; |
|
1010 |
} |
|
1011 |
else |
|
1012 |
{ // This is a RST or bad flags |
|
1013 |
if (tcpflags != TcpHeader::RST) |
|
1014 |
{ |
|
1015 |
NS_LOG_LOGIC ("Illegal flag " << tcpflags << " received. Reset packet is sent."); |
|
1016 |
SendRST (); |
|
1017 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1018 |
CloseAndNotify (); |
6694 | 1019 |
return; |
1020 |
} |
|
1021 |
||
1022 |
// Check if the close responder sent an in-sequence FIN, if so, respond ACK |
|
1023 |
if ((m_state == FIN_WAIT_1 || m_state == FIN_WAIT_2) && m_rxBuffer.Finished ()) |
|
1024 |
{ |
|
1025 |
if (m_state == FIN_WAIT_1) |
|
1026 |
{ |
|
1027 |
NS_LOG_INFO ("FIN_WAIT_1 -> CLOSING"); |
|
1028 |
m_state = CLOSING; |
|
1029 |
if (m_txBuffer.Size () == 0 && |
|
1030 |
tcpHeader.GetAckNumber () == m_highTxMark + SequenceNumber32 (1)) |
|
1031 |
{ // This ACK corresponds to the FIN sent |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1032 |
TimeWait (); |
6694 | 1033 |
} |
1034 |
} |
|
1035 |
else if (m_state == FIN_WAIT_2) |
|
1036 |
{ |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1037 |
TimeWait (); |
6694 | 1038 |
}; |
1039 |
SendEmptyPacket (TcpHeader::ACK); |
|
1040 |
if (!m_shutdownRecv) NotifyDataRecv (); |
|
1041 |
} |
|
1042 |
} |
|
1043 |
||
1044 |
/** Received a packet upon CLOSING */ |
|
1045 |
void |
|
1046 |
TcpSocketBase::ProcessClosing (Ptr<Packet> packet, const TcpHeader& tcpHeader) |
|
1047 |
{ |
|
1048 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
1049 |
||
1050 |
// Extract the flags. PSH and URG are not honoured. |
|
1051 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
1052 |
||
1053 |
if (tcpflags == TcpHeader::ACK) |
|
1054 |
{ |
|
1055 |
if (tcpHeader.GetSequenceNumber () == m_rxBuffer.NextRxSequence ()) |
|
1056 |
{ // This ACK corresponds to the FIN sent |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1057 |
TimeWait (); |
6694 | 1058 |
} |
1059 |
} |
|
1060 |
else |
|
1061 |
{ // CLOSING state means simultaneous close, i.e. no one is sending data to |
|
1062 |
// anyone. If anything other than ACK is received, respond with a reset. |
|
1063 |
if (tcpflags == TcpHeader::FIN || tcpflags == (TcpHeader::FIN | TcpHeader::ACK)) |
|
1064 |
{ // FIN from the peer as well. We can close immediately. |
|
1065 |
SendEmptyPacket (TcpHeader::ACK); |
|
1066 |
} |
|
1067 |
else if (tcpflags != TcpHeader::RST) |
|
1068 |
{ // Receive of SYN or SYN+ACK or bad flags or pure data |
|
1069 |
NS_LOG_LOGIC ("Illegal flag " << tcpflags << " received. Reset packet is sent."); |
|
1070 |
SendRST (); |
|
1071 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1072 |
CloseAndNotify (); |
6694 | 1073 |
} |
1074 |
} |
|
1075 |
||
1076 |
/** Received a packet upon LAST_ACK */ |
|
1077 |
void |
|
1078 |
TcpSocketBase::ProcessLastAck (Ptr<Packet> packet, const TcpHeader& tcpHeader) |
|
1079 |
{ |
|
1080 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
1081 |
||
1082 |
// Extract the flags. PSH and URG are not honoured. |
|
1083 |
uint8_t tcpflags = tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG); |
|
1084 |
||
1085 |
if (tcpflags == 0) |
|
1086 |
{ |
|
1087 |
ReceivedData (packet, tcpHeader); |
|
1088 |
} |
|
1089 |
else if (tcpflags == TcpHeader::ACK) |
|
1090 |
{ |
|
1091 |
if (tcpHeader.GetSequenceNumber () == m_rxBuffer.NextRxSequence ()) |
|
1092 |
{ // This ACK corresponds to the FIN sent. This socket closed peacefully. |
|
1093 |
CloseAndNotify (); |
|
1094 |
} |
|
1095 |
} |
|
1096 |
else if (tcpflags == TcpHeader::FIN) |
|
1097 |
{ // Received FIN again, the peer probably lost the FIN+ACK |
|
1098 |
SendEmptyPacket (TcpHeader::FIN | TcpHeader::ACK); |
|
1099 |
} |
|
1100 |
else if (tcpflags == (TcpHeader::FIN | TcpHeader::ACK) || tcpflags == TcpHeader::RST) |
|
1101 |
{ |
|
1102 |
CloseAndNotify (); |
|
1103 |
} |
|
1104 |
else |
|
1105 |
{ // Received a SYN or SYN+ACK or bad flags |
|
1106 |
NS_LOG_LOGIC ("Illegal flag " << tcpflags << " received. Reset packet is sent."); |
|
1107 |
SendRST (); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1108 |
CloseAndNotify (); |
6694 | 1109 |
} |
1110 |
} |
|
1111 |
||
1112 |
/** Peer sent me a FIN. Remember its sequence in rx buffer. */ |
|
1113 |
void |
|
1114 |
TcpSocketBase::PeerClose (Ptr<Packet> p, const TcpHeader& tcpHeader) |
|
1115 |
{ |
|
1116 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
1117 |
||
1118 |
// Ignore all out of range packets |
|
1119 |
if (tcpHeader.GetSequenceNumber () < m_rxBuffer.NextRxSequence () || |
|
1120 |
tcpHeader.GetSequenceNumber () > m_rxBuffer.MaxRxSequence ()) |
|
1121 |
{ |
|
1122 |
return; |
|
1123 |
}; |
|
1124 |
// For any case, remember the FIN position in rx buffer first |
|
1125 |
m_rxBuffer.SetFinSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (p->GetSize ())); |
|
1126 |
NS_LOG_LOGIC ("Accepted FIN at seq " << tcpHeader.GetSequenceNumber () + SequenceNumber32 (p->GetSize ())); |
|
1127 |
// If there is any piggybacked data, process it |
|
1128 |
if (p->GetSize ()) |
|
1129 |
{ |
|
1130 |
ReceivedData (p, tcpHeader); |
|
1131 |
} |
|
1132 |
// Return if FIN is out of sequence, otherwise move to CLOSE_WAIT state by DoPeerClose |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
7045
diff
changeset
|
1133 |
if (!m_rxBuffer.Finished ()) |
6694 | 1134 |
{ |
1135 |
return; |
|
1136 |
}; |
|
1137 |
||
1138 |
// Simultaneous close: Application invoked Close() when we are processing this FIN packet |
|
1139 |
if (m_state == FIN_WAIT_1) |
|
1140 |
{ |
|
1141 |
NS_LOG_INFO ("FIN_WAIT_1 -> CLOSING"); |
|
1142 |
m_state = CLOSING; |
|
1143 |
return; |
|
1144 |
} |
|
1145 |
||
1146 |
DoPeerClose (); // Change state, respond with ACK |
|
1147 |
} |
|
1148 |
||
1149 |
/** Received a in-sequence FIN. Close down this socket. */ |
|
1150 |
void |
|
1151 |
TcpSocketBase::DoPeerClose (void) |
|
1152 |
{ |
|
1153 |
NS_ASSERT (m_state == ESTABLISHED || m_state == SYN_RCVD); |
|
1154 |
||
1155 |
// Move the state to CLOSE_WAIT |
|
1156 |
NS_LOG_INFO (TcpStateName[m_state] << " -> CLOSE_WAIT"); |
|
1157 |
m_state = CLOSE_WAIT; |
|
1158 |
||
1159 |
if (!m_closeNotified) |
|
1160 |
{ |
|
1161 |
// The normal behaviour for an application is that, when the peer sent a in-sequence |
|
1162 |
// FIN, the app should prepare to close. The app has two choices at this point: either |
|
1163 |
// respond with ShutdownSend() call to declare that it has nothing more to send and |
|
1164 |
// the socket can be closed immediately; or remember the peer's close request, wait |
|
1165 |
// until all its existing data are pushed into the TCP socket, then call Close() |
|
1166 |
// explicitly. |
|
1167 |
NS_LOG_LOGIC ("TCP " << this << " calling NotifyNormalClose"); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1168 |
NotifyNormalClose (); |
6694 | 1169 |
m_closeNotified = true; |
1170 |
} |
|
1171 |
if (m_shutdownSend) |
|
1172 |
{ // The application declares that it would not sent any more, close this socket |
|
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7252
diff
changeset
|
1173 |
Close (); |
6694 | 1174 |
} |
1175 |
else |
|
1176 |
{ // Need to ack, the application will close later |
|
1177 |
SendEmptyPacket (TcpHeader::ACK); |
|
1178 |
} |
|
1179 |
if (m_state == LAST_ACK) |
|
1180 |
{ |
|
1181 |
NS_LOG_LOGIC ("TcpSocketBase " << this << " scheduling LATO1"); |
|
1182 |
m_lastAckEvent = Simulator::Schedule (m_rtt->RetransmitTimeout (), |
|
1183 |
&TcpSocketBase::LastAckTimeout, this); |
|
1184 |
} |
|
1185 |
} |
|
1186 |
||
1187 |
/** Kill this socket. This is a callback function configured to m_endpoint in |
|
1188 |
SetupCallback(), invoked when the endpoint is destroyed. */ |
|
1189 |
void |
|
1190 |
TcpSocketBase::Destroy (void) |
|
1191 |
{ |
|
1192 |
NS_LOG_FUNCTION (this); |
|
1193 |
m_node = 0; |
|
1194 |
m_endPoint = 0; |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1195 |
std::vector<Ptr<TcpSocketBase> >::iterator it |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1196 |
= std::find (m_tcp->m_sockets.begin (), m_tcp->m_sockets.end (), this); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1197 |
if (it != m_tcp->m_sockets.end ()) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1198 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1199 |
m_tcp->m_sockets.erase (it); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1200 |
} |
6694 | 1201 |
m_tcp = 0; |
1202 |
NS_LOG_LOGIC (this << " Cancelled ReTxTimeout event which was set to expire at " << |
|
1203 |
(Simulator::Now () + Simulator::GetDelayLeft (m_retxEvent)).GetSeconds ()); |
|
1204 |
CancelAllTimers (); |
|
1205 |
} |
|
1206 |
||
1207 |
/** Send an empty packet with specified TCP flags */ |
|
1208 |
void |
|
1209 |
TcpSocketBase::SendEmptyPacket (uint8_t flags) |
|
1210 |
{ |
|
1211 |
NS_LOG_FUNCTION (this << (uint32_t)flags); |
|
1212 |
Ptr<Packet> p = Create<Packet> (); |
|
1213 |
TcpHeader header; |
|
1214 |
SequenceNumber32 s = m_nextTxSequence; |
|
1215 |
||
1216 |
if (m_endPoint == 0) |
|
1217 |
{ |
|
1218 |
NS_LOG_WARN ("Failed to send empty packet due to null endpoint"); |
|
1219 |
return; |
|
1220 |
} |
|
1221 |
if (flags & TcpHeader::FIN) |
|
1222 |
{ |
|
1223 |
flags |= TcpHeader::ACK; |
|
1224 |
} |
|
1225 |
else if (m_state == FIN_WAIT_1 || m_state == LAST_ACK || m_state == CLOSING) |
|
1226 |
{ |
|
1227 |
++s; |
|
1228 |
} |
|
1229 |
||
1230 |
header.SetFlags (flags); |
|
1231 |
header.SetSequenceNumber (s); |
|
1232 |
header.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
|
1233 |
header.SetSourcePort (m_endPoint->GetLocalPort ()); |
|
1234 |
header.SetDestinationPort (m_endPoint->GetPeerPort ()); |
|
1235 |
header.SetWindowSize (AdvertisedWindowSize ()); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1236 |
AddOptions (header); |
6694 | 1237 |
m_rto = m_rtt->RetransmitTimeout (); |
1238 |
bool hasSyn = flags & TcpHeader::SYN; |
|
1239 |
bool hasFin = flags & TcpHeader::FIN; |
|
1240 |
bool isAck = flags == TcpHeader::ACK; |
|
1241 |
if (hasSyn) |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1242 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1243 |
if (m_cnCount == 0) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1244 |
{ // No more connection retries, give up |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1245 |
NS_LOG_LOGIC ("Connection failed."); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1246 |
CloseAndNotify (); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1247 |
return; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1248 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1249 |
else |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1250 |
{ // Exponential backoff of connection time out |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1251 |
int backoffCount = 0x1 << (m_cnRetries - m_cnCount); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1252 |
m_rto = m_cnTimeout * backoffCount; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1253 |
m_cnCount--; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1254 |
} |
6694 | 1255 |
} |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1256 |
m_tcp->SendPacket (p, header, m_endPoint->GetLocalAddress (), m_endPoint->GetPeerAddress (), m_boundnetdevice); |
6694 | 1257 |
if (flags & TcpHeader::ACK) |
1258 |
{ // If sending an ACK, cancel the delay ACK as well |
|
1259 |
m_delAckEvent.Cancel (); |
|
1260 |
m_delAckCount = 0; |
|
1261 |
} |
|
1262 |
if (m_retxEvent.IsExpired () && (hasSyn || hasFin) && !isAck ) |
|
1263 |
{ // Retransmit SYN / SYN+ACK / FIN / FIN+ACK to guard against lost |
|
1264 |
NS_LOG_LOGIC ("Schedule retransmission timeout at time " |
|
1265 |
<< Simulator::Now ().GetSeconds () << " to expire at time " |
|
1266 |
<< (Simulator::Now () + m_rto.Get ()).GetSeconds ()); |
|
1267 |
m_retxEvent = Simulator::Schedule (m_rto, &TcpSocketBase::SendEmptyPacket, this, flags); |
|
1268 |
} |
|
1269 |
} |
|
1270 |
||
1271 |
/** This function closes the endpoint completely. Called upon RST_TX action. */ |
|
1272 |
void |
|
1273 |
TcpSocketBase::SendRST (void) |
|
1274 |
{ |
|
1275 |
NS_LOG_FUNCTION (this); |
|
1276 |
SendEmptyPacket (TcpHeader::RST); |
|
1277 |
NotifyErrorClose (); |
|
1278 |
DeallocateEndPoint (); |
|
1279 |
} |
|
1280 |
||
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1281 |
/** Deallocate the end point and cancel all the timers */ |
6694 | 1282 |
void |
1283 |
TcpSocketBase::DeallocateEndPoint (void) |
|
1284 |
{ |
|
1285 |
if (m_endPoint != 0) |
|
1286 |
{ |
|
1287 |
m_endPoint->SetDestroyCallback (MakeNullCallback<void> ()); |
|
1288 |
m_tcp->DeAllocate (m_endPoint); |
|
1289 |
m_endPoint = 0; |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1290 |
std::vector<Ptr<TcpSocketBase> >::iterator it |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1291 |
= std::find (m_tcp->m_sockets.begin (), m_tcp->m_sockets.end (), this); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1292 |
if (it != m_tcp->m_sockets.end ()) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1293 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1294 |
m_tcp->m_sockets.erase (it); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1295 |
} |
6694 | 1296 |
CancelAllTimers (); |
1297 |
} |
|
1298 |
} |
|
1299 |
||
1300 |
/** Configure the endpoint to a local address. Called by Connect() if Bind() didn't specify one. */ |
|
1301 |
int |
|
1302 |
TcpSocketBase::SetupEndpoint () |
|
1303 |
{ |
|
1304 |
NS_LOG_FUNCTION (this); |
|
1305 |
Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> (); |
|
1306 |
NS_ASSERT (ipv4 != 0); |
|
1307 |
if (ipv4->GetRoutingProtocol () == 0) |
|
1308 |
{ |
|
1309 |
NS_FATAL_ERROR ("No Ipv4RoutingProtocol in the node"); |
|
1310 |
} |
|
1311 |
// Create a dummy packet, then ask the routing function for the best output |
|
1312 |
// interface's address |
|
1313 |
Ipv4Header header; |
|
1314 |
header.SetDestination (m_endPoint->GetPeerAddress ()); |
|
1315 |
Socket::SocketErrno errno_; |
|
1316 |
Ptr<Ipv4Route> route; |
|
1317 |
Ptr<NetDevice> oif = m_boundnetdevice; |
|
1318 |
route = ipv4->GetRoutingProtocol ()->RouteOutput (Ptr<Packet> (), header, oif, errno_); |
|
1319 |
if (route == 0) |
|
1320 |
{ |
|
1321 |
NS_LOG_LOGIC ("Route to " << m_endPoint->GetPeerAddress () << " does not exist"); |
|
1322 |
NS_LOG_ERROR (errno_); |
|
1323 |
m_errno = errno_; |
|
1324 |
return -1; |
|
1325 |
} |
|
1326 |
NS_LOG_LOGIC ("Route exists"); |
|
1327 |
m_endPoint->SetLocalAddress (route->GetSource ()); |
|
1328 |
return 0; |
|
1329 |
} |
|
1330 |
||
1331 |
/** This function is called only if a SYN received in LISTEN state. After |
|
1332 |
TcpSocketBase cloned, allocate a new end point to handle the incoming |
|
1333 |
connection and send a SYN+ACK to complete the handshake. */ |
|
1334 |
void |
|
1335 |
TcpSocketBase::CompleteFork (Ptr<Packet> p, const TcpHeader& h, |
|
1336 |
const Address& fromAddress, const Address& toAddress) |
|
1337 |
{ |
|
1338 |
// Get port and address from peer (connecting host) |
|
1339 |
m_endPoint = m_tcp->Allocate (InetSocketAddress::ConvertFrom (toAddress).GetIpv4 (), |
|
1340 |
InetSocketAddress::ConvertFrom (toAddress).GetPort (), |
|
1341 |
InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
|
1342 |
InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1343 |
m_tcp->m_sockets.push_back (this); |
6694 | 1344 |
|
1345 |
// Change the cloned socket from LISTEN state to SYN_RCVD |
|
1346 |
NS_LOG_INFO ("LISTEN -> SYN_RCVD"); |
|
1347 |
m_state = SYN_RCVD; |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1348 |
m_cnCount = m_cnRetries; |
6694 | 1349 |
SetupCallback (); |
1350 |
// Set the sequence number and send SYN+ACK |
|
1351 |
m_rxBuffer.SetNextRxSequence (h.GetSequenceNumber () + SequenceNumber32 (1)); |
|
1352 |
SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
|
1353 |
} |
|
1354 |
||
1355 |
void |
|
1356 |
TcpSocketBase::ConnectionSucceeded () |
|
1357 |
{ // Wrapper to protected function NotifyConnectionSucceeded() so that it can |
|
1358 |
// be called as a scheduled event |
|
1359 |
NotifyConnectionSucceeded (); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1360 |
// The if-block below was moved from ProcessSynSent() to here because we need |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1361 |
// to invoke the NotifySend() only after NotifyConnectionSucceeded() to |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1362 |
// reflect the behaviour in the real world. |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1363 |
if (GetTxAvailable () > 0) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1364 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1365 |
NotifySend (GetTxAvailable ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1366 |
} |
6694 | 1367 |
} |
1368 |
||
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1369 |
/** Extract at most maxSize bytes from the TxBuffer at sequence seq, add the |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1370 |
TCP header, and send to TcpL4Protocol */ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1371 |
uint32_t |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1372 |
TcpSocketBase::SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1373 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1374 |
NS_LOG_FUNCTION (this << seq << maxSize << withAck); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1375 |
|
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1376 |
Ptr<Packet> p = m_txBuffer.CopyFromSequence (maxSize, seq); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1377 |
uint32_t sz = p->GetSize (); // Size of packet |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1378 |
uint8_t flags = withAck ? TcpHeader::ACK : 0; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1379 |
uint32_t remainingData = m_txBuffer.SizeFromSequence (seq + SequenceNumber32 (sz)); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1380 |
if (m_closeOnEmpty && (remainingData == 0)) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1381 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1382 |
flags |= TcpHeader::FIN; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1383 |
if (m_state == ESTABLISHED) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1384 |
{ // On active close: I am the first one to send FIN |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1385 |
NS_LOG_INFO ("ESTABLISHED -> FIN_WAIT_1"); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1386 |
m_state = FIN_WAIT_1; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1387 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1388 |
else if (m_state == CLOSE_WAIT) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1389 |
{ // On passive close: Peer sent me FIN already |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1390 |
NS_LOG_INFO ("CLOSE_WAIT -> LAST_ACK"); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1391 |
m_state = LAST_ACK; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1392 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1393 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1394 |
TcpHeader header; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1395 |
header.SetFlags (flags); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1396 |
header.SetSequenceNumber (seq); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1397 |
header.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1398 |
header.SetSourcePort (m_endPoint->GetLocalPort ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1399 |
header.SetDestinationPort (m_endPoint->GetPeerPort ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1400 |
header.SetWindowSize (AdvertisedWindowSize ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1401 |
AddOptions (header); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1402 |
if (m_retxEvent.IsExpired () ) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1403 |
{ // Schedule retransmit |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1404 |
m_rto = m_rtt->RetransmitTimeout (); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1405 |
NS_LOG_LOGIC (this << " SendDataPacket Schedule ReTxTimeout at time " << |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1406 |
Simulator::Now ().GetSeconds () << " to expire at time " << |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1407 |
(Simulator::Now () + m_rto.Get ()).GetSeconds () ); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1408 |
m_retxEvent = Simulator::Schedule (m_rto, &TcpSocketBase::ReTxTimeout, this); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1409 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1410 |
NS_LOG_LOGIC ("Send packet via TcpL4Protocol with flags 0x" << std::hex << static_cast<uint32_t> (flags) << std::dec); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1411 |
m_tcp->SendPacket (p, header, m_endPoint->GetLocalAddress (), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1412 |
m_endPoint->GetPeerAddress (), m_boundnetdevice); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1413 |
m_rtt->SentSeq (seq, sz); // notify the RTT |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1414 |
// Notify the application of the data being sent unless this is a retransmit |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1415 |
if (seq == m_nextTxSequence) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1416 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1417 |
Simulator::ScheduleNow (&TcpSocketBase::NotifyDataSent, this, sz); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1418 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1419 |
// Update highTxMark |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1420 |
m_highTxMark = std::max (seq + sz, m_highTxMark.Get ()); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1421 |
return sz; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1422 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1423 |
|
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1424 |
/** Send as much pending data as possible according to the Tx window. Note that |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1425 |
* this function did not implement the PSH flag |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1426 |
*/ |
6694 | 1427 |
bool |
1428 |
TcpSocketBase::SendPendingData (bool withAck) |
|
1429 |
{ |
|
1430 |
NS_LOG_FUNCTION (this << withAck); |
|
7252
c8200621e252
rerun check-style.py with uncrustify-0.58
Tom Henderson <tomh@tomh.org>
parents:
7223
diff
changeset
|
1431 |
if (m_txBuffer.Size () == 0) return false; // Nothing to send |
6694 | 1432 |
if (m_endPoint == 0) |
1433 |
{ |
|
6768
805f5fc7f670
remove stale doxygen and comments pertaining to TCP
Tom Henderson <tomh@tomh.org>
parents:
6738
diff
changeset
|
1434 |
NS_LOG_INFO ("TcpSocketBase::SendPendingData: No endpoint; m_shutdownSend=" << m_shutdownSend); |
6694 | 1435 |
return false; // Is this the right way to handle this condition? |
1436 |
} |
|
1437 |
uint32_t nPacketsSent = 0; |
|
1438 |
while (m_txBuffer.SizeFromSequence (m_nextTxSequence)) |
|
1439 |
{ |
|
1440 |
uint32_t w = AvailableWindow (); // Get available window size |
|
1441 |
NS_LOG_LOGIC ("TcpSocketBase " << this << " SendPendingData" << |
|
1442 |
" w " << w << |
|
1443 |
" rxwin " << m_rWnd << |
|
1444 |
" segsize " << m_segmentSize << |
|
1445 |
" nextTxSeq " << m_nextTxSequence << |
|
1446 |
" highestRxAck " << m_txBuffer.HeadSequence () << |
|
1447 |
" pd->Size " << m_txBuffer.Size () << |
|
1448 |
" pd->SFS " << m_txBuffer.SizeFromSequence (m_nextTxSequence)); |
|
1449 |
// Quit if send disallowed |
|
1450 |
if (m_shutdownSend) |
|
1451 |
{ |
|
1452 |
m_errno = ERROR_SHUTDOWN; |
|
1453 |
return false; |
|
1454 |
} |
|
1455 |
// Stop sending if we need to wait for a larger Tx window |
|
1456 |
if (w < m_segmentSize && m_txBuffer.SizeFromSequence (m_nextTxSequence) > w) |
|
1457 |
{ |
|
1458 |
break; // No more |
|
1459 |
} |
|
7619
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1460 |
// Nagle's algorithm (RFC896): Hold off sending if there is unacked data |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1461 |
// in the buffer and the amount of data to send is less than one segment |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1462 |
if (!m_noDelay && UnAckDataCount () > 0 && |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1463 |
m_txBuffer.SizeFromSequence (m_nextTxSequence) < m_segmentSize) |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1464 |
{ |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1465 |
NS_LOG_LOGIC ("Invoking Nagle's algorithm. Wait to send."); |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1466 |
break; |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1467 |
} |
6694 | 1468 |
uint32_t s = std::min (w, m_segmentSize); // Send no more than window |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1469 |
uint32_t sz = SendDataPacket (m_nextTxSequence, s, withAck); |
6694 | 1470 |
nPacketsSent++; // Count sent this loop |
1471 |
m_nextTxSequence += sz; // Advance next tx sequence |
|
1472 |
} |
|
1473 |
NS_LOG_LOGIC ("SendPendingData sent " << nPacketsSent << " packets"); |
|
1474 |
return (nPacketsSent > 0); |
|
1475 |
} |
|
1476 |
||
1477 |
uint32_t |
|
1478 |
TcpSocketBase::UnAckDataCount () |
|
1479 |
{ |
|
1480 |
NS_LOG_FUNCTION (this); |
|
1481 |
return m_nextTxSequence.Get () - m_txBuffer.HeadSequence (); |
|
1482 |
} |
|
1483 |
||
1484 |
uint32_t |
|
1485 |
TcpSocketBase::BytesInFlight () |
|
1486 |
{ |
|
1487 |
NS_LOG_FUNCTION (this); |
|
1488 |
return m_highTxMark.Get () - m_txBuffer.HeadSequence (); |
|
1489 |
} |
|
1490 |
||
1491 |
uint32_t |
|
1492 |
TcpSocketBase::Window () |
|
1493 |
{ |
|
1494 |
NS_LOG_FUNCTION (this); |
|
1495 |
return m_rWnd; |
|
1496 |
} |
|
1497 |
||
1498 |
uint32_t |
|
1499 |
TcpSocketBase::AvailableWindow () |
|
1500 |
{ |
|
1501 |
NS_LOG_FUNCTION_NOARGS (); |
|
1502 |
uint32_t unack = UnAckDataCount (); // Number of outstanding bytes |
|
1503 |
uint32_t win = Window (); // Number of bytes allowed to be outstanding |
|
1504 |
NS_LOG_LOGIC ("UnAckCount=" << unack << ", Win=" << win); |
|
1505 |
return (win < unack) ? 0 : (win - unack); |
|
1506 |
} |
|
1507 |
||
1508 |
uint16_t |
|
1509 |
TcpSocketBase::AdvertisedWindowSize () |
|
1510 |
{ |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1511 |
return std::min (m_rxBuffer.MaxBufferSize () - m_rxBuffer.Size (), (uint32_t)m_maxWinSize); |
6694 | 1512 |
} |
1513 |
||
1514 |
// Receipt of new packet, put into Rx buffer |
|
1515 |
void |
|
1516 |
TcpSocketBase::ReceivedData (Ptr<Packet> p, const TcpHeader& tcpHeader) |
|
1517 |
{ |
|
1518 |
NS_LOG_FUNCTION (this << tcpHeader); |
|
1519 |
NS_LOG_LOGIC ("seq " << tcpHeader.GetSequenceNumber () << |
|
1520 |
" ack " << tcpHeader.GetAckNumber () << |
|
1521 |
" pkt size " << p->GetSize () ); |
|
1522 |
||
1523 |
// Put into Rx buffer |
|
1524 |
SequenceNumber32 expectedSeq = m_rxBuffer.NextRxSequence (); |
|
1525 |
if (!m_rxBuffer.Add (p, tcpHeader)) |
|
1526 |
{ // Insert failed: No data or RX buffer full |
|
1527 |
SendEmptyPacket (TcpHeader::ACK); |
|
1528 |
return; |
|
1529 |
} |
|
1530 |
// Now send a new ACK packet acknowledging all received and delivered data |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1531 |
if (m_rxBuffer.Size () > m_rxBuffer.Available () || m_rxBuffer.NextRxSequence () > expectedSeq + p->GetSize ()) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1532 |
{ // A gap exists in the buffer, or we filled a gap: Always ACK |
6694 | 1533 |
SendEmptyPacket (TcpHeader::ACK); |
1534 |
} |
|
1535 |
else |
|
1536 |
{ // In-sequence packet: ACK if delayed ack count allows |
|
1537 |
if (++m_delAckCount >= m_delAckMaxCount) |
|
1538 |
{ |
|
1539 |
m_delAckEvent.Cancel (); |
|
1540 |
m_delAckCount = 0; |
|
1541 |
SendEmptyPacket (TcpHeader::ACK); |
|
1542 |
} |
|
1543 |
else if (m_delAckEvent.IsExpired ()) |
|
1544 |
{ |
|
1545 |
m_delAckEvent = Simulator::Schedule (m_delAckTimeout, |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
7045
diff
changeset
|
1546 |
&TcpSocketBase::DelAckTimeout, this); |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1547 |
NS_LOG_LOGIC (this << " scheduled delayed ACK at " << (Simulator::Now () + Simulator::GetDelayLeft (m_delAckEvent)).GetSeconds ()); |
6694 | 1548 |
} |
1549 |
} |
|
1550 |
// Notify app to receive if necessary |
|
1551 |
if (expectedSeq < m_rxBuffer.NextRxSequence ()) |
|
1552 |
{ // NextRxSeq advanced, we have something to send to the app |
|
1553 |
if (!m_shutdownRecv) NotifyDataRecv (); |
|
1554 |
// Handle exceptions |
|
1555 |
if (m_closeNotified) |
|
1556 |
{ |
|
1557 |
NS_LOG_WARN ("Why TCP " << this << " got data after close notification?"); |
|
1558 |
} |
|
1559 |
// If we received FIN before and now completed all "holes" in rx buffer, |
|
1560 |
// invoke peer close procedure |
|
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7252
diff
changeset
|
1561 |
if (m_rxBuffer.Finished () && (tcpHeader.GetFlags () & TcpHeader::FIN) == 0) |
6694 | 1562 |
{ |
1563 |
DoPeerClose (); |
|
1564 |
} |
|
1565 |
} |
|
1566 |
} |
|
1567 |
||
1568 |
/** Called by ForwardUp() to estimate RTT */ |
|
1569 |
void |
|
1570 |
TcpSocketBase::EstimateRtt (const TcpHeader& tcpHeader) |
|
1571 |
{ |
|
1572 |
// Use m_rtt for the estimation. Note, RTT of duplicated acknowledgement |
|
1573 |
// (which should be ignored) is handled by m_rtt. Once timestamp option |
|
1574 |
// is implemented, this function would be more elaborated. |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1575 |
m_lastRtt = m_rtt->AckSeq (tcpHeader.GetAckNumber () ); |
6694 | 1576 |
}; |
1577 |
||
1578 |
// Called by the ReceivedAck() when new ACK received and by ProcessSynRcvd() |
|
1579 |
// when the three-way handshake completed. This cancels retransmission timer |
|
1580 |
// and advances Tx window |
|
1581 |
void |
|
1582 |
TcpSocketBase::NewAck (SequenceNumber32 const& ack) |
|
1583 |
{ |
|
1584 |
NS_LOG_FUNCTION (this << ack); |
|
1585 |
||
1586 |
if (m_state != SYN_RCVD) |
|
1587 |
{ // Set RTO unless the ACK is received in SYN_RCVD state |
|
1588 |
NS_LOG_LOGIC (this << " Cancelled ReTxTimeout event which was set to expire at " << |
|
1589 |
(Simulator::Now () + Simulator::GetDelayLeft (m_retxEvent)).GetSeconds ()); |
|
1590 |
m_retxEvent.Cancel (); |
|
1591 |
// On recieving a "New" ack we restart retransmission timer .. RFC 2988 |
|
1592 |
m_rto = m_rtt->RetransmitTimeout (); |
|
1593 |
NS_LOG_LOGIC (this << " Schedule ReTxTimeout at time " << |
|
1594 |
Simulator::Now ().GetSeconds () << " to expire at time " << |
|
1595 |
(Simulator::Now () + m_rto.Get ()).GetSeconds ()); |
|
1596 |
m_retxEvent = Simulator::Schedule (m_rto, &TcpSocketBase::ReTxTimeout, this); |
|
1597 |
} |
|
1598 |
if (m_rWnd.Get () == 0 && m_persistEvent.IsExpired ()) |
|
1599 |
{ // Zero window: Enter persist state to send 1 byte to probe |
|
1600 |
NS_LOG_LOGIC (this << "Enter zerowindow persist state"); |
|
1601 |
NS_LOG_LOGIC (this << "Cancelled ReTxTimeout event which was set to expire at " << |
|
1602 |
(Simulator::Now () + Simulator::GetDelayLeft (m_retxEvent)).GetSeconds ()); |
|
1603 |
m_retxEvent.Cancel (); |
|
1604 |
NS_LOG_LOGIC ("Schedule persist timeout at time " << |
|
1605 |
Simulator::Now ().GetSeconds () << " to expire at time " << |
|
1606 |
(Simulator::Now () + m_persistTimeout).GetSeconds ()); |
|
1607 |
m_persistEvent = Simulator::Schedule (m_persistTimeout, &TcpSocketBase::PersistTimeout, this); |
|
1608 |
NS_ASSERT (m_persistTimeout == Simulator::GetDelayLeft (m_persistEvent)); |
|
1609 |
} |
|
1610 |
// Note the highest ACK and tell app to send more |
|
1611 |
NS_LOG_LOGIC ("TCP " << this << " NewAck " << ack << |
|
1612 |
" numberAck " << (ack - m_txBuffer.HeadSequence ())); // Number bytes ack'ed |
|
1613 |
m_txBuffer.DiscardUpTo (ack); |
|
1614 |
if (GetTxAvailable () > 0) |
|
1615 |
{ |
|
1616 |
NotifySend (GetTxAvailable ()); |
|
1617 |
} |
|
1618 |
if (ack > m_nextTxSequence) |
|
1619 |
{ |
|
1620 |
m_nextTxSequence = ack; // If advanced |
|
1621 |
} |
|
1622 |
if (m_txBuffer.Size () == 0 && m_state != FIN_WAIT_1 && m_state != CLOSING) |
|
1623 |
{ // No retransmit timer if no data to retransmit |
|
1624 |
NS_LOG_LOGIC (this << " Cancelled ReTxTimeout event which was set to expire at " << |
|
1625 |
(Simulator::Now () + Simulator::GetDelayLeft (m_retxEvent)).GetSeconds ()); |
|
1626 |
m_retxEvent.Cancel (); |
|
1627 |
} |
|
1628 |
// Try to send more data |
|
1629 |
SendPendingData (m_connected); |
|
1630 |
} |
|
1631 |
||
1632 |
// Retransmit timeout |
|
1633 |
void |
|
1634 |
TcpSocketBase::ReTxTimeout () |
|
1635 |
{ |
|
1636 |
NS_LOG_FUNCTION (this); |
|
1637 |
NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ()); |
|
1638 |
// If erroneous timeout in closed/timed-wait state, just return |
|
1639 |
if (m_state == CLOSED || m_state == TIME_WAIT) return; |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1640 |
// If all data are received (non-closing socket and nothing to send), just return |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1641 |
if (m_state <= ESTABLISHED && m_txBuffer.HeadSequence () >= m_highTxMark) return; |
6694 | 1642 |
|
1643 |
Retransmit (); |
|
1644 |
} |
|
1645 |
||
1646 |
void |
|
1647 |
TcpSocketBase::DelAckTimeout (void) |
|
1648 |
{ |
|
1649 |
m_delAckCount = 0; |
|
1650 |
SendEmptyPacket (TcpHeader::ACK); |
|
1651 |
} |
|
1652 |
||
1653 |
void |
|
1654 |
TcpSocketBase::LastAckTimeout (void) |
|
1655 |
{ |
|
1656 |
NS_LOG_FUNCTION (this); |
|
1657 |
||
1658 |
m_lastAckEvent.Cancel (); |
|
1659 |
if (m_state == LAST_ACK) |
|
1660 |
{ |
|
1661 |
CloseAndNotify (); |
|
1662 |
} |
|
1663 |
if (!m_closeNotified) |
|
1664 |
{ |
|
1665 |
m_closeNotified = true; |
|
1666 |
} |
|
1667 |
} |
|
1668 |
||
1669 |
// Send 1-byte data to probe for the window size at the receiver when |
|
1670 |
// the local knowledge tells that the receiver has zero window size |
|
1671 |
// C.f.: RFC793 p.42, RFC1112 sec.4.2.2.17 |
|
1672 |
void |
|
1673 |
TcpSocketBase::PersistTimeout () |
|
1674 |
{ |
|
1675 |
NS_LOG_LOGIC ("PersistTimeout expired at " << Simulator::Now ().GetSeconds ()); |
|
7045
d13fa06886ce
merge with HEAD
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
6834
diff
changeset
|
1676 |
m_persistTimeout = std::min (Seconds (60), Time (2 * m_persistTimeout)); // max persist timeout = 60s |
6694 | 1677 |
Ptr<Packet> p = m_txBuffer.CopyFromSequence (1, m_nextTxSequence); |
1678 |
TcpHeader tcpHeader; |
|
1679 |
tcpHeader.SetSequenceNumber (m_nextTxSequence); |
|
1680 |
tcpHeader.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
|
1681 |
tcpHeader.SetSourcePort (m_endPoint->GetLocalPort ()); |
|
1682 |
tcpHeader.SetDestinationPort (m_endPoint->GetPeerPort ()); |
|
1683 |
tcpHeader.SetWindowSize (AdvertisedWindowSize ()); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1684 |
AddOptions (tcpHeader); |
6694 | 1685 |
|
1686 |
m_tcp->SendPacket (p, tcpHeader, m_endPoint->GetLocalAddress (), |
|
1687 |
m_endPoint->GetPeerAddress (), m_boundnetdevice); |
|
1688 |
NS_LOG_LOGIC ("Schedule persist timeout at time " |
|
1689 |
<< Simulator::Now ().GetSeconds () << " to expire at time " |
|
1690 |
<< (Simulator::Now () + m_persistTimeout).GetSeconds ()); |
|
1691 |
m_persistEvent = Simulator::Schedule (m_persistTimeout, &TcpSocketBase::PersistTimeout, this); |
|
1692 |
} |
|
1693 |
||
1694 |
void |
|
1695 |
TcpSocketBase::Retransmit () |
|
1696 |
{ |
|
1697 |
m_nextTxSequence = m_txBuffer.HeadSequence (); // Start from highest Ack |
|
1698 |
m_rtt->IncreaseMultiplier (); // Double the timeout value for next retx timer |
|
1699 |
m_dupAckCount = 0; |
|
1700 |
DoRetransmit (); // Retransmit the packet |
|
1701 |
} |
|
1702 |
||
1703 |
void |
|
1704 |
TcpSocketBase::DoRetransmit () |
|
1705 |
{ |
|
1706 |
NS_LOG_FUNCTION (this); |
|
1707 |
// Retransmit SYN packet |
|
1708 |
if (m_state == SYN_SENT) |
|
1709 |
{ |
|
1710 |
if (m_cnCount > 0) |
|
1711 |
{ |
|
1712 |
SendEmptyPacket (TcpHeader::SYN); |
|
1713 |
} |
|
1714 |
else |
|
1715 |
{ |
|
1716 |
NotifyConnectionFailed (); |
|
1717 |
} |
|
1718 |
return; |
|
1719 |
} |
|
1720 |
// Retransmit non-data packet: Only if in FIN_WAIT_1 or CLOSING state |
|
1721 |
if (m_txBuffer.Size () == 0) |
|
1722 |
{ |
|
1723 |
if (m_state == FIN_WAIT_1 || m_state == CLOSING) |
|
1724 |
{ // Must have lost FIN, re-send |
|
1725 |
SendEmptyPacket (TcpHeader::FIN); |
|
1726 |
} |
|
1727 |
return; |
|
1728 |
} |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1729 |
// Retransmit a data packet: Call SendDataPacket |
6694 | 1730 |
NS_LOG_LOGIC ("TcpSocketBase " << this << " retxing seq " << m_txBuffer.HeadSequence ()); |
7611
d462369b70f1
Advance m_nextTxSequence upon retransmit after RTO (fixes bug 1112)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7610
diff
changeset
|
1731 |
uint32_t sz = SendDataPacket (m_txBuffer.HeadSequence (), m_segmentSize, true); |
d462369b70f1
Advance m_nextTxSequence upon retransmit after RTO (fixes bug 1112)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7610
diff
changeset
|
1732 |
// In case of RTO, advance m_nextTxSequence |
d462369b70f1
Advance m_nextTxSequence upon retransmit after RTO (fixes bug 1112)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7610
diff
changeset
|
1733 |
m_nextTxSequence = std::max (m_nextTxSequence.Get (), m_txBuffer.HeadSequence () + sz); |
d462369b70f1
Advance m_nextTxSequence upon retransmit after RTO (fixes bug 1112)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7610
diff
changeset
|
1734 |
|
6694 | 1735 |
} |
1736 |
||
1737 |
void |
|
1738 |
TcpSocketBase::CancelAllTimers () |
|
1739 |
{ |
|
1740 |
m_retxEvent.Cancel (); |
|
1741 |
m_persistEvent.Cancel (); |
|
1742 |
m_delAckEvent.Cancel (); |
|
1743 |
m_lastAckEvent.Cancel (); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1744 |
m_timewaitEvent.Cancel (); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1745 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1746 |
|
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1747 |
/** Move TCP to Time_Wait state and schedule a transition to Closed state */ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1748 |
void |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1749 |
TcpSocketBase::TimeWait () |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1750 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1751 |
NS_LOG_INFO (TcpStateName[m_state] << " -> TIME_WAIT"); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1752 |
m_state = TIME_WAIT; |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1753 |
CancelAllTimers (); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1754 |
// Move from TIME_WAIT to CLOSED after 2*MSL. Max segment lifetime is 2 min |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1755 |
// according to RFC793, p.28 |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1756 |
m_timewaitEvent = Simulator::Schedule (Seconds (2*m_msl), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1757 |
&TcpSocketBase::CloseAndNotify, this); |
6694 | 1758 |
} |
1759 |
||
1760 |
/** Below are the attribute get/set functions */ |
|
1761 |
||
1762 |
void |
|
1763 |
TcpSocketBase::SetSndBufSize (uint32_t size) |
|
1764 |
{ |
|
1765 |
m_txBuffer.SetMaxBufferSize (size); |
|
1766 |
} |
|
1767 |
||
1768 |
uint32_t |
|
1769 |
TcpSocketBase::GetSndBufSize (void) const |
|
1770 |
{ |
|
1771 |
return m_txBuffer.MaxBufferSize (); |
|
1772 |
} |
|
1773 |
||
1774 |
void |
|
1775 |
TcpSocketBase::SetRcvBufSize (uint32_t size) |
|
1776 |
{ |
|
1777 |
m_rxBuffer.SetMaxBufferSize (size); |
|
1778 |
} |
|
1779 |
||
1780 |
uint32_t |
|
1781 |
TcpSocketBase::GetRcvBufSize (void) const |
|
1782 |
{ |
|
1783 |
return m_rxBuffer.MaxBufferSize (); |
|
1784 |
} |
|
1785 |
||
1786 |
void |
|
1787 |
TcpSocketBase::SetSegSize (uint32_t size) |
|
1788 |
{ |
|
1789 |
m_segmentSize = size; |
|
1790 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "Cannot change segment size dynamically."); |
|
1791 |
} |
|
1792 |
||
1793 |
uint32_t |
|
1794 |
TcpSocketBase::GetSegSize (void) const |
|
1795 |
{ |
|
1796 |
return m_segmentSize; |
|
1797 |
} |
|
1798 |
||
1799 |
void |
|
1800 |
TcpSocketBase::SetConnTimeout (Time timeout) |
|
1801 |
{ |
|
1802 |
m_cnTimeout = timeout; |
|
1803 |
} |
|
1804 |
||
1805 |
Time |
|
1806 |
TcpSocketBase::GetConnTimeout (void) const |
|
1807 |
{ |
|
1808 |
return m_cnTimeout; |
|
1809 |
} |
|
1810 |
||
1811 |
void |
|
1812 |
TcpSocketBase::SetConnCount (uint32_t count) |
|
1813 |
{ |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1814 |
m_cnRetries = count; |
6694 | 1815 |
} |
1816 |
||
1817 |
uint32_t |
|
1818 |
TcpSocketBase::GetConnCount (void) const |
|
1819 |
{ |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1820 |
return m_cnRetries; |
6694 | 1821 |
} |
1822 |
||
1823 |
void |
|
1824 |
TcpSocketBase::SetDelAckTimeout (Time timeout) |
|
1825 |
{ |
|
1826 |
m_delAckTimeout = timeout; |
|
1827 |
} |
|
1828 |
||
1829 |
Time |
|
1830 |
TcpSocketBase::GetDelAckTimeout (void) const |
|
1831 |
{ |
|
1832 |
return m_delAckTimeout; |
|
1833 |
} |
|
1834 |
||
1835 |
void |
|
1836 |
TcpSocketBase::SetDelAckMaxCount (uint32_t count) |
|
1837 |
{ |
|
1838 |
m_delAckMaxCount = count; |
|
1839 |
} |
|
1840 |
||
1841 |
uint32_t |
|
1842 |
TcpSocketBase::GetDelAckMaxCount (void) const |
|
1843 |
{ |
|
1844 |
return m_delAckMaxCount; |
|
1845 |
} |
|
1846 |
||
1847 |
void |
|
7619
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1848 |
TcpSocketBase::SetTcpNoDelay (bool noDelay) |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1849 |
{ |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1850 |
m_noDelay = noDelay; |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1851 |
} |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1852 |
|
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1853 |
bool |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1854 |
TcpSocketBase::GetTcpNoDelay (void) const |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1855 |
{ |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1856 |
return m_noDelay; |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1857 |
} |
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1858 |
|
b4dee6307aa7
Nagle's algorithm in TCP (closes bug 1039)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7611
diff
changeset
|
1859 |
void |
6694 | 1860 |
TcpSocketBase::SetPersistTimeout (Time timeout) |
1861 |
{ |
|
1862 |
m_persistTimeout = timeout; |
|
1863 |
} |
|
1864 |
||
1865 |
Time |
|
1866 |
TcpSocketBase::GetPersistTimeout (void) const |
|
1867 |
{ |
|
1868 |
return m_persistTimeout; |
|
1869 |
} |
|
1870 |
||
1871 |
bool |
|
1872 |
TcpSocketBase::SetAllowBroadcast (bool allowBroadcast) |
|
1873 |
{ |
|
1874 |
// Broadcast is not implemented. Return true only if allowBroadcast==false |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
7045
diff
changeset
|
1875 |
return (!allowBroadcast); |
6694 | 1876 |
} |
1877 |
||
1878 |
bool |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1879 |
TcpSocketBase::GetAllowBroadcast (void) const |
6694 | 1880 |
{ |
1881 |
return false; |
|
1882 |
} |
|
1883 |
||
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1884 |
/** Placeholder function for future extension that reads more from the TCP header */ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1885 |
void |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1886 |
TcpSocketBase::ReadOptions (const TcpHeader&) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1887 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1888 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1889 |
|
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1890 |
/** Placeholder function for future extension that changes the TCP header */ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1891 |
void |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1892 |
TcpSocketBase::AddOptions (TcpHeader&) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1893 |
{ |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1894 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7441
diff
changeset
|
1895 |
|
6694 | 1896 |
} // namespace ns3 |