res_pjsip_session: properly handle SDP from a forked call with early media
[asterisk/asterisk.git] / res / res_pjsip_nat.c
index 683ae61..1d42805 100644 (file)
 static void rewrite_uri(pjsip_rx_data *rdata, pjsip_sip_uri *uri)
 {
        pj_cstr(&uri->host, rdata->pkt_info.src_name);
-       if (strcasecmp("udp", rdata->tp_info.transport->type_name)) {
+       uri->port = rdata->pkt_info.src_port;
+       if (!strcasecmp("WSS", rdata->tp_info.transport->type_name)) {
+               /* WSS is special, we don't want to overwrite the URI at all as it needs to be ws */
+       } else if (strcasecmp("udp", rdata->tp_info.transport->type_name)) {
                uri->transport_param = pj_str(rdata->tp_info.transport->type_name);
        } else {
                uri->transport_param.slen = 0;
        }
-       uri->port = rdata->pkt_info.src_port;
 }
 
 static int rewrite_route_set(pjsip_rx_data *rdata, pjsip_dialog *dlg)
@@ -150,20 +152,20 @@ struct request_transport_details {
 };
 
 /*! \brief Callback function for finding the transport the request is going out on */
-static int find_transport_in_use(void *obj, void *arg, int flags)
+static int find_transport_state_in_use(void *obj, void *arg, int flags)
 {
-       struct ast_sip_transport *transport = obj;
+       struct ast_sip_transport_state *transport_state = obj;
        struct request_transport_details *details = arg;
 
        /* If an explicit transport or factory matches then this is what is in use, if we are unavailable
         * to compare based on that we make sure that the type is the same and the source IP address/port are the same
         */
-       if ((details->transport && details->transport == transport->state->transport) ||
-               (details->factory && details->factory == transport->state->factory) ||
-               ((details->type == transport->type) && (transport->state->factory) &&
-                       !pj_strcmp(&transport->state->factory->addr_name.host, &details->local_address) &&
-                       transport->state->factory->addr_name.port == details->local_port)) {
-               return CMP_MATCH | CMP_STOP;
+       if (transport_state && ((details->transport && details->transport == transport_state->transport) ||
+               (details->factory && details->factory == transport_state->factory) ||
+               ((details->type == transport_state->type) && (transport_state->factory) &&
+                       !pj_strcmp(&transport_state->factory->addr_name.host, &details->local_address) &&
+                       transport_state->factory->addr_name.port == details->local_port))) {
+               return CMP_MATCH;
        }
 
        return 0;
@@ -204,8 +206,9 @@ static int nat_invoke_hook(void *obj, void *arg, int flags)
 
 static pj_status_t nat_on_tx_message(pjsip_tx_data *tdata)
 {
-       RAII_VAR(struct ao2_container *, transports, NULL, ao2_cleanup);
+       RAII_VAR(struct ao2_container *, transport_states, NULL, ao2_cleanup);
        RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_sip_transport_state *, transport_state, NULL, ao2_cleanup);
        struct request_transport_details details = { 0, };
        pjsip_via_hdr *via = NULL;
        struct ast_sockaddr addr = { { 0, } };
@@ -247,34 +250,45 @@ static pj_status_t nat_on_tx_message(pjsip_tx_data *tdata)
                }
        }
 
-       if (!(transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL)) ||
-               !(transport = ao2_callback(transports, 0, find_transport_in_use, &details)) || !transport->localnet ||
-               ast_sockaddr_isnull(&transport->external_address)) {
+       if (!(transport_states = ast_sip_get_transport_states())) {
                return PJ_SUCCESS;
        }
 
-       ast_sockaddr_parse(&addr, tdata->tp_info.dst_name, PARSE_PORT_FORBID);
-       ast_sockaddr_set_port(&addr, tdata->tp_info.dst_port);
+       if (!(transport_state = ao2_callback(transport_states, 0, find_transport_state_in_use, &details))) {
+               return PJ_SUCCESS;
+       }
 
-       /* See if where we are sending this request is local or not, and if not that we can get a Contact URI to modify */
-       if (ast_apply_ha(transport->localnet, &addr) != AST_SENSE_ALLOW) {
+       if (!(transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_state->id))) {
                return PJ_SUCCESS;
        }
 
-       /* Update the contact header with the external address */
-       if (uri || (uri = nat_get_contact_sip_uri(tdata))) {
-               pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport->external_address));
-               if (transport->external_signaling_port) {
-                       uri->port = transport->external_signaling_port;
-                       ast_debug(4, "Re-wrote Contact URI port to %d\n", uri->port);
+       if (transport_state->localnet) {
+               ast_sockaddr_parse(&addr, tdata->tp_info.dst_name, PARSE_PORT_FORBID);
+               ast_sockaddr_set_port(&addr, tdata->tp_info.dst_port);
+
+               /* See if where we are sending this request is local or not, and if not that we can get a Contact URI to modify */
+               if (ast_sip_transport_is_local(transport_state, &addr)) {
+                       ast_debug(5, "Request is being sent to local address, skipping NAT manipulation\n");
+                       return PJ_SUCCESS;
                }
        }
 
-       /* Update the via header if relevant */
-       if ((tdata->msg->type == PJSIP_REQUEST_MSG) && (via || (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL)))) {
-               pj_strdup2(tdata->pool, &via->sent_by.host, ast_sockaddr_stringify_host(&transport->external_address));
-               if (transport->external_signaling_port) {
-                       via->sent_by.port = transport->external_signaling_port;
+       if (!ast_sockaddr_isnull(&transport_state->external_signaling_address)) {
+               /* Update the contact header with the external address */
+               if (uri || (uri = nat_get_contact_sip_uri(tdata))) {
+                       pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport_state->external_signaling_address));
+                       if (transport->external_signaling_port) {
+                               uri->port = transport->external_signaling_port;
+                               ast_debug(4, "Re-wrote Contact URI port to %d\n", uri->port);
+                       }
+               }
+
+               /* Update the via header if relevant */
+               if ((tdata->msg->type == PJSIP_REQUEST_MSG) && (via || (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL)))) {
+                       pj_strdup2(tdata->pool, &via->sent_by.host, ast_sockaddr_stringify_host(&transport_state->external_signaling_address));
+                       if (transport->external_signaling_port) {
+                               via->sent_by.port = transport->external_signaling_port;
+                       }
                }
        }
 
@@ -343,18 +357,12 @@ static int unload_module(void)
 
 static int load_module(void)
 {
-       CHECK_PJSIP_SESSION_MODULE_LOADED();
-
        if (ast_sip_register_service(&nat_module)) {
                ast_log(LOG_ERROR, "Could not register NAT module for incoming and outgoing requests\n");
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
-       if (ast_sip_session_register_supplement(&nat_supplement)) {
-               ast_log(LOG_ERROR, "Could not register NAT session supplement for incoming and outgoing INVITE requests\n");
-               unload_module();
-               return AST_MODULE_LOAD_FAILURE;
-       }
+       ast_sip_session_register_supplement(&nat_supplement);
 
        return AST_MODULE_LOAD_SUCCESS;
 }
@@ -364,4 +372,5 @@ AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP NAT Support",
        .load = load_module,
        .unload = unload_module,
        .load_pri = AST_MODPRI_APP_DEPEND,
+       .requires = "res_pjsip,res_pjsip_session",
 );