func_pjsip_contact.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, 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. /*! \file
  19. *
  20. * \brief Get information about a PJSIP contact
  21. *
  22. * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
  23. *
  24. * \ingroup functions
  25. *
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. <depend>pjproject</depend>
  30. <depend>res_pjsip</depend>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <pjsip.h>
  35. #include <pjlib.h>
  36. #include "asterisk/app.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/sorcery.h"
  40. #include "asterisk/res_pjsip.h"
  41. /*** DOCUMENTATION
  42. <function name="PJSIP_CONTACT" language="en_US">
  43. <synopsis>
  44. Get information about a PJSIP contact
  45. </synopsis>
  46. <syntax>
  47. <parameter name="name" required="true">
  48. <para>The name of the contact to query.</para>
  49. </parameter>
  50. <parameter name="field" required="true">
  51. <para>The configuration option for the contact to query for.
  52. Supported options are those fields on the
  53. <replaceable>contact</replaceable> object.</para>
  54. <enumlist>
  55. <configOptionToEnum>
  56. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='contact']/configOption)"/>
  57. </configOptionToEnum>
  58. </enumlist>
  59. </parameter>
  60. </syntax>
  61. </function>
  62. ***/
  63. static int contact_function_get_permanent(void *obj, void *arg, int flags)
  64. {
  65. const char *id = arg;
  66. if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
  67. return CMP_MATCH | CMP_STOP;
  68. }
  69. return 0;
  70. }
  71. static int pjsip_contact_function_read(struct ast_channel *chan,
  72. const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  73. {
  74. struct ast_sorcery *pjsip_sorcery;
  75. char *parsed_data = ast_strdupa(data);
  76. char *contact_name;
  77. RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
  78. RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
  79. int res = 0;
  80. AST_DECLARE_APP_ARGS(args,
  81. AST_APP_ARG(contact_name);
  82. AST_APP_ARG(field_name);
  83. );
  84. /* Check for zero arguments */
  85. if (ast_strlen_zero(parsed_data)) {
  86. ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
  87. return -1;
  88. }
  89. AST_STANDARD_APP_ARGS(args, parsed_data);
  90. if (ast_strlen_zero(args.contact_name)) {
  91. ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
  92. return -1;
  93. }
  94. if (ast_strlen_zero(args.field_name)) {
  95. ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
  96. return -1;
  97. }
  98. pjsip_sorcery = ast_sip_get_sorcery();
  99. if (!pjsip_sorcery) {
  100. ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
  101. return -1;
  102. }
  103. /* Determine if this is a permanent contact or a normal contact */
  104. if ((contact_name = strstr(args.contact_name, "@@"))) {
  105. size_t aor_name_len = contact_name - args.contact_name;
  106. char aor_name[aor_name_len + 1];
  107. RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
  108. /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
  109. strncpy(aor_name, args.contact_name, aor_name_len);
  110. aor_name[aor_name_len] = '\0';
  111. aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
  112. if (!aor_obj) {
  113. ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
  114. return -1;
  115. }
  116. contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
  117. } else {
  118. contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
  119. }
  120. if (!contact_obj) {
  121. ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
  122. return -1;
  123. }
  124. contact_status = ast_sorcery_retrieve_by_id(pjsip_sorcery, CONTACT_STATUS, ast_sorcery_object_get_id(contact_obj));
  125. if (!strcmp(args.field_name, "status")) {
  126. if (!contact_status) {
  127. ast_str_set(buf, len, "%s", "Unknown");
  128. } else if (contact_status->status == UNAVAILABLE) {
  129. ast_str_set(buf, len, "%s", "Unreachable");
  130. } else if (contact_status->status == AVAILABLE) {
  131. ast_str_set(buf, len, "%s", "Reachable");
  132. }
  133. } else if (!strcmp(args.field_name, "rtt")) {
  134. if (!contact_status) {
  135. ast_str_set(buf, len, "%s", "N/A");
  136. } else {
  137. ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
  138. }
  139. } else {
  140. struct ast_variable *change_set;
  141. struct ast_variable *it_change_set;
  142. change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
  143. if (!change_set) {
  144. ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
  145. return -1;
  146. }
  147. for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
  148. if (!strcmp(it_change_set->name, args.field_name)) {
  149. ast_str_set(buf, len, "%s", it_change_set->value);
  150. break;
  151. }
  152. }
  153. if (!it_change_set) {
  154. ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
  155. res = 1;
  156. }
  157. ast_variables_destroy(change_set);
  158. }
  159. return res;
  160. }
  161. static struct ast_custom_function pjsip_contact_function = {
  162. .name = "PJSIP_CONTACT",
  163. .read2 = pjsip_contact_function_read,
  164. };
  165. static int unload_module(void)
  166. {
  167. return ast_custom_function_unregister(&pjsip_contact_function);
  168. }
  169. static int load_module(void)
  170. {
  171. return ast_custom_function_register(&pjsip_contact_function);
  172. }
  173. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP contact");