Skip to content

Instantly share code, notes, and snippets.

@whosgonna
Last active September 17, 2020 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whosgonna/5acfb33e064faf6d28e755dce0fe8b87 to your computer and use it in GitHub Desktop.
Save whosgonna/5acfb33e064faf6d28e755dce0fe8b87 to your computer and use it in GitHub Desktop.
Kamailio common tasks

Mosly transformations for which I forget the syntax:

Getting data out of a header:

In this example, assume the following X-Custom: header

X-Custom: "BetaUser01" <sip:+15555551234@host.domain.com:5075;user=phone>;tag=aa70809-41171;sgid=5;tenant=customer123

"X-Custom" is used for this example, because frequently common headers will not have existing psuedo variables to get specific sections. For exxample, to get the user portion of the URI in a from header it is easier to just use $fU.

Get the full value of a specific header:

In kamailio.cfg

$var(CustomHeader) = $hdr(X-Custom)

$var(CustomHeader) is now equal to:

"BetaUser01" <sip:+15555551234@host.domain.com:5075;user=phone>;tag=aa70809-41171;sgid=5;tenant=customer123

Get the display name and URI of a specific header.

The nameaddr transformations can be used to extract display name and URI from a header like so:

In kamailio.cfg:

$var(CustomHeaderUri)  = $(hdr(X-Custom){nameaddr.uri});   ## sip:+15555551234@host.domain.com:5075;user=phone
$var(CustomHeaderName) = $(hdr(X-Custom){nameaddr.name});  ## "BetaUser01" (includes the quotation marks)

Getting URI parameters

Note that our example has two different types of parameters. user=phone is a URI Parameter because it is part of the URI (meaning it is enclosed in the < and > symbols. The section tag=aa70809-41171;sgid=5;tenant=customer123 is a Header Paramter. We'll get to that in a second.

To get the URI parameter:

$var(CustomHeaderUri)       = $(hdr(X-Custom){nameaddr.uri});       ## sip:+15555551234@host.domain.com:5075;user=phone 
$var(CustomHeaderUriParams) = $(var(CustomHeaderUri){uri.params});  ## user=phone

This could also be written in a single line:

$var(CustomHeaderUriParams) = $(hdr(X-Custom){nameaddr.uri}{uri.params});  ## user=phone

Getting Header parameters

In our example header, the string tag=aa70809-41171;sgid=5;tenant=customer123 is the header paramters. Here's how to extract the header parameters:

$var(CustomHeaderHdrParams) = $(hdr(X-Custom){tobody.params});  ## tag=aa70809-41171;sgid=5;tenant=customer123

Getting individual parameters

Once a parameter list is retrieved, it doesn't matter if it was from the URI params or header params.

  • Access the name of the key of the first item in the list (it is a zero indexed array)
$var(FirstKey1) = $(hdr(X-Custom){tobody.params}{param.name,0});  ## tag
  • Access the value of the last parameter:
$var(LastVal) = $(hdr(X-Custom){tobody.params}{param.valueat,-1});  ## customer123
  • Get the value of the sgid parameter:
$var(sgid) = $(hdr(X-Custom){tobody.params}{param.value,sgid});  ## 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment