– Kamailio SIP Server –

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
transformations:1.5.x [2011/09/09 10:13]
92.112.187.211
transformations:1.5.x [2012/03/22 13:32] (current)
80.250.1.245 removed spam
Line 1: Line 1:
 +====== Kamailio (OpenSER) Transformations for version 1.5.x ======
 +<​code>​
 +Main author:
 +   ​Daniel-Constantin Mierla <miconda (at) asipto.com>​
 +</​code>​
 +
 +[b]Transformation[/​b] is basically a function that is applied to a pseudo-variable (PV) to get a special value from it. The value of PV is not altered.
 +
 +<hi #​ffa500>​IMPORTANT:​ Starting with version 1.5.0, the module **pv** must be loaded to get access to core transformations.</​hi>​
 +
 +Starting with [b]1.2.0, all PV[/b] can be used directly in the script. For example:
 +
 +<​code>​
 +# check if username in From header is equal with username in To header
 +if($fU==$tU) {
 +   ...
 +}
 +
 +# r-uri username based processing
 +switch($ruri.user) {
 +   case "​1234":​
 +      ...
 +   ​break;​
 +   case "​5678":​
 +      ...
 +   ​break;​
 +   ​default:​
 +     ...
 +}
 +
 +# assign integer value to an AVP
 +$avp(i:11) = 1;
 +
 +#assing string value to an AVP
 +$avp(i:22) = "​openser";​
 +
 +# write ruri in an AVP
 +$avp(i:33) = $ruri;
 +
 +# concat "​sip:"​ + From username + "​@"​ + To domain in a script variable x
 +$var(x) = "​sip:"​ + $fU +"​@"​ + $td;
 +
 +
 +</​code>​
 +
 +The transformations are intended to facilitate access to different attributes of PV (like strlen of value, parts of value, substrings) or complete different value of PV (encoded in hexa, md5 value, escape/​unescape PV value for DB operations...).
 +
 +A transformation is represented in between [b]'​{'​[/​b] and [b]'​}'​[/​b] and follows the name of a PV. When using transformations,​ the PV name and transformations [b]must[/b] be enclosed in between [b]'​('​[/​b] and [b]'​)'​[/​b].
 +
 +<​code>​
 +# the length of From URI ($fu is PV for From URI)
 +
 +$(fu{s.len})
 +</​code>​
 +
 +Many transformations can be applied in the same time to a PV.
 +
 +<​code>​
 +# the length of escaped '​Test'​ header body
 +
 +$(hdr(Test){s.escape.common}{s.len})
 +</​code>​
 +
 +The transformations can be used anywhere, being considered parts of PV -- in xlog, avpops or other modules'​ functions and parameters, in right side assignment expressions or in comparisons.
 +
 +[color=brown]IMPORTANT[/​color]:​ To learn what variables can be used with transformations see [[pseudovariables:​devel|Pseudo variables list]].
 +
 +===== String Transformations =====
 +The name of these transformation starts with '​s.'​. They are intended to apply string operations to PV.
 +
 +Available transformations in this class:
 +
 +
 +==== {s.len} ====
 +
 +Return strlen of PV value
 +
 +<​code>​
 +$var(x) = "​abc";​
 +if($(var(x){s.len}) == 3)
 +{
 +   ...
 +}
 +</​code>​
 +
 +==== {s.int} ====
 +
 +Return integer value of a string-represented number
 +
 +<​code>​
 +$var(x) = "​1234";​
 +if($(var(x){s.int})==1234) {
 +  ...
 +}
 +</​code>​
 +==== {s.md5} ====
 +
 +Return md5 over PV value
 +
 +<​code>​
 +xlog("​MD5 over From username: $(fU{s.md5})"​);​
 +</​code>​
 +
 +==== {s.substr,​offset,​length} ====
 +
 +Return substring starting at offset having size of '​length'​. If offset is negative, then it is counted from the end of PV value, -1 being the last char. In case of positive value, 0 is first char. Length must be positive, in case of 0, substring to the end of PV value is returned. offset and length can be PV as well.
 +
 +Example:
 +<​code>​
 +$var(x) = "​abcd";​
 +$(var(x){s.substr,​1,​0}) = "​bcd"​
 +</​code>​
 +
 +==== {s.select,​index,​separator} ====
 +
 +Return a field from PV value. The field is selected based on separator and index. The separator must be a character used to identify the fields. Index must be a integer value or a PV. If index is negative, the count of fields starts from end of PV value, -1 being last field. If index is positive, 0 is the first field.
 +
 +Example:
 +<​code>​
 +$var(x) = "​12=34=56";​
 +$(var(x){s.select,​1,​=}) => "​34"​ ;
 +
 +$var(x) = "​12@34@56";​
 +$(var(x){s.select,​-2,​@}) => "​34"​
 +</​code>​
 +
 +==== {s.encode.hexa} ====
 +
 +Return encoding in hexa of PV value
 +
 +==== {s.decode.hexa} ====
 +
 +Return decoding from hexa of PV value
 +
 +
 +
 +==== {s.escape.common} ====
 +
 +Return escaped string of PV value. Characters escaped are ', ", \ and 0. Useful when doing DB queries (care should be taken for non Latin character set).
 +
 +
 +
 +==== {s.unescape.common} ====
 +
 +Return unescaped string of PV value. Reverse of above transformation.
 +
 +
 +==== {s.escape.user} ====
 +
 +Return escaped string of PV value, changing to '​%hexa'​ the characters that are not allowed in user part of SIP URI following RFC requirements.
 +
 +==== {s.unescape.user} ====
 +
 +Return unescaped string of PV value, changing '​%hexa'​ to character code. Reverse of above transformation.
 +
 +
 +==== {s.escape.param} ====
 +
 +Return escaped string of PV value, changing to '​%hexa'​ the characters that are not allowed in the param part of SIP URI following RFC requirements.
 +
 +
 +==== {s.unescape.param} ====
 +
 +Return unescaped string of PV value, changing '​%hexa'​ to character code. Reverse of above transformation.
 +
 +==== {s.tolower} ====
 +
 +Return string with lower case ASCII letters.
 +
 +
 +==== {s.toupper} ====
 +
 +Return string with upper case ASCII letters.
 +
 +
 +===== URI Transformations =====
 +
 +The name of transformation starts with '​uri.'​. The PV value is considered to be a SIP URI. This transformation returns parts of SIP URI (see struct sip_uri). If that part is missing, the returned value is an empty string.
 +
 +Available transformations in this class:
 +
 +==== {uri.user} ====
 +
 +Return the user part
 +
 +==== {uri.host} ====
 +
 +(same as [b]{uri.domain}[/​b])
 +
 +Return the domain part
 +
 +==== {uri.passwd} ====
 +
 +Return the password
 +
 +==== {uri.port} ====
 +
 +Return the port
 +
 +==== {uri.params} ====
 +
 +Return the URI parameters in a string
 +
 +==== {uri.param,​name} ====
 +
 +Return the value of parameter with name '​name'​
 +
 +==== {uri.headers} ====
 +
 +Return URI headers
 +
 +==== {uri.transport} ====
 +
 +Return the value of transport parameter
 +
 +==== {uri.ttl} ====
 +
 +Return the value of ttl parameter
 +
 +==== {uri.uparam} ====
 +
 +Return the value of user parameter
 +
 +==== {uri.maddr} ====
 +
 +Return the value of maddr parameter
 +
 +==== {uri.method} ====
 +
 +Return the value of method parameter
 +
 +==== {uri.lr} ====
 +
 +Return the value of lr parameter
 +
 +==== {uri.r2} ====
 +
 +Return the value of r2 parameter
 +
 +===== Parameters List Transformations =====
 +
 +The name of the transformation starts with '​param.'​. The PV value is considered to be a string like name1=value1;​name2=value2;​..."​. The transformations returns the value for a specific parameter, or the name of a parameter at a specific index.
 +
 +Available transformations in this class:
 +
 +
 +==== {param.value,​name} ====
 +
 +Return the value of parameter '​name'​
 +
 +Example:
 +<​code>​
 +"​a=1;​b=2;​c=3"​{param.value,​c} = "​3"​
 +</​code>​
 +
 +'​name'​ can be a pseudo-variable
 +
 +==== {param.valueat,​index} ====
 +
 +Return the value of parameter at position give by '​index'​ (0-based index)
 +
 +Example:
 +<​code>​
 +"​a=1;​b=2;​c=3"​{param.valueat,​1} = "​2"​
 +</​code>​
 +
 +'​index'​ can be a pseudo-variable
 +
 +==== {param.name,​index} ====
 +
 +Return the name of parameter at position '​index'​.
 +
 +Example:
 +<​code>​
 +"​a=1;​b=2;​c=3"​{param.name,​1} = "​b"​
 +</​code>​
 +
 +
 +==== {param.count} ====
 +
 +Return the number of parameters in the list.
 +
 +Example:
 +<​code>​
 +"​a=1;​b=2;​c=3"​{param.count} = 3
 +</​code>​
 +
 +===== Name-address Transformations =====
 +
 +The name of the transformation starts with '​nameaddr.'​. The PV value is considered to be a string like '​[display_name] uri'. The transformations returns the value for a specific field.
 +
 +Available transformations in this class:
 +
 +
 +==== {nameaddr.name} ====
 +
 +Return the value of display name
 +
 +Example:
 +<​code>​
 +'"​test"​ <​sip:​test@kamailio.net>'​ {nameaddr.name} = "​test"​
 +</​code>​
 +
 +
 +==== {nameaddr.uri} ====
 +
 +Return the value of URI
 +
 +Example:
 +<​code>​
 +'"​test"​ <​sip:​test@kamailio.net>'​ {nameaddr.uri} = sip:​test@kamailio.net
 +</​code>​
 +
 +
 +==== {nameaddr.len} ====
 +
 +Return the length of the entire name-addr part from the value.
 +
 +===== To-Body Transformations =====
 +
 +<hi #​c0c0c0>​This transformation class is exported by **pv** module.</​hi>​
 +
 +Access parts of a ToBody-like structure.
 +
 +==== {tobody.uri} ====
 +
 +* return URI from To body
 +
 +==== {tobody.display} ====
 +
 +* return Display name from To body
 +
 +==== {tobody.tag} ====
 +
 +* return Tag parameter from To body
 +
 +==== {tobody.user} ====
 +
 +* return URI User from To body
 +
 +==== {tobody.host} ====
 +
 +* return URI Host from To body
 +
 +==== {tobody.params} ====
 +
 +* return parameters part from To body
 +
 +
 +===== Regular Expression Transformations =====
 +
 +<hi #​c0c0c0>​This transformation class is exported by **textops** module.</​hi>​
 +
 +Access parts of a ToBody-like structure.
 + 
 +
 +==== {re.subst,​expression} ====
 +
 +Perform Perl-like substitutions on string value pseudo-variables.
 +
 +<​code>​
 +$var(user) = $(ru{re.subst,/​^sip:​(.*)@(.*)/​\1/​});​
 +</​code>​
 +
 +Modifiers are supported, e.g. 
 +<​code>​
 +$var(user) = $(ru{re.subst,/​^sip:​(.*)@(.*)/​\1/​i});​ # ignore case
 +$var(user) = $(var(user){re.subst,/​~//​g});​ # remove all ~
 +</​code>​
 +
 +Attention: If match expr doesn'​t match anything, it returns null (or something undefined). e.g. 
 +<​code>​$var(test)="​asdfg";​ $(var(test){re.subst,/​^xx//​});​ # don't even need $var(test)=$(var(test){re.subst,/​^xx//​}); ​
 +append_hf("​X-Test:​ $var(test)\r\n"​);</​code>​
 +resulting in a 
 +<​code>​
 +Apr 13 16:57:52 test-1 /​usr/​local/​sbin/​kamailio[21903]:​ CRITICAL:​core:​comp_scriptvar:​ cannot get left var value
 +Apr 13 16:57:52 test-1 /​usr/​local/​sbin/​kamailio[21903]:​ WARNING:​core:​do_action:​ error in expression (l=983)
 +</​code>​
 +on next use of that variable.
 +
 +===== Examples =====
 +
 +Within a PV, many transformation can be applied, being executed from left to right.
 +
 +* The length of the value of parameter at postion 1 (remember 0 is first position, 1 is second position)
 +
 +<​code>​
 +$var(x) = "​a=1;​b=22;​c=333";​
 +$(var(x){param.value,​$(var(x){param.name,​1})}{s.len}) = 2
 +</​code>​
 +
 +* Test if whether is un-registration or not
 +
 +<​code>​
 +if(is_method("​REGISTER"​) && is_present_hf("​Expires"​) && $(hdr(Expires){s.int})==0)
 +    xlog("​This is an un-registration\n"​);​
 +</​code>​