You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_XMPP_XMPPENGINEIMPL_H_
  28. #define TALK_XMPP_XMPPENGINEIMPL_H_
  29. #include <sstream>
  30. #include <vector>
  31. #include "talk/xmpp/xmppengine.h"
  32. #include "talk/xmpp/xmppstanzaparser.h"
  33. namespace buzz {
  34. class XmppLoginTask;
  35. class XmppEngine;
  36. class XmppIqEntry;
  37. class SaslHandler;
  38. class SaslMechanism;
  39. //! The XMPP connection engine.
  40. //! This engine implements the client side of the 'core' XMPP protocol.
  41. //! To use it, register an XmppOutputHandler to handle socket output
  42. //! and pass socket input to HandleInput. Then application code can
  43. //! set up the connection with a user, password, and other settings,
  44. //! and then call Connect() to initiate the connection.
  45. //! An application can listen for events and receive stanzas by
  46. //! registering an XmppStanzaHandler via AddStanzaHandler().
  47. class XmppEngineImpl : public XmppEngine {
  48. public:
  49. XmppEngineImpl();
  50. virtual ~XmppEngineImpl();
  51. // SOCKET INPUT AND OUTPUT ------------------------------------------------
  52. //! Registers the handler for socket output
  53. virtual XmppReturnStatus SetOutputHandler(XmppOutputHandler *pxoh);
  54. //! Provides socket input to the engine
  55. virtual XmppReturnStatus HandleInput(const char* bytes, size_t len);
  56. //! Advises the engine that the socket has closed
  57. virtual XmppReturnStatus ConnectionClosed(int subcode);
  58. // SESSION SETUP ---------------------------------------------------------
  59. //! Indicates the (bare) JID for the user to use.
  60. virtual XmppReturnStatus SetUser(const Jid& jid);
  61. //! Get the login (bare) JID.
  62. virtual const Jid& GetUser();
  63. //! Indicates the autentication to use. Takes ownership of the object.
  64. virtual XmppReturnStatus SetSaslHandler(SaslHandler* sasl_handler);
  65. //! Sets whether TLS will be used within the connection (default true).
  66. virtual XmppReturnStatus SetTls(TlsOptions use_tls);
  67. //! Sets an alternate domain from which we allows TLS certificates.
  68. //! This is for use in the case where a we want to allow a proxy to
  69. //! serve up its own certificate rather than one owned by the underlying
  70. //! domain.
  71. virtual XmppReturnStatus SetTlsServer(const std::string& proxy_hostname,
  72. const std::string& proxy_domain);
  73. //! Gets whether TLS will be used within the connection.
  74. virtual TlsOptions GetTls();
  75. //! Sets the request resource name, if any (optional).
  76. //! Note that the resource name may be overridden by the server; after
  77. //! binding, the actual resource name is available as part of FullJid().
  78. virtual XmppReturnStatus SetRequestedResource(const std::string& resource);
  79. //! Gets the request resource name.
  80. virtual const std::string& GetRequestedResource();
  81. //! Sets language
  82. virtual void SetLanguage(const std::string& lang) {
  83. lang_ = lang;
  84. }
  85. // SESSION MANAGEMENT ---------------------------------------------------
  86. //! Set callback for state changes.
  87. virtual XmppReturnStatus SetSessionHandler(XmppSessionHandler* handler);
  88. //! Initiates the XMPP connection.
  89. //! After supplying connection settings, call this once to initiate,
  90. //! (optionally) encrypt, authenticate, and bind the connection.
  91. virtual XmppReturnStatus Connect();
  92. //! The current engine state.
  93. virtual State GetState() { return state_; }
  94. //! Returns true if the connection is encrypted (under TLS)
  95. virtual bool IsEncrypted() { return encrypted_; }
  96. //! The error code.
  97. //! Consult this after XmppOutputHandler.OnClose().
  98. virtual Error GetError(int *subcode) {
  99. if (subcode) {
  100. *subcode = subcode_;
  101. }
  102. return error_code_;
  103. }
  104. //! The stream:error stanza, when the error is XmppEngine::ERROR_STREAM.
  105. //! Notice the stanza returned is owned by the XmppEngine and
  106. //! is deleted when the engine is destroyed.
  107. virtual const XmlElement* GetStreamError() { return stream_error_.get(); }
  108. //! Closes down the connection.
  109. //! Sends CloseConnection to output, and disconnects and registered
  110. //! session handlers. After Disconnect completes, it is guaranteed
  111. //! that no further callbacks will be made.
  112. virtual XmppReturnStatus Disconnect();
  113. // APPLICATION USE -------------------------------------------------------
  114. //! Adds a listener for session events.
  115. //! Stanza delivery is chained to session handlers; the first to
  116. //! return 'true' is the last to get each stanza.
  117. virtual XmppReturnStatus AddStanzaHandler(XmppStanzaHandler* handler,
  118. XmppEngine::HandlerLevel level);
  119. //! Removes a listener for session events.
  120. virtual XmppReturnStatus RemoveStanzaHandler(XmppStanzaHandler* handler);
  121. //! Sends a stanza to the server.
  122. virtual XmppReturnStatus SendStanza(const XmlElement* stanza);
  123. //! Sends raw text to the server
  124. virtual XmppReturnStatus SendRaw(const std::string& text);
  125. //! Sends an iq to the server, and registers a callback for the result.
  126. //! Returns the cookie passed to the result handler.
  127. virtual XmppReturnStatus SendIq(const XmlElement* stanza,
  128. XmppIqHandler* iq_handler,
  129. XmppIqCookie* cookie);
  130. //! Unregisters an iq callback handler given its cookie.
  131. //! No callback will come to this handler after it's unregistered.
  132. virtual XmppReturnStatus RemoveIqHandler(XmppIqCookie cookie,
  133. XmppIqHandler** iq_handler);
  134. //! Forms and sends an error in response to the given stanza.
  135. //! Swaps to and from, sets type to "error", and adds error information
  136. //! based on the passed code. Text is optional and may be STR_EMPTY.
  137. virtual XmppReturnStatus SendStanzaError(const XmlElement* pelOriginal,
  138. XmppStanzaError code,
  139. const std::string& text);
  140. //! The fullly bound JID.
  141. //! This JID is only valid after binding has succeeded. If the value
  142. //! is JID_NULL, the binding has not succeeded.
  143. virtual const Jid& FullJid() { return bound_jid_; }
  144. //! The next unused iq id for this connection.
  145. //! Call this when building iq stanzas, to ensure that each iq
  146. //! gets its own unique id.
  147. virtual std::string NextId();
  148. private:
  149. friend class XmppLoginTask;
  150. friend class XmppIqEntry;
  151. void IncomingStanza(const XmlElement *stanza);
  152. void IncomingStart(const XmlElement *stanza);
  153. void IncomingEnd(bool isError);
  154. void InternalSendStart(const std::string& domainName);
  155. void InternalSendStanza(const XmlElement* stanza);
  156. std::string ChooseBestSaslMechanism(
  157. const std::vector<std::string>& mechanisms, bool encrypted);
  158. SaslMechanism* GetSaslMechanism(const std::string& name);
  159. void SignalBound(const Jid& fullJid);
  160. void SignalStreamError(const XmlElement* streamError);
  161. void SignalError(Error errorCode, int subCode);
  162. bool HasError();
  163. void DeleteIqCookies();
  164. bool HandleIqResponse(const XmlElement* element);
  165. void StartTls(const std::string& domain);
  166. void RaiseReset() { raised_reset_ = true; }
  167. class StanzaParseHandler : public XmppStanzaParseHandler {
  168. public:
  169. StanzaParseHandler(XmppEngineImpl* outer) : outer_(outer) {}
  170. virtual ~StanzaParseHandler() {}
  171. virtual void StartStream(const XmlElement* stream) {
  172. outer_->IncomingStart(stream);
  173. }
  174. virtual void Stanza(const XmlElement* stanza) {
  175. outer_->IncomingStanza(stanza);
  176. }
  177. virtual void EndStream() {
  178. outer_->IncomingEnd(false);
  179. }
  180. virtual void XmlError() {
  181. outer_->IncomingEnd(true);
  182. }
  183. private:
  184. XmppEngineImpl* const outer_;
  185. };
  186. class EnterExit {
  187. public:
  188. EnterExit(XmppEngineImpl* engine);
  189. ~EnterExit();
  190. private:
  191. XmppEngineImpl* engine_;
  192. State state_;
  193. Error error_;
  194. };
  195. friend class StanzaParseHandler;
  196. friend class EnterExit;
  197. StanzaParseHandler stanza_parse_handler_;
  198. XmppStanzaParser stanza_parser_;
  199. // state
  200. int engine_entered_;
  201. Jid user_jid_;
  202. std::string password_;
  203. std::string requested_resource_;
  204. TlsOptions tls_option_;
  205. std::string tls_server_hostname_;
  206. std::string tls_server_domain_;
  207. talk_base::scoped_ptr<XmppLoginTask> login_task_;
  208. std::string lang_;
  209. int next_id_;
  210. Jid bound_jid_;
  211. State state_;
  212. bool encrypted_;
  213. Error error_code_;
  214. int subcode_;
  215. talk_base::scoped_ptr<XmlElement> stream_error_;
  216. bool raised_reset_;
  217. XmppOutputHandler* output_handler_;
  218. XmppSessionHandler* session_handler_;
  219. XmlnsStack xmlns_stack_;
  220. typedef std::vector<XmppStanzaHandler*> StanzaHandlerVector;
  221. talk_base::scoped_ptr<StanzaHandlerVector> stanza_handlers_[HL_COUNT];
  222. typedef std::vector<XmppIqEntry*> IqEntryVector;
  223. talk_base::scoped_ptr<IqEntryVector> iq_entries_;
  224. talk_base::scoped_ptr<SaslHandler> sasl_handler_;
  225. talk_base::scoped_ptr<std::stringstream> output_;
  226. };
  227. } // namespace buzz
  228. #endif // TALK_XMPP_XMPPENGINEIMPL_H_