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
23 namespace rtc {
24
25 static const int BUF_SIZE = 64 * 1024;
26
Create(AsyncSocket * socket,const SocketAddress & bind_address)27 AsyncUDPSocket* AsyncUDPSocket::Create(AsyncSocket* socket,
28 const SocketAddress& bind_address) {
29 std::unique_ptr<AsyncSocket> owned_socket(socket);
30 if (socket->Bind(bind_address) < 0) {
31 RTC_LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
32 return nullptr;
33 }
34 return new AsyncUDPSocket(owned_socket.release());
35 }
36
Create(SocketFactory * factory,const SocketAddress & bind_address)37 AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
38 const SocketAddress& bind_address) {
39 AsyncSocket* socket =
40 factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM);
41 if (!socket)
42 return nullptr;
43 return Create(socket, bind_address);
44 }
45
AsyncUDPSocket(AsyncSocket * socket)46 AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket) : socket_(socket) {
47 size_ = BUF_SIZE;
48 buf_ = new char[size_];
49
50 // The socket should start out readable but not writable.
51 socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent);
52 socket_->SignalWriteEvent.connect(this, &AsyncUDPSocket::OnWriteEvent);
53 }
54
~AsyncUDPSocket()55 AsyncUDPSocket::~AsyncUDPSocket() {
56 delete[] buf_;
57 }
58
GetLocalAddress() const59 SocketAddress AsyncUDPSocket::GetLocalAddress() const {
60 return socket_->GetLocalAddress();
61 }
62
GetRemoteAddress() const63 SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
64 return socket_->GetRemoteAddress();
65 }
66
Send(const void * pv,size_t cb,const rtc::PacketOptions & options)67 int AsyncUDPSocket::Send(const void* pv,
68 size_t cb,
69 const rtc::PacketOptions& options) {
70 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
71 options.info_signaled_after_sent);
72 CopySocketInformationToPacketInfo(cb, *this, false, &sent_packet.info);
73 int ret = socket_->Send(pv, cb);
74 SignalSentPacket(this, sent_packet);
75 return ret;
76 }
77
SendTo(const void * pv,size_t cb,const SocketAddress & addr,const rtc::PacketOptions & options)78 int AsyncUDPSocket::SendTo(const void* pv,
79 size_t cb,
80 const SocketAddress& addr,
81 const rtc::PacketOptions& options) {
82 rtc::SentPacket sent_packet(options.packet_id, rtc::TimeMillis(),
83 options.info_signaled_after_sent);
84 CopySocketInformationToPacketInfo(cb, *this, true, &sent_packet.info);
85 int ret = socket_->SendTo(pv, cb, addr);
86 SignalSentPacket(this, sent_packet);
87 return ret;
88 }
89
Close()90 int AsyncUDPSocket::Close() {
91 return socket_->Close();
92 }
93
GetState() const94 AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
95 return STATE_BOUND;
96 }
97
GetOption(Socket::Option opt,int * value)98 int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
99 return socket_->GetOption(opt, value);
100 }
101
SetOption(Socket::Option opt,int value)102 int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
103 return socket_->SetOption(opt, value);
104 }
105
GetError() const106 int AsyncUDPSocket::GetError() const {
107 return socket_->GetError();
108 }
109
SetError(int error)110 void AsyncUDPSocket::SetError(int error) {
111 return socket_->SetError(error);
112 }
113
OnReadEvent(AsyncSocket * socket)114 void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
115 RTC_DCHECK(socket_.get() == socket);
116
117 SocketAddress remote_addr;
118 int64_t timestamp;
119 int len = socket_->RecvFrom(buf_, size_, &remote_addr, ×tamp);
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
131 // TODO: Make sure that we got all of the packet.
132 // If we did not, then we should resize our buffer to be large enough.
133 SignalReadPacket(this, buf_, static_cast<size_t>(len), remote_addr,
134 (timestamp > -1 ? timestamp : TimeMicros()));
135 }
136
OnWriteEvent(AsyncSocket * socket)137 void AsyncUDPSocket::OnWriteEvent(AsyncSocket* socket) {
138 SignalReadyToSend(this);
139 }
140
141 } // namespace rtc
142