henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/asynctcpsocket.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 15 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 16 | #include <memory> |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/byteorder.h" |
| 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/logging.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | |
| 22 | #if defined(WEBRTC_POSIX) |
| 23 | #include <errno.h> |
| 24 | #endif // WEBRTC_POSIX |
| 25 | |
| 26 | namespace rtc { |
| 27 | |
| 28 | static const size_t kMaxPacketSize = 64 * 1024; |
| 29 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 30 | typedef uint16_t PacketLength; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | static const size_t kPacketLenSize = sizeof(PacketLength); |
| 32 | |
| 33 | static const size_t kBufSize = kMaxPacketSize + kPacketLenSize; |
| 34 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 35 | // The input buffer will be resized so that at least kMinimumRecvSize bytes can |
| 36 | // be received (but it will not grow above the maximum size passed to the |
| 37 | // constructor). |
| 38 | static const size_t kMinimumRecvSize = 128; |
| 39 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | static const int kListenBacklog = 5; |
| 41 | |
| 42 | // Binds and connects |socket| |
| 43 | AsyncSocket* AsyncTCPSocketBase::ConnectSocket( |
| 44 | rtc::AsyncSocket* socket, |
| 45 | const rtc::SocketAddress& bind_address, |
| 46 | const rtc::SocketAddress& remote_address) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 47 | std::unique_ptr<rtc::AsyncSocket> owned_socket(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | if (socket->Bind(bind_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 49 | RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 50 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | } |
| 52 | if (socket->Connect(remote_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 53 | RTC_LOG(LS_ERROR) << "Connect() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 54 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | } |
| 56 | return owned_socket.release(); |
| 57 | } |
| 58 | |
| 59 | AsyncTCPSocketBase::AsyncTCPSocketBase(AsyncSocket* socket, bool listen, |
| 60 | size_t max_packet_size) |
| 61 | : socket_(socket), |
| 62 | listen_(listen), |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 63 | max_insize_(max_packet_size), |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 64 | max_outsize_(max_packet_size) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 65 | if (!listen_) { |
| 66 | // Listening sockets don't send/receive data, so they don't need buffers. |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 67 | inbuf_.EnsureCapacity(kMinimumRecvSize); |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 68 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 70 | RTC_DCHECK(socket_.get() != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | socket_->SignalConnectEvent.connect( |
| 72 | this, &AsyncTCPSocketBase::OnConnectEvent); |
| 73 | socket_->SignalReadEvent.connect(this, &AsyncTCPSocketBase::OnReadEvent); |
| 74 | socket_->SignalWriteEvent.connect(this, &AsyncTCPSocketBase::OnWriteEvent); |
| 75 | socket_->SignalCloseEvent.connect(this, &AsyncTCPSocketBase::OnCloseEvent); |
| 76 | |
| 77 | if (listen_) { |
| 78 | if (socket_->Listen(kListenBacklog) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 79 | RTC_LOG(LS_ERROR) << "Listen() failed with error " << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 84 | AsyncTCPSocketBase::~AsyncTCPSocketBase() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | |
| 86 | SocketAddress AsyncTCPSocketBase::GetLocalAddress() const { |
| 87 | return socket_->GetLocalAddress(); |
| 88 | } |
| 89 | |
| 90 | SocketAddress AsyncTCPSocketBase::GetRemoteAddress() const { |
| 91 | return socket_->GetRemoteAddress(); |
| 92 | } |
| 93 | |
| 94 | int AsyncTCPSocketBase::Close() { |
| 95 | return socket_->Close(); |
| 96 | } |
| 97 | |
| 98 | AsyncTCPSocket::State AsyncTCPSocketBase::GetState() const { |
| 99 | switch (socket_->GetState()) { |
| 100 | case Socket::CS_CLOSED: |
| 101 | return STATE_CLOSED; |
| 102 | case Socket::CS_CONNECTING: |
| 103 | if (listen_) { |
| 104 | return STATE_BOUND; |
| 105 | } else { |
| 106 | return STATE_CONNECTING; |
| 107 | } |
| 108 | case Socket::CS_CONNECTED: |
| 109 | return STATE_CONNECTED; |
| 110 | default: |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 111 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 112 | return STATE_CLOSED; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | int AsyncTCPSocketBase::GetOption(Socket::Option opt, int* value) { |
| 117 | return socket_->GetOption(opt, value); |
| 118 | } |
| 119 | |
| 120 | int AsyncTCPSocketBase::SetOption(Socket::Option opt, int value) { |
| 121 | return socket_->SetOption(opt, value); |
| 122 | } |
| 123 | |
| 124 | int AsyncTCPSocketBase::GetError() const { |
| 125 | return socket_->GetError(); |
| 126 | } |
| 127 | |
| 128 | void AsyncTCPSocketBase::SetError(int error) { |
| 129 | return socket_->SetError(error); |
| 130 | } |
| 131 | |
| 132 | int AsyncTCPSocketBase::SendTo(const void *pv, size_t cb, |
| 133 | const SocketAddress& addr, |
| 134 | const rtc::PacketOptions& options) { |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 135 | const SocketAddress& remote_address = GetRemoteAddress(); |
| 136 | if (addr == remote_address) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | return Send(pv, cb, options); |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 138 | // Remote address may be empty if there is a sudden network change. |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 139 | RTC_DCHECK(remote_address.IsNil()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | socket_->SetError(ENOTCONN); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | int AsyncTCPSocketBase::SendRaw(const void * pv, size_t cb) { |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 145 | if (outbuf_.size() + cb > max_outsize_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | socket_->SetError(EMSGSIZE); |
| 147 | return -1; |
| 148 | } |
| 149 | |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 150 | RTC_DCHECK(!listen_); |
| 151 | outbuf_.AppendData(static_cast<const uint8_t*>(pv), cb); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | |
| 153 | return FlushOutBuffer(); |
| 154 | } |
| 155 | |
| 156 | int AsyncTCPSocketBase::FlushOutBuffer() { |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 157 | RTC_DCHECK(!listen_); |
| 158 | int res = socket_->Send(outbuf_.data(), outbuf_.size()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | if (res <= 0) { |
| 160 | return res; |
| 161 | } |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 162 | if (static_cast<size_t>(res) > outbuf_.size()) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 163 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | return -1; |
| 165 | } |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 166 | size_t new_size = outbuf_.size() - res; |
| 167 | if (new_size > 0) { |
| 168 | memmove(outbuf_.data(), outbuf_.data() + res, new_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 169 | } |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 170 | outbuf_.SetSize(new_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | return res; |
| 172 | } |
| 173 | |
| 174 | void AsyncTCPSocketBase::AppendToOutBuffer(const void* pv, size_t cb) { |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 175 | RTC_DCHECK(outbuf_.size() + cb <= max_outsize_); |
| 176 | RTC_DCHECK(!listen_); |
| 177 | outbuf_.AppendData(static_cast<const uint8_t*>(pv), cb); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void AsyncTCPSocketBase::OnConnectEvent(AsyncSocket* socket) { |
| 181 | SignalConnect(this); |
| 182 | } |
| 183 | |
| 184 | void AsyncTCPSocketBase::OnReadEvent(AsyncSocket* socket) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 185 | RTC_DCHECK(socket_.get() == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 186 | |
| 187 | if (listen_) { |
| 188 | rtc::SocketAddress address; |
| 189 | rtc::AsyncSocket* new_socket = socket->Accept(&address); |
| 190 | if (!new_socket) { |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 191 | // TODO(stefan): Do something better like forwarding the error |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | // to the user. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 193 | RTC_LOG(LS_ERROR) << "TCP accept failed with error " |
| 194 | << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | HandleIncomingConnection(new_socket); |
| 199 | |
| 200 | // Prime a read event in case data is waiting. |
| 201 | new_socket->SignalReadEvent(new_socket); |
| 202 | } else { |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 203 | size_t total_recv = 0; |
| 204 | while (true) { |
| 205 | size_t free_size = inbuf_.capacity() - inbuf_.size(); |
| 206 | if (free_size < kMinimumRecvSize && inbuf_.capacity() < max_insize_) { |
| 207 | inbuf_.EnsureCapacity(std::min(max_insize_, inbuf_.capacity() * 2)); |
| 208 | free_size = inbuf_.capacity() - inbuf_.size(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 209 | } |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 210 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 211 | int len = |
| 212 | socket_->Recv(inbuf_.data() + inbuf_.size(), free_size, nullptr); |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 213 | if (len < 0) { |
| 214 | // TODO(stefan): Do something better like forwarding the error to the |
| 215 | // user. |
| 216 | if (!socket_->IsBlocking()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 217 | RTC_LOG(LS_ERROR) << "Recv() returned error: " << socket_->GetError(); |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | total_recv += len; |
| 223 | inbuf_.SetSize(inbuf_.size() + len); |
| 224 | if (!len || static_cast<size_t>(len) < free_size) { |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (!total_recv) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 233 | size_t size = inbuf_.size(); |
| 234 | ProcessInput(inbuf_.data<char>(), &size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 236 | if (size > inbuf_.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 237 | RTC_LOG(LS_ERROR) << "input buffer overflow"; |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 238 | RTC_NOTREACHED(); |
jbauch | 313afba | 2016-03-03 03:41:05 -0800 | [diff] [blame] | 239 | inbuf_.Clear(); |
| 240 | } else { |
| 241 | inbuf_.SetSize(size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void AsyncTCPSocketBase::OnWriteEvent(AsyncSocket* socket) { |
jbauch | 3c16576 | 2016-02-26 09:31:32 -0800 | [diff] [blame] | 247 | RTC_DCHECK(socket_.get() == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 249 | if (outbuf_.size() > 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 250 | FlushOutBuffer(); |
| 251 | } |
| 252 | |
jbauch | 250fc65 | 2016-02-28 15:06:40 -0800 | [diff] [blame] | 253 | if (outbuf_.size() == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | SignalReadyToSend(this); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void AsyncTCPSocketBase::OnCloseEvent(AsyncSocket* socket, int error) { |
| 259 | SignalClose(this, error); |
| 260 | } |
| 261 | |
| 262 | // AsyncTCPSocket |
| 263 | // Binds and connects |socket| and creates AsyncTCPSocket for |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 264 | // it. Takes ownership of |socket|. Returns null if bind() or |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | // connect() fail (|socket| is destroyed in that case). |
| 266 | AsyncTCPSocket* AsyncTCPSocket::Create( |
| 267 | AsyncSocket* socket, |
| 268 | const SocketAddress& bind_address, |
| 269 | const SocketAddress& remote_address) { |
| 270 | return new AsyncTCPSocket(AsyncTCPSocketBase::ConnectSocket( |
| 271 | socket, bind_address, remote_address), false); |
| 272 | } |
| 273 | |
| 274 | AsyncTCPSocket::AsyncTCPSocket(AsyncSocket* socket, bool listen) |
| 275 | : AsyncTCPSocketBase(socket, listen, kBufSize) { |
| 276 | } |
| 277 | |
| 278 | int AsyncTCPSocket::Send(const void *pv, size_t cb, |
| 279 | const rtc::PacketOptions& options) { |
| 280 | if (cb > kBufSize) { |
| 281 | SetError(EMSGSIZE); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | // If we are blocking on send, then silently drop this packet |
| 286 | if (!IsOutBufferEmpty()) |
| 287 | return static_cast<int>(cb); |
| 288 | |
| 289 | PacketLength pkt_len = HostToNetwork16(static_cast<PacketLength>(cb)); |
| 290 | AppendToOutBuffer(&pkt_len, kPacketLenSize); |
| 291 | AppendToOutBuffer(pv, cb); |
| 292 | |
| 293 | int res = FlushOutBuffer(); |
| 294 | if (res <= 0) { |
| 295 | // drop packet if we made no progress |
| 296 | ClearOutBuffer(); |
| 297 | return res; |
| 298 | } |
| 299 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 300 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 301 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-16 18:22:31 -0700 | [diff] [blame] | 302 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 303 | SignalSentPacket(this, sent_packet); |
| 304 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 305 | // We claim to have sent the whole thing, even if we only sent partial |
| 306 | return static_cast<int>(cb); |
| 307 | } |
| 308 | |
| 309 | void AsyncTCPSocket::ProcessInput(char * data, size_t* len) { |
| 310 | SocketAddress remote_addr(GetRemoteAddress()); |
| 311 | |
| 312 | while (true) { |
| 313 | if (*len < kPacketLenSize) |
| 314 | return; |
| 315 | |
| 316 | PacketLength pkt_len = rtc::GetBE16(data); |
| 317 | if (*len < kPacketLenSize + pkt_len) |
| 318 | return; |
| 319 | |
| 320 | SignalReadPacket(this, data + kPacketLenSize, pkt_len, remote_addr, |
| 321 | CreatePacketTime(0)); |
| 322 | |
| 323 | *len -= kPacketLenSize + pkt_len; |
| 324 | if (*len > 0) { |
| 325 | memmove(data, data + kPacketLenSize + pkt_len, *len); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void AsyncTCPSocket::HandleIncomingConnection(AsyncSocket* socket) { |
| 331 | SignalNewConnection(this, new AsyncTCPSocket(socket, false)); |
| 332 | } |
| 333 | |
| 334 | } // namespace rtc |