res_pjsip_phoneprov_provider.c File Reference

PJSIP Phoneprov Configuration Provider. More...

#include "asterisk.h"
#include <pjsip.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/phoneprov.h"
#include "res_pjsip/include/res_pjsip_private.h"

Include dependency graph for res_pjsip_phoneprov_provider.c:

Go to the source code of this file.

Data Structures

struct  phoneprov
 Structure for a phoneprov object. More...

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int aco_handler (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Adds a config name/value pair to the phoneprov object.
static int assign_and_insert (const char *name, const char *value, struct varshead *vars)
 Helper that creates an ast_var_t and inserts it into the list.
static int fields_handler (const void *obj, struct ast_variable **fields)
 Converts the phoneprov varlist to an ast_variable list.
static int load_endpoint (const char *id, const char *endpoint_name, struct varshead *vars, char *port_string)
static int load_module (void)
static int load_users (void)
 Callback that loads the users from phoneprov sections.
static void * phoneprov_alloc (const char *name)
 Allocator for phoneprov.
static void phoneprov_destroy (void *obj)
 Destructor function for phoneprov.
static int reload_module (void)
static int unload_module (void)
static void users_apply_handler (struct phoneprov *pp)
 Callback that validates the phoneprov object.

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Phoneprov Provider" , .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, .reload = reload_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_sorcerysorcery


Detailed Description

PJSIP Phoneprov Configuration Provider.

Author:
George Joseph <george.joseph@fairview5.com>

Definition in file res_pjsip_phoneprov_provider.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 420 of file res_pjsip_phoneprov_provider.c.

static void __unreg_module ( void   )  [static]

Definition at line 420 of file res_pjsip_phoneprov_provider.c.

static int aco_handler ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
) [static]

Adds a config name/value pair to the phoneprov object.

Definition at line 187 of file res_pjsip_phoneprov_provider.c.

References assign_and_insert(), ast_variable::name, ast_variable::value, and phoneprov::vars.

Referenced by load_module().

00188 {
00189    struct phoneprov *pp = obj;
00190 
00191    return assign_and_insert(var->name, var->value, pp->vars);
00192 }

static int assign_and_insert ( const char *  name,
const char *  value,
struct varshead vars 
) [static]

Helper that creates an ast_var_t and inserts it into the list.

Definition at line 163 of file res_pjsip_phoneprov_provider.c.

References ast_log, ast_strlen_zero, ast_var_assign(), AST_VAR_LIST_INSERT_TAIL(), LOG_ERROR, and var.

Referenced by aco_handler(), load_endpoint(), and users_apply_handler().

00164 {
00165    struct ast_var_t *var;
00166 
00167    if (ast_strlen_zero(name) || !vars) {
00168       return -1;
00169    }
00170 
00171    /* Just ignore if the value is NULL or empty */
00172    if (ast_strlen_zero(value)) {
00173       return 0;
00174    }
00175 
00176    var = ast_var_assign(name, value);
00177    if (!var) {
00178       ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
00179       return -1;
00180    }
00181    AST_VAR_LIST_INSERT_TAIL(vars, var);
00182 
00183    return 0;
00184 }

static int fields_handler ( const void *  obj,
struct ast_variable **  fields 
) [static]

Converts the phoneprov varlist to an ast_variable list.

Definition at line 195 of file res_pjsip_phoneprov_provider.c.

References AST_VAR_LIST_TRAVERSE, ast_variable_new(), ast_variables_destroy(), ast_var_t::name, ast_variable::next, NULL, ast_var_t::value, var, and phoneprov::vars.

Referenced by load_module().

00196 {
00197    const struct phoneprov *pp = obj;
00198    struct ast_var_t *pvar;
00199    struct ast_variable *head = NULL;
00200    struct ast_variable *tail = NULL;
00201    struct ast_variable *var;
00202 
00203    AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
00204       var = ast_variable_new(pvar->name, pvar->value, "");
00205       if (!var) {
00206          ast_variables_destroy(head);
00207          return -1;
00208       }
00209       if (!head) {
00210          head = var;
00211          tail = var;
00212          continue;
00213       }
00214       tail->next = var;
00215       tail = var;
00216    }
00217 
00218    *fields = head;
00219 
00220    return 0;
00221 }

static int load_endpoint ( const char *  id,
const char *  endpoint_name,
struct varshead vars,
char *  port_string 
) [static]

Definition at line 223 of file res_pjsip_phoneprov_provider.c.

References ao2_cleanup, ao2_ref, assign_and_insert(), ast_log, AST_PHONEPROV_STD_CALLERID, AST_PHONEPROV_STD_DISPLAY_NAME, AST_PHONEPROV_STD_SECRET, AST_PHONEPROV_STD_USERNAME, ast_phoneprov_std_variable_lookup(), ast_sorcery_retrieve_by_id(), ast_strlen_zero, AST_VECTOR_GET, AST_VECTOR_SIZE, ast_sip_auth::auth_pass, ast_sip_auth::auth_user, LOG_ERROR, NULL, and RAII_VAR.

Referenced by users_apply_handler().

00225 {
00226    struct ast_sip_auth *auth;
00227    RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
00228    RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
00229    const char *auth_name;
00230 
00231    *port_string = '\0';
00232 
00233    /* We need to use res_pjsip's sorcery instance instead of our own to
00234     * get endpoint and auth.
00235     */
00236    endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint",
00237       endpoint_name);
00238    if (!endpoint) {
00239       ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
00240          endpoint_name);
00241       return -1;
00242    }
00243 
00244    assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
00245    assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
00246 
00247    if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
00248       assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_CALLERID),
00249          endpoint->id.self.number.str, vars);
00250    }
00251 
00252    if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
00253       assign_and_insert(
00254          ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_DISPLAY_NAME),
00255          endpoint->id.self.name.str, vars);
00256    }
00257 
00258    transport = ast_sorcery_retrieve_by_id(sorcery, "transport",
00259       endpoint->transport);
00260    if (!transport) {
00261       ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
00262          endpoint->transport);
00263       return -1;
00264    }
00265    snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
00266 
00267    if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
00268       return 0;
00269    }
00270    auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);
00271 
00272    auth = ast_sorcery_retrieve_by_id(sorcery, "auth", auth_name);
00273    if (!auth) {
00274       ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
00275       return -1;
00276    }
00277 
00278    assign_and_insert("AUTH_ID", auth_name, vars);
00279    assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME),
00280       auth->auth_user, vars);
00281    assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SECRET),
00282       auth->auth_pass, vars);
00283    ao2_ref(auth, -1);
00284 
00285    return 0;
00286 }

static int load_module ( void   )  [static]

static int load_users ( void   )  [static]

Callback that loads the users from phoneprov sections.

Definition at line 343 of file res_pjsip_phoneprov_provider.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_reload_object(), ast_sorcery_retrieve_by_fields(), NULL, and users_apply_handler().

00344 {
00345    struct ao2_container *users;
00346    struct ao2_iterator i;
00347    struct phoneprov *pp;
00348 
00349    ast_sorcery_reload_object(sorcery, "phoneprov");
00350 
00351    users = ast_sorcery_retrieve_by_fields(sorcery, "phoneprov",
00352       AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
00353    if (!users) {
00354       return 0;
00355    }
00356 
00357    i = ao2_iterator_init(users, 0);
00358    while ((pp = ao2_iterator_next(&i))) {
00359       users_apply_handler(pp);
00360       ao2_ref(pp, -1);
00361    }
00362    ao2_iterator_destroy(&i);
00363    ao2_ref(users, -1);
00364 
00365    return 0;
00366 }

static void* phoneprov_alloc ( const char *  name  )  [static]

Allocator for phoneprov.

Definition at line 148 of file res_pjsip_phoneprov_provider.c.

References ao2_cleanup, ast_log, ast_sorcery_generic_alloc(), ast_var_list_create(), LOG_ERROR, NULL, phoneprov_destroy(), and phoneprov::vars.

Referenced by load_module().

00149 {
00150    struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
00151 
00152    if (!pp || !(pp->vars = ast_var_list_create())) {
00153       ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
00154          name);
00155       ao2_cleanup(pp);
00156       return NULL;
00157    }
00158 
00159    return pp;
00160 }

static void phoneprov_destroy ( void *  obj  )  [static]

Destructor function for phoneprov.

Definition at line 135 of file res_pjsip_phoneprov_provider.c.

References ast_phoneprov_delete_extension(), ast_var_find(), ast_var_list_destroy(), and phoneprov::vars.

Referenced by phoneprov_alloc().

00136 {
00137    struct phoneprov *pp = obj;
00138    char *mac = ast_var_find(pp->vars, "MAC");
00139 
00140    if (mac) {
00141       ast_phoneprov_delete_extension(AST_MODULE, mac);
00142    }
00143 
00144    ast_var_list_destroy(pp->vars);
00145 }

static int reload_module ( void   )  [static]

Definition at line 403 of file res_pjsip_phoneprov_provider.c.

References ast_log, AST_MODULE_LOAD_DECLINE, ast_phoneprov_provider_register(), ast_phoneprov_provider_unregister(), load_users(), and LOG_ERROR.

00404 {
00405    ast_phoneprov_provider_unregister(AST_MODULE);
00406 
00407    if (ast_phoneprov_provider_register(AST_MODULE, load_users)) {
00408       ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
00409       return AST_MODULE_LOAD_DECLINE;
00410    }
00411 
00412    return 0;
00413 }

static int unload_module ( void   )  [static]

Definition at line 396 of file res_pjsip_phoneprov_provider.c.

References ast_phoneprov_provider_unregister().

00397 {
00398    ast_phoneprov_provider_unregister(AST_MODULE);
00399 
00400    return 0;
00401 }

static void users_apply_handler ( struct phoneprov pp  )  [static]

Callback that validates the phoneprov object.

Definition at line 289 of file res_pjsip_phoneprov_provider.c.

References assign_and_insert(), ast_log, ast_phoneprov_add_extension(), AST_PHONEPROV_STD_LABEL, AST_PHONEPROV_STD_MAC, AST_PHONEPROV_STD_PROFILE, AST_PHONEPROV_STD_SERVER_PORT, AST_PHONEPROV_STD_USERNAME, ast_phoneprov_std_variable_lookup(), ast_sorcery_object_get_id(), ast_var_find(), load_endpoint(), LOG_ERROR, S_OR, and phoneprov::vars.

Referenced by load_users().

00290 {
00291    const char *id = ast_sorcery_object_get_id(pp);
00292    const char *endpoint_name;
00293    char port_string[6];
00294 
00295    if (!ast_var_find(pp->vars,
00296       ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_MAC))) {
00297       ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
00298       return;
00299    }
00300 
00301    if (!ast_var_find(pp->vars,
00302       ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
00303       ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
00304       return;
00305    }
00306 
00307    endpoint_name = ast_var_find(pp->vars, "endpoint");
00308    if (endpoint_name) {
00309       if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
00310          return;
00311       }
00312    }
00313 
00314    if (!ast_var_find(pp->vars,
00315       ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME))) {
00316       assign_and_insert(
00317          ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_USERNAME), id,
00318          pp->vars);
00319    }
00320 
00321    if (!ast_var_find(pp->vars,
00322       ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL))) {
00323       assign_and_insert(ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_LABEL),
00324          id, pp->vars);
00325    }
00326 
00327    if (!ast_var_find(pp->vars,
00328       ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_SERVER_PORT))) {
00329       assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
00330    }
00331 
00332    if (!ast_var_find(pp->vars,
00333       ast_phoneprov_std_variable_lookup(AST_PHONEPROV_STD_PROFILE))) {
00334       ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
00335    }
00336 
00337    ast_phoneprov_add_extension(AST_MODULE, pp->vars);
00338 
00339    return;
00340 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Phoneprov Provider" , .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, .reload = reload_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, } [static]

Definition at line 420 of file res_pjsip_phoneprov_provider.c.

Definition at line 420 of file res_pjsip_phoneprov_provider.c.

struct ast_sorcery* sorcery [static]


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