1/*
2 * Copyright (c) 2000-2001,2004,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// socks - socks version of IP sockets
27//
28// [Also see comments in header file.]
29//
30// This file contains the "generic" Socks functionality.
31// Socks4 and Socks5 implementations are in their separate files.
32//
33#include "socks++.h"
34#include "socks++4.h"
35#include "socks++5.h"
36#include "hosts.h"
37
38
39namespace Security {
40namespace IPPlusPlus {
41
42
43//
44// Static objects
45//
46ModuleNexus<SocksServer::Global> SocksServer::global;
47
48
49//
50// SocksServer destruction
51//
52SocksServer::~SocksServer()
53{ /* virtual */ }
54
55
56//
57// Create a SocksServer object
58//
59SocksServer *SocksServer::make(Version version, const IPSockAddress &addr)
60{
61    switch (version) {
62    case 0:
63        return NULL;		// no socks
64    case 4:
65        return new Socks4::Server(addr);
66    case 5:
67        return new Socks5::Server(addr);
68    default:
69        UnixError::throwMe(EINVAL);
70    }
71}
72
73
74//
75// TCPClientSockets (CONNECT access)
76//
77void SocksClientSocket::open(const IPSockAddress &peer)
78{
79    if (mServer) {
80        Support::connect(*this, peer);
81        lastConnected(mPeerAddress.address());
82    } else {
83        TCPClientSocket::open(peer);
84    }
85}
86
87void SocksClientSocket::open(const IPAddress &addr, IPPort port)
88{
89    open(IPSockAddress(addr, port));
90}
91
92void SocksClientSocket::open(const Host &host, IPPort port)
93{
94    if (mServer) {
95        Support::connect(*this, host, port);
96        lastConnected(mPeerAddress.address());
97    } else {
98        TCPClientSocket::open(host, port);
99    }
100}
101
102void SocksClientSocket::setFd(int fd, const IPSockAddress &local, const IPSockAddress &peer)
103{
104    Socket::setFd(fd);
105    mLocalAddress = local;
106    mPeerAddress = peer;
107}
108
109
110//
111// TCPServerSockets (BIND access)
112//
113void SocksServerSocket::open(const IPSockAddress &local, int)
114{
115    if (mServer) {
116#if BUG_GCC
117        if (mConnectionPeer)
118            Support::bind(*this, mConnectionPeer, local.port());
119        else
120            Support::bind(*this, lastConnected(), local.port());
121#else
122        Support::bind(*this,
123            mConnectionPeer ? mConnectionPeer : lastConnected(),
124            local.port());
125#endif
126    } else {
127        TCPServerSocket::open(local, 1);
128    }
129}
130
131void SocksServerSocket::receive(SocksClientSocket &client)
132{
133    if (mServer) {
134        Support::receive(*this, client);
135    } else {
136        TCPServerSocket::receive(client);
137    }
138}
139
140
141//
142// Address functions
143//
144IPSockAddress SocksServer::Support::localAddress(const Socket &me) const
145{
146    if (mServer)
147        return mLocalAddress;
148    else
149        return me.localAddress();
150}
151
152IPSockAddress SocksServer::Support::peerAddress(const Socket &me) const
153{
154    if (mServer)
155        return mPeerAddress;
156    else
157        return me.peerAddress();
158}
159
160
161}	// end namespace IPPlusPlus
162}	// end namespace Security
163