Target | SIPSorcery | Examples | Softphone |
---|---|---|---|
net452 | |||
netstandard2.0 | |||
dotnetcore3.0 |
This repository contains the source for a C# .NET library with full support for the Session Initiation Protocol (SIP) including IPv6 support. In addition there is partial support for the Real-time Transport Protocol (RTP), Web Real-Time Communication (WebRTC) and a number of related protocols such as RTCP, STUN, SDP and RTSP. Work is ongoing to fully support RTP.
This project does not provide any media (audio and video) handling. There are some limited capabilities in the separate SIPSorcery.Media project but they are Windows specific and not suitable for production. This project can be used for SIP signalling and to send and receive RTP packets but it does not have features to do anything with the payloads in the RTP packets.
The library is compliant with .NET Standard 2.0 and .NET Framework 4.5.2. It is available via NuGet.
For .NET Core:
dotnet add package SIPSorcery
With Visual Studio Package Manager Console (or search for SIPSorcery on NuGet):
Install-Package SIPSorcery
Class reference documentation and articles explaining common usage are available at https://sipsorcery.github.io/sipsorcery/.
The examples folder contains full sample code designed to demonstrate some common use cases. The GetStarted example is the best place to start and the main program is shown below.
using System;
using System.Net;
using SIPSorcery.SIP;
namespace demo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("SIPSorcery demo");
var sipTransport = new SIPTransport();
var sipChannel = new SIPUDPChannel(IPAddress.Any, 5060);
sipTransport.AddSIPChannel(sipChannel);
sipTransport.SIPTransportRequestReceived += (SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest) =>
{
Console.WriteLine($"Request received {localSIPEndPoint.ToString()}<-{remoteEndPoint.ToString()}: {sipRequest.StatusLine}");
if (sipRequest.Method == SIPMethodsEnum.OPTIONS)
{
SIPResponse optionsResponse = SIPResponse.GetResponse(sipRequest, SIPResponseStatusCodesEnum.Ok, null);
sipTransport.SendResponse(optionsResponse);
}
};
Console.Write("press any key to exit...");
Console.Read();
sipTransport.Shutdown();
}
}
}
To use the SIP functionality the first step is to initialise the SIPTransport
class. It takes care of things like retransmitting requests and responses, DNS resolution, selecting the next hop for requests, matching SIP messages to transactions and more.
The SIPTransport
class can have multiple SIP channels added to it. A SIP channel is roughly the equivalent to the HTTP connection between a Web Browser and Server. It expects all packets received to be either a SIP request or response. The types of SIP channels supported are UDP, TCP and TLS.
The code below shows how to create a SIPTransport
instance and add a single UDP channel to it.
var sipTransport = new SIPTransport();
var sipChannel = new SIPUDPChannel(IPAddress.Loopback, 5060);
sipTransport.AddSIPChannel(sipChannel);
To shutdown the SIPTransport
use:
sipTransport.Shutdown();
There are two common scenarios when using the SIPTransport
class:
- For a server application wire up the
SIPTransport
event handlers, see code below, - For client applications the
SIPTranpsort
class can be passed as a constructor parameter. There are a number of client user agents in theapp\SIPUserAgents
folder that can be used for common client scenarios. See Next Steps for a description of the example client applications.
An example of the first approach of wiring up the SIPTransport
event handlers is shown below. It will respond with a 200 OK response for OPTIONS requests
and will ignore all other request types.
sipTransport.SIPTransportRequestReceived += (SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest) =>
{
Console.WriteLine($"Request received {localSIPEndPoint.ToString()}<-{remoteEndPoint.ToString()}: {sipRequest.StatusLine}");
if (sipRequest.Method == SIPMethodsEnum.OPTIONS)
{
SIPResponse optionsResponse = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.Ok, null);
sipTransport.SendResponse(optionsResponse);
}
};
A convenient tool to test SIP applications is SIPp. The OPTIONS request handling can be tested from Ubuntu or WSL using the steps below.
$ sudo apt install sip-tester
$ wget https://raw.githubusercontent.com/saghul/sipp-scenarios/master/sipp_uac_options.xml
$ sipp -sf sipp_uac_options.xml -m 3 127.0.0.1
If working correctly the message below should appear on the SIPSorcery demo program console:
SIPSorcery Getting Started Demo
press any key to exit...
Request received udp:127.0.0.1:5060<-udp:127.0.0.1:5061: OPTIONS sip:127.0.0.1:5060 SIP/2.0
Request received udp:127.0.0.1:5060<-udp:127.0.0.1:5061: OPTIONS sip:127.0.0.1:5060 SIP/2.0
Request received udp:127.0.0.1:5060<-udp:127.0.0.1:5061: OPTIONS sip:127.0.0.1:5060 SIP/2.0
The SIPp program will also report some test results after a completed test run. In correct operation the Successful call
row should be greater than 0 and Failed call
should be 0.
-------------------------+---------------------------+--------------------------
Successful call | 0 | 3
Failed call | 0 | 0
-------------------------+---------------------------+--------------------------
Additional example programs are provided to demonstrate how to use the SIPSorcery library in some common scenarios. The example programs are in the examples
folder.
-
Get Started: Simplest example. Demonstrates how to initialise a SIP channel and respond to an OPTIONS request.
-
SIP Proxy: Expands the
Get Started
example to also handle REGISTER requests. -
Registration Client: Demonstrates how to use the
SIPRegistrationUserAgent
class to register with a SIP Registrar server. -
SIP Call Client: Demonstrates how to use
SIPClientUserAgent
class to place a call to a SIP server user agent. -
SIP Call Server: Demonstrates how to use the
SIPServerUserAgent
class to receive a call from a SIP client user agent. -
SoftPhone: A very rudimentary SIP softphone implementation.
-
Get Started Web Socket: An example of how to create a web socket listener to send and receive SIP messages. An explanation of the example is available here.
-
STUN Server: An example of how to create a basic STUN (RFC3849) server. An explanation of the example is available here.
-
Call Transfer: An example of how to transfer a call using a REFER request as specified in RFC3515. An explanation of the example is available here.
-
Send DTMF (as RTP events): An example of how to send DTMF tones using RTP events as specified in RFC2833. An explanation of the example is available here.