summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-07-29 09:47:23 (GMT)
committerTobias Markmann <tm@ayena.de>2016-11-28 10:35:05 (GMT)
commit2039930eadd4756068a8a60c8340d9908a7136d3 (patch)
treed8aca4bf98a2bb6e3b819305b1f87af3117f4910 /Swiften/Client/ClientSession.h
parent2f90eb7409df91a80c60b189242ac0c1de313910 (diff)
downloadswift-2039930eadd4756068a8a60c8340d9908a7136d3.zip
swift-2039930eadd4756068a8a60c8340d9908a7136d3.tar.bz2
Correctly handle server initiated closing of stream
If a server closes the XMPP stream, it sends a </stream:stream> tag. The client is supposed to respond with the same tag and then both parties can close the TLS/TCP socket. Previously Swift(-en) would simply ignore </stream:stream> tag if it was not directly followed by a shutdown of the TCP connection. In addition there is now a timeout timer started as soon as Swiften or the server initiates a shutdown. It will close the socket and cleanup the ClientSession if the server does not respond in time or the network is faulty. Refactored some code in ClientSession in the process. Moved ClientSession::State to a C++11 strongly typed enum class. This also fixes issues where duplicated </stream:stream> tags would be send by Swift. Test-Information: Tested against Prosody ba782a093b14 and M-Link 16.3v6-0, which provide ad-hoc commands to end a user session. Previously this was ignored by Swift. Now it correctly responds to the server, detects it as a disconnect and tries to reconnect afterwards. Added unit test for the case where the server closes the session stream. Change-Id: I59dfde3aa6b50dc117f340e5db6b9e58b54b3c60
Diffstat (limited to 'Swiften/Client/ClientSession.h')
-rw-r--r--Swiften/Client/ClientSession.h40
1 files changed, 27 insertions, 13 deletions
diff --git a/Swiften/Client/ClientSession.h b/Swiften/Client/ClientSession.h
index ad1b78c..c7b3658 100644
--- a/Swiften/Client/ClientSession.h
+++ b/Swiften/Client/ClientSession.h
@@ -16,18 +16,21 @@
#include <Swiften/Elements/ToplevelElement.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Session/SessionStream.h>
-#include <Swiften/StreamManagement/StanzaAckRequester.h>
-#include <Swiften/StreamManagement/StanzaAckResponder.h>
namespace Swift {
- class ClientAuthenticator;
class CertificateTrustChecker;
- class IDNConverter;
+ class ClientAuthenticator;
class CryptoProvider;
+ class IDNConverter;
+ class Stanza;
+ class StanzaAckRequester;
+ class StanzaAckResponder;
+ class TimerFactory;
+ class Timer;
class SWIFTEN_API ClientSession : public std::enable_shared_from_this<ClientSession> {
public:
- enum State {
+ enum class State {
Initial,
WaitingForStreamStart,
Negotiating,
@@ -55,7 +58,8 @@ namespace Swift {
SessionStartError,
TLSClientCertificateError,
TLSError,
- StreamError
+ StreamError,
+ StreamEndError, // The server send a closing stream tag.
} type;
std::shared_ptr<boost::system::error_code> errorCode;
Error(Type type) : type(type) {}
@@ -69,8 +73,8 @@ namespace Swift {
~ClientSession();
- static std::shared_ptr<ClientSession> create(const JID& jid, std::shared_ptr<SessionStream> stream, IDNConverter* idnConverter, CryptoProvider* crypto) {
- return std::shared_ptr<ClientSession>(new ClientSession(jid, stream, idnConverter, crypto));
+ static std::shared_ptr<ClientSession> create(const JID& jid, std::shared_ptr<SessionStream> stream, IDNConverter* idnConverter, CryptoProvider* crypto, TimerFactory* timerFactory) {
+ return std::shared_ptr<ClientSession>(new ClientSession(jid, stream, idnConverter, crypto, timerFactory));
}
State getState() const {
@@ -93,7 +97,6 @@ namespace Swift {
useAcks = b;
}
-
bool getStreamManagementEnabled() const {
// Explicitly convert to bool. In C++11, it would be cleaner to
// compare to nullptr.
@@ -116,7 +119,7 @@ namespace Swift {
void finish();
bool isFinished() const {
- return getState() == Finished;
+ return getState() == State::Finished;
}
void sendCredentials(const SafeByteArray& password);
@@ -138,6 +141,10 @@ namespace Swift {
authenticationPort = i;
}
+ void setSessionShutdownTimeout(int timeoutInMilliseconds) {
+ sessionShutdownTimeoutInMilliseconds = timeoutInMilliseconds;
+ }
+
public:
boost::signals2::signal<void ()> onNeedCredentials;
boost::signals2::signal<void ()> onInitialized;
@@ -150,7 +157,8 @@ namespace Swift {
const JID& jid,
std::shared_ptr<SessionStream>,
IDNConverter* idnConverter,
- CryptoProvider* crypto);
+ CryptoProvider* crypto,
+ TimerFactory* timerFactory);
void finishSession(Error::Type error);
void finishSession(std::shared_ptr<Swift::Error> error);
@@ -163,7 +171,9 @@ namespace Swift {
void handleElement(std::shared_ptr<ToplevelElement>);
void handleStreamStart(const ProtocolHeader&);
+ void handleStreamEnd();
void handleStreamClosed(std::shared_ptr<Swift::Error>);
+ void handleStreamShutdownTimeout();
void handleTLSEncrypted();
@@ -175,13 +185,17 @@ namespace Swift {
void ack(unsigned int handledStanzasCount);
void continueAfterTLSEncrypted();
void checkTrustOrFinish(const std::vector<Certificate::ref>& certificateChain, std::shared_ptr<CertificateVerificationError> error);
+ void initiateShutdown(bool sendFooter);
private:
JID localJID;
State state;
std::shared_ptr<SessionStream> stream;
- IDNConverter* idnConverter;
- CryptoProvider* crypto;
+ IDNConverter* idnConverter = nullptr;
+ CryptoProvider* crypto = nullptr;
+ TimerFactory* timerFactory = nullptr;
+ std::shared_ptr<Timer> streamShutdownTimeout;
+ int sessionShutdownTimeoutInMilliseconds = 10000;
bool allowPLAINOverNonTLS;
bool useStreamCompression;
UseTLS useTLS;