sznoname created an issue (kamailio/kamailio#4600)

Kamailio version: v6.1.0

Operating System: Debian 12  

cat kamailio.cfg:  

```
#!define DBGLEVEL 2

#!define FLT_NATS 5

#!define FLB_NATB 6
#!define FLB_NATSIPPING 7

debug=DBGLEVEL
log_stderror=no

memdbg=5
memlog=5

log_facility=LOG_LOCAL0
log_prefix="{$mt $hdr(CSeq) $ci} "
auto_aliases=no

loadmodule "jsonrpcs.so"
loadmodule "kex.so"
loadmodule "corex.so"
loadmodule "tm.so"
loadmodule "tmx.so"
loadmodule "sl.so"
loadmodule "rr.so"
loadmodule "pv.so"
loadmodule "maxfwd.so"
loadmodule "usrloc.so"
loadmodule "registrar.so"
loadmodule "textops.so"
loadmodule "textopsx.so"
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "ctl.so"
loadmodule "cfg_rpc.so"
loadmodule "counters.so"
loadmodule "kemix.so"

loadmodule "nathelper.so"
loadmodule "rtpengine.so"

loadmodule "dialog.so"

loadmodule "app_lua.so"

modparam("tm", "failure_reply_mode", 3)
modparam("tm", "fr_timer", 30000)
modparam("tm", "fr_inv_timer", 120000)

modparam("rr", "enable_full_lr", 0)
modparam("rr", "append_fromtag", 0)

modparam("registrar", "method_filtering", 1)
modparam("registrar", "max_expires", 3600)

modparam("usrloc", "preload", "location")
modparam("usrloc", "timer_interval", 60)
modparam("usrloc", "timer_procs", 1)
modparam("usrloc", "use_domain", 0)

modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:2223")

modparam("nathelper", "natping_interval", 30)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
modparam("nathelper", "sipping_from", "sip:[email protected]")

modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
modparam("usrloc", "nat_bflag", FLB_NATB)

modparam("dialog", "event_callback", "ksr_dialog_event")

modparam("app_lua", "load", "/etc/kamailio/kamailio.lua")
cfgengine = "lua"
```

cat  kamailio.lua:  

```
FLT_NATS=5

FLB_NATB=6
FLB_NATSIPPING=7

-- SIP request routing
-- equivalent of request_route{}
function ksr_request_route()

        -- per request initial checks
        ksr_route_reqinit();

        -- NAT detection
        ksr_route_natdetect();

        -- CANCEL processing
        if KSR.is_CANCEL() then
                if KSR.tm.t_check_trans()>0 then
                        ksr_route_relay();
                end
                return 1;
        end

        -- handle retransmissions
        if not KSR.is_ACK() then
                if KSR.tmx.t_precheck_trans()>0 then
                        KSR.tm.t_check_trans();
                        return 1;
                end
                if KSR.tm.t_check_trans()==0 then return 1 end
        end

        -- handle requests within SIP dialogs
        ksr_route_withindlg();

        -- -- only initial requests (no To tag)

        -- authentication
        ksr_route_auth();

        -- record routing for dialog forming requests (in case they are routed)
        -- - remove preloaded route headers
        KSR.hdr.remove("Route");
        -- if INVITE or SUBSCRIBE
        if KSR.is_method_in("IS") then
                KSR.rr.record_route();
                KSR.dialog.dlg_manage();
        end

        -- -- requests for my local domains

        -- handle registrations
        ksr_route_registrar();

        if KSR.corex.has_ruri_user() < 0 then
                -- request with no Username in RURI
                KSR.sl.sl_send_reply(484, "Address Incomplete");
                return 1;
        end

        -- user location service
        ksr_route_location();

        return 1;
end

-- wrapper around tm relay function
function ksr_route_relay()
        -- enable additional event routes for forwarded requests
        -- - serial forking, RTP relaying handling, a.s.o.
        if KSR.is_method_in("IBSU") then
                if KSR.tm.t_is_set("branch_route")<0 then
                        KSR.tm.t_on_branch("ksr_branch_manage");
                end
        end
        if KSR.is_method_in("ISU") then
                if KSR.tm.t_is_set("onreply_route")<0 then
                        KSR.tm.t_on_reply("ksr_onreply_manage");
                end
        end

        if KSR.is_INVITE() then
                if KSR.tm.t_is_set("failure_route")<0 then
                        KSR.tm.t_on_failure("ksr_failure_manage");
                end
        end

        if KSR.tm.t_relay()<0 then
                KSR.sl.sl_reply_error();
        end
        KSR.x.exit();
end


-- Per SIP request initial checks
function ksr_route_reqinit()
        -- no connect for sending replies
        KSR.set_reply_no_connect();
        -- enforce symmetric signaling
        -- send back replies to the source address of request
        KSR.force_rport();

        if KSR.maxfwd.process_maxfwd(10) < 0 then
                KSR.sl.sl_send_reply(483, "Too Many Hops");
                KSR.x.exit();
        end

        if KSR.is_OPTIONS() then
                KSR.sl.sl_send_reply(200, "Keepalive");
                KSR.x.exit();
        end

        if KSR.sanity.sanity_check(17895, 7)<0 then
                KSR.err("malformed SIP message from "
                                .. KSR.kx.get_srcip() .. ":" .. 
KSR.kx.get_srcport() .."\n");
                KSR.x.exit();
        end
end

-- Handle requests within SIP dialogs
function ksr_route_withindlg()
        if KSR.siputils.has_totag()<0 then return 1; end

        -- sequential request within a dialog should
        -- take the path determined by record-routing
        if KSR.rr.loose_route()>0 then
                ksr_route_dlguri();
                if KSR.is_ACK() then
                        -- ACK is forwarded statelessly
                        ksr_route_natmanage();
                elseif KSR.is_NOTIFY() then
                        -- Add Record-Route for in-dialog NOTIFY as per RFC 
6665.
                        KSR.rr.record_route();
                end
                ksr_route_relay();
                KSR.x.exit();
        end
        if KSR.is_ACK() then
                if KSR.tm.t_check_trans() >0 then
                        -- no loose-route, but stateful ACK;
                        -- must be an ACK after a 487
                        -- or e.g. 404 from upstream server
                        ksr_route_relay();
                        KSR.x.exit();
                else
                        -- ACK without matching transaction ... ignore and 
discard
                        KSR.x.exit();
                end
        end
        KSR.sl.sl_send_reply(404, "Not here");
        KSR.x.exit();
end

-- Handle SIP registrations
function ksr_route_registrar()
        if not KSR.is_REGISTER() then return 1; end
        if KSR.isflagset(FLT_NATS) then
                KSR.setbflag(FLB_NATB);
                -- do SIP NAT pinging
                KSR.setbflag(FLB_NATSIPPING);
        end
        if KSR.registrar.save("location", 0)<0 then
                KSR.sl.sl_reply_error();
        end
        KSR.x.exit();
end

-- User location service
function ksr_route_location()
        local rc = KSR.registrar.lookup("location");
        if rc<0 then
                KSR.tm.t_newtran();
                if rc==-1 or rc==-3 then
                        KSR.sl.send_reply(404, "Not Found");
                        KSR.x.exit();
                elseif rc==-2 then
                        KSR.sl.send_reply(405, "Method Not Allowed");
                        KSR.x.exit();
                end
        end

        ksr_route_relay();
        KSR.x.exit();
end

-- IP authorization and user authentication
function ksr_route_auth()
        return 1;
end

-- Caller NAT detection
function ksr_route_natdetect()
        if KSR.nathelper.nat_uac_test(19)>0 then
                if KSR.is_REGISTER() then
                        KSR.nathelper.fix_nated_register();
                elseif KSR.siputils.is_first_hop()>0 then
                        KSR.nathelper.set_contact_alias();
                end
                KSR.setflag(FLT_NATS);
        end
        return 1;
end

-- RTPProxy control
function ksr_route_natmanage()
        if KSR.siputils.is_request()>0 then
                if KSR.siputils.has_totag()>0 then
                        if KSR.rr.check_route_param("nat=yes")>0 then
                                KSR.setbflag(FLB_NATB);
                        end
                end
        end
        if (not (KSR.isflagset(FLT_NATS) or KSR.isbflagset(FLB_NATB))) then
                return 1;
        end

        KSR.rtpengine.rtpengine_manage("replace-origin 
replace-session-connection");

        if KSR.siputils.is_request()>0 then
                if KSR.siputils.has_totag()<0 then
                        if KSR.tmx.t_is_branch_route()>0 then
                                KSR.rr.add_rr_param(";nat=yes");
                        end
                end
        end
        if KSR.siputils.is_reply()>0 then
                if KSR.isbflagset(FLB_NATB) then
                        KSR.nathelper.set_contact_alias();
                end
        end
        return 1;
end

-- URI update for dialog requests
function ksr_route_dlguri()
        if not KSR.nathelper then
                return 1;
        end
        if not KSR.isdsturiset() then
                KSR.nathelper.handle_ruri_alias();
        end
        return 1;
end

-- Routing to foreign domains
function ksr_route_sipout()
        if KSR.is_myself_ruri() then return 1; end

        KSR.hdr.append("P-Hint: outbound\r\n");
        ksr_route_relay();
        KSR.x.exit();
end

-- Manage outgoing branches
-- equivalent of branch_route[...]{}
function ksr_branch_manage()
        KSR.dbg("new branch [".. KSR.pv.get("$T_branch_idx")
                                .. "] to " .. KSR.kx.get_ruri() .. "\n");
        ksr_route_natmanage();
        return 1;
end

-- Manage incoming replies
-- equivalent of onreply_route[...]{}
function ksr_onreply_manage()
        local scode = KSR.kx.get_status();
        KSR.info("incoming reply(" .. scode .. ")\n");
        if scode>100 and scode<299 then
                ksr_route_natmanage();
        end
        return 1;
end

-- Manage failure routing cases
-- equivalent of failure_route[...]{}
function ksr_failure_manage()
        ksr_route_natmanage();

        if KSR.tm.t_is_canceled()>0 then
                return 1;
        end
        return 1;
end

-- SIP response handling
-- equivalent of reply_route{}
function ksr_reply_route()
        KSR.dbg("response - from kamailio lua script\n");
        if KSR.sanity.sanity_check(17604, 6)<0 then
                KSR.err("malformed SIP response from "
                                .. KSR.kx.get_srcip() .. ":" .. 
KSR.kx.get_srcport() .."\n");
                KSR.x.drop();
        end
        return 1;
end

-- event callback function implemented in Lua
function ksr_dialog_event(evname)
        KSR.info("===== dialog module triggered event: " .. evname .. "\n");
        return 1;
end
```
cat  kamailio.log:  

```
INFO: {2 1 INVITE MzYxOTcwODAwZTliYjkxOGM0OGZhODU5ZjI1MDZkODY.} <core> 
[core/kemi.c:109]: sr_kemi_core_info(): incoming reply(180)
INFO: {2 1 INVITE MzYxOTcwODAwZTliYjkxOGM0OGZhODU5ZjI1MDZkODY.} <core> 
[core/kemi.c:109]: sr_kemi_core_info(): ===== dialog module triggered event: 
unknown
INFO: {2 1 INVITE MzYxOTcwODAwZTliYjkxOGM0OGZhODU5ZjI1MDZkODY.} <core> 
[core/kemi.c:109]: sr_kemi_core_info(): incoming reply(200)
INFO: {2 1 INVITE MzYxOTcwODAwZTliYjkxOGM0OGZhODU5ZjI1MDZkODY.} <core> 
[core/kemi.c:109]: sr_kemi_core_info(): ===== dialog module triggered event: 
dialog:start
INFO: {1 1 ACK MzYxOTcwODAwZTliYjkxOGM0OGZhODU5ZjI1MDZkODY.} <core> 
[core/kemi.c:109]: sr_kemi_core_info(): ===== dialog module triggered event: 
unknown
INFO: {1 2 BYE MzYxOTcwODAwZTliYjkxOGM0OGZhODU5ZjI1MDZkODY.} <core> 
[core/kemi.c:109]: sr_kemi_core_info(): ===== dialog module triggered event: 
dialog:end
```



-- 
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/issues/4600
You are receiving this because you are subscribed to this thread.

Message ID: <kamailio/kamailio/issues/[email protected]>
_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to