Debian Codegraph @

funcs/func_pjsip_contact.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
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 contact
22 *
23 * \author \verbatim Joshua Colp <[email protected]> \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_CONTACT" language="en_US">
48 <since>
49 <version>13.2.0</version>
50 </since>
51 <synopsis>
52 Get information about a PJSIP contact
53 </synopsis>
54 <syntax>
55 <parameter name="name" required="true">
56 <para>
57 Contact names are in the form of "aor_id@@hash" for dynamic contacts
58 created by the registrar and permanent contacts defined
59 in a <literal>contact</literal> parameter in an <literal>aor</literal>
60 object. You can get the "aor_id@@hash" contact ID by calling the
61 <literal>PJSIP_AOR()</literal> dialplpan function with the AOR name
62 and the <literal>contact</literal> field. You can then pass the value
63 returned to this function.
64 </para>
65 </parameter>
66 <parameter name="field" required="true">
67 <para>The configuration option for the contact to query for.
68 Supported options are those fields on the
69 <replaceable>contact</replaceable> object.</para>
70 <enumlist>
71 <configOptionToEnum>
72 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='contact']/configOption)"/>
73 </configOptionToEnum>
74 <enum name="rtt">
75 <para>The RTT of the last qualify</para>
76 </enum>
77 <enum name="status">
78 <para>Status of the contact</para>
79 </enum>
80 </enumlist>
81 </parameter>
82 </syntax>
83 <see-also>
84 <ref type="function">PJSIP_AOR</ref>
85 </see-also>
86 </function>
87 ***/
88
89 static int contact_function_get_permanent(void *obj, void *arg, int flags)
90 {
91 const char *id = arg;
92
93 if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
94 return CMP_MATCH | CMP_STOP;
95 }
96
97 return 0;
98 }
99
100 static int pjsip_contact_function_read(struct ast_channel *chan,
101 const char *cmd, char *data, struct ast_str **buf, ssize_t len)
102 {
103 struct ast_sorcery *pjsip_sorcery;
104 char *parsed_data = ast_strdupa(data);
105 char *contact_name;
106 RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
107 RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
108 int res = 0;
109
110 AST_DECLARE_APP_ARGS(args,
111 AST_APP_ARG(contact_name);
112 AST_APP_ARG(field_name);
113 );
114
115 /* Check for zero arguments */
116 if (ast_strlen_zero(parsed_data)) {
117 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
118 return -1;
119 }
120
121 AST_STANDARD_APP_ARGS(args, parsed_data);
122
123 if (ast_strlen_zero(args.contact_name)) {
124 ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
125 return -1;
126 }
127
128 if (ast_strlen_zero(args.field_name)) {
129 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
130 return -1;
131 }
132
133 pjsip_sorcery = ast_sip_get_sorcery();
134 if (!pjsip_sorcery) {
135 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
136 return -1;
137 }
138
139 /* Determine if this is a permanent contact or a normal contact */
140 if ((contact_name = strstr(args.contact_name, "@@"))) {
141 size_t aor_name_len = contact_name - args.contact_name;
142 char aor_name[aor_name_len + 1];
143 RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
144
145 /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
146 strncpy(aor_name, args.contact_name, aor_name_len);
147 aor_name[aor_name_len] = '\0';
148
149 aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
150 if (!aor_obj) {
151 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
152 return -1;
153 }
154
155 contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
156 } else {
157 contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
158 }
159
160 if (!contact_obj) {
161 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
162 return -1;
163 }
164
165 contact_status = ast_sip_get_contact_status(contact_obj);
166
167 if (!strcmp(args.field_name, "status")) {
168 ast_str_set(buf, len, "%s", ast_sip_get_contact_status_label(contact_status ? contact_status->status : UNKNOWN));
169 } else if (!strcmp(args.field_name, "rtt")) {
170 if (!contact_status || contact_status->status != AVAILABLE) {
171 ast_str_set(buf, len, "%s", "N/A");
172 } else {
173 ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
174 }
175 } else {
176 struct ast_variable *change_set;
177 struct ast_variable *it_change_set;
178
179 change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
180
181 if (!change_set) {
182 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
183 return -1;
184 }
185
186 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
187 if (!strcmp(it_change_set->name, args.field_name)) {
188 ast_str_set(buf, len, "%s", it_change_set->value);
189 break;
190 }
191 }
192
193 if (!it_change_set) {
194 ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
195
196 res = 1;
197 }
198
199 ast_variables_destroy(change_set);
200 }
201
202 return res;
203 }
204
205
206 static struct ast_custom_function pjsip_contact_function = {
207 .name = "PJSIP_CONTACT",
208 .read2 = pjsip_contact_function_read,
209 };
210
211 static int unload_module(void)
212 {
213 return ast_custom_function_unregister(&pjsip_contact_function);
214 }
215
216 static int load_module(void)
217 {
218 return ast_custom_function_register(&pjsip_contact_function);
219 }
220
221 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP contact",
222 .support_level = AST_MODULE_SUPPORT_CORE,
223 .load = load_module,
224 .unload = unload_module,
225 .requires = "res_pjsip",
226 );