Asterisk - The Open Source Telephony Project  21.4.1
func_pjsip_endpoint.c
Go to the documentation of this file.
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 
19 /*! \file
20  *
21  * \brief Get information about a PJSIP endpoint
22  *
23  * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
24  *
25  * \ingroup functions
26  *
27  */
28 
29 /*** MODULEINFO
30  <depend>pjproject</depend>
31  <depend>res_pjsip</depend>
32  <support_level>core</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 #include <pjsip.h>
38 #include <pjlib.h>
39 
40 #include "asterisk/app.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/sorcery.h"
45 #include "asterisk/res_pjsip.h"
46 
47 /*** DOCUMENTATION
48  <function name="PJSIP_ENDPOINT" language="en_US">
49  <synopsis>
50  Get information about a PJSIP endpoint
51  </synopsis>
52  <syntax>
53  <parameter name="name" required="true">
54  <para>The name of the endpoint to query.</para>
55  </parameter>
56  <parameter name="field" required="true">
57  <para>The configuration option for the endpoint to query for.
58  Supported options are those fields on the
59  <replaceable>endpoint</replaceable> object in
60  <filename>pjsip.conf</filename>.</para>
61  <enumlist>
62  <configOptionToEnum>
63  <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='endpoint']/configOption)"/>
64  </configOptionToEnum>
65  </enumlist>
66  </parameter>
67  </syntax>
68  </function>
69 ***/
70 
71 static int pjsip_endpoint_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  RAII_VAR(void *, endpoint_obj, NULL, ao2_cleanup);
77  struct ast_variable *change_set;
78  struct ast_variable *it_change_set;
79  int res;
80 
82  AST_APP_ARG(endpoint_name);
83  AST_APP_ARG(field_name);
84  );
85 
86  /* Check for zero arguments */
87  if (ast_strlen_zero(parsed_data)) {
88  ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
89  return -1;
90  }
91 
92  AST_STANDARD_APP_ARGS(args, parsed_data);
93 
94  if (ast_strlen_zero(args.endpoint_name)) {
95  ast_log(AST_LOG_ERROR, "Cannot call %s without an endpoint name to query\n", cmd);
96  return -1;
97  }
98 
99  if (ast_strlen_zero(args.field_name)) {
100  ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
101  return -1;
102  }
103 
104  pjsip_sorcery = ast_sip_get_sorcery();
105  if (!pjsip_sorcery) {
106  ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
107  return -1;
108  }
109 
110  endpoint_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "endpoint", args.endpoint_name);
111  if (!endpoint_obj) {
112  ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s'\n", args.endpoint_name);
113  return -1;
114  }
115 
116  change_set = ast_sorcery_objectset_create(pjsip_sorcery, endpoint_obj);
117  if (!change_set) {
118  ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s': change set is NULL\n", args.endpoint_name);
119  return -1;
120  }
121 
122  for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
123  if (!strcmp(it_change_set->name, args.field_name)) {
124  if (!strcmp(it_change_set->name, "disallow")) {
125  ast_str_set(buf, len, "!%s", it_change_set->value);
126  } else {
127  ast_str_set(buf, len, "%s", it_change_set->value);
128  }
129  break;
130  }
131  }
132 
133  res = it_change_set ? 0 : 1;
134  if (res) {
135  ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP endpoint\n", args.field_name);
136  }
137 
138  ast_variables_destroy(change_set);
139 
140  return res;
141 }
142 
143 
144 static struct ast_custom_function pjsip_endpoint_function = {
145  .name = "PJSIP_ENDPOINT",
146  .read2 = pjsip_endpoint_function_read,
147 };
148 
149 static int unload_module(void)
150 {
151  return ast_custom_function_unregister(&pjsip_endpoint_function);
152 }
153 
154 static int load_module(void)
155 {
156  return ast_custom_function_register(&pjsip_endpoint_function);
157 }
158 
159 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP endpoint",
160  .support_level = AST_MODULE_SUPPORT_CORE,
161  .load = load_module,
162  .unload = unload_module,
163  .requires = "res_pjsip",
164 );
const char * name
Definition: pbx.h:119
struct ast_variable * next
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
Structure for variables, used for configurations and for channel variables.
Full structure for sorcery.
Definition: sorcery.c:230
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Core PBX routines and definitions.
Support for dynamic strings.
Definition: strings.h:623
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1137
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
Sorcery Data Access Layer API.
#define AST_APP_ARG(name)
Define an application argument.