Asterisk - The Open Source Telephony Project  18.5.0
func_pjsip_contact.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 <[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  <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  <enum name="rtt">
64  <para>The RTT of the last qualify</para>
65  </enum>
66  <enum name="status">
67  <para>Status of the contact</para>
68  </enum>
69  </enumlist>
70  </parameter>
71  </syntax>
72  </function>
73 ***/
74 
75 static int contact_function_get_permanent(void *obj, void *arg, int flags)
76 {
77  const char *id = arg;
78 
79  if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
80  return CMP_MATCH | CMP_STOP;
81  }
82 
83  return 0;
84 }
85 
86 static int pjsip_contact_function_read(struct ast_channel *chan,
87  const char *cmd, char *data, struct ast_str **buf, ssize_t len)
88 {
89  struct ast_sorcery *pjsip_sorcery;
90  char *parsed_data = ast_strdupa(data);
91  char *contact_name;
92  RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
93  RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
94  int res = 0;
95 
97  AST_APP_ARG(contact_name);
98  AST_APP_ARG(field_name);
99  );
100 
101  /* Check for zero arguments */
102  if (ast_strlen_zero(parsed_data)) {
103  ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
104  return -1;
105  }
106 
107  AST_STANDARD_APP_ARGS(args, parsed_data);
108 
109  if (ast_strlen_zero(args.contact_name)) {
110  ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
111  return -1;
112  }
113 
114  if (ast_strlen_zero(args.field_name)) {
115  ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
116  return -1;
117  }
118 
119  pjsip_sorcery = ast_sip_get_sorcery();
120  if (!pjsip_sorcery) {
121  ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
122  return -1;
123  }
124 
125  /* Determine if this is a permanent contact or a normal contact */
126  if ((contact_name = strstr(args.contact_name, "@@"))) {
127  size_t aor_name_len = contact_name - args.contact_name;
128  char aor_name[aor_name_len + 1];
129  RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
130 
131  /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
132  strncpy(aor_name, args.contact_name, aor_name_len);
133  aor_name[aor_name_len] = '\0';
134 
135  aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
136  if (!aor_obj) {
137  ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
138  return -1;
139  }
140 
141  contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
142  } else {
143  contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
144  }
145 
146  if (!contact_obj) {
147  ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
148  return -1;
149  }
150 
151  contact_status = ast_sip_get_contact_status(contact_obj);
152 
153  if (!strcmp(args.field_name, "status")) {
154  ast_str_set(buf, len, "%s", ast_sip_get_contact_status_label(contact_status ? contact_status->status : UNKNOWN));
155  } else if (!strcmp(args.field_name, "rtt")) {
156  if (!contact_status || contact_status->status != AVAILABLE) {
157  ast_str_set(buf, len, "%s", "N/A");
158  } else {
159  ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
160  }
161  } else {
162  struct ast_variable *change_set;
163  struct ast_variable *it_change_set;
164 
165  change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
166 
167  if (!change_set) {
168  ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
169  return -1;
170  }
171 
172  for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
173  if (!strcmp(it_change_set->name, args.field_name)) {
174  ast_str_set(buf, len, "%s", it_change_set->value);
175  break;
176  }
177  }
178 
179  if (!it_change_set) {
180  ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
181 
182  res = 1;
183  }
184 
185  ast_variables_destroy(change_set);
186  }
187 
188  return res;
189 }
190 
191 
193  .name = "PJSIP_CONTACT",
195 };
196 
197 static int unload_module(void)
198 {
199  return ast_custom_function_unregister(&pjsip_contact_function);
200 }
201 
202 static int load_module(void)
203 {
204  return ast_custom_function_register(&pjsip_contact_function);
205 }
206 
207 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get information about a PJSIP contact",
208  .support_level = AST_MODULE_SUPPORT_CORE,
209  .load = load_module,
210  .unload = unload_module,
211  .requires = "res_pjsip",
212 );
const char * name
Definition: pbx.h:119
struct ast_variable * next
Main Channel structure associated with a channel.
A contact&#39;s status.
Definition: res_pjsip.h:341
Asterisk main include file. File version handling, generic pbx functions.
A SIP address of record.
Definition: res_pjsip.h:361
struct ast_sip_contact_status * ast_sip_get_contact_status(const struct ast_sip_contact *contact)
Retrieve the current status for a contact.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
static int contact_function_get_permanent(void *obj, void *arg, int flags)
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
Structure for variables, used for configurations and for channel variables.
#define AST_LOG_WARNING
Definition: logger.h:279
Full structure for sorcery.
Definition: sorcery.c:230
const char * ast_sip_get_contact_status_label(const enum ast_sip_contact_status_type status)
translate ast_sip_contact_status_type to character string.
const char * args
#define NULL
Definition: resample.c:96
static int pjsip_contact_function_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define ast_strlen_zero(foo)
Definition: strings.h:52
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:1065
#define ast_log
Definition: astobj2.c:42
#define AST_LOG_ERROR
Definition: logger.h:290
#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:911
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
Core PBX routines and definitions.
static int load_module(void)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Contact associated with an address of record.
Definition: res_pjsip.h:281
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1136
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;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:1508
static int unload_module(void)
Sorcery Data Access Layer API.
static struct ast_custom_function pjsip_contact_function
#define AST_APP_ARG(name)
Define an application argument.