Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
Data Structures | Functions | Variables
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.
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
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 = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .reload = reload_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, .requires = "res_pjsip,res_phoneprov", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_sorcerysorcery
 

Detailed Description

PJSIP Phoneprov Configuration Provider.

Author
George Joseph georg.nosp@m.e.jo.nosp@m.seph@.nosp@m.fair.nosp@m.view5.nosp@m..com

Definition in file res_pjsip_phoneprov_provider.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 426 of file res_pjsip_phoneprov_provider.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 426 of file res_pjsip_phoneprov_provider.c.

◆ aco_handler()

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 193 of file res_pjsip_phoneprov_provider.c.

194{
195 struct phoneprov *pp = obj;
196
197 return assign_and_insert(var->name, var->value, pp->vars);
198}
#define var
Definition ast_expr2f.c:605
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.
Structure for a phoneprov object.
struct varshead * vars

References assign_and_insert(), var, and phoneprov::vars.

Referenced by load_module().

◆ assign_and_insert()

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 169 of file res_pjsip_phoneprov_provider.c.

170{
171 struct ast_var_t *var;
172
173 if (ast_strlen_zero(name) || !vars) {
174 return -1;
175 }
176
177 /* Just ignore if the value is NULL or empty */
178 if (ast_strlen_zero(value)) {
179 return 0;
180 }
181
183 if (!var) {
184 ast_log(LOG_ERROR, "Could not allocate variable memory for variable.\n");
185 return -1;
186 }
188
189 return 0;
190}
#define ast_log
Definition astobj2.c:42
static void AST_VAR_LIST_INSERT_TAIL(struct varshead *head, struct ast_var_t *var)
Definition chanvars.h:51
#define ast_var_assign(name, value)
Definition chanvars.h:40
static const char name[]
Definition format_mp3.c:68
#define LOG_ERROR
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
int value
Definition syslog.c:37

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

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

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 426 of file res_pjsip_phoneprov_provider.c.

◆ fields_handler()

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

Converts the phoneprov varlist to an ast_variable list.

Definition at line 201 of file res_pjsip_phoneprov_provider.c.

202{
203 const struct phoneprov *pp = obj;
204 struct ast_var_t *pvar;
205 struct ast_variable *head = NULL;
206 struct ast_variable *tail = NULL;
207 struct ast_variable *var;
208
209 AST_VAR_LIST_TRAVERSE(pp->vars, pvar) {
210 var = ast_variable_new(pvar->name, pvar->value, "");
211 if (!var) {
213 return -1;
214 }
215 if (!head) {
216 head = var;
217 tail = var;
218 continue;
219 }
220 tail->next = var;
221 tail = var;
222 }
223
224 *fields = head;
225
226 return 0;
227}
#define AST_VAR_LIST_TRAVERSE(head, var)
Definition chanvars.h:49
#define ast_variable_new(name, value, filename)
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
#define NULL
Definition resample.c:96
char name[0]
Definition chanvars.h:31
char * value
Definition chanvars.h:30
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next

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().

◆ load_endpoint()

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

Definition at line 229 of file res_pjsip_phoneprov_provider.c.

231{
232 struct ast_sip_auth *auth;
233 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
234 RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
235 const char *auth_name;
236
237 *port_string = '\0';
238
239 /* We need to use res_pjsip's sorcery instance instead of our own to
240 * get endpoint and auth.
241 */
242 endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint",
243 endpoint_name);
244 if (!endpoint) {
245 ast_log(LOG_ERROR, "phoneprov %s contained invalid endpoint %s.\n", id,
246 endpoint_name);
247 return -1;
248 }
249
250 assign_and_insert("ENDPOINT_ID", endpoint_name, vars);
251 assign_and_insert("TRANSPORT_ID", endpoint->transport, vars);
252
253 if (endpoint->id.self.number.valid && !ast_strlen_zero(endpoint->id.self.number.str)) {
255 endpoint->id.self.number.str, vars);
256 }
257
258 if (endpoint->id.self.name.valid && !ast_strlen_zero(endpoint->id.self.name.str)) {
261 endpoint->id.self.name.str, vars);
262 }
263
264 transport = ast_sorcery_retrieve_by_id(sorcery, "transport",
265 endpoint->transport);
266 if (!transport) {
267 ast_log(LOG_ERROR, "Endpoint %s contained invalid transport %s.\n", endpoint_name,
268 endpoint->transport);
269 return -1;
270 }
271 snprintf(port_string, 6, "%d", pj_sockaddr_get_port(&transport->host));
272
273 if (!AST_VECTOR_SIZE(&endpoint->inbound_auths)) {
274 return 0;
275 }
276 auth_name = AST_VECTOR_GET(&endpoint->inbound_auths, 0);
277
278 auth = ast_sorcery_retrieve_by_id(sorcery, "auth", auth_name);
279 if (!auth) {
280 ast_log(LOG_ERROR, "phoneprov %s contained invalid auth %s.\n", id, auth_name);
281 return -1;
282 }
283
284 assign_and_insert("AUTH_ID", auth_name, vars);
286 auth->auth_user, vars);
288 auth->auth_pass, vars);
289 ao2_ref(auth, -1);
290
291 return 0;
292}
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
const char * ast_phoneprov_std_variable_lookup(enum ast_phoneprov_std_variables var)
Returns the string respresentation of a phoneprov standard variable.
@ AST_PHONEPROV_STD_CALLERID
Definition phoneprov.h:36
@ AST_PHONEPROV_STD_USERNAME
Definition phoneprov.h:32
@ AST_PHONEPROV_STD_DISPLAY_NAME
Definition phoneprov.h:33
@ AST_PHONEPROV_STD_SECRET
Definition phoneprov.h:34
static struct ast_sorcery * sorcery
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
const ast_string_field auth_user
Definition res_pjsip.h:681
const ast_string_field auth_pass
Definition res_pjsip.h:681
An entity with which Asterisk communicates.
Definition res_pjsip.h:1051
Transport to bind to.
Definition res_pjsip.h:219
#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:978
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition vector.h:620
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:691

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, RAII_VAR, and sorcery.

Referenced by users_apply_handler().

◆ load_module()

static int load_module ( void  )
static

Definition at line 374 of file res_pjsip_phoneprov_provider.c.

375{
377
378 ast_sorcery_apply_config(sorcery, "res_pjsip_phoneprov_provider");
379 ast_sorcery_apply_default(sorcery, "phoneprov", "config",
380 "pjsip.conf,criteria=type=phoneprov");
381
383 NULL);
384
385 ast_sorcery_object_field_register(sorcery, "phoneprov", "type", "", OPT_NOOP_T, 0,
386 0);
389
390 ast_sorcery_load_object(sorcery, "phoneprov");
391
393 ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
395 }
396
398}
#define AST_MODULE
@ OPT_NOOP_T
Type for a default handler that should do nothing.
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
int ast_phoneprov_provider_register(char *provider_name, ast_phoneprov_load_users_cb load_users)
Registers a config provider to phoneprov.
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
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 load_users(void)
Callback that loads the users from phoneprov sections.
static void * phoneprov_alloc(const char *name)
Allocator for phoneprov.
static int fields_handler(const void *obj, struct ast_variable **fields)
Converts the phoneprov varlist to an ast_variable list.
int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
Register a regex for multiple fields within an object.
Definition sorcery.c:1224
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition sorcery.h:837
void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to load persistent objects.
Definition sorcery.c:1457
#define ast_sorcery_apply_config(sorcery, name)
Definition sorcery.h:455
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition sorcery.h:955
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition sorcery.h:476

References aco_handler(), ast_log, AST_MODULE, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_phoneprov_provider_register(), ast_sip_get_sorcery(), ast_sorcery_apply_config, ast_sorcery_apply_default, ast_sorcery_load_object(), ast_sorcery_object_field_register, ast_sorcery_object_fields_register(), ast_sorcery_object_register, fields_handler(), load_users(), LOG_ERROR, NULL, OPT_NOOP_T, phoneprov_alloc(), and sorcery.

◆ load_users()

static int load_users ( void  )
static

Callback that loads the users from phoneprov sections.

Definition at line 349 of file res_pjsip_phoneprov_provider.c.

350{
351 struct ao2_container *users;
352 struct ao2_iterator i;
353 struct phoneprov *pp;
354
356
359 if (!users) {
360 return 0;
361 }
362
363 i = ao2_iterator_init(users, 0);
364 while ((pp = ao2_iterator_next(&i))) {
366 ao2_ref(pp, -1);
367 }
369 ao2_ref(users, -1);
370
371 return 0;
372}
#define ao2_iterator_next(iter)
Definition astobj2.h:1911
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
static void users_apply_handler(struct phoneprov *pp)
Callback that validates the phoneprov object.
@ AST_RETRIEVE_FLAG_MULTIPLE
Return all matching objects.
Definition sorcery.h:120
@ AST_RETRIEVE_FLAG_ALL
Perform no matching, return all objects.
Definition sorcery.h:123
void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to reload persistent objects.
Definition sorcery.c:1506
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition sorcery.c:1961
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition astobj2.h:1821
list of users found in the config file

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, sorcery, and users_apply_handler().

Referenced by load_module(), and reload_module().

◆ phoneprov_alloc()

static void * phoneprov_alloc ( const char *  name)
static

Allocator for phoneprov.

Definition at line 154 of file res_pjsip_phoneprov_provider.c.

155{
156 struct phoneprov *pp = ast_sorcery_generic_alloc(sizeof(*pp), phoneprov_destroy);
157
158 if (!pp || !(pp->vars = ast_var_list_create())) {
159 ast_log(LOG_ERROR, "Unable to allocate memory for phoneprov structure %s\n",
160 name);
161 ao2_cleanup(pp);
162 return NULL;
163 }
164
165 return pp;
166}
struct varshead * ast_var_list_create(void)
Definition chanvars.c:97
static void phoneprov_destroy(void *obj)
Destructor function for phoneprov.
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition sorcery.c:1792

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

Referenced by load_module().

◆ phoneprov_destroy()

static void phoneprov_destroy ( void *  obj)
static

Destructor function for phoneprov.

Definition at line 141 of file res_pjsip_phoneprov_provider.c.

142{
143 struct phoneprov *pp = obj;
144 char *mac = ast_var_find(pp->vars, "MAC");
145
146 if (mac) {
148 }
149
151}
void ast_var_list_destroy(struct varshead *head)
Definition chanvars.c:109
char * ast_var_find(const struct varshead *head, const char *name)
Definition chanvars.c:85
void ast_phoneprov_delete_extension(char *provider_name, char *macaddress)
Deletes an extension.

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

Referenced by phoneprov_alloc().

◆ reload_module()

static int reload_module ( void  )
static

Definition at line 407 of file res_pjsip_phoneprov_provider.c.

408{
410
412 ast_log(LOG_ERROR, "Unable to register pjsip phoneprov provider.\n");
414 }
415
416 return 0;
417}
void ast_phoneprov_provider_unregister(char *provider_name)
Unegisters a config provider from phoneprov and frees its resources.

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 400 of file res_pjsip_phoneprov_provider.c.

401{
403
404 return 0;
405}

References AST_MODULE, and ast_phoneprov_provider_unregister().

◆ users_apply_handler()

static void users_apply_handler ( struct phoneprov pp)
static

Callback that validates the phoneprov object.

Definition at line 295 of file res_pjsip_phoneprov_provider.c.

296{
297 const char *id = ast_sorcery_object_get_id(pp);
298 const char *endpoint_name;
299 char port_string[6];
300
301 if (!ast_var_find(pp->vars,
303 ast_log(LOG_ERROR, "phoneprov %s must contain a MAC entry.\n", id);
304 return;
305 }
306
307 if (!ast_var_find(pp->vars,
309 ast_log(LOG_ERROR, "phoneprov %s must contain a PROFILE entry.\n", id);
310 return;
311 }
312
313 endpoint_name = ast_var_find(pp->vars, "endpoint");
314 if (endpoint_name) {
315 if (load_endpoint(id, endpoint_name, pp->vars, port_string)) {
316 return;
317 }
318 }
319
320 if (!ast_var_find(pp->vars,
324 pp->vars);
325 }
326
327 if (!ast_var_find(pp->vars,
330 id, pp->vars);
331 }
332
333 if (!ast_var_find(pp->vars,
335 assign_and_insert("SERVER_PORT", S_OR(port_string, "5060"), pp->vars);
336 }
337
338 if (!ast_var_find(pp->vars,
340 ast_log(LOG_ERROR, "phoneprov %s didn't contain a PROFILE entry.\n", id);
341 }
342
344
345 return;
346}
int ast_phoneprov_add_extension(char *provider_name, struct varshead *vars)
Adds an extension.
@ AST_PHONEPROV_STD_SERVER_PORT
Definition phoneprov.h:41
@ AST_PHONEPROV_STD_MAC
Definition phoneprov.h:30
@ AST_PHONEPROV_STD_LABEL
Definition phoneprov.h:35
@ AST_PHONEPROV_STD_PROFILE
Definition phoneprov.h:31
static int load_endpoint(const char *id, const char *endpoint_name, struct varshead *vars, char *port_string)
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition sorcery.c:2381
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition strings.h:80

References assign_and_insert(), ast_log, AST_MODULE, 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().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Phoneprov Provider" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .reload = reload_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, .requires = "res_pjsip,res_phoneprov", }
static

Definition at line 426 of file res_pjsip_phoneprov_provider.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 426 of file res_pjsip_phoneprov_provider.c.

◆ sorcery

struct ast_sorcery* sorcery
static

Definition at line 132 of file res_pjsip_phoneprov_provider.c.

Referenced by load_endpoint(), load_module(), and load_users().