Merge "jansson-bundled: Patch for off-nominal crash."
[asterisk/asterisk.git] / funcs / func_pjsip_contact.c
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 contact
22  *
23  * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
24  *
25  * \ingroup functions
26  *
27  */
28
29 /*** MODULEINFO
30         <support_level>core</support_level>
31         <depend>pjproject</depend>
32         <depend>res_pjsip</depend>
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                 <synopsis>
49                         Get information about a PJSIP contact
50                 </synopsis>
51                 <syntax>
52                         <parameter name="name" required="true">
53                                 <para>The name of the contact to query.</para>
54                         </parameter>
55                         <parameter name="field" required="true">
56                                 <para>The configuration option for the contact to query for.
57                                 Supported options are those fields on the
58                                 <replaceable>contact</replaceable> object.</para>
59                                 <enumlist>
60                                         <configOptionToEnum>
61                                                 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='contact']/configOption)"/>
62                                         </configOptionToEnum>
63                                 </enumlist>
64                         </parameter>
65                 </syntax>
66         </function>
67 ***/
68
69 static int contact_function_get_permanent(void *obj, void *arg, int flags)
70 {
71         const char *id = arg;
72
73         if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
74                 return CMP_MATCH | CMP_STOP;
75         }
76
77         return 0;
78 }
79
80 static int pjsip_contact_function_read(struct ast_channel *chan,
81         const char *cmd, char *data, struct ast_str **buf, ssize_t len)
82 {
83         struct ast_sorcery *pjsip_sorcery;
84         char *parsed_data = ast_strdupa(data);
85         char *contact_name;
86         RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
87         RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
88         int res = 0;
89
90         AST_DECLARE_APP_ARGS(args,
91                 AST_APP_ARG(contact_name);
92                 AST_APP_ARG(field_name);
93         );
94
95         /* Check for zero arguments */
96         if (ast_strlen_zero(parsed_data)) {
97                 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
98                 return -1;
99         }
100
101         AST_STANDARD_APP_ARGS(args, parsed_data);
102
103         if (ast_strlen_zero(args.contact_name)) {
104                 ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
105                 return -1;
106         }
107
108         if (ast_strlen_zero(args.field_name)) {
109                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
110                 return -1;
111         }
112
113         pjsip_sorcery = ast_sip_get_sorcery();
114         if (!pjsip_sorcery) {
115                 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
116                 return -1;
117         }
118
119         /* Determine if this is a permanent contact or a normal contact */
120         if ((contact_name = strstr(args.contact_name, "@@"))) {
121                 size_t aor_name_len = contact_name - args.contact_name;
122                 char aor_name[aor_name_len + 1];
123                 RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
124
125                 /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
126                 strncpy(aor_name, args.contact_name, aor_name_len);
127                 aor_name[aor_name_len] = '\0';
128
129                 aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
130                 if (!aor_obj) {
131                         ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
132                         return -1;
133                 }
134
135                 contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
136         } else {
137                 contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
138         }
139
140         if (!contact_obj) {
141                 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
142                 return -1;
143         }
144
145         contact_status = ast_sip_get_contact_status(contact_obj);
146
147         if (!strcmp(args.field_name, "status")) {
148                 ast_str_set(buf, len, "%s", ast_sip_get_contact_status_label(contact_status ? contact_status->status : UNKNOWN));
149         } else if (!strcmp(args.field_name, "rtt")) {
150                 if (!contact_status || contact_status->status != AVAILABLE) {
151                         ast_str_set(buf, len, "%s", "N/A");
152                 } else {
153                         ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
154                 }
155         } else {
156                 struct ast_variable *change_set;
157                 struct ast_variable *it_change_set;
158
159                 change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
160
161                 if (!change_set) {
162                         ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
163                         return -1;
164                 }
165
166                 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
167                         if (!strcmp(it_change_set->name, args.field_name)) {
168                                 ast_str_set(buf, len, "%s", it_change_set->value);
169                                 break;
170                         }
171                 }
172
173                 if (!it_change_set) {
174                         ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
175
176                         res = 1;
177                 }
178
179                 ast_variables_destroy(change_set);
180         }
181
182         return res;
183 }
184
185
186 static struct ast_custom_function pjsip_contact_function = {
187         .name = "PJSIP_CONTACT",
188         .read2 = pjsip_contact_function_read,
189 };
190
191 static int unload_module(void)
192 {
193         return ast_custom_function_unregister(&pjsip_contact_function);
194 }
195
196 static int load_module(void)
197 {
198         return ast_custom_function_register(&pjsip_contact_function);
199 }
200
201 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP contact",
202         .support_level = AST_MODULE_SUPPORT_CORE,
203         .load = load_module,
204         .unload = unload_module,
205         .requires = "res_pjsip",
206 );