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
11 #include "rtc_base/async_udp_socket.h"
12
13 #include <stdint.h>
14
15 #include <string>
16
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19 #include "rtc_base/network/sent_packet.h"
20 #include "rtc_base/third_party/sigslot/sigslot.h"
21 #include "rtc_base/time_utils.h"
22 #include "system_wrappers/include/field_trial.h"
23
24 namespace rtc {
25
26 // Returns true if the the client is in the experiment to get timestamps
27 // from the socket implementation.
IsScmTimeStampExperimentEnabled()28 static bool IsScmTimeStampExperimentEnabled() {
29 return webrtc::field_trial::IsEnabled("WebRTC-SCM-Timestamp");
30 }
31
Create(Socket * socket,const SocketAddress & bind_address)32 AsyncUDPSocket* AsyncUDPSocket::Create(Socket* socket,
33 const SocketAddress& bind_address) {
34 std::unique_ptr<Socket> owned_socket(socket);
35 if (socket->Bind(bind_address) < 0) {
36 RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
37 return nullptr;
38 }
39 return new AsyncUDPSocket(owned_socket.release());
40 }
41
Create(SocketFactory * factory,const SocketAddress & bind_address)42 AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
43 const SocketAddress& bind_address) {
44 Socket* socket = factory->CreateSocket(bind_address.family(), SOCK_DGRAM);
45 if (!socket)
46 return nullptr;
47 return Create(socket, bind_address);
48 }
49
AsyncUDPSocket(Socket * socket)50 AsyncUDPSocket::AsyncUDPSocket(Socket* socket) : socket_(socket) {
51 sequence_checker_.Detach();
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
GetLocalAddress() const57 SocketAddress AsyncUDPSocket::GetLocalAddress() const {
58 return socket_->GetLocalAddress();
59 }
60
GetRemoteAddress() const61 SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
62 return socket_->GetRemoteAddress();
63 }
64
Send(const void * pv,size_t cb,const rtc::PacketOptions & options)65 int AsyncUDPSocket::Send(const void* pv,
66 size_t cb,
67 const rtc::PacketOptions& options) {
68 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
69 options.info_signaled_after_sent);
70 CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info);
71 int ret = socket_->Send(pv, cb);
72 SignalSentPacket(this, sent_packet);
73 return ret;
74 }
75
SendTo(const void * pv,size_t cb,const SocketAddress & addr,const rtc::PacketOptions & options)76 int AsyncUDPSocket::SendTo(const void* pv,
77 size_t cb,
78 const SocketAddress& addr,
79 const rtc::PacketOptions& options) {
80 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
81 options.info_signaled_after_sent);
82 CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info);
83 int ret = socket_->SendTo(pv, cb, addr);
84 SignalSentPacket(this, sent_packet);
85 return ret;
86 }
87
Close()88 int AsyncUDPSocket::Close() {
89 return socket_->Close();
90 }
91
GetState() const92 AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
93 return STATE_BOUND;
94 }
95
GetOption(Socket::Option opt,int * value)96 int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
97 return socket_->GetOption(opt, value);
98 }
99
SetOption(Socket::Option opt,int value)100 int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
101 return socket_->SetOption(opt, value);
102 }
103
GetError() const104 int AsyncUDPSocket::GetError() const {
105 return socket_->GetError();
106 }
107
SetError(int error)108 void AsyncUDPSocket::SetError(int error) {
109 return socket_->SetError(error);
110 }
111
OnReadEvent(Socket * socket)112 void AsyncUDPSocket::OnReadEvent(Socket* socket) {
113 RTC_DCHECK(socket_.get() == socket);
114 RTC_DCHECK_RUN_ON(&sequence_checker_);
115
116 SocketAddress remote_addr;
117 int64_t timestamp = -1;
118 int len = socket_->RecvFrom(buf_, BUF_SIZE, &remote_addr, ×tamp);
119
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();
126 RTC_LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToSensitiveString()
127 << "] receive failed with error " << socket_->GetError();
128 return;
129 }
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_ =
136 IsScmTimeStampExperimentEnabled() ? TimeMicros() - timestamp : 0;
137 }
138 timestamp += *socket_time_offset_;
139 }
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.
143 SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr,
144 timestamp);
145 }
146
OnWriteEvent(Socket * socket)147 void AsyncUDPSocket::OnWriteEvent(Socket* socket) {
148 SignalReadyToSend(this);
149 }
150
151 } // namespace rtc
152