Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

SIP Stack with SIP Proxy - (VOIP)

, 11 Jun 2007
C# implementation of SIP
SIP_Proxy_demo.zip
SIP_Proxy_demo
SIP_Proxy_demo.suo
SIP_Proxy_demo
bin
Debug
LumiSoft.Net.dll
SIP_Proxy_demo.exe
SIP_Proxy_demo.vshost.exe
dep
LumiSoft.Net.dll
LumiSoft.Net.pdb
Properties
Settings.settings
Resources
add.ico
app.ico
delete.ico
edit.ico
error.ico
info.ico
refresh.ico
rule.ico
server_running.ico
server_stopped.ico
viewmessages.ico
Stack.zip
Net
docs
dns
dns_records.jpg
dns_records.vsd
Net
_junk
_Obsolete
AUTH
bin
Release
LumiSoft.Net.dll
Data
Dns
Client
FTP
Client
Server
HTTP
Server
ICMP
IMAP
Client
Server
IO
Log
LumiSoft.Net
Mime
vCard
Net.csproj.user
Net.suo
NNTP
Client
POP3
Client
Server
RTP
SDP
ServersCore
SIP
Client
Message
Proxy
Stack
SMTP
Client
Server
STUN
Client
Message
URI
using System;
using System.Collections.Generic;
using System.Text;

using LumiSoft.Net.SIP.Message;
using LumiSoft.Net.SIP.Stack;

namespace LumiSoft.Net.SIP.Proxy
{
    /// <summary>
    /// This class implements SIP b2bua(back to back user agent). Defined in RFC 3261.
    /// </summary>
    public class SIP_B2BUA : IDisposable
    {
        private SIP_ProxyCore        m_pProxy     = null;
        private List<SIP_B2BUA_Call> m_pCalls     = null;
        private bool                 m_IsDisposed = false;

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="owner">Onwer SIP proxy.</param>
        internal SIP_B2BUA(SIP_ProxyCore owner)
        {
            m_pProxy = owner; 
            m_pCalls = new List<SIP_B2BUA_Call>();
        }

        #region method Dispose

        /// <summary>
        /// Cleans up any resources being used.
        /// </summary>
        public void Dispose()
        {
            if(m_IsDisposed){
                return;
            }
            m_IsDisposed = true;

            // Terminate all calls.
            foreach(SIP_B2BUA_Call call in m_pCalls){
                call.Terminate();
            }
        }

        #endregion


        #region method OnRequestReceived

        /// <summary>
        /// This method is called when new request is received.
        /// </summary>
        /// <param name="e">Request event arguments.</param>
        public void OnRequestReceived(SIP_RequestReceivedEventArgs e)
        {
            SIP_Request request = e.Request;

            // We never should get CANCEL here, because we handle CANCEL in server transaction.
            if(request.Method == SIP_Methods.CANCEL){
                /* RFC 3261 9.2.
                    If the UAS did not find a matching transaction for the CANCEL
                    according to the procedure above, it SHOULD respond to the CANCEL
                    with a 481 (Call Leg/Transaction Does Not Exist).  
                */
                e.ServerTransaction.SendResponse(request.CreateResponse(SIP_ResponseCodes.x481_Call_Transaction_Does_Not_Exist));
            }
            // We never should ge BYE here, because transport layer must match it to dialog.
            else if(request.Method == SIP_Methods.BYE){
                /* RFC 3261 15.1.2.
                    If the BYE does not match an existing dialog, the UAS core SHOULD generate a 481
                    (Call/Transaction Does Not Exist) response and pass that to the server transaction.
                */
                e.ServerTransaction.SendResponse(request.CreateResponse(SIP_ResponseCodes.x481_Call_Transaction_Does_Not_Exist));
            }
            // We never should ge ACK here, because transport layer must match it to dialog.
            else if(request.Method == SIP_Methods.ACK){
                // Because ACK is response less request, skip it.
            }
            // B2BUA must respond to OPTIONS request, not to forward it.
            else if(request.Method == SIP_Methods.OPTIONS){               
                SIP_Response response = e.Request.CreateResponse(SIP_ResponseCodes.x200_Ok);                
                // Add Allow to non ACK response.
                if(e.Request.Method != SIP_Methods.ACK){
                    response.Allow.Add("INVITE,ACK,OPTIONS,CANCEL,BYE,PRACK,MESSAGE,UPDATE");
                }
                // Add Supported to 2xx non ACK response.
                if(response.StatusCodeType == SIP_StatusCodeType.Success && e.Request.Method != SIP_Methods.ACK){
                    response.Supported.Add("100rel,timer");
                }
                e.ServerTransaction.SendResponse(response);
            }
            // We never should get PRACK here, because transport layer must match it to dialog.
            else if(request.Method == SIP_Methods.PRACK){
                e.ServerTransaction.SendResponse(request.CreateResponse(SIP_ResponseCodes.x481_Call_Transaction_Does_Not_Exist));
            }
            // We never should get UPDATE here, because transport layer must match it to dialog.
            else if(request.Method == SIP_Methods.UPDATE){
                e.ServerTransaction.SendResponse(request.CreateResponse(SIP_ResponseCodes.x481_Call_Transaction_Does_Not_Exist));
            }
            else{
                /* draft-marjou-sipping-b2bua-00 4.1.3.
                    When the UAS of the B2BUA receives an upstream SIP request, its
                    associated UAC generates a new downstream SIP request with its new
                    Via, Max-Forwards, Call-Id, CSeq, and Contact header fields. Route
                    header fields of the upstream request are copied in the downstream
                    request, except the first Route header if it is under the
                    responsibility of the B2BUA.  Record-Route header fields of the
                    upstream request are not copied in the new downstream request, as
                    Record-Route is only meaningful for the upstream dialog.  The UAC
                    SHOULD copy other header fields and body from the upstream request
                    into this downstream request before sending it.
                */

                SIP_Request b2buaRequest = e.Request.Copy();
                b2buaRequest.Via.RemoveAll();
                b2buaRequest.MaxForwards = 70;                
                b2buaRequest.CallID = SIP_t_CallID.CreateCallID().CallID;
                b2buaRequest.CSeq.SequenceNumber = 1;
                b2buaRequest.Contact.RemoveAll();
                b2buaRequest.Contact.Add(m_pProxy.CreateContact(b2buaRequest.To.Address).ToStringValue());
                if(b2buaRequest.Route.Count > 0 && m_pProxy.IsLocalRoute(b2buaRequest.Route.GetTopMostValue())){
                    b2buaRequest.Route.RemoveTopMostValue();
                }                
                b2buaRequest.RecordRoute.RemoveAll();

                //--- Add/replace default fields. ------------------------------------------
                b2buaRequest.Allow.RemoveAll();
                b2buaRequest.Supported.RemoveAll();
                // Accept to non ACK,BYE request.
                if(request.Method != SIP_Methods.ACK && request.Method != SIP_Methods.BYE){
                    b2buaRequest.Allow.Add("INVITE,ACK,OPTIONS,CANCEL,BYE,PRACK");
                }
                // Supported to non ACK request. 
                if(request.Method != SIP_Methods.ACK){
                    b2buaRequest.Supported.Add("100rel,timer");
                }
                // Remove Require: header.
                b2buaRequest.Require.RemoveAll();

                // RFC 4028 7.4. For re-INVITE and UPDATE we need to add Session-Expires and Min-SE: headers.
                if(request.Method == SIP_Methods.INVITE || request.Method == SIP_Methods.UPDATE){
                    b2buaRequest.SessionExpires = new SIP_t_SessionExpires(m_pProxy.Stack.SessionExpries,"uac");
                    b2buaRequest.MinSE = new SIP_t_MinSE(m_pProxy.Stack.MinimumSessionExpries);
                }

                // Forward request.
                m_pProxy.ForwardRequest(true,e,b2buaRequest,false);
            }
        }
                
        #endregion

        #region method OnResponseReceived

        /// <summary>
        /// This method is called when new response is received.
        /// </summary>
        /// <param name="e">Response event arguments.</param>
        public void OnResponseReceived(SIP_ResponseReceivedEventArgs e)
        {
            // If we get response here, that means we have stray response, just do nothing.
            // All reponses must match to transactions, so we never should reach here.

            // FIX ME: probably we don't need it.
        }

        #endregion


        #region method AddCall

        /// <summary>
        /// Adds specified call to calls list.
        /// </summary>
        /// <param name="caller">Caller side dialog.</param>
        /// <param name="calee">Calee side dialog.</param>
        internal void AddCall(SIP_Dialog caller,SIP_Dialog calee)
        {
            lock(m_pCalls){
                m_pCalls.Add(new SIP_B2BUA_Call(this,caller,calee));
            }            
        }

        #endregion

        #region method RemoveCall

        /// <summary>
        /// Removes specified call from calls list.
        /// </summary>
        /// <param name="call">Call to remove.</param>
        internal void RemoveCall(SIP_B2BUA_Call call)
        {
            m_pCalls.Remove(call);
        }

        #endregion


        #region Properties Implementation

        /// <summary>
        /// Gets B2BUA owner SIP stack.
        /// </summary>
        public SIP_Stack Stack
        {
            get{ return m_pProxy.Stack; }
        }

        /// <summary>
        /// Gets active calls.
        /// </summary>
        public SIP_B2BUA_Call[] Calls
        {
            get{ return m_pCalls.ToArray(); }
        }

        #endregion

        #region Events Implementation

        /// <summary>
        /// Is called when new call is created.
        /// </summary>
        public event EventHandler CallCreated = null;

        /// <summary>
        /// Raises CallCreated event.
        /// </summary>
        /// <param name="call">Call created.</param>
        protected void OnCallCreated(SIP_B2BUA_Call call)
        {
            if(this.CallCreated != null){
                this.CallCreated(call,new EventArgs());
            }
        }

        /// <summary>
        /// Is called when call has terinated.
        /// </summary>
        public event EventHandler CallTerminated = null;

        /// <summary>
        /// Raises CallTerminated event.
        /// </summary>
        /// <param name="call">Call terminated.</param>
        internal protected void OnCallTerminated(SIP_B2BUA_Call call)
        {
            if(this.CallTerminated != null){
                this.CallTerminated(call,new EventArgs());
            }
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ivar Lumi

Estonia Estonia
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140826.1 | Last Updated 11 Jun 2007
Article Copyright 2007 by Ivar Lumi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid