]>
Commit | Line | Data |
---|---|---|
bac41a7b A |
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++int - internal Socks implementation | |
21 | // | |
22 | #include "socks++5.h" | |
23 | #include "hosts.h" | |
24 | ||
25 | ||
26 | namespace Security { | |
27 | namespace IPPlusPlus { | |
28 | namespace Socks5 { | |
29 | ||
30 | ||
31 | // | |
32 | // Socks5 Protocol implementation | |
33 | // | |
34 | void Server::open(Socket &s, Support &my) | |
35 | { | |
36 | s.open(SOCK_STREAM); | |
37 | s.connect(my.mServer->address()); | |
df0e469f | 38 | secdebug("socks", "%d connected to server %s", s.fd(), string(my.mServer->address()).c_str()); |
bac41a7b A |
39 | Byte request[] = { 5, 1, socksAuthPublic }; |
40 | s.write(request, sizeof(request)); | |
41 | Byte reply[2]; | |
42 | s.read(reply, sizeof(reply)); | |
43 | if (reply[0] != 5 || reply[1] != socksAuthPublic) { | |
df0e469f | 44 | secdebug("socks", "%d server failed (v%d auth=%d)", s.fd(), reply[0], reply[1]); |
bac41a7b A |
45 | s.close(); |
46 | UnixError::throwMe(EPROTONOSUPPORT); | |
47 | } | |
48 | } | |
49 | ||
50 | void Server::connect(SocksClientSocket &me, const IPSockAddress &peer) | |
51 | { | |
52 | open(me, me); | |
53 | Message request(socksConnect, peer.address(), peer.port()); | |
54 | request.send(me); | |
55 | Message reply(me); | |
56 | me.mLocalAddress = reply.address(); | |
57 | me.mPeerAddress = peer; | |
df0e469f | 58 | secdebug("socks", "%d socks connected to %s", me.fd(), string(peer).c_str()); |
bac41a7b A |
59 | } |
60 | ||
61 | void Server::connect(SocksClientSocket &me, const Host &host, IPPort port) | |
62 | { | |
63 | #if 1 | |
64 | //@@@ should be using Hostname (server resolution) mode, but this won't get us | |
65 | //@@@ any useful peer address to use for bind relaying. Need to rethink this scenario. | |
66 | set<IPAddress> addrs = host.addresses(); | |
67 | for (set<IPAddress>::const_iterator it = addrs.begin(); it != addrs.end(); it++) { | |
68 | try { | |
69 | IPSockAddress addr(*it, port); | |
70 | connect(me, addr); | |
71 | return; | |
29654253 | 72 | } catch (const UnixError &err) { |
bac41a7b A |
73 | errno = err.error; |
74 | } | |
75 | } | |
76 | // exhausted | |
77 | UnixError::throwMe(); | |
78 | #else | |
79 | open(me, me); | |
80 | Message request(socksConnect, host.name().c_str(), port); | |
81 | request.send(me); | |
82 | Message reply(me); | |
83 | me.mLocalAddress = reply.address(); | |
84 | //me.mPeerAddress = not provided by Socks5 protocol; | |
df0e469f | 85 | secdebug("socks", "%d socks connected to %s", me.fd(), host.name().c_str()); |
bac41a7b A |
86 | #endif |
87 | } | |
88 | ||
89 | ||
90 | void Server::bind(SocksServerSocket &me, const IPAddress &peer, IPPort port) | |
91 | { | |
92 | open(me, me); | |
93 | Message request(socksBind, peer, port); | |
94 | request.send(me); | |
95 | Message reply(me); | |
96 | me.mLocalAddress = reply.address(); | |
97 | //me.mPeerAddress not available yet; | |
df0e469f | 98 | secdebug("socks", "%d socks bound to %s", me.fd(), string(me.mLocalAddress).c_str()); |
bac41a7b A |
99 | } |
100 | ||
101 | void Server::receive(SocksServerSocket &me, SocksClientSocket &receiver) | |
102 | { | |
103 | Message reply(me); | |
104 | receiver.setFd(me.fd(), me.mLocalAddress, reply.address()); | |
105 | me.clear(); // clear our own (don't close on destruction) | |
df0e469f | 106 | secdebug("socks", "%d socks received from %s", receiver.fd(), string(reply.address()).c_str()); |
bac41a7b A |
107 | } |
108 | ||
109 | ||
110 | // | |
111 | // Construct a request from an IPv4 address and port | |
112 | // | |
113 | Message::Message(Command cmd, IPAddress addr, IPPort port) | |
114 | { | |
115 | version = 5; | |
116 | message = cmd; | |
117 | reserved = 0; | |
118 | addressType = socksIPv4; | |
119 | this->addr = addr; | |
120 | this->port = htons(port); | |
121 | length = 4 + sizeof(this->addr) + sizeof(this->port); | |
122 | } | |
123 | ||
124 | ||
125 | // | |
126 | // Construct a request from a hostname and port (server resolves name) | |
127 | // | |
128 | Message::Message(Command cmd, const char *hostname, IPPort port) | |
129 | { | |
130 | version = 5; | |
131 | message = cmd; | |
132 | reserved = 0; | |
133 | addressType = socksName; | |
134 | ||
135 | size_t nameLength = strlen(hostname); | |
136 | if (nameLength > 255) | |
137 | UnixError::throwMe(ENAMETOOLONG); | |
138 | char *addrp = reinterpret_cast<char *>(&addr); | |
139 | addrp[0] = nameLength; | |
140 | memcpy(addrp + 1, hostname, nameLength); | |
141 | IPPort nboPort = htons(port); | |
142 | memcpy(addrp + 1 + nameLength, &nboPort, sizeof(nboPort)); | |
143 | length = 4 + 1 + nameLength + sizeof(nboPort); | |
144 | } | |
145 | ||
146 | ||
147 | // | |
148 | // Send a completed request message | |
149 | // | |
150 | void Message::send(Socket &s) | |
151 | { | |
152 | if (s.write(this, length) != length) { | |
153 | s.close(); | |
154 | UnixError::throwMe(EIO); | |
155 | } | |
156 | } | |
157 | ||
158 | ||
159 | // | |
160 | // Construct a reply object from a socket source. | |
161 | // Throws exceptions if the reply is not successful and supported. | |
162 | // | |
163 | Message::Message(Socket &socket) | |
164 | { | |
165 | length = 4 + sizeof(addr) + sizeof(port); //@@@ calculate if addrType != 1 supported | |
166 | ||
167 | if (socket.read(this, length) != length) { | |
168 | socket.close(); | |
169 | UnixError::throwMe(EIO); | |
170 | } | |
171 | ||
172 | // check error code | |
173 | switch (message) { | |
174 | case socksSuccess: | |
175 | break; | |
176 | case socksDenied: | |
177 | UnixError::throwMe(EPERM); | |
178 | case socksNetUnreach: | |
179 | UnixError::throwMe(ENETUNREACH); | |
180 | case socksHostUnreach: | |
181 | UnixError::throwMe(EHOSTUNREACH); | |
182 | case socksConRefused: | |
183 | UnixError::throwMe(ECONNREFUSED); | |
184 | case socksTTLExpired: | |
185 | UnixError::throwMe(ETIMEDOUT); // not really, but what's better? | |
186 | case socksUnsupported: | |
187 | UnixError::throwMe(EOPNOTSUPP); | |
188 | case socksAddressNotSupported: | |
189 | UnixError::throwMe(EADDRNOTAVAIL); | |
190 | default: | |
191 | UnixError::throwMe(EIO); // what else? :-) | |
192 | } | |
193 | ||
194 | // can't deal with non-IPv4 address replies | |
195 | if (addressType != socksIPv4 || reserved != 0) | |
196 | UnixError::throwMe(ENOTSUP); | |
197 | } | |
198 | ||
199 | ||
200 | } // end namespace Socks | |
201 | } // end namespace IPPlusPlus | |
202 | } // end namespace Security |