summaryrefslogtreecommitdiffstats
blob: 1243d119c9a9680b2244882b2c7aabf7a48f51d7 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <boost/shared_ptr.hpp>
#include <boost/cstdint.hpp>

#include <Swiften/Base/API.h>
#include <Swiften/Base/IDGenerator.h>
#include <Swiften/Network/Timer.h>
#include <Swiften/Jingle/JingleSession.h>
#include <Swiften/Jingle/JingleContentID.h>
#include <Swiften/FileTransfer/IncomingFileTransfer.h>
#include <Swiften/FileTransfer/JingleTransport.h>
#include <Swiften/FileTransfer/JingleIncomingIBBTransport.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamClientSession.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamServerSession.h>
#include <Swiften/Elements/JingleContentPayload.h>
#include <Swiften/Elements/JingleFileTransferDescription.h>
#include <Swiften/Elements/JingleIBBTransportPayload.h>
#include <Swiften/Elements/JingleS5BTransportPayload.h>
#include <Swiften/Elements/S5BProxyRequest.h>
#include <Swiften/Elements/ErrorPayload.h>

namespace Swift {
	class IQRouter;
	class RemoteJingleTransportCandidateSelectorFactory;
	class LocalJingleTransportCandidateGeneratorFactory;
	class RemoteJingleTransportCandidateSelector;
	class LocalJingleTransportCandidateGenerator;
	class SOCKS5BytestreamRegistry;
	class SOCKS5BytestreamProxy;
	class IncrementalBytestreamHashCalculator;
	class CryptoProvider;

	class SWIFTEN_API IncomingJingleFileTransfer : public IncomingFileTransfer {
		public:
			typedef boost::shared_ptr<IncomingJingleFileTransfer> ref;
			enum State {
				Initial,
				CreatingInitialTransports,
				NegotiatingTransport,
				Transferring,
				WaitingForFallbackOrTerminate,
				Terminated
			};

			IncomingJingleFileTransfer(
					const JID& recipient,
					JingleSession::ref,
					JingleContentPayload::ref content,
					RemoteJingleTransportCandidateSelectorFactory*,
					LocalJingleTransportCandidateGeneratorFactory*,
					IQRouter* router,
					SOCKS5BytestreamRegistry* bytestreamRegistry,
					SOCKS5BytestreamProxy* bytestreamProxy,
					TimerFactory*,
					CryptoProvider*);
			~IncomingJingleFileTransfer();

			virtual void accept(WriteBytestream::ref);
			virtual const JID& getSender() const;
			virtual const JID& getRecipient() const;
			void cancel();

		private:
			void handleSessionTerminateReceived(boost::optional<JinglePayload::Reason>);
			void handleSessionInfoReceived(JinglePayload::ref);
			void handleTransportReplaceReceived(const JingleContentID&, JingleTransportPayload::ref);
			void handleTransportInfoReceived(const JingleContentID&, JingleTransportPayload::ref);
			void handleLocalTransportCandidatesGenerated(JingleTransportPayload::ref candidates);
			void handleRemoteTransportCandidateSelectFinished(JingleTransportPayload::ref candidate);
			void setActiveTransport(JingleTransport::ref transport);
			void handleTransportDataReceived(const std::vector<unsigned char>& data);
			void handleWriteStreamDataReceived(const std::vector<unsigned char>& data);
			void stopActiveTransport();
			void checkCandidateSelected();
			JingleIncomingIBBTransport::ref createIBBTransport(JingleIBBTransportPayload::ref ibbTransport);
			JingleContentID getContentID() const;
			void checkIfAllDataReceived();
			bool verifyReceviedData();
			void finishOffTransfer();
			void handleTransferFinished(boost::optional<FileTransferError>);

		private:
			typedef std::map<std::string, JingleS5BTransportPayload::Candidate> CandidateMap;

		private:
			void activateProxySession(const JID &proxy);
			void handleActivateProxySessionResult(boost::shared_ptr<S5BProxyRequest> request, ErrorPayload::ref error);
			void proxySessionReady(const JID& proxy, bool error);

		private:
			void useOurCandidateChoiceForTransfer(JingleS5BTransportPayload::Candidate candidate);
			void useTheirCandidateChoiceForTransfer(JingleS5BTransportPayload::Candidate candidate);
			void decideOnUsedTransport();
			void fillCandidateMap(CandidateMap& map, JingleS5BTransportPayload::ref s5bPayload);

		private:
			JID ourJID;
			JingleSession::ref session;
			IQRouter* router;
			JingleContentPayload::ref initialContent;
			CryptoProvider* crypto;
			State state;
			JingleFileTransferDescription::ref description;
			WriteBytestream::ref stream;
			boost::uintmax_t receivedBytes;
			IncrementalBytestreamHashCalculator* hashCalculator;
			Timer::ref waitOnHashTimer;
			IDGenerator idGenerator;

			RemoteJingleTransportCandidateSelector* candidateSelector;
			LocalJingleTransportCandidateGenerator* candidateGenerator;
			SOCKS5BytestreamRegistry* s5bRegistry;
			SOCKS5BytestreamProxy* s5bProxy;
			bool remoteTransportCandidateSelectFinished;
			JingleTransportPayload::ref selectedRemoteTransportCandidate;
			bool localTransportCandidateSelectFinished;
			JingleTransportPayload::ref selectedLocalTransportCandidate;

			JingleS5BTransportPayload::ref ourCandidate;
			JingleS5BTransportPayload::ref theirCandidate;
			CandidateMap ourCandidates;
			CandidateMap theirCandidates;
			SOCKS5BytestreamClientSession::ref clientSession;
			std::string s5bDestination;
			std::string s5bSessionID;
			SOCKS5BytestreamServerSession* serverSession;

			JingleTransport::ref activeTransport;
	};
}