]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_pjsip_transport_websocket: save the original contact host
authorThomas Guebels <tgu@escaux.com>
Wed, 4 May 2022 15:37:18 +0000 (17:37 +0200)
committerFriendly Automation <jenkins2@gerrit.asterisk.org>
Fri, 13 May 2022 13:58:09 +0000 (08:58 -0500)
This is needed to be able to restore it in REGISTER responses,
otherwise the client won't be able to find the contact it created.

ASTERISK-30042

Change-Id: I0c5823918199acf09246b3b206fbde66773688f6

res/res_pjsip_transport_websocket.c

index 1b882dac14bd90faee172c51c7cf0affe293a284..9894a8c4ae1cb11727265f781ea5e56508f5ce72 100644 (file)
@@ -415,6 +415,32 @@ static void websocket_cb(struct ast_websocket *session, struct ast_variable *par
        ast_websocket_unref(session);
 }
 
+static void save_orig_contact_host(pjsip_rx_data *rdata, pjsip_sip_uri *uri)
+{
+       pjsip_param *x_orig_host;
+       pj_str_t p_value;
+#define COLON_LEN 1
+#define MAX_PORT_LEN 5
+
+       if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG ||
+               rdata->msg_info.msg->line.req.method.id != PJSIP_REGISTER_METHOD) {
+               return;
+       }
+
+       ast_debug(1, "Saving contact '%.*s:%d'\n",
+               (int)uri->host.slen, uri->host.ptr, uri->port);
+
+       x_orig_host = PJ_POOL_ALLOC_T(rdata->tp_info.pool, pjsip_param);
+       x_orig_host->name = pj_strdup3(rdata->tp_info.pool, "x-ast-orig-host");
+       p_value.slen = pj_strlen(&uri->host) + COLON_LEN + MAX_PORT_LEN;
+       p_value.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, p_value.slen + 1);
+       p_value.slen = snprintf(p_value.ptr, p_value.slen + 1, "%.*s:%d", (int)uri->host.slen, uri->host.ptr, uri->port);
+       pj_strassign(&x_orig_host->value, &p_value);
+       pj_list_insert_before(&uri->other_param, x_orig_host);
+
+       return;
+}
+
 /*!
  * \brief Store the transport a message came in on, so it can be used for outbound messages to that contact.
  */
@@ -436,6 +462,10 @@ static pj_bool_t websocket_on_rx_msg(pjsip_rx_data *rdata)
                pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
                const pj_str_t *txp_str = &STR_WS;
 
+               /* Saving the contact on REGISTER so it can be restored on outbound response
+                * This will actually be done by restore_orig_contact_host in res_pjsip_nat, via nat_on_tx_message */
+               save_orig_contact_host(rdata, uri);
+
                if (DEBUG_ATLEAST(4)) {
                        char src_addr_buffer[AST_SOCKADDR_BUFLEN];
                        const char *ipv6_s = "", *ipv6_e = "";