]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/socks++.cpp
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / socks++.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // socks - socks version of IP sockets
21 //
22 // [Also see comments in header file.]
23 //
24 // This file contains the "generic" Socks functionality.
25 // Socks4 and Socks5 implementations are in their separate files.
26 //
27 #include "socks++.h"
28 #include "socks++4.h"
29 #include "socks++5.h"
30 #include "hosts.h"
31
32
33 namespace Security {
34 namespace IPPlusPlus {
35
36
37 //
38 // Static objects
39 //
40 ModuleNexus<SocksServer::Global> SocksServer::global;
41
42
43 //
44 // Create a SocksServer object
45 //
46 SocksServer *SocksServer::make(Version version, const IPSockAddress &addr)
47 {
48 switch (version) {
49 case 0:
50 return NULL; // no socks
51 case 4:
52 return new Socks4::Server(addr);
53 case 5:
54 return new Socks5::Server(addr);
55 default:
56 UnixError::throwMe(EINVAL);
57 }
58 }
59
60
61 //
62 // TCPClientSockets (CONNECT access)
63 //
64 void SocksClientSocket::open(const IPSockAddress &peer)
65 {
66 if (mServer) {
67 Support::connect(*this, peer);
68 lastConnected(mPeerAddress.address());
69 } else {
70 TCPClientSocket::open(peer);
71 }
72 }
73
74 void SocksClientSocket::open(const IPAddress &addr, IPPort port)
75 {
76 open(IPSockAddress(addr, port));
77 }
78
79 void SocksClientSocket::open(const Host &host, IPPort port)
80 {
81 if (mServer) {
82 Support::connect(*this, host, port);
83 lastConnected(mPeerAddress.address());
84 } else {
85 TCPClientSocket::open(host, port);
86 }
87 }
88
89 void SocksClientSocket::setFd(int fd, const IPSockAddress &local, const IPSockAddress &peer)
90 {
91 Socket::setFd(fd);
92 mLocalAddress = local;
93 mPeerAddress = peer;
94 }
95
96
97 //
98 // TCPServerSockets (BIND access)
99 //
100 void SocksServerSocket::open(const IPSockAddress &local, int)
101 {
102 if (mServer) {
103 #if BUG_GCC
104 if (mConnectionPeer)
105 Support::bind(*this, mConnectionPeer, local.port());
106 else
107 Support::bind(*this, lastConnected(), local.port());
108 #else
109 Support::bind(*this,
110 mConnectionPeer ? mConnectionPeer : lastConnected(),
111 local.port());
112 #endif
113 } else {
114 TCPServerSocket::open(local, 1);
115 }
116 }
117
118 void SocksServerSocket::receive(SocksClientSocket &client)
119 {
120 if (mServer) {
121 Support::receive(*this, client);
122 } else {
123 TCPServerSocket::receive(client);
124 }
125 }
126
127
128 //
129 // Address functions
130 //
131 IPSockAddress SocksServer::Support::localAddress(const Socket &me) const
132 {
133 if (mServer)
134 return mLocalAddress;
135 else
136 return me.localAddress();
137 }
138
139 IPSockAddress SocksServer::Support::peerAddress(const Socket &me) const
140 {
141 if (mServer)
142 return mPeerAddress;
143 else
144 return me.peerAddress();
145 }
146
147
148 } // end namespace IPPlusPlus
149 } // end namespace Security