3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_PROXYSERVER_H_
29 #define TALK_BASE_PROXYSERVER_H_
32 #include "talk/base/asyncsocket.h"
33 #include "talk/base/socketadapters.h"
34 #include "talk/base/socketaddress.h"
35 #include "talk/base/stream.h"
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.
48 class ProxyBinding : public sigslot::has_slots<> {
50 ProxyBinding(AsyncProxyServerSocket* in_socket, AsyncSocket* out_socket);
51 sigslot::signal1<ProxyBinding*> SignalDestroyed;
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);
64 static void Read(AsyncSocket* socket, FifoBuffer* buffer);
65 static void Write(AsyncSocket* socket, FifoBuffer* buffer);
68 static const int kBufferSize = 4096;
69 scoped_ptr<AsyncProxyServerSocket> int_socket_;
70 scoped_ptr<AsyncSocket> ext_socket_;
72 FifoBuffer out_buffer_;
73 FifoBuffer in_buffer_;
74 DISALLOW_EVIL_CONSTRUCTORS(ProxyBinding);
77 class ProxyServer : public sigslot::has_slots<> {
79 ProxyServer(SocketFactory* int_factory, const SocketAddress& int_addr,
80 SocketFactory* ext_factory, const SocketAddress& ext_ip);
81 virtual ~ProxyServer();
84 void OnAcceptEvent(AsyncSocket* socket);
85 virtual AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) = 0;
86 void OnBindingDestroyed(ProxyBinding* binding);
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);
97 // SocksProxyServer is a simple extension of ProxyServer to implement SOCKS.
98 class SocksProxyServer : public ProxyServer {
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) {
105 AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) {
106 return new AsyncSocksProxyServerSocket(socket);
108 DISALLOW_EVIL_CONSTRUCTORS(SocksProxyServer);
111 } // namespace talk_base
113 #endif // TALK_BASE_PROXYSERVER_H_