henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
| 28 | #ifndef TALK_BASE_PROXYSERVER_H_ |
| 29 | #define TALK_BASE_PROXYSERVER_H_ |
| 30 | |
| 31 | #include <list> |
| 32 | #include "talk/base/asyncsocket.h" |
| 33 | #include "talk/base/socketadapters.h" |
| 34 | #include "talk/base/socketaddress.h" |
| 35 | #include "talk/base/stream.h" |
| 36 | |
| 37 | namespace talk_base { |
| 38 | |
| 39 | class SocketFactory; |
| 40 | |
| 41 | // ProxyServer is a base class that allows for easy construction of proxy |
| 42 | // servers. With its helper class ProxyBinding, it contains all the necessary |
| 43 | // logic for receiving and bridging connections. The specific client-server |
| 44 | // proxy protocol is implemented by an instance of the AsyncProxyServerSocket |
| 45 | // class; children of ProxyServer implement WrapSocket appropriately to return |
| 46 | // the correct protocol handler. |
| 47 | |
| 48 | class ProxyBinding : public sigslot::has_slots<> { |
| 49 | public: |
| 50 | ProxyBinding(AsyncProxyServerSocket* in_socket, AsyncSocket* out_socket); |
| 51 | sigslot::signal1<ProxyBinding*> SignalDestroyed; |
| 52 | |
| 53 | private: |
| 54 | void OnConnectRequest(AsyncProxyServerSocket* socket, |
| 55 | const SocketAddress& addr); |
| 56 | void OnInternalRead(AsyncSocket* socket); |
| 57 | void OnInternalWrite(AsyncSocket* socket); |
| 58 | void OnInternalClose(AsyncSocket* socket, int err); |
| 59 | void OnExternalConnect(AsyncSocket* socket); |
| 60 | void OnExternalRead(AsyncSocket* socket); |
| 61 | void OnExternalWrite(AsyncSocket* socket); |
| 62 | void OnExternalClose(AsyncSocket* socket, int err); |
| 63 | |
| 64 | static void Read(AsyncSocket* socket, FifoBuffer* buffer); |
| 65 | static void Write(AsyncSocket* socket, FifoBuffer* buffer); |
| 66 | void Destroy(); |
| 67 | |
| 68 | static const int kBufferSize = 4096; |
| 69 | scoped_ptr<AsyncProxyServerSocket> int_socket_; |
| 70 | scoped_ptr<AsyncSocket> ext_socket_; |
| 71 | bool connected_; |
| 72 | FifoBuffer out_buffer_; |
| 73 | FifoBuffer in_buffer_; |
| 74 | DISALLOW_EVIL_CONSTRUCTORS(ProxyBinding); |
| 75 | }; |
| 76 | |
| 77 | class ProxyServer : public sigslot::has_slots<> { |
| 78 | public: |
| 79 | ProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr, |
| 80 | SocketFactory* ext_factory, const SocketAddress& ext_ip); |
| 81 | virtual ~ProxyServer(); |
| 82 | |
| 83 | protected: |
| 84 | void OnAcceptEvent(AsyncSocket* socket); |
| 85 | virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0; |
| 86 | void OnBindingDestroyed(ProxyBinding* binding); |
| 87 | |
| 88 | private: |
| 89 | typedef std::list<ProxyBinding*> BindingList; |
| 90 | SocketFactory* ext_factory_; |
| 91 | SocketAddress ext_ip_; |
| 92 | scoped_ptr<AsyncSocket> server_socket_; |
| 93 | BindingList bindings_; |
| 94 | DISALLOW_EVIL_CONSTRUCTORS(ProxyServer); |
| 95 | }; |
| 96 | |
| 97 | // SocksProxyServer is a simple extension of ProxyServer to implement SOCKS. |
| 98 | class SocksProxyServer : public ProxyServer { |
| 99 | public: |
| 100 | SocksProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr, |
| 101 | SocketFactory* ext_factory, const SocketAddress& ext_ip) |
| 102 | : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) { |
| 103 | } |
| 104 | protected: |
| 105 | AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) { |
| 106 | return new AsyncSocksProxyServerSocket(socket); |
| 107 | } |
| 108 | DISALLOW_EVIL_CONSTRUCTORS(SocksProxyServer); |
| 109 | }; |
| 110 | |
| 111 | } // namespace talk_base |
| 112 | |
| 113 | #endif // TALK_BASE_PROXYSERVER_H_ |