1 /* 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/p2p/base/pseudotcp.h" 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 16 #include <algorithm> 17 #include <set> 18 19 #include "webrtc/base/arraysize.h" 20 #include "webrtc/base/basictypes.h" 21 #include "webrtc/base/bytebuffer.h" 22 #include "webrtc/base/byteorder.h" 23 #include "webrtc/base/common.h" 24 #include "webrtc/base/logging.h" 25 #include "webrtc/base/scoped_ptr.h" 26 #include "webrtc/base/socket.h" 27 #include "webrtc/base/stringutils.h" 28 #include "webrtc/base/timeutils.h" 29 30 // The following logging is for detailed (packet-level) analysis only. 31 #define _DBG_NONE 0 32 #define _DBG_NORMAL 1 33 #define _DBG_VERBOSE 2 34 #define _DEBUGMSG _DBG_NONE 35 36 namespace cricket { 37 38 ////////////////////////////////////////////////////////////////////// 39 // Network Constants 40 ////////////////////////////////////////////////////////////////////// 41 42 // Standard MTUs 43 const uint16_t PACKET_MAXIMUMS[] = { 44 65535, // Theoretical maximum, Hyperchannel 45 32000, // Nothing 46 17914, // 16Mb IBM Token Ring 47 8166, // IEEE 802.4 48 // 4464, // IEEE 802.5 (4Mb max) 49 4352, // FDDI 50 // 2048, // Wideband Network 51 2002, // IEEE 802.5 (4Mb recommended) 52 // 1536, // Expermental Ethernet Networks 53 // 1500, // Ethernet, Point-to-Point (default) 54 1492, // IEEE 802.3 55 1006, // SLIP, ARPANET 56 // 576, // X.25 Networks 57 // 544, // DEC IP Portal 58 // 512, // NETBIOS 59 508, // IEEE 802/Source-Rt Bridge, ARCNET 60 296, // Point-to-Point (low delay) 61 // 68, // Official minimum 62 0, // End of list marker 63 }; 64 65 const uint32_t MAX_PACKET = 65535; 66 // Note: we removed lowest level because packet overhead was larger! 67 const uint32_t MIN_PACKET = 296; 68 69 const uint32_t IP_HEADER_SIZE = 20; // (+ up to 40 bytes of options?) 70 const uint32_t UDP_HEADER_SIZE = 8; 71 // TODO: Make JINGLE_HEADER_SIZE transparent to this code? 72 const uint32_t JINGLE_HEADER_SIZE = 64; // when relay framing is in use 73 74 // Default size for receive and send buffer. 75 const uint32_t DEFAULT_RCV_BUF_SIZE = 60 * 1024; 76 const uint32_t DEFAULT_SND_BUF_SIZE = 90 * 1024; 77 78 ////////////////////////////////////////////////////////////////////// 79 // Global Constants and Functions 80 ////////////////////////////////////////////////////////////////////// 81 // 82 // 0 1 2 3 83 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 84 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 85 // 0 | Conversation Number | 86 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 87 // 4 | Sequence Number | 88 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 89 // 8 | Acknowledgment Number | 90 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 91 // | | |U|A|P|R|S|F| | 92 // 12 | Control | |R|C|S|S|Y|I| Window | 93 // | | |G|K|H|T|N|N| | 94 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 95 // 16 | Timestamp sending | 96 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 97 // 20 | Timestamp receiving | 98 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 99 // 24 | data | 100 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 101 // 102 ////////////////////////////////////////////////////////////////////// 103 104 #define PSEUDO_KEEPALIVE 0 105 106 const uint32_t HEADER_SIZE = 24; 107 const uint32_t PACKET_OVERHEAD = 108 HEADER_SIZE + UDP_HEADER_SIZE + IP_HEADER_SIZE + JINGLE_HEADER_SIZE; 109 110 const uint32_t MIN_RTO = 111 250; // 250 ms (RFC1122, Sec 4.2.3.1 "fractions of a second") 112 const uint32_t DEF_RTO = 3000; // 3 seconds (RFC1122, Sec 4.2.3.1) 113 const uint32_t MAX_RTO = 60000; // 60 seconds 114 const uint32_t DEF_ACK_DELAY = 100; // 100 milliseconds 115 116 const uint8_t FLAG_CTL = 0x02; 117 const uint8_t FLAG_RST = 0x04; 118 119 const uint8_t CTL_CONNECT = 0; 120 121 // TCP options. 122 const uint8_t TCP_OPT_EOL = 0; // End of list. 123 const uint8_t TCP_OPT_NOOP = 1; // No-op. 124 const uint8_t TCP_OPT_MSS = 2; // Maximum segment size. 125 const uint8_t TCP_OPT_WND_SCALE = 3; // Window scale factor. 126 127 const long DEFAULT_TIMEOUT = 4000; // If there are no pending clocks, wake up every 4 seconds 128 const long CLOSED_TIMEOUT = 60 * 1000; // If the connection is closed, once per minute 129 130 #if PSEUDO_KEEPALIVE 131 // !?! Rethink these times 132 const uint32_t IDLE_PING = 133 20 * 134 1000; // 20 seconds (note: WinXP SP2 firewall udp timeout is 90 seconds) 135 const uint32_t IDLE_TIMEOUT = 90 * 1000; // 90 seconds; 136 #endif // PSEUDO_KEEPALIVE 137 138 ////////////////////////////////////////////////////////////////////// 139 // Helper Functions 140 ////////////////////////////////////////////////////////////////////// 141 142 inline void long_to_bytes(uint32_t val, void* buf) { 143 *static_cast<uint32_t*>(buf) = rtc::HostToNetwork32(val); 144 } 145 146 inline void short_to_bytes(uint16_t val, void* buf) { 147 *static_cast<uint16_t*>(buf) = rtc::HostToNetwork16(val); 148 } 149 150 inline uint32_t bytes_to_long(const void* buf) { 151 return rtc::NetworkToHost32(*static_cast<const uint32_t*>(buf)); 152 } 153 154 inline uint16_t bytes_to_short(const void* buf) { 155 return rtc::NetworkToHost16(*static_cast<const uint16_t*>(buf)); 156 } 157 158 uint32_t bound(uint32_t lower, uint32_t middle, uint32_t upper) { 159 return std::min(std::max(lower, middle), upper); 160 } 161 162 ////////////////////////////////////////////////////////////////////// 163 // Debugging Statistics 164 ////////////////////////////////////////////////////////////////////// 165 166 #if 0 // Not used yet 167 168 enum Stat { 169 S_SENT_PACKET, // All packet sends 170 S_RESENT_PACKET, // All packet sends that are retransmits 171 S_RECV_PACKET, // All packet receives 172 S_RECV_NEW, // All packet receives that are too new 173 S_RECV_OLD, // All packet receives that are too old 174 S_NUM_STATS 175 }; 176 177 const char* const STAT_NAMES[S_NUM_STATS] = { 178 "snt", 179 "snt-r", 180 "rcv" 181 "rcv-n", 182 "rcv-o" 183 }; 184 185 int g_stats[S_NUM_STATS]; 186 inline void Incr(Stat s) { ++g_stats[s]; } 187 void ReportStats() { 188 char buffer[256]; 189 size_t len = 0; 190 for (int i = 0; i < S_NUM_STATS; ++i) { 191 len += rtc::sprintfn(buffer, arraysize(buffer), "%s%s:%d", 192 (i == 0) ? "" : ",", STAT_NAMES[i], g_stats[i]); 193 g_stats[i] = 0; 194 } 195 LOG(LS_INFO) << "Stats[" << buffer << "]"; 196 } 197 198 #endif 199 200 ////////////////////////////////////////////////////////////////////// 201 // PseudoTcp 202 ////////////////////////////////////////////////////////////////////// 203 204 uint32_t PseudoTcp::Now() { 205 #if 0 // Use this to synchronize timers with logging timestamps (easier debug) 206 return rtc::TimeSince(StartTime()); 207 #else 208 return rtc::Time(); 209 #endif 210 } 211 212 PseudoTcp::PseudoTcp(IPseudoTcpNotify* notify, uint32_t conv) 213 : m_notify(notify), 214 m_shutdown(SD_NONE), 215 m_error(0), 216 m_rbuf_len(DEFAULT_RCV_BUF_SIZE), 217 m_rbuf(m_rbuf_len), 218 m_sbuf_len(DEFAULT_SND_BUF_SIZE), 219 m_sbuf(m_sbuf_len) { 220 // Sanity check on buffer sizes (needed for OnTcpWriteable notification logic) 221 ASSERT(m_rbuf_len + MIN_PACKET < m_sbuf_len); 222 223 uint32_t now = Now(); 224 225 m_state = TCP_LISTEN; 226 m_conv = conv; 227 m_rcv_wnd = m_rbuf_len; 228 m_rwnd_scale = m_swnd_scale = 0; 229 m_snd_nxt = 0; 230 m_snd_wnd = 1; 231 m_snd_una = m_rcv_nxt = 0; 232 m_bReadEnable = true; 233 m_bWriteEnable = false; 234 m_t_ack = 0; 235 236 m_msslevel = 0; 237 m_largest = 0; 238 ASSERT(MIN_PACKET > PACKET_OVERHEAD); 239 m_mss = MIN_PACKET - PACKET_OVERHEAD; 240 m_mtu_advise = MAX_PACKET; 241 242 m_rto_base = 0; 243 244 m_cwnd = 2 * m_mss; 245 m_ssthresh = m_rbuf_len; 246 m_lastrecv = m_lastsend = m_lasttraffic = now; 247 m_bOutgoing = false; 248 249 m_dup_acks = 0; 250 m_recover = 0; 251 252 m_ts_recent = m_ts_lastack = 0; 253 254 m_rx_rto = DEF_RTO; 255 m_rx_srtt = m_rx_rttvar = 0; 256 257 m_use_nagling = true; 258 m_ack_delay = DEF_ACK_DELAY; 259 m_support_wnd_scale = true; 260 } 261 262 PseudoTcp::~PseudoTcp() { 263 } 264 265 int PseudoTcp::Connect() { 266 if (m_state != TCP_LISTEN) { 267 m_error = EINVAL; 268 return -1; 269 } 270 271 m_state = TCP_SYN_SENT; 272 LOG(LS_INFO) << "State: TCP_SYN_SENT"; 273 274 queueConnectMessage(); 275 attemptSend(); 276 277 return 0; 278 } 279 280 void PseudoTcp::NotifyMTU(uint16_t mtu) { 281 m_mtu_advise = mtu; 282 if (m_state == TCP_ESTABLISHED) { 283 adjustMTU(); 284 } 285 } 286 287 void PseudoTcp::NotifyClock(uint32_t now) { 288 if (m_state == TCP_CLOSED) 289 return; 290 291 // Check if it's time to retransmit a segment 292 if (m_rto_base && (rtc::TimeDiff(m_rto_base + m_rx_rto, now) <= 0)) { 293 if (m_slist.empty()) { 294 ASSERT(false); 295 } else { 296 // Note: (m_slist.front().xmit == 0)) { 297 // retransmit segments 298 #if _DEBUGMSG >= _DBG_NORMAL 299 LOG(LS_INFO) << "timeout retransmit (rto: " << m_rx_rto 300 << ") (rto_base: " << m_rto_base 301 << ") (now: " << now 302 << ") (dup_acks: " << static_cast<unsigned>(m_dup_acks) 303 << ")"; 304 #endif // _DEBUGMSG 305 if (!transmit(m_slist.begin(), now)) { 306 closedown(ECONNABORTED); 307 return; 308 } 309 310 uint32_t nInFlight = m_snd_nxt - m_snd_una; 311 m_ssthresh = std::max(nInFlight / 2, 2 * m_mss); 312 //LOG(LS_INFO) << "m_ssthresh: " << m_ssthresh << " nInFlight: " << nInFlight << " m_mss: " << m_mss; 313 m_cwnd = m_mss; 314 315 // Back off retransmit timer. Note: the limit is lower when connecting. 316 uint32_t rto_limit = (m_state < TCP_ESTABLISHED) ? DEF_RTO : MAX_RTO; 317 m_rx_rto = std::min(rto_limit, m_rx_rto * 2); 318 m_rto_base = now; 319 } 320 } 321 322 // Check if it's time to probe closed windows 323 if ((m_snd_wnd == 0) 324 && (rtc::TimeDiff(m_lastsend + m_rx_rto, now) <= 0)) { 325 if (rtc::TimeDiff(now, m_lastrecv) >= 15000) { 326 closedown(ECONNABORTED); 327 return; 328 } 329 330 // probe the window 331 packet(m_snd_nxt - 1, 0, 0, 0); 332 m_lastsend = now; 333 334 // back off retransmit timer 335 m_rx_rto = std::min(MAX_RTO, m_rx_rto * 2); 336 } 337 338 // Check if it's time to send delayed acks 339 if (m_t_ack && (rtc::TimeDiff(m_t_ack + m_ack_delay, now) <= 0)) { 340 packet(m_snd_nxt, 0, 0, 0); 341 } 342 343 #if PSEUDO_KEEPALIVE 344 // Check for idle timeout 345 if ((m_state == TCP_ESTABLISHED) && (TimeDiff(m_lastrecv + IDLE_TIMEOUT, now) <= 0)) { 346 closedown(ECONNABORTED); 347 return; 348 } 349 350 // Check for ping timeout (to keep udp mapping open) 351 if ((m_state == TCP_ESTABLISHED) && (TimeDiff(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3/2 : IDLE_PING), now) <= 0)) { 352 packet(m_snd_nxt, 0, 0, 0); 353 } 354 #endif // PSEUDO_KEEPALIVE 355 } 356 357 bool PseudoTcp::NotifyPacket(const char* buffer, size_t len) { 358 if (len > MAX_PACKET) { 359 LOG_F(WARNING) << "packet too large"; 360 return false; 361 } 362 return parse(reinterpret_cast<const uint8_t*>(buffer), uint32_t(len)); 363 } 364 365 bool PseudoTcp::GetNextClock(uint32_t now, long& timeout) { 366 return clock_check(now, timeout); 367 } 368 369 void PseudoTcp::GetOption(Option opt, int* value) { 370 if (opt == OPT_NODELAY) { 371 *value = m_use_nagling ? 0 : 1; 372 } else if (opt == OPT_ACKDELAY) { 373 *value = m_ack_delay; 374 } else if (opt == OPT_SNDBUF) { 375 *value = m_sbuf_len; 376 } else if (opt == OPT_RCVBUF) { 377 *value = m_rbuf_len; 378 } else { 379 ASSERT(false); 380 } 381 } 382 void PseudoTcp::SetOption(Option opt, int value) { 383 if (opt == OPT_NODELAY) { 384 m_use_nagling = value == 0; 385 } else if (opt == OPT_ACKDELAY) { 386 m_ack_delay = value; 387 } else if (opt == OPT_SNDBUF) { 388 ASSERT(m_state == TCP_LISTEN); 389 resizeSendBuffer(value); 390 } else if (opt == OPT_RCVBUF) { 391 ASSERT(m_state == TCP_LISTEN); 392 resizeReceiveBuffer(value); 393 } else { 394 ASSERT(false); 395 } 396 } 397 398 uint32_t PseudoTcp::GetCongestionWindow() const { 399 return m_cwnd; 400 } 401 402 uint32_t PseudoTcp::GetBytesInFlight() const { 403 return m_snd_nxt - m_snd_una; 404 } 405 406 uint32_t PseudoTcp::GetBytesBufferedNotSent() const { 407 size_t buffered_bytes = 0; 408 m_sbuf.GetBuffered(&buffered_bytes); 409 return static_cast<uint32_t>(m_snd_una + buffered_bytes - m_snd_nxt); 410 } 411 412 uint32_t PseudoTcp::GetRoundTripTimeEstimateMs() const { 413 return m_rx_srtt; 414 } 415 416 // 417 // IPStream Implementation 418 // 419 420 int PseudoTcp::Recv(char* buffer, size_t len) { 421 if (m_state != TCP_ESTABLISHED) { 422 m_error = ENOTCONN; 423 return SOCKET_ERROR; 424 } 425 426 size_t read = 0; 427 rtc::StreamResult result = m_rbuf.Read(buffer, len, &read, NULL); 428 429 // If there's no data in |m_rbuf|. 430 if (result == rtc::SR_BLOCK) { 431 m_bReadEnable = true; 432 m_error = EWOULDBLOCK; 433 return SOCKET_ERROR; 434 } 435 ASSERT(result == rtc::SR_SUCCESS); 436 437 size_t available_space = 0; 438 m_rbuf.GetWriteRemaining(&available_space); 439 440 if (uint32_t(available_space) - m_rcv_wnd >= 441 std::min<uint32_t>(m_rbuf_len / 2, m_mss)) { 442 // TODO(jbeda): !?! Not sure about this was closed business 443 bool bWasClosed = (m_rcv_wnd == 0); 444 m_rcv_wnd = static_cast<uint32_t>(available_space); 445 446 if (bWasClosed) { 447 attemptSend(sfImmediateAck); 448 } 449 } 450 451 return static_cast<int>(read); 452 } 453 454 int PseudoTcp::Send(const char* buffer, size_t len) { 455 if (m_state != TCP_ESTABLISHED) { 456 m_error = ENOTCONN; 457 return SOCKET_ERROR; 458 } 459 460 size_t available_space = 0; 461 m_sbuf.GetWriteRemaining(&available_space); 462 463 if (!available_space) { 464 m_bWriteEnable = true; 465 m_error = EWOULDBLOCK; 466 return SOCKET_ERROR; 467 } 468 469 int written = queue(buffer, uint32_t(len), false); 470 attemptSend(); 471 return written; 472 } 473 474 void PseudoTcp::Close(bool force) { 475 LOG_F(LS_VERBOSE) << "(" << (force ? "true" : "false") << ")"; 476 m_shutdown = force ? SD_FORCEFUL : SD_GRACEFUL; 477 } 478 479 int PseudoTcp::GetError() { 480 return m_error; 481 } 482 483 // 484 // Internal Implementation 485 // 486 487 uint32_t PseudoTcp::queue(const char* data, uint32_t len, bool bCtrl) { 488 size_t available_space = 0; 489 m_sbuf.GetWriteRemaining(&available_space); 490 491 if (len > static_cast<uint32_t>(available_space)) { 492 ASSERT(!bCtrl); 493 len = static_cast<uint32_t>(available_space); 494 } 495 496 // We can concatenate data if the last segment is the same type 497 // (control v. regular data), and has not been transmitted yet 498 if (!m_slist.empty() && (m_slist.back().bCtrl == bCtrl) && 499 (m_slist.back().xmit == 0)) { 500 m_slist.back().len += len; 501 } else { 502 size_t snd_buffered = 0; 503 m_sbuf.GetBuffered(&snd_buffered); 504 SSegment sseg(static_cast<uint32_t>(m_snd_una + snd_buffered), len, bCtrl); 505 m_slist.push_back(sseg); 506 } 507 508 size_t written = 0; 509 m_sbuf.Write(data, len, &written, NULL); 510 return static_cast<uint32_t>(written); 511 } 512 513 IPseudoTcpNotify::WriteResult PseudoTcp::packet(uint32_t seq, 514 uint8_t flags, 515 uint32_t offset, 516 uint32_t len) { 517 ASSERT(HEADER_SIZE + len <= MAX_PACKET); 518 519 uint32_t now = Now(); 520 521 rtc::scoped_ptr<uint8_t[]> buffer(new uint8_t[MAX_PACKET]); 522 long_to_bytes(m_conv, buffer.get()); 523 long_to_bytes(seq, buffer.get() + 4); 524 long_to_bytes(m_rcv_nxt, buffer.get() + 8); 525 buffer[12] = 0; 526 buffer[13] = flags; 527 short_to_bytes(static_cast<uint16_t>(m_rcv_wnd >> m_rwnd_scale), 528 buffer.get() + 14); 529 530 // Timestamp computations 531 long_to_bytes(now, buffer.get() + 16); 532 long_to_bytes(m_ts_recent, buffer.get() + 20); 533 m_ts_lastack = m_rcv_nxt; 534 535 if (len) { 536 size_t bytes_read = 0; 537 rtc::StreamResult result = m_sbuf.ReadOffset( 538 buffer.get() + HEADER_SIZE, len, offset, &bytes_read); 539 RTC_UNUSED(result); 540 ASSERT(result == rtc::SR_SUCCESS); 541 ASSERT(static_cast<uint32_t>(bytes_read) == len); 542 } 543 544 #if _DEBUGMSG >= _DBG_VERBOSE 545 LOG(LS_INFO) << "<-- <CONV=" << m_conv 546 << "><FLG=" << static_cast<unsigned>(flags) 547 << "><SEQ=" << seq << ":" << seq + len 548 << "><ACK=" << m_rcv_nxt 549 << "><WND=" << m_rcv_wnd 550 << "><TS=" << (now % 10000) 551 << "><TSR=" << (m_ts_recent % 10000) 552 << "><LEN=" << len << ">"; 553 #endif // _DEBUGMSG 554 555 IPseudoTcpNotify::WriteResult wres = m_notify->TcpWritePacket( 556 this, reinterpret_cast<char *>(buffer.get()), len + HEADER_SIZE); 557 // Note: When len is 0, this is an ACK packet. We don't read the return value for those, 558 // and thus we won't retry. So go ahead and treat the packet as a success (basically simulate 559 // as if it were dropped), which will prevent our timers from being messed up. 560 if ((wres != IPseudoTcpNotify::WR_SUCCESS) && (0 != len)) 561 return wres; 562 563 m_t_ack = 0; 564 if (len > 0) { 565 m_lastsend = now; 566 } 567 m_lasttraffic = now; 568 m_bOutgoing = true; 569 570 return IPseudoTcpNotify::WR_SUCCESS; 571 } 572 573 bool PseudoTcp::parse(const uint8_t* buffer, uint32_t size) { 574 if (size < 12) 575 return false; 576 577 Segment seg; 578 seg.conv = bytes_to_long(buffer); 579 seg.seq = bytes_to_long(buffer + 4); 580 seg.ack = bytes_to_long(buffer + 8); 581 seg.flags = buffer[13]; 582 seg.wnd = bytes_to_short(buffer + 14); 583 584 seg.tsval = bytes_to_long(buffer + 16); 585 seg.tsecr = bytes_to_long(buffer + 20); 586 587 seg.data = reinterpret_cast<const char *>(buffer) + HEADER_SIZE; 588 seg.len = size - HEADER_SIZE; 589 590 #if _DEBUGMSG >= _DBG_VERBOSE 591 LOG(LS_INFO) << "--> <CONV=" << seg.conv 592 << "><FLG=" << static_cast<unsigned>(seg.flags) 593 << "><SEQ=" << seg.seq << ":" << seg.seq + seg.len 594 << "><ACK=" << seg.ack 595 << "><WND=" << seg.wnd 596 << "><TS=" << (seg.tsval % 10000) 597 << "><TSR=" << (seg.tsecr % 10000) 598 << "><LEN=" << seg.len << ">"; 599 #endif // _DEBUGMSG 600 601 return process(seg); 602 } 603 604 bool PseudoTcp::clock_check(uint32_t now, long& nTimeout) { 605 if (m_shutdown == SD_FORCEFUL) 606 return false; 607 608 size_t snd_buffered = 0; 609 m_sbuf.GetBuffered(&snd_buffered); 610 if ((m_shutdown == SD_GRACEFUL) 611 && ((m_state != TCP_ESTABLISHED) 612 || ((snd_buffered == 0) && (m_t_ack == 0)))) { 613 return false; 614 } 615 616 if (m_state == TCP_CLOSED) { 617 nTimeout = CLOSED_TIMEOUT; 618 return true; 619 } 620 621 nTimeout = DEFAULT_TIMEOUT; 622 623 if (m_t_ack) { 624 nTimeout = 625 std::min<int32_t>(nTimeout, rtc::TimeDiff(m_t_ack + m_ack_delay, now)); 626 } 627 if (m_rto_base) { 628 nTimeout = 629 std::min<int32_t>(nTimeout, rtc::TimeDiff(m_rto_base + m_rx_rto, now)); 630 } 631 if (m_snd_wnd == 0) { 632 nTimeout = 633 std::min<int32_t>(nTimeout, rtc::TimeDiff(m_lastsend + m_rx_rto, now)); 634 } 635 #if PSEUDO_KEEPALIVE 636 if (m_state == TCP_ESTABLISHED) { 637 nTimeout = std::min<int32_t>( 638 nTimeout, rtc::TimeDiff(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3 / 2 639 : IDLE_PING), 640 now)); 641 } 642 #endif // PSEUDO_KEEPALIVE 643 return true; 644 } 645 646 bool PseudoTcp::process(Segment& seg) { 647 // If this is the wrong conversation, send a reset!?! (with the correct conversation?) 648 if (seg.conv != m_conv) { 649 //if ((seg.flags & FLAG_RST) == 0) { 650 // packet(tcb, seg.ack, 0, FLAG_RST, 0, 0); 651 //} 652 LOG_F(LS_ERROR) << "wrong conversation"; 653 return false; 654 } 655 656 uint32_t now = Now(); 657 m_lasttraffic = m_lastrecv = now; 658 m_bOutgoing = false; 659 660 if (m_state == TCP_CLOSED) { 661 // !?! send reset? 662 LOG_F(LS_ERROR) << "closed"; 663 return false; 664 } 665 666 // Check if this is a reset segment 667 if (seg.flags & FLAG_RST) { 668 closedown(ECONNRESET); 669 return false; 670 } 671 672 // Check for control data 673 bool bConnect = false; 674 if (seg.flags & FLAG_CTL) { 675 if (seg.len == 0) { 676 LOG_F(LS_ERROR) << "Missing control code"; 677 return false; 678 } else if (seg.data[0] == CTL_CONNECT) { 679 bConnect = true; 680 681 // TCP options are in the remainder of the payload after CTL_CONNECT. 682 parseOptions(&seg.data[1], seg.len - 1); 683 684 if (m_state == TCP_LISTEN) { 685 m_state = TCP_SYN_RECEIVED; 686 LOG(LS_INFO) << "State: TCP_SYN_RECEIVED"; 687 //m_notify->associate(addr); 688 queueConnectMessage(); 689 } else if (m_state == TCP_SYN_SENT) { 690 m_state = TCP_ESTABLISHED; 691 LOG(LS_INFO) << "State: TCP_ESTABLISHED"; 692 adjustMTU(); 693 if (m_notify) { 694 m_notify->OnTcpOpen(this); 695 } 696 //notify(evOpen); 697 } 698 } else { 699 LOG_F(LS_WARNING) << "Unknown control code: " << seg.data[0]; 700 return false; 701 } 702 } 703 704 // Update timestamp 705 if ((seg.seq <= m_ts_lastack) && (m_ts_lastack < seg.seq + seg.len)) { 706 m_ts_recent = seg.tsval; 707 } 708 709 // Check if this is a valuable ack 710 if ((seg.ack > m_snd_una) && (seg.ack <= m_snd_nxt)) { 711 // Calculate round-trip time 712 if (seg.tsecr) { 713 int32_t rtt = rtc::TimeDiff(now, seg.tsecr); 714 if (rtt >= 0) { 715 if (m_rx_srtt == 0) { 716 m_rx_srtt = rtt; 717 m_rx_rttvar = rtt / 2; 718 } else { 719 uint32_t unsigned_rtt = static_cast<uint32_t>(rtt); 720 uint32_t abs_err = unsigned_rtt > m_rx_srtt 721 ? unsigned_rtt - m_rx_srtt 722 : m_rx_srtt - unsigned_rtt; 723 m_rx_rttvar = (3 * m_rx_rttvar + abs_err) / 4; 724 m_rx_srtt = (7 * m_rx_srtt + rtt) / 8; 725 } 726 m_rx_rto = 727 bound(MIN_RTO, m_rx_srtt + std::max<uint32_t>(1, 4 * m_rx_rttvar), 728 MAX_RTO); 729 #if _DEBUGMSG >= _DBG_VERBOSE 730 LOG(LS_INFO) << "rtt: " << rtt 731 << " srtt: " << m_rx_srtt 732 << " rto: " << m_rx_rto; 733 #endif // _DEBUGMSG 734 } else { 735 ASSERT(false); 736 } 737 } 738 739 m_snd_wnd = static_cast<uint32_t>(seg.wnd) << m_swnd_scale; 740 741 uint32_t nAcked = seg.ack - m_snd_una; 742 m_snd_una = seg.ack; 743 744 m_rto_base = (m_snd_una == m_snd_nxt) ? 0 : now; 745 746 m_sbuf.ConsumeReadData(nAcked); 747 748 for (uint32_t nFree = nAcked; nFree > 0;) { 749 ASSERT(!m_slist.empty()); 750 if (nFree < m_slist.front().len) { 751 m_slist.front().len -= nFree; 752 nFree = 0; 753 } else { 754 if (m_slist.front().len > m_largest) { 755 m_largest = m_slist.front().len; 756 } 757 nFree -= m_slist.front().len; 758 m_slist.pop_front(); 759 } 760 } 761 762 if (m_dup_acks >= 3) { 763 if (m_snd_una >= m_recover) { // NewReno 764 uint32_t nInFlight = m_snd_nxt - m_snd_una; 765 m_cwnd = std::min(m_ssthresh, nInFlight + m_mss); // (Fast Retransmit) 766 #if _DEBUGMSG >= _DBG_NORMAL 767 LOG(LS_INFO) << "exit recovery"; 768 #endif // _DEBUGMSG 769 m_dup_acks = 0; 770 } else { 771 #if _DEBUGMSG >= _DBG_NORMAL 772 LOG(LS_INFO) << "recovery retransmit"; 773 #endif // _DEBUGMSG 774 if (!transmit(m_slist.begin(), now)) { 775 closedown(ECONNABORTED); 776 return false; 777 } 778 m_cwnd += m_mss - std::min(nAcked, m_cwnd); 779 } 780 } else { 781 m_dup_acks = 0; 782 // Slow start, congestion avoidance 783 if (m_cwnd < m_ssthresh) { 784 m_cwnd += m_mss; 785 } else { 786 m_cwnd += std::max<uint32_t>(1, m_mss * m_mss / m_cwnd); 787 } 788 } 789 } else if (seg.ack == m_snd_una) { 790 // !?! Note, tcp says don't do this... but otherwise how does a closed window become open? 791 m_snd_wnd = static_cast<uint32_t>(seg.wnd) << m_swnd_scale; 792 793 // Check duplicate acks 794 if (seg.len > 0) { 795 // it's a dup ack, but with a data payload, so don't modify m_dup_acks 796 } else if (m_snd_una != m_snd_nxt) { 797 m_dup_acks += 1; 798 if (m_dup_acks == 3) { // (Fast Retransmit) 799 #if _DEBUGMSG >= _DBG_NORMAL 800 LOG(LS_INFO) << "enter recovery"; 801 LOG(LS_INFO) << "recovery retransmit"; 802 #endif // _DEBUGMSG 803 if (!transmit(m_slist.begin(), now)) { 804 closedown(ECONNABORTED); 805 return false; 806 } 807 m_recover = m_snd_nxt; 808 uint32_t nInFlight = m_snd_nxt - m_snd_una; 809 m_ssthresh = std::max(nInFlight / 2, 2 * m_mss); 810 //LOG(LS_INFO) << "m_ssthresh: " << m_ssthresh << " nInFlight: " << nInFlight << " m_mss: " << m_mss; 811 m_cwnd = m_ssthresh + 3 * m_mss; 812 } else if (m_dup_acks > 3) { 813 m_cwnd += m_mss; 814 } 815 } else { 816 m_dup_acks = 0; 817 } 818 } 819 820 // !?! A bit hacky 821 if ((m_state == TCP_SYN_RECEIVED) && !bConnect) { 822 m_state = TCP_ESTABLISHED; 823 LOG(LS_INFO) << "State: TCP_ESTABLISHED"; 824 adjustMTU(); 825 if (m_notify) { 826 m_notify->OnTcpOpen(this); 827 } 828 //notify(evOpen); 829 } 830 831 // If we make room in the send queue, notify the user 832 // The goal it to make sure we always have at least enough data to fill the 833 // window. We'd like to notify the app when we are halfway to that point. 834 const uint32_t kIdealRefillSize = (m_sbuf_len + m_rbuf_len) / 2; 835 size_t snd_buffered = 0; 836 m_sbuf.GetBuffered(&snd_buffered); 837 if (m_bWriteEnable && 838 static_cast<uint32_t>(snd_buffered) < kIdealRefillSize) { 839 m_bWriteEnable = false; 840 if (m_notify) { 841 m_notify->OnTcpWriteable(this); 842 } 843 //notify(evWrite); 844 } 845 846 // Conditions were acks must be sent: 847 // 1) Segment is too old (they missed an ACK) (immediately) 848 // 2) Segment is too new (we missed a segment) (immediately) 849 // 3) Segment has data (so we need to ACK!) (delayed) 850 // ... so the only time we don't need to ACK, is an empty segment that points to rcv_nxt! 851 852 SendFlags sflags = sfNone; 853 if (seg.seq != m_rcv_nxt) { 854 sflags = sfImmediateAck; // (Fast Recovery) 855 } else if (seg.len != 0) { 856 if (m_ack_delay == 0) { 857 sflags = sfImmediateAck; 858 } else { 859 sflags = sfDelayedAck; 860 } 861 } 862 #if _DEBUGMSG >= _DBG_NORMAL 863 if (sflags == sfImmediateAck) { 864 if (seg.seq > m_rcv_nxt) { 865 LOG_F(LS_INFO) << "too new"; 866 } else if (seg.seq + seg.len <= m_rcv_nxt) { 867 LOG_F(LS_INFO) << "too old"; 868 } 869 } 870 #endif // _DEBUGMSG 871 872 // Adjust the incoming segment to fit our receive buffer 873 if (seg.seq < m_rcv_nxt) { 874 uint32_t nAdjust = m_rcv_nxt - seg.seq; 875 if (nAdjust < seg.len) { 876 seg.seq += nAdjust; 877 seg.data += nAdjust; 878 seg.len -= nAdjust; 879 } else { 880 seg.len = 0; 881 } 882 } 883 884 size_t available_space = 0; 885 m_rbuf.GetWriteRemaining(&available_space); 886 887 if ((seg.seq + seg.len - m_rcv_nxt) > 888 static_cast<uint32_t>(available_space)) { 889 uint32_t nAdjust = 890 seg.seq + seg.len - m_rcv_nxt - static_cast<uint32_t>(available_space); 891 if (nAdjust < seg.len) { 892 seg.len -= nAdjust; 893 } else { 894 seg.len = 0; 895 } 896 } 897 898 bool bIgnoreData = (seg.flags & FLAG_CTL) || (m_shutdown != SD_NONE); 899 bool bNewData = false; 900 901 if (seg.len > 0) { 902 if (bIgnoreData) { 903 if (seg.seq == m_rcv_nxt) { 904 m_rcv_nxt += seg.len; 905 } 906 } else { 907 uint32_t nOffset = seg.seq - m_rcv_nxt; 908 909 rtc::StreamResult result = m_rbuf.WriteOffset(seg.data, seg.len, 910 nOffset, NULL); 911 ASSERT(result == rtc::SR_SUCCESS); 912 RTC_UNUSED(result); 913 914 if (seg.seq == m_rcv_nxt) { 915 m_rbuf.ConsumeWriteBuffer(seg.len); 916 m_rcv_nxt += seg.len; 917 m_rcv_wnd -= seg.len; 918 bNewData = true; 919 920 RList::iterator it = m_rlist.begin(); 921 while ((it != m_rlist.end()) && (it->seq <= m_rcv_nxt)) { 922 if (it->seq + it->len > m_rcv_nxt) { 923 sflags = sfImmediateAck; // (Fast Recovery) 924 uint32_t nAdjust = (it->seq + it->len) - m_rcv_nxt; 925 #if _DEBUGMSG >= _DBG_NORMAL 926 LOG(LS_INFO) << "Recovered " << nAdjust << " bytes (" << m_rcv_nxt << " -> " << m_rcv_nxt + nAdjust << ")"; 927 #endif // _DEBUGMSG 928 m_rbuf.ConsumeWriteBuffer(nAdjust); 929 m_rcv_nxt += nAdjust; 930 m_rcv_wnd -= nAdjust; 931 } 932 it = m_rlist.erase(it); 933 } 934 } else { 935 #if _DEBUGMSG >= _DBG_NORMAL 936 LOG(LS_INFO) << "Saving " << seg.len << " bytes (" << seg.seq << " -> " << seg.seq + seg.len << ")"; 937 #endif // _DEBUGMSG 938 RSegment rseg; 939 rseg.seq = seg.seq; 940 rseg.len = seg.len; 941 RList::iterator it = m_rlist.begin(); 942 while ((it != m_rlist.end()) && (it->seq < rseg.seq)) { 943 ++it; 944 } 945 m_rlist.insert(it, rseg); 946 } 947 } 948 } 949 950 attemptSend(sflags); 951 952 // If we have new data, notify the user 953 if (bNewData && m_bReadEnable) { 954 m_bReadEnable = false; 955 if (m_notify) { 956 m_notify->OnTcpReadable(this); 957 } 958 //notify(evRead); 959 } 960 961 return true; 962 } 963 964 bool PseudoTcp::transmit(const SList::iterator& seg, uint32_t now) { 965 if (seg->xmit >= ((m_state == TCP_ESTABLISHED) ? 15 : 30)) { 966 LOG_F(LS_VERBOSE) << "too many retransmits"; 967 return false; 968 } 969 970 uint32_t nTransmit = std::min(seg->len, m_mss); 971 972 while (true) { 973 uint32_t seq = seg->seq; 974 uint8_t flags = (seg->bCtrl ? FLAG_CTL : 0); 975 IPseudoTcpNotify::WriteResult wres = packet(seq, 976 flags, 977 seg->seq - m_snd_una, 978 nTransmit); 979 980 if (wres == IPseudoTcpNotify::WR_SUCCESS) 981 break; 982 983 if (wres == IPseudoTcpNotify::WR_FAIL) { 984 LOG_F(LS_VERBOSE) << "packet failed"; 985 return false; 986 } 987 988 ASSERT(wres == IPseudoTcpNotify::WR_TOO_LARGE); 989 990 while (true) { 991 if (PACKET_MAXIMUMS[m_msslevel + 1] == 0) { 992 LOG_F(LS_VERBOSE) << "MTU too small"; 993 return false; 994 } 995 // !?! We need to break up all outstanding and pending packets and then retransmit!?! 996 997 m_mss = PACKET_MAXIMUMS[++m_msslevel] - PACKET_OVERHEAD; 998 m_cwnd = 2 * m_mss; // I added this... haven't researched actual formula 999 if (m_mss < nTransmit) { 1000 nTransmit = m_mss; 1001 break; 1002 } 1003 } 1004 #if _DEBUGMSG >= _DBG_NORMAL 1005 LOG(LS_INFO) << "Adjusting mss to " << m_mss << " bytes"; 1006 #endif // _DEBUGMSG 1007 } 1008 1009 if (nTransmit < seg->len) { 1010 LOG_F(LS_VERBOSE) << "mss reduced to " << m_mss; 1011 1012 SSegment subseg(seg->seq + nTransmit, seg->len - nTransmit, seg->bCtrl); 1013 //subseg.tstamp = seg->tstamp; 1014 subseg.xmit = seg->xmit; 1015 seg->len = nTransmit; 1016 1017 SList::iterator next = seg; 1018 m_slist.insert(++next, subseg); 1019 } 1020 1021 if (seg->xmit == 0) { 1022 m_snd_nxt += seg->len; 1023 } 1024 seg->xmit += 1; 1025 //seg->tstamp = now; 1026 if (m_rto_base == 0) { 1027 m_rto_base = now; 1028 } 1029 1030 return true; 1031 } 1032 1033 void PseudoTcp::attemptSend(SendFlags sflags) { 1034 uint32_t now = Now(); 1035 1036 if (rtc::TimeDiff(now, m_lastsend) > static_cast<long>(m_rx_rto)) { 1037 m_cwnd = m_mss; 1038 } 1039 1040 #if _DEBUGMSG 1041 bool bFirst = true; 1042 RTC_UNUSED(bFirst); 1043 #endif // _DEBUGMSG 1044 1045 while (true) { 1046 uint32_t cwnd = m_cwnd; 1047 if ((m_dup_acks == 1) || (m_dup_acks == 2)) { // Limited Transmit 1048 cwnd += m_dup_acks * m_mss; 1049 } 1050 uint32_t nWindow = std::min(m_snd_wnd, cwnd); 1051 uint32_t nInFlight = m_snd_nxt - m_snd_una; 1052 uint32_t nUseable = (nInFlight < nWindow) ? (nWindow - nInFlight) : 0; 1053 1054 size_t snd_buffered = 0; 1055 m_sbuf.GetBuffered(&snd_buffered); 1056 uint32_t nAvailable = 1057 std::min(static_cast<uint32_t>(snd_buffered) - nInFlight, m_mss); 1058 1059 if (nAvailable > nUseable) { 1060 if (nUseable * 4 < nWindow) { 1061 // RFC 813 - avoid SWS 1062 nAvailable = 0; 1063 } else { 1064 nAvailable = nUseable; 1065 } 1066 } 1067 1068 #if _DEBUGMSG >= _DBG_VERBOSE 1069 if (bFirst) { 1070 size_t available_space = 0; 1071 m_sbuf.GetWriteRemaining(&available_space); 1072 1073 bFirst = false; 1074 LOG(LS_INFO) << "[cwnd: " << m_cwnd 1075 << " nWindow: " << nWindow 1076 << " nInFlight: " << nInFlight 1077 << " nAvailable: " << nAvailable 1078 << " nQueued: " << snd_buffered 1079 << " nEmpty: " << available_space 1080 << " ssthresh: " << m_ssthresh << "]"; 1081 } 1082 #endif // _DEBUGMSG 1083 1084 if (nAvailable == 0) { 1085 if (sflags == sfNone) 1086 return; 1087 1088 // If this is an immediate ack, or the second delayed ack 1089 if ((sflags == sfImmediateAck) || m_t_ack) { 1090 packet(m_snd_nxt, 0, 0, 0); 1091 } else { 1092 m_t_ack = Now(); 1093 } 1094 return; 1095 } 1096 1097 // Nagle's algorithm. 1098 // If there is data already in-flight, and we haven't a full segment of 1099 // data ready to send then hold off until we get more to send, or the 1100 // in-flight data is acknowledged. 1101 if (m_use_nagling && (m_snd_nxt > m_snd_una) && (nAvailable < m_mss)) { 1102 return; 1103 } 1104 1105 // Find the next segment to transmit 1106 SList::iterator it = m_slist.begin(); 1107 while (it->xmit > 0) { 1108 ++it; 1109 ASSERT(it != m_slist.end()); 1110 } 1111 SList::iterator seg = it; 1112 1113 // If the segment is too large, break it into two 1114 if (seg->len > nAvailable) { 1115 SSegment subseg(seg->seq + nAvailable, seg->len - nAvailable, seg->bCtrl); 1116 seg->len = nAvailable; 1117 m_slist.insert(++it, subseg); 1118 } 1119 1120 if (!transmit(seg, now)) { 1121 LOG_F(LS_VERBOSE) << "transmit failed"; 1122 // TODO: consider closing socket 1123 return; 1124 } 1125 1126 sflags = sfNone; 1127 } 1128 } 1129 1130 void PseudoTcp::closedown(uint32_t err) { 1131 LOG(LS_INFO) << "State: TCP_CLOSED"; 1132 m_state = TCP_CLOSED; 1133 if (m_notify) { 1134 m_notify->OnTcpClosed(this, err); 1135 } 1136 //notify(evClose, err); 1137 } 1138 1139 void 1140 PseudoTcp::adjustMTU() { 1141 // Determine our current mss level, so that we can adjust appropriately later 1142 for (m_msslevel = 0; PACKET_MAXIMUMS[m_msslevel + 1] > 0; ++m_msslevel) { 1143 if (static_cast<uint16_t>(PACKET_MAXIMUMS[m_msslevel]) <= m_mtu_advise) { 1144 break; 1145 } 1146 } 1147 m_mss = m_mtu_advise - PACKET_OVERHEAD; 1148 // !?! Should we reset m_largest here? 1149 #if _DEBUGMSG >= _DBG_NORMAL 1150 LOG(LS_INFO) << "Adjusting mss to " << m_mss << " bytes"; 1151 #endif // _DEBUGMSG 1152 // Enforce minimums on ssthresh and cwnd 1153 m_ssthresh = std::max(m_ssthresh, 2 * m_mss); 1154 m_cwnd = std::max(m_cwnd, m_mss); 1155 } 1156 1157 bool 1158 PseudoTcp::isReceiveBufferFull() const { 1159 size_t available_space = 0; 1160 m_rbuf.GetWriteRemaining(&available_space); 1161 return !available_space; 1162 } 1163 1164 void 1165 PseudoTcp::disableWindowScale() { 1166 m_support_wnd_scale = false; 1167 } 1168 1169 void 1170 PseudoTcp::queueConnectMessage() { 1171 rtc::ByteBuffer buf(rtc::ByteBuffer::ORDER_NETWORK); 1172 1173 buf.WriteUInt8(CTL_CONNECT); 1174 if (m_support_wnd_scale) { 1175 buf.WriteUInt8(TCP_OPT_WND_SCALE); 1176 buf.WriteUInt8(1); 1177 buf.WriteUInt8(m_rwnd_scale); 1178 } 1179 m_snd_wnd = static_cast<uint32_t>(buf.Length()); 1180 queue(buf.Data(), static_cast<uint32_t>(buf.Length()), true); 1181 } 1182 1183 void PseudoTcp::parseOptions(const char* data, uint32_t len) { 1184 std::set<uint8_t> options_specified; 1185 1186 // See http://www.freesoft.org/CIE/Course/Section4/8.htm for 1187 // parsing the options list. 1188 rtc::ByteBuffer buf(data, len); 1189 while (buf.Length()) { 1190 uint8_t kind = TCP_OPT_EOL; 1191 buf.ReadUInt8(&kind); 1192 1193 if (kind == TCP_OPT_EOL) { 1194 // End of option list. 1195 break; 1196 } else if (kind == TCP_OPT_NOOP) { 1197 // No op. 1198 continue; 1199 } 1200 1201 // Length of this option. 1202 ASSERT(len != 0); 1203 RTC_UNUSED(len); 1204 uint8_t opt_len = 0; 1205 buf.ReadUInt8(&opt_len); 1206 1207 // Content of this option. 1208 if (opt_len <= buf.Length()) { 1209 applyOption(kind, buf.Data(), opt_len); 1210 buf.Consume(opt_len); 1211 } else { 1212 LOG(LS_ERROR) << "Invalid option length received."; 1213 return; 1214 } 1215 options_specified.insert(kind); 1216 } 1217 1218 if (options_specified.find(TCP_OPT_WND_SCALE) == options_specified.end()) { 1219 LOG(LS_WARNING) << "Peer doesn't support window scaling"; 1220 1221 if (m_rwnd_scale > 0) { 1222 // Peer doesn't support TCP options and window scaling. 1223 // Revert receive buffer size to default value. 1224 resizeReceiveBuffer(DEFAULT_RCV_BUF_SIZE); 1225 m_swnd_scale = 0; 1226 } 1227 } 1228 } 1229 1230 void PseudoTcp::applyOption(char kind, const char* data, uint32_t len) { 1231 if (kind == TCP_OPT_MSS) { 1232 LOG(LS_WARNING) << "Peer specified MSS option which is not supported."; 1233 // TODO: Implement. 1234 } else if (kind == TCP_OPT_WND_SCALE) { 1235 // Window scale factor. 1236 // http://www.ietf.org/rfc/rfc1323.txt 1237 if (len != 1) { 1238 LOG_F(WARNING) << "Invalid window scale option received."; 1239 return; 1240 } 1241 applyWindowScaleOption(data[0]); 1242 } 1243 } 1244 1245 void PseudoTcp::applyWindowScaleOption(uint8_t scale_factor) { 1246 m_swnd_scale = scale_factor; 1247 } 1248 1249 void PseudoTcp::resizeSendBuffer(uint32_t new_size) { 1250 m_sbuf_len = new_size; 1251 m_sbuf.SetCapacity(new_size); 1252 } 1253 1254 void PseudoTcp::resizeReceiveBuffer(uint32_t new_size) { 1255 uint8_t scale_factor = 0; 1256 1257 // Determine the scale factor such that the scaled window size can fit 1258 // in a 16-bit unsigned integer. 1259 while (new_size > 0xFFFF) { 1260 ++scale_factor; 1261 new_size >>= 1; 1262 } 1263 1264 // Determine the proper size of the buffer. 1265 new_size <<= scale_factor; 1266 bool result = m_rbuf.SetCapacity(new_size); 1267 1268 // Make sure the new buffer is large enough to contain data in the old 1269 // buffer. This should always be true because this method is called either 1270 // before connection is established or when peers are exchanging connect 1271 // messages. 1272 ASSERT(result); 1273 RTC_UNUSED(result); 1274 m_rbuf_len = new_size; 1275 m_rwnd_scale = scale_factor; 1276 m_ssthresh = new_size; 1277 1278 size_t available_space = 0; 1279 m_rbuf.GetWriteRemaining(&available_space); 1280 m_rcv_wnd = static_cast<uint32_t>(available_space); 1281 } 1282 1283 } // namespace cricket 1284