<div dir="ltr">Hi,<div><br>We're attempting to implement a mid-registrar for websocket connections but are running into some issues where the tcpconn-id and socket attributes are not stored correctly in memory (usrloc).<br>REGISTER Flow: UA1 -> P1 (Mid-Registrar) ~> P2 (Core Proxy/Registrar)<br>INVITE Flow: UA2 -> P2 -> P1 -> UA1<br><br>UA1: baresip cli connecting to P1 using websockets<br>UA2: softphone<br><br>Everything is running locally on a dev machine, and we're using Docker to launch P1 and P2. <br><br>The below code snippet is what we use on P1, and has been simplified.<br>The general idea is as following:<br>1) UA1 sets up the websocket connection and sends a REGISTER request to P1<br>2) P1 checks if this request has already been sent to P2<br>    2.a) If not, then relay the request to P2. Upon receiving a 200 OK from P2 we save the registration in usrloc<br>    2.b) If so, then absorb the request (save) and generate a reply<br>3) UA2 tries to call UA1 via P1 and P2<br><br>The problem occurs at 2.a since the REGISTER request is forwarded using UDP and the 200 OK is also received using UDP. <br>When the registration is stored in reply route, the Socket (udp:x.x.x.x:xxxx) and Tcpconn-Id (-1) are not set to what we would prefer (see Output 1)<br>TCP was also tried as the transport between P1 and P2. Upon receiving the reply from P2 the Tcpconn-Id and Socket of the connection between P1 and P2 was saved in the binding.<br>When 2.b occurs the Socket and Tcpconn-Id are set to the correct values (see Output 2)<br><br>Making a call when the binding state is at 2.a we receive the following error: "TCP/TLS connection (id: 0) for WebSocket could not be found"<br>Making a call when the binding state is at 2.b works perfectly fine since the correct Tcpconn-Id is set. <br><br>We did find a solution, or rather a workaround to this issue:<br>1) Always call the save function in request_route<br>2) If P2 responds with an error (non-200 OK) use the unregister function to remove the binding<br><br>We also tried the tcpops tcp_set_otcpid and tcp_set_otcpid_flag functions after lookup, but this didn't do anything. <br><br>KEMI Script<br>def ksr_request_route(self, msg):<br>    if kemi.is_REGISTER():<br>        aor = "aor"<br><br>        if result := kemi.htable.sht_is_null("registrations", aor) == 1:<br>            kemi.tm.t_on_reply("ksr_register_reply_route")<br>            kemi.dispatcher.ds_select_dst(1, 4)<br>            kemi.tm.t_relay()<br>            return 1<br>        <br>        kemi.registrar.save("registrations", 1)<br><br>        return 1<br><br>    if kemi.is_INVITE():<br>        aor = "aor"<br>        if result := kemi.registrar.lookup_uri("registrations", aor):<br>            kemi.tm.t_relay()<br>            return 1<br><br>def ksr_register_reply_route(self, msg):<br>    if kemi.tm.t_check_status("200") == 1:<br>        kemi.registrar.save("registrations", 1)<br><br>        aor = "aor"<br><br>        kemi.htable.sht_sets("registrations", aor, "")<br><br>        return 1<br><br>Output 1: Binding state when request is being forwarded<br>kamcmd ul.dump<br>{<br>   Domains: {<br>            Domain: {<br>                     Domain: registrations<br>                 Size: 1024<br>                    AoRs: {<br>                               Info: {<br>                                       AoR: <a href="mailto:grant@127.0.0.1">grant@127.0.0.1</a><br>                                       HashID: 409622189<br>                                     Contacts: {<br>                                           Contact: {<br>                                                    Address: sip:grant-0x7fe915a79138@172.16.99.12:9;transport=ws<br>                                                 Expires: 13<br>                                                   Q: 1.000000<br>                                                   Call-ID: 7cdfbf93f23bb54f<br>                                                     CSeq: 34818<br>                                                   User-Agent: n/a<br>                                                       Received: sip:192.168.80.1:62184;transport=ws<br>                                                 Path: [not set]<br>                                                       State: CS_NEW<br>                                                 Flags: 1<br>                                                      CFlags: 20<br>                                                    Socket: udp:<a href="http://10.2.0.15:5060">10.2.0.15:5060</a><br>                                                  Methods: -1<br>                                                   Ruid: uloc-626a80d3-46-1<br>                                                      Instance: <urn:uuid:c67df38d-d5e0-e834-1d98-a8df2d3f291b><br>                                                       Reg-Id: 0<br>                                                     Server-Id: 0<br>                                                  Tcpconn-Id: -1<br>                                                        Keepalive: 0<br>                                                  Last-Keepalive: 1651146965<br>                                                    KA-Roundtrip: 0<br>                                                       Last-Modified: 1651146965<br>                                             }<br>                                     }<br>                             }<br>                     }<br>                     Stats: {<br>                              Records: 1<br>                            Max-Slots: 1<br>                  }<br>             }<br>     }<br>}<br>kamcmd core.tcp_list<br>{<br> id: 1<br> type: WS<br>      state: CONN_OK<br>        timeout: 2<br>    lifetime: 120<br> ref_count: 2<br>  src_ip: 192.168.80.1<br>  src_port: 62184<br>       dst_ip: 192.168.80.15<br> dst_port: 80<br>}<br>kamcmd ws.dump<br>{<br>    connections: {<br>                1: ws:<a href="http://192.168.80.1:62184">192.168.80.1:62184</a> -> ws:<a href="http://192.168.80.15:80">192.168.80.15:80</a> (state: OPEN,  last used 3s ago, sub-protocol: sip)<br>     }<br>     info: {<br>               wscounter: 1<br>          truncated: no<br> }<br>}<br><br>Output 2: Binding state when request is being absorbed<br>kamcmd ul.dump<br>{<br>     Domains: {<br>            Domain: {<br>                     Domain: registrations<br>                 Size: 1024<br>                    AoRs: {<br>                               Info: {<br>                                       AoR: <a href="mailto:grant@127.0.0.1">grant@127.0.0.1</a><br>                                       HashID: 409622189<br>                                     Contacts: {<br>                                           Contact: {<br>                                                    Address: sip:grant-0x7fe915a79138@172.16.99.12:9;transport=ws<br>                                                 Expires: 9<br>                                                    Q: 1.000000<br>                                                   Call-ID: 7cdfbf93f23bb54f<br>                                                     CSeq: 34819<br>                                                   User-Agent: baresip v2.0.2 (x86_64/darwin)<br>                                                    Received: sip:192.168.80.1:62184;transport=ws<br>                                                 Path: [not set]<br>                                                       State: CS_NEW<br>                                                 Flags: 1<br>                                                      CFlags: 20<br>                                                    Socket: tcp:<a href="http://192.168.80.15:80">192.168.80.15:80</a><br>                                                      Methods: 5087<br>                                                 Ruid: uloc-626a80d3-46-1<br>                                                      Instance: <urn:uuid:c67df38d-d5e0-e834-1d98-a8df2d3f291b><br>                                                       Reg-Id: 0<br>                                                     Server-Id: 0<br>                                                  Tcpconn-Id: 1<br>                                                 Keepalive: 0<br>                                                  Last-Keepalive: 1651146979<br>                                                    KA-Roundtrip: 0<br>                                                       Last-Modified: 1651146979<br>                                             }<br>                                     }<br>                             }<br>                     }<br>                     Stats: {<br>                              Records: 1<br>                            Max-Slots: 1<br>                  }<br>             }<br>     }<br>}<br>kamcmd core.tcp_list<br>{<br> id: 1<br> type: WS<br>      state: CONN_OK<br>        timeout: 3<br>    lifetime: 120<br> ref_count: 2<br>  src_ip: 192.168.80.1<br>  src_port: 62184<br>       dst_ip: 192.168.80.15<br> dst_port: 80<br>}<br>kamcmd ws.dump<br>{<br>    connections: {<br>                1: ws:<a href="http://192.168.80.1:62184">192.168.80.1:62184</a> -> ws:<a href="http://192.168.80.15:80">192.168.80.15:80</a> (state: OPEN,  last used 1s ago, sub-protocol: sip)<br>     }<br>     info: {<br>               wscounter: 1<br>          truncated: no<br> }<br>}<br></div></div>