]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/socks++4.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // socks++int - internal Socks implementation
28 namespace IPPlusPlus
{
34 // Socks4 Protocol implementation
36 void Server::connect(SocksClientSocket
&me
, const IPSockAddress
&peer
)
38 me
.Socket::open(SOCK_STREAM
);
39 me
.Socket::connect(mServerAddress
);
40 Message
request(socksConnect
, peer
);
41 request
.send(me
, "nobody");
42 (Message(me
)); // read and check reply message
43 me
.mPeerAddress
= peer
; // best guess, Mr. Sulu
44 debug("socks", "%d socks4 connected to %s", me
.fd(), string(peer
).c_str());
47 void Server::connect(SocksClientSocket
&me
, const Host
&host
, IPPort port
)
49 // Socks4 has no name resolution support. Do it here
50 //@@@ error reporting sucks here
51 set
<IPAddress
> addrs
= host
.addresses();
52 for (set
<IPAddress
>::const_iterator it
= addrs
.begin(); it
!= addrs
.end(); it
++) {
54 IPSockAddress
addr(*it
, port
);
57 } catch (const UnixError
&err
) {
66 void Server::bind(SocksServerSocket
&me
, const IPAddress
&peer
, IPPort port
)
68 me
.Socket::open(SOCK_STREAM
);
69 me
.Socket::connect(mServerAddress
);
70 Message
request(socksBind
, IPSockAddress(peer
, port
));
71 request
.send(me
, "nobody");
73 me
.mLocalAddress
= reply
.address().defaults(mServerAddress
.address());
74 debug("socks", "%d socks4 bound to %s", me
.fd(), string(me
.mLocalAddress
).c_str());
77 void Server::receive(SocksServerSocket
&me
, SocksClientSocket
&receiver
)
80 receiver
.setFd(me
.fd(), me
.mLocalAddress
, reply
.address());
81 me
.clear(); // clear our own (don't close on destruction)
82 debug("socks", "%d socks4 inbound connect", receiver
.fd());
89 Message::Message(Command cmd
, const IPSockAddress
&address
)
90 : version(4), message(cmd
), port(htons(address
.port())), addr(address
.address())
95 void Message::send(Socket
&s
, const char *userid
)
97 if (s
.write(this, sizeof(*this)) != sizeof(*this))
99 // now append zero-terminated userid (what a crock)
100 size_t length
= strlen(userid
) + 1;
101 if (s
.write(userid
, length
) != length
) {
103 UnixError::throwMe();
107 Message::Message(Socket
&s
)
109 if (s
.read(this, sizeof(*this)) != sizeof(*this)) {
111 UnixError::throwMe();
115 UnixError::throwMe(EPROTONOSUPPORT
);
118 case requestAccepted
:
121 UnixError::throwMe(ECONNREFUSED
); //@@@ hardly any diagnostics here
126 } // end namespace Socks
127 } // end namespace IPPlusPlus
128 } // end namespace Security