• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
OnAcceptEvent(AsyncSocket * socket)39 void ProxyServer::OnAcceptEvent(AsyncSocket* socket) {
40   ASSERT(socket != NULL && socket == server_socket_.get());
41   AsyncSocket* int_socket = socket->Accept(NULL);
42   AsyncProxyServerSocket* wrapped_socket = WrapSocket(int_socket);
43   AsyncSocket* ext_socket = ext_factory_->CreateAsyncSocket(ext_ip_.family(),
44                                                             SOCK_STREAM);
45   if (ext_socket) {
46     ext_socket->Bind(ext_ip_);
47     bindings_.push_back(new ProxyBinding(wrapped_socket, ext_socket));
48   } else {
49     LOG(LS_ERROR) << "Unable to create external socket on proxy accept event";
50   }
51 }
52 
OnBindingDestroyed(ProxyBinding * binding)53 void ProxyServer::OnBindingDestroyed(ProxyBinding* binding) {
54   BindingList::iterator it =
55       std::find(bindings_.begin(), bindings_.end(), binding);
56   delete (*it);
57   bindings_.erase(it);
58 }
59 
60 // ProxyBinding
ProxyBinding(AsyncProxyServerSocket * int_socket,AsyncSocket * ext_socket)61 ProxyBinding::ProxyBinding(AsyncProxyServerSocket* int_socket,
62                            AsyncSocket* ext_socket)
63     : int_socket_(int_socket), ext_socket_(ext_socket), connected_(false),
64       out_buffer_(kBufferSize), in_buffer_(kBufferSize) {
65   int_socket_->SignalConnectRequest.connect(this,
66                                             &ProxyBinding::OnConnectRequest);
67   int_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnInternalRead);
68   int_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnInternalWrite);
69   int_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnInternalClose);
70   ext_socket_->SignalConnectEvent.connect(this,
71                                           &ProxyBinding::OnExternalConnect);
72   ext_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnExternalRead);
73   ext_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnExternalWrite);
74   ext_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnExternalClose);
75 }
76 
OnConnectRequest(AsyncProxyServerSocket * socket,const SocketAddress & addr)77 void ProxyBinding::OnConnectRequest(AsyncProxyServerSocket* socket,
78                                    const SocketAddress& addr) {
79   ASSERT(!connected_ && ext_socket_.get() != NULL);
80   ext_socket_->Connect(addr);
81   // TODO: handle errors here
82 }
83 
OnInternalRead(AsyncSocket * socket)84 void ProxyBinding::OnInternalRead(AsyncSocket* socket) {
85   Read(int_socket_.get(), &out_buffer_);
86   Write(ext_socket_.get(), &out_buffer_);
87 }
88 
OnInternalWrite(AsyncSocket * socket)89 void ProxyBinding::OnInternalWrite(AsyncSocket* socket) {
90   Write(int_socket_.get(), &in_buffer_);
91 }
92 
OnInternalClose(AsyncSocket * socket,int err)93 void ProxyBinding::OnInternalClose(AsyncSocket* socket, int err) {
94   Destroy();
95 }
96 
OnExternalConnect(AsyncSocket * socket)97 void ProxyBinding::OnExternalConnect(AsyncSocket* socket) {
98   ASSERT(socket != NULL);
99   connected_ = true;
100   int_socket_->SendConnectResult(0, socket->GetRemoteAddress());
101 }
102 
OnExternalRead(AsyncSocket * socket)103 void ProxyBinding::OnExternalRead(AsyncSocket* socket) {
104   Read(ext_socket_.get(), &in_buffer_);
105   Write(int_socket_.get(), &in_buffer_);
106 }
107 
OnExternalWrite(AsyncSocket * socket)108 void ProxyBinding::OnExternalWrite(AsyncSocket* socket) {
109   Write(ext_socket_.get(), &out_buffer_);
110 }
111 
OnExternalClose(AsyncSocket * socket,int err)112 void ProxyBinding::OnExternalClose(AsyncSocket* socket, int err) {
113   if (!connected_) {
114     int_socket_->SendConnectResult(err, SocketAddress());
115   }
116   Destroy();
117 }
118 
Read(AsyncSocket * socket,FifoBuffer * buffer)119 void ProxyBinding::Read(AsyncSocket* socket, FifoBuffer* buffer) {
120   // Only read if the buffer is empty.
121   ASSERT(socket != NULL);
122   size_t size;
123   int read;
124   if (buffer->GetBuffered(&size) && size == 0) {
125     void* p = buffer->GetWriteBuffer(&size);
126     read = socket->Recv(p, size);
127     buffer->ConsumeWriteBuffer(_max(read, 0));
128   }
129 }
130 
Write(AsyncSocket * socket,FifoBuffer * buffer)131 void ProxyBinding::Write(AsyncSocket* socket, FifoBuffer* buffer) {
132   ASSERT(socket != NULL);
133   size_t size;
134   int written;
135   const void* p = buffer->GetReadData(&size);
136   written = socket->Send(p, size);
137   buffer->ConsumeReadData(_max(written, 0));
138 }
139 
Destroy()140 void ProxyBinding::Destroy() {
141   SignalDestroyed(this);
142 }
143 
144 }  // namespace rtc
145