res_pjsip_dialog_info_body_generator.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. <depend>res_pjsip_pubsub</depend>
  22. <depend>res_pjsip_exten_state</depend>
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. #include <pjsip.h>
  27. #include <pjsip_simple.h>
  28. #include <pjlib.h>
  29. #include "asterisk/module.h"
  30. #include "asterisk/res_pjsip.h"
  31. #include "asterisk/res_pjsip_pubsub.h"
  32. #include "asterisk/res_pjsip_presence_xml.h"
  33. #include "asterisk/res_pjsip_body_generator_types.h"
  34. /*! \brief Structure which contains dialog-info+xml state information */
  35. struct dialog_info_xml_state {
  36. /*! \brief Version to place into the next NOTIFY */
  37. unsigned int version;
  38. };
  39. /*! \brief Destructor for dialog-info+xml information */
  40. static void dialog_info_xml_state_destroy(void *obj)
  41. {
  42. ast_free(obj);
  43. }
  44. /*! \brief Datastore for attaching dialog-info+xml state information */
  45. static const struct ast_datastore_info dialog_info_xml_datastore = {
  46. .type = "dialog-info+xml",
  47. .destroy = dialog_info_xml_state_destroy,
  48. };
  49. static void *dialog_info_allocate_body(void *data)
  50. {
  51. struct ast_sip_exten_state_data *state_data = data;
  52. return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
  53. }
  54. static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ast_sip_subscription *sub)
  55. {
  56. struct ast_datastore *datastore = ast_sip_subscription_get_datastore(sub, "dialog-info+xml");
  57. if (datastore) {
  58. return datastore;
  59. }
  60. datastore = ast_sip_subscription_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
  61. if (!datastore) {
  62. return NULL;
  63. }
  64. datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state));
  65. if (!datastore->data || ast_sip_subscription_add_datastore(sub, datastore)) {
  66. ao2_ref(datastore, -1);
  67. return NULL;
  68. }
  69. return datastore;
  70. }
  71. static unsigned int dialog_info_xml_get_version(struct ast_sip_subscription *sub, unsigned int *version)
  72. {
  73. struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(sub);
  74. struct dialog_info_xml_state *state;
  75. if (!datastore) {
  76. return -1;
  77. }
  78. state = datastore->data;
  79. *version = state->version++;
  80. ao2_ref(datastore, -1);
  81. return 0;
  82. }
  83. static int dialog_info_generate_body_content(void *body, void *data)
  84. {
  85. pj_xml_node *dialog_info = body, *dialog, *state;
  86. struct ast_sip_exten_state_data *state_data = data;
  87. char *local = ast_strdupa(state_data->local), *stripped, *statestring = NULL;
  88. char *pidfstate = NULL, *pidfnote = NULL;
  89. enum ast_sip_pidf_state local_state;
  90. unsigned int version;
  91. char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
  92. if (!local || !state_data->sub) {
  93. return -1;
  94. }
  95. if (dialog_info_xml_get_version(state_data->sub, &version)) {
  96. ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
  97. return -1;
  98. }
  99. stripped = ast_strip_quoted(local, "<", ">");
  100. ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
  101. ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
  102. &pidfstate, &pidfnote, &local_state);
  103. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
  104. snprintf(version_str, sizeof(version_str), "%u", version);
  105. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
  106. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
  107. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
  108. dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
  109. ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
  110. state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
  111. pj_strdup2(state_data->pool, &state->content, statestring);
  112. if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
  113. pj_xml_node *local_node, *target, *param;
  114. local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
  115. target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
  116. ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
  117. param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
  118. ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
  119. ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
  120. }
  121. return 0;
  122. }
  123. /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
  124. * too large to send.
  125. */
  126. #define MAX_STRING_GROWTHS 3
  127. static void dialog_info_to_string(void *body, struct ast_str **str)
  128. {
  129. pj_xml_node *dialog_info = body;
  130. int growths = 0;
  131. int size;
  132. do {
  133. size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str), PJ_TRUE);
  134. if (size == AST_PJSIP_XML_PROLOG_LEN) {
  135. ast_str_make_space(str, ast_str_size(*str) * 2);
  136. ++growths;
  137. }
  138. } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
  139. if (size == AST_PJSIP_XML_PROLOG_LEN) {
  140. ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
  141. return;
  142. }
  143. *(ast_str_buffer(*str) + size) = '\0';
  144. ast_str_update(*str);
  145. }
  146. static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
  147. .type = "application",
  148. .subtype = "dialog-info+xml",
  149. .body_type = AST_SIP_EXTEN_STATE_DATA,
  150. .allocate_body = dialog_info_allocate_body,
  151. .generate_body_content = dialog_info_generate_body_content,
  152. .to_string = dialog_info_to_string,
  153. /* No need for a destroy_body callback since we use a pool */
  154. };
  155. static int load_module(void)
  156. {
  157. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  158. if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
  159. return AST_MODULE_LOAD_DECLINE;
  160. }
  161. return AST_MODULE_LOAD_SUCCESS;
  162. }
  163. static int unload_module(void)
  164. {
  165. ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
  166. return 0;
  167. }
  168. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
  169. .support_level = AST_MODULE_SUPPORT_CORE,
  170. .load = load_module,
  171. .unload = unload_module,
  172. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  173. );