Merge "rtp_engine/res_rtp_asterisk: Fix RTP struct reentrancy crashes."
[asterisk/asterisk.git] / res / res_pjsip_rfc3326.c
index e0b1939..d49a170 100644 (file)
@@ -32,6 +32,7 @@
 #include "asterisk/res_pjsip_session.h"
 #include "asterisk/module.h"
 #include "asterisk/causes.h"
+#include "asterisk/threadpool.h"
 
 static void rfc3326_use_reason_header(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
 {
@@ -101,9 +102,15 @@ static void rfc3326_add_reason_header(struct ast_sip_session *session, struct pj
 
 static void rfc3326_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
 {
-       if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method) &&
-            pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method)) ||
-           !session->channel) {
+       if ((pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_bye_method)
+                       && pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method))
+               || !session->channel
+               /*
+                * The session->channel has been seen to go away on us between
+                * checks so we must also be running under the call's serializer
+                * thread.
+                */
+               || session->serializer != ast_threadpool_serializer_get_current()) {
                return;
        }
 
@@ -114,7 +121,9 @@ static void rfc3326_outgoing_response(struct ast_sip_session *session, struct pj
 {
        struct pjsip_status_line status = tdata->msg->line.status;
 
-       if ((status.code < 300) || !session->channel) {
+       if (status.code < 300
+               || !session->channel
+               || session->serializer != ast_threadpool_serializer_get_current()) {
                return;
        }
 
@@ -130,6 +139,8 @@ static struct ast_sip_session_supplement rfc3326_supplement = {
 
 static int load_module(void)
 {
+       CHECK_PJSIP_SESSION_MODULE_LOADED();
+
        ast_sip_session_register_supplement(&rfc3326_supplement);
        return AST_MODULE_LOAD_SUCCESS;
 }
@@ -141,8 +152,8 @@ static int unload_module(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3326 Support",
-               .support_level = AST_MODULE_SUPPORT_CORE,
-               .load = load_module,
-               .unload = unload_module,
-               .load_pri = AST_MODPRI_APP_DEPEND,
-              );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND,
+);