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)
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_LOAD_ORDER , .description = "Get information about a PJSIP contact" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static 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

static void __reg_module ( void   )  [static]

Definition at line 209 of file func_pjsip_contact.c.

static void __unreg_module ( void   )  [static]

Definition at line 209 of file func_pjsip_contact.c.

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

Definition at line 71 of file func_pjsip_contact.c.

References ast_sorcery_object_get_id(), CMP_MATCH, and CMP_STOP.

Referenced by pjsip_contact_function_read().

00072 {
00073    const char *id = arg;
00074 
00075    if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
00076       return CMP_MATCH | CMP_STOP;
00077    }
00078 
00079    return 0;
00080 }

static int load_module ( void   )  [static]

Definition at line 204 of file func_pjsip_contact.c.

References ast_custom_function_register.

00205 {
00206    return ast_custom_function_register(&pjsip_contact_function);
00207 }

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 82 of file func_pjsip_contact.c.

References ao2_callback, ao2_cleanup, args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, AST_LOG_WARNING, ast_sip_get_sorcery(), ast_sorcery_object_get_id(), 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, contact_function_get_permanent(), CONTACT_STATUS, ast_variable::name, ast_variable::next, NULL, RAII_VAR, UNAVAILABLE, and ast_variable::value.

00084 {
00085    struct ast_sorcery *pjsip_sorcery;
00086    char *parsed_data = ast_strdupa(data);
00087    char *contact_name;
00088    RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
00089    RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
00090    int res = 0;
00091 
00092    AST_DECLARE_APP_ARGS(args,
00093       AST_APP_ARG(contact_name);
00094       AST_APP_ARG(field_name);
00095    );
00096 
00097    /* Check for zero arguments */
00098    if (ast_strlen_zero(parsed_data)) {
00099       ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
00100       return -1;
00101    }
00102 
00103    AST_STANDARD_APP_ARGS(args, parsed_data);
00104 
00105    if (ast_strlen_zero(args.contact_name)) {
00106       ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
00107       return -1;
00108    }
00109 
00110    if (ast_strlen_zero(args.field_name)) {
00111       ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
00112       return -1;
00113    }
00114 
00115    pjsip_sorcery = ast_sip_get_sorcery();
00116    if (!pjsip_sorcery) {
00117       ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
00118       return -1;
00119    }
00120 
00121    /* Determine if this is a permanent contact or a normal contact */
00122    if ((contact_name = strstr(args.contact_name, "@@"))) {
00123       size_t aor_name_len = contact_name - args.contact_name;
00124       char aor_name[aor_name_len + 1];
00125       RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
00126 
00127       /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
00128       strncpy(aor_name, args.contact_name, aor_name_len);
00129       aor_name[aor_name_len] = '\0';
00130 
00131       aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
00132       if (!aor_obj) {
00133          ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
00134          return -1;
00135       }
00136 
00137       contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
00138    } else {
00139       contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
00140    }
00141 
00142    if (!contact_obj) {
00143       ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
00144       return -1;
00145    }
00146 
00147    contact_status = ast_sorcery_retrieve_by_id(pjsip_sorcery, CONTACT_STATUS, ast_sorcery_object_get_id(contact_obj));
00148 
00149    if (!strcmp(args.field_name, "status")) {
00150       if (!contact_status) {
00151          ast_str_set(buf, len, "%s", "Unknown");
00152       } else if (contact_status->status == UNAVAILABLE) {
00153          ast_str_set(buf, len, "%s", "Unreachable");
00154       } else if (contact_status->status == AVAILABLE) {
00155          ast_str_set(buf, len, "%s", "Reachable");
00156       }
00157    } else if (!strcmp(args.field_name, "rtt")) {
00158       if (!contact_status) {
00159          ast_str_set(buf, len, "%s", "N/A");
00160       } else {
00161          ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
00162       }
00163    } else {
00164       struct ast_variable *change_set;
00165       struct ast_variable *it_change_set;
00166 
00167       change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
00168 
00169       if (!change_set) {
00170          ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
00171          return -1;
00172       }
00173 
00174       for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
00175          if (!strcmp(it_change_set->name, args.field_name)) {
00176             ast_str_set(buf, len, "%s", it_change_set->value);
00177             break;
00178          }
00179       }
00180 
00181       if (!it_change_set) {
00182          ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
00183 
00184          res = 1;
00185       }
00186 
00187       ast_variables_destroy(change_set);
00188    }
00189 
00190    return res;
00191 }

static int unload_module ( void   )  [static]

Definition at line 199 of file func_pjsip_contact.c.

References ast_custom_function_unregister().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Get information about a PJSIP contact" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, } [static]

Definition at line 209 of file func_pjsip_contact.c.

Definition at line 209 of file func_pjsip_contact.c.

Initial value:

 {
   .name = "PJSIP_CONTACT",
   .read2 = pjsip_contact_function_read,
}

Definition at line 194 of file func_pjsip_contact.c.


Generated on Thu Apr 16 06:31:22 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6