res_pjsip_dialog_info_body_generator.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. struct ast_sip_endpoint *endpoint = NULL;
  93. unsigned int notify_early_inuse_ringing = 0;
  94. if (!local || !state_data->sub) {
  95. return -1;
  96. }
  97. if (dialog_info_xml_get_version(state_data->sub, &version)) {
  98. ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
  99. return -1;
  100. }
  101. stripped = ast_strip_quoted(local, "<", ">");
  102. ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
  103. if (state_data->sub && (endpoint = ast_sip_subscription_get_endpoint(state_data->sub))) {
  104. notify_early_inuse_ringing = endpoint->notify_early_inuse_ringing;
  105. ao2_cleanup(endpoint);
  106. }
  107. ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
  108. &pidfstate, &pidfnote, &local_state, notify_early_inuse_ringing);
  109. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
  110. snprintf(version_str, sizeof(version_str), "%u", version);
  111. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
  112. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
  113. ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
  114. dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
  115. ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
  116. if (!ast_strlen_zero(statestring) && !strcmp(statestring, "early")) {
  117. ast_sip_presence_xml_create_attr(state_data->pool, dialog, "direction", "recipient");
  118. }
  119. state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
  120. pj_strdup2(state_data->pool, &state->content, statestring);
  121. if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
  122. pj_xml_node *local_node, *target, *param;
  123. local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
  124. target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
  125. ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
  126. param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
  127. ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
  128. ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
  129. }
  130. return 0;
  131. }
  132. /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
  133. * too large to send.
  134. */
  135. #define MAX_STRING_GROWTHS 3
  136. static void dialog_info_to_string(void *body, struct ast_str **str)
  137. {
  138. pj_xml_node *dialog_info = body;
  139. int growths = 0;
  140. int size;
  141. do {
  142. size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
  143. if (size <= AST_PJSIP_XML_PROLOG_LEN) {
  144. ast_str_make_space(str, ast_str_size(*str) * 2);
  145. ++growths;
  146. }
  147. } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
  148. if (size <= AST_PJSIP_XML_PROLOG_LEN) {
  149. ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
  150. return;
  151. }
  152. *(ast_str_buffer(*str) + size) = '\0';
  153. ast_str_update(*str);
  154. }
  155. static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
  156. .type = "application",
  157. .subtype = "dialog-info+xml",
  158. .body_type = AST_SIP_EXTEN_STATE_DATA,
  159. .allocate_body = dialog_info_allocate_body,
  160. .generate_body_content = dialog_info_generate_body_content,
  161. .to_string = dialog_info_to_string,
  162. /* No need for a destroy_body callback since we use a pool */
  163. };
  164. static int load_module(void)
  165. {
  166. CHECK_PJSIP_PUBSUB_MODULE_LOADED();
  167. if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
  168. return AST_MODULE_LOAD_DECLINE;
  169. }
  170. return AST_MODULE_LOAD_SUCCESS;
  171. }
  172. static int unload_module(void)
  173. {
  174. ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
  175. return 0;
  176. }
  177. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
  178. .support_level = AST_MODULE_SUPPORT_CORE,
  179. .load = load_module,
  180. .unload = unload_module,
  181. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  182. );