3578
|
1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
4 |
* it under the terms of the GNU General Public License version 2 as
|
|
5 |
* published by the Free Software Foundation;
|
|
6 |
*
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
* GNU General Public License for more details.
|
|
11 |
*
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
13 |
* along with this program; if not, write to the Free Software
|
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
15 |
*
|
|
16 |
* based on tcp-socket-impl.cc, Author: Raj Bhattacharjea <raj.b@gatech.edu>
|
|
17 |
* Author: Florian Westphal <fw@strlen.de>
|
|
18 |
*/
|
|
19 |
|
|
20 |
#include "ns3/node.h"
|
|
21 |
#include "ns3/inet-socket-address.h"
|
|
22 |
#include "ns3/log.h"
|
|
23 |
#include "ns3/ipv4.h"
|
|
24 |
#include "ipv4-end-point.h"
|
|
25 |
#include "nsc-tcp-l4-protocol.h"
|
|
26 |
#include "nsc-tcp-socket-impl.h"
|
|
27 |
#include "ns3/simulation-singleton.h"
|
|
28 |
#include "tcp-typedefs.h"
|
|
29 |
#include "ns3/simulator.h"
|
|
30 |
#include "ns3/packet.h"
|
|
31 |
#include "ns3/uinteger.h"
|
|
32 |
#include "ns3/trace-source-accessor.h"
|
|
33 |
|
|
34 |
#include <algorithm>
|
|
35 |
|
|
36 |
#include <sys/socket.h>
|
|
37 |
#include <netinet/in.h>
|
|
38 |
#include <arpa/inet.h>
|
|
39 |
#include <netinet/ip.h>
|
|
40 |
#include <netinet/tcp.h>
|
|
41 |
|
|
42 |
#include "nsc/sim/sim_errno.h"
|
|
43 |
|
|
44 |
NS_LOG_COMPONENT_DEFINE ("NscTcpSocketImpl");
|
|
45 |
|
|
46 |
using namespace std;
|
|
47 |
|
|
48 |
namespace ns3 {
|
|
49 |
|
|
50 |
NS_OBJECT_ENSURE_REGISTERED (NscTcpSocketImpl);
|
|
51 |
|
|
52 |
TypeId
|
|
53 |
NscTcpSocketImpl::GetTypeId ()
|
|
54 |
{
|
|
55 |
static TypeId tid = TypeId("ns3::NscTcpSocketImpl")
|
|
56 |
.SetParent<TcpSocket> ()
|
|
57 |
.AddTraceSource ("CongestionWindow",
|
|
58 |
"The TCP connection's congestion window",
|
|
59 |
MakeTraceSourceAccessor (&NscTcpSocketImpl::m_cWnd))
|
|
60 |
;
|
|
61 |
return tid;
|
|
62 |
}
|
|
63 |
|
|
64 |
NscTcpSocketImpl::NscTcpSocketImpl ()
|
|
65 |
: m_endPoint (0),
|
|
66 |
m_node (0),
|
|
67 |
m_tcp (0),
|
|
68 |
m_peerAddress ("0.0.0.0", 0),
|
|
69 |
m_errno (ERROR_NOTERROR),
|
|
70 |
m_shutdownSend (false),
|
|
71 |
m_shutdownRecv (false),
|
|
72 |
m_connected (false),
|
|
73 |
m_state (CLOSED),
|
|
74 |
m_closeOnEmpty (false),
|
|
75 |
m_txBufferSize (0),
|
|
76 |
m_rtt (0),
|
|
77 |
m_lastMeasuredRtt (Seconds(0.0))
|
|
78 |
{
|
|
79 |
NS_LOG_FUNCTION (this);
|
|
80 |
}
|
|
81 |
|
|
82 |
NscTcpSocketImpl::NscTcpSocketImpl(const NscTcpSocketImpl& sock)
|
|
83 |
: TcpSocket(sock), //copy the base class callbacks
|
|
84 |
m_delAckMaxCount (sock.m_delAckMaxCount),
|
|
85 |
m_delAckTimeout (sock.m_delAckTimeout),
|
|
86 |
m_endPoint (0),
|
|
87 |
m_node (sock.m_node),
|
|
88 |
m_tcp (sock.m_tcp),
|
|
89 |
m_remoteAddress (sock.m_remoteAddress),
|
|
90 |
m_remotePort (sock.m_remotePort),
|
|
91 |
m_localAddress (sock.m_localAddress),
|
|
92 |
m_localPort (sock.m_localPort),
|
|
93 |
m_peerAddress (sock.m_peerAddress),
|
|
94 |
m_errno (sock.m_errno),
|
|
95 |
m_shutdownSend (sock.m_shutdownSend),
|
|
96 |
m_shutdownRecv (sock.m_shutdownRecv),
|
|
97 |
m_connected (sock.m_connected),
|
|
98 |
m_state (sock.m_state),
|
|
99 |
m_closeOnEmpty (sock.m_closeOnEmpty),
|
|
100 |
m_segmentSize (sock.m_segmentSize),
|
|
101 |
m_rxWindowSize (sock.m_rxWindowSize),
|
|
102 |
m_advertisedWindowSize (sock.m_advertisedWindowSize),
|
|
103 |
m_cWnd (sock.m_cWnd),
|
|
104 |
m_ssThresh (sock.m_ssThresh),
|
|
105 |
m_initialCWnd (sock.m_initialCWnd),
|
|
106 |
m_rtt (0),
|
|
107 |
m_lastMeasuredRtt (Seconds(0.0)),
|
|
108 |
m_cnTimeout (sock.m_cnTimeout),
|
|
109 |
m_cnCount (sock.m_cnCount),
|
|
110 |
m_rxAvailable (0),
|
|
111 |
m_nscTcpSocket (0),
|
|
112 |
m_sndBufSize (sock.m_sndBufSize)
|
|
113 |
{
|
|
114 |
NS_LOG_FUNCTION_NOARGS ();
|
|
115 |
NS_LOG_LOGIC("Invoked the copy constructor");
|
|
116 |
//copy the pending data if necessary
|
|
117 |
if(!sock.m_txBuffer.empty () )
|
|
118 |
{
|
|
119 |
m_txBuffer = sock.m_txBuffer;
|
|
120 |
}
|
|
121 |
//copy the rtt if necessary
|
|
122 |
if (sock.m_rtt)
|
|
123 |
{
|
|
124 |
m_rtt = sock.m_rtt->Copy();
|
|
125 |
}
|
|
126 |
//can't "copy" the endpoint just yes, must do this when we know the peer info
|
|
127 |
//too; this is in SYN_ACK_TX
|
|
128 |
}
|
|
129 |
|
|
130 |
NscTcpSocketImpl::~NscTcpSocketImpl ()
|
|
131 |
{
|
|
132 |
NS_LOG_FUNCTION(this);
|
|
133 |
m_node = 0;
|
|
134 |
if (m_endPoint != 0)
|
|
135 |
{
|
|
136 |
NS_ASSERT (m_tcp != 0);
|
|
137 |
/**
|
|
138 |
* Note that this piece of code is a bit tricky:
|
|
139 |
* when DeAllocate is called, it will call into
|
|
140 |
* Ipv4EndPointDemux::Deallocate which triggers
|
|
141 |
* a delete of the associated endPoint which triggers
|
|
142 |
* in turn a call to the method ::Destroy below
|
|
143 |
* will will zero the m_endPoint field.
|
|
144 |
*/
|
|
145 |
NS_ASSERT (m_endPoint != 0);
|
|
146 |
m_tcp->DeAllocate (m_endPoint);
|
|
147 |
NS_ASSERT (m_endPoint == 0);
|
|
148 |
}
|
|
149 |
m_tcp = 0;
|
|
150 |
}
|
|
151 |
|
|
152 |
void
|
|
153 |
NscTcpSocketImpl::SetNode (Ptr<Node> node)
|
|
154 |
{
|
|
155 |
m_node = node;
|
|
156 |
// Initialize some variables
|
|
157 |
m_cWnd = m_initialCWnd * m_segmentSize;
|
|
158 |
m_rxWindowSize = m_advertisedWindowSize;
|
|
159 |
}
|
|
160 |
|
|
161 |
void
|
|
162 |
NscTcpSocketImpl::SetTcp (Ptr<NscTcpL4Protocol> tcp)
|
|
163 |
{
|
|
164 |
m_nscTcpSocket = tcp->m_nscStack->new_tcp_socket();
|
|
165 |
m_tcp = tcp;
|
|
166 |
}
|
|
167 |
void
|
|
168 |
NscTcpSocketImpl::SetRtt (Ptr<RttEstimator> rtt)
|
|
169 |
{
|
|
170 |
m_rtt = rtt;
|
|
171 |
}
|
|
172 |
|
|
173 |
|
|
174 |
enum Socket::SocketErrno
|
|
175 |
NscTcpSocketImpl::GetErrno (void) const
|
|
176 |
{
|
|
177 |
NS_LOG_FUNCTION_NOARGS ();
|
|
178 |
return m_errno;
|
|
179 |
}
|
|
180 |
|
|
181 |
Ptr<Node>
|
|
182 |
NscTcpSocketImpl::GetNode (void) const
|
|
183 |
{
|
|
184 |
NS_LOG_FUNCTION_NOARGS ();
|
|
185 |
return m_node;
|
|
186 |
}
|
|
187 |
|
|
188 |
void
|
|
189 |
NscTcpSocketImpl::Destroy (void)
|
|
190 |
{
|
|
191 |
NS_LOG_FUNCTION_NOARGS ();
|
|
192 |
m_node = 0;
|
|
193 |
m_endPoint = 0;
|
|
194 |
m_tcp = 0;
|
|
195 |
}
|
|
196 |
int
|
|
197 |
NscTcpSocketImpl::FinishBind (void)
|
|
198 |
{
|
|
199 |
NS_LOG_FUNCTION_NOARGS ();
|
|
200 |
if (m_endPoint == 0)
|
|
201 |
{
|
|
202 |
return -1;
|
|
203 |
}
|
|
204 |
m_endPoint->SetRxCallback (MakeCallback (&NscTcpSocketImpl::ForwardUp, Ptr<NscTcpSocketImpl>(this)));
|
|
205 |
m_endPoint->SetDestroyCallback (MakeCallback (&NscTcpSocketImpl::Destroy, Ptr<NscTcpSocketImpl>(this)));
|
|
206 |
m_localAddress = m_endPoint->GetLocalAddress ();
|
|
207 |
m_localPort = m_endPoint->GetLocalPort ();
|
|
208 |
return 0;
|
|
209 |
}
|
|
210 |
|
|
211 |
int
|
|
212 |
NscTcpSocketImpl::Bind (void)
|
|
213 |
{
|
|
214 |
NS_LOG_FUNCTION_NOARGS ();
|
|
215 |
m_endPoint = m_tcp->Allocate ();
|
|
216 |
return FinishBind ();
|
|
217 |
}
|
|
218 |
int
|
|
219 |
NscTcpSocketImpl::Bind (const Address &address)
|
|
220 |
{
|
|
221 |
NS_LOG_FUNCTION (this<<address);
|
|
222 |
if (!InetSocketAddress::IsMatchingType (address))
|
|
223 |
{
|
|
224 |
return ERROR_INVAL;
|
|
225 |
}
|
|
226 |
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
|
|
227 |
Ipv4Address ipv4 = transport.GetIpv4 ();
|
|
228 |
uint16_t port = transport.GetPort ();
|
|
229 |
if (ipv4 == Ipv4Address::GetAny () && port == 0)
|
|
230 |
{
|
|
231 |
m_endPoint = m_tcp->Allocate ();
|
|
232 |
NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint);
|
|
233 |
}
|
|
234 |
else if (ipv4 == Ipv4Address::GetAny () && port != 0)
|
|
235 |
{
|
|
236 |
m_endPoint = m_tcp->Allocate (port);
|
|
237 |
NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint);
|
|
238 |
}
|
|
239 |
else if (ipv4 != Ipv4Address::GetAny () && port == 0)
|
|
240 |
{
|
|
241 |
m_endPoint = m_tcp->Allocate (ipv4);
|
|
242 |
NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint);
|
|
243 |
}
|
|
244 |
else if (ipv4 != Ipv4Address::GetAny () && port != 0)
|
|
245 |
{
|
|
246 |
m_endPoint = m_tcp->Allocate (ipv4, port);
|
|
247 |
NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint);
|
|
248 |
}
|
|
249 |
|
|
250 |
m_localPort = port;
|
|
251 |
return FinishBind ();
|
|
252 |
}
|
|
253 |
|
|
254 |
int
|
|
255 |
NscTcpSocketImpl::ShutdownSend (void)
|
|
256 |
{
|
|
257 |
NS_LOG_FUNCTION_NOARGS ();
|
|
258 |
m_shutdownSend = true;
|
|
259 |
return 0;
|
|
260 |
}
|
|
261 |
int
|
|
262 |
NscTcpSocketImpl::ShutdownRecv (void)
|
|
263 |
{
|
|
264 |
NS_LOG_FUNCTION_NOARGS ();
|
|
265 |
m_shutdownRecv = false;
|
|
266 |
return 0;
|
|
267 |
}
|
|
268 |
|
|
269 |
int
|
|
270 |
NscTcpSocketImpl::Close (void)
|
|
271 |
{
|
|
272 |
NS_LOG_FUNCTION (this << m_state);
|
|
273 |
|
|
274 |
if (m_state == CLOSED)
|
|
275 |
{
|
|
276 |
return -1;
|
|
277 |
}
|
|
278 |
if (!m_txBuffer.empty ())
|
|
279 |
{ // App close with pending data must wait until all data transmitted
|
|
280 |
m_closeOnEmpty = true;
|
|
281 |
NS_LOG_LOGIC("Socket " << this <<
|
|
282 |
" deferring close, state " << m_state);
|
|
283 |
return 0;
|
|
284 |
}
|
|
285 |
|
|
286 |
m_nscTcpSocket->disconnect();
|
|
287 |
m_state = CLOSED;
|
|
288 |
ShutdownSend ();
|
|
289 |
return 0;
|
|
290 |
}
|
|
291 |
|
|
292 |
int
|
|
293 |
NscTcpSocketImpl::Connect (const Address & address)
|
|
294 |
{
|
|
295 |
NS_LOG_FUNCTION (this << address);
|
|
296 |
if (m_endPoint == 0)
|
|
297 |
{
|
|
298 |
if (Bind () == -1)
|
|
299 |
{
|
|
300 |
NS_ASSERT (m_endPoint == 0);
|
|
301 |
return -1;
|
|
302 |
}
|
|
303 |
NS_ASSERT (m_endPoint != 0);
|
|
304 |
}
|
|
305 |
InetSocketAddress transport = InetSocketAddress::ConvertFrom (address);
|
|
306 |
m_remoteAddress = transport.GetIpv4 ();
|
|
307 |
m_remotePort = transport.GetPort ();
|
|
308 |
|
|
309 |
struct in_addr remoteAddr;
|
|
310 |
uint32_t addr32;
|
|
311 |
|
|
312 |
m_remoteAddress.Serialize((uint8_t*)&addr32);
|
|
313 |
remoteAddr.s_addr = addr32;
|
|
314 |
|
|
315 |
m_nscTcpSocket->connect(inet_ntoa(remoteAddr), m_remotePort);
|
|
316 |
m_state = SYN_SENT;
|
|
317 |
return 0;
|
|
318 |
}
|
|
319 |
|
|
320 |
int
|
|
321 |
NscTcpSocketImpl::Send (const Ptr<Packet> p, uint32_t flags)
|
|
322 |
{
|
|
323 |
NS_LOG_FUNCTION (this << p);
|
|
324 |
|
|
325 |
NS_ASSERT (p->GetSize () > 0);
|
|
326 |
if (m_state == ESTABLISHED || m_state == SYN_SENT || m_state == CLOSE_WAIT)
|
|
327 |
{
|
|
328 |
if (p->GetSize () > GetTxAvailable ())
|
|
329 |
{
|
|
330 |
m_errno = ERROR_MSGSIZE;
|
|
331 |
return -1;
|
|
332 |
}
|
|
333 |
|
|
334 |
bool txEmpty = m_txBuffer.empty();
|
|
335 |
if (m_state == ESTABLISHED)
|
|
336 |
{
|
|
337 |
if (txEmpty)
|
|
338 |
{
|
|
339 |
m_txBuffer.push(p);
|
|
340 |
m_txBufferSize += p->GetSize ();
|
|
341 |
}
|
|
342 |
if (!SendPendingData())
|
|
343 |
{
|
|
344 |
if (m_errno == ERROR_AGAIN)
|
|
345 |
{
|
|
346 |
return txEmpty ? p->GetSize () : -1;
|
|
347 |
}
|
|
348 |
if (txEmpty)
|
|
349 |
{
|
|
350 |
m_txBuffer.pop ();
|
|
351 |
m_txBufferSize = 0;
|
|
352 |
}
|
|
353 |
return -1;
|
|
354 |
}
|
|
355 |
}
|
|
356 |
else
|
|
357 |
{ // SYN_SET -- Queue Data
|
|
358 |
m_txBuffer.push(p);
|
|
359 |
m_txBufferSize += p->GetSize ();
|
|
360 |
}
|
|
361 |
return p->GetSize ();
|
|
362 |
}
|
|
363 |
else
|
|
364 |
{
|
|
365 |
m_errno = ERROR_NOTCONN;
|
|
366 |
return -1;
|
|
367 |
}
|
|
368 |
}
|
|
369 |
|
|
370 |
int
|
|
371 |
NscTcpSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags, const Address &address)
|
|
372 |
{
|
|
373 |
NS_LOG_FUNCTION (this << address << p);
|
|
374 |
if (!m_connected)
|
|
375 |
{
|
|
376 |
m_errno = ERROR_NOTCONN;
|
|
377 |
return -1;
|
|
378 |
}
|
|
379 |
else
|
|
380 |
{
|
|
381 |
return Send (p, flags); //drop the address according to BSD manpages
|
|
382 |
}
|
|
383 |
}
|
|
384 |
|
|
385 |
uint32_t
|
|
386 |
NscTcpSocketImpl::GetTxAvailable (void) const
|
|
387 |
{
|
|
388 |
NS_LOG_FUNCTION_NOARGS ();
|
|
389 |
if (m_txBufferSize != 0)
|
|
390 |
{
|
|
391 |
NS_ASSERT (m_txBufferSize <= m_sndBufSize);
|
|
392 |
return m_sndBufSize - m_txBufferSize;
|
|
393 |
}
|
|
394 |
else
|
|
395 |
{
|
|
396 |
return m_sndBufSize;
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
int
|
|
401 |
NscTcpSocketImpl::Listen (uint32_t q)
|
|
402 |
{
|
|
403 |
NS_LOG_FUNCTION (this << q);
|
|
404 |
m_nscTcpSocket->listen(m_localPort);
|
|
405 |
m_state = LISTEN;
|
|
406 |
return 0;
|
|
407 |
}
|
|
408 |
|
|
409 |
|
|
410 |
void
|
|
411 |
NscTcpSocketImpl::NSCWakeup ()
|
|
412 |
{
|
|
413 |
switch (m_state) {
|
|
414 |
case SYN_SENT:
|
|
415 |
if (!m_nscTcpSocket->is_connected())
|
|
416 |
break;
|
|
417 |
m_state = ESTABLISHED;
|
|
418 |
Simulator::ScheduleNow(&NscTcpSocketImpl::ConnectionSucceeded, this);
|
|
419 |
// fall through to schedule read/write events
|
|
420 |
case ESTABLISHED:
|
|
421 |
if (!m_txBuffer.empty ())
|
|
422 |
Simulator::ScheduleNow(&NscTcpSocketImpl::SendPendingData, this);
|
|
423 |
Simulator::ScheduleNow(&NscTcpSocketImpl::ReadPendingData, this);
|
|
424 |
break;
|
|
425 |
case LISTEN:
|
|
426 |
Simulator::ScheduleNow(&NscTcpSocketImpl::Accept, this);
|
|
427 |
break;
|
|
428 |
case CLOSED: break;
|
|
429 |
default:
|
|
430 |
NS_LOG_DEBUG (this << " invalid state: " << m_state);
|
|
431 |
}
|
|
432 |
}
|
|
433 |
|
|
434 |
Ptr<Packet>
|
|
435 |
NscTcpSocketImpl::Recv (uint32_t maxSize, uint32_t flags)
|
|
436 |
{
|
|
437 |
NS_LOG_FUNCTION_NOARGS ();
|
|
438 |
if (m_deliveryQueue.empty() )
|
|
439 |
{
|
|
440 |
return 0;
|
|
441 |
}
|
|
442 |
Ptr<Packet> p = m_deliveryQueue.front ();
|
|
443 |
if (p->GetSize () <= maxSize)
|
|
444 |
{
|
|
445 |
m_deliveryQueue.pop ();
|
|
446 |
m_rxAvailable -= p->GetSize ();
|
|
447 |
}
|
|
448 |
else
|
|
449 |
{
|
|
450 |
p = 0;
|
|
451 |
}
|
|
452 |
return p;
|
|
453 |
}
|
|
454 |
|
|
455 |
Ptr<Packet>
|
|
456 |
NscTcpSocketImpl::RecvFrom (uint32_t maxSize, uint32_t flags,
|
|
457 |
Address &fromAddress)
|
|
458 |
{
|
|
459 |
NS_LOG_FUNCTION (this << maxSize << flags);
|
|
460 |
Ptr<Packet> packet = Recv (maxSize, flags);
|
|
461 |
if (packet != 0)
|
|
462 |
{
|
|
463 |
SocketAddressTag tag;
|
|
464 |
bool found;
|
|
465 |
found = packet->FindFirstMatchingTag (tag);
|
|
466 |
NS_ASSERT (found);
|
|
467 |
fromAddress = tag.GetAddress ();
|
|
468 |
}
|
|
469 |
return packet;
|
|
470 |
}
|
|
471 |
|
|
472 |
uint32_t
|
|
473 |
NscTcpSocketImpl::GetRxAvailable (void) const
|
|
474 |
{
|
|
475 |
NS_LOG_FUNCTION_NOARGS ();
|
|
476 |
// We separately maintain this state to avoid walking the queue
|
|
477 |
// every time this might be called
|
|
478 |
return m_rxAvailable;
|
|
479 |
}
|
|
480 |
|
|
481 |
void
|
|
482 |
NscTcpSocketImpl::ForwardUp (Ptr<Packet> packet, Ipv4Address ipv4, uint16_t port)
|
|
483 |
{
|
|
484 |
NSCWakeup();
|
|
485 |
}
|
|
486 |
|
|
487 |
void NscTcpSocketImpl::CompleteFork(void)
|
|
488 |
{
|
|
489 |
// The address pairs (m_localAddress, m_localPort, m_remoteAddress, m_remotePort)
|
|
490 |
// are bogus, but this isn't important at the moment, because
|
|
491 |
// address <-> Socket handling is done by NSC internally.
|
|
492 |
// We only need to add the new ns-3 socket to the list of sockets, so
|
|
493 |
// we use plain Allocate() instead of Allocate(m_localAddress, ... )
|
|
494 |
uint8_t buf[4];
|
|
495 |
int port;
|
|
496 |
size_t buflen = sizeof(buf);
|
|
497 |
|
|
498 |
if (0 == m_nscTcpSocket->getpeername((void *) buf, &buflen, &port)) {
|
|
499 |
m_remotePort = ntohs(port);
|
|
500 |
m_remoteAddress = m_remoteAddress.Deserialize(buf);
|
|
501 |
m_peerAddress = InetSocketAddress(m_remoteAddress, m_remotePort);
|
|
502 |
}
|
|
503 |
|
|
504 |
m_endPoint = m_tcp->Allocate ();
|
|
505 |
|
|
506 |
//the cloned socket with be in listen state, so manually change state
|
|
507 |
NS_ASSERT(m_state == LISTEN);
|
|
508 |
m_state = ESTABLISHED;
|
|
509 |
|
|
510 |
buflen = sizeof(buf);
|
|
511 |
if (0 == m_nscTcpSocket->getsockname((void *) &buf, &buflen, &port))
|
|
512 |
m_localAddress = m_localAddress.Deserialize(buf);
|
|
513 |
|
|
514 |
NS_LOG_LOGIC ("NscTcpSocketImpl " << this << " accepted connection from "
|
|
515 |
<< m_remoteAddress << ":" << m_remotePort
|
|
516 |
<< " to " << m_localAddress << ":" << m_localPort);
|
|
517 |
//equivalent to FinishBind
|
|
518 |
m_endPoint->SetRxCallback (MakeCallback (&NscTcpSocketImpl::ForwardUp, Ptr<NscTcpSocketImpl>(this)));
|
|
519 |
m_endPoint->SetDestroyCallback (MakeCallback (&NscTcpSocketImpl::Destroy, Ptr<NscTcpSocketImpl>(this)));
|
|
520 |
|
|
521 |
NotifyNewConnectionCreated (this, m_peerAddress);
|
|
522 |
}
|
|
523 |
|
|
524 |
void NscTcpSocketImpl::ConnectionSucceeded()
|
|
525 |
{ // We would preferred to have scheduled an event directly to
|
|
526 |
// NotifyConnectionSucceeded, but (sigh) these are protected
|
|
527 |
// and we can get the address of it :(
|
|
528 |
|
|
529 |
uint8_t buf[4];
|
|
530 |
int port;
|
|
531 |
size_t buflen = sizeof(buf);
|
|
532 |
if (0 == m_nscTcpSocket->getsockname((void *) &buf, &buflen, &port)) {
|
|
533 |
m_localAddress = m_localAddress.Deserialize(buf);
|
|
534 |
m_localPort = ntohs(port);
|
|
535 |
}
|
|
536 |
|
|
537 |
NS_LOG_LOGIC ("NscTcpSocketImpl " << this << " connected to "
|
|
538 |
<< m_remoteAddress << ":" << m_remotePort
|
|
539 |
<< " from " << m_localAddress << ":" << m_localPort);
|
|
540 |
NotifyConnectionSucceeded();
|
|
541 |
}
|
|
542 |
|
|
543 |
|
|
544 |
bool NscTcpSocketImpl::Accept (void)
|
|
545 |
{
|
|
546 |
if (m_state == CLOSED)
|
|
547 |
{ // Happens if application closes listening socket after Accept() was scheduled.
|
|
548 |
return false;
|
|
549 |
}
|
|
550 |
NS_ASSERT (m_state == LISTEN);
|
|
551 |
|
|
552 |
if (!m_nscTcpSocket->is_listening())
|
|
553 |
{
|
|
554 |
return false;
|
|
555 |
}
|
|
556 |
INetStreamSocket *newsock;
|
|
557 |
int res = m_nscTcpSocket->accept(&newsock);
|
|
558 |
if (res != 0)
|
|
559 |
{
|
|
560 |
return false;
|
|
561 |
}
|
|
562 |
// We could obtain a fromAddress using getpeername, but we've already
|
|
563 |
// finished the tcp handshake here, i.e. this is a new connection
|
|
564 |
// and not a connection request.
|
|
565 |
// if (!NotifyConnectionRequest(fromAddress))
|
|
566 |
// return true;
|
|
567 |
|
|
568 |
// Clone the socket
|
|
569 |
Ptr<NscTcpSocketImpl> newSock = Copy ();
|
|
570 |
newSock->m_nscTcpSocket = newsock;
|
|
571 |
NS_LOG_LOGIC ("Cloned a NscTcpSocketImpl " << newSock);
|
|
572 |
|
|
573 |
Simulator::ScheduleNow (&NscTcpSocketImpl::CompleteFork, newSock);
|
|
574 |
return true;
|
|
575 |
}
|
|
576 |
|
|
577 |
bool NscTcpSocketImpl::ReadPendingData (void)
|
|
578 |
{
|
|
579 |
if (m_state != ESTABLISHED)
|
|
580 |
{
|
|
581 |
return false;
|
|
582 |
}
|
|
583 |
int len, err;
|
|
584 |
uint8_t buffer[8192];
|
|
585 |
len = sizeof(buffer);
|
|
586 |
m_errno = ERROR_NOTERROR;
|
|
587 |
err = m_nscTcpSocket->read_data(buffer, &len);
|
|
588 |
if (err == 0 && len == 0)
|
|
589 |
{
|
|
590 |
NS_LOG_LOGIC ("ReadPendingData got EOF from socket");
|
|
591 |
m_state = CLOSED;
|
|
592 |
return false;
|
|
593 |
}
|
|
594 |
m_errno = GetNativeNs3Errno(err);
|
|
595 |
switch (m_errno)
|
|
596 |
{
|
|
597 |
case ERROR_NOTERROR: break; // some data was sent
|
|
598 |
case ERROR_AGAIN: return false;
|
|
599 |
default:
|
|
600 |
NS_LOG_WARN ("Error (" << err << ") " <<
|
|
601 |
"during read_data, ns-3 errno set to" << m_errno);
|
|
602 |
m_state = CLOSED;
|
|
603 |
return false;
|
|
604 |
}
|
|
605 |
|
|
606 |
Ptr<Packet> p = Create<Packet> (buffer, len);
|
|
607 |
|
|
608 |
SocketAddressTag tag;
|
|
609 |
|
|
610 |
tag.SetAddress (m_peerAddress);
|
|
611 |
p->AddTag (tag);
|
|
612 |
m_deliveryQueue.push (p);
|
|
613 |
m_rxAvailable += p->GetSize ();
|
|
614 |
|
|
615 |
NotifyDataRecv ();
|
|
616 |
return true;
|
|
617 |
}
|
|
618 |
|
|
619 |
bool NscTcpSocketImpl::SendPendingData (void)
|
|
620 |
{
|
|
621 |
NS_LOG_FUNCTION (this);
|
|
622 |
NS_LOG_LOGIC ("ENTERING SendPendingData");
|
|
623 |
|
|
624 |
if (m_txBuffer.empty ())
|
|
625 |
{
|
|
626 |
return false;
|
|
627 |
}
|
|
628 |
|
|
629 |
int ret;
|
|
630 |
size_t size, written = 0;
|
|
631 |
|
|
632 |
do {
|
|
633 |
Ptr<Packet> &p = m_txBuffer.front ();
|
|
634 |
size = p->GetSize ();
|
|
635 |
NS_ASSERT (size > 0);
|
|
636 |
|
|
637 |
m_errno = ERROR_NOTERROR;
|
|
638 |
ret = m_nscTcpSocket->send_data((const char *)p->PeekData (), size);
|
|
639 |
if (ret <= 0)
|
|
640 |
{
|
|
641 |
m_errno = GetNativeNs3Errno(ret);
|
|
642 |
if (m_errno != ERROR_AGAIN)
|
|
643 |
{
|
|
644 |
NS_LOG_WARN ("Error (" << ret << ") " <<
|
|
645 |
"during send_data, ns-3 errno set to" << m_errno);
|
|
646 |
}
|
|
647 |
break;
|
|
648 |
}
|
|
649 |
written += ret;
|
|
650 |
|
|
651 |
NS_ASSERT (m_txBufferSize >= (size_t)ret);
|
|
652 |
m_txBufferSize -= ret;
|
|
653 |
|
|
654 |
if ((size_t)ret < size)
|
|
655 |
{
|
|
656 |
p->RemoveAtStart(ret);
|
|
657 |
break;
|
|
658 |
}
|
|
659 |
|
|
660 |
m_txBuffer.pop ();
|
|
661 |
|
|
662 |
if (m_txBuffer.empty ())
|
|
663 |
{
|
|
664 |
if (m_closeOnEmpty)
|
|
665 |
{
|
|
666 |
m_nscTcpSocket->disconnect();
|
|
667 |
m_state = CLOSED;
|
|
668 |
}
|
|
669 |
break;
|
|
670 |
}
|
|
671 |
} while ((size_t) ret == size);
|
|
672 |
|
|
673 |
if (written > 0)
|
|
674 |
{
|
|
675 |
Simulator::ScheduleNow(&NscTcpSocketImpl::NotifyDataSent, this, ret);
|
|
676 |
return true;
|
|
677 |
}
|
|
678 |
return false;
|
|
679 |
}
|
|
680 |
|
|
681 |
Ptr<NscTcpSocketImpl> NscTcpSocketImpl::Copy ()
|
|
682 |
{
|
|
683 |
return CopyObject<NscTcpSocketImpl> (this);
|
|
684 |
}
|
|
685 |
|
|
686 |
void
|
|
687 |
NscTcpSocketImpl::SetSndBufSize (uint32_t size)
|
|
688 |
{
|
|
689 |
m_sndBufSize = size;
|
|
690 |
}
|
|
691 |
|
|
692 |
uint32_t
|
|
693 |
NscTcpSocketImpl::GetSndBufSize (void) const
|
|
694 |
{
|
|
695 |
return m_sndBufSize;
|
|
696 |
}
|
|
697 |
|
|
698 |
void
|
|
699 |
NscTcpSocketImpl::SetRcvBufSize (uint32_t size)
|
|
700 |
{
|
|
701 |
m_rcvBufSize = size;
|
|
702 |
}
|
|
703 |
|
|
704 |
uint32_t
|
|
705 |
NscTcpSocketImpl::GetRcvBufSize (void) const
|
|
706 |
{
|
|
707 |
return m_rcvBufSize;
|
|
708 |
}
|
|
709 |
|
|
710 |
void
|
|
711 |
NscTcpSocketImpl::SetSegSize (uint32_t size)
|
|
712 |
{
|
|
713 |
m_segmentSize = size;
|
|
714 |
}
|
|
715 |
|
|
716 |
uint32_t
|
|
717 |
NscTcpSocketImpl::GetSegSize (void) const
|
|
718 |
{
|
|
719 |
return m_segmentSize;
|
|
720 |
}
|
|
721 |
|
|
722 |
void
|
|
723 |
NscTcpSocketImpl::SetAdvWin (uint32_t window)
|
|
724 |
{
|
|
725 |
m_advertisedWindowSize = window;
|
|
726 |
}
|
|
727 |
|
|
728 |
uint32_t
|
|
729 |
NscTcpSocketImpl::GetAdvWin (void) const
|
|
730 |
{
|
|
731 |
return m_advertisedWindowSize;
|
|
732 |
}
|
|
733 |
|
|
734 |
void
|
|
735 |
NscTcpSocketImpl::SetSSThresh (uint32_t threshold)
|
|
736 |
{
|
|
737 |
m_ssThresh = threshold;
|
|
738 |
}
|
|
739 |
|
|
740 |
uint32_t
|
|
741 |
NscTcpSocketImpl::GetSSThresh (void) const
|
|
742 |
{
|
|
743 |
return m_ssThresh;
|
|
744 |
}
|
|
745 |
|
|
746 |
void
|
|
747 |
NscTcpSocketImpl::SetInitialCwnd (uint32_t cwnd)
|
|
748 |
{
|
|
749 |
m_initialCWnd = cwnd;
|
|
750 |
}
|
|
751 |
|
|
752 |
uint32_t
|
|
753 |
NscTcpSocketImpl::GetInitialCwnd (void) const
|
|
754 |
{
|
|
755 |
return m_initialCWnd;
|
|
756 |
}
|
|
757 |
|
|
758 |
void
|
|
759 |
NscTcpSocketImpl::SetConnTimeout (Time timeout)
|
|
760 |
{
|
|
761 |
m_cnTimeout = timeout;
|
|
762 |
}
|
|
763 |
|
|
764 |
Time
|
|
765 |
NscTcpSocketImpl::GetConnTimeout (void) const
|
|
766 |
{
|
|
767 |
return m_cnTimeout;
|
|
768 |
}
|
|
769 |
|
|
770 |
void
|
|
771 |
NscTcpSocketImpl::SetConnCount (uint32_t count)
|
|
772 |
{
|
|
773 |
m_cnCount = count;
|
|
774 |
}
|
|
775 |
|
|
776 |
uint32_t
|
|
777 |
NscTcpSocketImpl::GetConnCount (void) const
|
|
778 |
{
|
|
779 |
return m_cnCount;
|
|
780 |
}
|
|
781 |
|
|
782 |
void
|
|
783 |
NscTcpSocketImpl::SetDelAckTimeout (Time timeout)
|
|
784 |
{
|
|
785 |
m_delAckTimeout = timeout;
|
|
786 |
}
|
|
787 |
|
|
788 |
Time
|
|
789 |
NscTcpSocketImpl::GetDelAckTimeout (void) const
|
|
790 |
{
|
|
791 |
return m_delAckTimeout;
|
|
792 |
}
|
|
793 |
|
|
794 |
void
|
|
795 |
NscTcpSocketImpl::SetDelAckMaxCount (uint32_t count)
|
|
796 |
{
|
|
797 |
m_delAckMaxCount = count;
|
|
798 |
}
|
|
799 |
|
|
800 |
uint32_t
|
|
801 |
NscTcpSocketImpl::GetDelAckMaxCount (void) const
|
|
802 |
{
|
|
803 |
return m_delAckMaxCount;
|
|
804 |
}
|
|
805 |
|
|
806 |
enum Socket::SocketErrno
|
|
807 |
NscTcpSocketImpl::GetNativeNs3Errno(int error) const
|
|
808 |
{
|
|
809 |
enum nsc_errno err;
|
|
810 |
|
|
811 |
if (error >= 0)
|
|
812 |
{
|
|
813 |
return ERROR_NOTERROR;
|
|
814 |
}
|
|
815 |
err = (enum nsc_errno) error;
|
|
816 |
switch (err)
|
|
817 |
{
|
|
818 |
case NSC_EADDRINUSE: // fallthrough
|
|
819 |
case NSC_EADDRNOTAVAIL: return ERROR_AFNOSUPPORT;
|
|
820 |
case NSC_EINPROGRESS: // Altough nsc sockets are nonblocking, we pretend they're not.
|
|
821 |
case NSC_EAGAIN: return ERROR_AGAIN;
|
|
822 |
case NSC_EISCONN: // fallthrough
|
|
823 |
case NSC_EALREADY: return ERROR_ISCONN;
|
|
824 |
case NSC_ECONNREFUSED: return ERROR_NOROUTETOHOST; // XXX, better mapping?
|
|
825 |
case NSC_ECONNRESET: // for no, all of these fall through
|
|
826 |
case NSC_EHOSTDOWN:
|
|
827 |
case NSC_ENETUNREACH:
|
|
828 |
case NSC_EHOSTUNREACH: return ERROR_NOROUTETOHOST;
|
|
829 |
case NSC_EMSGSIZE: return ERROR_MSGSIZE;
|
|
830 |
case NSC_ENOTCONN: return ERROR_NOTCONN;
|
|
831 |
case NSC_ESHUTDOWN: return ERROR_SHUTDOWN;
|
|
832 |
case NSC_ETIMEDOUT: return ERROR_NOTCONN; // XXX - this mapping isn't correct
|
|
833 |
case NSC_ENOTDIR: // used by eg. sysctl(2). Shouldn't happen normally,
|
|
834 |
// but is triggered by e.g. show_config().
|
|
835 |
case NSC_EUNKNOWN: return ERROR_INVAL; // Catches stacks that 'return -1' without real mapping
|
|
836 |
}
|
|
837 |
NS_ASSERT_MSG(0, "Unknown NSC error");
|
|
838 |
return ERROR_INVAL;
|
|
839 |
}
|
|
840 |
|
|
841 |
}//namespace ns3
|