Difference between revisions of "M4 Kamailio Transformations"

From Kolmisoft Wiki
Jump to navigationJump to search
Line 1: Line 1:
Transformations allow modifying PAI and RPID headers.
Transformations allow modifying PAI and RPID headers.


<br><br>
=Usage=
=Usage=


Line 23: Line 24:
** g - replace all matches
** g - replace all matches


<br><br>
==Match Expression==
==Match Expression==


POSIX regular expression (regex) matching is done on full PAI/RPID header (for example: "name" <sip:number@host>;params). This allows to match and modify any part of the header (name, number, host, parameters).
POSIX regular expression (regex) matching is done on full PAI/RPID header (for example: "name" <sip:number@host>;params). This allows to match and modify any part of the header (name, number, host, parameters).


<br><br>
==Replacement Expression==
==Replacement Expression==


Line 78: Line 81:
Here we construct a new header and set the '''name''' part to '''\2''' (which in our case is a number) and set static text as a '''host'''.
Here we construct a new header and set the '''name''' part to '''\2''' (which in our case is a number) and set static text as a '''host'''.


<br><br>
=Kamailio variables=
=Kamailio variables=


Line 100: Line 104:
Here we are using the [http://www.kamailio.org/wiki/cookbooks/5.4.x/pseudovariables#fu_-_from_uri_username $fU] Kamailio variable which refers to FROM URI username. PAI number '''anonymous''' is replaced with number '''1111111'''.
Here we are using the [http://www.kamailio.org/wiki/cookbooks/5.4.x/pseudovariables#fu_-_from_uri_username $fU] Kamailio variable which refers to FROM URI username. PAI number '''anonymous''' is replaced with number '''1111111'''.


<br><br>
=Testing=
=Testing=


Before putting Transformations to production, it is advisable to test them on various values.  
Before putting Transformations to production, it is advisable to test them on various values.  


<br><br>
====Test in Linux console====
====Test in Linux console====


Line 110: Line 116:
  echo '<sip:00370123456789@myhost.sip>' | sed -E 's/sip:00/sip:/'
  echo '<sip:00370123456789@myhost.sip>' | sed -E 's/sip:00/sip:/'


<br><br>
====Test in online sed live editor====
====Test in online sed live editor====


Line 118: Line 125:
Regex part of a Transformation can be validated using https://regexr.com/ (or any other online regex editor).
Regex part of a Transformation can be validated using https://regexr.com/ (or any other online regex editor).


<br><br>
=Quick notes=
=Quick notes=


Line 124: Line 132:
* Only POSIX regex is allowed
* Only POSIX regex is allowed
* If a Transformation was applied by OP settings, then TP Transformations will be applied on top of the already modified header
* If a Transformation was applied by OP settings, then TP Transformations will be applied on top of the already modified header
* Destination Transformation uses different syntax, please refer to [[M2_Destination_Transformation | Destination Transformation]]
* Destination Transformation uses a different syntax, please refer to [[M2_Destination_Transformation | Destination Transformation]]


<br><br>
=Examples=
=Examples=


Line 143: Line 152:
|}
|}


<br><br>
=See also=
=See also=


* [[M2_Origination_Points | M2 Origination Points]]
* [[M2_Origination_Points | M2 Origination Points]]
* [[M2_Termination_Points| M2 Termination Points]]
* [[M2_Termination_Points| M2 Termination Points]]

Revision as of 07:07, 19 January 2023

Transformations allow modifying PAI and RPID headers.



Usage

Go to Users -> Connection Points, click EDIT on Connection Point.

PAI/RPID Transformations for OP are located under Advanced Origination Point Settings:

Op pai rpid transformations.png

PAI/RPID Transformations for TP are located under Termination Point Signaling Settings:

Pai rpid transformations.png

Transformation format

/match_expression/replacement_expression/flags

  • match_expression - POSIX regular expression
  • replacement_expression - replacement expression with back references to matched tokes: \1, \2, …, \9
  • flags:
    • i - match ignore case
    • g - replace all matches



Match Expression

POSIX regular expression (regex) matching is done on full PAI/RPID header (for example: "name" <sip:number@host>;params). This allows to match and modify any part of the header (name, number, host, parameters).



Replacement Expression

If the regex match is successful, then the portion which was matched is replaced with a replacement.

A Transformation:

/sip:00/sip:/

applied on a header:

<sip:00370123456789@example.sip>

would result in:

<sip:370123456789@example.sip>

Here we match sip:00 in the original header and replace it with sip:. If the original header did not contain a number starting with 00, then regex would not match and nothing is replaced.

Additionally, the replacement can contain \n (n is a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between parentheses ( ). References are numbered by counting their opening parentheses from left to right.

A regex:

(.*) <sip:(.*)@(.*)>(.*)

applied on a header:

"John" <sip:00370123456789@example.sip>;user=phone 

would produce the following references:

  • \1 - "John"
  • \2 - 00370123456789
  • \3 - example.sip
  • \4 - ;user=phone

Using these references you can construct your own header:

A Transformation:

/(.*) <sip:(.*)@(.*)>(.*)/\2 <sip:\2@myhost.sip>\4/

applied on a header:

"John" <sip:00370123456789@example.sip>;user=phone

would result in:

00370123456789 <sip:00370123456789@myhost.sip>;user=phone

Here we construct a new header and set the name part to \2 (which in our case is a number) and set static text as a host.



Kamailio variables

Replacement Expression accepts Kamailio variables.

As an example, let's replace the URI username (number part) in the PAI header with URI username from FROM header:

A Transformation:

/sip:(.*)@/sip:$fU@/

applied on a PAI header:

From: <1111111@myhost.sip>
P-Asserted-Identity: <anonymous@myhost.sip>

would result in:

From: <1111111@myhost.sip>
P-Asserted-Identity: <1111111@myhost.sip>

Here we are using the $fU Kamailio variable which refers to FROM URI username. PAI number anonymous is replaced with number 1111111.



Testing

Before putting Transformations to production, it is advisable to test them on various values.



Test in Linux console

Execute sed command (add s before transformation s/regex/replacement/flags):

echo '<sip:00370123456789@myhost.sip>' | sed -E 's/sip:00/sip:/'



Test in online sed live editor

If you do not have access to the Linux console, use https://sed.js.org/ (or any other online sed editor).

Validate regex

Regex part of a Transformation can be validated using https://regexr.com/ (or any other online regex editor).



Quick notes

  • Header must be present in order to apply Transformations
  • In case of error, no changes will be applied
  • Only POSIX regex is allowed
  • If a Transformation was applied by OP settings, then TP Transformations will be applied on top of the already modified header
  • Destination Transformation uses a different syntax, please refer to Destination Transformation



Examples

Transformation Original header Modified header Comment
/sip:00/sip:/ <sip:003701234567@myhost.sip> <sip:3701234567@myhost.sip> Cut prefix 00
/sip:\+/sip:/ <sip:+3701234567@myhost.sip> <sip:3701234567@myhost.sip> Cut + (here + is escaped by \ since it is special regex symbol)
/sip:\+370/sip:86/ <sip:+3701234567@myhost.sip> <sip:861234567@myhost.sip> Cut prefix +370, add prefix 86
/sip:\+?(.*)/sip:+\1/ <sip:3701234567@myhost.sip> <sip:+3701234567@myhost.sip> Add + if it is not present already
/sip:(.*)@/sip:$fU@/ <sip:anonymous@myhost.sip> <sip:3701234567@myhost.sip> Using Kamailio variable $fU replace the original number with the number from SIP FROM header



See also