func_pjsip_endpoint.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@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 endpoint
  21. *
  22. * \author \verbatim Matt Jordan <mjordan@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/channel.h"
  40. #include "asterisk/sorcery.h"
  41. #include "asterisk/res_pjsip.h"
  42. /*** DOCUMENTATION
  43. <function name="PJSIP_ENDPOINT" language="en_US">
  44. <synopsis>
  45. Get information about a PJSIP endpoint
  46. </synopsis>
  47. <syntax>
  48. <parameter name="name" required="true">
  49. <para>The name of the endpoint to query.</para>
  50. </parameter>
  51. <parameter name="field" required="true">
  52. <para>The configuration option for the endpoint to query for.
  53. Supported options are those fields on the
  54. <replaceable>endpoint</replaceable> object in
  55. <filename>pjsip.conf</filename>.</para>
  56. <enumlist>
  57. <configOptionToEnum>
  58. <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='endpoint']/configOption)"/>
  59. </configOptionToEnum>
  60. </enumlist>
  61. </parameter>
  62. </syntax>
  63. </function>
  64. ***/
  65. static int pjsip_endpoint_function_read(struct ast_channel *chan,
  66. const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  67. {
  68. struct ast_sorcery *pjsip_sorcery;
  69. char *parsed_data = ast_strdupa(data);
  70. RAII_VAR(void *, endpoint_obj, NULL, ao2_cleanup);
  71. struct ast_variable *change_set;
  72. struct ast_variable *it_change_set;
  73. int res;
  74. AST_DECLARE_APP_ARGS(args,
  75. AST_APP_ARG(endpoint_name);
  76. AST_APP_ARG(field_name);
  77. );
  78. /* Check for zero arguments */
  79. if (ast_strlen_zero(parsed_data)) {
  80. ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
  81. return -1;
  82. }
  83. AST_STANDARD_APP_ARGS(args, parsed_data);
  84. if (ast_strlen_zero(args.endpoint_name)) {
  85. ast_log(AST_LOG_ERROR, "Cannot call %s without an endpoint name to query\n", cmd);
  86. return -1;
  87. }
  88. if (ast_strlen_zero(args.field_name)) {
  89. ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
  90. return -1;
  91. }
  92. pjsip_sorcery = ast_sip_get_sorcery();
  93. if (!pjsip_sorcery) {
  94. ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
  95. return -1;
  96. }
  97. endpoint_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "endpoint", args.endpoint_name);
  98. if (!endpoint_obj) {
  99. ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s'\n", args.endpoint_name);
  100. return -1;
  101. }
  102. change_set = ast_sorcery_objectset_create(pjsip_sorcery, endpoint_obj);
  103. if (!change_set) {
  104. ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s': change set is NULL\n", args.endpoint_name);
  105. return -1;
  106. }
  107. for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
  108. if (!strcmp(it_change_set->name, args.field_name)) {
  109. if (!strcmp(it_change_set->name, "disallow")) {
  110. ast_str_set(buf, len, "!%s", it_change_set->value);
  111. } else {
  112. ast_str_set(buf, len, "%s", it_change_set->value);
  113. }
  114. break;
  115. }
  116. }
  117. res = it_change_set ? 0 : 1;
  118. if (res) {
  119. ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP endpoint\n", args.field_name);
  120. }
  121. ast_variables_destroy(change_set);
  122. return res;
  123. }
  124. static struct ast_custom_function pjsip_endpoint_function = {
  125. .name = "PJSIP_ENDPOINT",
  126. .read2 = pjsip_endpoint_function_read,
  127. };
  128. static int unload_module(void)
  129. {
  130. return ast_custom_function_unregister(&pjsip_endpoint_function);
  131. }
  132. static int load_module(void)
  133. {
  134. return ast_custom_function_register(&pjsip_endpoint_function);
  135. }
  136. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP endpoint");