res_srtp: Add support for libsrtp2.1.
[asterisk/asterisk.git] / res / res_pjsip_transport_management.c
index 862ae72..eb92eb7 100644 (file)
@@ -24,6 +24,8 @@
 
 #include "asterisk.h"
 
+#include <signal.h>
+
 #include <pjsip.h>
 #include <pjsip_ua.h>
 
@@ -31,8 +33,8 @@
 #include "asterisk/module.h"
 #include "asterisk/astobj2.h"
 
-/*! \brief Number of buckets for keepalive transports */
-#define TRANSPORTS_BUCKETS 53
+/*! \brief Number of buckets for monitored transports */
+#define TRANSPORTS_BUCKETS 127
 
 #define IDLE_TIMEOUT (pjsip_cfg()->tsx.td)
 
@@ -40,7 +42,7 @@
 static const pj_str_t keepalive_packet = { "\r\n\r\n", 4 };
 
 /*! \brief Global container of active transports */
-static struct ao2_container *transports;
+static AO2_GLOBAL_OBJ_STATIC(monitored_transports);
 
 /*! \brief Scheduler context for timing out connections with no data received */
 static struct ast_sched_context *sched;
@@ -51,9 +53,6 @@ static pthread_t keepalive_thread = AST_PTHREADT_NULL;
 /*! \brief The global interval at which to send keepalives */
 static unsigned int keepalive_interval;
 
-/*! \brief Existing transport manager callback that we need to invoke */
-static pjsip_tp_state_callback tpmgr_state_callback;
-
 /*! \brief Structure for transport to be monitored */
 struct monitored_transport {
        /*! \brief The underlying PJSIP transport */
@@ -82,6 +81,7 @@ static int keepalive_transport_cb(void *obj, void *arg, int flags)
 /*! \brief Thread which sends keepalives to all active connection-oriented transports */
 static void *keepalive_transport_thread(void *data)
 {
+       struct ao2_container *transports;
        pj_thread_desc desc;
        pj_thread_t *thread;
 
@@ -90,69 +90,98 @@ static void *keepalive_transport_thread(void *data)
                return NULL;
        }
 
+       transports = ao2_global_obj_ref(monitored_transports);
+       if (!transports) {
+               return NULL;
+       }
+
        /* Once loaded this module just keeps on going as it is unsafe to stop and change the underlying
         * callback for the transport manager.
         */
-       while (1) {
+       while (keepalive_interval) {
                sleep(keepalive_interval);
                ao2_callback(transports, OBJ_NODATA, keepalive_transport_cb, NULL);
        }
 
+       ao2_ref(transports, -1);
        return NULL;
 }
 
+AST_THREADSTORAGE(desc_storage);
+
 static int idle_sched_cb(const void *data)
 {
-       struct monitored_transport *keepalive = (struct monitored_transport *) data;
-       int sip_received = ast_atomic_fetchadd_int(&keepalive->sip_received, 0);
+       struct monitored_transport *monitored = (struct monitored_transport *) data;
+
+       if (!pj_thread_is_registered()) {
+               pj_thread_t *thread;
+               pj_thread_desc *desc;
+
+               desc = ast_threadstorage_get(&desc_storage, sizeof(pj_thread_desc));
+               if (!desc) {
+                       ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage.\n");
+                       ao2_ref(monitored, -1);
+                       return 0;
+               }
+
+               pj_bzero(*desc, sizeof(*desc));
 
-       if (!sip_received) {
+               pj_thread_register("Transport Monitor", *desc, &thread);
+       }
+
+       if (!monitored->sip_received) {
                ast_log(LOG_NOTICE, "Shutting down transport '%s' since no request was received in %d seconds\n",
-                               keepalive->transport->info, IDLE_TIMEOUT);
-               pjsip_transport_shutdown(keepalive->transport);
+                       monitored->transport->info, IDLE_TIMEOUT / 1000);
+               pjsip_transport_shutdown(monitored->transport);
        }
 
-       ao2_ref(keepalive, -1);
+       ao2_ref(monitored, -1);
        return 0;
 }
 
 /*! \brief Destructor for keepalive transport */
 static void monitored_transport_destroy(void *obj)
 {
-       struct monitored_transport *keepalive = obj;
+       struct monitored_transport *monitored = obj;
 
-       pjsip_transport_dec_ref(keepalive->transport);
+       pjsip_transport_dec_ref(monitored->transport);
 }
 
 /*! \brief Callback invoked when transport changes occur */
 static void monitored_transport_state_callback(pjsip_transport *transport, pjsip_transport_state state,
        const pjsip_transport_state_info *info)
 {
+       struct ao2_container *transports;
+
        /* We only care about reliable transports */
-       if (PJSIP_TRANSPORT_IS_RELIABLE(transport) &&
-                       (transport->dir == PJSIP_TP_DIR_INCOMING || keepalive_interval)) {
+       if (PJSIP_TRANSPORT_IS_RELIABLE(transport)
+               && (transport->dir == PJSIP_TP_DIR_INCOMING || keepalive_interval)
+               && (transports = ao2_global_obj_ref(monitored_transports))) {
                struct monitored_transport *monitored;
 
                switch (state) {
                case PJSIP_TP_STATE_CONNECTED:
-                       monitored = ao2_alloc(sizeof(*monitored), monitored_transport_destroy);
+                       monitored = ao2_alloc_options(sizeof(*monitored),
+                               monitored_transport_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
                        if (!monitored) {
                                break;
                        }
                        monitored->transport = transport;
                        pjsip_transport_add_ref(monitored->transport);
+
                        ao2_link(transports, monitored);
+
                        if (transport->dir == PJSIP_TP_DIR_INCOMING) {
                                /* Let the scheduler inherit the reference from allocation */
                                if (ast_sched_add_variable(sched, IDLE_TIMEOUT, idle_sched_cb, monitored, 1) < 0) {
-                                       ao2_unlink(transports, monitored);
-                                       ao2_ref(monitored, -1);
+                                       /* Uh Oh.  Could not schedule the idle check.  Kill the transport. */
                                        pjsip_transport_shutdown(transport);
+                               } else {
+                                       /* monitored ref successfully passed to idle_sched_cb() */
+                                       break;
                                }
-                       } else {
-                               /* No scheduled task, so get rid of the allocation reference */
-                               ao2_ref(monitored, -1);
                        }
+                       ao2_ref(monitored, -1);
                        break;
                case PJSIP_TP_STATE_SHUTDOWN:
                case PJSIP_TP_STATE_DISCONNECTED:
@@ -161,14 +190,15 @@ static void monitored_transport_state_callback(pjsip_transport *transport, pjsip
                default:
                        break;
                }
-       }
 
-       /* Forward to the old state callback if present */
-       if (tpmgr_state_callback) {
-               tpmgr_state_callback(transport, state, info);
+               ao2_ref(transports, -1);
        }
 }
 
+struct ast_sip_tpmgr_state_callback monitored_transport_reg = {
+       monitored_transport_state_callback,
+};
+
 /*! \brief Hashing function for monitored transport */
 static int monitored_transport_hash_fn(const void *obj, int flags)
 {
@@ -222,7 +252,7 @@ static int monitored_transport_cmp_fn(void *obj, void *arg, int flags)
                break;
        }
 
-       return !cmp ? CMP_MATCH | CMP_STOP : 0;
+       return !cmp ? CMP_MATCH : 0;
 }
 
 static void keepalive_global_loaded(const char *object_type)
@@ -245,8 +275,8 @@ static void keepalive_global_loaded(const char *object_type)
 
        if (ast_pthread_create(&keepalive_thread, NULL, keepalive_transport_thread, NULL)) {
                ast_log(LOG_ERROR, "Could not create thread for sending keepalive messages.\n");
-               ao2_ref(transports, -1);
-               return;
+               keepalive_thread = AST_PTHREADT_NULL;
+               keepalive_interval = 0;
        }
 }
 
@@ -263,14 +293,21 @@ static struct ast_sorcery_observer keepalive_global_observer = {
  */
 static pj_bool_t idle_monitor_on_rx_request(pjsip_rx_data *rdata)
 {
+       struct ao2_container *transports;
        struct monitored_transport *idle_trans;
 
+       transports = ao2_global_obj_ref(monitored_transports);
+       if (!transports) {
+               return PJ_FALSE;
+       }
+
        idle_trans = ao2_find(transports, rdata->tp_info.transport->obj_name, OBJ_SEARCH_KEY);
+       ao2_ref(transports, -1);
        if (!idle_trans) {
                return PJ_FALSE;
        }
 
-       ast_atomic_fetchadd_int(&idle_trans->sip_received, +1);
+       idle_trans->sip_received = 1;
        ao2_ref(idle_trans, -1);
 
        return PJ_FALSE;
@@ -284,7 +321,7 @@ static pjsip_module idle_monitor_module = {
 
 static int load_module(void)
 {
-       pjsip_tpmgr *tpmgr;
+       struct ao2_container *transports;
 
        CHECK_PJSIP_MODULE_LOADED();
 
@@ -294,42 +331,57 @@ static int load_module(void)
                ast_log(LOG_ERROR, "Could not create container for transports to perform keepalive on.\n");
                return AST_MODULE_LOAD_DECLINE;
        }
-
-       tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
-       if (!tpmgr) {
-               ast_log(LOG_ERROR, "No transport manager to attach keepalive functionality to.\n");
-               ao2_ref(transports, -1);
-               return AST_MODULE_LOAD_DECLINE;
-       }
+       ao2_global_obj_replace_unref(monitored_transports, transports);
+       ao2_ref(transports, -1);
 
        sched = ast_sched_context_create();
        if (!sched) {
                ast_log(LOG_ERROR, "Failed to create keepalive scheduler context.\n");
-               ao2_ref(transports, -1);
+               ao2_global_obj_release(monitored_transports);
                return AST_MODULE_LOAD_DECLINE;
        }
 
        if (ast_sched_start_thread(sched)) {
                ast_log(LOG_ERROR, "Failed to start keepalive scheduler thread\n");
                ast_sched_context_destroy(sched);
-               ao2_ref(transports, -1);
+               sched = NULL;
+               ao2_global_obj_release(monitored_transports);
                return AST_MODULE_LOAD_DECLINE;
        }
 
        ast_sip_register_service(&idle_monitor_module);
 
-       tpmgr_state_callback = pjsip_tpmgr_get_state_cb(tpmgr);
-       pjsip_tpmgr_set_state_cb(tpmgr, &monitored_transport_state_callback);
+       ast_sip_transport_state_register(&monitored_transport_reg);
 
        ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &keepalive_global_observer);
        ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
+
        ast_module_shutdown_ref(ast_module_info->self);
        return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
 {
-       /* This will never get called */
+       if (keepalive_interval) {
+               keepalive_interval = 0;
+               if (keepalive_thread != AST_PTHREADT_NULL) {
+                       pthread_kill(keepalive_thread, SIGURG);
+                       pthread_join(keepalive_thread, NULL);
+                       keepalive_thread = AST_PTHREADT_NULL;
+               }
+       }
+
+       ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &keepalive_global_observer);
+
+       ast_sip_transport_state_unregister(&monitored_transport_reg);
+
+       ast_sip_unregister_service(&idle_monitor_module);
+
+       ast_sched_context_destroy(sched);
+       sched = NULL;
+
+       ao2_global_obj_release(monitored_transports);
+
        return 0;
 }