2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // socks - socks version of IP sockets
28 // This Socks implementation replaces the TCP-functional layer of the socket interface
29 // (TCPClientSocket and TCPServerSocket), not the raw Socket layer. Remember what
30 // Socks was invented for -- it's NOT a generic socket abstraction layer, valiant efforts
31 // of the various -lsocks libraries nonwithstanding.
32 // Do note that these are not virtual overrides, but textual replacements.
34 // This implementation supports Socks versions 4 and 5, as well as direct (un-socksed) sockets.
35 // The choice is per socket object.
38 // SocksServer *server = SocksServer::make(version, IP-address);
39 // SocksServer::defaultServer(server); // for new sockets
40 // SocksClientSocket clientSocket(...);
41 // clientSocket.server(server); // for this socket
42 // SocksServerSocket serverSocket(...); // only supports .receive()
43 // Otherwise, Socks{Client,Server}Socket is functionally equivalent to {Client,Server}Socket.
44 // Sockets without a Server (explicit or by default) are direct.
46 // Minimum replacement strategy:
47 // #define TCPClientSocket SocksClientSocket
48 // #define TCPServerSocket SocksServerSocket
49 // SocksServer::defaultServer(SocksServer::make(...));
52 // There is no UDP Socks support.
53 // @@@ Nonblocking sockets may not work quite right.
55 #ifndef _H_SOCKSPLUSPLUS
56 #define _H_SOCKSPLUSPLUS
59 #include <security_utilities/threading.h>
60 #include <security_utilities/globalizer.h>
63 using namespace UnixPlusPlus
;
67 namespace IPPlusPlus
{
70 class SocksServerSocket
;
71 class SocksClientSocket
;
75 // A particular Socks server and version. Get one by calling SocksServer::make().
76 // You can express "no socks server" (direct connect) with a NULL pointer (or version==0).
80 class Support
; friend class Support
;
84 mutable Mutex lock
; // lock for mGlobalServerAddress
85 SocksServer
*mServer
; // global default server
86 ThreadNexus
<IPAddress
> lastConnected
; // last address connected to (for aux. bind)
88 Global() : mServer(NULL
) { }
90 void server(SocksServer
*srv
) { StLock
<Mutex
> _(lock
); mServer
= srv
; }
91 SocksServer
*server() const { StLock
<Mutex
> _(lock
); return mServer
; }
93 static ModuleNexus
<Global
> global
; // global state
96 typedef unsigned int Version
;
98 static SocksServer
*make(Version version
, const IPSockAddress
&addr
);
100 const IPSockAddress
&address() const { return mServerAddress
; }
101 Version
version() const { return mVersion
; }
104 static SocksServer
*defaultServer() { return global().server(); }
105 static void defaultServer(SocksServer
*server
) { global().server(server
); }
108 virtual ~SocksServer();
110 virtual void connect(SocksClientSocket
&me
, const IPSockAddress
&peer
) = 0;
111 virtual void connect(SocksClientSocket
&me
, const Host
&host
, IPPort port
) = 0;
112 virtual void bind(SocksServerSocket
&me
, const IPAddress
&peer
, IPPort port
) = 0;
113 virtual void receive(SocksServerSocket
&me
, SocksClientSocket
&receiver
) = 0;
115 SocksServer(Version v
, const IPSockAddress
&addr
) : mVersion(v
), mServerAddress(addr
) { }
119 IPSockAddress mServerAddress
;
124 SocksServer
*server() const { return mServer
; }
125 void server(SocksServer
*srv
) { mServer
= srv
; }
127 IPSockAddress
localAddress(const Socket
&me
) const;
128 IPSockAddress
peerAddress(const Socket
&me
) const;
131 Support() : mServer(defaultServer()) { }
133 void connect(SocksClientSocket
&me
, const IPSockAddress
&peer
)
134 { mServer
->connect(me
, peer
); }
135 void connect(SocksClientSocket
&me
, const Host
&host
, IPPort port
)
136 { mServer
->connect(me
, host
, port
); }
137 void bind(SocksServerSocket
&me
, const IPAddress
&peer
, IPPort port
)
138 { mServer
->bind(me
, peer
, port
); }
139 void receive(SocksServerSocket
&me
, SocksClientSocket
&receiver
)
140 { mServer
->receive(me
, receiver
); }
142 void lastConnected(IPAddress addr
) { global().lastConnected() = addr
; }
143 IPAddress
lastConnected() const { return global().lastConnected(); }
146 SocksServer
*mServer
; // server for this socket
147 IPSockAddress mLocalAddress
; // my own address, as reported by server
148 IPSockAddress mPeerAddress
; // peer address
154 // The Socks version of a TCPClientSocket
156 class SocksClientSocket
: public TCPClientSocket
, public SocksServer::Support
{
158 SocksClientSocket() { }
159 SocksClientSocket(const IPSockAddress
&peer
) { open(peer
); }
160 SocksClientSocket(const IPAddress
&addr
, IPPort port
) { open(addr
, port
); }
161 SocksClientSocket(const Host
&host
, IPPort port
) { open(host
, port
); }
163 void open(const IPSockAddress
&peer
);
164 void open(const IPAddress
&addr
, IPPort port
);
165 void open(const Host
&host
, IPPort port
);
167 IPSockAddress
localAddress() const { return Support::localAddress(*this); }
168 IPSockAddress
peerAddress() const { return Support::peerAddress(*this); }
171 void setFd(int fd
, const IPSockAddress
&local
, const IPSockAddress
&peer
);
176 // The Socks version of a TCPServerSocket.
177 // Note that this version only supports the receive() access method.
178 // By the nature of things, the queue-length argument is ignored (it's always 1).
180 // A note about setMainConnection: There is a structural problem
181 // with the Socks protocol. When a SocksServerSocket goes active,
182 // the protocol requires the IP address of the host the connection will be
183 // coming from. Typical Socks library layers simply assume that this will
184 // be the address of the last server connected to by another (TCP) socket.
185 // We do this heuristic too, but it's unreliable: it's a per-thread global, and will
186 // fail if you interleave multiple socks "sessions" in the same thread. For this
187 // case (or if you just want to be safe and explicit), you can call setMainConnection to
188 // explicitly link this socket to a TCPClientSocket whose peer we should use.
189 // Do note that this call does not exist in the plain (non-socks) socket layer.
191 class SocksServerSocket
: public TCPServerSocket
, public SocksServer::Support
{
193 SocksServerSocket() { }
194 SocksServerSocket(const IPSockAddress
&local
, int = 1) { open(local
); }
195 SocksServerSocket(IPPort port
, int = 1) { open(port
); }
197 void open(const IPSockAddress
&local
, int = 1);
198 void open(IPPort port
= 0, int = 1)
199 { open(IPSockAddress(IPAddress::any
, port
)); }
201 void receive(SocksClientSocket
&client
); // accept incoming and close listener
203 IPSockAddress
localAddress() const { return Support::localAddress(*this); }
204 IPSockAddress
peerAddress() const { return Support::peerAddress(*this); }
206 // this special call is not an overlay of TCPServerSocket - it exists only for Socks
207 void setMainConnection(TCPClientSocket
&main
)
208 { mConnectionPeer
= main
.peerAddress().address(); }
211 IPAddress mConnectionPeer
; // address to say we're peered with
214 void operator () (TCPClientSocket
&newClient
); // not supported by Socks
218 } // end namespace IPPlusPlus
219 } // end namespace Security
222 #endif //_H_IPPLUSPLUS