pjsip_transport_management.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include "asterisk.h"
  19. #include <signal.h>
  20. #include <pjsip.h>
  21. #include <pjsip_ua.h>
  22. #include "asterisk/res_pjsip.h"
  23. #include "asterisk/module.h"
  24. #include "asterisk/astobj2.h"
  25. #include "include/res_pjsip_private.h"
  26. /*! \brief Number of buckets for monitored transports */
  27. #define TRANSPORTS_BUCKETS 127
  28. #define IDLE_TIMEOUT (pjsip_cfg()->tsx.td)
  29. /*! \brief The keep alive packet to send */
  30. static const pj_str_t keepalive_packet = { "\r\n\r\n", 4 };
  31. /*! \brief Global container of active transports */
  32. static AO2_GLOBAL_OBJ_STATIC(monitored_transports);
  33. /*! \brief Scheduler context for timing out connections with no data received */
  34. static struct ast_sched_context *sched;
  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 Structure for transport to be monitored */
  40. struct monitored_transport {
  41. /*! \brief The underlying PJSIP transport */
  42. pjsip_transport *transport;
  43. /*! \brief Non-zero if a PJSIP request was received */
  44. int sip_received;
  45. };
  46. static void keepalive_transport_send_keepalive(struct monitored_transport *monitored)
  47. {
  48. pjsip_tpselector selector = {
  49. .type = PJSIP_TPSELECTOR_TRANSPORT,
  50. .u.transport = monitored->transport,
  51. };
  52. pjsip_tpmgr_send_raw(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()),
  53. monitored->transport->key.type,
  54. &selector,
  55. NULL,
  56. keepalive_packet.ptr,
  57. keepalive_packet.slen,
  58. &monitored->transport->key.rem_addr,
  59. pj_sockaddr_get_len(&monitored->transport->key.rem_addr),
  60. NULL, NULL);
  61. }
  62. /*! \brief Thread which sends keepalives to all active connection-oriented transports */
  63. static void *keepalive_transport_thread(void *data)
  64. {
  65. struct ao2_container *transports;
  66. pj_thread_desc desc;
  67. pj_thread_t *thread;
  68. if (pj_thread_register("Asterisk Keepalive Thread", desc, &thread) != PJ_SUCCESS) {
  69. ast_log(LOG_ERROR, "Could not register keepalive thread with PJLIB, keepalives will not occur.\n");
  70. return NULL;
  71. }
  72. transports = ao2_global_obj_ref(monitored_transports);
  73. if (!transports) {
  74. return NULL;
  75. }
  76. /*
  77. * Once loaded this module just keeps on going as it is unsafe to stop
  78. * and change the underlying callback for the transport manager.
  79. */
  80. while (keepalive_interval) {
  81. struct ao2_iterator iter;
  82. struct monitored_transport *monitored;
  83. sleep(keepalive_interval);
  84. /*
  85. * We must use the iterator to avoid deadlock between the container lock
  86. * and the pjproject transport manager group lock when sending
  87. * the keepalive packet.
  88. */
  89. iter = ao2_iterator_init(transports, 0);
  90. for (; (monitored = ao2_iterator_next(&iter)); ao2_ref(monitored, -1)) {
  91. keepalive_transport_send_keepalive(monitored);
  92. }
  93. ao2_iterator_destroy(&iter);
  94. }
  95. ao2_ref(transports, -1);
  96. return NULL;
  97. }
  98. AST_THREADSTORAGE(desc_storage);
  99. static int idle_sched_init_pj_thread(void)
  100. {
  101. if (!pj_thread_is_registered()) {
  102. pj_thread_t *thread;
  103. pj_thread_desc *desc;
  104. desc = ast_threadstorage_get(&desc_storage, sizeof(pj_thread_desc));
  105. if (!desc) {
  106. ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage.\n");
  107. return -1;
  108. }
  109. pj_bzero(*desc, sizeof(*desc));
  110. pj_thread_register("Transport Monitor", *desc, &thread);
  111. }
  112. return 0;
  113. }
  114. static struct monitored_transport *get_monitored_transport_by_name(const char *obj_name)
  115. {
  116. struct ao2_container *transports;
  117. struct monitored_transport *monitored = NULL;
  118. transports = ao2_global_obj_ref(monitored_transports);
  119. if (transports) {
  120. monitored = ao2_find(transports, obj_name, OBJ_SEARCH_KEY);
  121. }
  122. ao2_cleanup(transports);
  123. /* Caller is responsible for cleaning up reference */
  124. return monitored;
  125. }
  126. static int idle_sched_cb(const void *data)
  127. {
  128. char *obj_name = (char *) data;
  129. struct monitored_transport *monitored;
  130. if (idle_sched_init_pj_thread()) {
  131. ast_free(obj_name);
  132. return 0;
  133. }
  134. monitored = get_monitored_transport_by_name(obj_name);
  135. if (monitored) {
  136. if (!monitored->sip_received) {
  137. ast_log(LOG_NOTICE, "Shutting down transport '%s' since no request was received in %d seconds\n",
  138. monitored->transport->info, IDLE_TIMEOUT / 1000);
  139. pjsip_transport_shutdown(monitored->transport);
  140. }
  141. ao2_ref(monitored, -1);
  142. }
  143. ast_free(obj_name);
  144. return 0;
  145. }
  146. static int idle_sched_cleanup(const void *data)
  147. {
  148. char *obj_name = (char *) data;
  149. struct monitored_transport *monitored;
  150. if (idle_sched_init_pj_thread()) {
  151. ast_free(obj_name);
  152. return 0;
  153. }
  154. monitored = get_monitored_transport_by_name(obj_name);
  155. if (monitored) {
  156. pjsip_transport_shutdown(monitored->transport);
  157. ao2_ref(monitored, -1);
  158. }
  159. ast_free(obj_name);
  160. return 0;
  161. }
  162. /*! \brief Destructor for keepalive transport */
  163. static void monitored_transport_destroy(void *obj)
  164. {
  165. struct monitored_transport *monitored = obj;
  166. pjsip_transport_dec_ref(monitored->transport);
  167. }
  168. /*! \brief Callback invoked when transport changes occur */
  169. static void monitored_transport_state_callback(pjsip_transport *transport, pjsip_transport_state state,
  170. const pjsip_transport_state_info *info)
  171. {
  172. struct ao2_container *transports;
  173. /* We only care about reliable transports */
  174. if (PJSIP_TRANSPORT_IS_RELIABLE(transport)
  175. && (transport->dir == PJSIP_TP_DIR_INCOMING || keepalive_interval)
  176. && (transports = ao2_global_obj_ref(monitored_transports))) {
  177. struct monitored_transport *monitored;
  178. switch (state) {
  179. case PJSIP_TP_STATE_CONNECTED:
  180. monitored = ao2_alloc_options(sizeof(*monitored),
  181. monitored_transport_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  182. if (!monitored) {
  183. break;
  184. }
  185. monitored->transport = transport;
  186. pjsip_transport_add_ref(monitored->transport);
  187. ao2_link(transports, monitored);
  188. if (transport->dir == PJSIP_TP_DIR_INCOMING) {
  189. char *obj_name = ast_strdup(transport->obj_name);
  190. if (!obj_name
  191. || ast_sched_add_variable(sched, IDLE_TIMEOUT, idle_sched_cb, obj_name, 1) < 0) {
  192. /* Shut down the transport if anything fails */
  193. pjsip_transport_shutdown(transport);
  194. ast_free(obj_name);
  195. }
  196. }
  197. ao2_ref(monitored, -1);
  198. break;
  199. case PJSIP_TP_STATE_SHUTDOWN:
  200. case PJSIP_TP_STATE_DISCONNECTED:
  201. ao2_find(transports, transport->obj_name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
  202. break;
  203. default:
  204. break;
  205. }
  206. ao2_ref(transports, -1);
  207. }
  208. }
  209. struct ast_sip_tpmgr_state_callback monitored_transport_reg = {
  210. monitored_transport_state_callback,
  211. };
  212. /*! \brief Hashing function for monitored transport */
  213. static int monitored_transport_hash_fn(const void *obj, int flags)
  214. {
  215. const struct monitored_transport *object;
  216. const char *key;
  217. switch (flags & OBJ_SEARCH_MASK) {
  218. case OBJ_SEARCH_KEY:
  219. key = obj;
  220. break;
  221. case OBJ_SEARCH_OBJECT:
  222. object = obj;
  223. key = object->transport->obj_name;
  224. break;
  225. default:
  226. /* Hash can only work on something with a full key. */
  227. ast_assert(0);
  228. return 0;
  229. }
  230. return ast_str_hash(key);
  231. }
  232. /*! \brief Comparison function for monitored transport */
  233. static int monitored_transport_cmp_fn(void *obj, void *arg, int flags)
  234. {
  235. const struct monitored_transport *object_left = obj;
  236. const struct monitored_transport *object_right = arg;
  237. const char *right_key = arg;
  238. int cmp;
  239. switch (flags & OBJ_SEARCH_MASK) {
  240. case OBJ_SEARCH_OBJECT:
  241. right_key = object_right->transport->obj_name;
  242. /* Fall through */
  243. case OBJ_SEARCH_KEY:
  244. cmp = strcmp(object_left->transport->obj_name, right_key);
  245. break;
  246. case OBJ_SEARCH_PARTIAL_KEY:
  247. /*
  248. * We could also use a partial key struct containing a length
  249. * so strlen() does not get called for every comparison instead.
  250. */
  251. cmp = strncmp(object_left->transport->obj_name, right_key, strlen(right_key));
  252. break;
  253. default:
  254. /*
  255. * What arg points to is specific to this traversal callback
  256. * and has no special meaning to astobj2.
  257. */
  258. cmp = 0;
  259. break;
  260. }
  261. return !cmp ? CMP_MATCH : 0;
  262. }
  263. static void keepalive_global_loaded(const char *object_type)
  264. {
  265. unsigned int new_interval = ast_sip_get_keep_alive_interval();
  266. if (new_interval) {
  267. keepalive_interval = new_interval;
  268. } else if (keepalive_interval) {
  269. ast_log(LOG_NOTICE, "Keepalive support can not be disabled once activated.\n");
  270. return;
  271. } else {
  272. /* This will occur if no keepalive interval has been specified at initial start */
  273. return;
  274. }
  275. if (keepalive_thread != AST_PTHREADT_NULL) {
  276. return;
  277. }
  278. if (ast_pthread_create(&keepalive_thread, NULL, keepalive_transport_thread, NULL)) {
  279. ast_log(LOG_ERROR, "Could not create thread for sending keepalive messages.\n");
  280. keepalive_thread = AST_PTHREADT_NULL;
  281. keepalive_interval = 0;
  282. }
  283. }
  284. /*! \brief Observer which is used to update our interval when the global setting changes */
  285. static struct ast_sorcery_observer keepalive_global_observer = {
  286. .loaded = keepalive_global_loaded,
  287. };
  288. /*!
  289. * \brief
  290. * On incoming TCP connections, when we receive a SIP request, we mark that we have
  291. * received a valid SIP request. This way, we will not shut the transport down for
  292. * idleness
  293. */
  294. static pj_bool_t idle_monitor_on_rx_request(pjsip_rx_data *rdata)
  295. {
  296. struct monitored_transport *idle_trans;
  297. idle_trans = get_monitored_transport_by_name(rdata->tp_info.transport->obj_name);
  298. if (idle_trans) {
  299. idle_trans->sip_received = 1;
  300. ao2_ref(idle_trans, -1);
  301. }
  302. return PJ_FALSE;
  303. }
  304. static pjsip_module idle_monitor_module = {
  305. .name = {"idle monitor module", 19},
  306. .priority = PJSIP_MOD_PRIORITY_TRANSPORT_LAYER + 3,
  307. .on_rx_request = idle_monitor_on_rx_request,
  308. };
  309. int ast_sip_initialize_transport_management(void)
  310. {
  311. struct ao2_container *transports;
  312. transports = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, TRANSPORTS_BUCKETS,
  313. monitored_transport_hash_fn, NULL, monitored_transport_cmp_fn);
  314. if (!transports) {
  315. ast_log(LOG_ERROR, "Could not create container for transports to perform keepalive on.\n");
  316. return AST_MODULE_LOAD_DECLINE;
  317. }
  318. ao2_global_obj_replace_unref(monitored_transports, transports);
  319. ao2_ref(transports, -1);
  320. sched = ast_sched_context_create();
  321. if (!sched) {
  322. ast_log(LOG_ERROR, "Failed to create keepalive scheduler context.\n");
  323. ao2_global_obj_release(monitored_transports);
  324. return AST_MODULE_LOAD_DECLINE;
  325. }
  326. if (ast_sched_start_thread(sched)) {
  327. ast_log(LOG_ERROR, "Failed to start keepalive scheduler thread\n");
  328. ast_sched_context_destroy(sched);
  329. sched = NULL;
  330. ao2_global_obj_release(monitored_transports);
  331. return AST_MODULE_LOAD_DECLINE;
  332. }
  333. internal_sip_register_service(&idle_monitor_module);
  334. ast_sip_transport_state_register(&monitored_transport_reg);
  335. ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &keepalive_global_observer);
  336. ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
  337. return AST_MODULE_LOAD_SUCCESS;
  338. }
  339. void ast_sip_destroy_transport_management(void)
  340. {
  341. if (keepalive_interval) {
  342. keepalive_interval = 0;
  343. if (keepalive_thread != AST_PTHREADT_NULL) {
  344. pthread_kill(keepalive_thread, SIGURG);
  345. pthread_join(keepalive_thread, NULL);
  346. keepalive_thread = AST_PTHREADT_NULL;
  347. }
  348. }
  349. ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &keepalive_global_observer);
  350. ast_sip_transport_state_unregister(&monitored_transport_reg);
  351. internal_sip_unregister_service(&idle_monitor_module);
  352. ast_sched_clean_by_callback(sched, idle_sched_cb, idle_sched_cleanup);
  353. ast_sched_context_destroy(sched);
  354. sched = NULL;
  355. ao2_global_obj_release(monitored_transports);
  356. }