henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/async_udp_socket.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
| 13 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "rtc_base/network/sent_packet.h" |
| 20 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/time_utils.h" |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 22 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
Per K | 7efd372 | 2023-03-21 13:10:44 | [diff] [blame] | 26 | // Returns true if the experiement "WebRTC-SCM-Timestamp" is explicitly |
| 27 | // disabled. |
| 28 | static bool IsScmTimeStampExperimentDisabled() { |
| 29 | return webrtc::field_trial::IsDisabled("WebRTC-SCM-Timestamp"); |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 30 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 31 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 32 | AsyncUDPSocket* AsyncUDPSocket::Create(Socket* socket, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 33 | const SocketAddress& bind_address) { |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 34 | std::unique_ptr<Socket> owned_socket(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 35 | if (socket->Bind(bind_address) < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 36 | RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError(); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 37 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 38 | } |
| 39 | return new AsyncUDPSocket(owned_socket.release()); |
| 40 | } |
| 41 | |
| 42 | AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory, |
| 43 | const SocketAddress& bind_address) { |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 44 | Socket* socket = factory->CreateSocket(bind_address.family(), SOCK_DGRAM); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 45 | if (!socket) |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 46 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 47 | return Create(socket, bind_address); |
| 48 | } |
| 49 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 50 | AsyncUDPSocket::AsyncUDPSocket(Socket* socket) : socket_(socket) { |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 51 | sequence_checker_.Detach(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 52 | // The socket should start out readable but not writable. |
| 53 | socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent); |
| 54 | socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent); |
| 55 | } |
| 56 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 57 | SocketAddress AsyncUDPSocket::GetLocalAddress() const { |
| 58 | return socket_->GetLocalAddress(); |
| 59 | } |
| 60 | |
| 61 | SocketAddress AsyncUDPSocket::GetRemoteAddress() const { |
| 62 | return socket_->GetRemoteAddress(); |
| 63 | } |
| 64 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 65 | int AsyncUDPSocket::Send(const void* pv, |
| 66 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 67 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-12 03:14:17 | [diff] [blame] | 68 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 69 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-17 01:22:31 | [diff] [blame] | 70 | CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 71 | int ret = socket_->Send(pv, cb); |
| 72 | SignalSentPacket(this, sent_packet); |
| 73 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 74 | } |
| 75 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 76 | int AsyncUDPSocket::SendTo(const void* pv, |
| 77 | size_t cb, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 78 | const SocketAddress& addr, |
| 79 | const rtc::PacketOptions& options) { |
Qingsi Wang | 6e641e6 | 2018-04-12 03:14:17 | [diff] [blame] | 80 | rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(), |
| 81 | options.info_signaled_after_sent); |
Qingsi Wang | 4ea53b3 | 2018-04-17 01:22:31 | [diff] [blame] | 82 | CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info); |
stefan | c1aeaf0 | 2015-10-15 14:26:07 | [diff] [blame] | 83 | int ret = socket_->SendTo(pv, cb, addr); |
| 84 | SignalSentPacket(this, sent_packet); |
| 85 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int AsyncUDPSocket::Close() { |
| 89 | return socket_->Close(); |
| 90 | } |
| 91 | |
| 92 | AsyncUDPSocket::State AsyncUDPSocket::GetState() const { |
| 93 | return STATE_BOUND; |
| 94 | } |
| 95 | |
| 96 | int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) { |
| 97 | return socket_->GetOption(opt, value); |
| 98 | } |
| 99 | |
| 100 | int AsyncUDPSocket::SetOption(Socket::Option opt, int value) { |
| 101 | return socket_->SetOption(opt, value); |
| 102 | } |
| 103 | |
| 104 | int AsyncUDPSocket::GetError() const { |
| 105 | return socket_->GetError(); |
| 106 | } |
| 107 | |
| 108 | void AsyncUDPSocket::SetError(int error) { |
| 109 | return socket_->SetError(error); |
| 110 | } |
| 111 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 112 | void AsyncUDPSocket::OnReadEvent(Socket* socket) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 113 | RTC_DCHECK(socket_.get() == socket); |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 114 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 115 | |
| 116 | SocketAddress remote_addr; |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 117 | int64_t timestamp = -1; |
| 118 | int len = socket_->RecvFrom(buf_, BUF_SIZE, &remote_addr, ×tamp); |
| 119 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 120 | if (len < 0) { |
| 121 | // An error here typically means we got an ICMP error in response to our |
| 122 | // send datagram, indicating the remote address was unreachable. |
| 123 | // When doing ICE, this kind of thing will often happen. |
| 124 | // TODO: Do something better like forwarding the error to the user. |
| 125 | SocketAddress local_addr = socket_->GetLocalAddress(); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 126 | RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString() |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 127 | << "] receive failed with error " << socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 128 | return; |
| 129 | } |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 130 | if (timestamp == -1) { |
| 131 | // Timestamp from socket is not available. |
| 132 | timestamp = TimeMicros(); |
| 133 | } else { |
| 134 | if (!socket_time_offset_) { |
| 135 | socket_time_offset_ = |
Per K | 7efd372 | 2023-03-21 13:10:44 | [diff] [blame] | 136 | !IsScmTimeStampExperimentDisabled() ? TimeMicros() - timestamp : 0; |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 137 | } |
| 138 | timestamp += *socket_time_offset_; |
| 139 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 140 | |
| 141 | // TODO: Make sure that we got all of the packet. |
| 142 | // If we did not, then we should resize our buffer to be large enough. |
Niels Möller | 15ca5a9 | 2018-11-01 13:32:47 | [diff] [blame] | 143 | SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr, |
Per Kjellander | 3daf696 | 2022-11-08 17:23:43 | [diff] [blame] | 144 | timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 145 | } |
| 146 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 147 | void AsyncUDPSocket::OnWriteEvent(Socket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 148 | SignalReadyToSend(this); |
| 149 | } |
| 150 | |
| 151 | } // namespace rtc |