Inheritance: SIPChannel
Exemple #1
0
        static void Main(string[] args)
        {
            try
            {
                // Configure the SIP transport layer.
                _sipTransport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());

                if (_sipSocketsNode != null)
                {
                    // Set up the SIP channels based on the app.config file.
                    List<SIPChannel> sipChannels = SIPTransportConfig.ParseSIPChannelsNode(_sipSocketsNode);
                    _sipTransport.AddSIPChannel(sipChannels);
                }
                else
                {
                    // Use default options to set up a SIP channel.
                    int port = FreePort.FindNextAvailableUDPPort(_defaultSIPUdpPort);
                    var sipChannel = new SIPUDPChannel(new IPEndPoint(_defaultLocalAddress, port));
                    _sipTransport.AddSIPChannel(sipChannel);
                }

                // Wire up the transport layer so SIP requests and responses have somewhere to go.
                _sipTransport.SIPTransportRequestReceived += SIPTransportRequestReceived;
                _sipTransport.SIPTransportResponseReceived += SIPTransportResponseReceived;

                // If you want to see ALL the nitty gritty SIP traffic wire up the events below.
                //_sipTransport.SIPBadRequestInTraceEvent += SIPBadRequestInTraceEvent;
                //_sipTransport.SIPBadResponseInTraceEvent += SIPBadResponseInTraceEvent;
                //_sipTransport.SIPRequestInTraceEvent += SIPRequestInTraceEvent;
                //_sipTransport.SIPRequestOutTraceEvent += SIPRequestOutTraceEvent;
                //_sipTransport.SIPResponseInTraceEvent += SIPResponseInTraceEvent;
                //_sipTransport.SIPResponseOutTraceEvent += SIPResponseOutTraceEvent;

                ManualResetEvent mre = new ManualResetEvent(false);
                mre.WaitOne();
            }
            catch (Exception excp)
            {
                Console.WriteLine("Exception Main. " + excp);
            }
            finally
            {
                Console.WriteLine("Press any key to exit...");
                Console.ReadLine();
            }
        }
        public Dictionary<string, SIPDispatcherJob> Load(SIPTransport sipTransport)
        {
            try
            {
                Dictionary<string, SIPDispatcherJob> dispatcherJobs = new Dictionary<string, SIPDispatcherJob>();

                if (sipTransport == null)
                {
                    SIPChannel dispatcherChannel = new SIPUDPChannel(new IPEndPoint(IPAddress.Loopback, 0));
                    sipTransport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine(), dispatcherChannel, true);
                }

                foreach (XmlNode dispatcherNode in m_configNode.ChildNodes)
                {
                    string jobType = dispatcherNode.Attributes.GetNamedItem("class").Value;
                    string jobKey = dispatcherNode.Attributes.GetNamedItem("key").Value;

                    if (!jobKey.IsNullOrBlank() && !jobType.IsNullOrBlank())
                    {
                        SIPDispatcherJob job = SIPDispatcherJobFactory.CreateJob(jobType, dispatcherNode, sipTransport);
                        if (job != null && !dispatcherJobs.ContainsKey(jobKey))
                        {
                            ThreadPool.QueueUserWorkItem(delegate { job.Start(); });
                            dispatcherJobs.Add(jobKey, job);
                        }
                    }
                    else
                    {
                        logger.Warn("The job key or class were empty for a SIPDispatcherJob node.\n" + dispatcherNode.OuterXml);
                    }
                }

                return dispatcherJobs;
            }
            catch (Exception dispatcherExcp)
            {
                logger.Error("Exception StatelessProxyCore Starting Dispatcher. " + dispatcherExcp.Message);
                return null;
            }
        }
        public static List <SIPChannel> ParseSIPChannelsNode(XmlNode sipChannelsNode, int port = 0)
        {
            var sipChannels = new List <SIPChannel>();

            foreach (XmlNode sipSocketNode in sipChannelsNode.ChildNodes)
            {
                logger.Debug("Creating SIP Channel for " + sipSocketNode.OuterXml + ".");

                var localSocket = sipSocketNode.InnerText;

                var protocol = SIPProtocolsEnum.udp;
                if (sipSocketNode.Attributes.GetNamedItem(SIP_PROTOCOL_PARAMETER) != null)
                {
                    protocol = SIPProtocolsType.GetProtocolType(sipSocketNode.Attributes.GetNamedItem(SIP_PROTOCOL_PARAMETER).Value);
                }

                var nodeSIPEndPoints = GetSIPEndPoints(localSocket, protocol, port);

                foreach (var sipEndPoint in nodeSIPEndPoints)
                {
                    try
                    {
                        switch (protocol)
                        {
                        case SIPProtocolsEnum.udp:
                        {
                            logger.Debug(" attempting to create SIP UDP channel for " + sipEndPoint.GetIPEndPoint() + ".");
                            var udpChannel = new SIPUDPChannel(sipEndPoint.GetIPEndPoint());
                            sipChannels.Add(udpChannel);
                        }
                        break;

                        case SIPProtocolsEnum.tcp:
                        {
                            logger.Debug(" attempting to create SIP TCP channel for " + sipEndPoint.GetIPEndPoint() + ".");

                            var tcpChannel = new SIPTCPChannel(sipEndPoint.GetIPEndPoint());
                            sipChannels.Add(tcpChannel);
                        }
                        break;

                        case SIPProtocolsEnum.tls:
                            if (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER) == null)
                            {
                                logger.Warn("Could not create SIPTLSChannel from XML configuration node as no " + CERTIFICATE_PATH_PARAMETER + " attribute was present.");
                            }
                            else
                            {
                                var certificateType = "machinestore";
                                if (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_TYPE_PARAMETER) != null)
                                {
                                    certificateType = sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_TYPE_PARAMETER).Value;
                                }

                                var certificatePath        = (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER) != null) ? sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER).Value : null;
                                var certificateKeyPassword = (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_KEY_PASSWORD_PARAMETER) != null) ? sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_KEY_PASSWORD_PARAMETER).Value : String.Empty;
                                logger.Debug(" attempting to create SIP TLS channel for " + sipEndPoint.GetIPEndPoint() + " and certificate type of " + certificateType + " at " + certificatePath + ".");
                                var certificate = LoadCertificate(certificateType, certificatePath, certificateKeyPassword);
                                if (certificate != null)
                                {
                                    var tlsChannel = new SIPTLSChannel(certificate, sipEndPoint.GetIPEndPoint());
                                    sipChannels.Add(tlsChannel);
                                }
                                else
                                {
                                    logger.Warn("A SIP TLS channel was not created because the certificate could not be loaded.");
                                }
                            }
                            break;

                        default:
                            logger.Warn("Could not create a SIP channel for protocol " + protocol + ".");
                            break;
                        }
                    }
                    catch (Exception excp)
                    {
                        logger.Warn("Exception SIPTransportConfig Adding SIP Channel for " + sipEndPoint + ". " + excp.Message);
                    }
                }
            }

            return(sipChannels);
        }
        public static List<SIPChannel> ParseSIPChannelsNode(XmlNode sipChannelsNode)
        {
            List<SIPChannel> sipChannels = new List<SIPChannel>();

            foreach (XmlNode sipSocketNode in sipChannelsNode.ChildNodes)
            {
                logger.Debug("Creating SIP Channel for " + sipSocketNode.OuterXml + ".");

                string localSocket = sipSocketNode.InnerText;

                SIPProtocolsEnum protocol = SIPProtocolsEnum.udp;
                if (sipSocketNode.Attributes.GetNamedItem(SIP_PROTOCOL_PARAMETER) != null)
                {
                    protocol = SIPProtocolsType.GetProtocolType(sipSocketNode.Attributes.GetNamedItem(SIP_PROTOCOL_PARAMETER).Value);
                }

                List<SIPEndPoint> nodeSIPEndPoints = GetSIPEndPoints(localSocket, protocol);

                foreach (SIPEndPoint sipEndPoint in nodeSIPEndPoints)
                {
                    try
                    {
                        if (protocol == SIPProtocolsEnum.udp)
                        {
                            logger.Debug(" attempting to create SIP UDP channel for " + sipEndPoint.GetIPEndPoint() + ".");
                            SIPUDPChannel udpChannel = new SIPUDPChannel(sipEndPoint.GetIPEndPoint());
                            sipChannels.Add(udpChannel);
                        }
                        else if (protocol == SIPProtocolsEnum.tcp)
                        {
                            logger.Debug(" attempting to create SIP TCP channel for " + sipEndPoint.GetIPEndPoint() + ".");

                            SIPTCPChannel tcpChannel = new SIPTCPChannel(sipEndPoint.GetIPEndPoint());
                            sipChannels.Add(tcpChannel);
                        }
                        else if (protocol == SIPProtocolsEnum.tls)
                        {
                            if (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER) == null)
                            {
                                logger.Warn("Could not create SIPTLSChannel from XML configuration node as no " + CERTIFICATE_PATH_PARAMETER + " attribute was present.");
                            }
                            else
                            {
                                string certificateType = "machinestore";
                                if (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_TYPE_PARAMETER) != null)
                                {
                                    certificateType = sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_TYPE_PARAMETER).Value;
                                }

                                string certificatePath = (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER) != null) ? sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_PATH_PARAMETER).Value : null;
                                string certificateKeyPassword = (sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_KEY_PASSWORD_PARAMETER) != null) ? sipSocketNode.Attributes.GetNamedItem(CERTIFICATE_KEY_PASSWORD_PARAMETER).Value : String.Empty;
                                logger.Debug(" attempting to create SIP TLS channel for " + sipEndPoint.GetIPEndPoint() + " and certificate type of " + certificateType + " at " + certificatePath + ".");
                                X509Certificate2 certificate = LoadCertificate(certificateType, certificatePath, certificateKeyPassword);
                                if (certificate != null)
                                {
                                    SIPTLSChannel tlsChannel = new SIPTLSChannel(certificate, sipEndPoint.GetIPEndPoint());
                                    sipChannels.Add(tlsChannel);
                                }
                                else
                                {
                                    logger.Warn("A SIP TLS channel was not created because the certificate could not be loaded.");
                                }
                            }
                        }
                        else
                        {
                            logger.Warn("Could not create a SIP channel for protocol " + protocol + ".");
                        }
                    }
                    catch (Exception excp)
                    {
                        logger.Warn("Exception SIPTransportConfig Adding SIP Channel for " + sipEndPoint.ToString() + ". " + excp.Message);
                    }
                }
            }

            return sipChannels;
        }
Exemple #5
0
        /// <summary>
        /// Initialises the SIP transport layer.
        /// </summary>
        private void InitialiseSIP()
        {
            // Configure the SIP transport layer.
            m_sipTransport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());

            if (m_sipSocketsNode != null)
            {
                // Set up the SIP channels based on the app.config file.
                List<SIPChannel> sipChannels = SIPTransportConfig.ParseSIPChannelsNode(m_sipSocketsNode);
                m_sipTransport.AddSIPChannel(sipChannels);
            }
            else
            {
                // Use default options to set up a SIP channel.
                int port = FreePort.FindNextAvailableUDPPort(_defaultSIPUdpPort);
                var sipChannel = new SIPUDPChannel(new IPEndPoint(_defaultLocalAddress, port));
                m_sipTransport.AddSIPChannel(sipChannel);
            }

            // Wire up the transport layer so incoming SIP requests have somewhere to go.
            m_sipTransport.SIPTransportRequestReceived += SIPTransportRequestReceived;

            // Log all SIP packets received to a log file.
            m_sipTransport.SIPRequestInTraceEvent += (localSIPEndPoint, endPoint, sipRequest) => { _sipTraceLogger.Debug("Request Received : " + localSIPEndPoint + "<-" + endPoint + "\r\n" + sipRequest.ToString()); };
            m_sipTransport.SIPRequestOutTraceEvent += (localSIPEndPoint, endPoint, sipRequest) => { _sipTraceLogger.Debug("Request Sent: " + localSIPEndPoint + "->" + endPoint + "\r\n" + sipRequest.ToString()); };
            m_sipTransport.SIPResponseInTraceEvent += (localSIPEndPoint, endPoint, sipResponse) => { _sipTraceLogger.Debug("Response Received: " + localSIPEndPoint + "<-" + endPoint + "\r\n" + sipResponse.ToString()); };
            m_sipTransport.SIPResponseOutTraceEvent += (localSIPEndPoint, endPoint, sipResponse) => { _sipTraceLogger.Debug("Response Sent: " + localSIPEndPoint + "->" + endPoint + "\r\n" + sipResponse.ToString()); };
        }
        static void Main(string[] args)
        {
            try
            {
                if (args == null || args.Count() == 0)
                {
                    Console.WriteLine(USAGE_STRING);
                }
                else
                {
                    bool validArgs = ParseArgs(args);

                    if (validArgs)
                    {
                        Console.WriteLine("XMPP Test Console:");

                        m_sipTransport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());
                        SIPUDPChannel udpChannel = new SIPUDPChannel(new IPEndPoint(m_ipAddress, m_port));
                        m_sipTransport.AddSIPChannel(udpChannel);
                        m_sipTransport.SIPTransportRequestReceived += SIPTransportRequestReceived;

                        Console.WriteLine("Waiting for SIP INVITE...");

                        ManualResetEvent mre = new ManualResetEvent(false);
                        mre.WaitOne();
                    }
                }
            }
            catch (Exception excp)
            {
                Console.WriteLine("Exception Main. " + excp.Message);
            }
        }