mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 22:00:47 +01:00

WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
155 lines
5 KiB
C++
155 lines
5 KiB
C++
/*
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "rtc_base/proxy_server.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <memory>
|
|
#include "rtc_base/checks.h"
|
|
#include "rtc_base/logging.h"
|
|
#include "rtc_base/socket_factory.h"
|
|
|
|
namespace rtc {
|
|
|
|
// ProxyServer
|
|
ProxyServer::ProxyServer(SocketFactory* int_factory,
|
|
const SocketAddress& int_addr,
|
|
SocketFactory* ext_factory,
|
|
const SocketAddress& ext_ip)
|
|
: ext_factory_(ext_factory),
|
|
ext_ip_(ext_ip.ipaddr(), 0), // strip off port
|
|
server_socket_(
|
|
int_factory->CreateAsyncSocket(int_addr.family(), SOCK_STREAM)) {
|
|
RTC_DCHECK(server_socket_.get() != nullptr);
|
|
RTC_DCHECK(int_addr.family() == AF_INET || int_addr.family() == AF_INET6);
|
|
server_socket_->Bind(int_addr);
|
|
server_socket_->Listen(5);
|
|
server_socket_->SignalReadEvent.connect(this, &ProxyServer::OnAcceptEvent);
|
|
}
|
|
|
|
ProxyServer::~ProxyServer() = default;
|
|
|
|
SocketAddress ProxyServer::GetServerAddress() {
|
|
return server_socket_->GetLocalAddress();
|
|
}
|
|
|
|
void ProxyServer::OnAcceptEvent(AsyncSocket* socket) {
|
|
RTC_DCHECK(socket);
|
|
RTC_DCHECK_EQ(socket, server_socket_.get());
|
|
AsyncSocket* int_socket = socket->Accept(nullptr);
|
|
AsyncProxyServerSocket* wrapped_socket = WrapSocket(int_socket);
|
|
AsyncSocket* ext_socket =
|
|
ext_factory_->CreateAsyncSocket(ext_ip_.family(), SOCK_STREAM);
|
|
if (ext_socket) {
|
|
ext_socket->Bind(ext_ip_);
|
|
bindings_.emplace_back(
|
|
std::make_unique<ProxyBinding>(wrapped_socket, ext_socket));
|
|
} else {
|
|
RTC_LOG(LS_ERROR)
|
|
<< "Unable to create external socket on proxy accept event";
|
|
}
|
|
}
|
|
|
|
// ProxyBinding
|
|
ProxyBinding::ProxyBinding(AsyncProxyServerSocket* int_socket,
|
|
AsyncSocket* ext_socket)
|
|
: int_socket_(int_socket),
|
|
ext_socket_(ext_socket),
|
|
connected_(false),
|
|
out_buffer_(kBufferSize),
|
|
in_buffer_(kBufferSize) {
|
|
int_socket_->SignalConnectRequest.connect(this,
|
|
&ProxyBinding::OnConnectRequest);
|
|
int_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnInternalRead);
|
|
int_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnInternalWrite);
|
|
int_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnInternalClose);
|
|
ext_socket_->SignalConnectEvent.connect(this,
|
|
&ProxyBinding::OnExternalConnect);
|
|
ext_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnExternalRead);
|
|
ext_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnExternalWrite);
|
|
ext_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnExternalClose);
|
|
}
|
|
|
|
ProxyBinding::~ProxyBinding() = default;
|
|
|
|
void ProxyBinding::OnConnectRequest(AsyncProxyServerSocket* socket,
|
|
const SocketAddress& addr) {
|
|
RTC_DCHECK(!connected_);
|
|
RTC_DCHECK(ext_socket_);
|
|
ext_socket_->Connect(addr);
|
|
// TODO: handle errors here
|
|
}
|
|
|
|
void ProxyBinding::OnInternalRead(AsyncSocket* socket) {
|
|
Read(int_socket_.get(), &out_buffer_);
|
|
Write(ext_socket_.get(), &out_buffer_);
|
|
}
|
|
|
|
void ProxyBinding::OnInternalWrite(AsyncSocket* socket) {
|
|
Write(int_socket_.get(), &in_buffer_);
|
|
}
|
|
|
|
void ProxyBinding::OnInternalClose(AsyncSocket* socket, int err) {
|
|
Destroy();
|
|
}
|
|
|
|
void ProxyBinding::OnExternalConnect(AsyncSocket* socket) {
|
|
RTC_DCHECK(socket != nullptr);
|
|
connected_ = true;
|
|
int_socket_->SendConnectResult(0, socket->GetRemoteAddress());
|
|
}
|
|
|
|
void ProxyBinding::OnExternalRead(AsyncSocket* socket) {
|
|
Read(ext_socket_.get(), &in_buffer_);
|
|
Write(int_socket_.get(), &in_buffer_);
|
|
}
|
|
|
|
void ProxyBinding::OnExternalWrite(AsyncSocket* socket) {
|
|
Write(ext_socket_.get(), &out_buffer_);
|
|
}
|
|
|
|
void ProxyBinding::OnExternalClose(AsyncSocket* socket, int err) {
|
|
if (!connected_) {
|
|
int_socket_->SendConnectResult(err, SocketAddress());
|
|
}
|
|
Destroy();
|
|
}
|
|
|
|
void ProxyBinding::Read(AsyncSocket* socket, FifoBuffer* buffer) {
|
|
// Only read if the buffer is empty.
|
|
RTC_DCHECK(socket != nullptr);
|
|
size_t size;
|
|
int read;
|
|
if (buffer->GetBuffered(&size) && size == 0) {
|
|
void* p = buffer->GetWriteBuffer(&size);
|
|
read = socket->Recv(p, size, nullptr);
|
|
buffer->ConsumeWriteBuffer(std::max(read, 0));
|
|
}
|
|
}
|
|
|
|
void ProxyBinding::Write(AsyncSocket* socket, FifoBuffer* buffer) {
|
|
RTC_DCHECK(socket != nullptr);
|
|
size_t size;
|
|
int written;
|
|
const void* p = buffer->GetReadData(&size);
|
|
written = socket->Send(p, size);
|
|
buffer->ConsumeReadData(std::max(written, 0));
|
|
}
|
|
|
|
void ProxyBinding::Destroy() {
|
|
SignalDestroyed(this);
|
|
}
|
|
|
|
AsyncProxyServerSocket* SocksProxyServer::WrapSocket(AsyncSocket* socket) {
|
|
return new AsyncSocksProxyServerSocket(socket);
|
|
}
|
|
|
|
} // namespace rtc
|