blob: ec00c048288ea642b47ac425490625cb8584f888 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, 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#include "talk/p2p/base/asyncstuntcpsocket.h"
29
30#include <cstring>
31
32#include "talk/base/common.h"
33#include "talk/base/logging.h"
34#include "talk/p2p/base/stun.h"
35
36namespace cricket {
37
38static const size_t kMaxPacketSize = 64 * 1024;
39
40typedef uint16 PacketLength;
41static const size_t kPacketLenSize = sizeof(PacketLength);
42static const size_t kPacketLenOffset = 2;
43static const size_t kBufSize = kMaxPacketSize + kStunHeaderSize;
44static const size_t kTurnChannelDataHdrSize = 4;
45
46inline bool IsStunMessage(uint16 msg_type) {
47 // The first two bits of a channel data message are 0b01.
48 return (msg_type & 0xC000) ? false : true;
49}
50
51// AsyncStunTCPSocket
52// Binds and connects |socket| and creates AsyncTCPSocket for
53// it. Takes ownership of |socket|. Returns NULL if bind() or
54// connect() fail (|socket| is destroyed in that case).
55AsyncStunTCPSocket* AsyncStunTCPSocket::Create(
56 talk_base::AsyncSocket* socket,
57 const talk_base::SocketAddress& bind_address,
58 const talk_base::SocketAddress& remote_address) {
59 return new AsyncStunTCPSocket(AsyncTCPSocketBase::ConnectSocket(
60 socket, bind_address, remote_address), false);
61}
62
63AsyncStunTCPSocket::AsyncStunTCPSocket(
64 talk_base::AsyncSocket* socket, bool listen)
65 : talk_base::AsyncTCPSocketBase(socket, listen, kBufSize) {
66}
67
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000068// TODO(mallinath) - Add support of setting DSCP code on AsyncSocket.
69int AsyncStunTCPSocket::Send(const void *pv, size_t cb,
70 talk_base::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 if (cb > kBufSize || cb < kPacketLenSize + kPacketLenOffset) {
72 SetError(EMSGSIZE);
73 return -1;
74 }
75
76 // If we are blocking on send, then silently drop this packet
77 if (!IsOutBufferEmpty())
78 return static_cast<int>(cb);
79
80 int pad_bytes;
81 size_t expected_pkt_len = GetExpectedLength(pv, cb, &pad_bytes);
82
83 // Accepts only complete STUN/ChannelData packets.
84 if (cb != expected_pkt_len)
85 return -1;
86
87 AppendToOutBuffer(pv, cb);
88
89 ASSERT(pad_bytes < 4);
90 char padding[4] = {0};
91 AppendToOutBuffer(padding, pad_bytes);
92
93 int res = FlushOutBuffer();
94 if (res <= 0) {
95 // drop packet if we made no progress
96 ClearOutBuffer();
97 return res;
98 }
99
100 // We claim to have sent the whole thing, even if we only sent partial
101 return static_cast<int>(cb);
102}
103
104void AsyncStunTCPSocket::ProcessInput(char* data, size_t* len) {
105 talk_base::SocketAddress remote_addr(GetRemoteAddress());
106 // STUN packet - First 4 bytes. Total header size is 20 bytes.
107 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 // |0 0| STUN Message Type | Message Length |
109 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110
111 // TURN ChannelData
112 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 // | Channel Number | Length |
114 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115
116 while (true) {
117 // We need at least 4 bytes to read the STUN or ChannelData packet length.
118 if (*len < kPacketLenOffset + kPacketLenSize)
119 return;
120
121 int pad_bytes;
122 size_t expected_pkt_len = GetExpectedLength(data, *len, &pad_bytes);
123 size_t actual_length = expected_pkt_len + pad_bytes;
124
125 if (*len < actual_length) {
126 return;
127 }
128
wu@webrtc.org20182692013-12-12 22:54:25 +0000129 SignalReadPacket(this, data, expected_pkt_len, remote_addr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 *len -= actual_length;
132 if (*len > 0) {
133 memmove(data, data + actual_length, *len);
134 }
135 }
136}
137
138void AsyncStunTCPSocket::HandleIncomingConnection(
139 talk_base::AsyncSocket* socket) {
140 SignalNewConnection(this, new AsyncStunTCPSocket(socket, false));
141}
142
143size_t AsyncStunTCPSocket::GetExpectedLength(const void* data, size_t len,
144 int* pad_bytes) {
145 *pad_bytes = 0;
146 PacketLength pkt_len =
147 talk_base::GetBE16(static_cast<const char*>(data) + kPacketLenOffset);
148 size_t expected_pkt_len;
149 uint16 msg_type = talk_base::GetBE16(data);
150 if (IsStunMessage(msg_type)) {
151 // STUN message.
152 expected_pkt_len = kStunHeaderSize + pkt_len;
153 } else {
154 // TURN ChannelData message.
155 expected_pkt_len = kTurnChannelDataHdrSize + pkt_len;
156 // From RFC 5766 section 11.5
157 // Over TCP and TLS-over-TCP, the ChannelData message MUST be padded to
158 // a multiple of four bytes in order to ensure the alignment of
159 // subsequent messages. The padding is not reflected in the length
160 // field of the ChannelData message, so the actual size of a ChannelData
161 // message (including padding) is (4 + Length) rounded up to the nearest
162 // multiple of 4. Over UDP, the padding is not required but MAY be
163 // included.
164 if (expected_pkt_len % 4)
165 *pad_bytes = 4 - (expected_pkt_len % 4);
166 }
167 return expected_pkt_len;
168}
169
170} // namespace cricket