Files
@ ab2424659344
Branch filter:
Location: libtransport.git/src/transport.cpp - annotation
ab2424659344
13.1 KiB
text/x-c++hdr
SChannelServerContext and Win32 compilation fixes
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 ab2424659344 1c72e967096c c5edfd19b1aa c8006a0ebab6 1e8eb7076f17 c5edfd19b1aa a6253712ccbc 778a11d5dc68 ab2424659344 ab2424659344 ab2424659344 ab2424659344 ab2424659344 9275aa8c8e2b ab2424659344 9275aa8c8e2b ab2424659344 d6c003bad3b6 d6c003bad3b6 64f1921f8d5c 64f1921f8d5c 6ff8b24992b1 6ff8b24992b1 320738eda85a 320738eda85a 8faecb5664c8 b3725b47c4ba b3725b47c4ba c38103451c30 c38103451c30 b5c2a6e00113 b5c2a6e00113 b5c2a6e00113 0498acd07a1d 4c334e9f1c0b e678a46286f4 e678a46286f4 e678a46286f4 f9d40b961a35 e678a46286f4 e678a46286f4 f9d40b961a35 f9d40b961a35 b5c2a6e00113 79cc6dc63de8 dd11e43325de 79cc6dc63de8 79cc6dc63de8 79cc6dc63de8 e678a46286f4 e678a46286f4 c8006a0ebab6 828ae9d2cb88 1e8eb7076f17 e678a46286f4 bf4183c181eb e678a46286f4 24d25a6f406b e678a46286f4 f8f37ecbdd59 28ca4ba95e42 e678a46286f4 79cc6dc63de8 f9d40b961a35 79cc6dc63de8 57b59b8220e5 f9d40b961a35 ff42794006fd ab2424659344 ab2424659344 ab2424659344 ab2424659344 57b59b8220e5 ab2424659344 ab2424659344 ab2424659344 57b59b8220e5 f9d40b961a35 f9d40b961a35 f9d40b961a35 28ca4ba95e42 79cc6dc63de8 79cc6dc63de8 edcb55d4ce22 778a11d5dc68 d6c003bad3b6 64f1921f8d5c b3725b47c4ba 0741375ec667 6ff8b24992b1 320738eda85a d6c003bad3b6 d6c003bad3b6 64f1921f8d5c b3725b47c4ba c38103451c30 6ff8b24992b1 8faecb5664c8 320738eda85a 778a11d5dc68 7e8720849725 7e8720849725 79cc6dc63de8 79cc6dc63de8 f8f37ecbdd59 79cc6dc63de8 79cc6dc63de8 79cc6dc63de8 7e8720849725 7e8720849725 7e8720849725 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 6ff8b24992b1 320738eda85a 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 1e3dc1ef4d6d 6ff8b24992b1 8faecb5664c8 320738eda85a 1e3dc1ef4d6d 79cc6dc63de8 79cc6dc63de8 79cc6dc63de8 e678a46286f4 e678a46286f4 79cc6dc63de8 79cc6dc63de8 5f017bd15dc7 e678a46286f4 f035510e4798 c5edfd19b1aa e678a46286f4 3fe27413f536 c5edfd19b1aa a6253712ccbc 79cc6dc63de8 a6253712ccbc 07859ef3936a e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 c5edfd19b1aa e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 07859ef3936a e31c07794705 79cc6dc63de8 79cc6dc63de8 e31c07794705 e31c07794705 79cc6dc63de8 e31c07794705 e678a46286f4 e678a46286f4 79cc6dc63de8 79cc6dc63de8 2ae880ff94b5 2ae880ff94b5 f035510e4798 2ae880ff94b5 2ae880ff94b5 2ae880ff94b5 c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa d0adbddc0ba7 d0adbddc0ba7 d0adbddc0ba7 d0adbddc0ba7 c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa 28ca4ba95e42 751eaaaf3216 f8f37ecbdd59 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 f9d40b961a35 28ca4ba95e42 56dd64999814 56dd64999814 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 4c885cc94785 4c885cc94785 28ca4ba95e42 28ca4ba95e42 28ca4ba95e42 f9d40b961a35 28ca4ba95e42 28ca4ba95e42 e678a46286f4 e678a46286f4 c5edfd19b1aa 99aff9256622 e678a46286f4 e678a46286f4 e678a46286f4 c5edfd19b1aa 99aff9256622 e678a46286f4 c5edfd19b1aa f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 f8f37ecbdd59 e678a46286f4 e678a46286f4 e678a46286f4 e678a46286f4 877d783276c0 1c72e967096c 1c72e967096c 1c72e967096c 1c72e967096c 99aff9256622 99aff9256622 877d783276c0 b5c2a6e00113 99aff9256622 99aff9256622 c5edfd19b1aa 1ebe5535c4f3 c5edfd19b1aa e5fcbf00b799 c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa f9d40b961a35 c5edfd19b1aa e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa c5edfd19b1aa e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d e23f8e3c7f9d 5f017bd15dc7 e23f8e3c7f9d c5edfd19b1aa c5edfd19b1aa e678a46286f4 | /**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "transport/transport.h"
#include <boost/bind.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "transport/storagebackend.h"
#include "transport/factory.h"
#include "transport/userregistry.h"
#include "discoinforesponder.h"
#include "discoitemsresponder.h"
#include "storageparser.h"
#ifdef _WIN32
#include <Swiften/TLS/CAPICertificate.h>
#include "Swiften/TLS/Schannel/SchannelServerContext.h"
#include "Swiften/TLS/Schannel/SchannelServerContextFactory.h"
#else
#include "Swiften/TLS/PKCS12Certificate.h"
#include "Swiften/TLS/OpenSSL/OpenSSLServerContext.h"
#include "Swiften/TLS/OpenSSL/OpenSSLServerContextFactory.h"
#endif
#include "Swiften/Parser/PayloadParsers/AttentionParser.h"
#include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h"
#include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h"
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
#include "Swiften/Parser/PayloadParsers/StatsParser.h"
#include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h"
#include "Swiften/Parser/PayloadParsers/GatewayPayloadParser.h"
#include "Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.h"
#include "Swiften/Serializer/PayloadSerializers/SpectrumErrorSerializer.h"
#include "transport/BlockParser.h"
#include "transport/BlockSerializer.h"
#include "Swiften/Parser/PayloadParsers/InvisibleParser.h"
#include "Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h"
#include "log4cxx/logger.h"
#include "log4cxx/consoleappender.h"
#include "log4cxx/patternlayout.h"
#include "log4cxx/propertyconfigurator.h"
#include "Swiften/Swiften.h"
using namespace Swift;
using namespace boost;
using namespace log4cxx;
namespace Transport {
static LoggerPtr logger = Logger::getLogger("Component");
static LoggerPtr logger_xml = Logger::getLogger("Component.XML");
Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, Config *config, Factory *factory, Transport::UserRegistry *userRegistry) {
m_component = NULL;
m_userRegistry = NULL;
m_server = NULL;
m_reconnectCount = 0;
m_config = config;
m_factory = factory;
m_loop = loop;
m_userRegistry = userRegistry;
m_jid = Swift::JID(CONFIG_STRING(m_config, "service.jid"));
m_factories = factories;
m_reconnectTimer = m_factories->getTimerFactory()->createTimer(3000);
m_reconnectTimer->onTick.connect(bind(&Component::start, this));
if (CONFIG_BOOL(m_config, "service.server_mode")) {
LOG4CXX_INFO(logger, "Creating component in server mode on port " << CONFIG_INT(m_config, "service.port"));
m_server = new Swift::Server(loop, m_factories, m_userRegistry, m_jid, CONFIG_INT(m_config, "service.port"));
if (!CONFIG_STRING(m_config, "service.cert").empty()) {
LOG4CXX_INFO(logger, "Using PKCS#12 certificate " << CONFIG_STRING(m_config, "service.cert"));
LOG4CXX_INFO(logger, "SSLv23_server_method used.");
#ifdef _WIN32
TLSServerContextFactory *f = new SchannelServerContextFactory();
m_server->addTLSEncryption(f, boost::make_shared<CAPICertificate>(CONFIG_STRING(m_config, "service.cert")));
#else
TLSServerContextFactory *f = new OpenSSLServerContextFactory();
m_server->addTLSEncryption(f, boost::make_shared<PKCS12Certificate>(CONFIG_STRING(m_config, "service.cert"), createSafeByteArray(CONFIG_STRING(m_config, "service.cert_password"))));
#endif
}
else {
LOG4CXX_WARN(logger, "No PKCS#12 certificate used. TLS is disabled.");
}
// m_server->start();
m_stanzaChannel = m_server->getStanzaChannel();
m_iqRouter = m_server->getIQRouter();
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::GatewayPayloadParser>("query", "jabber:iq:gateway"));
m_server->addPayloadSerializer(new Swift::AttentionSerializer());
m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer());
m_server->addPayloadSerializer(new Transport::BlockSerializer());
m_server->addPayloadSerializer(new Swift::InvisibleSerializer());
m_server->addPayloadSerializer(new Swift::StatsSerializer());
m_server->addPayloadSerializer(new Swift::SpectrumErrorSerializer());
m_server->addPayloadSerializer(new Swift::GatewayPayloadSerializer());
m_server->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1));
m_server->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1));
}
else {
LOG4CXX_INFO(logger, "Creating component in gateway mode");
m_component = new Swift::Component(loop, m_factories, m_jid, CONFIG_STRING(m_config, "service.password"));
m_component->setSoftwareVersion("", "");
m_component->onConnected.connect(bind(&Component::handleConnected, this));
m_component->onError.connect(boost::bind(&Component::handleConnectionError, this, _1));
m_component->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1));
m_component->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<StorageParser>("private", "jabber:iq:private"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::AttentionParser>("attention", "urn:xmpp:attention:0"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("query", "http://jabber.org/protocol/stats"));
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::GatewayPayloadParser>("query", "jabber:iq:gateway"));
m_component->addPayloadSerializer(new Swift::AttentionSerializer());
m_component->addPayloadSerializer(new Swift::XHTMLIMSerializer());
m_component->addPayloadSerializer(new Transport::BlockSerializer());
m_component->addPayloadSerializer(new Swift::InvisibleSerializer());
m_component->addPayloadSerializer(new Swift::StatsSerializer());
m_component->addPayloadSerializer(new Swift::SpectrumErrorSerializer());
m_component->addPayloadSerializer(new Swift::GatewayPayloadSerializer());
m_stanzaChannel = m_component->getStanzaChannel();
m_iqRouter = m_component->getIQRouter();
}
m_capsMemoryStorage = new CapsMemoryStorage();
m_capsManager = new CapsManager(m_capsMemoryStorage, m_stanzaChannel, m_iqRouter);
m_entityCapsManager = new EntityCapsManager(m_capsManager, m_stanzaChannel);
m_entityCapsManager->onCapsChanged.connect(boost::bind(&Component::handleCapsChanged, this, _1));
m_presenceOracle = new Transport::PresenceOracle(m_stanzaChannel);
m_presenceOracle->onPresenceChange.connect(bind(&Component::handlePresence, this, _1));
m_discoInfoResponder = new DiscoInfoResponder(m_iqRouter, m_config);
m_discoInfoResponder->start();
m_discoItemsResponder = new DiscoItemsResponder(m_iqRouter);
m_discoItemsResponder->start();
//
// m_registerHandler = new SpectrumRegisterHandler(m_component);
// m_registerHandler->start();
}
Component::~Component() {
delete m_presenceOracle;
delete m_entityCapsManager;
delete m_capsManager;
delete m_capsMemoryStorage;
delete m_discoInfoResponder;
delete m_discoItemsResponder;
if (m_component)
delete m_component;
if (m_server) {
m_server->stop();
delete m_server;
}
}
Swift::StanzaChannel *Component::getStanzaChannel() {
return m_stanzaChannel;
}
Transport::PresenceOracle *Component::getPresenceOracle() {
return m_presenceOracle;
}
void Component::setTransportFeatures(std::list<std::string> &features) {
m_discoInfoResponder->setTransportFeatures(features);
}
Swift::CapsInfo &Component::getBuddyCapsInfo() {
return m_discoInfoResponder->getBuddyCapsInfo();
}
void Component::setBuddyFeatures(std::list<std::string> &features) {
// TODO: handle caps change
m_discoInfoResponder->setBuddyFeatures(features);
}
void Component::start() {
if (m_component && !m_component->isAvailable()) {
LOG4CXX_INFO(logger, "Connecting XMPP server " << CONFIG_STRING(m_config, "service.server") << " port " << CONFIG_INT(m_config, "service.port"));
m_reconnectCount++;
m_component->connect(CONFIG_STRING(m_config, "service.server"), CONFIG_INT(m_config, "service.port"));
m_reconnectTimer->stop();
}
else if (m_server) {
LOG4CXX_INFO(logger, "Starting component in server mode on port " << CONFIG_INT(m_config, "service.port"));
m_server->start();
// We're connected right here, because we're in server mode...
handleConnected();
}
}
void Component::stop() {
if (m_component) {
m_reconnectCount = 0;
// TODO: Call this once swiften will fix assert(!session_);
// m_component->disconnect();
m_reconnectTimer->stop();
}
else if (m_server) {
LOG4CXX_INFO(logger, "Stopping component in server mode on port " << CONFIG_INT(m_config, "service.port"));
m_server->stop();
}
}
void Component::handleConnected() {
onConnected();
m_reconnectCount = 0;
}
void Component::handleConnectionError(const ComponentError &error) {
onConnectionError(error);
// if (m_reconnectCount == 2)
// Component::instance()->userManager()->removeAllUsers();
std::string str = "Unknown error";
switch (error.getType()) {
case ComponentError::UnknownError: str = "Unknown error"; break;
case ComponentError::ConnectionError: str = "Connection error"; break;
case ComponentError::ConnectionReadError: str = "Connection read error"; break;
case ComponentError::ConnectionWriteError: str = "Connection write error"; break;
case ComponentError::XMLError: str = "XML Error"; break;
case ComponentError::AuthenticationFailedError: str = "Authentication failed error"; break;
case ComponentError::UnexpectedElementError: str = "Unexpected element error"; break;
}
LOG4CXX_INFO(logger, "Disconnected from XMPP server. Error: " << str);
m_reconnectTimer->start();
}
void Component::handleDataRead(const Swift::SafeByteArray &data) {
std::string d = safeByteArrayToString(data);
if (!boost::starts_with(d, "<auth")) {
LOG4CXX_INFO(logger_xml, "XML IN " << d);
}
}
void Component::handleDataWritten(const Swift::SafeByteArray &data) {
LOG4CXX_INFO(logger_xml, "XML OUT " << safeByteArrayToString(data));
}
void Component::handlePresence(Swift::Presence::ref presence) {
bool isMUC = presence->getPayload<MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
// filter out login/logout presence spam
if (!presence->getTo().getNode().empty() && isMUC == false)
return;
// filter out bad presences
if (!presence->getFrom().isValid()) {
return;
}
// check if we have this client's capabilities and ask for them
if (presence->getType() != Swift::Presence::Unavailable) {
boost::shared_ptr<CapsInfo> capsInfo = presence->getPayload<CapsInfo>();
if (capsInfo && capsInfo->getHash() == "sha-1") {
/*haveFeatures = */m_entityCapsManager->getCaps(presence->getFrom()) != DiscoInfo::ref();
}
#ifdef SUPPORT_LEGACY_CAPS
else {
GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(presence->getFrom(), m_iqRouter);
discoInfoRequest->onResponse.connect(boost::bind(&Component::handleDiscoInfoResponse, this, _1, _2, presence->getFrom()));
discoInfoRequest->send();
}
#endif
}
onUserPresenceReceived(presence);
}
void Component::handleDiscoInfoResponse(boost::shared_ptr<Swift::DiscoInfo> info, Swift::ErrorPayload::ref error, const Swift::JID& jid) {
#ifdef SUPPORT_LEGACY_CAPS
onUserDiscoInfoReceived(jid, info);
#endif
}
void Component::handleCapsChanged(const Swift::JID& jid) {
onUserDiscoInfoReceived(jid, m_entityCapsManager->getCaps(jid));
}
}
|