summaryrefslogtreecommitdiffstats
blob: 55296f909c050659669daef007e52e883a654ada (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (c) 2013-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/FileTransfer/JingleFileTransfer.h>

#include <boost/typeof/typeof.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Crypto/CryptoProvider.h>
#include <Swiften/FileTransfer/FileTransferTransporter.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Jingle/JingleSession.h>
#include <Swiften/StringCodecs/Hexify.h>

using namespace Swift;

JingleFileTransfer::JingleFileTransfer(
        boost::shared_ptr<JingleSession> session,
        const JID& target,
        FileTransferTransporterFactory* transporterFactory) :
            session(session),
            target(target),
            transporterFactory(transporterFactory),
            transporter(NULL),
            ourCandidateSelectFinished(false),
            theirCandidateSelectFinished(false) {

    session->addListener(this);

}

JingleFileTransfer::~JingleFileTransfer() {
    session->removeListener(this);
}

void JingleFileTransfer::fillCandidateMap(CandidateMap& map, const std::vector<JingleS5BTransportPayload::Candidate>& candidates) {
    map.clear();
    foreach (JingleS5BTransportPayload::Candidate candidate, candidates) {
        map[candidate.cid] = candidate;
    }
}

/*
std::string JingleFileTransfer::getS5BDstAddr(const JID& requester, const JID& target) const {
    return Hexify::hexify(crypto->getSHA1Hash(
                createSafeByteArray(s5bSessionID + requester.toString() + target.toString())));
}
*/

const JID& JingleFileTransfer::getInitiator() const {
    return session->getInitiator();
}

const JID& JingleFileTransfer::getResponder() const {
    return target;
}

FileTransfer::State::Type JingleFileTransfer::getExternalFinishedState(JinglePayload::Reason::Type reason) {
    if (reason == JinglePayload::Reason::Cancel || reason == JinglePayload::Reason::Decline) {
        return FileTransfer::State::Canceled;
    }
    else if (reason == JinglePayload::Reason::Success) {
        return FileTransfer::State::Finished;
    }
    else {
        return FileTransfer::State::Failed;
    }
}

boost::optional<FileTransferError> JingleFileTransfer::getFileTransferError(JinglePayload::Reason::Type reason) {
    if (reason == JinglePayload::Reason::Success) {
        return boost::optional<FileTransferError>();
    }
    else {
        return boost::optional<FileTransferError>(FileTransferError::UnknownError);
    }
}

void JingleFileTransfer::handleRemoteTransportCandidateSelectFinished(
        const std::string& s5bSessionID, const boost::optional<JingleS5BTransportPayload::Candidate>& candidate) {
    SWIFT_LOG(debug) << std::endl;

    ourCandidateChoice = candidate;
    ourCandidateSelectFinished = true;

    JingleS5BTransportPayload::ref s5bPayload = boost::make_shared<JingleS5BTransportPayload>();
    s5bPayload->setSessionID(s5bSessionID);
    if (candidate) {
        s5bPayload->setCandidateUsed(candidate->cid);
    }
    else {
        s5bPayload->setCandidateError(true);
    }
    candidateSelectRequestID = session->sendTransportInfo(getContentID(), s5bPayload);

    decideOnCandidates();
}

// decide on candidates according to http://xmpp.org/extensions/xep-0260.html#complete
void JingleFileTransfer::decideOnCandidates() {
    SWIFT_LOG(debug) << std::endl;
    if (!ourCandidateSelectFinished || !theirCandidateSelectFinished) {
        SWIFT_LOG(debug) << "Can't make a decision yet!" << std::endl;
        return;
    }
    if (!ourCandidateChoice && !theirCandidateChoice) {
        SWIFT_LOG(debug) << "No candidates succeeded." << std::endl;
        fallback();
    }
    else if (ourCandidateChoice && !theirCandidateChoice) {
        SWIFT_LOG(debug) << "Start transfer using remote candidate: " << ourCandidateChoice.get().cid << "." << std::endl;
        startTransferViaRemoteCandidate();
    }
    else if (theirCandidateChoice && !ourCandidateChoice) {
        SWIFT_LOG(debug) << "Start transfer using local candidate: " << theirCandidateChoice.get().cid << "." << std::endl;
        startTransferViaLocalCandidate();
    }
    else {
        SWIFT_LOG(debug) << "Choosing between candidates "
            << ourCandidateChoice->cid << "(" << ourCandidateChoice->priority << ")" << " and "
            << theirCandidateChoice->cid << "(" << theirCandidateChoice->priority << ")" << std::endl;
        if (ourCandidateChoice->priority > theirCandidateChoice->priority) {
            SWIFT_LOG(debug) << "Start transfer using remote candidate: " << ourCandidateChoice.get().cid << "." << std::endl;
            startTransferViaRemoteCandidate();
        }
        else if (ourCandidateChoice->priority < theirCandidateChoice->priority) {
            SWIFT_LOG(debug) << "Start transfer using local candidate:" << theirCandidateChoice.get().cid << "." << std::endl;
            startTransferViaLocalCandidate();
        }
        else {
            if (hasPriorityOnCandidateTie()) {
                SWIFT_LOG(debug) << "Start transfer using remote candidate: " << ourCandidateChoice.get().cid << std::endl;
                startTransferViaRemoteCandidate();
            }
            else {
                SWIFT_LOG(debug) << "Start transfer using local candidate: " << theirCandidateChoice.get().cid << std::endl;
                startTransferViaLocalCandidate();
            }
        }
    }
}

void JingleFileTransfer::handleProxyActivateFinished(
        const std::string& s5bSessionID, ErrorPayload::ref error) {
    SWIFT_LOG(debug) << std::endl;
    if (!isWaitingForLocalProxyActivate()) { SWIFT_LOG(warning) << "Incorrect state" << std::endl; return; }

    if (error) {
        SWIFT_LOG(debug) << "Error activating proxy" << std::endl;
        JingleS5BTransportPayload::ref proxyError = boost::make_shared<JingleS5BTransportPayload>();
        proxyError->setSessionID(s5bSessionID);
        proxyError->setProxyError(true);
        session->sendTransportInfo(getContentID(), proxyError);
        fallback();
    }
    else {
        JingleS5BTransportPayload::ref proxyActivate = boost::make_shared<JingleS5BTransportPayload>();
        proxyActivate->setSessionID(s5bSessionID);
        proxyActivate->setActivated(theirCandidateChoice->cid);
        session->sendTransportInfo(getContentID(), proxyActivate);
        startTransferring(createLocalCandidateSession());
    }
}

void JingleFileTransfer::handleTransportInfoReceived(
        const JingleContentID& /* contentID */, JingleTransportPayload::ref transport) {
    SWIFT_LOG(debug) << std::endl;

    if (JingleS5BTransportPayload::ref s5bPayload = boost::dynamic_pointer_cast<JingleS5BTransportPayload>(transport)) {
        if (s5bPayload->hasCandidateError() || !s5bPayload->getCandidateUsed().empty()) {
            SWIFT_LOG(debug) << "Received candidate decision from peer" << std::endl;
            if (!isTryingCandidates()) { SWIFT_LOG(warning) << "Incorrect state" << std::endl; return; }

            theirCandidateSelectFinished = true;
            if (!s5bPayload->hasCandidateError()) {
                BOOST_AUTO(theirCandidate, localCandidates.find(s5bPayload->getCandidateUsed()));
                if (theirCandidate == localCandidates.end()) {
                    SWIFT_LOG(warning) << "Got invalid candidate" << std::endl;
                    terminate(JinglePayload::Reason::GeneralError);
                    return;
                }
                theirCandidateChoice = theirCandidate->second;
            }
            decideOnCandidates();
        }
        else if (!s5bPayload->getActivated().empty()) {
            SWIFT_LOG(debug) << "Received peer activate from peer" << std::endl;
            if (!isWaitingForPeerProxyActivate()) { SWIFT_LOG(warning) << "Incorrect state" << std::endl; return; }

            if (ourCandidateChoice->cid == s5bPayload->getActivated()) {
                startTransferring(createRemoteCandidateSession());
            }
            else {
                SWIFT_LOG(warning) << "ourCandidateChoice doesn't match activated proxy candidate!" << std::endl;
                terminate(JinglePayload::Reason::GeneralError);
            }
        }
        else if (s5bPayload->hasProxyError()) {
            SWIFT_LOG(debug) << "Received proxy error. Trying to fall back to IBB." << std::endl;
            fallback();
        }
        else {
            SWIFT_LOG(debug) << "Ignoring unknown info" << std::endl;
        }
    }
    else {
        SWIFT_LOG(debug) << "Ignoring unknown info" << std::endl;
    }
}

void JingleFileTransfer::setTransporter(FileTransferTransporter* transporter) {
    SWIFT_LOG_ASSERT(!this->transporter, error);
    this->transporter = transporter;
    localTransportCandidatesGeneratedConnection = transporter->onLocalCandidatesGenerated.connect(
        boost::bind(&JingleFileTransfer::handleLocalTransportCandidatesGenerated, this, _1, _2, _3));
    remoteTransportCandidateSelectFinishedConnection = transporter->onRemoteCandidateSelectFinished.connect(
        boost::bind(&JingleFileTransfer::handleRemoteTransportCandidateSelectFinished, this, _1, _2));
    proxyActivatedConnection = transporter->onProxyActivated.connect(
        boost::bind(&JingleFileTransfer::handleProxyActivateFinished, this, _1, _2));
}

void JingleFileTransfer::removeTransporter() {
    if (transporter) {
        localTransportCandidatesGeneratedConnection.release();
        remoteTransportCandidateSelectFinishedConnection.release();
        proxyActivatedConnection.release();
        delete transporter;
        transporter = NULL;
    }
}