You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/proxyserver.h"
  28. #include <algorithm>
  29. #include "talk/base/socketfactory.h"
  30. namespace talk_base {
  31. // ProxyServer
  32. ProxyServer::ProxyServer(
  33. SocketFactory* int_factory, const SocketAddress& int_addr,
  34. SocketFactory* ext_factory, const SocketAddress& ext_ip)
  35. : ext_factory_(ext_factory), ext_ip_(ext_ip.ipaddr(), 0), // strip off port
  36. server_socket_(int_factory->CreateAsyncSocket(int_addr.family(),
  37. SOCK_STREAM)) {
  38. ASSERT(server_socket_.get() != NULL);
  39. ASSERT(int_addr.family() == AF_INET || int_addr.family() == AF_INET6);
  40. server_socket_->Bind(int_addr);
  41. server_socket_->Listen(5);
  42. server_socket_->SignalReadEvent.connect(this, &ProxyServer::OnAcceptEvent);
  43. }
  44. ProxyServer::~ProxyServer() {
  45. for (BindingList::iterator it = bindings_.begin();
  46. it != bindings_.end(); ++it) {
  47. delete (*it);
  48. }
  49. }
  50. void ProxyServer::OnAcceptEvent(AsyncSocket* socket) {
  51. ASSERT(socket != NULL && socket == server_socket_.get());
  52. AsyncSocket* int_socket = socket->Accept(NULL);
  53. AsyncProxyServerSocket* wrapped_socket = WrapSocket(int_socket);
  54. AsyncSocket* ext_socket = ext_factory_->CreateAsyncSocket(ext_ip_.family(),
  55. SOCK_STREAM);
  56. if (ext_socket) {
  57. ext_socket->Bind(ext_ip_);
  58. bindings_.push_back(new ProxyBinding(wrapped_socket, ext_socket));
  59. } else {
  60. LOG(LS_ERROR) << "Unable to create external socket on proxy accept event";
  61. }
  62. }
  63. void ProxyServer::OnBindingDestroyed(ProxyBinding* binding) {
  64. BindingList::iterator it =
  65. std::find(bindings_.begin(), bindings_.end(), binding);
  66. delete (*it);
  67. bindings_.erase(it);
  68. }
  69. // ProxyBinding
  70. ProxyBinding::ProxyBinding(AsyncProxyServerSocket* int_socket,
  71. AsyncSocket* ext_socket)
  72. : int_socket_(int_socket), ext_socket_(ext_socket), connected_(false),
  73. out_buffer_(kBufferSize), in_buffer_(kBufferSize) {
  74. int_socket_->SignalConnectRequest.connect(this,
  75. &ProxyBinding::OnConnectRequest);
  76. int_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnInternalRead);
  77. int_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnInternalWrite);
  78. int_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnInternalClose);
  79. ext_socket_->SignalConnectEvent.connect(this,
  80. &ProxyBinding::OnExternalConnect);
  81. ext_socket_->SignalReadEvent.connect(this, &ProxyBinding::OnExternalRead);
  82. ext_socket_->SignalWriteEvent.connect(this, &ProxyBinding::OnExternalWrite);
  83. ext_socket_->SignalCloseEvent.connect(this, &ProxyBinding::OnExternalClose);
  84. }
  85. void ProxyBinding::OnConnectRequest(AsyncProxyServerSocket* socket,
  86. const SocketAddress& addr) {
  87. ASSERT(!connected_ && ext_socket_.get() != NULL);
  88. ext_socket_->Connect(addr);
  89. // TODO: handle errors here
  90. }
  91. void ProxyBinding::OnInternalRead(AsyncSocket* socket) {
  92. Read(int_socket_.get(), &out_buffer_);
  93. Write(ext_socket_.get(), &out_buffer_);
  94. }
  95. void ProxyBinding::OnInternalWrite(AsyncSocket* socket) {
  96. Write(int_socket_.get(), &in_buffer_);
  97. }
  98. void ProxyBinding::OnInternalClose(AsyncSocket* socket, int err) {
  99. Destroy();
  100. }
  101. void ProxyBinding::OnExternalConnect(AsyncSocket* socket) {
  102. ASSERT(socket != NULL);
  103. connected_ = true;
  104. int_socket_->SendConnectResult(0, socket->GetRemoteAddress());
  105. }
  106. void ProxyBinding::OnExternalRead(AsyncSocket* socket) {
  107. Read(ext_socket_.get(), &in_buffer_);
  108. Write(int_socket_.get(), &in_buffer_);
  109. }
  110. void ProxyBinding::OnExternalWrite(AsyncSocket* socket) {
  111. Write(ext_socket_.get(), &out_buffer_);
  112. }
  113. void ProxyBinding::OnExternalClose(AsyncSocket* socket, int err) {
  114. if (!connected_) {
  115. int_socket_->SendConnectResult(err, SocketAddress());
  116. }
  117. Destroy();
  118. }
  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. void ProxyBinding::Write(AsyncSocket* socket, FifoBuffer* buffer) {
  131. ASSERT(socket != NULL);
  132. size_t size;
  133. int written;
  134. const void* p = buffer->GetReadData(&size);
  135. written = socket->Send(p, size);
  136. buffer->ConsumeReadData(_max(written, 0));
  137. }
  138. void ProxyBinding::Destroy() {
  139. SignalDestroyed(this);
  140. }
  141. } // namespace talk_base