Asterisk - The Open Source Telephony Project GIT-master-f36a736
func_pjsip_aor.c
Go to the documentation of this file.
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
19/*! \file
20 *
21 * \brief Get information about a PJSIP AOR
22 *
23 * \author \verbatim Joshua Colp <jcolp@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/sorcery.h"
44#include "asterisk/res_pjsip.h"
45
46/*** DOCUMENTATION
47 <function name="PJSIP_AOR" language="en_US">
48 <synopsis>
49 Get information about a PJSIP AOR
50 </synopsis>
51 <syntax>
52 <parameter name="name" required="true">
53 <para>The name of the AOR to query.</para>
54 </parameter>
55 <parameter name="field" required="true">
56 <para>The configuration option for the AOR to query for.
57 Supported options are those fields on the
58 <replaceable>aor</replaceable> object in
59 <filename>pjsip.conf</filename>.</para>
60 <note><para>
61 When requested with this function, the <literal>contact</literal>
62 parameter will return both permanent and dynamic contacts.
63 </para></note>
64 <note><para>
65 The return value of the <literal>contact</literal> parameter is
66 one or more internal contact IDs separated by commans.
67 To get details about the contact itself, including the URI,
68 call the <literal>PJSIP_CONTACT</literal> dialplan function
69 with the contact ID and the desired contact parameter.
70 </para></note>
71 <para>
72 </para>
73 <para>
74 Available Fields:
75 </para>
76 <enumlist>
77 <configOptionToEnum>
78 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption)"/>
79 </configOptionToEnum>
80 </enumlist>
81 </parameter>
82 </syntax>
83 <see-also>
84 <ref type="function">PJSIP_CONTACT</ref>
85 </see-also>
86 </function>
87***/
88
89static int pjsip_aor_function_read(struct ast_channel *chan,
90 const char *cmd, char *data, struct ast_str **buf, ssize_t len)
91{
92 struct ast_sorcery *pjsip_sorcery;
93 char *parsed_data = ast_strdupa(data);
94 RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
95 int res = 0;
96
98 AST_APP_ARG(aor_name);
99 AST_APP_ARG(field_name);
100 );
101
102 /* Check for zero arguments */
103 if (ast_strlen_zero(parsed_data)) {
104 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
105 return -1;
106 }
107
108 AST_STANDARD_APP_ARGS(args, parsed_data);
109
110 if (ast_strlen_zero(args.aor_name)) {
111 ast_log(AST_LOG_ERROR, "Cannot call %s without an AOR name to query\n", cmd);
112 return -1;
113 }
114
115 if (ast_strlen_zero(args.field_name)) {
116 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
117 return -1;
118 }
119
120 pjsip_sorcery = ast_sip_get_sorcery();
121 if (!pjsip_sorcery) {
122 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
123 return -1;
124 }
125
126 aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", args.aor_name);
127 if (!aor_obj) {
128 ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s'\n", args.aor_name);
129 return -1;
130 }
131
132 if (!strcmp(args.field_name, "contact")) {
133 /* The multiple fields handler for contact does not provide a list of contact object names, which is what we want, so we
134 * handle contact specifically to provide this.
135 */
136 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
137 struct ao2_iterator i;
138 struct ast_sip_contact *contact;
139 int first = 1;
140
141 contacts = ast_sip_location_retrieve_aor_contacts(aor_obj);
142 if (!contacts) {
143 ast_log(LOG_WARNING, "Failed to retrieve contacts for AOR '%s'\n", args.aor_name);
144 return -1;
145 }
146
147 i = ao2_iterator_init(contacts, 0);
148 while ((contact = ao2_iterator_next(&i))) {
149 if (!first) {
150 ast_str_append(buf, len, "%s", ",");
151 }
152
154 first = 0;
155
156 ao2_ref(contact, -1);
157 }
159 } else {
160 struct ast_variable *change_set;
161 struct ast_variable *it_change_set;
162
163 change_set = ast_sorcery_objectset_create(pjsip_sorcery, aor_obj);
164 if (!change_set) {
165 ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s': change set is NULL\n", args.aor_name);
166 return -1;
167 }
168
169 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
170 if (!strcmp(it_change_set->name, args.field_name)) {
171 ast_str_set(buf, len, "%s", it_change_set->value);
172 break;
173 }
174 }
175
176 if (!it_change_set) {
177 ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP AOR\n", args.field_name);
178 res = 1;
179 }
180
181 ast_variables_destroy(change_set);
182 }
183
184 return res;
185}
186
187
189 .name = "PJSIP_AOR",
191};
192
193static int unload_module(void)
194{
196}
197
198static int load_module(void)
199{
201}
202
203AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP AOR",
204 .support_level = AST_MODULE_SUPPORT_CORE,
205 .load = load_module,
206 .unload = unload_module,
207 .requires = "res_pjsip",
struct sla_ringing_trunk * first
Definition: app_sla.c:332
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static struct ast_custom_function pjsip_aor_function
static int pjsip_aor_function_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
static int load_module(void)
static int unload_module(void)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
#define AST_LOG_WARNING
#define AST_LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
struct ao2_container * ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
Retrieve all contacts currently available for an AOR.
Definition: location.c:247
#define NULL
Definition: resample.c:96
Sorcery Data Access Layer API.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1137
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_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
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
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
A SIP address of record.
Definition: res_pjsip.h:475
Contact associated with an address of record.
Definition: res_pjsip.h:389
Full structure for sorcery.
Definition: sorcery.c:230
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
const char * args
#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