Hello,
I'm a a bit confused as to when string concatenation will work in kamailio config file. So far I have identified the following cases:
Working:
As part of the right hand side of an assignment: $var(x) = "string1" + "string2";
String argument of some functions: xlog("L_ERR", "string1" + "string2" + DEFINED_STRING + '\n');
Conditional Statements: if ( ru =~ "$" + "sip:" + $var(user) ) {
Not Working:
In string transformations (the fact that quotes is not part of the argument should give a hint though: $var(x) = $(var(y){s.replace,match,"repl1" + "repl2"});
String argument of some other functions: append_hf_value("P-Asserted-Identity", "<" + "$var(new_pai)" + ">"); lookup("location", "sip:$var(username)@" + SIP_REALM_DEFINITION));
In module parameter definitions: modparam("nathelper", "sipping_from", "sip:keepalive@" + SIP_REALM_DEFINITION)
So far, I have found this to be fairly consistent, except for use within functions. For example, string concatenation works for the second argument of xlog, but not for the string arguments of append_hf_value() or lookup().
Is there anyway to know this for each function beforehand, or is my only choice to go through trial and error here? Thanks!
BR, George
You are correct that concatenation vs interpolation works and doesn't work in slightly inconsistent ways, for instance function string arguments vs assignment.
The $_s() transformation may help make life easier.
On May 2, 2018 10:50:53 AM EDT, George Diamantopoulos georgediam@gmail.com wrote:
Hello,
I'm a a bit confused as to when string concatenation will work in kamailio config file. So far I have identified the following cases:
Working:
As part of the right hand side of an assignment: $var(x) = "string1" + "string2";
String argument of some functions: xlog("L_ERR", "string1" + "string2" + DEFINED_STRING + '\n');
Conditional Statements: if ( ru =~ "$" + "sip:" + $var(user) ) {
Not Working:
In string transformations (the fact that quotes is not part of the argument should give a hint though: $var(x) = $(var(y){s.replace,match,"repl1" + "repl2"});
String argument of some other functions: append_hf_value("P-Asserted-Identity", "<" + "$var(new_pai)" + ">"); lookup("location", "sip:$var(username)@" + SIP_REALM_DEFINITION));
In module parameter definitions: modparam("nathelper", "sipping_from", "sip:keepalive@" + SIP_REALM_DEFINITION)
So far, I have found this to be fairly consistent, except for use within functions. For example, string concatenation works for the second argument of xlog, but not for the string arguments of append_hf_value() or lookup().
Is there anyway to know this for each function beforehand, or is my only choice to go through trial and error here? Thanks!
BR, George
-- Alex
-- Sent via mobile, please forgive typos and brevity.
Hello,
indeed, there is some inconsistency here brought in after the merge with SIP Express Router back in 2008.
Traditionally, Kamailio (or openser before 2008) didn't support concatenation expressions as function parameters. That came via SER branch and it is not available everywhere in a consistent and coherent way (even for the code of modules imported from SER).
If you want to be in the safe side from Kamailio behaviour point of view, as I never got into using SER style, here are some hints based on what I typically do:
- use concatenation expressions with '+' only in assignments, like:
$var(x) = "string1" + "string2";
- use dynamic strings with variables as function parameters, like:
xlog("L_ERR", "string $var(x) more-string $var(y)\n");
- use and assignment to a private variable first or $_s(...) variable on positions that expect a string or a var, such as transformation parameters, like:
$var(r) = "repl1" + "repl2"; $var(x) = $(var(y){s.replace,match,$var(r)});
Cheers, Daniel
On 02.05.18 17:05, Alex Balashov wrote:
You are correct that concatenation vs interpolation works and doesn't work in slightly inconsistent ways, for instance function string arguments vs assignment.
The $_s() transformation may help make life easier.
On May 2, 2018 10:50:53 AM EDT, George Diamantopoulos georgediam@gmail.com wrote:
Hello,
I'm a a bit confused as to when string concatenation will work in kamailio config file. So far I have identified the following cases:
Working:
As part of the right hand side of an assignment: $var(x) = "string1" + "string2";
String argument of some functions: xlog("L_ERR", "string1" + "string2" + DEFINED_STRING + '\n');
Conditional Statements: if ( ru =~ "$" + "sip:" + $var(user) ) {
Not Working:
In string transformations (the fact that quotes is not part of the argument should give a hint though: $var(x) = $(var(y){s.replace,match,"repl1" + "repl2"});
String argument of some other functions: append_hf_value("P-Asserted-Identity", "<" + "$var(new_pai)" + ">"); lookup("location", "sip:$var(username)@" + SIP_REALM_DEFINITION));
In module parameter definitions: modparam("nathelper", "sipping_from", "sip:keepalive@" + SIP_REALM_DEFINITION)
So far, I have found this to be fairly consistent, except for use within functions. For example, string concatenation works for the second argument of xlog, but not for the string arguments of append_hf_value() or lookup().
Is there anyway to know this for each function beforehand, or is my only choice to go through trial and error here? Thanks!
BR, George
-- Alex
-- Sent via mobile, please forgive typos and brevity.
Kamailio (SER) - Users Mailing List sr-users@lists.kamailio.org https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
Thank you both, this has been quite informative. I wasn't also aware of the $_s() PV, it's handy indeed. Three more clarifications:
- Will $_s() work before the routing part of the config file, and specifically with pre-processor definitions? For example in the following:
modparam("nathelper", "sipping_from", $_s(sip:keepalive@SIP_REALM_DEFINITION ))?
- Will providing PVs in a dynamic string work with all functions, even for those currently not supporting string concatenation? E.g.:
append_hf_value("P-Asserted-Identity", "<$var(new_pai)>");
If yes, I assume this will only work for PVs, as preprocessor defined strings won't be substituted if the parser finds their labels enclosed in quotes. In other words, the following won't work: append_hf_value("P-Asserted-Identity", "<$var(new_pai);SOME_DEF>");
- I assumed this would work in the first example, but maybe I should ask explicitly. Will pre-processor defines work with the $_s() PV? Is this valid at all?
$_s($ru;SOME_PREPROC_DEF=$var(value))
BR, George
On 2 May 2018 at 20:07, Daniel-Constantin Mierla miconda@gmail.com wrote:
Hello,
indeed, there is some inconsistency here brought in after the merge with SIP Express Router back in 2008.
Traditionally, Kamailio (or openser before 2008) didn't support concatenation expressions as function parameters. That came via SER branch and it is not available everywhere in a consistent and coherent way (even for the code of modules imported from SER).
If you want to be in the safe side from Kamailio behaviour point of view, as I never got into using SER style, here are some hints based on what I typically do:
- use concatenation expressions with '+' only in assignments, like:
$var(x) = "string1" + "string2";
- use dynamic strings with variables as function parameters, like:
xlog("L_ERR", "string $var(x) more-string $var(y)\n");
- use and assignment to a private variable first or $_s(...) variable
on positions that expect a string or a var, such as transformation parameters, like:
$var(r) = "repl1" + "repl2"; $var(x) = $(var(y){s.replace,match,$var(r)});
Cheers, Daniel
On 02.05.18 17:05, Alex Balashov wrote:
You are correct that concatenation vs interpolation works and doesn't
work in slightly inconsistent ways, for instance function string arguments vs assignment.
The $_s() transformation may help make life easier.
On May 2, 2018 10:50:53 AM EDT, George Diamantopoulos <
georgediam@gmail.com> wrote:
Hello,
I'm a a bit confused as to when string concatenation will work in kamailio config file. So far I have identified the following cases:
Working:
As part of the right hand side of an assignment: $var(x) = "string1" + "string2";
String argument of some functions: xlog("L_ERR", "string1" + "string2" + DEFINED_STRING + '\n');
Conditional Statements: if ( ru =~ "$" + "sip:" + $var(user) ) {
Not Working:
In string transformations (the fact that quotes is not part of the argument should give a hint though: $var(x) = $(var(y){s.replace,match,"repl1" + "repl2"});
String argument of some other functions: append_hf_value("P-Asserted-Identity", "<" + "$var(new_pai)" + ">"); lookup("location", "sip:$var(username)@" + SIP_REALM_DEFINITION));
In module parameter definitions: modparam("nathelper", "sipping_from", "sip:keepalive@" + SIP_REALM_DEFINITION)
So far, I have found this to be fairly consistent, except for use within functions. For example, string concatenation works for the second argument of xlog, but not for the string arguments of append_hf_value() or lookup().
Is there anyway to know this for each function beforehand, or is my only choice to go through trial and error here? Thanks!
BR, George
-- Alex
-- Sent via mobile, please forgive typos and brevity.
Kamailio (SER) - Users Mailing List sr-users@lists.kamailio.org https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
-- Daniel-Constantin Mierla www.twitter.com/miconda -- www.linkedin.com/in/miconda Kamailio World Conference - May 14-16, 2018 - www.kamailioworld.com
Kamailio (SER) - Users Mailing List sr-users@lists.kamailio.org https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
On Wed, May 02, 2018 at 08:52:03PM +0300, George Diamantopoulos wrote:
- Will $_s() work before the routing part of the config file, and
specifically with pre-processor definitions? For example in the following:
modparam("nathelper", "sipping_from", $_s(sip:keepalive@SIP_REALM_DEFINITION ))?
No. Transformations work in route script only.
- Will providing PVs in a dynamic string work with all functions, even for
those currently not supporting string concatenation? E.g.:
append_hf_value("P-Asserted-Identity", "<$var(new_pai)>");
Only for functions which support PV interpolation. That's most functions nowadays, but not some of the older ones from OpenSER <= 1.2.
If yes, I assume this will only work for PVs, as preprocessor defined strings won't be substituted if the parser finds their labels enclosed in quotes. In other words, the following won't work: append_hf_value("P-Asserted-Identity", "<$var(new_pai);SOME_DEF>");
It will if SOME_DEF was defined via #!subst.
- I assumed this would work in the first example, but maybe I should ask
explicitly. Will pre-processor defines work with the $_s() PV? Is this valid at all?
$_s($ru;SOME_PREPROC_DEF=$var(value))
Not if SOME_PREPROC_DEF is a #!defined constant, no. But it will work if it's a #!subst, as is also the case if it appears in strong constants.
#!subst "/SOME_PREPROC_DEF/xyz/"
$var(x) = 'abc123'; xlog("L_INFO", "$_s($ru;SOME_PREPROC_DEF=$var(x))\n");
May 02 13:57:42 allegro-1.evaristesys.com /usr/local/sbin/kamailio[13998]: INFO: <script>: sip:s@10.150.20.2;xyz=abc123
-- Alex