Kamailio can execute managed code assemblies via app_mono module:
The C# API exported by Kamailio to the embedded Mono interpreter is defined in SR.cs file located in source tree modules/app_mono/lib/SR.cs.
To compile SR.cs to a library, use:
gmcs -t:library SR.cs
This will generate the file SR.dll. To link your C# file to it, use:
$ gmcs -r:SR.dll MyFile.cs
The API is stored in a namespace, named SR. The several classes offer access to internal Kamailio functionalities via public static functions.
public extern static string APIVersion();
Return API version string.
public extern static void Log(int level, string text);
Write a message via Kamailio logging system to specified log level.
public extern static void Err(string text);
Write a message via Kamailio logging system to error log level.
public extern static void Dbg(string text);
Write a message via Kamailio logging system to debug log level.
public extern static int ModF(string text);
Execute the function named by the value of parameter 'text' exported by a Kamailio module. It has to be a function with no parameters.
public extern static string GetS(string pvn);
Get the string value of a pseudo-variable.
public extern static int GetI(string pvn);
Get the integer value of a pseudo-variable.
public extern static int SetS(string pvn, string val);
Set the pseudo-variable to a string value.
public extern static int SetI(string pvn, int val);
Set the pseudo-variable to an integer value.
public extern static int Unset(string pvn);
Unset the pseudo-variable (like assigning $null in kamailio.cfg).
public extern static int IsNull(string pvn);
Return true if the pseudo-variable is null.
public extern static int Append(string hv);
Add a header to the end of the headers list. The parameter must be full header, including name, body header and ending '\r\n'.
public extern static int Remove(string name);
Remove all headers matching the name.
public extern static int Insert(string hv);
Add a header to the beginning of the headers list. The parameter must be full header, including name, body header and ending '\r\n'.
public extern static int AppendToReply(string hv);
Add a header to the headers list for local reply. The parameter must be full header, including name, body header and ending '\r\n'.
A basic example of C# application using SR API:
using SR; namespace MySRTest { class Test { static int Main(string[] args) { SR.Core.Log(1, "Kamailio API Version: " + SR.Core.APIVersion() + "\n"); if (args.Length == 1) { SR.Core.Log(1, "Parameter from Kamailio config:" + args[0] + "\n"); } SR.Core.Dbg("Request URI is: " + SR.PV.GetS("$ru") + "\n"); SR.PV.SetS("$rU", "123"); SR.HDR.AppendToReply("My-Mono-Hdr: ok\r\n"); SR.Core.ModF("sl_reply_error"); return 1; } } }