deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "p2p/base/udptransport.h" |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 12 | |
| 13 | #include <string> |
| 14 | #include <utility> // For std::move. |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/asyncpacketsocket.h" |
| 17 | #include "rtc_base/asyncudpsocket.h" |
| 18 | #include "rtc_base/logging.h" |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 19 | #include "rtc_base/nethelper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/socketaddress.h" |
| 21 | #include "rtc_base/thread.h" |
| 22 | #include "rtc_base/thread_checker.h" |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 23 | |
| 24 | namespace cricket { |
| 25 | |
| 26 | UdpTransport::UdpTransport(const std::string& transport_name, |
| 27 | std::unique_ptr<rtc::AsyncPacketSocket> socket) |
| 28 | : transport_name_(transport_name), socket_(std::move(socket)) { |
| 29 | RTC_DCHECK(socket_); |
| 30 | socket_->SignalReadPacket.connect(this, &UdpTransport::OnSocketReadPacket); |
| 31 | socket_->SignalSentPacket.connect(this, &UdpTransport::OnSocketSentPacket); |
| 32 | } |
| 33 | |
| 34 | UdpTransport::~UdpTransport() { |
| 35 | RTC_DCHECK_RUN_ON(&network_thread_checker_); |
| 36 | } |
| 37 | |
| 38 | rtc::SocketAddress UdpTransport::GetLocalAddress() const { |
| 39 | RTC_DCHECK_RUN_ON(&network_thread_checker_); |
| 40 | return socket_->GetLocalAddress(); |
| 41 | } |
| 42 | |
| 43 | bool UdpTransport::SetRemoteAddress(const rtc::SocketAddress& addr) { |
| 44 | RTC_DCHECK_RUN_ON(&network_thread_checker_); |
| 45 | if (!addr.IsComplete()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 46 | RTC_LOG(LS_WARNING) << "Remote address not complete."; |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | // TODO(johan): check for ipv4, other settings. |
| 50 | bool prev_destination_nil = remote_address_.IsNil(); |
| 51 | remote_address_ = addr; |
| 52 | // Going from "didn't have destination" to "have destination" or vice versa. |
| 53 | if (prev_destination_nil != remote_address_.IsNil()) { |
| 54 | SignalWritableState(this); |
| 55 | if (prev_destination_nil) { |
| 56 | SignalReadyToSend(this); |
| 57 | } |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | rtc::SocketAddress UdpTransport::GetRemoteAddress() const { |
| 63 | RTC_DCHECK_RUN_ON(&network_thread_checker_); |
| 64 | return remote_address_; |
| 65 | } |
| 66 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 67 | const std::string& UdpTransport::transport_name() const { |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 68 | return transport_name_; |
| 69 | } |
| 70 | |
| 71 | bool UdpTransport::receiving() const { |
| 72 | // TODO(johan): Implement method and signal. |
| 73 | return true; |
| 74 | } |
| 75 | |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 76 | bool UdpTransport::writable() const { |
| 77 | RTC_DCHECK_RUN_ON(&network_thread_checker_); |
| 78 | return !remote_address_.IsNil(); |
| 79 | } |
| 80 | |
| 81 | int UdpTransport::SendPacket(const char* data, |
| 82 | size_t len, |
| 83 | const rtc::PacketOptions& options, |
| 84 | int flags) { |
| 85 | // No thread_checker in high frequency network function. |
| 86 | if (remote_address_.IsNil()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 87 | RTC_LOG(LS_WARNING) << "Remote address not set."; |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 88 | send_error_ = ENOTCONN; |
| 89 | return -1; |
| 90 | } |
| 91 | int result = |
| 92 | socket_->SendTo((const void*)data, len, remote_address_, options); |
| 93 | if (result <= 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 94 | RTC_LOG(LS_VERBOSE) << "SendPacket() " << result; |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 99 | absl::optional<rtc::NetworkRoute> UdpTransport::network_route() const { |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 100 | rtc::NetworkRoute network_route; |
| 101 | network_route.packet_overhead = |
| 102 | /*kUdpOverhead=*/8 + GetIpOverhead(GetLocalAddress().family()); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 103 | return absl::optional<rtc::NetworkRoute>(network_route); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Steve Anton | 33f69db | 2017-10-30 10:01:15 -0700 | [diff] [blame] | 106 | int UdpTransport::SetOption(rtc::Socket::Option opt, int value) { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int UdpTransport::GetError() { |
| 111 | return send_error_; |
| 112 | } |
| 113 | |
| 114 | rtc::PacketTransportInternal* UdpTransport::GetInternal() { |
| 115 | return this; |
| 116 | } |
| 117 | |
deadbeef | d1c0998 | 2017-01-18 15:16:37 -0800 | [diff] [blame] | 118 | void UdpTransport::OnSocketReadPacket(rtc::AsyncPacketSocket* socket, |
| 119 | const char* data, |
| 120 | size_t len, |
| 121 | const rtc::SocketAddress& remote_addr, |
| 122 | const rtc::PacketTime& packet_time) { |
| 123 | // No thread_checker in high frequency network function. |
| 124 | SignalReadPacket(this, data, len, packet_time, 0); |
| 125 | } |
| 126 | |
| 127 | void UdpTransport::OnSocketSentPacket(rtc::AsyncPacketSocket* socket, |
| 128 | const rtc::SentPacket& packet) { |
| 129 | RTC_DCHECK_EQ(socket_.get(), socket); |
| 130 | SignalSentPacket(this, packet); |
| 131 | } |
| 132 | |
| 133 | } // namespace cricket |