summaryrefslogtreecommitdiffstats
blob: f9a1832884a2e16f32b98154e617493c86f501aa (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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2015-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Serializer/PayloadSerializers/JingleS5BTransportPayloadSerializer.h>

#include <memory>

#include <boost/lexical_cast.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLNode.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>

namespace Swift {

JingleS5BTransportPayloadSerializer::JingleS5BTransportPayloadSerializer() {
}

std::string JingleS5BTransportPayloadSerializer::serializePayload(std::shared_ptr<JingleS5BTransportPayload> payload) const {
    XMLElement payloadXML("transport", "urn:xmpp:jingle:transports:s5b:1");
    payloadXML.setAttribute("sid", payload->getSessionID());
    payloadXML.setAttribute("mode", modeToString(payload->getMode()));
    if (!payload->getDstAddr().empty()) {
        payloadXML.setAttribute("dstaddr", payload->getDstAddr());
    }

    for (const auto& candidate : payload->getCandidates()) {
        std::shared_ptr<XMLElement> candidateXML = std::make_shared<XMLElement>("candidate");
        candidateXML->setAttribute("cid", candidate.cid);
        candidateXML->setAttribute("host", candidate.hostPort.getAddress().toString());
        candidateXML->setAttribute("jid", candidate.jid.toString());
        candidateXML->setAttribute("port", std::to_string(candidate.hostPort.getPort()));
        candidateXML->setAttribute("priority", std::to_string(candidate.priority));
        candidateXML->setAttribute("type", typeToString(candidate.type));
        payloadXML.addNode(candidateXML);
    }

    if (payload->hasCandidateError()) {
        payloadXML.addNode(std::make_shared<XMLElement>("candidate-error"));
    }
    if (payload->hasProxyError()) {
        payloadXML.addNode(std::make_shared<XMLElement>("proxy-error"));
    }

    if (!payload->getActivated().empty()) {
        std::shared_ptr<XMLElement> activatedXML = std::make_shared<XMLElement>("activated");
        activatedXML->setAttribute("cid", payload->getActivated());
        payloadXML.addNode(activatedXML);
    }
    if (!payload->getCandidateUsed().empty()) {
        std::shared_ptr<XMLElement> candusedXML = std::make_shared<XMLElement>("candidate-used");
        candusedXML->setAttribute("cid", payload->getCandidateUsed());
        payloadXML.addNode(candusedXML);
    }

    return payloadXML.serialize();
}

std::string JingleS5BTransportPayloadSerializer::modeToString(JingleS5BTransportPayload::Mode mode) const {
    switch(mode) {
        case JingleS5BTransportPayload::TCPMode:
            return "tcp";
        case JingleS5BTransportPayload::UDPMode:
            return "udp";
    }
    assert(false);
    return "";
}

std::string JingleS5BTransportPayloadSerializer::typeToString(JingleS5BTransportPayload::Candidate::Type type) const {
    switch(type) {
        case JingleS5BTransportPayload::Candidate::AssistedType:
            return "assisted";
        case JingleS5BTransportPayload::Candidate::DirectType:
            return "direct";
        case JingleS5BTransportPayload::Candidate::ProxyType:
            return "proxy";
        case JingleS5BTransportPayload::Candidate::TunnelType:
            return "tunnel";
    }
    assert(false);
    return "";
}

}