Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Functions | Variables
func_pjsip_contact.c File Reference

Get information about a PJSIP contact. More...

#include "asterisk.h"
#include <pjsip.h>
#include <pjlib.h>
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/res_pjsip.h"
Include dependency graph for func_pjsip_contact.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int contact_function_get_permanent (void *obj, void *arg, int flags)
 
static int load_module (void)
 
static int pjsip_contact_function_read (struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Get information about a PJSIP contact" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_pjsip", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function pjsip_contact_function
 

Detailed Description

Get information about a PJSIP contact.

Author
Joshua Colp <jcolp@digium.com> 

Definition in file func_pjsip_contact.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 226 of file func_pjsip_contact.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 226 of file func_pjsip_contact.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 226 of file func_pjsip_contact.c.

◆ contact_function_get_permanent()

static int contact_function_get_permanent ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 89 of file func_pjsip_contact.c.

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}
@ CMP_MATCH
Definition astobj2.h:1027
@ CMP_STOP
Definition astobj2.h:1028
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition sorcery.c:2381

References ast_sorcery_object_get_id(), CMP_MATCH, and CMP_STOP.

Referenced by pjsip_contact_function_read().

◆ load_module()

static int load_module ( void  )
static

Definition at line 216 of file func_pjsip_contact.c.

217{
219}
static struct ast_custom_function pjsip_contact_function
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562

References ast_custom_function_register, and pjsip_contact_function.

◆ pjsip_contact_function_read()

static int pjsip_contact_function_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
struct ast_str **  buf,
ssize_t  len 
)
static

Definition at line 100 of file func_pjsip_contact.c.

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
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}
#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_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition astobj2.h:1693
#define ao2_cleanup(obj)
Definition astobj2.h:1934
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static int contact_function_get_permanent(void *obj, void *arg, int flags)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#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:1260
#define AST_LOG_WARNING
#define AST_LOG_ERROR
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
struct ast_sip_contact_status * ast_sip_get_contact_status(const struct ast_sip_contact *contact)
Retrieve the current status for a contact.
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.
@ AVAILABLE
Definition res_pjsip.h:438
@ UNKNOWN
Definition res_pjsip.h:440
static struct @519 args
#define NULL
Definition resample.c:96
#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:1917
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
A SIP address of record.
Definition res_pjsip.h:478
A contact's status.
Definition res_pjsip.h:451
Contact associated with an address of record.
Definition res_pjsip.h:390
Full structure for sorcery.
Definition sorcery.c:231
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
#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:981

References ao2_callback, ao2_cleanup, args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, AST_LOG_WARNING, ast_sip_get_contact_status(), ast_sip_get_contact_status_label(), ast_sip_get_sorcery(), ast_sorcery_objectset_create, ast_sorcery_retrieve_by_id(), AST_STANDARD_APP_ARGS, ast_str_set(), ast_strdupa, ast_strlen_zero(), ast_variables_destroy(), AVAILABLE, buf, contact_function_get_permanent(), len(), ast_variable::name, ast_variable::next, NULL, RAII_VAR, UNKNOWN, and ast_variable::value.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 211 of file func_pjsip_contact.c.

212{
214}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), and pjsip_contact_function.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Get information about a PJSIP contact" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_pjsip", }
static

Definition at line 226 of file func_pjsip_contact.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 226 of file func_pjsip_contact.c.

◆ pjsip_contact_function

struct ast_custom_function pjsip_contact_function
static
Initial value:
= {
.name = "PJSIP_CONTACT",
}
static int pjsip_contact_function_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)

Definition at line 206 of file func_pjsip_contact.c.

206 {
207 .name = "PJSIP_CONTACT",
209};

Referenced by load_module(), and unload_module().