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

Language C# Lines 151
MD5 Hash 7ec3f32a96d9b01bdb2242ce6cf45ba8
Repository https://github.com/thecc4re/sipsorcery-mono.git View Raw File
  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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//-----------------------------------------------------------------------------
// Filename: XMPPAuthenticatedStream.cs
//
// Description: Represents the XMPP stream after the client has successfully authenticated
// with the XMPP server.
// 
// History:
// 13 Dec 2010	Aaron Clauson	Created.
//
// License: 
// This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
//
// Copyright (c) 2010 Aaron Clauson (aaron@sipsorcery.com), Hobart, Tasmania, Australia (www.sipsorcery.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
// the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
// disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of SIP Sorcery. 
// nor the names of its contributors may be used to endorse or promote products derived from this software without specific 
// prior written permission. 
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using SIPSorcery.Sys;
using log4net;

namespace SIPSorcery.XMPP
{
    public class XMPPAuthenticatedStream : XMPPStream
    {
        private static ILog logger = AppState.logger;

        private static XNamespace m_bindNS = BIND_NAMESPACE;
        private static XNamespace m_sessionNS = GOOGLE_SESSION_NAMESPACE;
        private static XNamespace m_phoneNS = GOOGLE_PHONE_SESSION_NAMESPACE;
        private static XNamespace m_transportNS = TRANSPORT_NAMESPACE;

        private bool m_isBound;

        public Action IsBound;
        private Dictionary<string, Action<XElement>> m_sessions = new Dictionary<string, Action<XElement>>();            // [id, Action(iq element)]
        private Dictionary<string, Action<string, XElement>> m_iqs = new Dictionary<string, Action<string, XElement>>(); // [id, Action(id, iq element)]

        public XMPPAuthenticatedStream(Stream stream) :
            base(stream)
        {
            IsTLS = true;
            IsAuthenticated = true;
            base.ElementReceived += Receive;
        }

        public void RegisterSession(string session, Action<XElement> handler)
        {
            m_sessions.Add(session, handler);
        }

        public void WriteElement(XElement element, Action<string, XElement> handler)
        {
            m_iqs.Add(element.Attribute("id").Value, handler);
            base.WriteElement(element);
        }

        private void Receive(XElement element)
        {
            //logger.Debug("XMPPAuthenticatedStream ElementReceived " + element.Name + ".");

            switch (element.Name.LocalName)
            {
                case "features":
                    if (Features != null && (from feature in Features where feature.Name == BIND_ELEMENT_NAME select feature).Count() > 0)
                    {
                        // Bind.
                        //Console.WriteLine("Binding is required.");
                        string bindingID = Crypto.GetRandomString(6);
                        XElement bindElement = new XElement(JabberClientNS + IQ_ELEMENT_NAME,
                            new XAttribute("id", bindingID),
                            new XAttribute("type", "set"),
                            new XElement(m_bindNS + BIND_ELEMENT_NAME));
                        //Console.WriteLine(bindElement);
                        WriteElement(bindElement);
                    }
                    break;
                case "iq":
                    if(!m_isBound)
                    {
                        IQBinding(element);
                    }
                    else
                    {
                        string iqID = element.Attribute("id").Value;
                        if (m_iqs.ContainsKey(iqID))
                        {
                            m_iqs[iqID](iqID, element);
                            m_iqs.Remove(iqID);
                        }
                        else
                        {
                            string sessionID = element.Element(m_sessionNS + "session").Attribute("id").Value;
                            if (m_sessions.ContainsKey(sessionID))
                            {
                                m_sessions[sessionID](element);
                            }
                            else
                            {
                                logger.Warn("XMPPAuthenticatedStream session id with " + sessionID + " was not matched.");
                            }
                        }
                    }
                    break;
                default:
                    logger.Warn("XMPPAuthenticatedStream node " + element.Name.LocalName + " was not recognised.");
                    break;
            }
        }

        private void IQBinding(XElement element)
        {
            m_isBound = true;
            JID = element.Element(m_bindNS + "bind").Element(m_bindNS + "jid").Value;
            logger.Debug("XMPPAuthenticatedStream JID=" + JID);
            IsBound();
        }

        public void SendMessage(string to, string message)
        {
            XElement messageElement = new XElement(JabberClientNS + MESSAGE_ELEMENT_NAME,
                                            new XAttribute("to", to), new XElement(JabberClientNS + "body", message));
            WriteElement(messageElement);
        }
    }
}
Back to Top