~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/pseudotcp.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2004--2005, Google Inc.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without 
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
8
 *  1. Redistributions of source code must retain the above copyright notice, 
 
9
 *     this list of conditions and the following disclaimer.
 
10
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 
11
 *     this list of conditions and the following disclaimer in the documentation
 
12
 *     and/or other materials provided with the distribution.
 
13
 *  3. The name of the author may not be used to endorse or promote products 
 
14
 *     derived from this software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 
20
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 
24
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 
25
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
#ifndef TALK_P2P_BASE_PSEUDOTCP_H_
 
29
#define TALK_P2P_BASE_PSEUDOTCP_H_
 
30
 
 
31
#include <list>
 
32
 
 
33
#include "talk/base/basictypes.h"
 
34
#include "talk/base/stream.h"
 
35
 
 
36
namespace cricket {
 
37
 
 
38
//////////////////////////////////////////////////////////////////////
 
39
// IPseudoTcpNotify
 
40
//////////////////////////////////////////////////////////////////////
 
41
 
 
42
class PseudoTcp;
 
43
 
 
44
class IPseudoTcpNotify {
 
45
 public:
 
46
  virtual ~IPseudoTcpNotify() {}
 
47
  // Notification of tcp events
 
48
  virtual void OnTcpOpen(PseudoTcp* tcp) = 0;
 
49
  virtual void OnTcpReadable(PseudoTcp* tcp) = 0;
 
50
  virtual void OnTcpWriteable(PseudoTcp* tcp) = 0;
 
51
  virtual void OnTcpClosed(PseudoTcp* tcp, uint32 error) = 0;
 
52
 
 
53
  // Write the packet onto the network
 
54
  enum WriteResult { WR_SUCCESS, WR_TOO_LARGE, WR_FAIL };
 
55
  virtual WriteResult TcpWritePacket(PseudoTcp* tcp,
 
56
                                     const char* buffer, size_t len) = 0;
 
57
};
 
58
 
 
59
//////////////////////////////////////////////////////////////////////
 
60
// PseudoTcp
 
61
//////////////////////////////////////////////////////////////////////
 
62
 
 
63
class PseudoTcp {
 
64
 public:
 
65
  static uint32 Now();
 
66
 
 
67
  PseudoTcp(IPseudoTcpNotify* notify, uint32 conv);
 
68
  virtual ~PseudoTcp();
 
69
 
 
70
  int Connect();
 
71
  int Recv(char* buffer, size_t len);
 
72
  int Send(const char* buffer, size_t len);
 
73
  void Close(bool force);
 
74
  int GetError();
 
75
 
 
76
  enum TcpState {
 
77
    TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECEIVED, TCP_ESTABLISHED, TCP_CLOSED
 
78
  };
 
79
  TcpState State() const { return m_state; }
 
80
 
 
81
  // Call this when the PMTU changes.
 
82
  void NotifyMTU(uint16 mtu);
 
83
 
 
84
  // Call this based on timeout value returned from GetNextClock.
 
85
  // It's ok to call this too frequently.
 
86
  void NotifyClock(uint32 now);
 
87
 
 
88
  // Call this whenever a packet arrives.
 
89
  // Returns true if the packet was processed successfully.
 
90
  bool NotifyPacket(const char * buffer, size_t len);
 
91
 
 
92
  // Call this to determine the next time NotifyClock should be called.
 
93
  // Returns false if the socket is ready to be destroyed.
 
94
  bool GetNextClock(uint32 now, long& timeout);
 
95
 
 
96
  // Call these to get/set option values to tailor this PseudoTcp
 
97
  // instance's behaviour for the kind of data it will carry.
 
98
  // If an unrecognized option is set or got, an assertion will fire.
 
99
  //
 
100
  // Setting options for OPT_RCVBUF or OPT_SNDBUF after Connect() is called
 
101
  // will result in an assertion.
 
102
  enum Option {
 
103
    OPT_NODELAY,      // Whether to enable Nagle's algorithm (0 == off)
 
104
    OPT_ACKDELAY,     // The Delayed ACK timeout (0 == off).
 
105
    OPT_RCVBUF,       // Set the receive buffer size, in bytes.
 
106
    OPT_SNDBUF,       // Set the send buffer size, in bytes.
 
107
  };
 
108
  void GetOption(Option opt, int* value);
 
109
  void SetOption(Option opt, int value);
 
110
 
 
111
  // Returns current congestion window in bytes.
 
112
  uint32 GetCongestionWindow() const;
 
113
 
 
114
  // Returns amount of data in bytes that has been sent, but haven't
 
115
  // been acknowledged.
 
116
  uint32 GetBytesInFlight() const;
 
117
 
 
118
  // Returns number of bytes that were written in buffer and haven't
 
119
  // been sent.
 
120
  uint32 GetBytesBufferedNotSent() const;
 
121
 
 
122
  // Returns current round-trip time estimate in milliseconds.
 
123
  uint32 GetRoundTripTimeEstimateMs() const;
 
124
 
 
125
 protected:
 
126
  enum SendFlags { sfNone, sfDelayedAck, sfImmediateAck };
 
127
 
 
128
  struct Segment {
 
129
    uint32 conv, seq, ack;
 
130
    uint8 flags;
 
131
    uint16 wnd;
 
132
    const char * data;
 
133
    uint32 len;
 
134
    uint32 tsval, tsecr;
 
135
  };
 
136
 
 
137
  struct SSegment {
 
138
    SSegment(uint32 s, uint32 l, bool c)
 
139
        : seq(s), len(l), /*tstamp(0),*/ xmit(0), bCtrl(c) {
 
140
    }
 
141
    uint32 seq, len;
 
142
    //uint32 tstamp;
 
143
    uint8 xmit;
 
144
    bool bCtrl;
 
145
  };
 
146
  typedef std::list<SSegment> SList;
 
147
 
 
148
  struct RSegment {
 
149
    uint32 seq, len;
 
150
  };
 
151
 
 
152
  uint32 queue(const char* data, uint32 len, bool bCtrl);
 
153
 
 
154
  // Creates a packet and submits it to the network. This method can either
 
155
  // send payload or just an ACK packet.
 
156
  //
 
157
  // |seq| is the sequence number of this packet.
 
158
  // |flags| is the flags for sending this packet.
 
159
  // |offset| is the offset to read from |m_sbuf|.
 
160
  // |len| is the number of bytes to read from |m_sbuf| as payload. If this
 
161
  // value is 0 then this is an ACK packet, otherwise this packet has payload.
 
162
  IPseudoTcpNotify::WriteResult packet(uint32 seq, uint8 flags,
 
163
                                       uint32 offset, uint32 len);
 
164
  bool parse(const uint8* buffer, uint32 size);
 
165
 
 
166
  void attemptSend(SendFlags sflags = sfNone);
 
167
 
 
168
  void closedown(uint32 err = 0);
 
169
 
 
170
  bool clock_check(uint32 now, long& nTimeout);
 
171
 
 
172
  bool process(Segment& seg);
 
173
  bool transmit(const SList::iterator& seg, uint32 now);
 
174
 
 
175
  void adjustMTU();
 
176
 
 
177
 protected:
 
178
  // This method is used in test only to query receive buffer state.
 
179
  bool isReceiveBufferFull() const;
 
180
 
 
181
  // This method is only used in tests, to disable window scaling
 
182
  // support for testing backward compatibility.
 
183
  void disableWindowScale();
 
184
 
 
185
 private:
 
186
  // Queue the connect message with TCP options.
 
187
  void queueConnectMessage();
 
188
 
 
189
  // Parse TCP options in the header.
 
190
  void parseOptions(const char* data, uint32 len);
 
191
 
 
192
  // Apply a TCP option that has been read from the header.
 
193
  void applyOption(char kind, const char* data, uint32 len);
 
194
 
 
195
  // Apply window scale option.
 
196
  void applyWindowScaleOption(uint8 scale_factor);
 
197
 
 
198
  // Resize the send buffer with |new_size| in bytes.
 
199
  void resizeSendBuffer(uint32 new_size);
 
200
 
 
201
  // Resize the receive buffer with |new_size| in bytes. This call adjusts
 
202
  // window scale factor |m_swnd_scale| accordingly.
 
203
  void resizeReceiveBuffer(uint32 new_size);
 
204
 
 
205
  IPseudoTcpNotify* m_notify;
 
206
  enum Shutdown { SD_NONE, SD_GRACEFUL, SD_FORCEFUL } m_shutdown;
 
207
  int m_error;
 
208
 
 
209
  // TCB data
 
210
  TcpState m_state;
 
211
  uint32 m_conv;
 
212
  bool m_bReadEnable, m_bWriteEnable, m_bOutgoing;
 
213
  uint32 m_lasttraffic;
 
214
 
 
215
  // Incoming data
 
216
  typedef std::list<RSegment> RList;
 
217
  RList m_rlist;
 
218
  uint32 m_rbuf_len, m_rcv_nxt, m_rcv_wnd, m_lastrecv;
 
219
  uint8 m_rwnd_scale;  // Window scale factor.
 
220
  talk_base::FifoBuffer m_rbuf;
 
221
 
 
222
  // Outgoing data
 
223
  SList m_slist;
 
224
  uint32 m_sbuf_len, m_snd_nxt, m_snd_wnd, m_lastsend, m_snd_una;
 
225
  uint8 m_swnd_scale;  // Window scale factor.
 
226
  talk_base::FifoBuffer m_sbuf;
 
227
 
 
228
  // Maximum segment size, estimated protocol level, largest segment sent
 
229
  uint32 m_mss, m_msslevel, m_largest, m_mtu_advise;
 
230
  // Retransmit timer
 
231
  uint32 m_rto_base;
 
232
 
 
233
  // Timestamp tracking
 
234
  uint32 m_ts_recent, m_ts_lastack;
 
235
 
 
236
  // Round-trip calculation
 
237
  uint32 m_rx_rttvar, m_rx_srtt, m_rx_rto;
 
238
 
 
239
  // Congestion avoidance, Fast retransmit/recovery, Delayed ACKs
 
240
  uint32 m_ssthresh, m_cwnd;
 
241
  uint8 m_dup_acks;
 
242
  uint32 m_recover;
 
243
  uint32 m_t_ack;
 
244
 
 
245
  // Configuration options
 
246
  bool m_use_nagling;
 
247
  uint32 m_ack_delay;
 
248
 
 
249
  // This is used by unit tests to test backward compatibility of
 
250
  // PseudoTcp implementations that don't support window scaling.
 
251
  bool m_support_wnd_scale;
 
252
};
 
253
 
 
254
}  // namespace cricket
 
255
 
 
256
#endif  // TALK_P2P_BASE_PSEUDOTCP_H_