• Articles
  • Api Documentation
Search Results for

    Show / Hide Table of Contents
    • Introduction
    • SIP Message Transport Management
    • SIP Channel Classes
    • Working with SIP Messages
    • Session Description Protocol
    • Real Time Protocol
    • Real Time Text
    • Message Session Relay Protocol (MSRP)
    • SipLib Application Logging

    Introduction

    The following table lists the key namespaces that relate to the processing of SIP messages.

    Namespace Description
    SipLib.Core Contains classes for building and processing SIP messages.
    SipLib.Body Contains classes for building and processing the body contents of a SIPRequest or a SIPResponse. The classes support simple message bodies or multipart/mixed message bodies.
    SipLib.Sdp Contains classes for the Session Description Protocol (SDP)

    SIP Messages

    The SIPMessage class is the base class for SIP requests and SIP responses. Applications do not work with the SIPMessage class directly.

    SIP Requests

    The SIPRequest class represents a SIP request message. The SIPRequest class consists of the following key components.

    1. Request line consisting of a request method and a request URI
    2. SIP message headers encapusulated in a field called Header which is of type SIPHeader
    3. An optional message body

    Applications typically receive a SIPRequest object as a parameter of an event handler of an event generated by a SipTransport or a SIPChannel derived object.

    The Method field of a SIPRequest object is of type SIPMethodsEnum and can be used to determine the type of request (INVITE, BYE, etc.).

    The URI field of a SIPRequest object is of type SIPURI and it contains the request URI from the request line. This field identifies the resource being requested by the sender of the SIPRequest message.

    The Header field is of type SIPHeader and contains objects for each SIP header of the request.

    The message body may be contained in the Body field of a SIPRequest. This field is a string. Applications can use the helper classes described in Received SIP Message Body Processing to parse and process the message body.

    The SIPRequest class has a static method called CreateBasicRequest() that can be used for building a SIPRequest. The declaration of this method is:

    public static SIPRequest CreateBasicRequest(SIPMethodsEnum Method, SIPURI reqUri,
        SIPURI ToSipUri, string? ToDisplayName, SIPURI FromSipUri, string? FromDisplayName)
    

    The CreateBasicRequest() method creates a new SIPRequest object with a request line and adds the following SIP headers.

    1. From
    2. To
    3. Via
    4. Contact
    5. Call-ID
    6. Max-Forwards (set to 70)
    7. Content-Length (set to 0)
    8. CSeq

    Applications can modify these headers and add new new headers.

    Applications can add a body to the SIPRequest message using the methods described in Building SIP Message Bodies.

    SIP Responses

    The SIPResponse class represents a SIP response message. The SIPResponse class consists of the following key components.

    1. A status line containing a response code and a response reason string
    2. SIP message headers encapusulated in a field called Header which is of type SIPHeader
    3. An optional message body

    Applications typically receive a SIPResponse object as a parameter of an event handler of an event generated by a SipTransport or a SIPChannel derived object.

    The Status field is of type SIPResponseStatusCodesEnum and the StatusCode field is the numeric representation of the Status enumeration. These fields should be used to determine the type of the response message.

    The Header field is of type SIPHeader and contains objects for each SIP header of the response.

    The message body may be contained in the Body field of a SIPResponse. This field is a string. Applications can use the helper classes described in Received SIP Message Body Processing to parse and process the message body.

    Applications build SIPResponse objects in response to SIPRequest message objects that they receive. They can do so by using the constructors of the SIPResponse class and fill in the status code, headers and body of the new SIPResponse object.

    The SipUtils class has a static helper method called BuildResponse() that can greatly simplify construction of new SIPResponse objects. The declaration of this method is:

    public static SIPResponse BuildResponse(SIPRequest Sr, SIPResponseStatusCodesEnum StatCode,
        string ReasonPhrase, SIPChannel sipChannel, string? SipUserName);
    

    This method returns a SIPResponse object that the application can send to the originator of a SIPRequest with the following header fields set.

    1. To
    2. From
    3. Via
    4. Record-Route
    5. CSeq
    6. Call-ID
    7. Contact
    8. Content-Length (set to 0)
    9. Max-Forwards

    Applications can add a body to the SIPResponse message using the methods described in Building SIP Message Bodies.

    SIP Message Bodies

    Both SIP requests and SIP responses may have a message body.

    Received SIP Message Body Processing

    The following discussion applies to SIP messages that have been received by an application.

    The HasBody() method of a SIPRequest or a SIPResponse object returns true if the message has body or false if it does not. You can call the GetBodyContents() method of a SIPRequest or a SIPResponse object to get a list of body parts. The definition of this method is:

    public List<MessageContentsContainer> GetBodyContents();
    

    The returned list will always be non-null, even if the message does not have a body. You can enumerate through the returned list object to parse and process each body part.

    The MessageContentsContainer class is used to contain information about a body contents block.

    The following code sample shows how to process a SIPRequest containing SDP data and NG9-1-1 location information. The same logic pertains to the SIPResponse class.

    using SipLib.Core;
    using SipLib.Body;
    using SipLib.Sdp;
    
    // Import Ng911Lib from NuGet to use
    using Ng911Lib.Utilities;
    using Pidf;
    ...
    
    private void ProcessSipRequest(SIPRequest sipRequest)
    {
        List<MessageContentsContainer> containers = sipRequest.GetBodyContents();
        foreach (MessageContentsContainer container in containers)
        {
            if (string.IsNullOrEmpty(container.StringContents) == true)
                continue; // Skip, log it or do something
    
            switch (container.ContentType)
            {
                case ContentTypes.sdp:
                    ProcessSdp(container.StringContents);
                    break;
                case ContentTypes.Pidf:
                    ProcessPidf(container.StringContents);
                    break;
            }
        }
    }
    
    private void ProcessSdp(string strSdp)
    {
        Sdp sdp = Sdp.ParseSDP(strSdp);
        // Process the sdp object
    }
    
    private void ProcessPidf(string strPidf)
    {
        Presence presence = XmlHelper.DeserializeFromString<Presence>(strPidf);
        if (presence != null)
        {
            // Process the location data
        }
    }
    
    

    The GetContentsContainer() method returns the body part for a specified MIME body type.

    public MessageContentsContainer? GetContentsContainer(string contentType);
    

    For example, the following code gets the contents container for the SDP.

    using SipLib.Core;
    using SipLib.Body;
    using SipLib.Sdp;
    ...
    
    private void ProcessSipRequest(SIPRequest sipRequest)
    {
        MessageContentsContainer? sdpContainer = sipRequest.GetContentsContainer(
            ContentTypes.Sdp);
        if (sdpContainer != null && sdpContainer.StringContents != null)
        {
            Sdp sdp = Sdp.ParseSDP(sdpContainer.StringContents);
            // Process the sdp object
        }
    }
    

    The ContentTypes class provides named fields that are constant string values for each known MIME type. Use the fields in this class instead of quoted string values to ensure consistency and avoid coding errors.

    Building SIP Message Bodies

    The SipBodyBuilder class can be used to build simple or multipart/mixed SIP message bodies and attach the message body to a SIPRequest or a SIPResponse message.

    Follow these steps to use the SipBodyBuilder class to add a body to a SIP message.

    1. Create an instance of the SipBodyBuilder class
    2. Convert the object to add to the body to a string
    3. Call the AddContent() method of the SipBodyBuilder class for each body part that needs to be attached to the SIP message
    4. Call the AttachMessageBody() method of the SipBodyBuilder class with the SIPRequest or the SIPResponse object

    The AttachMessageBody() method performs the following actions.

    1. Concactenates each body part into a string and sets the Body field of the SIPMessage derived object
    2. Calculates the total contents length and sets the Content-Length header of the SIPMessage derived object
    3. Determines the appropriage Content-Type header value and sets the Content-Type header field for the message

    The following code sample shows how to use the SipBodyBuilder class.

    using SipLib.Core;
    using SipLib.Body;
    using SipLib.Sdp;
    ...
    
    private void AttachSdp(SIPRequest sipRequest)
    {
        SipBodyBuilder bodyBuilder = new SipBodyBuilder();
        Sdp sdp = SdpUtils.BuildSimpleAudioSdp("192.168.1.100, 7000, "MyUaName");
        bodyBuilder.AddContent(ContentTypes.sdp, sdp.ToString());
        bodyBuilder.AttachMessageBody(sipRequest);
    }
    
    • Edit this page
    In this article
    Back to top Generated by DocFX