res_pjsip_keepalive.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include <pjsip_ua.h>
  26. #include "asterisk/res_pjsip.h"
  27. #include "asterisk/module.h"
  28. #include "asterisk/astobj2.h"
  29. /*! \brief Number of buckets for keepalive transports */
  30. #define KEEPALIVE_TRANSPORTS_BUCKETS 53
  31. /*! \brief The keep alive packet to send */
  32. static const pj_str_t keepalive_packet = { "\r\n\r\n", 4 };
  33. /*! \brief Global container of active transports */
  34. static struct ao2_container *transports;
  35. /*! \brief Thread keeping things alive */
  36. static pthread_t keepalive_thread = AST_PTHREADT_NULL;
  37. /*! \brief The global interval at which to send keepalives */
  38. static unsigned int keepalive_interval;
  39. /*! \brief Existing transport manager callback that we need to invoke */
  40. static pjsip_tp_state_callback tpmgr_state_callback;
  41. /*! \brief Structure for transport to be kept alive */
  42. struct keepalive_transport {
  43. /*! \brief The underlying PJSIP transport */
  44. pjsip_transport *transport;
  45. };
  46. /*! \brief Callback function to send keepalive */
  47. static int keepalive_transport_cb(void *obj, void *arg, int flags)
  48. {
  49. struct keepalive_transport *keepalive = obj;
  50. pjsip_tpselector selector = {
  51. .type = PJSIP_TPSELECTOR_TRANSPORT,
  52. .u.transport = keepalive->transport,
  53. };
  54. pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()),
  55. keepalive->transport->key.type, &selector, NULL, keepalive_packet.ptr, keepalive_packet.slen,
  56. &keepalive->transport->key.rem_addr, pj_sockaddr_get_len(&keepalive->transport->key.rem_addr),
  57. NULL, NULL);
  58. return 0;
  59. }
  60. /*! \brief Thread which sends keepalives to all active connection-oriented transports */
  61. static void *keepalive_transport_thread(void *data)
  62. {
  63. pj_thread_desc desc;
  64. pj_thread_t *thread;
  65. if (pj_thread_register("Asterisk Keepalive Thread", desc, &thread) != PJ_SUCCESS) {
  66. ast_log(LOG_ERROR, "Could not register keepalive thread with PJLIB, keepalives will not occur.\n");
  67. return NULL;
  68. }
  69. /* Once loaded this module just keeps on going as it is unsafe to stop and change the underlying
  70. * callback for the transport manager.
  71. */
  72. while (1) {
  73. sleep(keepalive_interval);
  74. ao2_callback(transports, OBJ_NODATA, keepalive_transport_cb, NULL);
  75. }
  76. return NULL;
  77. }
  78. /*! \brief Destructor for keepalive transport */
  79. static void keepalive_transport_destroy(void *obj)
  80. {
  81. struct keepalive_transport *keepalive = obj;
  82. pjsip_transport_dec_ref(keepalive->transport);
  83. }
  84. /*! \brief Callback invoked when transport changes occur */
  85. static void keepalive_transport_state_callback(pjsip_transport *transport, pjsip_transport_state state,
  86. const pjsip_transport_state_info *info)
  87. {
  88. /* We only care about connection-oriented transports */
  89. if (transport->flag & PJSIP_TRANSPORT_RELIABLE) {
  90. struct keepalive_transport *keepalive;
  91. switch (state) {
  92. case PJSIP_TP_STATE_CONNECTED:
  93. keepalive = ao2_alloc(sizeof(*keepalive), keepalive_transport_destroy);
  94. if (keepalive) {
  95. keepalive->transport = transport;
  96. pjsip_transport_add_ref(keepalive->transport);
  97. ao2_link(transports, keepalive);
  98. ao2_ref(keepalive, -1);
  99. }
  100. break;
  101. case PJSIP_TP_STATE_DISCONNECTED:
  102. ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. /* Forward to the old state callback if present */
  109. if (tpmgr_state_callback) {
  110. tpmgr_state_callback(transport, state, info);
  111. }
  112. }
  113. /*! \brief Hashing function for keepalive transport */
  114. static int keepalive_transport_hash_fn(const void *obj, int flags)
  115. {
  116. const struct keepalive_transport *object;
  117. const char *key;
  118. switch (flags & OBJ_SEARCH_MASK) {
  119. case OBJ_SEARCH_KEY:
  120. key = obj;
  121. break;
  122. case OBJ_SEARCH_OBJECT:
  123. object = obj;
  124. key = object->transport->obj_name;
  125. break;
  126. default:
  127. /* Hash can only work on something with a full key. */
  128. ast_assert(0);
  129. return 0;
  130. }
  131. return ast_str_hash(key);
  132. }
  133. /*! \brief Comparison function for keepalive transport */
  134. static int keepalive_transport_cmp_fn(void *obj, void *arg, int flags)
  135. {
  136. const struct keepalive_transport *object_left = obj;
  137. const struct keepalive_transport *object_right = arg;
  138. const char *right_key = arg;
  139. int cmp;
  140. switch (flags & OBJ_SEARCH_MASK) {
  141. case OBJ_SEARCH_OBJECT:
  142. right_key = object_right->transport->obj_name;
  143. /* Fall through */
  144. case OBJ_SEARCH_KEY:
  145. cmp = strcmp(object_left->transport->obj_name, right_key);
  146. break;
  147. case OBJ_SEARCH_PARTIAL_KEY:
  148. /*
  149. * We could also use a partial key struct containing a length
  150. * so strlen() does not get called for every comparison instead.
  151. */
  152. cmp = strncmp(object_left->transport->obj_name, right_key, strlen(right_key));
  153. break;
  154. default:
  155. /*
  156. * What arg points to is specific to this traversal callback
  157. * and has no special meaning to astobj2.
  158. */
  159. cmp = 0;
  160. break;
  161. }
  162. return !cmp ? CMP_MATCH | CMP_STOP : 0;
  163. }
  164. static void keepalive_global_loaded(const char *object_type)
  165. {
  166. unsigned int new_interval = ast_sip_get_keep_alive_interval();
  167. pjsip_tpmgr *tpmgr;
  168. if (new_interval) {
  169. keepalive_interval = new_interval;
  170. } else if (keepalive_interval) {
  171. ast_log(LOG_NOTICE, "Keepalive support can not be disabled once activated.\n");
  172. return;
  173. } else {
  174. /* This will occur if no keepalive interval has been specified at initial start */
  175. return;
  176. }
  177. if (keepalive_thread != AST_PTHREADT_NULL) {
  178. return;
  179. }
  180. transports = ao2_container_alloc(KEEPALIVE_TRANSPORTS_BUCKETS, keepalive_transport_hash_fn,
  181. keepalive_transport_cmp_fn);
  182. if (!transports) {
  183. ast_log(LOG_ERROR, "Could not create container for transports to perform keepalive on.\n");
  184. return;
  185. }
  186. tpmgr = pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint());
  187. if (!tpmgr) {
  188. ast_log(LOG_ERROR, "No transport manager to attach keepalive functionality to.\n");
  189. ao2_ref(transports, -1);
  190. return;
  191. }
  192. if (ast_pthread_create(&keepalive_thread, NULL, keepalive_transport_thread, NULL)) {
  193. ast_log(LOG_ERROR, "Could not create thread for sending keepalive messages.\n");
  194. ao2_ref(transports, -1);
  195. return;
  196. }
  197. tpmgr_state_callback = pjsip_tpmgr_get_state_cb(tpmgr);
  198. pjsip_tpmgr_set_state_cb(tpmgr, &keepalive_transport_state_callback);
  199. }
  200. /*! \brief Observer which is used to update our interval when the global setting changes */
  201. static struct ast_sorcery_observer keepalive_global_observer = {
  202. .loaded = keepalive_global_loaded,
  203. };
  204. static int load_module(void)
  205. {
  206. CHECK_PJSIP_MODULE_LOADED();
  207. ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &keepalive_global_observer);
  208. ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
  209. ast_module_shutdown_ref(ast_module_info->self);
  210. return AST_MODULE_LOAD_SUCCESS;
  211. }
  212. static int unload_module(void)
  213. {
  214. /* This will never get called */
  215. return 0;
  216. }
  217. static int reload_module(void)
  218. {
  219. ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
  220. return 0;
  221. }
  222. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Stateful Connection Keepalive Support",
  223. .support_level = AST_MODULE_SUPPORT_CORE,
  224. .load = load_module,
  225. .reload = reload_module,
  226. .unload = unload_module,
  227. .load_pri = AST_MODPRI_CHANNEL_DEPEND - 4,
  228. );