• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "p2p/base/pseudo_tcp.h"
12 
13 #include <errno.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <memory>
20 #include <set>
21 
22 #include "rtc_base/byte_buffer.h"
23 #include "rtc_base/byte_order.h"
24 #include "rtc_base/checks.h"
25 #include "rtc_base/logging.h"
26 #include "rtc_base/numerics/safe_minmax.h"
27 #include "rtc_base/socket.h"
28 #include "rtc_base/time_utils.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 =
128     4000;  // If there are no pending clocks, wake up every 4 seconds
129 const long CLOSED_TIMEOUT =
130     60 * 1000;  // If the connection is closed, once per minute
131 
132 #if PSEUDO_KEEPALIVE
133 // !?! Rethink these times
134 const uint32_t IDLE_PING =
135     20 *
136     1000;  // 20 seconds (note: WinXP SP2 firewall udp timeout is 90 seconds)
137 const uint32_t IDLE_TIMEOUT = 90 * 1000;  // 90 seconds;
138 #endif                                    // PSEUDO_KEEPALIVE
139 
140 //////////////////////////////////////////////////////////////////////
141 // Helper Functions
142 //////////////////////////////////////////////////////////////////////
143 
long_to_bytes(uint32_t val,void * buf)144 inline void long_to_bytes(uint32_t val, void* buf) {
145   *static_cast<uint32_t*>(buf) = rtc::HostToNetwork32(val);
146 }
147 
short_to_bytes(uint16_t val,void * buf)148 inline void short_to_bytes(uint16_t val, void* buf) {
149   *static_cast<uint16_t*>(buf) = rtc::HostToNetwork16(val);
150 }
151 
bytes_to_long(const void * buf)152 inline uint32_t bytes_to_long(const void* buf) {
153   return rtc::NetworkToHost32(*static_cast<const uint32_t*>(buf));
154 }
155 
bytes_to_short(const void * buf)156 inline uint16_t bytes_to_short(const void* buf) {
157   return rtc::NetworkToHost16(*static_cast<const uint16_t*>(buf));
158 }
159 
160 //////////////////////////////////////////////////////////////////////
161 // Debugging Statistics
162 //////////////////////////////////////////////////////////////////////
163 
164 #if 0  // Not used yet
165 
166 enum Stat {
167   S_SENT_PACKET,    // All packet sends
168   S_RESENT_PACKET,  // All packet sends that are retransmits
169   S_RECV_PACKET,    // All packet receives
170   S_RECV_NEW,       // All packet receives that are too new
171   S_RECV_OLD,       // All packet receives that are too old
172   S_NUM_STATS
173 };
174 
175 const char* const STAT_NAMES[S_NUM_STATS] = {
176   "snt",
177   "snt-r",
178   "rcv"
179   "rcv-n",
180   "rcv-o"
181 };
182 
183 int g_stats[S_NUM_STATS];
184 inline void Incr(Stat s) { ++g_stats[s]; }
185 void ReportStats() {
186   char buffer[256];
187   size_t len = 0;
188   for (int i = 0; i < S_NUM_STATS; ++i) {
189     len += snprintf(buffer, arraysize(buffer), "%s%s:%d",
190                           (i == 0) ? "" : ",", STAT_NAMES[i], g_stats[i]);
191     g_stats[i] = 0;
192   }
193   RTC_LOG(LS_INFO) << "Stats[" << buffer << "]";
194 }
195 
196 #endif
197 
198 //////////////////////////////////////////////////////////////////////
199 // PseudoTcp
200 //////////////////////////////////////////////////////////////////////
201 
Now()202 uint32_t PseudoTcp::Now() {
203 #if 0  // Use this to synchronize timers with logging timestamps (easier debug)
204   return static_cast<uint32_t>(rtc::TimeSince(StartTime()));
205 #else
206   return rtc::Time32();
207 #endif
208 }
209 
PseudoTcp(IPseudoTcpNotify * notify,uint32_t conv)210 PseudoTcp::PseudoTcp(IPseudoTcpNotify* notify, uint32_t conv)
211     : m_notify(notify),
212       m_shutdown(SD_NONE),
213       m_error(0),
214       m_rbuf_len(DEFAULT_RCV_BUF_SIZE),
215       m_rbuf(m_rbuf_len),
216       m_sbuf_len(DEFAULT_SND_BUF_SIZE),
217       m_sbuf(m_sbuf_len) {
218   // Sanity check on buffer sizes (needed for OnTcpWriteable notification logic)
219   RTC_DCHECK(m_rbuf_len + MIN_PACKET < m_sbuf_len);
220 
221   uint32_t now = Now();
222 
223   m_state = TCP_LISTEN;
224   m_conv = conv;
225   m_rcv_wnd = m_rbuf_len;
226   m_rwnd_scale = m_swnd_scale = 0;
227   m_snd_nxt = 0;
228   m_snd_wnd = 1;
229   m_snd_una = m_rcv_nxt = 0;
230   m_bReadEnable = true;
231   m_bWriteEnable = false;
232   m_t_ack = 0;
233 
234   m_msslevel = 0;
235   m_largest = 0;
236   RTC_DCHECK(MIN_PACKET > PACKET_OVERHEAD);
237   m_mss = MIN_PACKET - PACKET_OVERHEAD;
238   m_mtu_advise = MAX_PACKET;
239 
240   m_rto_base = 0;
241 
242   m_cwnd = 2 * m_mss;
243   m_ssthresh = m_rbuf_len;
244   m_lastrecv = m_lastsend = m_lasttraffic = now;
245   m_bOutgoing = false;
246 
247   m_dup_acks = 0;
248   m_recover = 0;
249 
250   m_ts_recent = m_ts_lastack = 0;
251 
252   m_rx_rto = DEF_RTO;
253   m_rx_srtt = m_rx_rttvar = 0;
254 
255   m_use_nagling = true;
256   m_ack_delay = DEF_ACK_DELAY;
257   m_support_wnd_scale = true;
258 }
259 
~PseudoTcp()260 PseudoTcp::~PseudoTcp() {}
261 
Connect()262 int PseudoTcp::Connect() {
263   if (m_state != TCP_LISTEN) {
264     m_error = EINVAL;
265     return -1;
266   }
267 
268   m_state = TCP_SYN_SENT;
269   RTC_LOG(LS_INFO) << "State: TCP_SYN_SENT";
270 
271   queueConnectMessage();
272   attemptSend();
273 
274   return 0;
275 }
276 
NotifyMTU(uint16_t mtu)277 void PseudoTcp::NotifyMTU(uint16_t mtu) {
278   m_mtu_advise = mtu;
279   if (m_state == TCP_ESTABLISHED) {
280     adjustMTU();
281   }
282 }
283 
NotifyClock(uint32_t now)284 void PseudoTcp::NotifyClock(uint32_t now) {
285   if (m_state == TCP_CLOSED)
286     return;
287 
288   // Check if it's time to retransmit a segment
289   if (m_rto_base && (rtc::TimeDiff32(m_rto_base + m_rx_rto, now) <= 0)) {
290     if (m_slist.empty()) {
291       RTC_DCHECK_NOTREACHED();
292     } else {
293 // Note: (m_slist.front().xmit == 0)) {
294 // retransmit segments
295 #if _DEBUGMSG >= _DBG_NORMAL
296       RTC_LOG(LS_INFO) << "timeout retransmit (rto: " << m_rx_rto
297                        << ") (rto_base: " << m_rto_base << ") (now: " << now
298                        << ") (dup_acks: " << static_cast<unsigned>(m_dup_acks)
299                        << ")";
300 #endif  // _DEBUGMSG
301       if (!transmit(m_slist.begin(), now)) {
302         closedown(ECONNABORTED);
303         return;
304       }
305 
306       uint32_t nInFlight = m_snd_nxt - m_snd_una;
307       m_ssthresh = std::max(nInFlight / 2, 2 * m_mss);
308       // RTC_LOG(LS_INFO) << "m_ssthresh: " << m_ssthresh << "  nInFlight: " <<
309       // nInFlight << "  m_mss: " << m_mss;
310       m_cwnd = m_mss;
311 
312       // Back off retransmit timer.  Note: the limit is lower when connecting.
313       uint32_t rto_limit = (m_state < TCP_ESTABLISHED) ? DEF_RTO : MAX_RTO;
314       m_rx_rto = std::min(rto_limit, m_rx_rto * 2);
315       m_rto_base = now;
316     }
317   }
318 
319   // Check if it's time to probe closed windows
320   if ((m_snd_wnd == 0) && (rtc::TimeDiff32(m_lastsend + m_rx_rto, now) <= 0)) {
321     if (rtc::TimeDiff32(now, m_lastrecv) >= 15000) {
322       closedown(ECONNABORTED);
323       return;
324     }
325 
326     // probe the window
327     packet(m_snd_nxt - 1, 0, 0, 0);
328     m_lastsend = now;
329 
330     // back off retransmit timer
331     m_rx_rto = std::min(MAX_RTO, m_rx_rto * 2);
332   }
333 
334   // Check if it's time to send delayed acks
335   if (m_t_ack && (rtc::TimeDiff32(m_t_ack + m_ack_delay, now) <= 0)) {
336     packet(m_snd_nxt, 0, 0, 0);
337   }
338 
339 #if PSEUDO_KEEPALIVE
340   // Check for idle timeout
341   if ((m_state == TCP_ESTABLISHED) &&
342       (TimeDiff32(m_lastrecv + IDLE_TIMEOUT, now) <= 0)) {
343     closedown(ECONNABORTED);
344     return;
345   }
346 
347   // Check for ping timeout (to keep udp mapping open)
348   if ((m_state == TCP_ESTABLISHED) &&
349       (TimeDiff32(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3 / 2 : IDLE_PING),
350                   now) <= 0)) {
351     packet(m_snd_nxt, 0, 0, 0);
352   }
353 #endif  // PSEUDO_KEEPALIVE
354 }
355 
NotifyPacket(const char * buffer,size_t len)356 bool PseudoTcp::NotifyPacket(const char* buffer, size_t len) {
357   if (len > MAX_PACKET) {
358     RTC_LOG_F(LS_WARNING) << "packet too large";
359     return false;
360   }
361   return parse(reinterpret_cast<const uint8_t*>(buffer), uint32_t(len));
362 }
363 
GetNextClock(uint32_t now,long & timeout)364 bool PseudoTcp::GetNextClock(uint32_t now, long& timeout) {
365   return clock_check(now, timeout);
366 }
367 
GetOption(Option opt,int * value)368 void PseudoTcp::GetOption(Option opt, int* value) {
369   if (opt == OPT_NODELAY) {
370     *value = m_use_nagling ? 0 : 1;
371   } else if (opt == OPT_ACKDELAY) {
372     *value = m_ack_delay;
373   } else if (opt == OPT_SNDBUF) {
374     *value = m_sbuf_len;
375   } else if (opt == OPT_RCVBUF) {
376     *value = m_rbuf_len;
377   } else {
378     RTC_DCHECK_NOTREACHED();
379   }
380 }
SetOption(Option opt,int value)381 void PseudoTcp::SetOption(Option opt, int value) {
382   if (opt == OPT_NODELAY) {
383     m_use_nagling = value == 0;
384   } else if (opt == OPT_ACKDELAY) {
385     m_ack_delay = value;
386   } else if (opt == OPT_SNDBUF) {
387     RTC_DCHECK(m_state == TCP_LISTEN);
388     resizeSendBuffer(value);
389   } else if (opt == OPT_RCVBUF) {
390     RTC_DCHECK(m_state == TCP_LISTEN);
391     resizeReceiveBuffer(value);
392   } else {
393     RTC_DCHECK_NOTREACHED();
394   }
395 }
396 
GetCongestionWindow() const397 uint32_t PseudoTcp::GetCongestionWindow() const {
398   return m_cwnd;
399 }
400 
GetBytesInFlight() const401 uint32_t PseudoTcp::GetBytesInFlight() const {
402   return m_snd_nxt - m_snd_una;
403 }
404 
GetBytesBufferedNotSent() const405 uint32_t PseudoTcp::GetBytesBufferedNotSent() const {
406   return static_cast<uint32_t>(m_snd_una + m_sbuf.GetBuffered() - m_snd_nxt);
407 }
408 
GetRoundTripTimeEstimateMs() const409 uint32_t PseudoTcp::GetRoundTripTimeEstimateMs() const {
410   return m_rx_srtt;
411 }
412 
413 //
414 // IPStream Implementation
415 //
416 
Recv(char * buffer,size_t len)417 int PseudoTcp::Recv(char* buffer, size_t len) {
418   if (m_state != TCP_ESTABLISHED) {
419     m_error = ENOTCONN;
420     return SOCKET_ERROR;
421   }
422 
423   size_t read = 0;
424   if (!m_rbuf.Read(buffer, len, &read)) {
425     m_bReadEnable = true;
426     m_error = EWOULDBLOCK;
427     return SOCKET_ERROR;
428   }
429 
430   size_t available_space = 0;
431   m_rbuf.GetWriteRemaining(&available_space);
432 
433   if (uint32_t(available_space) - m_rcv_wnd >=
434       std::min<uint32_t>(m_rbuf_len / 2, m_mss)) {
435     // TODO(jbeda): !?! Not sure about this was closed business
436     bool bWasClosed = (m_rcv_wnd == 0);
437     m_rcv_wnd = static_cast<uint32_t>(available_space);
438 
439     if (bWasClosed) {
440       attemptSend(sfImmediateAck);
441     }
442   }
443 
444   return static_cast<int>(read);
445 }
446 
Send(const char * buffer,size_t len)447 int PseudoTcp::Send(const char* buffer, size_t len) {
448   if (m_state != TCP_ESTABLISHED) {
449     m_error = ENOTCONN;
450     return SOCKET_ERROR;
451   }
452 
453   size_t available_space = 0;
454   m_sbuf.GetWriteRemaining(&available_space);
455 
456   if (!available_space) {
457     m_bWriteEnable = true;
458     m_error = EWOULDBLOCK;
459     return SOCKET_ERROR;
460   }
461 
462   int written = queue(buffer, uint32_t(len), false);
463   attemptSend();
464   return written;
465 }
466 
Close(bool force)467 void PseudoTcp::Close(bool force) {
468   RTC_LOG_F(LS_VERBOSE) << "(" << (force ? "true" : "false") << ")";
469   m_shutdown = force ? SD_FORCEFUL : SD_GRACEFUL;
470 }
471 
GetError()472 int PseudoTcp::GetError() {
473   return m_error;
474 }
475 
476 //
477 // Internal Implementation
478 //
479 
queue(const char * data,uint32_t len,bool bCtrl)480 uint32_t PseudoTcp::queue(const char* data, uint32_t len, bool bCtrl) {
481   size_t available_space = 0;
482   m_sbuf.GetWriteRemaining(&available_space);
483 
484   if (len > static_cast<uint32_t>(available_space)) {
485     RTC_DCHECK(!bCtrl);
486     len = static_cast<uint32_t>(available_space);
487   }
488 
489   // We can concatenate data if the last segment is the same type
490   // (control v. regular data), and has not been transmitted yet
491   if (!m_slist.empty() && (m_slist.back().bCtrl == bCtrl) &&
492       (m_slist.back().xmit == 0)) {
493     m_slist.back().len += len;
494   } else {
495     SSegment sseg(static_cast<uint32_t>(m_snd_una + m_sbuf.GetBuffered()), len,
496                   bCtrl);
497     m_slist.push_back(sseg);
498   }
499 
500   size_t written = 0;
501   m_sbuf.Write(data, len, &written);
502   return static_cast<uint32_t>(written);
503 }
504 
packet(uint32_t seq,uint8_t flags,uint32_t offset,uint32_t len)505 IPseudoTcpNotify::WriteResult PseudoTcp::packet(uint32_t seq,
506                                                 uint8_t flags,
507                                                 uint32_t offset,
508                                                 uint32_t len) {
509   RTC_DCHECK(HEADER_SIZE + len <= MAX_PACKET);
510 
511   uint32_t now = Now();
512 
513   std::unique_ptr<uint8_t[]> buffer(new uint8_t[MAX_PACKET]);
514   long_to_bytes(m_conv, buffer.get());
515   long_to_bytes(seq, buffer.get() + 4);
516   long_to_bytes(m_rcv_nxt, buffer.get() + 8);
517   buffer[12] = 0;
518   buffer[13] = flags;
519   short_to_bytes(static_cast<uint16_t>(m_rcv_wnd >> m_rwnd_scale),
520                  buffer.get() + 14);
521 
522   // Timestamp computations
523   long_to_bytes(now, buffer.get() + 16);
524   long_to_bytes(m_ts_recent, buffer.get() + 20);
525   m_ts_lastack = m_rcv_nxt;
526 
527   if (len) {
528     size_t bytes_read = 0;
529     bool result =
530         m_sbuf.ReadOffset(buffer.get() + HEADER_SIZE, len, offset, &bytes_read);
531     RTC_DCHECK(result);
532     RTC_DCHECK(static_cast<uint32_t>(bytes_read) == len);
533   }
534 
535 #if _DEBUGMSG >= _DBG_VERBOSE
536   RTC_LOG(LS_INFO) << "<-- <CONV=" << m_conv
537                    << "><FLG=" << static_cast<unsigned>(flags)
538                    << "><SEQ=" << seq << ":" << seq + len
539                    << "><ACK=" << m_rcv_nxt << "><WND=" << m_rcv_wnd
540                    << "><TS=" << (now % 10000)
541                    << "><TSR=" << (m_ts_recent % 10000) << "><LEN=" << len
542                    << ">";
543 #endif  // _DEBUGMSG
544 
545   IPseudoTcpNotify::WriteResult wres = m_notify->TcpWritePacket(
546       this, reinterpret_cast<char*>(buffer.get()), len + HEADER_SIZE);
547   // Note: When len is 0, this is an ACK packet.  We don't read the return value
548   // for those, and thus we won't retry.  So go ahead and treat the packet as a
549   // success (basically simulate as if it were dropped), which will prevent our
550   // timers from being messed up.
551   if ((wres != IPseudoTcpNotify::WR_SUCCESS) && (0 != len))
552     return wres;
553 
554   m_t_ack = 0;
555   if (len > 0) {
556     m_lastsend = now;
557   }
558   m_lasttraffic = now;
559   m_bOutgoing = true;
560 
561   return IPseudoTcpNotify::WR_SUCCESS;
562 }
563 
parse(const uint8_t * buffer,uint32_t size)564 bool PseudoTcp::parse(const uint8_t* buffer, uint32_t size) {
565   if (size < HEADER_SIZE)
566     return false;
567 
568   Segment seg;
569   seg.conv = bytes_to_long(buffer);
570   seg.seq = bytes_to_long(buffer + 4);
571   seg.ack = bytes_to_long(buffer + 8);
572   seg.flags = buffer[13];
573   seg.wnd = bytes_to_short(buffer + 14);
574 
575   seg.tsval = bytes_to_long(buffer + 16);
576   seg.tsecr = bytes_to_long(buffer + 20);
577 
578   seg.data = reinterpret_cast<const char*>(buffer) + HEADER_SIZE;
579   seg.len = size - HEADER_SIZE;
580 
581 #if _DEBUGMSG >= _DBG_VERBOSE
582   RTC_LOG(LS_INFO) << "--> <CONV=" << seg.conv
583                    << "><FLG=" << static_cast<unsigned>(seg.flags)
584                    << "><SEQ=" << seg.seq << ":" << seg.seq + seg.len
585                    << "><ACK=" << seg.ack << "><WND=" << seg.wnd
586                    << "><TS=" << (seg.tsval % 10000)
587                    << "><TSR=" << (seg.tsecr % 10000) << "><LEN=" << seg.len
588                    << ">";
589 #endif  // _DEBUGMSG
590 
591   return process(seg);
592 }
593 
clock_check(uint32_t now,long & nTimeout)594 bool PseudoTcp::clock_check(uint32_t now, long& nTimeout) {
595   if (m_shutdown == SD_FORCEFUL)
596     return false;
597 
598   if ((m_shutdown == SD_GRACEFUL) &&
599       ((m_state != TCP_ESTABLISHED) ||
600        ((m_sbuf.GetBuffered() == 0) && (m_t_ack == 0)))) {
601     return false;
602   }
603 
604   if (m_state == TCP_CLOSED) {
605     nTimeout = CLOSED_TIMEOUT;
606     return true;
607   }
608 
609   nTimeout = DEFAULT_TIMEOUT;
610 
611   if (m_t_ack) {
612     nTimeout = std::min<int32_t>(nTimeout,
613                                  rtc::TimeDiff32(m_t_ack + m_ack_delay, now));
614   }
615   if (m_rto_base) {
616     nTimeout = std::min<int32_t>(nTimeout,
617                                  rtc::TimeDiff32(m_rto_base + m_rx_rto, now));
618   }
619   if (m_snd_wnd == 0) {
620     nTimeout = std::min<int32_t>(nTimeout,
621                                  rtc::TimeDiff32(m_lastsend + m_rx_rto, now));
622   }
623 #if PSEUDO_KEEPALIVE
624   if (m_state == TCP_ESTABLISHED) {
625     nTimeout = std::min<int32_t>(
626         nTimeout,
627         rtc::TimeDiff32(
628             m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3 / 2 : IDLE_PING),
629             now));
630   }
631 #endif  // PSEUDO_KEEPALIVE
632   return true;
633 }
634 
process(Segment & seg)635 bool PseudoTcp::process(Segment& seg) {
636   // If this is the wrong conversation, send a reset!?! (with the correct
637   // conversation?)
638   if (seg.conv != m_conv) {
639     // if ((seg.flags & FLAG_RST) == 0) {
640     //  packet(tcb, seg.ack, 0, FLAG_RST, 0, 0);
641     //}
642     RTC_LOG_F(LS_ERROR) << "wrong conversation";
643     return false;
644   }
645 
646   uint32_t now = Now();
647   m_lasttraffic = m_lastrecv = now;
648   m_bOutgoing = false;
649 
650   if (m_state == TCP_CLOSED) {
651     // !?! send reset?
652     RTC_LOG_F(LS_ERROR) << "closed";
653     return false;
654   }
655 
656   // Check if this is a reset segment
657   if (seg.flags & FLAG_RST) {
658     closedown(ECONNRESET);
659     return false;
660   }
661 
662   // Check for control data
663   bool bConnect = false;
664   if (seg.flags & FLAG_CTL) {
665     if (seg.len == 0) {
666       RTC_LOG_F(LS_ERROR) << "Missing control code";
667       return false;
668     } else if (seg.data[0] == CTL_CONNECT) {
669       bConnect = true;
670 
671       // TCP options are in the remainder of the payload after CTL_CONNECT.
672       parseOptions(&seg.data[1], seg.len - 1);
673 
674       if (m_state == TCP_LISTEN) {
675         m_state = TCP_SYN_RECEIVED;
676         RTC_LOG(LS_INFO) << "State: TCP_SYN_RECEIVED";
677         // m_notify->associate(addr);
678         queueConnectMessage();
679       } else if (m_state == TCP_SYN_SENT) {
680         m_state = TCP_ESTABLISHED;
681         RTC_LOG(LS_INFO) << "State: TCP_ESTABLISHED";
682         adjustMTU();
683         if (m_notify) {
684           m_notify->OnTcpOpen(this);
685         }
686         // notify(evOpen);
687       }
688     } else {
689       RTC_LOG_F(LS_WARNING) << "Unknown control code: " << seg.data[0];
690       return false;
691     }
692   }
693 
694   // Update timestamp
695   if ((seg.seq <= m_ts_lastack) && (m_ts_lastack < seg.seq + seg.len)) {
696     m_ts_recent = seg.tsval;
697   }
698 
699   // Check if this is a valuable ack
700   if ((seg.ack > m_snd_una) && (seg.ack <= m_snd_nxt)) {
701     // Calculate round-trip time
702     if (seg.tsecr) {
703       int32_t rtt = rtc::TimeDiff32(now, seg.tsecr);
704       if (rtt >= 0) {
705         if (m_rx_srtt == 0) {
706           m_rx_srtt = rtt;
707           m_rx_rttvar = rtt / 2;
708         } else {
709           uint32_t unsigned_rtt = static_cast<uint32_t>(rtt);
710           uint32_t abs_err = unsigned_rtt > m_rx_srtt
711                                  ? unsigned_rtt - m_rx_srtt
712                                  : m_rx_srtt - unsigned_rtt;
713           m_rx_rttvar = (3 * m_rx_rttvar + abs_err) / 4;
714           m_rx_srtt = (7 * m_rx_srtt + rtt) / 8;
715         }
716         m_rx_rto = rtc::SafeClamp(m_rx_srtt + rtc::SafeMax(1, 4 * m_rx_rttvar),
717                                   MIN_RTO, MAX_RTO);
718 #if _DEBUGMSG >= _DBG_VERBOSE
719         RTC_LOG(LS_INFO) << "rtt: " << rtt << "  srtt: " << m_rx_srtt
720                          << "  rto: " << m_rx_rto;
721 #endif  // _DEBUGMSG
722       } else {
723         RTC_LOG(LS_WARNING) << "rtt < 0";
724       }
725     }
726 
727     m_snd_wnd = static_cast<uint32_t>(seg.wnd) << m_swnd_scale;
728 
729     uint32_t nAcked = seg.ack - m_snd_una;
730     m_snd_una = seg.ack;
731 
732     m_rto_base = (m_snd_una == m_snd_nxt) ? 0 : now;
733 
734     m_sbuf.ConsumeReadData(nAcked);
735 
736     for (uint32_t nFree = nAcked; nFree > 0;) {
737       RTC_DCHECK(!m_slist.empty());
738       if (nFree < m_slist.front().len) {
739         m_slist.front().len -= nFree;
740         nFree = 0;
741       } else {
742         if (m_slist.front().len > m_largest) {
743           m_largest = m_slist.front().len;
744         }
745         nFree -= m_slist.front().len;
746         m_slist.pop_front();
747       }
748     }
749 
750     if (m_dup_acks >= 3) {
751       if (m_snd_una >= m_recover) {  // NewReno
752         uint32_t nInFlight = m_snd_nxt - m_snd_una;
753         m_cwnd = std::min(m_ssthresh, nInFlight + m_mss);  // (Fast Retransmit)
754 #if _DEBUGMSG >= _DBG_NORMAL
755         RTC_LOG(LS_INFO) << "exit recovery";
756 #endif  // _DEBUGMSG
757         m_dup_acks = 0;
758       } else {
759 #if _DEBUGMSG >= _DBG_NORMAL
760         RTC_LOG(LS_INFO) << "recovery retransmit";
761 #endif  // _DEBUGMSG
762         if (!transmit(m_slist.begin(), now)) {
763           closedown(ECONNABORTED);
764           return false;
765         }
766         m_cwnd += m_mss - std::min(nAcked, m_cwnd);
767       }
768     } else {
769       m_dup_acks = 0;
770       // Slow start, congestion avoidance
771       if (m_cwnd < m_ssthresh) {
772         m_cwnd += m_mss;
773       } else {
774         m_cwnd += std::max<uint32_t>(1, m_mss * m_mss / m_cwnd);
775       }
776     }
777   } else if (seg.ack == m_snd_una) {
778     // !?! Note, tcp says don't do this... but otherwise how does a closed
779     // window become open?
780     m_snd_wnd = static_cast<uint32_t>(seg.wnd) << m_swnd_scale;
781 
782     // Check duplicate acks
783     if (seg.len > 0) {
784       // it's a dup ack, but with a data payload, so don't modify m_dup_acks
785     } else if (m_snd_una != m_snd_nxt) {
786       m_dup_acks += 1;
787       if (m_dup_acks == 3) {  // (Fast Retransmit)
788 #if _DEBUGMSG >= _DBG_NORMAL
789         RTC_LOG(LS_INFO) << "enter recovery";
790         RTC_LOG(LS_INFO) << "recovery retransmit";
791 #endif  // _DEBUGMSG
792         if (!transmit(m_slist.begin(), now)) {
793           closedown(ECONNABORTED);
794           return false;
795         }
796         m_recover = m_snd_nxt;
797         uint32_t nInFlight = m_snd_nxt - m_snd_una;
798         m_ssthresh = std::max(nInFlight / 2, 2 * m_mss);
799         // RTC_LOG(LS_INFO) << "m_ssthresh: " << m_ssthresh << "  nInFlight: "
800         // << nInFlight << "  m_mss: " << m_mss;
801         m_cwnd = m_ssthresh + 3 * m_mss;
802       } else if (m_dup_acks > 3) {
803         m_cwnd += m_mss;
804       }
805     } else {
806       m_dup_acks = 0;
807     }
808   }
809 
810   // !?! A bit hacky
811   if ((m_state == TCP_SYN_RECEIVED) && !bConnect) {
812     m_state = TCP_ESTABLISHED;
813     RTC_LOG(LS_INFO) << "State: TCP_ESTABLISHED";
814     adjustMTU();
815     if (m_notify) {
816       m_notify->OnTcpOpen(this);
817     }
818     // notify(evOpen);
819   }
820 
821   // If we make room in the send queue, notify the user
822   // The goal it to make sure we always have at least enough data to fill the
823   // window.  We'd like to notify the app when we are halfway to that point.
824   const uint32_t kIdealRefillSize = (m_sbuf_len + m_rbuf_len) / 2;
825   if (m_bWriteEnable &&
826       static_cast<uint32_t>(m_sbuf.GetBuffered()) < kIdealRefillSize) {
827     m_bWriteEnable = false;
828     if (m_notify) {
829       m_notify->OnTcpWriteable(this);
830     }
831     // notify(evWrite);
832   }
833 
834   // Conditions were acks must be sent:
835   // 1) Segment is too old (they missed an ACK) (immediately)
836   // 2) Segment is too new (we missed a segment) (immediately)
837   // 3) Segment has data (so we need to ACK!) (delayed)
838   // ... so the only time we don't need to ACK, is an empty segment that points
839   // to rcv_nxt!
840 
841   SendFlags sflags = sfNone;
842   if (seg.seq != m_rcv_nxt) {
843     sflags = sfImmediateAck;  // (Fast Recovery)
844   } else if (seg.len != 0) {
845     if (m_ack_delay == 0) {
846       sflags = sfImmediateAck;
847     } else {
848       sflags = sfDelayedAck;
849     }
850   }
851 #if _DEBUGMSG >= _DBG_NORMAL
852   if (sflags == sfImmediateAck) {
853     if (seg.seq > m_rcv_nxt) {
854       RTC_LOG_F(LS_INFO) << "too new";
855     } else if (seg.seq + seg.len <= m_rcv_nxt) {
856       RTC_LOG_F(LS_INFO) << "too old";
857     }
858   }
859 #endif  // _DEBUGMSG
860 
861   // Adjust the incoming segment to fit our receive buffer
862   if (seg.seq < m_rcv_nxt) {
863     uint32_t nAdjust = m_rcv_nxt - seg.seq;
864     if (nAdjust < seg.len) {
865       seg.seq += nAdjust;
866       seg.data += nAdjust;
867       seg.len -= nAdjust;
868     } else {
869       seg.len = 0;
870     }
871   }
872 
873   size_t available_space = 0;
874   m_rbuf.GetWriteRemaining(&available_space);
875 
876   if ((seg.seq + seg.len - m_rcv_nxt) >
877       static_cast<uint32_t>(available_space)) {
878     uint32_t nAdjust =
879         seg.seq + seg.len - m_rcv_nxt - static_cast<uint32_t>(available_space);
880     if (nAdjust < seg.len) {
881       seg.len -= nAdjust;
882     } else {
883       seg.len = 0;
884     }
885   }
886 
887   bool bIgnoreData = (seg.flags & FLAG_CTL) || (m_shutdown != SD_NONE);
888   bool bNewData = false;
889 
890   if (seg.len > 0) {
891     bool bRecover = false;
892     if (bIgnoreData) {
893       if (seg.seq == m_rcv_nxt) {
894         m_rcv_nxt += seg.len;
895         // If we received a data segment out of order relative to a control
896         // segment, then we wrote it into the receive buffer at an offset (see
897         // "WriteOffset") below. So we need to advance the position in the
898         // buffer to avoid corrupting data. See bugs.webrtc.org/9208
899         //
900         // We advance the position in the buffer by N bytes by acting like we
901         // wrote N bytes and then immediately read them. We can only do this if
902         // there's not already data ready to read, but this should always be
903         // true in the problematic scenario, since control frames are always
904         // sent first in the stream.
905         if (m_rbuf.GetBuffered() == 0) {
906           m_rbuf.ConsumeWriteBuffer(seg.len);
907           m_rbuf.ConsumeReadData(seg.len);
908           // After shifting the position in the buffer, we may have
909           // out-of-order packets ready to be recovered.
910           bRecover = true;
911         }
912       }
913     } else {
914       uint32_t nOffset = seg.seq - m_rcv_nxt;
915 
916       if (!m_rbuf.WriteOffset(seg.data, seg.len, nOffset, NULL)) {
917         // Ignore incoming packets outside of the receive window.
918         return false;
919       }
920 
921       if (seg.seq == m_rcv_nxt) {
922         m_rbuf.ConsumeWriteBuffer(seg.len);
923         m_rcv_nxt += seg.len;
924         m_rcv_wnd -= seg.len;
925         bNewData = true;
926         // May be able to recover packets previously received out-of-order
927         // now.
928         bRecover = true;
929       } else {
930 #if _DEBUGMSG >= _DBG_NORMAL
931         RTC_LOG(LS_INFO) << "Saving " << seg.len << " bytes (" << seg.seq
932                          << " -> " << seg.seq + seg.len << ")";
933 #endif  // _DEBUGMSG
934         RSegment rseg;
935         rseg.seq = seg.seq;
936         rseg.len = seg.len;
937         RList::iterator it = m_rlist.begin();
938         while ((it != m_rlist.end()) && (it->seq < rseg.seq)) {
939           ++it;
940         }
941         m_rlist.insert(it, rseg);
942       }
943     }
944     if (bRecover) {
945       RList::iterator it = m_rlist.begin();
946       while ((it != m_rlist.end()) && (it->seq <= m_rcv_nxt)) {
947         if (it->seq + it->len > m_rcv_nxt) {
948           sflags = sfImmediateAck;  // (Fast Recovery)
949           uint32_t nAdjust = (it->seq + it->len) - m_rcv_nxt;
950 #if _DEBUGMSG >= _DBG_NORMAL
951           RTC_LOG(LS_INFO) << "Recovered " << nAdjust << " bytes (" << m_rcv_nxt
952                            << " -> " << m_rcv_nxt + nAdjust << ")";
953 #endif  // _DEBUGMSG
954           m_rbuf.ConsumeWriteBuffer(nAdjust);
955           m_rcv_nxt += nAdjust;
956           m_rcv_wnd -= nAdjust;
957           bNewData = true;
958         }
959         it = m_rlist.erase(it);
960       }
961     }
962   }
963 
964   attemptSend(sflags);
965 
966   // If we have new data, notify the user
967   if (bNewData && m_bReadEnable) {
968     m_bReadEnable = false;
969     if (m_notify) {
970       m_notify->OnTcpReadable(this);
971     }
972     // notify(evRead);
973   }
974 
975   return true;
976 }
977 
transmit(const SList::iterator & seg,uint32_t now)978 bool PseudoTcp::transmit(const SList::iterator& seg, uint32_t now) {
979   if (seg->xmit >= ((m_state == TCP_ESTABLISHED) ? 15 : 30)) {
980     RTC_LOG_F(LS_VERBOSE) << "too many retransmits";
981     return false;
982   }
983 
984   uint32_t nTransmit = std::min(seg->len, m_mss);
985 
986   while (true) {
987     uint32_t seq = seg->seq;
988     uint8_t flags = (seg->bCtrl ? FLAG_CTL : 0);
989     IPseudoTcpNotify::WriteResult wres =
990         packet(seq, flags, seg->seq - m_snd_una, nTransmit);
991 
992     if (wres == IPseudoTcpNotify::WR_SUCCESS)
993       break;
994 
995     if (wres == IPseudoTcpNotify::WR_FAIL) {
996       RTC_LOG_F(LS_VERBOSE) << "packet failed";
997       return false;
998     }
999 
1000     RTC_DCHECK(wres == IPseudoTcpNotify::WR_TOO_LARGE);
1001 
1002     while (true) {
1003       if (PACKET_MAXIMUMS[m_msslevel + 1] == 0) {
1004         RTC_LOG_F(LS_VERBOSE) << "MTU too small";
1005         return false;
1006       }
1007       // !?! We need to break up all outstanding and pending packets and then
1008       // retransmit!?!
1009 
1010       m_mss = PACKET_MAXIMUMS[++m_msslevel] - PACKET_OVERHEAD;
1011       m_cwnd = 2 * m_mss;  // I added this... haven't researched actual formula
1012       if (m_mss < nTransmit) {
1013         nTransmit = m_mss;
1014         break;
1015       }
1016     }
1017 #if _DEBUGMSG >= _DBG_NORMAL
1018     RTC_LOG(LS_INFO) << "Adjusting mss to " << m_mss << " bytes";
1019 #endif  // _DEBUGMSG
1020   }
1021 
1022   if (nTransmit < seg->len) {
1023     RTC_LOG_F(LS_VERBOSE) << "mss reduced to " << m_mss;
1024 
1025     SSegment subseg(seg->seq + nTransmit, seg->len - nTransmit, seg->bCtrl);
1026     // subseg.tstamp = seg->tstamp;
1027     subseg.xmit = seg->xmit;
1028     seg->len = nTransmit;
1029 
1030     SList::iterator next = seg;
1031     m_slist.insert(++next, subseg);
1032   }
1033 
1034   if (seg->xmit == 0) {
1035     m_snd_nxt += seg->len;
1036   }
1037   seg->xmit += 1;
1038   // seg->tstamp = now;
1039   if (m_rto_base == 0) {
1040     m_rto_base = now;
1041   }
1042 
1043   return true;
1044 }
1045 
attemptSend(SendFlags sflags)1046 void PseudoTcp::attemptSend(SendFlags sflags) {
1047   uint32_t now = Now();
1048 
1049   if (rtc::TimeDiff32(now, m_lastsend) > static_cast<long>(m_rx_rto)) {
1050     m_cwnd = m_mss;
1051   }
1052 
1053 #if _DEBUGMSG
1054   bool bFirst = true;
1055 #endif  // _DEBUGMSG
1056 
1057   while (true) {
1058     uint32_t cwnd = m_cwnd;
1059     if ((m_dup_acks == 1) || (m_dup_acks == 2)) {  // Limited Transmit
1060       cwnd += m_dup_acks * m_mss;
1061     }
1062     uint32_t nWindow = std::min(m_snd_wnd, cwnd);
1063     uint32_t nInFlight = m_snd_nxt - m_snd_una;
1064     uint32_t nUseable = (nInFlight < nWindow) ? (nWindow - nInFlight) : 0;
1065 
1066     size_t snd_buffered = m_sbuf.GetBuffered();
1067     uint32_t nAvailable =
1068         std::min(static_cast<uint32_t>(snd_buffered) - nInFlight, m_mss);
1069 
1070     if (nAvailable > nUseable) {
1071       if (nUseable * 4 < nWindow) {
1072         // RFC 813 - avoid SWS
1073         nAvailable = 0;
1074       } else {
1075         nAvailable = nUseable;
1076       }
1077     }
1078 
1079 #if _DEBUGMSG >= _DBG_VERBOSE
1080     if (bFirst) {
1081       size_t available_space = 0;
1082       m_sbuf.GetWriteRemaining(&available_space);
1083 
1084       bFirst = false;
1085       RTC_LOG(LS_INFO) << "[cwnd: " << m_cwnd << "  nWindow: " << nWindow
1086                        << "  nInFlight: " << nInFlight
1087                        << "  nAvailable: " << nAvailable
1088                        << "  nQueued: " << snd_buffered
1089                        << "  nEmpty: " << available_space
1090                        << "  ssthresh: " << m_ssthresh << "]";
1091     }
1092 #endif  // _DEBUGMSG
1093 
1094     if (nAvailable == 0) {
1095       if (sflags == sfNone)
1096         return;
1097 
1098       // If this is an immediate ack, or the second delayed ack
1099       if ((sflags == sfImmediateAck) || m_t_ack) {
1100         packet(m_snd_nxt, 0, 0, 0);
1101       } else {
1102         m_t_ack = Now();
1103       }
1104       return;
1105     }
1106 
1107     // Nagle's algorithm.
1108     // If there is data already in-flight, and we haven't a full segment of
1109     // data ready to send then hold off until we get more to send, or the
1110     // in-flight data is acknowledged.
1111     if (m_use_nagling && (m_snd_nxt > m_snd_una) && (nAvailable < m_mss)) {
1112       return;
1113     }
1114 
1115     // Find the next segment to transmit
1116     SList::iterator it = m_slist.begin();
1117     while (it->xmit > 0) {
1118       ++it;
1119       RTC_DCHECK(it != m_slist.end());
1120     }
1121     SList::iterator seg = it;
1122 
1123     // If the segment is too large, break it into two
1124     if (seg->len > nAvailable) {
1125       SSegment subseg(seg->seq + nAvailable, seg->len - nAvailable, seg->bCtrl);
1126       seg->len = nAvailable;
1127       m_slist.insert(++it, subseg);
1128     }
1129 
1130     if (!transmit(seg, now)) {
1131       RTC_LOG_F(LS_VERBOSE) << "transmit failed";
1132       // TODO(?): consider closing socket
1133       return;
1134     }
1135 
1136     sflags = sfNone;
1137   }
1138 }
1139 
closedown(uint32_t err)1140 void PseudoTcp::closedown(uint32_t err) {
1141   RTC_LOG(LS_INFO) << "State: TCP_CLOSED";
1142   m_state = TCP_CLOSED;
1143   if (m_notify) {
1144     m_notify->OnTcpClosed(this, err);
1145   }
1146   // notify(evClose, err);
1147 }
1148 
adjustMTU()1149 void PseudoTcp::adjustMTU() {
1150   // Determine our current mss level, so that we can adjust appropriately later
1151   for (m_msslevel = 0; PACKET_MAXIMUMS[m_msslevel + 1] > 0; ++m_msslevel) {
1152     if (static_cast<uint16_t>(PACKET_MAXIMUMS[m_msslevel]) <= m_mtu_advise) {
1153       break;
1154     }
1155   }
1156   m_mss = m_mtu_advise - PACKET_OVERHEAD;
1157 // !?! Should we reset m_largest here?
1158 #if _DEBUGMSG >= _DBG_NORMAL
1159   RTC_LOG(LS_INFO) << "Adjusting mss to " << m_mss << " bytes";
1160 #endif  // _DEBUGMSG
1161   // Enforce minimums on ssthresh and cwnd
1162   m_ssthresh = std::max(m_ssthresh, 2 * m_mss);
1163   m_cwnd = std::max(m_cwnd, m_mss);
1164 }
1165 
isReceiveBufferFull() const1166 bool PseudoTcp::isReceiveBufferFull() const {
1167   size_t available_space = 0;
1168   m_rbuf.GetWriteRemaining(&available_space);
1169   return !available_space;
1170 }
1171 
disableWindowScale()1172 void PseudoTcp::disableWindowScale() {
1173   m_support_wnd_scale = false;
1174 }
1175 
queueConnectMessage()1176 void PseudoTcp::queueConnectMessage() {
1177   rtc::ByteBufferWriter buf;
1178 
1179   buf.WriteUInt8(CTL_CONNECT);
1180   if (m_support_wnd_scale) {
1181     buf.WriteUInt8(TCP_OPT_WND_SCALE);
1182     buf.WriteUInt8(1);
1183     buf.WriteUInt8(m_rwnd_scale);
1184   }
1185   m_snd_wnd = static_cast<uint32_t>(buf.Length());
1186   queue(buf.Data(), static_cast<uint32_t>(buf.Length()), true);
1187 }
1188 
parseOptions(const char * data,uint32_t len)1189 void PseudoTcp::parseOptions(const char* data, uint32_t len) {
1190   std::set<uint8_t> options_specified;
1191 
1192   // See http://www.freesoft.org/CIE/Course/Section4/8.htm for
1193   // parsing the options list.
1194   rtc::ByteBufferReader buf(data, len);
1195   while (buf.Length()) {
1196     uint8_t kind = TCP_OPT_EOL;
1197     buf.ReadUInt8(&kind);
1198 
1199     if (kind == TCP_OPT_EOL) {
1200       // End of option list.
1201       break;
1202     } else if (kind == TCP_OPT_NOOP) {
1203       // No op.
1204       continue;
1205     }
1206 
1207     // Length of this option.
1208     RTC_DCHECK(len != 0);
1209     uint8_t opt_len = 0;
1210     buf.ReadUInt8(&opt_len);
1211 
1212     // Content of this option.
1213     if (opt_len <= buf.Length()) {
1214       applyOption(kind, buf.Data(), opt_len);
1215       buf.Consume(opt_len);
1216     } else {
1217       RTC_LOG(LS_ERROR) << "Invalid option length received.";
1218       return;
1219     }
1220     options_specified.insert(kind);
1221   }
1222 
1223   if (options_specified.find(TCP_OPT_WND_SCALE) == options_specified.end()) {
1224     RTC_LOG(LS_WARNING) << "Peer doesn't support window scaling";
1225 
1226     if (m_rwnd_scale > 0) {
1227       // Peer doesn't support TCP options and window scaling.
1228       // Revert receive buffer size to default value.
1229       resizeReceiveBuffer(DEFAULT_RCV_BUF_SIZE);
1230       m_swnd_scale = 0;
1231     }
1232   }
1233 }
1234 
applyOption(char kind,const char * data,uint32_t len)1235 void PseudoTcp::applyOption(char kind, const char* data, uint32_t len) {
1236   if (kind == TCP_OPT_MSS) {
1237     RTC_LOG(LS_WARNING) << "Peer specified MSS option which is not supported.";
1238     // TODO(?): Implement.
1239   } else if (kind == TCP_OPT_WND_SCALE) {
1240     // Window scale factor.
1241     // http://www.ietf.org/rfc/rfc1323.txt
1242     if (len != 1) {
1243       RTC_LOG_F(LS_WARNING) << "Invalid window scale option received.";
1244       return;
1245     }
1246     applyWindowScaleOption(data[0]);
1247   }
1248 }
1249 
applyWindowScaleOption(uint8_t scale_factor)1250 void PseudoTcp::applyWindowScaleOption(uint8_t scale_factor) {
1251   m_swnd_scale = scale_factor;
1252 }
1253 
resizeSendBuffer(uint32_t new_size)1254 void PseudoTcp::resizeSendBuffer(uint32_t new_size) {
1255   m_sbuf_len = new_size;
1256   m_sbuf.SetCapacity(new_size);
1257 }
1258 
resizeReceiveBuffer(uint32_t new_size)1259 void PseudoTcp::resizeReceiveBuffer(uint32_t new_size) {
1260   uint8_t scale_factor = 0;
1261 
1262   // Determine the scale factor such that the scaled window size can fit
1263   // in a 16-bit unsigned integer.
1264   while (new_size > 0xFFFF) {
1265     ++scale_factor;
1266     new_size >>= 1;
1267   }
1268 
1269   // Determine the proper size of the buffer.
1270   new_size <<= scale_factor;
1271   bool result = m_rbuf.SetCapacity(new_size);
1272 
1273   // Make sure the new buffer is large enough to contain data in the old
1274   // buffer. This should always be true because this method is called either
1275   // before connection is established or when peers are exchanging connect
1276   // messages.
1277   RTC_DCHECK(result);
1278   m_rbuf_len = new_size;
1279   m_rwnd_scale = scale_factor;
1280   m_ssthresh = new_size;
1281 
1282   size_t available_space = 0;
1283   m_rbuf.GetWriteRemaining(&available_space);
1284   m_rcv_wnd = static_cast<uint32_t>(available_space);
1285 }
1286 
LockedFifoBuffer(size_t size)1287 PseudoTcp::LockedFifoBuffer::LockedFifoBuffer(size_t size)
1288     : buffer_(new char[size]),
1289       buffer_length_(size),
1290       data_length_(0),
1291       read_position_(0) {}
1292 
~LockedFifoBuffer()1293 PseudoTcp::LockedFifoBuffer::~LockedFifoBuffer() {}
1294 
GetBuffered() const1295 size_t PseudoTcp::LockedFifoBuffer::GetBuffered() const {
1296   webrtc::MutexLock lock(&mutex_);
1297   return data_length_;
1298 }
1299 
SetCapacity(size_t size)1300 bool PseudoTcp::LockedFifoBuffer::SetCapacity(size_t size) {
1301   webrtc::MutexLock lock(&mutex_);
1302   if (data_length_ > size)
1303     return false;
1304 
1305   if (size != buffer_length_) {
1306     char* buffer = new char[size];
1307     const size_t copy = data_length_;
1308     const size_t tail_copy = std::min(copy, buffer_length_ - read_position_);
1309     memcpy(buffer, &buffer_[read_position_], tail_copy);
1310     memcpy(buffer + tail_copy, &buffer_[0], copy - tail_copy);
1311     buffer_.reset(buffer);
1312     read_position_ = 0;
1313     buffer_length_ = size;
1314   }
1315 
1316   return true;
1317 }
1318 
ReadOffset(void * buffer,size_t bytes,size_t offset,size_t * bytes_read)1319 bool PseudoTcp::LockedFifoBuffer::ReadOffset(void* buffer,
1320                                              size_t bytes,
1321                                              size_t offset,
1322                                              size_t* bytes_read) {
1323   webrtc::MutexLock lock(&mutex_);
1324   return ReadOffsetLocked(buffer, bytes, offset, bytes_read);
1325 }
1326 
WriteOffset(const void * buffer,size_t bytes,size_t offset,size_t * bytes_written)1327 bool PseudoTcp::LockedFifoBuffer::WriteOffset(const void* buffer,
1328                                               size_t bytes,
1329                                               size_t offset,
1330                                               size_t* bytes_written) {
1331   webrtc::MutexLock lock(&mutex_);
1332   return WriteOffsetLocked(buffer, bytes, offset, bytes_written);
1333 }
1334 
Read(void * buffer,size_t bytes,size_t * bytes_read)1335 bool PseudoTcp::LockedFifoBuffer::Read(void* buffer,
1336                                        size_t bytes,
1337                                        size_t* bytes_read) {
1338   webrtc::MutexLock lock(&mutex_);
1339   size_t copy = 0;
1340   if (!ReadOffsetLocked(buffer, bytes, 0, &copy))
1341     return false;
1342 
1343   // If read was successful then adjust the read position and number of
1344   // bytes buffered.
1345   read_position_ = (read_position_ + copy) % buffer_length_;
1346   data_length_ -= copy;
1347   if (bytes_read)
1348     *bytes_read = copy;
1349 
1350   return true;
1351 }
1352 
Write(const void * buffer,size_t bytes,size_t * bytes_written)1353 bool PseudoTcp::LockedFifoBuffer::Write(const void* buffer,
1354                                         size_t bytes,
1355                                         size_t* bytes_written) {
1356   webrtc::MutexLock lock(&mutex_);
1357   size_t copy = 0;
1358   if (!WriteOffsetLocked(buffer, bytes, 0, &copy))
1359     return false;
1360 
1361   // If write was successful then adjust the number of readable bytes.
1362   data_length_ += copy;
1363   if (bytes_written) {
1364     *bytes_written = copy;
1365   }
1366 
1367   return true;
1368 }
1369 
ConsumeReadData(size_t size)1370 void PseudoTcp::LockedFifoBuffer::ConsumeReadData(size_t size) {
1371   webrtc::MutexLock lock(&mutex_);
1372   RTC_DCHECK(size <= data_length_);
1373   read_position_ = (read_position_ + size) % buffer_length_;
1374   data_length_ -= size;
1375 }
1376 
ConsumeWriteBuffer(size_t size)1377 void PseudoTcp::LockedFifoBuffer::ConsumeWriteBuffer(size_t size) {
1378   webrtc::MutexLock lock(&mutex_);
1379   RTC_DCHECK(size <= buffer_length_ - data_length_);
1380   data_length_ += size;
1381 }
1382 
GetWriteRemaining(size_t * size) const1383 bool PseudoTcp::LockedFifoBuffer::GetWriteRemaining(size_t* size) const {
1384   webrtc::MutexLock lock(&mutex_);
1385   *size = buffer_length_ - data_length_;
1386   return true;
1387 }
1388 
ReadOffsetLocked(void * buffer,size_t bytes,size_t offset,size_t * bytes_read)1389 bool PseudoTcp::LockedFifoBuffer::ReadOffsetLocked(void* buffer,
1390                                                    size_t bytes,
1391                                                    size_t offset,
1392                                                    size_t* bytes_read) {
1393   if (offset >= data_length_)
1394     return false;
1395 
1396   const size_t available = data_length_ - offset;
1397   const size_t read_position = (read_position_ + offset) % buffer_length_;
1398   const size_t copy = std::min(bytes, available);
1399   const size_t tail_copy = std::min(copy, buffer_length_ - read_position);
1400   char* const p = static_cast<char*>(buffer);
1401   memcpy(p, &buffer_[read_position], tail_copy);
1402   memcpy(p + tail_copy, &buffer_[0], copy - tail_copy);
1403 
1404   if (bytes_read)
1405     *bytes_read = copy;
1406 
1407   return true;
1408 }
1409 
WriteOffsetLocked(const void * buffer,size_t bytes,size_t offset,size_t * bytes_written)1410 bool PseudoTcp::LockedFifoBuffer::WriteOffsetLocked(const void* buffer,
1411                                                     size_t bytes,
1412                                                     size_t offset,
1413                                                     size_t* bytes_written) {
1414   if (data_length_ + offset >= buffer_length_)
1415     return false;
1416 
1417   const size_t available = buffer_length_ - data_length_ - offset;
1418   const size_t write_position =
1419       (read_position_ + data_length_ + offset) % buffer_length_;
1420   const size_t copy = std::min(bytes, available);
1421   const size_t tail_copy = std::min(copy, buffer_length_ - write_position);
1422   const char* const p = static_cast<const char*>(buffer);
1423   memcpy(&buffer_[write_position], p, tail_copy);
1424   memcpy(&buffer_[0], p + tail_copy, copy - tail_copy);
1425 
1426   if (bytes_written)
1427     *bytes_written = copy;
1428 
1429   return true;
1430 }
1431 
1432 }  // namespace cricket
1433