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