Hello everyone,

I'm doing some custom Kamailio configuration to achieve few things but seem to be stuck with SIP trunks. I'm currently using Kamailio 4.4.5 and here is what I have at the moment:

1. Asterisk 11.25.1 servers behing Kamailio using RealTime for SIP peers.

2. Route that checks if packet was sent from one of configured SIP trunks. This part works correctly:
route[FROMPSTN] {
        xlog("== FROMPSTN ==\n");
        $var(socket) = $si + ":" + $sp;
        xlog("**** $var(socket)\n");
        xlog("**** $(sht(trunks_kamailio=>$var(socket)))\n");
        if ($(sht(trunks_kamailio=>$var(socket))) != $null) {
                xlog("**** From $(sht(trunks_kamailio=>$var(socket))) PSTN\n");
                return 1;
                }
        xlog("**** Not from PSTN\n");
        return -1;
}

3. Here is a fragment that sends INVITE packet to TOASTERISK route if it was received from one of trunks. I have 4444444444@XXX.XXX.XXX.XXX hardcoded for now, and in my configuration 4444444444 and XXX.XXX.XXX.XXX represent my real caller id number and carrier's IP address:

if(route(FROMPSTN)) {
        uac_replace_from("sip:4444444444@XXX.XXX.XXX.XXX");
        route(TOASTERISK);
}

4. TOASTERISK basically has folloving fragment that load balances traffic between Asterisk nodes:

if(!ds_select_dst("0", "4")) {
        xlog("**** Call was not sent to none of Asterisk hosts\n");
        if(!is_method("REGISTER")) {
                xlog("**** Method is not REGISTER\n");
                send_reply("404", "No destination");
        }
        xlog("**** Dispatcher fail\n");
        exit;
}

So when I'm making a call to my test number, I can see following incomming packet in Asterisk from Kamailio. From header has correct caller id number and carrier's IP address (192.168.88.5 is Kamailio's internal IP address):
Вбудоване зображення 1

And when Asterisk tries to find a maching peer for this incoming call, it is using Kamailio's instead of Carrier's IP address. So it is actually finds one of peers (101-XXXXXXX) that is being authenticated on Kamailio as many others:
Вбудоване зображення 2

I'm not sure if Asterisk is just using packet's source IP to lookup for corresponding peer or something else from it's content. I will really appreciate any help on this.

Thanks a lot!