summaryrefslogtreecommitdiffstats
blob: e8a43f98c1d0cd50e3b343c50dcdae666df0aaf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (c) 2012-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

#include <Swiften/Base/boost_bsignals.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/NATPortMapping.h>

namespace Swift {
	class NetworkEnvironment;
	class NATTraverser;
	class NATTraversalGetPublicIPRequest;
	class NATTraversalForwardPortRequest;
	class NATTraversalRemovePortForwardingRequest;
	class SOCKS5BytestreamRegistry;
	class ConnectionServerFactory;
	class ConnectionServer;
	class SOCKS5BytestreamServer;
	class SOCKS5BytestreamServerResourceUser;
	class SOCKS5BytestreamServerPortForwardingUser;

	class SOCKS5BytestreamServerManager {
		friend class SOCKS5BytestreamServerResourceUser;
		friend class SOCKS5BytestreamServerPortForwardingUser;

		public:
			SOCKS5BytestreamServerManager(
					SOCKS5BytestreamRegistry* bytestreamRegistry,
					ConnectionServerFactory* connectionServerFactory, 
					NetworkEnvironment* networkEnvironment,
					NATTraverser* natTraverser);
			~SOCKS5BytestreamServerManager();

			boost::shared_ptr<SOCKS5BytestreamServerResourceUser> aquireResourceUser();
			boost::shared_ptr<SOCKS5BytestreamServerPortForwardingUser> aquirePortForwardingUser();

			void stop();
			
			std::vector<HostAddressPort> getHostAddressPorts() const;
			std::vector<HostAddressPort> getAssistedHostAddressPorts() const;

			SOCKS5BytestreamServer* getServer() const {
				return server;
			}

		private:
			bool isInitialized() const;
			void initialize();

			bool isPortForwardingReady() const;
			void setupPortForwarding();
			void removePortForwarding();

			void checkInitializeFinished();

			void handleGetPublicIPResult(boost::optional<HostAddress> address);
			void handleForwardPortResult(boost::optional<NATPortMapping> mapping);
			void handleUnforwardPortResult(boost::optional<bool> result);

			boost::signal<void (bool /* success */)> onInitialized;
			boost::signal<void (bool /* success */)> onPortForwardingSetup;

		private:
			friend class SOCKS5BytestreamServerInitializeRequest;
			SOCKS5BytestreamRegistry* bytestreamRegistry;
			ConnectionServerFactory* connectionServerFactory;
			NetworkEnvironment* networkEnvironment;
			NATTraverser* natTraverser;
			enum { Start, Initializing, Initialized } state; 
			SOCKS5BytestreamServer* server;
			boost::shared_ptr<ConnectionServer> connectionServer;
			int connectionServerPort;

			boost::shared_ptr<NATTraversalGetPublicIPRequest> getPublicIPRequest;
			boost::shared_ptr<NATTraversalForwardPortRequest> forwardPortRequest;
			boost::shared_ptr<NATTraversalRemovePortForwardingRequest> unforwardPortRequest;
			boost::optional<HostAddress> publicAddress;
			boost::optional<NATPortMapping> portMapping;
			bool attemptedPortMapping_;

			boost::weak_ptr<SOCKS5BytestreamServerResourceUser> s5bServerResourceUser_;
			boost::weak_ptr<SOCKS5BytestreamServerPortForwardingUser> s5bServerPortForwardingUser_;
	};
}