PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-xmpp/SIPSorcery.XMPP/XMPPAuthenticatedStream.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 150 lines | 102 code | 12 blank | 36 comment | 8 complexity | 7ec3f32a96d9b01bdb2242ce6cf45ba8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------------
  2. // Filename: XMPPAuthenticatedStream.cs
  3. //
  4. // Description: Represents the XMPP stream after the client has successfully authenticated
  5. // with the XMPP server.
  6. //
  7. // History:
  8. // 13 Dec 2010 Aaron Clauson Created.
  9. //
  10. // License:
  11. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  12. //
  13. // Copyright (c) 2010 Aaron Clauson (aaron@sipsorcery.com), Hobart, Tasmania, Australia (www.sipsorcery.com)
  14. // All rights reserved.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  17. // the following conditions are met:
  18. //
  19. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  20. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  21. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of SIP Sorcery.
  22. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  23. // prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  26. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  28. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  29. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. // POSSIBILITY OF SUCH DAMAGE.
  32. //-----------------------------------------------------------------------------
  33. using System;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.Linq;
  37. using System.Text;
  38. using System.Threading;
  39. using System.Xml;
  40. using System.Xml.Linq;
  41. using System.Xml.Schema;
  42. using SIPSorcery.Sys;
  43. using log4net;
  44. namespace SIPSorcery.XMPP
  45. {
  46. public class XMPPAuthenticatedStream : XMPPStream
  47. {
  48. private static ILog logger = AppState.logger;
  49. private static XNamespace m_bindNS = BIND_NAMESPACE;
  50. private static XNamespace m_sessionNS = GOOGLE_SESSION_NAMESPACE;
  51. private static XNamespace m_phoneNS = GOOGLE_PHONE_SESSION_NAMESPACE;
  52. private static XNamespace m_transportNS = TRANSPORT_NAMESPACE;
  53. private bool m_isBound;
  54. public Action IsBound;
  55. private Dictionary<string, Action<XElement>> m_sessions = new Dictionary<string, Action<XElement>>(); // [id, Action(iq element)]
  56. private Dictionary<string, Action<string, XElement>> m_iqs = new Dictionary<string, Action<string, XElement>>(); // [id, Action(id, iq element)]
  57. public XMPPAuthenticatedStream(Stream stream) :
  58. base(stream)
  59. {
  60. IsTLS = true;
  61. IsAuthenticated = true;
  62. base.ElementReceived += Receive;
  63. }
  64. public void RegisterSession(string session, Action<XElement> handler)
  65. {
  66. m_sessions.Add(session, handler);
  67. }
  68. public void WriteElement(XElement element, Action<string, XElement> handler)
  69. {
  70. m_iqs.Add(element.Attribute("id").Value, handler);
  71. base.WriteElement(element);
  72. }
  73. private void Receive(XElement element)
  74. {
  75. //logger.Debug("XMPPAuthenticatedStream ElementReceived " + element.Name + ".");
  76. switch (element.Name.LocalName)
  77. {
  78. case "features":
  79. if (Features != null && (from feature in Features where feature.Name == BIND_ELEMENT_NAME select feature).Count() > 0)
  80. {
  81. // Bind.
  82. //Console.WriteLine("Binding is required.");
  83. string bindingID = Crypto.GetRandomString(6);
  84. XElement bindElement = new XElement(JabberClientNS + IQ_ELEMENT_NAME,
  85. new XAttribute("id", bindingID),
  86. new XAttribute("type", "set"),
  87. new XElement(m_bindNS + BIND_ELEMENT_NAME));
  88. //Console.WriteLine(bindElement);
  89. WriteElement(bindElement);
  90. }
  91. break;
  92. case "iq":
  93. if(!m_isBound)
  94. {
  95. IQBinding(element);
  96. }
  97. else
  98. {
  99. string iqID = element.Attribute("id").Value;
  100. if (m_iqs.ContainsKey(iqID))
  101. {
  102. m_iqs[iqID](iqID, element);
  103. m_iqs.Remove(iqID);
  104. }
  105. else
  106. {
  107. string sessionID = element.Element(m_sessionNS + "session").Attribute("id").Value;
  108. if (m_sessions.ContainsKey(sessionID))
  109. {
  110. m_sessions[sessionID](element);
  111. }
  112. else
  113. {
  114. logger.Warn("XMPPAuthenticatedStream session id with " + sessionID + " was not matched.");
  115. }
  116. }
  117. }
  118. break;
  119. default:
  120. logger.Warn("XMPPAuthenticatedStream node " + element.Name.LocalName + " was not recognised.");
  121. break;
  122. }
  123. }
  124. private void IQBinding(XElement element)
  125. {
  126. m_isBound = true;
  127. JID = element.Element(m_bindNS + "bind").Element(m_bindNS + "jid").Value;
  128. logger.Debug("XMPPAuthenticatedStream JID=" + JID);
  129. IsBound();
  130. }
  131. public void SendMessage(string to, string message)
  132. {
  133. XElement messageElement = new XElement(JabberClientNS + MESSAGE_ELEMENT_NAME,
  134. new XAttribute("to", to), new XElement(JabberClientNS + "body", message));
  135. WriteElement(messageElement);
  136. }
  137. }
  138. }