[sr-dev] [kamailio/kamailio] KSR.drop() from python script does not take effect for TOPOS module (#1924)

Kirill Usov notifications at github.com
Mon Apr 8 12:08:54 CEST 2019


### Description
I am using kamailio with Python scripting and TOPOS module to hide topology.
The issue is that I cannot skip topology hiding for internal network. 
Documentation says:

```
It is executed before doing topology stripping processing for an outgoing SIP message. If 'drop' is executed inside the event route, then the module skips doing the topology hiding.

event_route[topos:msg-outgoing] {
  if($sndto(ip)=="10.1.1.10") {
    drop;
  }
}
```

My code is:
```
    def ksr_topos_event(self, msg, evname):
        if evname == "topos:msg-outgoing":
            to_ip = KSR.pv.get("$sndto(ip)")
            node = self.__configuration.freeswitch_nodes.get(to_ip)

            if node:
                KSR.drop()

        return 1
```

I see in the log file that KSR.drop() method is called. But with no effect, headers of INVITE message to internal infrastructure were modified by TOPOS module.

#### Configuration file
```

#!KAMAILIO
debug=4
#log_stderror=yes

memdbg=5
memlog=5

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

/* number of SIP routing processes */
children=8

/* uncomment the next line to disable TCP (default on) */
disable_tcp=yes

/* uncomment the next line to disable the auto discovery of local aliases
 * based on reverse DNS on IPs (default on) */
auto_aliases=no

/* uncomment and configure the following line if you want Kamailio to
 * bind on a specific interface/port/proto (default bind on all available) */
listen=udp:10.164.0.10:5060 advertise 35.204.224.219:5060

port=5060

####### Modules Section ########

/* set paths to location of modules */
# mpath="/usr/lib/x86_64-linux-gnu/kamailio/modules/"

loadmodule "ctl.so"
loadmodule "tm.so"
loadmodule "corex.so"
loadmodule "sl.so"
loadmodule "pv.so"
loadmodule "maxfwd.so"
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "auth.so"
loadmodule "dispatcher.so"
loadmodule "db_sqlite.so"
loadmodule "app_python.so"
loadmodule "htable.so"
loadmodule "rr.so"
loadmodule "dialog.so"
#loadmodule "rtpengine.so"
loadmodule "topos.so"
# ----------------- setting module-specific parameters ---------------

# auto-discard branches from previous serial forking leg
modparam("tm", "failure_reply_mode", 3)
# default retransmission timeout: 30sec
modparam("tm", "fr_timer", 3000)
# default invite retransmission timeout after 1xx: 120sec
modparam("tm", "fr_inv_timer", 120000)
modparam("tm", "contacts_avp", "tm_contacts")
modparam("tm", "contact_flows_avp", "tm_contact_flows")
modparam("tm", "remap_503_500", 0)

modparam("dispatcher", "list_file", "/opt/balancer/dispatcher.conf")
modparam("dispatcher", "flags", 2)
modparam("dispatcher", "dst_avp", "$avp(AVP_DST)")
modparam("dispatcher", "grp_avp", "$avp(AVP_GRP)")
modparam("dispatcher", "cnt_avp", "$avp(AVP_CNT)")
#set next two parameters if you want to enable balance alg. no. 10
#modparam("dispatcher", "dstid_avp", "$avp(dsdstid)")
#modparam("dispatcher", "ds_hash_size", 8)
modparam("dispatcher", "ds_ping_interval", 120)
modparam("dispatcher", "ds_ping_from", "sip:kamailio at simberry.com")
#modparam("dispatcher", "ds_ping_method", "INFO")
modparam("dispatcher", "ds_probing_mode", 1)
modparam("dispatcher", "ds_probing_threshold", 1)
#configure codes or classes of SIP replies to list only allowed replies (i.e. when temporarily unavailable=480)
modparam("dispatcher", "ds_ping_reply_codes", "class=2;code=480;code=404")

modparam("htable", "htable", "a=>size=12;autoexpire=3600;dbmode=1;dbtable=htable")
modparam("htable", "db_url", "sqlite:///opt/balancer/kamailio.sqlite")
modparam("htable", "db_expires", 1)
modparam("htable", "fetch_rows", 1000)
modparam("htable", "timer_interval", 5)
modparam("htable", "timer_procs", 1)

modparam("dialog", "dlg_flag", 5)
modparam("dialog", "db_url", "sqlite:///opt/balancer/kamailio.sqlite")
modparam("dialog", "db_mode", 1)
#modparam("dialog", "db_update_period", 30)
modparam("dialog", "db_fetch_rows", 1000)
modparam("dialog", "default_timeout", 7500)
modparam("dialog", "early_timeout", 400)
modparam("dialog", "end_timeout", 60)

modparam("topos", "db_url", "sqlite:///opt/balancer/kamailio.sqlite")
modparam("topos", "clean_interval", 300)
modparam("topos", "event_callback", "ksr_topos_event")

#modparam("rtpengine", "rtpengine_sock", "udp:localhost:2223")

#
####### Routing Logic ########

modparam("app_python", "script_name", "/opt/balancer/load_balancer.py")

cfgengine "python"

```

#### Log Messages

```
Apr  8 09:38:40 vp-balancer-dev-01 /usr/sbin/kamailio[26752]: {1 1 INVITE 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE} DEBUG: topos [topos_mod.c:477]: tps_execute_event_route(): executing event_route[topos:...] (-1)
Apr  8 09:38:40 vp-balancer-dev-01 /usr/sbin/kamailio[26752]: {1 1 INVITE 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE} DEBUG: app_python [apy_kemi.c:776]: sr_apy_kemi_f_pv_get_mode(): pv get: $sndto(ip)
Apr  8 09:38:40 vp-balancer-dev-01 /usr/sbin/kamailio[26752]: {1 1 INVITE 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE} DEBUG: <core> [core/pvapi.c:395]: pv_cache_lookup(): pvar [$sndto(ip)] found in cache
Apr  8 09:38:40 vp-balancer-dev-01 /usr/sbin/kamailio[26752]: {1 1 INVITE 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE} DEBUG: app_python [apy_kemi.c:213]: sr_apy_kemi_exec_func(): execution of method: .drop
Apr  8 09:38:40 vp-balancer-dev-01 /usr/sbin/kamailio[26752]: {1 1 INVITE 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE} DEBUG: app_python [apy_kemi.c:122]: sr_kemi_config_engine_python(): execution of route type 513 with name [ksr_topos_event] returned 1
```

#### SIP Traffic
10.x.x.x - internal network IP addresses
95.79.44.x - external network IP addresses

95.79.44.125 - SIP Phone
10.164.0.10 - Kamailio (external IP 35.204.224.219)
10.166.0.2 - Freeswitch
95.79.44.126 - Gateway

```
*** 95.79.44.125 -> 10.164.0.10
INVITE sip:79200204544 at 35.204.224.219 SIP/2.0
Via: SIP/2.0/UDP 95.79.44.125:5060;branch=z9hG4bK-524287-1---fc4d9360f0b3d218;rport
Max-Forwards: 70
Contact: <sip:testin2 at 95.79.44.125:5060>
To: <sip:79200204544 at 35.204.224.219>
From: <sip:testin2 at 35.204.224.219>;tag=8035856a
Call-ID: 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE
CSeq: 1 INVITE
Allow: OPTIONS, SUBSCRIBE, NOTIFY, INVITE, ACK, CANCEL, BYE, REFER, INFO
Content-Type: application/sdp
Supported: replaces
User-Agent: X-Lite release 5.4.0 stamp 94385
Content-Length: 210

v=0
o=- 1554716320712461 1 IN IP4 95.79.44.125
s=X-Lite release 5.4.0 stamp 94385
c=IN IP4 95.79.44.125
t=0 0
m=audio 55414 RTP/AVP 8 0 101
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv

*** 10.164.0.10 -> 95.79.44.125
SIP/2.0 100 trying -- your call is important to us
Via: SIP/2.0/UDP 95.79.44.125:5060;branch=z9hG4bK-524287-1---fc4d9360f0b3d218;rport=5060;received=95.79.44.125
To: <sip:79200204544 at 35.204.224.219>
From: <sip:testin2 at 35.204.224.219>;tag=8035856a
Call-ID: 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE
CSeq: 1 INVITE
Server: kamailio (5.1.7 (x86_64/linux))
Content-Length: 0

*** 10.164.0.10 -> 10.166.0.2
INVITE sip:79200204544 at 10.166.0.2:5060 SIP/2.0
Via: SIP/2.0/UDP 35.204.224.219:5060;branch=z9hG4bK9e01.c28fe550f8dd384f5b591612c5abf080.0
Max-Forwards: 70
To: <sip:79200204544 at 35.204.224.219>
From: <sip:testin2 at 35.204.224.219>;tag=8035856a
Call-ID: 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE
CSeq: 1 INVITE
Allow: OPTIONS, SUBSCRIBE, NOTIFY, INVITE, ACK, CANCEL, BYE, REFER, INFO
Content-Type: application/sdp
Supported: replaces
User-Agent: X-Lite release 5.4.0 stamp 94385
Content-Length: 210
X-Equipment-ID: 6741
X-Equipment-IP: 95.79.44.125
X-Equipment-Port: 5060
Contact: <sip:btpsh-5cab164e-6880-1 at 35.204.224.219>

v=0
o=- 1554716320712461 1 IN IP4 95.79.44.125
s=X-Lite release 5.4.0 stamp 94385
c=IN IP4 95.79.44.125
t=0 0
m=audio 55414 RTP/AVP 8 0 101
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv

*** 10.166.0.2 -> 10.164.0.10
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 35.204.224.219:5060;branch=z9hG4bK9e01.c28fe550f8dd384f5b591612c5abf080.0;received=10.164.0.10
From: <sip:testin2 at 35.204.224.219>;tag=8035856a
To: <sip:79200204544 at 35.204.224.219>
Call-ID: 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE
CSeq: 1 INVITE
User-Agent: FreeSWITCH-mod_sofia/1.8.5+git-20190227T164909Z~cb1f3ab6f2~64bit
Content-Length: 0

*** 10.166.0.2 -> 10.164.0.10
SIP/2.0 183 Session Progress
Via: SIP/2.0/UDP 35.204.224.219:5060;branch=z9hG4bK9e01.c28fe550f8dd384f5b591612c5abf080.0;received=10.164.0.10
From: <sip:testin2 at 35.204.224.219>;tag=8035856a
To: <sip:79200204544 at 35.204.224.219>;tag=pDr153DcQr0tB
Call-ID: 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE
CSeq: 1 INVITE
Contact: <sip:79200204544 at 10.166.0.2:5060;transport=udp>
User-Agent: FreeSWITCH-mod_sofia/1.8.5+git-20190227T164909Z~cb1f3ab6f2~64bit
Accept: application/sdp
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 216
Remote-Party-ID: "79200204544" <sip:79200204544 at 35.204.224.219>;party=calling;privacy=off;screen=no

v=0
o=FreeSWITCH 1554690954 1554690955 IN IP4 10.166.0.2
s=FreeSWITCH
c=IN IP4 10.166.0.2
t=0 0
m=audio 25366 RTP/AVP 8 101
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20

*** 10.166.0.2 -> 10.164.0.10
INVITE sip:79200204544 at 10.164.0.10:5060 SIP/2.0
Via: SIP/2.0/UDP 10.166.0.2;rport;branch=z9hG4bKt1B9UyKUa3vgD
Max-Forwards: 70
From: "" <sip:testin2 at 10.166.0.2>;tag=QpHt7yyFm1pDQ
To: <sip:79200204544 at 10.164.0.10:5060>
Call-ID: ef3258bd-d484-1237-cfbc-42010aa60002
CSeq: 2804368 INVITE
Contact: <sip:gw+kamailio at 10.166.0.2:5060;transport=udp;gw=kamailio>
User-Agent: FreeSWITCH-mod_sofia/1.8.5+git-20190227T164909Z~cb1f3ab6f2~64bit
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 346
X-Equipment-ID: 6742
X-FS-Support: update_display,send_info
Remote-Party-ID: <sip:"testin2"@10.166.0.2>;party=calling;screen=yes;privacy=off

v=0
o=FreeSWITCH 1554694904 1554694905 IN IP4 10.166.0.2
s=FreeSWITCH
c=IN IP4 10.166.0.2
t=0 0
m=audio 21416 RTP/AVP 18 0 8 102 103 4 101
a=rtpmap:18 G729/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:102 SPEEX/8000
a=rtpmap:103 L16/8000
a=rtpmap:4 G723/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20

*** 10.164.0.10 -> 10.166.0.2
SIP/2.0 100 trying -- your call is important to us
Via: SIP/2.0/UDP 10.166.0.2;rport=5060;branch=z9hG4bKt1B9UyKUa3vgD;received=10.166.0.2
From: "" <sip:testin2 at 10.166.0.2>;tag=QpHt7yyFm1pDQ
To: <sip:79200204544 at 10.164.0.10:5060>
Call-ID: ef3258bd-d484-1237-cfbc-42010aa60002
CSeq: 2804368 INVITE
Server: kamailio (5.1.7 (x86_64/linux))
Content-Length: 0


*** 10.164.0.10 -> 95.79.44.125
SIP/2.0 183 Session Progress
From: <sip:testin2 at 35.204.224.219>;tag=8035856a
To: <sip:79200204544 at 35.204.224.219>;tag=pDr153DcQr0tB
Call-ID: 94385YjUxNjIwNzgxNmQ3N2NkNjdhYTNmZDc0OGUxOTQ0NGE
CSeq: 1 INVITE
User-Agent: FreeSWITCH-mod_sofia/1.8.5+git-20190227T164909Z~cb1f3ab6f2~64bit
Accept: application/sdp
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 216
Remote-Party-ID: "79200204544" <sip:79200204544 at 35.204.224.219>;party=calling;privacy=off;screen=no
Via: SIP/2.0/UDP 95.79.44.125:5060;received=95.79.44.125;branch=z9hG4bK-524287-1---fc4d9360f0b3d218;rport=5060
Contact: <sip:atpsh-5cab164e-6880-1 at 35.204.224.219>

v=0
o=FreeSWITCH 1554690954 1554690955 IN IP4 10.166.0.2
s=FreeSWITCH
c=IN IP4 10.166.0.2
t=0 0
m=audio 25366 RTP/AVP 8 101
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20

*** 10.164.0.10 -> 95.79.44.126
INVITE sip:79200204544 at 95.79.44.126:5060 SIP/2.0
Via: SIP/2.0/UDP 35.204.224.219:5060;branch=z9hG4bK03b2.9008b440cc6f02db12a9949bd46ba9ff.0
Max-Forwards: 70
From: "" <sip:testin2 at 10.166.0.2>;tag=QpHt7yyFm1pDQ
To: <sip:79200204544 at 10.164.0.10:5060>
Call-ID: ef3258bd-d484-1237-cfbc-42010aa60002
CSeq: 2804368 INVITE
User-Agent: FreeSWITCH-mod_sofia/1.8.5+git-20190227T164909Z~cb1f3ab6f2~64bit
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 346
X-FS-Support: update_display,send_info
Remote-Party-ID: <sip:"testin2"@10.166.0.2>;party=calling;screen=yes;privacy=off
Contact: <sip:btpsh-5cab164e-6884-1 at 35.204.224.219>

v=0
o=FreeSWITCH 1554694904 1554694905 IN IP4 10.166.0.2
s=FreeSWITCH
c=IN IP4 10.166.0.2
t=0 0
m=audio 21416 RTP/AVP 18 0 8 102 103 4 101
a=rtpmap:18 G729/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:102 SPEEX/8000
a=rtpmap:103 L16/8000
a=rtpmap:4 G723/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=ptime:20

*** 95.79.44.126 -> 10.164.0.10
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 35.204.224.219:5060;branch=z9hG4bK03b2.9008b440cc6f02db12a9949bd46ba9ff.0
From: "" <sip:testin2 at 10.166.0.2>;tag=QpHt7yyFm1pDQ
To: <sip:79200204544 at 10.164.0.10:5060>
Call-ID: ef3258bd-d484-1237-cfbc-42010aa60002
CSeq: 2804368 INVITE
Allow: INVITE,ACK,CANCEL,BYE,UPDATE,REGISTER,PRACK,INFO,NOTIFY,REFER,SUBSCRIBE
Cisco-Guid: 32268-2874933264-2348313813-1719855771
User-Agent: TELES CELLX Plus 20.5.36.1 58216060749209
Content-Length: 0

```


### Additional Information

  * **Kamailio Version** - output of `kamailio -v`
version: kamailio 5.1.7 (x86_64/linux)
flags: STATS: Off, USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MEM, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLACKLIST, HAVE_RESOLV_RES
ADAPTIVE_WAIT_LOOPS=1024, MAX_RECV_BUFFER_SIZE 262144 MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: unknown
compiled with gcc 6.3.0

* **Operating System**:
Linux vp-balancer-dev-01 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3 (2019-02-02) x86_64 GNU/Linux


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/issues/1924
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kamailio.org/pipermail/sr-dev/attachments/20190408/55238b44/attachment-0001.html>


More information about the sr-dev mailing list