On 29 April 2013 14:51, Aldo Antignano <aldo@antignano.it> wrote:
I have read and applied the excellent guide found on:
http://kb.asipto.com/asterisk:realtime:kamailio-3.3.x-asterisk-10.7.0-astdb

Now I have added to Kamailio the HA/Load Balancer support, with the "dispatcher" module.
This way I have 1 Kamailio and 2 Asterisk machines.

How can I change the routing logic of the sections route[REGFWD] | route[FROMASTERISK] route[TOASTERISK] to use the dispatcher module? (in the guide above the asterisk binded ip address is cabled in the kamailio config code)

I have done that. Relevant route entries below.

# Forward REGISTER to Asterisk
route[REGFWD] {
if(!is_method("REGISTER"))
{
return;
}

# for each Asterisk server in setid=2 (External) we send a registration on behalf of the user.
sql_xquery("ca", "SELECT SUBSTRING_INDEX(destination,':',-1) AS port, SUBSTRING_INDEX(SUBSTRING(destination,5),':',1) AS address 
FROM dispatcher WHERE setid = 2", "ra");
$var(i) = 0;
    while($xavp(ra[$var(i)]) != $null)
    {
$var(rip) = $xavp(ra[$var(i)]=>address);
$uac_req(method)="REGISTER";
$uac_req(ruri)="sip:" + $var(rip) + ":" + $xavp(ra[$var(i)]=>port);
$uac_req(furi)="sip:" + $au + "@" + $var(rip);
$uac_req(turi)="sip:" + $au + "@" + $var(rip);
$uac_req(hdrs)="Contact: <sip:" + $au +  "@"
+ $sel(cfg_get.kamailio.bindip)
+ ":" + $sel(cfg_get.kamailio.bindport) + ">\r\n";
if($sel(contact.expires) != $null)
$uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $sel(contact.expires) + "\r\n";
else
$uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $hdr(Expires) + "\r\n";
uac_req_send();
        $var(i) = $var(i) + 1;
    }
sql_result_free("ra");

}

# Test if coming from Asterisk. We check the dispatcher "ds_is_from_list()" function to see if this is one of our Asterisk IPs
route[FROMASTERISK] {
if(ds_is_from_list())
{
return 1;
} else {
return -1;
}
}
 
# Send to Asterisk
route[TOASTERISK] {

# If call comes in to the .75 iface, we need to send it to the .75 iface of Asterisk as well. 
# otherwise we send to the .76 iface. We do this by calling different dispatcher sets. This is 
# because Asterisk needs to use NAT on the .76. (public) interface but not on the .75.
if($td=~"10.5.75.")
{
$var(setid) = 4;
xlog("SCRIPT: Call to 10.5.75. ip - using set $var(setid) \n");
} else {
$var(setid) = 2;
xlog("SCRIPT: Call from $fn to 10.5.76. ip - using set $var(setid) \n");
}
    # round robin dispatching on set determined above
    if(!ds_select_dst($var(setid), "4"))
    {
        send_reply("404", "No destination");
        exit;
    }
    t_on_failure("RTF_DISPATCH");
    route(RELAY);
  exit;
}




Hope this helps.

-Barry Flanagan