Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in dns_cache.c with dns_cache_init=off #3350

Closed
dilyanpalauzov opened this issue Jan 27, 2023 · 18 comments
Closed

Crash in dns_cache.c with dns_cache_init=off #3350

dilyanpalauzov opened this issue Jan 27, 2023 · 18 comments

Comments

@dilyanpalauzov
Copy link
Contributor

With Kamailio 5.6.3 I add this code

 -- Routing to foreign domains
 function ksr_route_sipout()
+        local y = KSR.enum.enum_query()
+        KSR.err('ENUM RESULT ' .. tostring(y) .. ' :RURI ' .. KSR.kx.get_ruri() .. '\n')

         if KSR.is_myself_ruri() then return 1; end
 
         KSR.hdr.append("P-Hint: outbound\r\n");

and dial +437200101011 . The code above produces the output “ENUM RESULT 1 :RURI sip:enum-test@sip.nemox.net”.

I have the DNS-related settings:

dns_try_naptr=on
use_dns_cache=off
dns_cache_init=off

Kamailio logs during the dial-in process:

INVITE sip:+437200101011@sip.example.org SIP/2.0
Via: SIP/2.0/UDP 192.168.0.199:40867;rport;branch=z9hG4bKU2Fc14H2NX4Kc
Max-Forwards: 70
From: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B
To: <sip:+437200101011@sip.example.org>
Call-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b
CSeq: 62865320 INVITE
Contact: <sip:myself@sip.example.org>
User-Agent: calls sofia-sip/1.13.12
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE
Supported: timer, 100rel, replaces, gruu, outbound
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 240

v=0
o=- 9163509233734166984 5909793187606445082 IN IP4 192.168.0.199
s=-
c=IN IP4 22.222.222.222
t=0 0
m=audio 43264 RTP/AVP 9 8 0 3
a=rtpmap:9 G722/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:3 GSM/8000
a=rtcp:33730

 6(7) ERROR: <core> [core/kemi.c:96]: sr_kemi_core_err(): ksr_sipdump_event 1--------- sipdump:msg
SIP/2.0 407 Proxy Authentication Required
Via: SIP/2.0/UDP 192.168.0.199:40867;rport=40867;branch=z9hG4bKU2Fc14H2NX4Kc;received=22.222.222.222
From: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B
To: <sip:+437200101011@sip.example.org>;tag=44af9dbde0a62ae84f5c1ff272adb901.20dd651d
Call-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b
CSeq: 62865320 INVITE
Proxy-Authenticate: Digest realm="example.int", nonce="Y9QB/GPUANBJum6LTOLsDnjaSLKc/8o1A1zk1oUtFXzlFe/OfiMEAg=="
Content-Length: 0



 8(9) ERROR: <core> [core/kemi.c:96]: sr_kemi_core_err(): ksr_sipdump_event 1--------- sipdump:msg
ACK sip:+437200101011@sip.example.org SIP/2.0
Via: SIP/2.0/UDP 192.168.0.199:40867;rport;branch=z9hG4bKU2Fc14H2NX4Kc
Max-Forwards: 70
From: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B
To: <sip:+437200101011@sip.example.org>;tag=44af9dbde0a62ae84f5c1ff272adb901.20dd651d
Call-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b
CSeq: 62865320 ACK
Content-Length: 0


 5(6) ERROR: <core> [core/kemi.c:96]: sr_kemi_core_err(): ksr_sipdump_event 1--------- sipdump:msg
INVITE sip:+437200101011@sip.example.org SIP/2.0
Via: SIP/2.0/UDP 192.168.0.199:40867;rport;branch=z9hG4bKvB942Z25j6t6Q
Max-Forwards: 70
From: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B
To: <sip:+437200101011@sip.example.org>
Call-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b
CSeq: 62865321 INVITE
Contact: <sip:myself@sip.example.org>
User-Agent: calls sofia-sip/1.13.12
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE
Supported: timer, 100rel, replaces, gruu, outbound
Proxy-Authorization: Digest username="myself", realm="example.int", nonce="Y9QB/GPUANBJum6LTOLsDnjaSLKc/8o1A1zk1oUtFXzlFe/OfiMEAg==", algorithm=MD5, uri="sip:+437200101011@sip.example.org", response="da2f40487bb63329105222dab911d9bd"
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 240

v=0
o=- 9163509233734166984 5909793187606445082 IN IP4 192.168.0.199
s=-
c=IN IP4 22.222.222.222
t=0 0
m=audio 43264 RTP/AVP 9 8 0 3
a=rtpmap:9 G722/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:3 GSM/8000
a=rtcp:33730

 5(6) ERROR: <core> [core/kemi.c:96]: sr_kemi_core_err(): ENUM RESULT 1 :RURI sip:enum-test@sip.nemox.net
 5(6) ERROR: <core> [core/kemi.c:96]: sr_kemi_core_err(): ksr_sipdump_event 1--------- sipdump:msg
SIP/2.0 100 trying -- your call is important to us
Via: SIP/2.0/UDP 192.168.0.199:40867;rport=40867;branch=z9hG4bKvB942Z25j6t6Q;received=22.222.222.222
From: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B
To: <sip:+437200101011@sip.example.org>
Call-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b
CSeq: 62865321 INVITE
Authentication-Info: nextnonce="Y9QB/GPUANBJum6LTOLsDnjaSLKc/8o1A1zk1oUtFXzlFe/OfiMEAg==", qop=, rspauth="d9dd065ba472cbc3f8cc4505dbd2ad60", cnonce="", nc=
Content-Length: 0


 0(1) ALERT: <core> [main.c:774]: handle_sigs(): child process 6 exited by a signal 11
24(25) CRITICAL: <core> [core/pass_fd.c:277]: receive_fd(): EOF on 13
 0(1) ALERT: <core> [main.c:777]: handle_sigs(): core was generated

The full backtrace is:

#0  0x00000000004867e5 in atomic_cmpxchg_int (var=0x0, old=0, new_v=1) at core/mem/../atomic/atomic_x86.h:224
        ret = 32663
#1  0x0000000000486831 in futex_get (lock=0x0) at core/mem/../futexlock.h:99
        v = 0
        i = 1024
#2  0x0000000000490ce5 in dns_hash_get (name=0x7ffe2446dbc0, type=33, h=0x7ffe2446da90, err=0x7ffe2446da7c) at core/dns_cache.c:673
        e = 0x0
#3  0x00000000004972a7 in dns_get_entry (name=0x7ffe2446dbc0, type=33) at core/dns_cache.c:2001
        h = 608623712
        e = 0x0
        cname_val = {
          s = 0x0,
          len = 5860299
        }
        err = 32766
        rec_cnt = 1
        __func__ = "dns_get_entry"
#4  0x0000000000499289 in dns_srv_get_he (name=0x7ffe2446dbc0, port=0x7ffe2446df1c, flags=0) at core/dns_cache.c:2455
        e = 0x2
        rr = 0x0
        rr_name = {
          s = 0xa3e33d203 <error: Cannot access memory at address 0xa3e33d203>,
          len = 1043567768
        }
        he = 0x0
        now = 611930531
        rr_no = 0 '\000'
#5  0x0000000000597aae in no_naptr_srv_sip_resolvehost (name=0x7ffe2446e100, port=0x7ffe2446df1c, proto=0x7f973a751788 "\001") at core/resolve.c:1599
        srv_proto_list = {{
            proto = 1 '\001',
            proto_pref = 30
          }, {
            proto = 2 '\002',
            proto_pref = 20
          }, {
            proto = 4 '\004',
            proto_pref = 20
          }, {
            proto = 3 '\003',
            proto_pref = 10
          }, {
            proto = 0 '\000',
            proto_pref = 0
          }, {
            proto = 13 '\r',
            proto_pref = 1
          }, {
            proto = 80 'P',
            proto_pref = 319
          }}
        he = 0x0
        ip = 0x0
        srv_name = {
          s = 0x929320 <tmp_srv> "_sip._udp.sip.nemox.net",
          len = 23
        }
        tmp_srv = "_sip._udp.sip.nemox.net", '\000' <repeats 232 times>
        i = 0
        list_len = 4
        __func__ = "no_naptr_srv_sip_resolvehost"
#6  0x0000000000598333 in naptr_sip_resolvehost (name=0x7ffe2446e100, port=0x7ffe2446df1c, proto=0x7f973a751788 "\001") at core/resolve.c:1675
        he = 0x0
        ip = 0x0
        tmp = "sip.nemox.net", '\000' <repeats 242 times>
        l = 0x0
        naptr_head = 0x0
        n_proto = 0 '\000'
        srv_name = {
          s = 0x7f97390fee12 <sr_kemi_lua_exec_func_514> "UH\211\345H\203\354\020H\211}\370H\213E\370\276\002\002",
          len = 1051642410
        }
        tried_bmp = 0
        origproto = 0 '\000'
        __func__ = "naptr_sip_resolvehost"
#7  0x00000000005983c6 in _sip_resolvehost (name=0x7ffe2446e100, port=0x7ffe2446df1c, proto=0x7f973a751788 "\001") at core/resolve.c:1707
        res = 0x0
#8  0x000000000049943c in dns_srv_sip_resolvehost (name=0x7ffe2446e100, port=0x7ffe2446df1c, proto=0x7f973a751788 "\001") at core/dns_cache.c:2516
        he = 0x7f9739119c91 <luaD_callnoyield+449>
        ip = 0x7f97390fca97 <sr_kemi_lua_exec_func_221>
        tmp = '\000' <repeats 255 times>
        srv_name = {
          s = 0x1a67740 "\020h\247\001",
          len = 0
        }
        srv_proto = 0 '\000'
        __func__ = "dns_srv_sip_resolvehost"
#9  0x000000000049b3de in dns_sip_resolvehost (name=0x7ffe2446e100, port=0x7ffe2446df1c, proto=0x7f973a751788 "\001") at core/dns_cache.c:2738
No locals.
#10 0x000000000059846b in sip_hostport2su (su=0x7f973a751700, name=0x7ffe2446e100, port=5060, proto=0x7f973a751788 "\001") at core/resolve.c:1727
        he = 0x7f973e33d2be
        __func__ = "sip_hostport2su"
#11 0x00007f9739efdff0 in uri2dst2 (dns_h=0x7f973a751888, dst=0x7f973a7516f8, force_send_socket=0x0, sflags=..., uri=0x7f973e3390d0, proto=0) at /git/voip/kamailio/src/modules/tm/ut.h:308
        parsed_uri = {
          user = {
            s = 0x7f973e33d2b4 "enum-test@sip.nemox.net",
            len = 9
          },
          passwd = {
            s = 0x0,
            len = 0
          },
          host = {
            s = 0x7f973e33d2be "sip.nemox.net",
            len = 13
          },
          port = {
            s = 0x0,
            len = 0
          },
          params = {
            s = 0x0,
            len = 0
          },
          sip_params = {
            s = 0x0,
            len = 0
          },
          headers = {
            s = 0x0,
            len = 0
          },
          port_no = 0,
          proto = 0,
          type = SIP_URI_T,
          flags = 0,
          transport = {
            s = 0x0,
            len = 0
          },
          ttl = {
            s = 0x0,
            len = 0
          },
          user_param = {
            s = 0x0,
            len = 0
          },
          maddr = {
            s = 0x0,
            len = 0
          },
          method = {
            s = 0x0,
            len = 0
          },
          lr = {
            s = 0x0,
            len = 0
          },
          r2 = {
            s = 0x0,
            len = 0
          },
          gr = {
            s = 0x0,
            len = 0
          },
          transport_val = {
            s = 0x0,
            len = 0
          },
          ttl_val = {
            s = 0x0,
            len = 0
          },
          user_param_val = {
            s = 0x0,
            len = 0
          },
          maddr_val = {
            s = 0x0,
            len = 0
          },
          method_val = {
            s = 0x0,
            len = 0
          },
          lr_val = {
            s = 0x0,
            len = 0
          },
          r2_val = {
            s = 0x0,
            len = 0
          },
          gr_val = {
            s = 0x0,
            len = 0
          }
        }
        uri_proto = PROTO_NONE
        host = 0x7ffe2446e100
        ip_found = 0
        to = {
          s = {
            sa_family = 57744,
            sa_data = "F$\376\177\000\000\211\216\0169\227\177\000"
          },
          sin = {
            sin_family = 57744,
            sin_port = 9286,
            sin_addr = {
              s_addr = 32766
            },
            sin_zero = "\211\216\0169\227\177\000"
          },
          sin6 = {
            sin6_family = 57744,
            sin6_port = 9286,
            sin6_flowinfo = 32766,
            sin6_addr = {
              __in6_u = {
                __u6_addr8 = "\211\216\0169\227\177\000\000\001\000\000\000\000\000\000",
                __u6_addr16 = {36489, 14606, 32663, 0, 1, 0, 0, 0},
                __u6_addr32 = {957255305, 32663, 1, 0}
              }
            },
            sin6_scope_id = 1042640912
          },
          sas = {
            ss_family = 57744,
            __ss_padding = "F$\376\177\000\000\211\216\0169\227\177\000\000\001\000\000\000\000\000\000\000\020p%>\227\177\000\000\260\340F$\376\177\000\000\304\311K", '\000' <repeats 37 times>, "\020;\v:\227\177\000\000\360\2153>\227\177\000\000Px\246\001\000\000\000\000\230\2243>\227\177\000\000\002\000\000\000\000\000\000",
            __ss_align = 27947488
          }
        }
        err = 32766
        __func__ = "uri2dst2"
#12 0x00007f9739f00878 in prepare_new_uac (t=0x7f973a751408, i_req=0x7f973e338df0, branch=0, uri=0x7ffe2446e490, path=0x7ffe2446e470, next_hop=0x7f973e3390d0, fsocket=0x0, snd_flags=..., fproto=0, flags=2, instance=0x7ffe2446e460, ruid=0x7ffe2446e450, location_ua=0x7ffe2446e440) at t_fwd.c:447
        shbuf = 0x0
        add_rm_backup = 0x7f973e33b288
        body_lumps_backup = 0x0
        parsed_uri_bak = {
          user = {
            s = 0x7f973e33a634 "enum-test@sip.nemox.net",
            len = 9
          },
          passwd = {
            s = 0x0,
            len = 0
          },
          host = {
            s = 0x7f973e33a63e "sip.nemox.net",
            len = 13
          },
          port = {
            s = 0x0,
            len = 0
          },
          params = {
            s = 0x0,
            len = 0
          },
          sip_params = {
            s = 0x0,
            len = 0
          },
          headers = {
            s = 0x0,
            len = 0
          },
          port_no = 0,
          proto = 0,
          type = SIP_URI_T,
          flags = 0,
          transport = {
            s = 0x0,
            len = 0
          },
          ttl = {
            s = 0x0,
            len = 0
          },
          user_param = {
            s = 0x0,
            len = 0
          },
          maddr = {
            s = 0x0,
            len = 0
          },
          method = {
            s = 0x0,
            len = 0
          },
          lr = {
            s = 0x0,
            len = 0
          },
          r2 = {
            s = 0x0,
            len = 0
          },
          gr = {
            s = 0x0,
            len = 0
          },
          transport_val = {
            s = 0x0,
            len = 0
          },
          ttl_val = {
            s = 0x0,
            len = 0
          },
          user_param_val = {
            s = 0x0,
            len = 0
          },
          maddr_val = {
            s = 0x0,
            len = 0
          },
          method_val = {
            s = 0x0,
            len = 0
          },
          lr_val = {
            s = 0x0,
            len = 0
          },
          r2_val = {
            s = 0x0,
            len = 0
          },
          gr_val = {
            s = 0x0,
            len = 0
          }
        }
        ret = -1
        len = 32663
        parsed_uri_ok_bak = 1
        free_new_uri = 1
        msg_uri_bak = {
          s = 0x7f973e33a630 "sip:enum-test@sip.nemox.net",
          len = 27
        }
        dst_uri_bak = {
          s = 0x0,
          len = 0
        }
        dst_uri_backed_up = 1
        path_bak = {
          s = 0x0,
          len = 0
        }
        free_path = 1
        instance_bak = {
          s = 0x0,
          len = 0
        }
        free_instance = 1
        ruid_bak = {
          s = 0x0,
          len = 0
        }
        free_ruid = 1
        ua_bak = {
          s = 0x0,
          len = 0
        }
        free_ua = 1
        backup_route_type = 1
        test_dst = 23
        fwd_snd_flags_bak = {
          f = 0,
          blst_imask = 0
        }
        rpl_snd_flags_bak = {
          f = 1,
          blst_imask = 0
        }
        force_send_socket_bak = 0x0
        dst = 0x7f973a7516f8
        ctx = {
          rec_lev = 0,
          run_flags = 0,
          last_retcode = 0,
          jmp_env = {{
              __jmpbuf = {140729507046528, 107383054981, 140729507046336, 7983668, 140729507046712, 140729507046688, 0, 0},
              __mask_was_saved = 0,
              __saved_mask = {
                __val = {0, 0, 0, 0, 1016, 2510101137099545603, 12909040900923746448, 140729507046448, 7983053, 3689621524717335394, 0, 140729507046488, 140729507046688, 8, 8872581, 4179003179399}
              }
            }}
        }
        bctx = 0x0
        keng = 0x913440 <_sr_kemi_eng_list>
        __func__ = "prepare_new_uac"
#13 0x00007f9739f058f0 in add_uac (t=0x7f973a751408, request=0x7f973e338df0, uri=0x7f973e3390d0, next_hop=0x7f973e3390d0, path=0x7f973e339498, proxy=0x0, fsocket=0x0, snd_flags=..., proto=0, flags=2, instance=0x7f973e3394a8, ruid=0x7f973e3394c0, location_ua=0x7f973e3394d0) at t_fwd.c:814
        ret = 0
        branch = 0
        __func__ = "add_uac"
#14 0x00007f9739f0f73f in t_forward_nonack (t=0x7f973a751408, p_msg=0x7f973e338df0, proxy=0x0, proto=0) at t_fwd.c:1694
        branch_ret = 0
        lowest_ret = -1
        added_branches = 0
        first_branch = 0
        i = 32766
        t_invite = 0x7f973e33c570
        success_branch = 608627136
        try_new = 1
        lock_replies = 939792
        backup_bflags = 0
        obranch = {
          uri = {
            s = 0x1f600000000 <error: Cannot access memory at address 0x1f600000000>,
            len = 27594632
          },
          dst_uri = {
            s = 0x1 <error: Cannot access memory at address 0x1>,
            len = 27947488
          },
          q = 27687040,
          path = {
            s = 0x7f97390fca97 <sr_kemi_lua_exec_func_221> "UH\211\345H\203\354\020H\211}\370H\213E\370\276", <incomplete sequence \335>,
            len = 608627072
          },
          flags = 972537629,
          force_socket = 0x1,
          ruid = {
            s = 0x7ffe2446e950 "",
            len = 608627120
          },
          instance = {
            s = 0x6400000001 <error: Cannot access memory at address 0x6400000001>,
            len = 1043566064
          },
          location_ua = {
            s = 0x7f973a751408 "p\344\032:\227\177",
            len = 8388608
          },
          otcpid = 7449488
        }
        __func__ = "t_forward_nonack"
#15 0x00007f9739ef6bc5 in t_relay_to (p_msg=0x7f973e338df0, proxy=0x0, proto=0, replicate=0) at t_funcs.c:332
        ret = 0
        new_tran = 1
        t = 0x7f973a751408
        dst = {
          send_sock = 0x7ffe2446eb80,
          to = {
            s = {
              sa_family = 50040,
              sa_data = "3>\227\177\000\000\000\000\000\000\000\000\000"
            },
            sin = {
              sin_family = 50040,
              sin_port = 15923,
              sin_addr = {
                s_addr = 32663
              },
              sin_zero = "\000\000\000\000\000\000\000"
            },
            sin6 = {
              sin6_family = 50040,
              sin6_port = 15923,
              sin6_flowinfo = 32663,
              sin6_addr = {
                __in6_u = {
                  __u6_addr8 = "\000\000\000\000\000\000\000\000\020p%>\227\177\000",
                  __u6_addr16 = {0, 0, 0, 0, 28688, 15909, 32663, 0},
                  __u6_addr32 = {0, 0, 1042640912, 32663}
                }
              },
              sin6_scope_id = 27687040
            },
            sas = {
              ss_family = 50040,
              __ss_padding = "3>\227\177\000\000\000\000\000\000\000\000\000\000\020p%>\227\177\000\000\200x\246\001\000\000\000\000\320\377\377\377\377\377\377\377\000\000\000\000\000\000\000\000\300c\252\001\000\000\000\000\200x\246\001\000\000\000\000\360\273\0179\227\177\000\000\200\353F$\376\177\000\000\257\363G\000\000\000\000\000\000\000\000\000S\003\000\000\360\2153>\227\177", '\000' <repeats 14 times>, "\022\000\000",
              __ss_align = 140287560368784
            }
          },
          id = 1043579664,
          send_flags = {
            f = 32663,
            blst_imask = 0
          },
          proto = 0 '\000',
          proto_pad0 = 0 '\000',
          proto_pad1 = 0
        }
        port = 0
        host = {
          s = 0x0,
          len = 1043579608
        }
        comp = 0
        __func__ = "t_relay_to"
#16 0x00007f9739f48a33 in _w_t_relay_to (p_msg=0x7f973e338df0, proxy=0x0, force_proto=0) at tm.c:1764
        t = 0x7ffe2446ec30
        res = 3
        __func__ = "_w_t_relay_to"
#17 0x00007f9739f51dec in ki_t_relay (msg=0x7f973e338df0) at tm.c:2917
No locals.
#18 0x00007f97390eb94c in sr_kemi_lua_exec_func_ex (L=0x1a50f88, ket=0x7f9739ff5be0 <tm_kemi_exports>, pdelta=0) at app_lua_api.c:1022
        i = 1
        argc = 0
        ret = 1
        fname = 0x7f9739ff5bf0 <tm_kemi_exports+16>
        mname = 0x7f9739ff5be0 <tm_kemi_exports>
        vps = {{
            n = 27739704,
            s = {
              s = 0x1a74638 "ksr_failure_manage",
              len = 18
            }
          }, {
            n = 0,
            s = {
              s = 0x0,
              len = 0
            }
          }, {
            n = 0,
            s = {
              s = 0x0,
              len = 0
            }
          }, {
            n = 0,
            s = {
              s = 0x0,
              len = 0
            }
          }, {
            n = 0,
            s = {
              s = 0x0,
              len = 0
            }
          }, {
            n = 0,
            s = {
              s = 0x0,
              len = 0
            }
          }}
        env_L = 0x7f9739175a00 <_sr_L_env>
        xret = 0x7f973a047580 <_sr_kemi_kx_xval>
        __func__ = "sr_kemi_lua_exec_func_ex"
#19 0x00007f97390f337e in sr_kemi_lua_exec_func (L=0x1a50f88, eidx=221) at app_lua_api.c:1706
        ket = 0x7f9739ff5be0 <tm_kemi_exports>
        ret = 1
        tvb = {
          tv_sec = 0,
          tv_usec = 0
        }
        tve = {
          tv_sec = 0,
          tv_usec = 0
        }
        tz = {
          tz_minuteswest = 1052892288,
          tz_dsttime = 32663
        }
        tdiff = 27594832
        dinfo = {
          event = 1043568344,
          name = 0x7f973e339980 <incomplete sequence \350>,
          namewhat = 0x7ffe2446f03e "",
          what = 0x3000000018 <error: Cannot access memory at address 0x3000000018>,
          source = 0x7ffe2446efa0 "\200x\246\001",
          srclen = 140729507049184,
          currentline = 8989040,
          linedefined = 0,
          lastlinedefined = 73,
          nups = 0 '\000',
          nparams = 0 '\000',
          isvararg = 0 '\000',
          istailcall = 0 '\000',
          ftransfer = 4176,
          ntransfer = 421,
          short_src = "\000\000\000\000\004\000\000\000\000\000\000\000I\000\000\000\000\000\000\000\204\275\256>\227\177\000\000\300\a\000\000\000\000\000\000@\000\000\000\000\000\000\000\005\000\000\000\000\000\000\000P\020\245\001\000\000\000",
          i_ci = 0x1a50f88
        }
        __func__ = "sr_kemi_lua_exec_func"
#20 0x00007f97390fcab4 in sr_kemi_lua_exec_func_221 (L=0x1a50f88) at app_lua_kemi_export.c:1813
No locals.
#21 0x00007f97391193c1 in luaD_precall () from /var/lib/containers/storage/overlay/1ba9d537e5be2b3a47b220bd68cdb6646cd0a1f4ebb83a234c8bcc2324ca5f3f/merged/lib64/kamailio/modules/app_lua.so
No symbol table info available.
#22 0x00007f973912d4cb in luaV_execute () from /var/lib/containers/storage/overlay/1ba9d537e5be2b3a47b220bd68cdb6646cd0a1f4ebb83a234c8bcc2324ca5f3f/merged/lib64/kamailio/modules/app_lua.so
No symbol table info available.
#23 0x00007f9739119c91 in luaD_callnoyield () from /var/lib/containers/storage/overlay/1ba9d537e5be2b3a47b220bd68cdb6646cd0a1f4ebb83a234c8bcc2324ca5f3f/merged/lib64/kamailio/modules/app_lua.so
No symbol table info available.
#24 0x00007f973911802a in luaD_rawrunprotected () from /var/lib/containers/storage/overlay/1ba9d537e5be2b3a47b220bd68cdb6646cd0a1f4ebb83a234c8bcc2324ca5f3f/merged/lib64/kamailio/modules/app_lua.so
No symbol table info available.
#25 0x00007f973911a1e0 in luaD_pcall () from /var/lib/containers/storage/overlay/1ba9d537e5be2b3a47b220bd68cdb6646cd0a1f4ebb83a234c8bcc2324ca5f3f/merged/lib64/kamailio/modules/app_lua.so
No symbol table info available.
#26 0x00007f97391143df in lua_pcallk () from /var/lib/containers/storage/overlay/1ba9d537e5be2b3a47b220bd68cdb6646cd0a1f4ebb83a234c8bcc2324ca5f3f/merged/lib64/kamailio/modules/app_lua.so
No symbol table info available.
#27 0x00007f97390e7f0d in app_lua_run_ex (msg=0x7f973e338df0, func=0x7f973914d598 "ksr_request_route", p1=0x0, p2=0x0, p3=0x0, emode=1) at app_lua_api.c:773
        n = 0
        ret = 0
        txt = {
          s = 0x7ffe2446f520 "\020\370F$\376\177",
          len = 5783320
        }
        bmsg = 0x0
        ltop = 36
        __func__ = "app_lua_run_ex"
#28 0x00007f9739106f07 in sr_kemi_config_engine_lua (msg=0x7f973e338df0, rtype=1, rname=0x0, rparam=0x0) at app_lua_mod.c:119
        ret = -1
        __func__ = "sr_kemi_config_engine_lua"
#29 0x00000000005048bf in sr_kemi_route (keng=0x913440 <_sr_kemi_eng_list>, msg=0x7f973e338df0, rtype=1, ename=0x0, edata=0x0) at core/kemi.c:3784
        sfbk = 0
        ret = 1054122048
#30 0x000000000058890a in receive_msg (buf=0x9b22a0 <buf> "INVITE sip:+437200101011@sip.example.org SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.0.199:40867;TH=div;rport;branch=z9hG4bKvB942Z25j6t6Q\r\nMax-Forwards: 69\r\nFrom: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B\r\nTo: <sip:+437200101011@sip.example.org>\r\nCall-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b\r\nCSeq: 62865321 INVITE\r\nContact: <sip:myself@sip.example.org>\r\nUser-Agent: calls sofia-sip/1.13.12\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE\r\nSupported: timer, 100rel, replaces, gruu, outbound\r\nProxy-Authorization: Digest username=\"myself\", realm=\"example.int\", nonce=\"Y9QB/GPUANBJum6LTOLsDnjaSLKc/8o1A1zk1oUtFXzlFe/OfiMEAg==\", algorithm=MD5, uri=\"sip:+437200101011@sip.example.org\", response=\"da2f40487bb63329105222dab911d9bd\"\r\nContent-Type: application/sdp\r\nContent-Disposition: session\r\nContent-Length: 240\r\nTH: dih\r\n\r\nv=0\r\no=- 9163509233734166984 5909793187606445082 IN IP4 192.168.0.199\r\ns=-\r\nc=IN IP4 22.222.222.222\r\nt=0 0\r\nm=audio 43264 RTP/AVP 9 8 0 3\r\na=rtpmap:9 G722/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:3 GSM/8000\r\na=rtcp:33730\r\n", len=1093, rcv_info=0x7ffe2446fae0) at core/receive.c:502
        msg = 0x7f973e338df0
        ctx = {
          rec_lev = 256,
          run_flags = 16777216,
          last_retcode = 0,
          jmp_env = {{
              __jmpbuf = {1099511627776, 512, 0, 0, 0, 0, 0, 0},
              __mask_was_saved = 0,
              __saved_mask = {
                __val = {0, 0, 0, 0, 0, 0, 0, 4294967296, 1225543208507932942, 33554432, 3298534883584, 72057594037928192, 72057594037927936, 1099511627776, 512, 0}
              }
            }}
        }
        bctx = 0x0
        ret = 0
        tvb = {
          tv_sec = 0,
          tv_usec = 0
        }
        tve = {
          tv_sec = 0,
          tv_usec = 0
        }
        diff = 0
        inb = {
          s = 0x9b22a0 <buf> "INVITE sip:+437200101011@sip.example.org SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.0.199:40867;TH=div;rport;branch=z9hG4bKvB942Z25j6t6Q\r\nMax-Forwards: 69\r\nFrom: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B\r\nTo: <sip:+437200101011@sip.example.org>\r\nCall-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b\r\nCSeq: 62865321 INVITE\r\nContact: <sip:myself@sip.example.org>\r\nUser-Agent: calls sofia-sip/1.13.12\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE\r\nSupported: timer, 100rel, replaces, gruu, outbound\r\nProxy-Authorization: Digest username=\"myself\", realm=\"example.int\", nonce=\"Y9QB/GPUANBJum6LTOLsDnjaSLKc/8o1A1zk1oUtFXzlFe/OfiMEAg==\", algorithm=MD5, uri=\"sip:+437200101011@sip.example.org\", response=\"da2f40487bb63329105222dab911d9bd\"\r\nContent-Type: application/sdp\r\nContent-Disposition: session\r\nContent-Length: 240\r\nTH: dih\r\n\r\nv=0\r\no=- 9163509233734166984 5909793187606445082 IN IP4 192.168.0.199\r\ns=-\r\nc=IN IP4 22.222.222.222\r\nt=0 0\r\nm=audio 43264 RTP/AVP 9 8 0 3\r\na=rtpmap:9 G722/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:3 GSM/8000\r\na=rtcp:33730\r\n",
          len = 1093
        }
        netinfo = {
          data = {
            s = 0x0,
            len = 0
          },
          bufsize = 0,
          rcv = 0x0,
          dst = 0x0
        }
        keng = 0x913440 <_sr_kemi_eng_list>
        evp = {
          data = 0x7ffe2446f630,
          obuf = {
            s = 0x0,
            len = 0
          },
          rcv = 0x7ffe2446fae0,
          dst = 0x0,
          req = 0x0,
          rpl = 0x0,
          rplcode = 0,
          mode = 0
        }
        cidlockidx = 0
        cidlockset = 0
        errsipmsg = 0
        exectime = 0
        __func__ = "receive_msg"
#31 0x00000000006cad81 in udp_rcv_loop () at core/udp_server.c:544
        len = 1077
        buf = "INVITE sip:+437200101011@sip.example.org SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.0.199:40867;TH=div;rport;branch=z9hG4bKvB942Z25j6t6Q\r\nMax-Forwards: 69\r\nFrom: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B\r\nTo: <sip:+437200101011@sip.example.org>\r\nCall-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b\r\nCSeq: 62865321 INVITE\r\nContact: <sip:myself@sip.example.org>\r\nUser-Agent: calls sofia-sip/1.13.12\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE\r\nSupported: timer, 100rel, replaces, gruu, outbound\r\nProxy-Authorization: Digest username=\"myself\", realm=\"example.int\", nonce=\"Y9QB/GPUANBJum6LTOLsDnjaSLKc/8o1A1zk1oUtFXzlFe/OfiMEAg==\", algorithm=MD5, uri=\"sip:+437200101011@sip.example.org\", response=\"da2f40487bb63329105222dab911d9bd\"\r\nContent-Type: application/sdp\r\nContent-Disposition: session\r\nContent-Length: 240\r\nTH: dih\r\n\r\nv=0\r\no=- 9163509233734166984 5909793187606445082 IN IP4 192.168.0.199\r\ns=-\r\nc=IN IP4 22.222.222.222\r\nt=0 0\r\nm=audio 43264 RTP/AVP 9 8 0 3\r\na=rtpmap:9 G722/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:3 GSM/8000\r\na=rtcp:33730\r\n", '\000' <repeats 64442 times>
        tmp = 0x13a3d9090 <error: Cannot access memory at address 0x13a3d9090>
        fromaddr = 0x7f973e338ba8
        fromaddrlen = 16
        rcvi = {
          src_ip = {
            af = 2,
            len = 4,
            u = {
              addrl = {2576512599, 0},
              addr32 = {2576512599, 0, 0, 0},
              addr16 = {30295, 39314, 0, 0, 0, 0, 0, 0},
              addr = "Wv\222\231", '\000' <repeats 11 times>
            }
          },
          dst_ip = {
            af = 2,
            len = 4,
            u = {
              addrl = {1317948560, 0},
              addr32 = {1317948560, 0, 0, 0},
              addr16 = {19600, 20110, 0, 0, 0, 0, 0, 0},
              addr = "\220L\216N", '\000' <repeats 11 times>
            }
          },
          src_port = 40867,
          dst_port = 5060,
          proto_reserved1 = 0,
          proto_reserved2 = 0,
          src_su = {
            s = {
              sa_family = 2,
              sa_data = "\237\243Wv\222\231\000\000\000\000\000\000\000"
            },
            sin = {
              sin_family = 2,
              sin_port = 41887,
              sin_addr = {
                s_addr = 2576512599
              },
              sin_zero = "\000\000\000\000\000\000\000"
            },
            sin6 = {
              sin6_family = 2,
              sin6_port = 41887,
              sin6_flowinfo = 2576512599,
              sin6_addr = {
                __in6_u = {
                  __u6_addr8 = '\000' <repeats 15 times>,
                  __u6_addr16 = {0, 0, 0, 0, 0, 0, 0, 0},
                  __u6_addr32 = {0, 0, 0, 0}
                }
              },
              sin6_scope_id = 0
            },
            sas = {
              ss_family = 2,
              __ss_padding = "\237\243Wv\222\231", '\000' <repeats 111 times>,
              __ss_align = 0
            }
          },
          bind_address = 0x7f973e2e7768,
          rflags = 0,
          proto = 1 '\001',
          proto_pad0 = 0 '\000',
          proto_pad1 = 0
        }
        evp = {
          data = 0x0,
          obuf = {
            s = 0x0,
            len = 0
          },
          rcv = 0x0,
          dst = 0x0,
          req = 0x0,
          rpl = 0x0,
          rplcode = 0,
          mode = 0
        }
        printbuf = "\000\000\000\000\000\000\000\000\270\003G$\376\177\000\000\370\235\216\000\000\000\000\000@\240\324>\227\177\000\000\340\370F$\376\177\000\000D\204\0209\227\177\000\000\370\235\216\000\000\000\000\000@\240\324>\005\000\000\000\260\371F$\376\177\000\000\263\372b", '\000' <repeats 57 times>, "\005\000\000\000P\2210>\227\177", '\000' <repeats 90 times>, "\274\202=:\227\177\000\000\000\000\000\000\004\000\000\000\260-\205\000\000\000\000\000\370\235\216\000\000\000\000\000@\240\324>\005\000\000\000\200\372F$\376\177\000\000\263\372b", '\000' <repeats 13 times>, "0^\235\000\000\000\000\000\000^\235\000\000\000\000\000\230g\235\000\000\000\000\000K\306[\302!\307S\354\230\"7(ɘR\354\263ƚ\316\005\000\000\000\350\2260>\227\177\000\000 ^\235\000\000\000\000\000\230g\235\000\000\000\000\000\031\216\n\274\001\000\000\000@h\235\000\000\000\000\000P\372F$\376\177\000\000\240h\235\000\000\000\000\000\240g\235\000\000\000\000\000\000^\235\000\000\000\000\000p\372F$\376\177\000\000\235\275y\000\000\000\000\000p\372F$\376\177\000\000\274\202=:\227\177\000\000\000\000\000\000\000\000\000\000\270\003G$\376\177\000\000\370\235\216\000\000\000\000\000@\240\324>\227\177\000\0000\373F$\376\177\000\000\250\002c\000\000\000\000\000\320\372F$\376\177\000\000\213\305y\000\000\000\000"
        i = -1
        j = 41600
        l = 5
        __func__ = "udp_rcv_loop"
#32 0x000000000042d3af in main_loop () at main.c:1722
        i = 4
        pid = 0
        si = 0x7f973e2e7768
        si_desc = "udp receiver child=4 sock=111.11.111.11:5060\000\000d\000p\375F$\376\177\000\000\000\000\000\000\000\000\000\000\200\375F$\376\177\000\000s\300\3229\227\177\000\000\020\376F$\376\177\000\000\v\006\3239\227\177\000\000`\375F$\376\177\000\0001\rp\000\000\000\000\000\320/\203\000\000\000\000\000\001\000\000\000k\000\000"
        nrprocs = 8
        woneinit = 1
        __func__ = "main_loop"
#33 0x00000000004390be in main (argc=3, argv=0x7ffe24470398) at main.c:3078
        cfg_stream = 0x1931380
        c = -1
        r = 0
        tmp = 0x0
        tmp_len = 0
        port = 0
        proto = 0
        ahost = 0x0
        aport = 0
        options = 0x8081f8 ":f:cm:M:dVIhEeb:l:L:n:vKrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:x:X:Y:"
        ret = -1
        seed = 3092184453
        rfd = 4
        debug_save = 0
        debug_flag = 0
        dont_fork_cnt = 2
        n_lst = 0x0
        p = 0x0
        st = {
          st_dev = 65025,
          st_ino = 3424267,
          st_nlink = 3,
          st_mode = 16895,
          st_uid = 65534,
          st_gid = 65534,
          __pad0 = 0,
          st_rdev = 0,
          st_size = 4096,
          st_blksize = 4096,
          st_blocks = 8,
          st_atim = {
            tv_sec = 1674835346,
            tv_nsec = 294486590
          },
          st_mtim = {
            tv_sec = 1674837962,
            tv_nsec = 574996365
          },
          st_ctim = {
            tv_sec = 1674837962,
            tv_nsec = 574996365
          },
          __glibc_reserved = {0, 0, 0}
        }
        tbuf = "D\377F$\376\177", '\000' <repeats 18 times>, "hq\247>\227\177\000\000\200\374\245>\227\177\000\000\020\000G$\376\177\000\000\b\000G$\376\177\000\000W\301\000\000\000\000\000\000``\321>\227\177\000\000\005", '\000' <repeats 23 times>, "\210\266\324>\227\177\000\000\000p\321>\227\177\000\000\342z\321>\227\177\000\000f\024\322>\227\177\000\000\260إ>", '\000' <repeats 12 times>, "\260Z\321>\227\177\000\0008^\321>\227\177\000\000\344U0\000\000\000\000\000hq\247>\227\177\000\000\220\000G$\376\177\000\000\b\000G$\376\177\000\000\340h\321>\227\177\000\000/\v\322>\227\177\000\000\004\000\000\000\000\000\000\000\340h\321>\227\177\000\000\001", '\000' <repeats 15 times>, "\001\000\000\000\000\000\000\000\260Z\321>\227\177\000\000\260Z\321>\227\177\000\000\001\000\000\000\003\000\000\000\020\000G$\376\177\000\0008^\321>\227\177\000\000\000\000\000\000\000\000\000\000\001", '\000' <repeats 16 times>, "\037G$\376\177\000\000\001\261\324>\227\177\000\000\377\377\377\377\000\000\000\000\330w\321>\227\177\000\000\360\252\324>\227\177\000\0006\027g\n3@\022\000\000\263\324>\227\177\000\000\306\363v\n3@\022\000\005", '\000' <repeats 15 times>, "\260Z\321>\227\177\000\000\017\000\000\000\000\000\000\000\300\321\301>\227\177\000\000\270\003G$\376\177\000\000\370\235\216\000\000\000\000\000@\240\324>\227\177\000\000^n\322>\227\177\000\000\001", '\000' <repeats 15 times>, "\330w\321>\227\177\000\000\340\326\322>\227\177\000\000\000\000\000\000\000\000\000\000\020\003G$\376\177\000\000\003", '\000' <repeats 14 times>
        option_index = 0
        long_options = {{
            name = 0x80a91f "help",
            has_arg = 0,
            flag = 0x0,
            val = 104
          }, {
            name = 0x8054cc "version",
            has_arg = 0,
            flag = 0x0,
            val = 118
          }, {
            name = 0x80a924 "alias",
            has_arg = 1,
            flag = 0x0,
            val = 1024
          }, {
            name = 0x80a92a "subst",
            has_arg = 1,
            flag = 0x0,
            val = 1025
          }, {
            name = 0x80a930 "substdef",
            has_arg = 1,
            flag = 0x0,
            val = 1026
          }, {
            name = 0x80a939 "substdefs",
            has_arg = 1,
            flag = 0x0,
            val = 1027
          }, {
            name = 0x80a943 "server-id",
            has_arg = 1,
            flag = 0x0,
            val = 1028
          }, {
            name = 0x80a94d "loadmodule",
            has_arg = 1,
            flag = 0x0,
            val = 1029
          }, {
            name = 0x80a958 "modparam",
            has_arg = 1,
            flag = 0x0,
            val = 1030
          }, {
            name = 0x80a961 "log-engine",
            has_arg = 1,
            flag = 0x0,
            val = 1031
          }, {
            name = 0x80a96c "debug",
            has_arg = 1,
            flag = 0x0,
            val = 1032
          }, {
            name = 0x80a972 "cfg-print",
            has_arg = 0,
            flag = 0x0,
            val = 1033
          }, {
            name = 0x80a97c "atexit",
            has_arg = 1,
            flag = 0x0,
            val = 1034
          }, {
            name = 0x0,
            has_arg = 0,
            flag = 0x0,
            val = 0
          }}
        __func__ = "main"
quit

111.11.111.11 is the IP address of the server. 22.222.222.222 is my public IP address. 192.168.0.199 is my address behind NAT.

@dilyanpalauzov
Copy link
Contributor Author

Without topoh, calling +437200101011, resolved by the enum module to sip:enum-test@sip.nemox.net, running under valgrind with

dns_try_ipv6=off
dns_retr_time=2  
use_dns_failover=on 
dns_srv_lb=on
dns_try_naptr=on
dns_cache_flags=1

I get:

  • 1st test case
dns_cache_init=off
use_dns_cache=off

→ CRASHES

  • 2nd test case
dns_cache_init=off
use_dns_cache=on

→ CRASHES

  • 3rd test case
dns_cache_init=on
use_dns_cache=off

→ NO CRASH, Kamailio receives “SIP/2.0 488 Not acceptable here” from the remote server. (This is not a problem of Kamailio)

  • 4th test case
dns_cache_init=on
use_dns_cache=on

→ NO CRASH, Kamailio receives “SIP/2.0 488 Not acceptable here” from the remote server. (This is not a problem of Kamailio)

In each case I have performed the test twice and got consistent results.

@dilyanpalauzov dilyanpalauzov changed the title Crash in dns_cache.c with use_dns_cache=off Crash in dns_cache.c with dns_cache_init=off Feb 1, 2023
Copy link

github-actions bot commented Nov 8, 2023

This issue is stale because it has been open 6 weeks with no activity. Remove stale label or comment or this will be closed in 2 weeks.

@github-actions github-actions bot added the Stale label Nov 8, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2023
@dilyanpalauzov
Copy link
Contributor Author

Closing valid bugs, without fixing the real problem, results very few current reports for real problems in the software.

This practice leads to the wrong impression, that Kamailio has no known problems, and therefore is good software.

This is misleading and nobody has interest in having such misleading context.

@miconda
Copy link
Member

miconda commented Nov 26, 2023

See #3475 (comment) .

@dilyanpalauzov
Copy link
Contributor Author

/notexpired

Copy link

This issue is stale because it has been open 6 weeks with no activity. Remove stale label or comment or this will be closed in 2 weeks.

@github-actions github-actions bot added the Stale label Jan 14, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2024
@miconda miconda reopened this Jan 29, 2024
Copy link

This issue is stale because it has been open 6 weeks with no activity. Remove stale label or comment or this will be closed in 2 weeks.

@github-actions github-actions bot added the Stale label Mar 12, 2024
@henningw henningw removed the Stale label Mar 12, 2024
Copy link

This issue is stale because it has been open 6 weeks with no activity. Remove stale label or comment or this will be closed in 2 weeks.

@github-actions github-actions bot added the Stale label Apr 24, 2024
@henningw henningw removed the Stale label May 8, 2024
@xkaraman
Copy link
Contributor

@dilyanpalauzov Hey can you confirm if this only happens on LUA config or also on the native config file?

I've tried on native config file and it seems to be operating fine for both dns_cache_init=off/on.

@xkaraman
Copy link
Contributor

Also used in lua with no problems. Using the app_lua module, the provided configs and sending a test message multiple times produces the output you have described with no crash on kamalio 5.6.3 and 5.8.1.

kamailio.cfg
...
dns_cache_init=off
use_dns_cache=off # I tried all the config you stated with no crashes
loadmodule "enum"
loadmodule "kemix"
loadmodule "app_lua"

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

cfgengine "lua"
kamailio.lua
function ksr_route_sipout()
    KSR.info("===== sipout - from kamailio lua script\n");
    local y = KSR.enum.enum_query()
    KSR.err('ENUM RESULT ' .. tostring(y) .. ' :RURI ' .. KSR.kx.get_ruri() .. '\n')

    if KSR.is_myself_ruri() then return 1; end

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

    -- Kamailio - equivalent of routing blocks in Lua
-- KSR - the new dynamic object exporting Kamailio functions
-- sr - the old static object exporting Kamailio functions
--
-- SIP request routing
-- equivalent of request_route{}
function ksr_request_route()
    KSR.info("===== request - from kamailio lua script\n");

    ksr_route_sipout();

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

    -- KSR.sl.sreply(200, "OK Lua");

    KSR.pv.sets("$du", "sip:127.0.0.1:5080")
    KSR.tm.t_on_branch("ksr_branch_route_one");
    KSR.tm.t_on_reply("ksr_onreply_route_one");
    KSR.tm.t_on_failure("ksr_failure_route_one");

    if KSR.tm.t_relay() < 0 then
        KSR.sl.send_reply(500, "Server error")
    end
end

-- SIP response routing
-- equivalent of reply_route{}
function ksr_reply_route()
    KSR.info("===== response - from kamailio lua script\n");
end

-- branch route callback
-- equivalent of a branch_route{}
function ksr_branch_route_one()
    KSR.info("===== branch route - from kamailio lua script\n");
end

-- onreply route callback
-- equivalent of an onreply_route{}
function ksr_onreply_route_one()
    KSR.info("===== onreply route - from kamailio lua script\n");
end

-- failure route callback
-- equivalent of a failure_route{}
function ksr_failure_route_one()
    KSR.info("===== failure route - from kamailio lua script\n");
end
test_message.sh
CSeq=$((RANDOM%10000+1))
REGISTER_MSG="INVITE sip:+437200101011@sip.example.org SIP/2.0
Via: SIP/2.0/UDP 192.168.0.199:40867;rport;branch=z9hG4bKU2Fc14H2NX4Kc
Max-Forwards: 70
From: <sip:myself@sip.example.org>;tag=vmjX7H12KrH1B
To: <sip:+437200101011@sip.example.org>
Call-ID: 8956e785-1905-123c-24bb-e4aaeae9dd8b
CSeq: $CSeq INVITE
Contact: <sip:myself@sip.example.org>
User-Agent: calls sofia-sip/1.13.12
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, MESSAGE, SUBSCRIBE, NOTIFY, REFER, UPDATE
Supported: timer, 100rel, replaces, gruu, outbound
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 240

v=0
o=- 9163509233734166984 5909793187606445082 IN IP4 192.168.0.199
s=-
c=IN IP4 22.222.222.222
t=0 0
m=audio 43264 RTP/AVP 9 8 0 3
a=rtpmap:9 G722/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:3 GSM/8000
a=rtcp:33730\r\n
"

printf "$REGISTER_MSG" | timeout 0.3 nc -u -4 $SIP_SERVER $SIP_PORT

These are the logs produced when I call the test_message.sh multiple times, with no crash provided the setting you suggested.

May 20 11:00:34 app01 kamailio[708931]: INFO: {1 3165 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:106]: sr_kemi_core_info(): ===== request - from kamailio lua script
May 20 11:00:34 app01 kamailio[708931]: INFO: {1 3165 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:106]: sr_kemi_core_info(): ===== sipout - from kamailio lua script
May 20 11:00:34 app01 kamailio[708931]: ERROR: {1 3165 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:96]: sr_kemi_core_err(): ENUM RESULT 1 :RURI sip:enum-test@sip.nemox.net
May 20 11:00:34 app01 kamailio[708931]: INFO: {1 3165 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:106]: sr_kemi_core_info(): ===== branch route - from kamailio lua script
May 20 11:00:35 app01 kamailio[708928]: INFO: {1 1378 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:106]: sr_kemi_core_info(): ===== request - from kamailio lua script
May 20 11:00:35 app01 kamailio[708928]: INFO: {1 1378 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:106]: sr_kemi_core_info(): ===== sipout - from kamailio lua script
May 20 11:00:35 app01 kamailio[708928]: ERROR: {1 1378 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:96]: sr_kemi_core_err(): ENUM RESULT 1 :RURI sip:enum-test@sip.nemox.net
May 20 11:00:35 app01 kamailio[708928]: INFO: {1 1378 INVITE 8956e785-1905-123c-24bb-e4aaeae9dd8b} <core> [core/kemi.c:106]: sr_kemi_core_info(): ===== branch route - from kamailio lua script

@dilyanpalauzov
Copy link
Contributor Author

dilyanpalauzov commented May 21, 2024

With Kamailio 5.8 it still crashes. I use in the meantime the bytecode of luajit as KEMI.

kamailio.cfg
#.x!xKAMAILI2O
# *** To enable presence server execute:
#     - define WITH_PRESENCE
#     - if modified headers or body in config must be used by presence handling:
#     - define WITH_MSGREBUILD
#
# *** To block 3XX redirect replies execute:
#!define WITH_BLOCK3XX
#
# *** To block 401 and 407 authentication replies execute:
#     - define WITH_BLOCK401407
server_signature=off
# force_rport=on # made in routling_logic.lua
# local_rport=on
log_stderror=yes
corelog=-1
/* LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR, ... */
debug=1

dns_try_ipv6=off
dns_retr_time=2
use_dns_failover=on
dns_srv_lb=on
dns_try_naptr=on
dns_cache_flags=1
#next line crashes, unless =on
dns_cache_init=on
use_dns_cache=on

server_id=2
rundir="/conf"

children=8
enable_sctp = 1
sctp_children = 2
tcp_children = 4
enable_tls=yes
listen=tls:144.76.142.78:5061
listen=tcp:144.76.142.78:5060
listen=udp:144.76.142.78:5060
listen=sctp:144.76.142.78:5060

#onsend_route_reply=yes
user="kamailio"
group="kamailio"
real_time = 3
#tcp_defer_accept = 3
#!define DBURLRO "sqlite:///conf/kamailio-ro.db"
#!define DBURLRW "sqlite:///conf/kamailio.db"
####### Defined Values #########

# *** Value defines - IDs used later in config
# - flags
#   FLT_ - per transaction (message) flags
#!define FLT_ACC 1
#!define FLT_ACCMISSED 2
#!define FLT_ACCFAILED 3
#!define FLT_NATS 5

#	FLB_ - per branch flags
#!define FLB_NATB 6
#!define FLB_NATSIPPING 7

#!define FLT_DIALOG 10
#!define FLT_SST 11

####### Global Parameters #########

mlock_pages=yes

auto_aliases=no
#alias=sip.bapha.be:5060
#alias=sip.bapha.be:5061
#alias=sip.aegee.org:5060
#alias=sip.aegee.org:5061
#alias=mail.aegee.org:5060
#alias=mail.aegee.org:5061

/* life time of TCP connection when there is no traffic
 * - a bit higher than registration expires to cope with UA behind NAT */
tcp_connection_lifetime=3605
tcp_accept_no_cl=yes
#tcp_keepalive=yes
tcp_keepcnt=6
tcp_keepidle=60
tcp_keepintvl=10

loadmodule "db_sqlite.so"
loadmodule "permissions.so"
loadmodule "sctp.so"

loadmodule "enum.so"
loadmodule "kex.so"
loadmodule "kemix.so"
loadmodule "corex.so"
loadmodule "tm.so"
loadmodule "tmx.so"
loadmodule "sl.so"
loadmodule "usrloc.so"
loadmodule "tls.so"
loadmodule "stun.so"
loadmodule "outbound.so"
loadmodule "rr.so"
loadmodule "pv.so"
loadmodule "sipdump"
loadmodule "dialog.so"
loadmodule "sst.so"
loadmodule "uac.so"
loadmodule "acc.so"
loadmodule "maxfwd.so"
loadmodule "registrar.so"
loadmodule "textops.so"
loadmodule "tcpops.so"
# loadmodule "textopsx.so" ## unused
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "nathelper.so"
loadmodule "ctl.so"
loadmodule "cfg_rpc.so"
loadmodule "counters.so"
loadmodule "rtpengine.so"
loadmodule "auth.so"
loadmodule "auth_db.so"
loadmodule "alias_db.so"
loadmodule "domain.so"

#!ifdef WITH_PRESENCE
loadmodule "presence.so"
loadmodule "presence_xml.so"
#!endif

loadmodule "htable.so"
loadmodule "pike.so"

loadmodule "xhttp.so"
loadmodule "websocket.so"
loadmodule "app_lua.so"
loadmodule "statsc.so"

modparam("sipdump", "enable", 1)
modparam("sipdump", "mode", 2)
modparam("sipdump", "event_callback", "ksr_sipdump_event")
#modparam("sipdump", "folder", "/conf")

modparam("auth", "auth_checks_register", 11)
modparam("auth", "qop", "auth")
modparam("auth", "auth_checks_no_dlg", 9)
modparam("auth", "auth_checks_in_dlg", 15)
modparam("auth", "qop", "auth")
modparam("auth", "use_domain", yes);
# modparam("auth", "algorithm", "SHA-256") # gnome-calls does not support algorithm=sha-256
modparam("auth", "add_authinfo_hdr", yes)
modparam("permissions", "load_backends", 1)
modparam("permissions", "db_url", DBURLRO)

# ----- auth_db params -----
modparam("auth_db", "db_url", DBURLRO)
modparam("auth_db", "calculate_ha1", 1)
modparam("auth_db", "password_column", "password")
modparam("auth_db", "load_credentials", "$avp(credentials)=password")
modparam("auth_db", "use_domain", 1)


modparam("dialog", "timeout_avp", "$avp(dlgtimeout)")
modparam("dialog", "bridge_controller", "sip:controller@aegee.org")
modparam("dialog", "bridge_contact", "sip:controller@144.76.142.78:5060")
modparam("dialog", "send_bye", 1)
modparam("dialog", "dlg_flag", FLT_DIALOG)

modparam("sst", "timeout_avp", "$avp(dlgtimeout)")
modparam("sst", "sst_flag", FLT_SST)
modparam("xhttp", "event_callback", "ksr_xhttp_event")
modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:1555")
modparam("ctl", "binrpc", "unix:/conf/kamailio_ctl")
modparam("sanity", "uri_checks", 15)
modparam("uac", "restore_passwd", "my_secret_EIA99iatruai")
# modparam("uac", "default_socket", "udp:144.76.142.78:5060")
modparam("uac", "reg_use_domain", 1)
modparam("uac", "reg_db_url", DBURLRO)

# ----- tm params -----
# auto-discard branches from previous serial forking leg
modparam("tm", "failure_reply_mode", 3)
# default retransmission timeout: 30sec
modparam("tm", "fr_timer", 30000)
# default invite retransmission timeout after 1xx: 120sec
modparam("tm", "fr_inv_timer", 120000)

# ----- rr params -----
# do not append from tag to the RR (no need for this script)
modparam("rr", "append_fromtag", 1)
modparam("uac", "reg_contact_addr", "mail.aegee.org:5060")

# ----- registrar params -----
# modparam("registrar", "method_filtering", 1)
modparam("registrar", "retry_after", 30)
modparam("registrar", "max_contacts", 10)
modparam("registrar", "max_expires", 3600)
modparam("registrar", "use_path", 1)
# modparam("registrar", "realm_prefix", "sip.")

# ----- usrloc params -----
modparam("usrloc", "timer_interval", 60)
modparam("usrloc", "timer_procs", 1)
modparam("usrloc", "use_domain", 1)
modparam("usrloc", "preload", "location")
modparam("usrloc", "xavp_contact", "uladdrs")
# modparam("usrloc", "db_url", DBURLRW)
# modparam("usrloc", "db_mode", 1)

# ----- alias_db params -----
modparam("alias_db", "db_url", DBURLRO)
modparam("alias_db", "use_domain", 1)
modparam("alias_db", "append_branches", 1)
# modparam("alias_db", "domain_prefix", "sip.")

# ----- domain params -----
modparam("domain", "db_url", DBURLRO)
/* register callback to match myself condition with domains list */
modparam("domain", "register_myself", 1)

#!ifdef WITH_PRESENCE
# ----- presence params -----
# modparam("presence", "db_url", DBURLRW)

# ----- presence_xml params -----
# modparam("presence_xml", "db_url", DBURL)
modparam("presence_xml", "force_active", 1)
#!endif

#modparam("nathelper", "natping_interval", 5)
modparam("nathelper", "natping_interval", 30)
# has to be few times higher than natping_interval
modparam("nathelper", "keepalive_timeout", 60)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
modparam("nathelper", "sipping_from", "sip:pinger@aegee.org")

# params needed for NAT traversal in other modules
modparam("nathelper", "received_avp", "$avp(RECEIVED)")
modparam("registrar", "received_avp", "$avp(RECEIVED)")
modparam("usrloc", "nat_bflag", FLB_NATB)

modparam("tls", "config", "/conf/tls.cfg")
modparam("tls", "send_close_notify", 1)
modparam("tls", "tls_method", "TLSv1.2+")

modparam("pike", "sampling_time_unit", 2)
modparam("pike", "reqs_density_per_unit", 16)
modparam("pike", "remove_latency", 4)

/* ip ban htable with autoexpire after 5 minutes */
modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
# modparam("htable", "db_url", DBURLRO)
#modparam("htable", "htable", "users=>size=8;dbtable=htable_users;")
modparam("htable", "htable", "users=>size=8;autoexpire=7200;")
modparam("websocket", "keepalive_mechanism", 1)
modparam("websocket", "keepalive_timeout", 3)

modparam("tcpops", "closed_event", 2)
modparam("tcpops", "event_callback", "ksr_tcpops_event")
modparam("app_lua", "load", "/conf/routing_logic.luajit.bc")
# modparam("app_lua", "load", "/conf/routing_logic.lua54.bc")
cfgengine "lua"
routing_logic.luajit
band = bit.band
-- global variables corresponding to defined values (flags) in kamailio.cfg
FLT_ACC = 1
FLT_ACCMISSED = 2
FLT_ACCFAILED = 3
FLT_NATS = 5

FLB_NATB = 6
FLB_NATSIPPING = 7
FLT_DIALOG=10
FLT_SST=11

PW_DISALLOW_CALL_FROM_UNAUTHENTICATED = 1
PW_DISALLOW_CALL_FROM_ANONYMOUS = 2
PW_FORCE_TLS_TO_SERVER = 4
PW_FORCE_SRTP_TO_SERVER = 8
PW_END_TO_END_ENCRYPTION_TLS = 16
PW_END_TO_END_ENCRYPTION_SRTP = 32
PW_ASYMMETRIC = 64
-- max = 32 + 16 + 1  + 2 = 48 + 3 = 51
-- perhaps small letter for non-symmetric NAT, with "{" and "~" or "}" as last characters.

function pw_prop(password)
  if string.sub(password, 2, 2) ~= '0' then return -1 end
  local p, ret = string.byte(string.sub(password, 1, 1)) - 65, 0
  if p > 32 then
    ret = PW_ASYMMETRIC
    p = p - 32
  end
  if p == 28 then p = 27 end
  if band(p, PW_DISALLOW_CALL_FROM_UNAUTHENTICATED) ~= 0 then ret = ret + PW_DISALLOW_CALL_FROM_UNAUTHENTICATED end
  if band(p, PW_DISALLOW_CALL_FROM_ANONYMOUS) ~= 0 then ret = ret + PW_DISALLOW_CALL_FROM_ANONYMOUS end
  if band(p, PW_END_TO_END_ENCRYPTION_TLS) ~= 0 then
    p = p - PW_FORCE_TLS_TO_SERVER - PW_FORCE_SRTP_TO_SERVER
    if band(p, PW_FORCE_TLS_TO_SERVER) ~= 0 then ret = ret + PW_END_TO_END_ENCRYPTION_TLS end
    if band(p, PW_FORCE_SRTP_TO_SERVER) ~= 0 then ret = ret + PW_END_TO_END_ENCRYPTION_SRTP end
  else
    if band(p, PW_FORCE_TLS_TO_SERVER) ~= 0 then ret = ret + PW_FORCE_TLS_TO_SERVER end
    if band(p, PW_FORCE_SRTP_TO_SERVER) ~= 0 then ret = ret + PW_FORCE_SRTP_TO_SERVER end
  end
  return ret
end

function ksr_request_route()
--        if KSR.kx.ifdef('WITH_BLOCK3XX') then
--           KSR.err('WITH_BLOCK3xx is defined\n')
--        else
--           KSR.err('WITH_BLOCK3xx is NOT defined\n')
--        end

        -- per request initial checks
        KSR.set_reply_no_connect()
        -- enforce symmetric signaling
        -- send back replies to the source address of request
        KSR.force_rport()
        if not KSR.is_myself_srcip() then
                local srcip = KSR.kx.get_srcip()
                if KSR.htable.sht_match_name("ipban", "eq", srcip) > 0 then
                        -- ip is already blocked
                        KSR.dbg("request from blocked IP - " .. KSR.kx.get_method() .. " from " .. KSR.kx.get_furi() .. " (IP:" .. srcip .. ":" .. KSR.kx.get_srcport() .. ")\n")
                        KSR.x.exit()
                end
                if KSR.pike.pike_check_req() < 0 then
                        KSR.err("ALERT: pike blocking " .. KSR.kx.get_method() .. " from " .. KSR.kx.get_furi() .. " (IP:" .. srcip .. ":" .. KSR.kx.get_srcport() .. ")\n")
                        KSR.htable.sht_seti("ipban", srcip, 1)
                        KSR.x.exit()
                end
        end

        local ua = KSR.kx.gete_ua()
        if string.find(ua, "friendly") or string.find(ua, "scanner") or string.find(ua, "sipcli") or string.find(ua, "sipvicious") or string.find(ua, "VaxSIPUserAgent") or string.find(ua, "pplsip") then
                KSR.sl.sl_send_reply(200, "OK")
                KSR.x.exit()
        end

        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() and KSR.is_myself_ruri() and KSR.corex.has_ruri_user() < 0 then
                KSR.sl.sl_send_reply(200, "Keepalive")
                KSR.x.exit()
        end
-- 7 = 1 + 2 +4
--17898 - 16384 - 1024 - 256  - 128 - 64 - 32 -8-2
--127 - malformed
-- bez 4

        if KSR.sanity.sanity_check(17896, 0)<0 then
                KSR.err("malformed SIP message from " .. KSR.kx.get_srcip() .. ":" .. KSR.kx.get_srcport() .."\n")
                KSR.x.exit()
        end
        if string.lower(KSR.kx.get_rhost() or "") == "mail.aegee.org" then
                KSR.sethost("aegee.org")
        end

        -- if KSR.is_REGISTER() and KSR.is_UDP() then
        --    KSR.sl.sl_send_reply(404, "Do not REGISTER over UDP")
        --    return 1
        -- end
        -- NAT detection
        if KSR.nathelper.nat_uac_test(19+64)>0 then
                if KSR.is_REGISTER() then
                        KSR.nathelper.fix_nated_register()
                elseif KSR.is_WSX() then
                    if KSR.nathelper.add_contact_alias() < 0 then
                        KSR.sl.sl_send_reply(400, "Bad Request")
                        KSR.x.exit()
                    end
                elseif KSR.siputils.is_first_hop()>0 then
                    KSR.nathelper.set_contact_alias()
                end
                KSR.setflag(FLT_NATS)
        end

        -- CANCEL processing
        if KSR.is_CANCEL() then
                if KSR.tm.t_check_trans()>0 and KSR.tm.t_relay()<0 then
                    KSR.sl.sl_reply_error()
                end
                KSR.x.exit()
                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
        if KSR.siputils.has_totag()>0 then
            -- sequential request within a dialog should take the path determined by record-routing
            if KSR.rr.loose_route()>0 then
                    -- URI update for dialog requests
                    if not KSR.isdsturiset() then
                       KSR.nathelper.handle_ruri_alias()
                    end

                    if KSR.is_BYE() then
                            KSR.setflag(FLT_ACC) -- do accounting ...
                            KSR.setflag(FLT_ACCFAILED) -- ... even if the transaction fails
                    elseif 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 and KSR.tm.t_relay()<0 then
                            -- no loose-route, but stateful ACK
                            -- must be an ACK after a 487
                            -- or e.g. 404 from upstream server
                            KSR.sl.sl_reply_error()
                    end
                    KSR.x.exit()
            end
            KSR.sl.sl_send_reply(404, "Not here")
            KSR.x.exit()
        end
        -- only initial requests (no To tag)

        -- authentication
        -- IP authorization and user authentication
        local is_register = KSR.is_REGISTER()
        if is_register or KSR.is_myself_furi() then
                -- authenticate requests
                local fhost = KSR.kx.gete_fhost()
                local turi = KSR.kx.get_turi()
                if string.lower(fhost) == "mail.aegee.org" then
                    fhost = "aegee.org"
                    turi = KSR.kx.gete_tuser() .. '@aegee.org'
                end
                if string.lower(string.sub(turi, 1, 4)) == 'sip:' then
                    turi = string.sub(turi, 5)
                elseif string.lower(string.sub(turi, 1, 5)) == 'sips:' then
                    turi = string.sub(turi, 6)
                end
                if KSR.auth_db.auth_check(fhost, "subscriber", 1)<0 then
                        KSR.auth.auth_challenge(fhost, 0)
                        KSR.x.exit()
                end
                local pw_props = pw_prop(KSR.pv.gete('$avp(credentials)'))
                if is_register then
                    KSR.htable.sht_seti("users", turi, pw_props)
                    KSR.tcpops.tcp_keepalive_enable(60, 5, 5)
                    if KSR.is_TLS() then
                        KSR.tcpops.tcp_enable_closed_event()
                    end
                    if ( band(pw_props, PW_FORCE_TLS_TO_SERVER) ~= 0 ) and not KSR.is_TLS() and not KSR.is_WSX() then
                        KSR.sl.sl_send_reply(404, "Use TLS!")
                        KSR.x.exit()
                    end
                elseif KSR.is_INVITE() then
                end

                -- user authenticated - remove auth header
                if not KSR.is_method_in("RP") then
                        KSR.auth.consume_credentials()
                end
        else
          -- validate IP address of caller (carrier)
	  KSR.err('FOREIGN FURI SRCURI ' .. KSR.kx.get_srcip() .. ':' .. KSR.kx.get_srcport() .. '\n')
          if KSR.permissions.allow_source_address_group() > -1 then
            -- KSR.err('KNOWN SOURCE IP\n')
          else
            -- KSR.err('XXX UNKNOWN SOURCE IP\n')
	  end
        end
        if not KSR.is_method_in("RS") then
            KSR.setflag(FLT_DIALOG)
            KSR.setflag(FLT_SST)
        end
        -- if caller is not local subscriber, then check if it calls a local destination, otherwise deny, not an open relay here
        if not (KSR.is_myself_furi() or KSR.is_myself_ruri()) then
            KSR.sl.sl_send_reply(403, "Not relaying")
            KSR.x.exit()
        end
        -- record routing for dialog forming requests (in case they are routed)
        -- - remove preloaded route headers
        KSR.hdr.remove("Route")
        if KSR.is_method_in("ISR") then
            KSR.rr.record_route()
            if KSR.is_method_in("IR") then
                if KSR.is_INVITE() then
                    KSR.setflag(FLT_ACC) -- do accounting
                end
            end
        end

        -- dispatch requests to foreign domains
        -- Routing to foreign domains
        ksr_route_sipout()

        -- requests for my local domains

        -- handle registrations
        if KSR.is_REGISTER() then
            if KSR.isflagset(FLT_NATS) then
                KSR.err('FLT_NATS IS set\n')
                KSR.setbflag(FLB_NATB)
                -- do SIP NAT pinging
                -- KSR.setbflag(FLB_NATSIPPING)
            else
                KSR.err('FLT_NATS is NOT SET\n')
            end
            if string.lower(KSR.kx.get_fhost()) == "mail.aegee.org" then
                if KSR.registrar.save_uri("location", 0, "sip:" .. KSR.kx.get_fuser() .. "@aegee.org") <0 then
                    KSR.sl.sl_reply_error()
                end
            else
                if KSR.registrar.save("location", 0) <0 then
                    KSR.sl.sl_reply_error()
                end
            end
            KSR.x.exit()
        end

        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

        KSR.alias_db.lookup("dbaliases")
        -- user location service
        KSR.err('LOOKING FOR ' .. KSR.kx.get_ruser() .. '\n')

        local callee_properties, caller_properties = 0, 0
        if KSR.is_INVITE() then
            if KSR.is_myself_ruri() then
              local rhost = KSR.kx.gete_rhost()
              if rhost == 'mail.aegee.org' then rhost = 'aegee.org' end
              local callee = KSR.kx.gete_ruser() .. '@' .. rhost
              local n = KSR.htable.sht_gete("users", KSR.kx.gete_ruser() .. '@' .. rhost)
              if n ~= "" then callee_properties = n end
           end
           if KSR.is_myself_furi() then
              local fhost = KSR.kx.gete_fhost()
	      if fhost == 'mail.aegee.org' then fhost = 'aegee.org' end
              local caller = KSR.kx.gete_fuser() .. '@' .. fhost
	      local n = KSR.htable.sht_gete("users", KSR.kx.gete_fuser() .. '@' .. fhost)
              if n ~= "" then caller_properties = n end
              KSR.err('caller props = ' .. caller_properties .. '\n')
              if band(caller_properties, PW_FORCE_TLS_TO_SERVER) ~= 0 and not KSR.is_TLS() and not KSR.is_WSX() then
                  KSR.sl.sl_send_reply(404, "Use TLS!")
                  KSR.x.exit()
              end
           end
        end

        local rc
        if KSR.kx.gete_rhost() == "mail.aegee.org" then
            rc = KSR.registrar.lookup_uri("location", "sip:" .. KSR.kx.get_ruser() .. "@aegee.org")
        else
            rc = KSR.registrar.lookup("location")
        end

        KSR.err('LOOKUP RESULT ' .. KSR.kx.get_ruri() .. '\n')
        if rc<0 and KSR.is_myself_ruri() 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
        -- when routing via usrloc, log the missed calls also
        if KSR.is_INVITE() then
                KSR.setflag(FLT_ACCMISSED)
        end

        ksr_route_relay()
        KSR.x.exit()
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
            if KSR.is_method_in("ISU") and 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

-- RTPEngine control
function ksr_route_natmanage()
        local isrequest = KSR.siputils.is_request() > 0
        if isrequest and KSR.siputils.has_totag()>0 and KSR.rr.check_route_param("nat=yes")>0 then
                KSR.setbflag(FLB_NATB)
        end
        if not (KSR.isflagset(FLT_NATS) or KSR.isbflagset(FLB_NATB)) then
                KSR.err('ksr_route_natmanage: NATS FLAGS NOT set\n')
                return 1
        end

        KSR.err('ksr_route_natmanage: NATS FLAGS SET\n')
        local no_srtp = KSR.kx.get_rhost() == 'sip.nemox.net' and " RTP" or ""
-- pad-crypto
--        KSR.err("USER AGENT" .. KSR.pv.gete("$xavp(uladdrs)") .. '\n')
        local useragent = KSR.hdr.gete("User-Agent")
        if string.find(useragent, "JsSIP") or string.find(useragent, "SIP.js") then
          no_srtp = no_srtp .. " rtcp-mux-accept"
        end
        if KSR.nathelper.nat_uac_test(8) > 0 then
            KSR.rtpengine.rtpengine_manage("replace-session-connection replace-origin SDES-pad SIP-source-address" .. no_srtp)
        else
            KSR.rtpengine.rtpengine_manage("replace-session-connection replace-origin SDES-pad" .. no_srtp)
        end
        if isrequest and KSR.siputils.has_totag()<0 and KSR.tmx.t_is_branch_route()>0 then
                KSR.rr.add_rr_param(";nat=yes")
        elseif not isrequest and KSR.siputils.is_reply()>0 and KSR.isbflagset(FLB_NATB) then
            KSR.nathelper.set_contact_alias()
        end
        return 1
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
function ksr_onreply_manage()
        local scode = KSR.kx.get_status()
        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()
        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

-- Routing to foreign domains
function ksr_route_sipout()
        local first=string.sub(KSR.kx.gete_ruser(), 1, 1)
        if first == '+' or first == '0' then
            local y = KSR.enum.enum_query()
            KSR.err('ENUM RESULT ' .. tostring(y) .. ' :RURI ' .. KSR.kx.get_ruri() .. '\n')
            if y == -1 and (not KSR.is_myself_furi() or KSR.kx.get_fuser() == 'online') then
               KSR.sl.sl_send_reply(403, "Aliens not served here.")
               KSR.x.exit()
            end
        end
        if KSR.is_myself_ruri() then return 1; end
        KSR.hdr.append("P-Hint: outbound\r\n");
        ksr_route_relay();
        KSR.x.exit();
end

function ksr_sipdump_event(evname)
    local dst_port, dst_ip, src_port, src_ip = KSR.pv.gete("$sipdump(dst_port)"), KSR.sipdump.get_dst_ip(), KSR.pv.gete("$sipdump(src_port)"), KSR.sipdump.get_src_ip()
      if (dst_ip == "144.76.142.78" and src_ip == "144.76.142.78") or not ((dst_port == 5060 and dst_ip == "144.76.142.78") or (src_port == 5060 and src_ip == "144.76.142.78")) then
        KSR.err("sipdump " .. evname .. " src " .. src_ip .. ':' .. src_port .. " dst " ..  dst_ip .. ':' ..  dst_port .. "\n" .. KSR.pv.gete("$sipdump(buf)") .. "\n")
      end
    return 1
end

-- event callback function implemented in Lua
function ksr_xhttp_event(evname)
        KSR.set_reply_close()
        KSR.set_reply_no_connect()
        if KSR.kx.get_srcip() ~= '144.76.142.78' then --         if KSR.pv.gete("$Rp") ~= 8080 then
            KSR.xhttp.xhttp_reply("403", "Forbidden", "text/plain", "Plan B2." .. KSR.kx.get_srcip())
            KSR.x.exit()
        end
        if string.lower(KSR.hdr.gete("Upgrade")) == "websocket" and string.find(string.lower(KSR.hdr.gete("Connection")), "upgrade") ~= nil and string.upper(KSR.kx.get_method()) == "GET" then
          local host = KSR.hdr.get("Host")
          if host == nil then --or not KSR.is_myself("sip:" .. host) then
            KSR.xhttp.xhttp_reply("403", "Forbidden", "text/plain", "Plan B3.")
            KSR.x.exit()
          end
          if KSR.websocket.handle_handshake() > 0 then
            KSR.x.exit()
          end
        end
        KSR.err("===== xhttp 1 module triggered event: " .. evname .. "\n")
        KSR.xhttp.xhttp_reply("200", "OK", "text/html", "<html><body>OK - [" .. KSR.pv.gete("$si") .. ":" .. KSR.pv.gete("$sp") .. "]</body></html>")
        return 1
end

function ksr_tcpops_event(evname)
  KSR.info("===== tcpops module triggered event: " .. evname .. " 	Source IP " .. KSR.pv.gete("$si") .. " Source port " .. KSR.pv.gete("$sp") .. "\n")
  return 1
end

@xkaraman
Copy link
Contributor

xkaraman commented May 23, 2024

Hmm interesting thing, indeed with LuaJIT and your routing_logic.luajit, kamailio crashes, but when used with my kamailio.lua does not. Both LUA files have the offending line off KSR.enum.enum_query() and dns_cache_init=off.

Edit: It seems the crash is coming from ksr_route_relay() instead when KSR.enum.enum_query() is present in my kamailio.lua.

@xkaraman
Copy link
Contributor

@dilyanpalauzov I just pushed #3858 for a possible fix.

Can you confirm that you no longer see the crash, and that it functions properly?

Thanks

@dilyanpalauzov
Copy link
Contributor Author

Thank you very much for looking into this!

The setup I had in the past, six months ago, with 5.7, has connected me to +437200101012.

When dns_cache_init=off without this patch applied on the 5.8 branch, with the configuration files I have, Kamailio crashes more or less immediately after it is started, or whenever a WebSocket connection is established.

When dns_cache_init=off with this patch applied on the 5.8 branch, Kamailio crashes after I try to call +437200101012 over WebSocket.

When dns_cache_init=on with this patch applied and I dial +437200101012 Kamailio does not crash, but does not connect me neither, with the latter being irrelevant here.

So I think the initial problem is not solved, but your patch addreses a problem, which has not existed at the time I reported about this crash.

@xkaraman
Copy link
Contributor

@dilyanpalauzov

So in 5.7 it worked correctly? I could try to see what changed in between.

Do you have any more details of where it crashed after the patch or some trace I can look into?

Thanks

@xkaraman
Copy link
Contributor

I pushed a new update on the PR, can you maybe try it again and report back?

With your config and without the patch, indeed kamailio crashes at some point.

With the new patch, it doesn't crash at all but want to verify that I replicated it correctly.

@dilyanpalauzov
Copy link
Contributor Author

I meant that in the past on 5.6 branch with my configuration Kamailio has not crashed immediately after start. Kamailio 5.8 crashes immediately after start. With your first version of the patch Kamailio does not crash anymore immediately after start.

The problem report here is that Kamailio 5.6.3 crashed in my configuration when I try to call an ENUM resolved number +437200101012. With your second patch applied on the 5.8 branch, the call is performed and there is no crash. Thanks!

@xkaraman
Copy link
Contributor

xkaraman commented Jun 4, 2024

Thanks for reporting and testing!

#3858 Merged to master.

@xkaraman xkaraman closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants