blob: b85e25ad14afca262862e84376daae30ac110aa9 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
2 * Copyright 2013 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
Steve Anton10542f22019-01-11 17:11:0011#include "p2p/base/async_stun_tcp_socket.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1112
Yves Gerey3e707812018-11-28 15:47:4913#include <errno.h>
14#include <stdint.h>
henrike@webrtc.org269fb4b2014-10-28 22:20:1115#include <string.h>
16
Patrik Höglund56d94522019-11-18 14:53:3217#include "api/transport/stun.h"
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 15:47:4920#include "rtc_base/network/sent_packet.h"
21#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 17:11:0022#include "rtc_base/time_utils.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1123
24namespace cricket {
25
26static const size_t kMaxPacketSize = 64 * 1024;
27
Peter Boström0c4e06b2015-10-07 10:23:2128typedef uint16_t PacketLength;
henrike@webrtc.org269fb4b2014-10-28 22:20:1129static const size_t kPacketLenSize = sizeof(PacketLength);
30static const size_t kPacketLenOffset = 2;
31static const size_t kBufSize = kMaxPacketSize + kStunHeaderSize;
32static const size_t kTurnChannelDataHdrSize = 4;
33
Peter Boström0c4e06b2015-10-07 10:23:2134inline bool IsStunMessage(uint16_t msg_type) {
henrike@webrtc.org269fb4b2014-10-28 22:20:1135 // The first two bits of a channel data message are 0b01.
36 return (msg_type & 0xC000) ? false : true;
37}
38
39// AsyncStunTCPSocket
40// Binds and connects |socket| and creates AsyncTCPSocket for
41// it. Takes ownership of |socket|. Returns NULL if bind() or
42// connect() fail (|socket| is destroyed in that case).
43AsyncStunTCPSocket* AsyncStunTCPSocket::Create(
44 rtc::AsyncSocket* socket,
45 const rtc::SocketAddress& bind_address,
46 const rtc::SocketAddress& remote_address) {
Yves Gerey665174f2018-06-19 13:03:0547 return new AsyncStunTCPSocket(
48 AsyncTCPSocketBase::ConnectSocket(socket, bind_address, remote_address),
49 false);
henrike@webrtc.org269fb4b2014-10-28 22:20:1150}
51
Yves Gerey665174f2018-06-19 13:03:0552AsyncStunTCPSocket::AsyncStunTCPSocket(rtc::AsyncSocket* socket, bool listen)
53 : rtc::AsyncTCPSocketBase(socket, listen, kBufSize) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:1154
Yves Gerey665174f2018-06-19 13:03:0555int AsyncStunTCPSocket::Send(const void* pv,
56 size_t cb,
henrike@webrtc.org269fb4b2014-10-28 22:20:1157 const rtc::PacketOptions& options) {
58 if (cb > kBufSize || cb < kPacketLenSize + kPacketLenOffset) {
59 SetError(EMSGSIZE);
60 return -1;
61 }
62
63 // If we are blocking on send, then silently drop this packet
64 if (!IsOutBufferEmpty())
65 return static_cast<int>(cb);
66
67 int pad_bytes;
68 size_t expected_pkt_len = GetExpectedLength(pv, cb, &pad_bytes);
69
70 // Accepts only complete STUN/ChannelData packets.
71 if (cb != expected_pkt_len)
72 return -1;
73
74 AppendToOutBuffer(pv, cb);
75
nisseede5da42017-01-12 13:15:3676 RTC_DCHECK(pad_bytes < 4);
henrike@webrtc.org269fb4b2014-10-28 22:20:1177 char padding[4] = {0};
78 AppendToOutBuffer(padding, pad_bytes);
79
80 int res = FlushOutBuffer();
81 if (res <= 0) {
82 // drop packet if we made no progress
83 ClearOutBuffer();
84 return res;
85 }
86
deadbeefb56671e2017-05-27 01:40:0587 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis());
88 SignalSentPacket(this, sent_packet);
89
henrike@webrtc.org269fb4b2014-10-28 22:20:1190 // We claim to have sent the whole thing, even if we only sent partial
91 return static_cast<int>(cb);
92}
93
94void AsyncStunTCPSocket::ProcessInput(char* data, size_t* len) {
95 rtc::SocketAddress remote_addr(GetRemoteAddress());
96 // STUN packet - First 4 bytes. Total header size is 20 bytes.
97 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 // |0 0| STUN Message Type | Message Length |
99 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100
101 // TURN ChannelData
102 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 // | Channel Number | Length |
104 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105
106 while (true) {
107 // We need at least 4 bytes to read the STUN or ChannelData packet length.
108 if (*len < kPacketLenOffset + kPacketLenSize)
109 return;
110
111 int pad_bytes;
112 size_t expected_pkt_len = GetExpectedLength(data, *len, &pad_bytes);
113 size_t actual_length = expected_pkt_len + pad_bytes;
114
115 if (*len < actual_length) {
116 return;
117 }
118
119 SignalReadPacket(this, data, expected_pkt_len, remote_addr,
Niels Möller15ca5a92018-11-01 13:32:47120 rtc::TimeMicros());
henrike@webrtc.org269fb4b2014-10-28 22:20:11121
122 *len -= actual_length;
123 if (*len > 0) {
124 memmove(data, data + actual_length, *len);
125 }
126 }
127}
128
Yves Gerey665174f2018-06-19 13:03:05129void AsyncStunTCPSocket::HandleIncomingConnection(rtc::AsyncSocket* socket) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11130 SignalNewConnection(this, new AsyncStunTCPSocket(socket, false));
131}
132
Yves Gerey665174f2018-06-19 13:03:05133size_t AsyncStunTCPSocket::GetExpectedLength(const void* data,
134 size_t len,
henrike@webrtc.org269fb4b2014-10-28 22:20:11135 int* pad_bytes) {
136 *pad_bytes = 0;
137 PacketLength pkt_len =
138 rtc::GetBE16(static_cast<const char*>(data) + kPacketLenOffset);
139 size_t expected_pkt_len;
Peter Boström0c4e06b2015-10-07 10:23:21140 uint16_t msg_type = rtc::GetBE16(data);
henrike@webrtc.org269fb4b2014-10-28 22:20:11141 if (IsStunMessage(msg_type)) {
142 // STUN message.
143 expected_pkt_len = kStunHeaderSize + pkt_len;
144 } else {
145 // TURN ChannelData message.
146 expected_pkt_len = kTurnChannelDataHdrSize + pkt_len;
147 // From RFC 5766 section 11.5
148 // Over TCP and TLS-over-TCP, the ChannelData message MUST be padded to
149 // a multiple of four bytes in order to ensure the alignment of
150 // subsequent messages. The padding is not reflected in the length
151 // field of the ChannelData message, so the actual size of a ChannelData
152 // message (including padding) is (4 + Length) rounded up to the nearest
153 // multiple of 4. Over UDP, the padding is not required but MAY be
154 // included.
155 if (expected_pkt_len % 4)
156 *pad_bytes = 4 - (expected_pkt_len % 4);
157 }
158 return expected_pkt_len;
159}
160
161} // namespace cricket