📄 client.cpp
字号:
/* Copyright (c) 2004-2008 by Jakob Schroeter <js@camaya.net> This file is part of the gloox library. http://camaya.net/gloox This software is distributed under a license. The full license agreement can be found in the file LICENSE in this distribution. This software may not be copied, modified, sold or distributed other than expressed in the named license agreement. This software is distributed without any warranty.*/#ifdef _WIN32# include "../config.h.win"#elif defined( _WIN32_WCE )# include "../config.h.win"#else# include "config.h"#endif#include "client.h"#include "rostermanager.h"#include "disco.h"#include "logsink.h"#include "nonsaslauth.h"#include "tag.h"#include "stanzaextensionfactory.h"#include "stanzaextension.h"#include "tlsbase.h"#if !defined( _WIN32 ) && !defined( _WIN32_WCE )# include <unistd.h>#endif#ifndef _WIN32_WCE# include <iostream># include <sstream>#else# include <stdio.h>#endifnamespace gloox{ Client::Client( const std::string& server ) : ClientBase( XMLNS_CLIENT, server ), m_rosterManager( 0 ), m_auth( 0 ), m_presence( PresenceAvailable ), m_resourceBound( false ), m_forceNonSasl( false ), m_manageRoster( true ), m_doAuth( false ), m_streamFeatures( 0 ), m_priority( 0 ) { m_jid.setServer( server ); init(); } Client::Client( const JID& jid, const std::string& password, int port ) : ClientBase( XMLNS_CLIENT, password, "", port ), m_rosterManager( 0 ), m_auth( 0 ), m_presence( PresenceAvailable ), m_resourceBound( false ), m_forceNonSasl( false ), m_manageRoster( true ), m_doAuth( true ), m_streamFeatures( 0 ), m_priority( 0 ) { m_jid = jid; m_server = m_jid.serverRaw(); init(); } Client::Client( const std::string& username, const std::string& password, const std::string& server, const std::string& resource, int port ) : ClientBase( XMLNS_CLIENT, password, server, port ), m_rosterManager( 0 ), m_auth( 0 ), m_presence( PresenceAvailable ), m_resourceBound( false ), m_forceNonSasl( false ), m_manageRoster( true ), m_doAuth( true ), m_streamFeatures( 0 ), m_priority( 0 ) { m_jid.setUsername( username ); m_jid.setServer( server ); m_jid.setResource( resource ); init(); } Client::~Client() { removePresenceExtensions(); delete m_rosterManager; delete m_auth; } void Client::init() { m_rosterManager = new RosterManager( this ); m_disco->setIdentity( "client", "bot" ); } void Client::setUsername( const std::string &username ) { m_jid.setUsername( username ); m_doAuth = true; } bool Client::handleNormalNode( Stanza *stanza ) { if( stanza->name() == "stream:features" ) { m_streamFeatures = getStreamFeatures( stanza ); if( m_tls == TLSRequired && !m_encryptionActive && ( !m_encryption || !( m_streamFeatures & StreamFeatureStartTls ) ) ) { logInstance().log( LogLevelError, LogAreaClassClient, "Client is configured to require TLS but either the server didn't offer TLS or " "TLS support is not compiled in." ); disconnect( ConnTlsNotAvailable ); } else if( m_tls > TLSDisabled && m_encryption && !m_encryptionActive && ( m_streamFeatures & StreamFeatureStartTls ) ) { notifyStreamEvent( StreamEventEncryption ); startTls(); } else if( m_sasl ) { if( m_authed ) { if( m_streamFeatures & StreamFeatureBind ) { notifyStreamEvent( StreamEventResourceBinding ); bindResource(); } } else if( m_doAuth && !username().empty() && !password().empty() ) { if( m_streamFeatures & SaslMechDigestMd5 && m_availableSaslMechs & SaslMechDigestMd5 && !m_forceNonSasl ) { notifyStreamEvent( StreamEventAuthentication ); startSASL( SaslMechDigestMd5 ); } else if( m_streamFeatures & SaslMechPlain && m_availableSaslMechs & SaslMechPlain && !m_forceNonSasl ) { notifyStreamEvent( StreamEventAuthentication ); startSASL( SaslMechPlain ); } else if( m_streamFeatures & StreamFeatureIqAuth || m_forceNonSasl ) { notifyStreamEvent( StreamEventAuthentication ); nonSaslLogin(); } else { logInstance().log( LogLevelError, LogAreaClassClient, "the server doesn't support any auth mechanisms we know about" ); disconnect( ConnNoSupportedAuth ); } } else if( m_doAuth && !m_clientCerts.empty() && !m_clientKey.empty() && m_streamFeatures & SaslMechExternal && m_availableSaslMechs & SaslMechExternal ) { notifyStreamEvent( StreamEventAuthentication ); startSASL( SaslMechExternal ); }#ifdef _WIN32 else if( m_doAuth && m_streamFeatures & SaslMechGssapi && m_availableSaslMechs & SaslMechGssapi ) { notifyStreamEvent( StreamEventAuthentication ); startSASL( SaslMechGssapi ); }#endif else if( m_doAuth && m_streamFeatures & SaslMechAnonymous && m_availableSaslMechs & SaslMechAnonymous ) { notifyStreamEvent( StreamEventAuthentication ); startSASL( SaslMechAnonymous ); } else { notifyStreamEvent( StreamEventFinished ); connected(); } } else if( m_compress && m_compression && !m_compressionActive && ( m_streamFeatures & StreamFeatureCompressZlib ) ) { notifyStreamEvent( StreamEventCompression ); negotiateCompression( StreamFeatureCompressZlib ); }// else if( ( m_streamFeatures & StreamFeatureCompressDclz )// && m_connection->initCompression( StreamFeatureCompressDclz ) )// {// negotiateCompression( StreamFeatureCompressDclz );// } else if( m_streamFeatures & StreamFeatureIqAuth ) { notifyStreamEvent( StreamEventAuthentication ); nonSaslLogin(); } else { logInstance().log( LogLevelError, LogAreaClassClient, "fallback: the server doesn't support any auth mechanisms we know about" ); disconnect( ConnNoSupportedAuth ); } } else if( ( stanza->name() == "proceed" ) && stanza->hasAttribute( "xmlns", XMLNS_STREAM_TLS ) ) { logInstance().log( LogLevelDebug, LogAreaClassClient, "starting TLS handshake..." ); if( m_encryption ) { m_encryptionActive = true; m_encryption->handshake(); } } else if( ( stanza->name() == "failure" ) && stanza->hasAttribute( "xmlns", XMLNS_STREAM_TLS ) ) { logInstance().log( LogLevelError, LogAreaClassClient, "TLS handshake failed (server-side)!" ); disconnect( ConnTlsFailed ); } else if( ( stanza->name() == "failure" ) && stanza->hasAttribute( "xmlns", XMLNS_COMPRESSION ) ) { logInstance().log( LogLevelError, LogAreaClassClient, "stream compression init failed!" ); disconnect( ConnCompressionFailed ); } else if( ( stanza->name() == "compressed" ) && stanza->hasAttribute( "xmlns", XMLNS_COMPRESSION ) ) { logInstance().log( LogLevelDebug, LogAreaClassClient, "stream compression inited" ); m_compressionActive = true; header(); } else if( ( stanza->name() == "challenge" ) && stanza->hasAttribute( "xmlns", XMLNS_STREAM_SASL ) ) { logInstance().log( LogLevelDebug, LogAreaClassClient, "processing SASL challenge" ); processSASLChallenge( stanza->cdata() ); } else if( ( stanza->name() == "failure" ) && stanza->hasAttribute( "xmlns", XMLNS_STREAM_SASL ) ) { logInstance().log( LogLevelError, LogAreaClassClient, "SASL authentication failed!" ); processSASLError( stanza ); disconnect( ConnAuthenticationFailed ); } else if( ( stanza->name() == "success" ) && stanza->hasAttribute( "xmlns", XMLNS_STREAM_SASL ) ) { logInstance().log( LogLevelDebug, LogAreaClassClient, "SASL authentication successful" ); setAuthed( true ); header(); } else { if( ( stanza->name() == "iq" ) && stanza->hasAttribute( "id", "bind" ) ) { processResourceBind( stanza ); } else if( ( stanza->name() == "iq" ) && stanza->hasAttribute( "id", "session" ) ) { processCreateSession( stanza ); } else return false; } return true; } int Client::getStreamFeatures( Stanza *stanza ) { if( stanza->name() != "stream:features" ) return 0; int features = 0; if( stanza->hasChild( "starttls", "xmlns", XMLNS_STREAM_TLS ) ) features |= StreamFeatureStartTls; if( stanza->hasChild( "mechanisms", "xmlns", XMLNS_STREAM_SASL ) ) features |= getSaslMechs( stanza->findChild( "mechanisms" ) ); if( stanza->hasChild( "bind", "xmlns", XMLNS_STREAM_BIND ) ) features |= StreamFeatureBind; if( stanza->hasChild( "session", "xmlns", XMLNS_STREAM_SESSION ) ) features |= StreamFeatureSession; if( stanza->hasChild( "auth", "xmlns", XMLNS_STREAM_IQAUTH ) ) features |= StreamFeatureIqAuth;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -