[SR-Users] How do I make subst_hf() use variables in search (not replacement)?

Alex Villací­s Lasso a_villacis at palosanto.com
Mon Aug 11 21:02:09 CEST 2014


El 11/08/14 02:30, Daniel-Constantin Mierla escribió:
> I haven't looked at the code, but probably the function doesn't accept variable for the matching regular expression.
>
> If the IP/port of asterisk are not changed, you can try:
>
> #!substdef "/ASTERISKIP/127.0.0.1/"
> #!substdef "/ASTERISKPORT/5080/"
>
> asterisk.bindip = "ASTERISKIP" ...
> asterisk.bindport = ASTERISKPORT ...
>
> subst_hf("Contact", "/ASTERISKIP:ASTERISKPORT/$td/", "a");
>
> Cheers,
> Daniel
>
> On 08/08/14 23:38, Alex Villací­s Lasso wrote:
>> Consider the following snippet:
>>
>> if (is_present_hf("Contact")) {
>>         xlog("L_ALERT", "===== reply to SUBSCRIBE has Contact: $ct\n");
>>         xlog("L_ALERT", "===== want to replace with $td\n");
>>         xlog("L_ALERT", "===== regexp to use is /$sel(cfg_get.asterisk.bindip):$sel(cfg_get.asterisk.bindport)/$td/\n");
>>         if (subst_hf("Contact", "/127.0.0.1:5080/$td/", "a")) {
>>                 xlog("L_ALERT", "===== reply had Contact modified\n");
>>         }
>> }
>>
>> If I use the hardcoded regexp "/127.0.0.1:5080/$td/", subst_hf() replaces the Contact value correctly.
>>
>> However, if I use "/$sel(cfg_get.asterisk.bindip):$sel(cfg_get.asterisk.bindport)/$td/" (where asterisk.bindip and asterisk.bindport are the supposed values 127.0.0.1 and 5080), subst_hf() fails to replace the Contact value.
>>
>> How do I make use of the existing configuration variables in order to substitute in the Contact header?
>>
>> _______________________________________________
>> SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
>> sr-users at lists.sip-router.org
>> http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
>
Thanks for the tip.

However, I also have an issue - subst_hf() does not seem to be working. Consider the following route, of which the snippet quoted above is a part:

# manage incoming replies
onreply_route[MANAGE_REPLY] {
         # run rtpproxy resolution
         route(MHOMED_ELASTIX);

         xdbg("incoming reply\n");
         if(status=~"[12][0-9][0-9]")
                 route(NATMANAGE);

         if (is_method("SUBSCRIBE") && route(FROMASTERISK)) {
                 #xlog("L_ALERT", "===== reply to SUBSCRIBE, coming from asterisk, Call-ID is $ci\n");
                 $sht(pending_subscribe=>$ci) = 0;

                 if (is_present_hf("Contact")) {
                         xlog("L_ALERT", "===== reply to SUBSCRIBE has Contact: $ct\n");
                         xlog("L_ALERT", "===== want to replace with $td\n");
                         xlog("L_ALERT", "===== regexp to use is /$sel(cfg_get.asterisk.bindip):$sel(cfg_get.asterisk.bindport)/$td/\n");
                         if (subst_hf("Contact", "/127.0.0.1:5080/$td/", "a")) {
                                 xlog("L_ALERT", "===== reply had Contact modified\n");
                         }
                 }
         }

         # manage websocket reply
         if (nat_uac_test(64)) {
                 # Do NAT traversal stuff for replies to a WebSocket connection
                 # - even if it is not behind a NAT!
                 # This won't be needed in the future if Kamailio and the
                 # WebSocket client support Outbound and Path.
                 add_contact_alias();
         }
}


When I run this with debug=4, I get the following:

Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [socket_info.c:583]: grep_sock_info(): grep_sock_info - checking if host==us: 9==9 && [127.0.0.1] == [127.0.0.1]
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [dset.c:769]: uri_add_rcv_alias(): encoded <sip:102 at 127.0.0.1:5080> => [sip:102 at 127.0.0.1:5080;alias=127.0.0.1~5080~1
]
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [select.c:429]: run_select(): Calling SELECT 0x7f8e431a3d90
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [select.c:429]: run_select(): Calling SELECT 0x7f8e43292ac0
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: htable [ht_var.c:90]: pv_set_ht_cell(): set value for $ht(pending_subscribe=>6a2222bca4cc2be8)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: ALERT: <script>: ===== reply to SUBSCRIBE has Contact: <sip:102 at 127.0.0.1:5080>;expires=3600
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: ALERT: <script>: ===== want to replace with pbx.villacis.com
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [select.c:429]: run_select(): Calling SELECT 0x7f8e431a3d90
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [select.c:429]: run_select(): Calling SELECT 0x7f8e43292ac0
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: ALERT: <script>: ===== regexp to use is /127.0.0.1:5080/pbx.villacis.com/
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [re.c:454]: subst_run(): subst_run: running. r=0
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [re.c:475]: subst_run(): subst_run: matched (9, 14): [127.0.0.1:5080]
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: textops [textops.c:2285]: subst_hf_f(): textops replacing at offset 849 [127.0.0.1:5080] with [pbx.villacis.com]
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: textops [textops.c:2351]: subst_hf_f(): lst was 0x7f8e432e4918
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: ALERT: <script>: ===== reply had Contact modified
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: tm [t_reply.c:1304]: t_should_relay_response(): ->>>>>>>>> T_code=0, new_code=200
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: tm [t_reply.c:1822]: relay_reply(): DEBUG: relay_reply: branch=0, save=0, relay=0 icode=0
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [msg_translator.c:2009]: generate_res_buf_from_sip_res():  old size: 900, new size: 813
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [msg_translator.c:2027]: generate_res_buf_from_sip_res(): copied size: orig:863, new: 776, rest: 37 msg=#012SIP/2.0 200 OK#015#012Via: SIP/2.0/UDP 
192.168.2.100:5060;rport=5060;branch=z9hG4bK036380ac70257f0f5#015#012Record-Route: <sip:127.0.0.1;r2=on;lr=on;ftag=239adbead7;vsf=cBoZSlANEgEdDBcAFx8lMBIaVlgfBQANAgoaXU0MAi0xMjcuMC4wLjE6NTA4MA--;nat=yes>#015#012Record-Route: 
<sip:192.168.2.18;r2=on;lr=on;ftag=239adbead7;vsf=cBoZSlANEgEdDBcAFx8lMBIaVlgfBQANAgoaXU0MAi0xMjcuMC4wLjE6NTA4MA--;nat=yes>#015#012From: "jgutierrez" <sip:jgutierrez at pbx.villacis.com>;tag=239adbead7#015#012To: "" 
<sip:102 at pbx.villacis.com>;tag=as2e48d5d6#015#012Call-ID: 6a2222bca4cc2be8#015#012CSeq: 22329 SUBSCRIBE#015#012Server: Asterisk PBX 11.11.0#015#012Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE#015#012Supported: 
replaces, timer#015#012Expires: 3600#015#012Contact: <sip:102 at 127.0.0.1:5080;alias=127.0.0.1~5080~1pbx.villacis.com>;expires=3600#015#012Content-Length: 0#015#012#015#012
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [mem/shm_mem.c:111]: _shm_resize(): WARNING:vqm_resize: resize(0) called
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: tm [t_reply.c:1663]: cleanup_uac_timers(): DEBUG: cleanup_uac_timers: RETR/FR timers reset
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [usr_avp.c:644]: destroy_avp_list(): DEBUG:destroy_avp_list: destroying list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [usr_avp.c:644]: destroy_avp_list(): DEBUG:destroy_avp_list: destroying list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [usr_avp.c:644]: destroy_avp_list(): DEBUG:destroy_avp_list: destroying list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [usr_avp.c:644]: destroy_avp_list(): DEBUG:destroy_avp_list: destroying list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [usr_avp.c:644]: destroy_avp_list(): DEBUG:destroy_avp_list: destroying list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [usr_avp.c:644]: destroy_avp_list(): DEBUG:destroy_avp_list: destroying list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [xavp.c:448]: xavp_destroy_list(): destroying xavp list (nil)
Aug 11 12:52:53 elx3 /usr/sbin/kamailio[2971]: DEBUG: <core> [receive.c:296]: receive_msg(): receive_msg: cleaning up

Apparently the search matched, and the replace was made. However, the replacement is incorrect:

Contact: <sip:102 at 127.0.0.1:5080;alias=127.0.0.1~5080~1pbx.villacis.com>;expires=3600

What I going on? Is the regexp incorrect? What I want for the Contact header is this:

Contact: <sip:102 at pbx.villacis.com>;expires=3600

The documentation says the regexp is perl-style, and a sample running with perl shows the regexp to work correctly and to do what I want.



More information about the sr-users mailing list