예제 #1
0
        public SIPApp(TransportInfo transport)
        {
            log4net.Config.XmlConfigurator.Configure();
            TempBuffer = new byte[4096];
            transport.Socket = transport.Type == ProtocolType.Tcp ? new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) : new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint localEP = new IPEndPoint(transport.Host, transport.Port);
            transport.Socket.Bind(localEP);

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint sendEP = sender;
            transport.Socket.BeginReceiveFrom(TempBuffer, 0, TempBuffer.Length, SocketFlags.None, ref sendEP, ReceiveDataCB, sendEP);
            Transport = transport;
            Useragents = new List<UserAgent>();
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:SIPLibDriver.SIPApp"/> class.
        /// </summary>
        /// <param name="transport">Takes in the specified trasnport object and creates the necessary sockets.</param>
        public SIPApp(TransportInfo transport)
        {
            log4net.Config.XmlConfigurator.Configure();
            this.TempBuffer = new byte[4096];
            if (transport.Type == ProtocolType.Tcp)
            {
                transport.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            else
            {
                transport.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            }
            IPEndPoint localEP = new IPEndPoint(transport.Host, transport.Port);
            transport.Socket.Bind(localEP);

            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint sendEP = (EndPoint)sender;
            transport.Socket.BeginReceiveFrom(TempBuffer, 0, TempBuffer.Length, SocketFlags.None, ref sendEP, new AsyncCallback(ReceiveDataCB), sendEP);
            this.Transport = transport;
        }
예제 #3
0
        /// <summary>
        /// Creates a client transaction
        /// </summary>
        /// <param name="stack">The SIP stack to use.</param>
        /// <param name="app">The associated useragent / application.</param>
        /// <param name="request">The SIP request.</param>
        /// <param name="transport">A TransportInfo object representing transmission medium.</param>
        /// <param name="remote">The remote.</param>
        /// <returns>Transaction.</returns>
        public static Transaction CreateClient(SIPStack stack, UserAgent app, Message request, TransportInfo transport,
                                               string remote)
        {
            Transaction t;

            if (request.Method == "INVITE")
            {
                t = new InviteClientTransaction(app);
            }
            else
            {
                t = new ClientTransaction(app);
            }
            t.Stack     = stack;
            t.App       = app;
            t.Request   = request;
            t.Transport = transport;
            t.Remote    = remote;

            if (request.Headers.ContainsKey("Via") && request.First("Via").Attributes.ContainsKey("branch"))
            {
                t.Branch = request.First("Via").Attributes["branch"];
            }
            else
            {
                t.Branch = CreateBranch(request, false);
            }
            t.ID = CreateId(t.Branch, request.Method);
            stack.Transactions[t.ID] = t;
            if (request.Method == "INVITE")
            {
                ((InviteClientTransaction)t).Start();
            }
            else
            {
                ((ClientTransaction)t).Start();
            }
            return(t);
        }
예제 #4
0
        /// <summary>
        /// Creates a server transaction
        /// </summary>
        /// <param name="stack">The SIP stack to use.</param>
        /// <param name="app">The associated useragent / application.</param>
        /// <param name="request">The SIP request.</param>
        /// <param name="transport">A TransportInfo object representing transmission medium.</param>
        /// <param name="tag">The tag.</param>
        /// <param name="start">Not used.</param>
        /// <returns>Transaction.</returns>
        public static Transaction CreateServer(SIPStack stack, UserAgent app, Message request, TransportInfo transport,
                                               string tag, Boolean start = true)
        {
            Transaction t;

            if (request.Method == "INVITE")
            {
                t = new InviteServerTransaction(app);
            }
            else
            {
                t = new ServerTransaction(app);
            }
            t.Stack     = stack;
            t.App       = app;
            t.Request   = request;
            t.Transport = transport;
            t.Tag       = tag;
            t.Remote    = request.First("Via").ViaUri.HostPort();
            if (request.Headers.ContainsKey("Via") && request.First("Via").Attributes.ContainsKey("branch"))
            {
                t.Branch = request.First("Via").Attributes["branch"];
            }
            else
            {
                t.Branch = CreateBranch(request, true);
            }
            t.ID = CreateId(t.Branch, request.Method);
            stack.Transactions[t.ID] = t;
            if (request.Method == "INVITE")
            {
                ((InviteServerTransaction)t).Start();
            }
            else
            {
                ((ServerTransaction)t).Start();
            }
            return(t);
        }
예제 #5
0
        /// <summary>
        /// Method to send SIP messages. Handles appending of necessary information and routing rules.
        /// </summary>
        /// <param name="data">The data to send (can be a SIPMessage or string representing a SIP message).</param>
        /// <param name="dest">The destination (can be a SIPURI or a string).</param>
        /// <param name="transport">Optional TransportInfo object.</param>
        public void Send(object data, object dest = null, TransportInfo transport = null)
        {
            string destinationHost = "";
            int destinationPort = 0;
            string finalData;
            if (ProxyHost != null && ProxyPort != 0)
            {
                destinationHost = ProxyHost;
                destinationPort = Convert.ToInt32(ProxyPort);
            }
            else if (dest is SIPURI)
            {
                SIPURI destination = (SIPURI) dest;
                if (destination.Host.Length == 0)
                {
                    Debug.Assert(false, String.Format("No host in destination URI \n{0}\n", destination));
                }
                else
                {
                    destinationHost = destination.Host;
                    destinationPort = destination.Port;
                }
            }
            else if (dest is string)
            {
                string destination = (string) (dest);
                string[] parts = destination.Split(':');
                destinationHost = parts[0];
                destinationPort = Convert.ToInt32(parts[1]);
            }

            if (data is Message)
            {
                Message m = (Message) data;
                //TODO: Fix stripping of record-route
                //if (m.Headers.ContainsKey("Record-Route")) m.Headers.Remove("Record-Route");
                //if (!Helpers.IsRequest(m) && m.Is2XX() && m.First("CSeq").Method.Contains("INVITE"))
                //{
                //    m.Headers.Remove("Record-Route");
                //}
                if (Helpers.IsRequest(m) && m.Method == "ACK")
                {
                    _log.Info("Sending ACK");
                }
                m.InsertHeader(new Header(_userAgentName, "User-Agent"));
                if (m.Headers.ContainsKey("Route"))
                {
                }
                else
                {
                    if (ServiceRoute != null)
                    {
                        if (
                            !(Helpers.IsRequest(m) &&
                              ((m.Method.ToLower().Contains("register") ||
                                (m.Method.ToLower().Contains("ack") || (m.Method.ToLower().Contains("bye")))))))
                        {
                            m.Headers["Route"] = ServiceRoute;
                        }
                    }
                    else if (!string.IsNullOrEmpty(ProxyHost))
                    {
                        m.InsertHeader(new Header("<sip:" + ProxyHost + ":" + ProxyPort + ">", "Route"));
                    }
                }
                if (!string.IsNullOrEmpty(m.Method))
                {
                    // TODO: Multicast handling of Maddr
                }
                else if (m.ResponseCode > 0)
                {
                    if (dest == null)
                    {
                        destinationHost = m.Headers["Via"][0].ViaUri.Host;
                        destinationPort = m.Headers["Via"][0].ViaUri.Port;
                    }
                }
                ////TODO FIX HACK
                //string route_list = "";
                //if (m.headers.ContainsKey("Route"))
                //{
                //foreach (Header h in m.headers["Route"])
                //{
                //    route_list = route_list + h.value.ToString() + ",";
                //}
                //route_list = route_list.Remove(route_list.Length - 1);
                //Header temp = new Header(route_list, "Temp");
                //temp.value = route_list;
                //temp.name = "Route";
                //m.headers.Remove("Route");
                //m.insertHeader(temp);
                //}

                finalData = m.ToString();
            }
            else
            {
                finalData = (string) data;
            }
            App.Send(finalData, destinationHost, destinationPort, this);
        }
예제 #6
0
        /// <summary>
        /// Method to send SIP messages. Handles appending of necessary information and routing rules.
        /// </summary>
        /// <param name="data">The data to send (can be a SIPMessage or string representing a SIP message).</param>
        /// <param name="dest">The destination (can be a SIPURI or a string).</param>
        /// <param name="transport">Optional TransportInfo object.</param>
        public void Send(object data, object dest = null, TransportInfo transport = null)
        {
            string destinationHost = "";
            int    destinationPort = 0;
            string finalData;

            if (ProxyHost != null && ProxyPort != 0)
            {
                destinationHost = ProxyHost;
                destinationPort = Convert.ToInt32(ProxyPort);
            }
            else if (dest is SIPURI)
            {
                SIPURI destination = (SIPURI)dest;
                if (destination.Host.Length == 0)
                {
                    Debug.Assert(false, String.Format("No host in destination URI \n{0}\n", destination));
                }
                else
                {
                    destinationHost = destination.Host;
                    destinationPort = destination.Port;
                }
            }
            else if (dest is string)
            {
                string   destination = (string)(dest);
                string[] parts       = destination.Split(':');
                destinationHost = parts[0];
                destinationPort = Convert.ToInt32(parts[1]);
            }

            if (data is Message)
            {
                Message m = (Message)data;
                //TODO: Fix stripping of record-route
                //if (m.Headers.ContainsKey("Record-Route")) m.Headers.Remove("Record-Route");
                //if (!Helpers.IsRequest(m) && m.Is2XX() && m.First("CSeq").Method.Contains("INVITE"))
                //{
                //    m.Headers.Remove("Record-Route");
                //}
                if (Helpers.IsRequest(m) && m.Method == "ACK")
                {
                    _log.Info("Sending ACK");
                }
                m.InsertHeader(new Header(_userAgentName, "User-Agent"));
                if (m.Headers.ContainsKey("Route"))
                {
                }
                else
                {
                    if (ServiceRoute != null)
                    {
                        if (
                            !(Helpers.IsRequest(m) &&
                              ((m.Method.ToLower().Contains("register") ||
                                (m.Method.ToLower().Contains("ack") || (m.Method.ToLower().Contains("bye")))))))
                        {
                            m.Headers["Route"] = ServiceRoute;
                        }
                    }
                    else if (!string.IsNullOrEmpty(ProxyHost))
                    {
                        m.InsertHeader(new Header("<sip:" + ProxyHost + ":" + ProxyPort + ">", "Route"));
                    }
                }
                if (!string.IsNullOrEmpty(m.Method))
                {
                    // TODO: Multicast handling of Maddr
                }
                else if (m.ResponseCode > 0)
                {
                    if (dest == null)
                    {
                        destinationHost = m.Headers["Via"][0].ViaUri.Host;
                        destinationPort = m.Headers["Via"][0].ViaUri.Port;
                    }
                }
                ////TODO FIX HACK
                //string route_list = "";
                //if (m.headers.ContainsKey("Route"))
                //{
                //foreach (Header h in m.headers["Route"])
                //{
                //    route_list = route_list + h.value.ToString() + ",";
                //}
                //route_list = route_list.Remove(route_list.Length - 1);
                //Header temp = new Header(route_list, "Temp");
                //temp.value = route_list;
                //temp.name = "Route";
                //m.headers.Remove("Route");
                //m.insertHeader(temp);
                //}

                finalData = m.ToString();
            }
            else
            {
                finalData = (string)data;
            }
            App.Send(finalData, destinationHost, destinationPort, this);
        }