How to reject incoming call with Ozeki VoIP SIP SDK using C#?

This example demonstrates how to reject incoming calls via softphone, in other words; how to respond to INVITE SIP request to reject the call. To fully understand this article, you might have to study the How to register to a SIP PBX chapter and the How to ring a SIP extension chapter first.
To use this example, you need to have Ozeki VoIP SIP SDK installed, and a reference to ozeki.dll should be added to your visual studio project.

Figure 1 - after the SIP INVITE request has arrived, the destination can reject the call
Figure 1 -

How to respond to SIP INVITE request to reject the call?

Most of the softphones are able to get notified about incoming calls, which means they can receive the SIP INVITE message. When the phone is ringing, a 180 Ringing SIP message indicates that the INVITE request has successfully arrived, and a decision is about to be made about that.

The INVITE SIP request indicates a client is being invited to participate in a call session. When the INVITE SIP message arrives, the softphone has several options to respond: accept the call, reject the call, forward the call etc. In this example the softphone will reject the call, which means a 486 BUSY HERE SIP message is being set back to indicate that the call has been rejected.

The learn more about the INVITE SIP message and how to implement it, please visit the How to ring a SIP extension chapter.

How to reject incoming call using C#?

Using Ozeki VoIP SIP SDK, softphones are able to get notified when there is an incoming call, by subscribing to the IncomingCall event of the softphone object. When the event occurs, the call can be rejected.
By calling the call object's Reject() method, the call is being rejected and the SIP 486 BUSY HERE message is being sent back, to the caller.
You can even create reject app, which would use a call reject list for the purpose to reject calls from specified numbers.

Call reject example in C#

using System;
using Ozeki.VoIP;

namespace Reject_Incoming_Call
{
    class Program
    {
        static ISoftPhone softphone;   // softphone object
        static IPhoneLine phoneLine;   // phoneline object
        static IPhoneCall call;

        private static void Main(string[] args)
        {
            // Create a softphone object with RTP port range 5000-10000
            softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

            // SIP account registration data, (supplied by your VoIP service provider)
            var registrationRequired = true;
            var userName = "858";
            var displayName = "858";
            var authenticationId = "858";
            var registerPassword = "858";
            var domainHost = "192.168.115.100";
            var domainPort = 5060;

            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);

            // Send SIP regitration request
            RegisterAccount(account);

            // Prevents the termination of the application
            Console.ReadLine();
        }

        static void RegisterAccount(SIPAccount account)
        {
            try
            {
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
                softphone.IncomingCall += softphone_IncomingCall;
                softphone.RegisterPhoneLine(phoneLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error during SIP registration: " + ex);
            }
        }

        static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
        {
            if (e.State == RegState.NotRegistered || e.State == RegState.Error)
                Console.WriteLine("Registration failed!");

            if (e.State == RegState.RegistrationSucceeded)
                Console.WriteLine("Registration succeeded - Online!");
        }

        static void softphone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
        {
            call = e.Item;
            call.Reject();
        }
    }
}

Communication through the network

When the INVITE sip message arrives to the softphone, it can decide how to react to it. When the call is being rejected, the sip message 486 Busy here is being sent back as an answer sip message, to the caller via PBX.

Step 1: Invite request arrives from caller via PBX (UDP message, PBX -> Softphone)

INVITE sip:1001@192.168.115.149:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.115.149:5060;branch=z9hG4bK7725f5b3-0d85-456c-8056-
2dd6a45bb13b;rport
To: "1001"<sip:1001@192.168.115.149:5060>
From: "1002"<sip:1002@192.168.115.149:5060>;tag=irqayimj
CSeq: 1 INVITE
Call-ID: edtcxswbjhfkuqjuyxnuoqgixijihqidgrfgateyuwhpmhftgt
Max-Forwards: 70
Contact: <sip:1002@192.168.115.149:5060>
User-Agent: Ozeki Phone System XE v5.2.1
Content-Type: application/sdp
Content-Length: 292
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REGISTER, SUBSCRIBE, NOTIFY, REFER, 
INFO, MESSAGE

v=0
o=- 763177212 429111000 IN IP4 192.168.115.149
s=Ozeki Call
c=IN IP4 192.168.115.149
t=0 0
m=audio 8653 RTP/AVP 8 0 3 101 9
a=rtpmap:8 PCMA/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:3 GSM/8000
a=rtpmap:101 telephone-event/8000
a=rtpmap:9 G722/8000
a=fmtp:9 bitrate=64000
a=sendrecv

Step 2: Reply status message to indicate that the softphone is trying to be ringed (UDP message, Softphone -> PBX)

SIP/2.0 100 Trying
Via: SIP/2.0/UDP 192.168.115.149:5060;branch=z9hG4bK7725f5b3-0d85-456c-8056-
2dd6a45bb13b;rport=5060;
received=192.168.115.149
From: "1002"<sip:1002@192.168.115.149:5060>;tag=irqayimj
Call-ID: edtcxswbjhfkuqjuyxnuoqgixijihqidgrfgateyuwhpmhftgt
CSeq: 1 INVITE
To: "1001"<sip:1001@192.168.115.149:5060>
User-Agent: Ozeki VoIP SIP SDK v10.1.8
Content-Length: 0

Step 3: The called softphone rejects the call, which means a 486 busy here sip reply (UDP message, Softphone -> PBX)

SIP/2.0 486 Busy Here
Via: SIP/2.0/UDP 192.168.115.149:5060;branch=z9hG4bK7725f5b3-0d85-456c-8056-
2dd6a45bb13b;rport=5060;
received=192.168.115.149
From: "1002"<sip:1002@192.168.115.149:5060>;tag=irqayimj
Call-ID: edtcxswbjhfkuqjuyxnuoqgixijihqidgrfgateyuwhpmhftgt
CSeq: 1 INVITE
To: "1001"<sip:1001@192.168.115.149:5060>;tag=hqihanei
User-Agent: Ozeki VoIP SIP SDK v10.1.8
Content-Length: 0
Warning: 399 ozsdk "User reject"

Step 4: Acknowledgement of call rejection via the PBX (UDP message, Softphone -> Softphone)

ACK sip:1001@192.168.115.149:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.115.149:5060;branch=z9hG4bK7725f5b3-0d85-456c-8056-
2dd6a45bb13b;rport
Call-ID: edtcxswbjhfkuqjuyxnuoqgixijihqidgrfgateyuwhpmhftgt
From: "1002"<sip:1002@192.168.115.149:5060>;tag=irqayimj
To: "1001"<sip:1001@192.168.115.149:5060>;tag=hqihanei
CSeq: 1 ACK
Max-Forwards: 70
Content-Length: 0

Related Pages

More information