summaryrefslogtreecommitdiffstats
blob: 543ee1e27217c48a8cde50162e1c90d6c6249154 (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
/*
 * Copyright (c) 2011 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Network/TLSConnection.h>

#include <boost/bind.hpp>

#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/TLS/TLSContext.h>
#include <Swiften/TLS/TLSContextFactory.h>

namespace Swift {

TLSConnection::TLSConnection(Connection::ref connection, TLSContextFactory* tlsFactory) : connection(connection) {
	context = tlsFactory->createTLSContext();
	context->onDataForNetwork.connect(boost::bind(&TLSConnection::handleTLSDataForNetwork, this, _1));
	context->onDataForApplication.connect(boost::bind(&TLSConnection::handleTLSDataForApplication, this, _1));
	context->onConnected.connect(boost::bind(&TLSConnection::handleTLSConnectFinished, this, false));
	context->onError.connect(boost::bind(&TLSConnection::handleTLSConnectFinished, this, true));

	connection->onConnectFinished.connect(boost::bind(&TLSConnection::handleRawConnectFinished, this, _1));
	connection->onDataRead.connect(boost::bind(&TLSConnection::handleRawDataRead, this, _1));
	connection->onDataWritten.connect(boost::bind(&TLSConnection::handleRawDataWritten, this));
	connection->onDisconnected.connect(boost::bind(&TLSConnection::handleRawDisconnected, this, _1));
}

TLSConnection::~TLSConnection() {
	connection->onConnectFinished.disconnect(boost::bind(&TLSConnection::handleRawConnectFinished, this, _1));
	connection->onDataRead.disconnect(boost::bind(&TLSConnection::handleRawDataRead, this, _1));
	connection->onDataWritten.disconnect(boost::bind(&TLSConnection::handleRawDataWritten, this));
	connection->onDisconnected.disconnect(boost::bind(&TLSConnection::handleRawDisconnected, this, _1));
	delete context;
}

void TLSConnection::handleTLSConnectFinished(bool error) {
	onConnectFinished(error);
	if (error) {
		disconnect();
	}
}

void TLSConnection::handleTLSDataForNetwork(const SafeByteArray& data) {
	connection->write(data);
}

void TLSConnection::handleTLSDataForApplication(const SafeByteArray& data) {
	onDataRead(boost::make_shared<SafeByteArray>(data));
}

void TLSConnection::connect(const HostAddressPort& address) {
	connection->connect(address);
}

void TLSConnection::disconnect() {
	connection->disconnect();
}

void TLSConnection::write(const SafeByteArray& data) {
	context->handleDataFromApplication(data);
}

HostAddressPort TLSConnection::getLocalAddress() const {
	return connection->getLocalAddress();
}

void TLSConnection::handleRawConnectFinished(bool error) {
	connection->onConnectFinished.disconnect(boost::bind(&TLSConnection::handleRawConnectFinished, this, _1));
	if (error) {
		onConnectFinished(true);
	}
	else {
		context->connect();
	}
}

void TLSConnection::handleRawDisconnected(const boost::optional<Error>& error) {
	onDisconnected(error);
}

void TLSConnection::handleRawDataRead(boost::shared_ptr<SafeByteArray> data) {
	context->handleDataFromNetwork(*data);
}

void TLSConnection::handleRawDataWritten() {
	onDataWritten();
}

}