Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
provision.c File Reference

IAX Provisioning Protocol. More...

#include "asterisk.h"
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include "asterisk/config.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/md5.h"
#include "asterisk/astdb.h"
#include "asterisk/utils.h"
#include "asterisk/acl.h"
#include "asterisk/format_cache.h"
#include "asterisk/format_compatibility.h"
#include "include/iax2.h"
#include "include/provision.h"
#include "include/parser.h"
Include dependency graph for provision.c:

Go to the source code of this file.

Data Structures

struct  iax_flag
 
struct  iax_template
 
struct  templates
 

Functions

static int iax_process_template (struct ast_config *cfg, char *s, char *def)
 
char * iax_prov_complete_template (const char *line, const char *word, int pos, int state)
 
char * iax_provflags2str (char *buf, int buflen, unsigned int flags)
 
int iax_provision_build (struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
 
static void iax_provision_free_templates (int dead)
 
static int iax_provision_init (void)
 
int iax_provision_reload (int reload)
 
int iax_provision_unload (void)
 
int iax_provision_version (unsigned int *version, const char *template, int force)
 
static const char * iax_server (unsigned int addr)
 
static char * iax_show_provisioning (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static unsigned int iax_str2flags (const char *buf)
 
static void iax_template_copy (struct iax_template *dst, struct iax_template *src)
 
static struct iax_templateiax_template_find (const char *s, int allowdead)
 
static int iax_template_parse (struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
 
static const char * ifthere (const char *s)
 
static unsigned int prov_ver_calc (struct iax_ie_data *provdata)
 

Variables

static struct ast_cli_entry cli_iax2_provision []
 
static struct iax_flag iax_flags []
 
static int provinit = 0
 
static ast_mutex_t provlock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 
static struct templates templates = { .first = NULL, .last = NULL, }
 

Detailed Description

IAX Provisioning Protocol.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file provision.c.

Function Documentation

◆ iax_process_template()

static int iax_process_template ( struct ast_config cfg,
char *  s,
char *  def 
)
static

Definition at line 387 of file provision.c.

References ast_calloc, ast_copy_string(), AST_LIST_INSERT_HEAD, ast_log, ast_mutex_lock, ast_mutex_unlock, iax_template::dead, iax_template_find(), iax_template_parse(), iax_template::list, LOG_WARNING, iax_template::name, and provlock.

Referenced by iax_provision_reload().

388 {
389  /* Find an already existing one if there */
390  struct iax_template *cur;
391  int mallocd = 0;
392 
393  cur = iax_template_find(s, 1 /* allow dead */);
394  if (!cur) {
395  mallocd = 1;
396  cur = ast_calloc(1, sizeof(*cur));
397  if (!cur) {
398  ast_log(LOG_WARNING, "Out of memory!\n");
399  return -1;
400  }
401  /* Initialize entry */
402  ast_copy_string(cur->name, s, sizeof(cur->name));
403  cur->dead = 1;
404  }
405  if (!iax_template_parse(cur, cfg, s, def))
406  cur->dead = 0;
407 
408  /* Link if we're mallocd */
409  if (mallocd) {
413  }
414  return 0;
415 }
char name[80]
Definition: provision.c:57
#define LOG_WARNING
Definition: logger.h:274
struct iax_template::@157 list
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_log
Definition: astobj2.c:42
static struct iax_template * iax_template_find(const char *s, int allowdead)
Definition: provision.c:163
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg, const char *s, const char *def)
Definition: provision.c:282
static ast_mutex_t provlock
Definition: provision.c:74
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ iax_prov_complete_template()

char* iax_prov_complete_template ( const char *  line,
const char *  word,
int  pos,
int  state 
)

Definition at line 179 of file provision.c.

References AST_LIST_TRAVERSE, ast_mutex_lock, ast_mutex_unlock, ast_strdup, c, iax_template::list, iax_template::name, NULL, and provlock.

Referenced by handle_cli_iax2_provision(), and iax_show_provisioning().

180 {
181  struct iax_template *c;
182  int which=0;
183  char *ret = NULL;
184  int wordlen = strlen(word);
185 
186  if (pos == 3) {
189  if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
190  ret = ast_strdup(c->name);
191  break;
192  }
193  }
195  }
196  return ret;
197 }
char name[80]
Definition: provision.c:57
struct iax_template::@157 list
#define ast_mutex_lock(a)
Definition: lock.h:187
static struct test_val c
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
static ast_mutex_t provlock
Definition: provision.c:74
short word
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ iax_provflags2str()

char* iax_provflags2str ( char *  buf,
int  buflen,
unsigned int  flags 
)

Definition at line 90 of file provision.c.

References ARRAY_LEN, ast_strlen_zero, buf, iax_flags, iax_template::name, NULL, and value.

Referenced by dump_prov_flags(), and iax_show_provisioning().

91 {
92  int x;
93 
94  if (!buf || buflen < 1)
95  return NULL;
96 
97  buf[0] = '\0';
98 
99  for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
100  if (flags & iax_flags[x].value){
101  strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
102  strncat(buf, ",", buflen - strlen(buf) - 1);
103  }
104  }
105 
106  if (!ast_strlen_zero(buf))
107  buf[strlen(buf) - 1] = '\0';
108  else
109  strncpy(buf, "none", buflen - 1);
110 
111  return buf;
112 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
#define ast_strlen_zero(foo)
Definition: strings.h:52
unsigned int flags
Definition: provision.c:66
static const char name[]
Definition: cdr_mysql.c:74
static struct iax_flag iax_flags[]

◆ iax_provision_build()

int iax_provision_build ( struct iax_ie_data provdata,
unsigned int *  signature,
const char *  template,
int  force 
)

Definition at line 209 of file provision.c.

References iax_template::altserver, ast_db_put(), ast_mutex_lock, ast_mutex_unlock, iax_template::flags, iax_template::format, iax_ie_append_byte(), iax_ie_append_int(), iax_ie_append_short(), iax_ie_append_str(), iax_template_find(), iax_template::lang, iax_template::pass, iax_template::port, PROV_IE_ALTSERVER, PROV_IE_FLAGS, PROV_IE_FORMAT, PROV_IE_LANG, PROV_IE_PASS, PROV_IE_PORTNO, PROV_IE_PROVVER, PROV_IE_SERVERIP, PROV_IE_SERVERPORT, PROV_IE_TOS, PROV_IE_USER, prov_ver_calc(), provlock, iax_template::server, iax_template::serverport, tmp(), iax_template::tos, and iax_template::user.

Referenced by iax2_provision(), and iax_provision_version().

210 {
211  struct iax_template *cur;
212  unsigned int sig;
213  char tmp[40];
214  memset(provdata, 0, sizeof(*provdata));
216  cur = iax_template_find(template, 1);
217  /* If no match, try searching for '*' */
218  if (!cur)
219  cur = iax_template_find("*", 1);
220  if (cur) {
221  /* found it -- add information elements as appropriate */
222  if (force || strlen(cur->user))
223  iax_ie_append_str(provdata, PROV_IE_USER, cur->user);
224  if (force || strlen(cur->pass))
225  iax_ie_append_str(provdata, PROV_IE_PASS, cur->pass);
226  if (force || strlen(cur->lang))
227  iax_ie_append_str(provdata, PROV_IE_LANG, cur->lang);
228  if (force || cur->port)
229  iax_ie_append_short(provdata, PROV_IE_PORTNO, cur->port);
230  if (force || cur->server)
231  iax_ie_append_int(provdata, PROV_IE_SERVERIP, cur->server);
232  if (force || cur->serverport)
234  if (force || cur->altserver)
236  if (force || cur->flags)
237  iax_ie_append_int(provdata, PROV_IE_FLAGS, cur->flags);
238  if (force || cur->format)
239  iax_ie_append_int(provdata, PROV_IE_FORMAT, cur->format);
240  if (force || cur->tos)
241  iax_ie_append_byte(provdata, PROV_IE_TOS, cur->tos);
242 
243  /* Calculate checksum of message so far */
244  sig = prov_ver_calc(provdata);
245  if (signature)
246  *signature = sig;
247  /* Store signature */
248  iax_ie_append_int(provdata, PROV_IE_PROVVER, sig);
249  /* Cache signature for later verification so we need not recalculate all this */
250  snprintf(tmp, sizeof(tmp), "v0x%08x", sig);
251  ast_db_put("iax/provisioning/cache", template, tmp);
252  } else
253  ast_db_put("iax/provisioning/cache", template, "u");
255  return cur ? 0 : -1;
256 }
int iax_ie_append_short(struct iax_ie_data *ied, unsigned char ie, unsigned short value)
Definition: parser.c:762
#define PROV_IE_FLAGS
Definition: provision.h:32
int iax_ie_append_int(struct iax_ie_data *ied, unsigned char ie, unsigned int value)
Definition: parser.c:755
static int tmp()
Definition: bt_open.c:389
char lang[10]
Definition: provision.c:61
#define ast_mutex_lock(a)
Definition: lock.h:187
unsigned int altserver
Definition: provision.c:65
#define PROV_IE_USER
Definition: provision.h:26
unsigned short port
Definition: provision.c:62
static struct iax_template * iax_template_find(const char *s, int allowdead)
Definition: provision.c:163
#define PROV_IE_FORMAT
Definition: provision.h:33
unsigned short serverport
Definition: provision.c:64
static unsigned int prov_ver_calc(struct iax_ie_data *provdata)
Definition: provision.c:199
#define PROV_IE_SERVERPORT
Definition: provision.h:36
char user[20]
Definition: provision.c:59
iax2_format format
Definition: provision.c:67
char pass[20]
Definition: provision.c:60
unsigned int flags
Definition: provision.c:66
unsigned int server
Definition: provision.c:63
#define PROV_IE_PORTNO
Definition: provision.h:25
int iax_ie_append_byte(struct iax_ie_data *ied, unsigned char ie, unsigned char dat)
Definition: parser.c:774
unsigned int tos
Definition: provision.c:68
int iax_ie_append_str(struct iax_ie_data *ied, unsigned char ie, const char *str)
Definition: parser.c:769
static ast_mutex_t provlock
Definition: provision.c:74
#define PROV_IE_SERVERIP
Definition: provision.h:35
#define PROV_IE_ALTSERVER
Definition: provision.h:39
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
#define PROV_IE_TOS
Definition: provision.h:31
#define PROV_IE_PASS
Definition: provision.h:27
#define PROV_IE_LANG
Definition: provision.h:30
#define ast_mutex_unlock(a)
Definition: lock.h:188
#define PROV_IE_PROVVER
Definition: provision.h:38

◆ iax_provision_free_templates()

static void iax_provision_free_templates ( int  dead)
static

Definition at line 504 of file provision.c.

References ast_free, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_mutex_lock, ast_mutex_unlock, iax_template::dead, iax_template::list, and provlock.

Referenced by iax_provision_reload(), and iax_provision_unload().

505 {
506  struct iax_template *cur;
507 
508  /* Drop dead or not (depending on dead) entries while locked */
511  if ((dead && cur->dead) || !dead) {
513  ast_free(cur);
514  }
515  }
518 }
struct iax_template::@157 list
#define ast_mutex_lock(a)
Definition: lock.h:187
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
#define ast_free(a)
Definition: astmm.h:182
static ast_mutex_t provlock
Definition: provision.c:74
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ iax_provision_init()

static int iax_provision_init ( void  )
static

Definition at line 497 of file provision.c.

References ast_cli_register_multiple, and provinit.

Referenced by iax_provision_reload().

498 {
500  provinit = 1;
501  return 0;
502 }
descriptor for a cli entry.
Definition: cli.h:171
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static int provinit
Definition: provision.c:53
static struct ast_cli_entry cli_iax2_provision[]
Definition: provision.c:493

◆ iax_provision_reload()

int iax_provision_reload ( int  reload)

Definition at line 529 of file provision.c.

References ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_db_deltree(), AST_LIST_TRAVERSE, ast_log, ast_verb, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, iax_template::dead, iax_process_template(), iax_provision_free_templates(), iax_provision_init(), iax_template::list, LOG_NOTICE, NULL, and provinit.

Referenced by load_module(), and reload_config().

530 {
531  struct ast_config *cfg;
532  struct iax_template *cur;
533  char *cat;
534  int found = 0;
535  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
536  if (!provinit)
538 
539  cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
540  if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
541  /* Mark all as dead. No need for locking */
542  AST_LIST_TRAVERSE(&templates, cur, list) {
543  cur->dead = 1;
544  }
545 
546  /* Load as appropriate */
547  cat = ast_category_browse(cfg, NULL);
548  while(cat) {
549  if (strcasecmp(cat, "general")) {
550  iax_process_template(cfg, cat, found ? "default" : NULL);
551  found++;
552  ast_verb(3, "Loaded provisioning template '%s'\n", cat);
553  }
554  cat = ast_category_browse(cfg, cat);
555  }
556  ast_config_destroy(cfg);
557  } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
558  return 0;
559  else
560  ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
561 
562  iax_provision_free_templates(1 /* remove only marked as dead */);
563 
564  /* Purge cached signature DB entries */
565  ast_db_deltree("iax/provisioning/cache", NULL);
566  return 0;
567 }
static int iax_process_template(struct ast_config *cfg, char *s, char *def)
Definition: provision.c:387
static void iax_provision_free_templates(int dead)
Definition: provision.c:504
#define CONFIG_STATUS_FILEINVALID
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3154
static int iax_provision_init(void)
Definition: provision.c:497
static int provinit
Definition: provision.c:53
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3328
#define NULL
Definition: resample.c:96
#define ast_verb(level,...)
Definition: logger.h:463
#define ast_log
Definition: astobj2.c:42
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define CONFIG_STATUS_FILEUNCHANGED
#define LOG_NOTICE
Definition: logger.h:263
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
static int reload(void)
Definition: cdr_mysql.c:741
Structure used to handle boolean flags.
Definition: utils.h:199
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
Definition: main/db.c:457

◆ iax_provision_unload()

int iax_provision_unload ( void  )

Definition at line 520 of file provision.c.

References ast_cli_unregister_multiple(), iax_provision_free_templates(), and provinit.

Referenced by __unload_module().

521 {
522  provinit = 0;
524  iax_provision_free_templates(0 /* Remove all templates. */);
525 
526  return 0;
527 }
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
descriptor for a cli entry.
Definition: cli.h:171
static void iax_provision_free_templates(int dead)
Definition: provision.c:504
static int provinit
Definition: provision.c:53
static struct ast_cli_entry cli_iax2_provision[]
Definition: provision.c:493

◆ iax_provision_version()

int iax_provision_version ( unsigned int *  version,
const char *  template,
int  force 
)

Definition at line 258 of file provision.c.

References ast_db_get(), ast_debug, ast_log, ast_mutex_lock, ast_mutex_unlock, iax_provision_build(), LOG_ERROR, provlock, and tmp().

Referenced by check_provisioning().

259 {
260  char tmp[80] = "";
261  struct iax_ie_data ied;
262  int ret=0;
263  memset(&ied, 0, sizeof(ied));
264 
266  if (ast_db_get("iax/provisioning/cache", template, tmp, sizeof(tmp))) {
267  ast_log(LOG_ERROR, "ast_db_get failed to retrieve iax/provisioning/cache/%s\n", template);
268  }
269  if (sscanf(tmp, "v%30x", version) != 1) {
270  if (strcmp(tmp, "u")) {
271  ret = iax_provision_build(&ied, version, template, force);
272  if (ret)
273  ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
274  } else
275  ret = -1;
276  } else
277  ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
279  return ret;
280 }
static int tmp()
Definition: bt_open.c:389
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
static ast_mutex_t provlock
Definition: provision.c:74
int iax_provision_build(struct iax_ie_data *provdata, unsigned int *signature, const char *template, int force)
Definition: provision.c:209
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ iax_server()

static const char* iax_server ( unsigned int  addr)
static

Definition at line 425 of file provision.c.

References ast_inet_ntoa().

Referenced by iax_show_provisioning().

426 {
427  struct in_addr ia;
428 
429  if (!addr)
430  return "<unspecified>";
431 
432  ia.s_addr = htonl(addr);
433 
434  return ast_inet_ntoa(ia);
435 }
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
Definition: main/utils.c:782

◆ iax_show_provisioning()

static char* iax_show_provisioning ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 438 of file provision.c.

References iax_template::altserver, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_copy_string(), AST_LIST_TRAVERSE, ast_mutex_lock, ast_mutex_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, iax_template::flags, iax_template::format, iax2_getformatname(), iax_prov_complete_template(), iax_provflags2str(), iax_server(), ifthere(), iax_template::lang, ast_cli_args::line, iax_template::list, ast_cli_args::n, iax_template::name, NULL, iax_template::pass, iax_template::port, ast_cli_args::pos, provlock, iax_template::server, iax_template::serverport, iax_template::src, iax_template::tos, ast_cli_entry::usage, iax_template::user, and ast_cli_args::word.

439 {
440  struct iax_template *cur;
441  char server[INET_ADDRSTRLEN];
442  char alternate[INET_ADDRSTRLEN];
443  char flags[80]; /* Has to be big enough for 'flags' too */
444  int found = 0;
445 
446  switch (cmd) {
447  case CLI_INIT:
448  e->command = "iax2 show provisioning";
449  e->usage =
450  "Usage: iax2 show provisioning [template]\n"
451  " Lists all known IAX provisioning templates or a\n"
452  " specific one if specified.\n";
453  return NULL;
454  case CLI_GENERATE:
455  return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
456  }
457 
458  if ((a->argc != 3) && (a->argc != 4))
459  return CLI_SHOWUSAGE;
460 
463  if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name))) {
464  if (found)
465  ast_cli(a->fd, "\n");
466  ast_copy_string(server, iax_server(cur->server), sizeof(server));
467  ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
468  ast_cli(a->fd, "== %s ==\n", cur->name);
469  ast_cli(a->fd, "Base Templ: %s\n", strlen(cur->src) ? cur->src : "<none>");
470  ast_cli(a->fd, "Username: %s\n", ifthere(cur->user));
471  ast_cli(a->fd, "Secret: %s\n", ifthere(cur->pass));
472  ast_cli(a->fd, "Language: %s\n", ifthere(cur->lang));
473  ast_cli(a->fd, "Bind Port: %d\n", cur->port);
474  ast_cli(a->fd, "Server: %s\n", server);
475  ast_cli(a->fd, "Server Port: %d\n", cur->serverport);
476  ast_cli(a->fd, "Alternate: %s\n", alternate);
477  ast_cli(a->fd, "Flags: %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
478  ast_cli(a->fd, "Format: %s\n", iax2_getformatname(cur->format));
479  ast_cli(a->fd, "TOS: 0x%x\n", cur->tos);
480  found++;
481  }
482  }
484  if (!found) {
485  if (a->argc == 3)
486  ast_cli(a->fd, "No provisioning templates found\n");
487  else
488  ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
489  }
490  return CLI_SUCCESS;
491 }
char name[80]
Definition: provision.c:57
const char * iax2_getformatname(iax2_format format)
iax2 wrapper function for ast_getformatname
Definition: chan_iax2.c:1903
const int argc
Definition: cli.h:160
static const char * iax_server(unsigned int addr)
Definition: provision.c:425
struct iax_template::@157 list
Definition: cli.h:152
char lang[10]
Definition: provision.c:61
#define ast_mutex_lock(a)
Definition: lock.h:187
unsigned int altserver
Definition: provision.c:65
#define NULL
Definition: resample.c:96
static const char * ifthere(const char *s)
Definition: provision.c:417
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
char * iax_provflags2str(char *buf, int buflen, unsigned int flags)
Definition: provision.c:90
const char * line
Definition: cli.h:162
unsigned short port
Definition: provision.c:62
char * iax_prov_complete_template(const char *line, const char *word, int pos, int state)
Definition: provision.c:179
const int fd
Definition: cli.h:159
const int n
Definition: cli.h:165
unsigned short serverport
Definition: provision.c:64
const char *const * argv
Definition: cli.h:161
char user[20]
Definition: provision.c:59
#define CLI_SHOWUSAGE
Definition: cli.h:45
iax2_format format
Definition: provision.c:67
char pass[20]
Definition: provision.c:60
unsigned int flags
Definition: provision.c:66
unsigned int server
Definition: provision.c:63
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
char * command
Definition: cli.h:186
const char * word
Definition: cli.h:163
unsigned int tos
Definition: provision.c:68
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
static ast_mutex_t provlock
Definition: provision.c:74
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
const int pos
Definition: cli.h:164
char src[80]
Definition: provision.c:58
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ iax_str2flags()

static unsigned int iax_str2flags ( const char *  buf)
static

Definition at line 114 of file provision.c.

References ARRAY_LEN, buf, iax_template::flags, iax_flags, len(), iax_template::name, and iax_flag::value.

Referenced by iax_template_parse().

115 {
116  int x;
117  int len;
118  unsigned int flags = 0;
119  char *e;
120  while(buf && *buf) {
121  e = strchr(buf, ',');
122  if (e)
123  len = e - buf;
124  else
125  len = 0;
126  for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
127  if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
128  (!len && !strcasecmp(iax_flags[x].name, buf))) {
129  flags |= iax_flags[x].value;
130  break;
131  }
132  }
133  if (e) {
134  buf = e + 1;
135  while(*buf && (*buf < 33))
136  buf++;
137  } else
138  break;
139  }
140  return flags;
141 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
unsigned int flags
Definition: provision.c:66
static const char name[]
Definition: cdr_mysql.c:74
static struct iax_flag iax_flags[]
int value
Definition: provision.c:78

◆ iax_template_copy()

static void iax_template_copy ( struct iax_template dst,
struct iax_template src 
)
static

Definition at line 143 of file provision.c.

References iax_template::altserver, ast_copy_string(), iax_template::dead, iax_template::flags, iax_template::format, iax_template::lang, iax_template::name, iax_template::pass, iax_template::port, iax_template::server, iax_template::src, iax_template::tos, and iax_template::user.

Referenced by iax_template_parse().

144 {
145  if (!dst || !src) {
146  return;
147  }
148 
149  dst->dead = src->dead;
150  ast_copy_string(dst->name, src->name, sizeof(dst->name));
151  ast_copy_string(dst->src, src->src, sizeof(dst->src));
152  ast_copy_string(dst->user, src->user, sizeof(dst->user));
153  ast_copy_string(dst->pass, src->pass, sizeof(dst->pass));
154  ast_copy_string(dst->lang, src->lang, sizeof(dst->lang));
155  dst->port = src->port;
156  dst->server = src->server;
157  dst->altserver = src->altserver;
158  dst->flags = src->flags;
159  dst->format = src->format;
160  dst->tos = src->tos;
161 }
char name[80]
Definition: provision.c:57
char lang[10]
Definition: provision.c:61
unsigned int altserver
Definition: provision.c:65
unsigned short port
Definition: provision.c:62
char user[20]
Definition: provision.c:59
iax2_format format
Definition: provision.c:67
char pass[20]
Definition: provision.c:60
unsigned int flags
Definition: provision.c:66
unsigned int server
Definition: provision.c:63
unsigned int tos
Definition: provision.c:68
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
char src[80]
Definition: provision.c:58

◆ iax_template_find()

static struct iax_template* iax_template_find ( const char *  s,
int  allowdead 
)
static

Definition at line 163 of file provision.c.

References AST_LIST_TRAVERSE, iax_template::dead, iax_template::list, iax_template::name, and NULL.

Referenced by iax_process_template(), iax_provision_build(), and iax_template_parse().

164 {
165  struct iax_template *cur;
166 
168  if (!strcasecmp(s, cur->name)) {
169  if (!allowdead && cur->dead) {
170  cur = NULL;
171  }
172  break;
173  }
174  }
175 
176  return cur;
177 }
char name[80]
Definition: provision.c:57
struct iax_template::@157 list
#define NULL
Definition: resample.c:96
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490

◆ iax_template_parse()

static int iax_template_parse ( struct iax_template cur,
struct ast_config cfg,
const char *  s,
const char *  def 
)
static

Definition at line 282 of file provision.c.

References iax_template::altserver, ao2_ref, ast_copy_string(), ast_format_cache_get, ast_format_compatibility_format2bitfield(), ast_gethostbyname(), ast_log, ast_mutex_lock, ast_mutex_unlock, ast_str2tos(), ast_variable_browse(), ast_variable_retrieve(), iax_template::dead, iax_template::flags, iax_template::format, hp, IAX_DEFAULT_PORTNO, iax_str2flags(), iax_template_copy(), iax_template_find(), iax_template::lang, ast_variable::lineno, LOG_WARNING, iax_template::name, ast_variable::name, ast_variable::next, NULL, iax_template::pass, iax_template::port, provlock, iax_template::server, iax_template::serverport, iax_template::src, tmp(), iax_template::tos, iax_template::user, and ast_variable::value.

Referenced by iax_process_template().

283 {
284  struct ast_variable *v;
285  int foundportno = 0;
286  int foundserverportno = 0;
287  int x;
288  struct in_addr ia;
289  struct hostent *hp;
290  struct ast_hostent h;
291  struct iax_template *src, tmp;
292  const char *t;
293  if (def) {
294  t = ast_variable_retrieve(cfg, s ,"template");
295  src = NULL;
296  if (t && strlen(t)) {
297  src = iax_template_find(t, 0);
298  if (!src)
299  ast_log(LOG_WARNING, "Unable to find base template '%s' for creating '%s'. Trying '%s'\n", t, s, def);
300  else
301  def = t;
302  }
303  if (!src) {
304  src = iax_template_find(def, 0);
305  if (!src)
306  ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
307  }
308  if (!src)
309  return -1;
311  /* Backup old data */
312  iax_template_copy(&tmp, cur);
313  /* Restore from src */
314  iax_template_copy(cur, src);
315  /* Restore important headers */
316  memcpy(cur->name, tmp.name, sizeof(cur->name));
317  cur->dead = tmp.dead;
319  }
320  if (def)
321  ast_copy_string(cur->src, def, sizeof(cur->src));
322  else
323  cur->src[0] = '\0';
324  v = ast_variable_browse(cfg, s);
325  while(v) {
326  if (!strcasecmp(v->name, "port") || !strcasecmp(v->name, "serverport")) {
327  if ((sscanf(v->value, "%5d", &x) == 1) && (x > 0) && (x < 65535)) {
328  if (!strcasecmp(v->name, "port")) {
329  cur->port = x;
330  foundportno = 1;
331  } else {
332  cur->serverport = x;
333  foundserverportno = 1;
334  }
335  } else
336  ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
337  } else if (!strcasecmp(v->name, "server") || !strcasecmp(v->name, "altserver")) {
338  hp = ast_gethostbyname(v->value, &h);
339  if (hp) {
340  memcpy(&ia, hp->h_addr, sizeof(ia));
341  if (!strcasecmp(v->name, "server"))
342  cur->server = ntohl(ia.s_addr);
343  else
344  cur->altserver = ntohl(ia.s_addr);
345  } else
346  ast_log(LOG_WARNING, "Ignoring invalid %s '%s' for '%s' at line %d\n", v->name, v->value, s, v->lineno);
347  } else if (!strcasecmp(v->name, "codec")) {
348  struct ast_format *tmpfmt;
349  if ((tmpfmt = ast_format_cache_get(v->value))) {
351  ao2_ref(tmpfmt, -1);
352  } else
353  ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
354  } else if (!strcasecmp(v->name, "tos")) {
355  if (ast_str2tos(v->value, &cur->tos))
356  ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
357  } else if (!strcasecmp(v->name, "user")) {
358  ast_copy_string(cur->user, v->value, sizeof(cur->user));
359  if (strcmp(cur->user, v->value))
360  ast_log(LOG_WARNING, "Truncating username from '%s' to '%s' for '%s' at line %d\n", v->value, cur->user, s, v->lineno);
361  } else if (!strcasecmp(v->name, "pass")) {
362  ast_copy_string(cur->pass, v->value, sizeof(cur->pass));
363  if (strcmp(cur->pass, v->value))
364  ast_log(LOG_WARNING, "Truncating password from '%s' to '%s' for '%s' at line %d\n", v->value, cur->pass, s, v->lineno);
365  } else if (!strcasecmp(v->name, "language")) {
366  ast_copy_string(cur->lang, v->value, sizeof(cur->lang));
367  if (strcmp(cur->lang, v->value))
368  ast_log(LOG_WARNING, "Truncating language from '%s' to '%s' for '%s' at line %d\n", v->value, cur->lang, s, v->lineno);
369  } else if (!strcasecmp(v->name, "flags")) {
370  cur->flags = iax_str2flags(v->value);
371  } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '+')) {
372  cur->flags |= iax_str2flags(v->value);
373  } else if (!strncasecmp(v->name, "flags", 5) && strchr(v->name, '-')) {
374  cur->flags &= ~iax_str2flags(v->value);
375  } else if (strcasecmp(v->name, "template")) {
376  ast_log(LOG_WARNING, "Unknown keyword '%s' in definition of '%s' at line %d\n", v->name, s, v->lineno);
377  }
378  v = v->next;
379  }
380  if (!foundportno)
381  cur->port = IAX_DEFAULT_PORTNO;
382  if (!foundserverportno)
384  return 0;
385 }
struct ast_variable * next
static void iax_template_copy(struct iax_template *dst, struct iax_template *src)
Definition: provision.c:143
uint64_t ast_format_compatibility_format2bitfield(const struct ast_format *format)
Convert a format structure to its respective bitfield.
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
char name[80]
Definition: provision.c:57
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
Structure for variables, used for configurations and for channel variables.
#define IAX_DEFAULT_PORTNO
Definition: iax2.h:128
char lang[10]
Definition: provision.c:61
Definition of a media format.
Definition: format.c:43
#define ast_mutex_lock(a)
Definition: lock.h:187
unsigned int altserver
Definition: provision.c:65
int ast_str2tos(const char *value, unsigned int *tos)
Convert a string to the appropriate TOS value.
Definition: acl.c:967
#define NULL
Definition: resample.c:96
#define ast_format_cache_get(name)
Definition: format_cache.h:286
unsigned short port
Definition: provision.c:62
#define ast_log
Definition: astobj2.c:42
static struct iax_template * iax_template_find(const char *s, int allowdead)
Definition: provision.c:163
unsigned short serverport
Definition: provision.c:64
#define ao2_ref(o, delta)
Definition: astobj2.h:464
char user[20]
Definition: provision.c:59
iax2_format format
Definition: provision.c:67
char pass[20]
Definition: provision.c:60
unsigned int flags
Definition: provision.c:66
unsigned int server
Definition: provision.c:63
static unsigned int iax_str2flags(const char *buf)
Definition: provision.c:114
unsigned int tos
Definition: provision.c:68
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:694
struct hostent * ast_gethostbyname(const char *host, struct ast_hostent *hp)
Thread-safe gethostbyname function to use in Asterisk.
Definition: main/utils.c:197
static ast_mutex_t provlock
Definition: provision.c:74
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
char src[80]
Definition: provision.c:58
static struct hostent * hp
Definition: chan_skinny.c:1236
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ ifthere()

static const char* ifthere ( const char *  s)
static

Definition at line 417 of file provision.c.

Referenced by iax_show_provisioning().

418 {
419  if (strlen(s))
420  return s;
421  else
422  return "<unspecified>";
423 }

◆ prov_ver_calc()

static unsigned int prov_ver_calc ( struct iax_ie_data provdata)
static

Definition at line 199 of file provision.c.

References iax_ie_data::buf, MD5Final(), MD5Init(), MD5Update(), iax_ie_data::pos, and tmp().

Referenced by iax_provision_build().

200 {
201  struct MD5Context md5;
202  unsigned int tmp[4];
203  MD5Init(&md5);
204  MD5Update(&md5, provdata->buf, provdata->pos);
205  MD5Final((unsigned char *)tmp, &md5);
206  return tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3];
207 }
static int tmp()
Definition: bt_open.c:389
void MD5Final(unsigned char digest[16], struct MD5Context *context)
Definition: md5.c:120
void MD5Init(struct MD5Context *context)
Definition: md5.c:57
void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len)
Definition: md5.c:72
int pos
Definition: parser.h:150
static int md5(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_md5.c:52
unsigned char buf[1024]
Definition: parser.h:149
Definition: md5.h:26

Variable Documentation

◆ cli_iax2_provision

struct ast_cli_entry cli_iax2_provision[]
static
Initial value:
= {
{ .handler = iax_show_provisioning , .summary = "Display iax provisioning" ,},
}
static char * iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: provision.c:438

Definition at line 493 of file provision.c.

◆ iax_flags

struct iax_flag iax_flags[]
static

Referenced by iax_provflags2str(), and iax_str2flags().

◆ provinit

int provinit = 0
static

Definition at line 53 of file provision.c.

Referenced by iax_provision_init(), iax_provision_reload(), and iax_provision_unload().

◆ provlock

ast_mutex_t provlock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static

◆ templates

struct templates templates = { .first = NULL, .last = NULL, }
static