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 "webrtc/base/proxyserver.h"
12 
13 #include <algorithm>
14 #include "webrtc/base/socketfactory.h"
15 
16 namespace rtc {
17 
18 // ProxyServer
ProxyServer(SocketFactory * int_factory,const SocketAddress & int_addr,SocketFactory * ext_factory,const SocketAddress & ext_ip)19 ProxyServer::ProxyServer(
20     SocketFactory* int_factory, const SocketAddress& int_addr,
21     SocketFactory* ext_factory, const SocketAddress& ext_ip)
22     : ext_factory_(ext_factory), ext_ip_(ext_ip.ipaddr(), 0),  // strip off port
23       server_socket_(int_factory->CreateAsyncSocket(int_addr.family(),
24                                                     SOCK_STREAM)) {
25   ASSERT(server_socket_.get() != NULL);
26   ASSERT(int_addr.family() == AF_INET || int_addr.family() == AF_INET6);
27   server_socket_->Bind(int_addr);
28   server_socket_->Listen(5);
29   server_socket_->SignalReadEvent.connect(this, &ProxyServer::OnAcceptEvent);
30 }
31 
~ProxyServer()32 ProxyServer::~ProxyServer() {
33   for (BindingList::iterator it = bindings_.begin();
34        it != bindings_.end(); ++it) {
35     delete (*it);
36   }
37 }
38 
GetServerAddress()39 SocketAddress ProxyServer::GetServerAddress() {
40   return server_socket_->GetLocalAddress();
41 }
42 
OnAcceptEvent(AsyncSocket * socket)43 void ProxyServer::OnAcceptEvent(AsyncSocket* socket) {
44   ASSERT(socket != NULL && socket == server_socket_.get());
45   AsyncSocket* int_socket = socket->Accept(NULL);
46   AsyncProxyServerSocket* wrapped_socket = WrapSocket(int_socket);
47   AsyncSocket* ext_socket = ext_factory_->CreateAsyncSocket(ext_ip_.family(),
48                                                             SOCK_STREAM);
49   if (ext_socket) {
50     ext_socket->Bind(ext_ip_);
51     bindings_.push_back(new ProxyBinding(wrapped_socket, ext_socket));
52   } else {
53     LOG(LS_ERROR) << "Unable to create external socket on proxy accept event";
54   }
55 }
56 
OnBindingDestroyed(ProxyBinding * binding)57 void ProxyServer::OnBindingDestroyed(ProxyBinding* binding) {
58   BindingList::iterator it =
59       std::find(bindings_.begin(), bindings_.end(), binding);
60   delete (*it);
61   bindings_.erase(it);
62 }
63 
64 // ProxyBinding
ProxyBinding(AsyncProxyServerSocket * int_socket,AsyncSocket * ext_socket)65 ProxyBinding::ProxyBinding(AsyncProxyServerSocket* int_socket,
66                            AsyncSocket* ext_socket)
67     : int_socket_(int_socket), ext_socket_(ext_socket), connected_(false),
68       out_buffer_(kBufferSize), in_buffer_(kBufferSize) {
69   int_socket_->SignalConnectRequest.connect(this,
70                                             &ProxyBinding::OnConnectRequest);
71   int_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnInternalRead);
72   int_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnInternalWrite);
73   int_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnInternalClose);
74   ext_socket_->SignalConnectEvent.connect(this,
75                                           &ProxyBinding::OnExternalConnect);
76   ext_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnExternalRead);
77   ext_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnExternalWrite);
78   ext_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnExternalClose);
79 }
80 
81 ProxyBinding::~ProxyBinding() = default;
82 
OnConnectRequest(AsyncProxyServerSocket * socket,const SocketAddress & addr)83 void ProxyBinding::OnConnectRequest(AsyncProxyServerSocket* socket,
84                                    const SocketAddress& addr) {
85   ASSERT(!connected_ && ext_socket_.get() != NULL);
86   ext_socket_->Connect(addr);
87   // TODO: handle errors here
88 }
89 
OnInternalRead(AsyncSocket * socket)90 void ProxyBinding::OnInternalRead(AsyncSocket* socket) {
91   Read(int_socket_.get(), &out_buffer_);
92   Write(ext_socket_.get(), &out_buffer_);
93 }
94 
OnInternalWrite(AsyncSocket * socket)95 void ProxyBinding::OnInternalWrite(AsyncSocket* socket) {
96   Write(int_socket_.get(), &in_buffer_);
97 }
98 
OnInternalClose(AsyncSocket * socket,int err)99 void ProxyBinding::OnInternalClose(AsyncSocket* socket, int err) {
100   Destroy();
101 }
102 
OnExternalConnect(AsyncSocket * socket)103 void ProxyBinding::OnExternalConnect(AsyncSocket* socket) {
104   ASSERT(socket != NULL);
105   connected_ = true;
106   int_socket_->SendConnectResult(0, socket->GetRemoteAddress());
107 }
108 
OnExternalRead(AsyncSocket * socket)109 void ProxyBinding::OnExternalRead(AsyncSocket* socket) {
110   Read(ext_socket_.get(), &in_buffer_);
111   Write(int_socket_.get(), &in_buffer_);
112 }
113 
OnExternalWrite(AsyncSocket * socket)114 void ProxyBinding::OnExternalWrite(AsyncSocket* socket) {
115   Write(ext_socket_.get(), &out_buffer_);
116 }
117 
OnExternalClose(AsyncSocket * socket,int err)118 void ProxyBinding::OnExternalClose(AsyncSocket* socket, int err) {
119   if (!connected_) {
120     int_socket_->SendConnectResult(err, SocketAddress());
121   }
122   Destroy();
123 }
124 
Read(AsyncSocket * socket,FifoBuffer * buffer)125 void ProxyBinding::Read(AsyncSocket* socket, FifoBuffer* buffer) {
126   // Only read if the buffer is empty.
127   ASSERT(socket != NULL);
128   size_t size;
129   int read;
130   if (buffer->GetBuffered(&size) && size == 0) {
131     void* p = buffer->GetWriteBuffer(&size);
132     read = socket->Recv(p, size);
133     buffer->ConsumeWriteBuffer(std::max(read, 0));
134   }
135 }
136 
Write(AsyncSocket * socket,FifoBuffer * buffer)137 void ProxyBinding::Write(AsyncSocket* socket, FifoBuffer* buffer) {
138   ASSERT(socket != NULL);
139   size_t size;
140   int written;
141   const void* p = buffer->GetReadData(&size);
142   written = socket->Send(p, size);
143   buffer->ConsumeReadData(std::max(written, 0));
144 }
145 
Destroy()146 void ProxyBinding::Destroy() {
147   SignalDestroyed(this);
148 }
149 
WrapSocket(AsyncSocket * socket)150 AsyncProxyServerSocket* SocksProxyServer::WrapSocket(AsyncSocket* socket) {
151   return new AsyncSocksProxyServerSocket(socket);
152 }
153 
154 }  // namespace rtc
155