Hi,

I've done a bit more digging and realised that $conid is read-only, and only available for an inbound connection - so I dont think it will achieve what I need.

I did a bit more troubleshooting and observed the differences in the debug log between two identical calls:

This example failed - the INVITEs went out to the incorrect endpoint / TCP connection. The "ulc_conid" from the location table for the TCP endpoint is 13:

Mar  9 10:30:20 proxy-01 /sbin/kamailio[27014]: ERROR: <script>: Adding to main branch. ua: Yealink SIP-T42G 29.83.0.120 ru: sip:442079460000@10.0.130.218:5060 du: sip:203.0.113.1:1046 ulc_conid: 0 flags: 192 ci: 162e3e84-4036-48d7-a1d1-79cc92b7cea4-2-1@sip-server-01
Mar  9 10:30:20 proxy-01 /sbin/kamailio[27014]: ERROR: <script>: Adding to child branch. ua: Yealink SIP-T58 58.85.0.5 ru: sip:442079460000@10.0.130.241:50130;transport=TCP du: sip:203.0.113.1:50130;transport=tcp ulc_conid: 13  flags: 192 ci: 162e3e84-4036-48d7-a1d1-79cc92b7cea4-2-1@sip-server-01

The debug log shows the wrong connection was found by id, which in this case was 2, but should have been 13:

Mar  9 10:30:21 proxy-01 /sbin/kamailio[27014]: INFO: <script>: ONSEND: rm: INVITE ru: sip:442079460000@10.0.130.218:5060 du: sip:203.0.113.1:1046 proto: udp src: 185.28.212.61:5060 dest: 203.0.113.1:1046 ci: 162e3e84-4036-48d7-a1d1-79cc92b7cea4-2-1@sip-server-01
Mar  9 10:30:21 proxy-01 /sbin/kamailio[27014]: INFO: <script>: ONSEND: rm: INVITE ru: sip:442079460000@10.0.130.218:5060 du: sip:203.0.113.1:1046 proto: tcp src: 185.28.212.61:5062 dest: 203.0.113.1:50130 ci: 162e3e84-4036-48d7-a1d1-79cc92b7cea4-2-1@sip-server-01
Mar  9 10:30:21 proxy-01 /sbin/kamailio[27014]: DEBUG: <core> [core/tcp_main.c:1651]: _tcpconn_find(): found connection by id: 2
Mar  9 10:30:21 proxy-01 /sbin/kamailio[27014]: DEBUG: <core> [core/tcp_main.c:2545]: tcpconn_send_put(): found fd in cache (5, 0x7fedc49d2448, 2)

This example worked - the INVITEs went out to the correct endpoints / TCP connections. The "ulc_conid" from the location table for the TCP endpoint is still 13:

Mar  9 10:42:30 proxy-01 /sbin/kamailio[27016]: ERROR: <script>: Adding to main branch. ua: Yealink SIP-T42G 29.83.0.120 ru: sip:442079460000@10.0.130.218:5060 du: sip:203.0.113.1:1046 ulc_conid: 0 flags: 192 ci: 95ba0691-bdfc-4293-b99d-d46946cb5180-2-1@sip-server-01
Mar  9 10:42:30 proxy-01 /sbin/kamailio[27016]: ERROR: <script>: Adding to child branch. ua: Yealink SIP-T58 58.85.0.5 ru: sip:442079460000@10.0.130.241:50130;transport=TCP du: sip:203.0.113.1:50130;transport=tcp ulc_conid: 13 flags: 192 ci: 95ba0691-bdfc-4293-b99d-d46946cb5180-2-1@sip-server-01


The debug log shows the correct connection was found by peer address and determined the correct connection 13:

Mar  9 10:42:31 proxy-01 /sbin/kamailio[27016]: INFO: <script>: ONSEND: rm: INVITE ru: sip:442079460000@10.0.130.218:5060 du: sip:203.0.113.1:1046 proto: udp src: 185.28.212.61:5060 dest: 203.0.113.1:1046 conid: <null> ci: 95ba0691-bdfc-4293-b99d-d46946cb5180-2-1@sip-server-01
Mar  9 10:42:31 proxy-01 /sbin/kamailio[27016]: INFO: <script>: ONSEND: rm: INVITE ru: sip:442079460000@10.0.130.218:5060 du: sip:203.0.113.1:1046 proto: tcp src: 185.28.212.61:5062 dest: 203.0.113.1:50130 conid: <null> ci: 95ba0691-bdfc-4293-b99d-d46946cb5180-2-1@sip-server-01
Mar  9 10:42:31 proxy-01 /sbin/kamailio[27016]: DEBUG: <core> [core/tcp_main.c:1670]: _tcpconn_find(): found connection by peer address (id: 13)
Mar  9 10:42:31 proxy-01 /sbin/kamailio[27016]: DEBUG: <core> [core/tcp_main.c:2548]: tcpconn_send_put(): tcp connection found (0x7fedc49ce0e8), acquiring fd

Additionally I checked the connection IDs and IPs / Ports before a failed and working call and they had not changed, it was the same TCP connection - so it doesn't appear to be some interaction with a connection closing or changing. We also don't see this issue using the standard lookup() function.

Does anyone know why Kamailio is intermittently switching between finding by peer address and id, and why it's using the wrong ID? 

Thanks again
Matthew


On Tue, Mar 9, 2021 at 8:56 AM Marrold <kamailio@marrold.co.uk> wrote:
Hi all,

I'm currently adding a feature to our Kamailio configuration to fork calls based on user agent.

To do so I'm getting the registered endpoints with reg_fetch_contacts() iterating through and matching on them, then using seturi() / append_branch() and setting the dst-uri and flags as required.

However, calls are intermittently going to the wrong TCP connection, despite the logs showing the correct destination IP and port in the ONSEND route.

Looking in the location table I can see each registration has a connection_id, and the debug log indicates it's finding the connection by ID:

 DEBUG: <core> [core/tcp_main.c:1651]: _tcpconn_find(): found connection by id: 3

Is there a way to set the conid per branch? Is it necessary?

We're using kamailio 5.3.3 on Debian 10.

Thanks
Matthew