Wed Aug 7 17:16:07 2019

Asterisk developer's documentation


misdn_config.c File Reference

chan_misdn configuration management More...

#include "asterisk.h"
#include "chan_misdn_config.h"
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/lock.h"
#include "asterisk/pbx.h"
#include "asterisk/strings.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Data Structures

union  misdn_cfg_pt
struct  misdn_cfg_spec
struct  msn_list

Defines

#define CLI_ERROR(name, value, section)
#define GEN_CFG   1
#define NO_DEFAULT   "<>"
#define NONE   0
#define NUM_GEN_ELEMENTS   (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))
#define NUM_PORT_ELEMENTS   (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))
#define PORT_CFG   2

Enumerations

enum  misdn_cfg_type {
  MISDN_CTYPE_STR, MISDN_CTYPE_INT, MISDN_CTYPE_BOOL, MISDN_CTYPE_BOOLINT,
  MISDN_CTYPE_MSNLIST, MISDN_CTYPE_ASTGROUP
}

Functions

static void _build_general_config (struct ast_variable *v)
static void _build_port_config (struct ast_variable *v, char *cat)
static int _enum_array_map (void)
static void _fill_defaults (void)
static void _free_general_cfg (void)
static void _free_msn_list (struct msn_list *iter)
static void _free_port_cfg (void)
static int _parse (union misdn_cfg_pt *dest, const char *value, enum misdn_cfg_type type, int boolint_def)
static int get_cfg_position (const char *name, int type)
void misdn_cfg_destroy (void)
void misdn_cfg_get (int port, enum misdn_cfg_elements elem, void *buf, int bufsize)
void misdn_cfg_get_config_string (int port, enum misdn_cfg_elements elem, char *buf, int bufsize)
void misdn_cfg_get_desc (enum misdn_cfg_elements elem, void *buf, int bufsize, void *buf_default, int bufsize_default)
enum misdn_cfg_elements misdn_cfg_get_elem (const char *name)
void misdn_cfg_get_name (enum misdn_cfg_elements elem, void *buf, int bufsize)
int misdn_cfg_get_next_port (int port)
int misdn_cfg_get_next_port_spin (int port)
void misdn_cfg_get_ports_string (char *ports)
 Generate a comma separated list of all active ports.
int misdn_cfg_init (int this_max_ports, int reload)
int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth)
int misdn_cfg_is_msn_valid (int port, char *msn)
int misdn_cfg_is_port_valid (int port)
static void misdn_cfg_lock (void)
void misdn_cfg_reload (void)
static void misdn_cfg_unlock (void)
void misdn_cfg_update_ptp (void)
struct ast_jb_confmisdn_get_global_jbconf ()

Variables

static ast_mutex_t config_mutex
static struct ast_jb_conf default_jbconf
static struct misdn_cfg_spec gen_spec []
static union misdn_cfg_ptgeneral_cfg
static struct ast_jb_conf global_jbconf
static int * map
static int max_ports
static union misdn_cfg_pt ** port_cfg
static struct misdn_cfg_spec port_spec []
static const char ports_description []
static int * ptp

Detailed Description

chan_misdn configuration management

Author:
Christian Richter <crich@beronet.com>

Definition in file misdn_config.c.


Define Documentation

#define CLI_ERROR ( name,
value,
section   ) 
Value:
({ \
   ast_log(LOG_WARNING, "misdn.conf: \"%s=%s\" (section: %s) invalid or out of range. " \
      "Please edit your misdn.conf and then do a \"misdn reload\".\n", name, value, section); \
})

Definition at line 438 of file misdn_config.c.

Referenced by _build_general_config(), and _build_port_config().

#define GEN_CFG   1

Definition at line 49 of file misdn_config.c.

Referenced by _build_general_config(), get_cfg_position(), and misdn_cfg_get_elem().

#define NO_DEFAULT   "<>"

Definition at line 46 of file misdn_config.c.

Referenced by _fill_defaults(), and misdn_cfg_get_desc().

#define NONE   0
#define NUM_GEN_ELEMENTS   (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))
#define NUM_PORT_ELEMENTS   (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))
#define PORT_CFG   2

Definition at line 50 of file misdn_config.c.

Referenced by _build_port_config(), get_cfg_position(), and misdn_cfg_get_elem().


Enumeration Type Documentation

Enumerator:
MISDN_CTYPE_STR 
MISDN_CTYPE_INT 
MISDN_CTYPE_BOOL 
MISDN_CTYPE_BOOLINT 
MISDN_CTYPE_MSNLIST 
MISDN_CTYPE_ASTGROUP 

Definition at line 67 of file misdn_config.c.


Function Documentation

static void _build_general_config ( struct ast_variable v  )  [static]

Definition at line 992 of file misdn_config.c.

References _parse(), ast_jb_read_conf(), misdn_cfg_spec::boolint_def, CLI_ERROR, GEN_CFG, get_cfg_position(), global_jbconf, ast_variable::name, ast_variable::next, misdn_cfg_spec::type, and ast_variable::value.

Referenced by misdn_cfg_init().

00993 {
00994    int pos;
00995 
00996    for (; v; v = v->next) {
00997       if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
00998          continue;
00999       if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) ||
01000          (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
01001          CLI_ERROR(v->name, v->value, "general");
01002    }
01003 }

static void _build_port_config ( struct ast_variable v,
char *  cat 
) [static]

Definition at line 1005 of file misdn_config.c.

References _parse(), ast_strdupa, misdn_cfg_spec::boolint_def, BUFFERSIZE, CLI_ERROR, get_cfg_position(), ast_variable::name, ast_variable::next, PORT_CFG, misdn_cfg_spec::type, type, and ast_variable::value.

Referenced by misdn_cfg_init().

01006 {
01007    int pos, i;
01008    union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
01009    int cfg_for_ports[max_ports + 1];
01010 
01011    if (!v || !cat)
01012       return;
01013 
01014    memset(cfg_tmp, 0, sizeof(cfg_tmp));
01015    memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
01016 
01017    if (!strcasecmp(cat, "default")) {
01018       cfg_for_ports[0] = 1;
01019    }
01020 
01021    if (((pos = get_cfg_position("name", PORT_CFG)) < 0) ||
01022       (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
01023       CLI_ERROR(v->name, v->value, cat);
01024       return;
01025    }
01026 
01027    for (; v; v = v->next) {
01028       if (!strcasecmp(v->name, "ports")) {
01029          char *token, *tmp = ast_strdupa(v->value);
01030          char ptpbuf[BUFFERSIZE] = "";
01031          int start, end;
01032          for (token = strsep(&tmp, ","); token; token = strsep(&tmp, ","), *ptpbuf = 0) {
01033             if (!*token)
01034                continue;
01035             if (sscanf(token, "%30d-%30d%511s", &start, &end, ptpbuf) >= 2) {
01036                for (; start <= end; start++) {
01037                   if (start <= max_ports && start > 0) {
01038                      cfg_for_ports[start] = 1;
01039                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
01040                   } else
01041                      CLI_ERROR(v->name, v->value, cat);
01042                }
01043             } else {
01044                if (sscanf(token, "%30d%511s", &start, ptpbuf)) {
01045                   if (start <= max_ports && start > 0) {
01046                      cfg_for_ports[start] = 1;
01047                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
01048                   } else
01049                      CLI_ERROR(v->name, v->value, cat);
01050                } else
01051                   CLI_ERROR(v->name, v->value, cat);
01052             }
01053          }
01054       } else {
01055          if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) ||
01056             (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
01057             CLI_ERROR(v->name, v->value, cat);
01058       }
01059    }
01060 
01061    for (i = 0; i < (max_ports + 1); ++i) {
01062       if (i > 0 && cfg_for_ports[0]) {
01063          /* default category, will populate the port_cfg with additional port
01064          categories in subsequent calls to this function */
01065          memset(cfg_tmp, 0, sizeof(cfg_tmp));
01066       }
01067       if (cfg_for_ports[i]) {
01068          memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
01069       }
01070    }
01071 }

static int _enum_array_map ( void   )  [static]

Definition at line 443 of file misdn_config.c.

References ast_log(), LOG_WARNING, MISDN_CFG_FIRST, MISDN_CFG_LAST, MISDN_CFG_PTP, MISDN_GEN_FIRST, MISDN_GEN_LAST, NUM_GEN_ELEMENTS, and NUM_PORT_ELEMENTS.

Referenced by misdn_cfg_init().

00444 {
00445    int i, j, ok;
00446 
00447    for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
00448       if (i == MISDN_CFG_PTP)
00449          continue;
00450       ok = 0;
00451       for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
00452          if (port_spec[j].elem == i) {
00453             map[i] = j;
00454             ok = 1;
00455             break;
00456          }
00457       }
00458       if (!ok) {
00459          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (port section) has no corresponding element in the config struct!\n", i);
00460          return -1;
00461       }
00462    }
00463    for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
00464       ok = 0;
00465       for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
00466          if (gen_spec[j].elem == i) {
00467             map[i] = j;
00468             ok = 1;
00469             break;
00470          }
00471       }
00472       if (!ok) {
00473          ast_log(LOG_WARNING, "Enum element %d in misdn_cfg_elements (general section) has no corresponding element in the config struct!\n", i);
00474          return -1;
00475       }
00476    }
00477    return 0;
00478 }

static void _fill_defaults ( void   )  [static]

Definition at line 1128 of file misdn_config.c.

References _parse(), misdn_cfg_pt::any, NO_DEFAULT, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, and type.

Referenced by misdn_cfg_init().

01129 {
01130    int i;
01131 
01132    for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
01133       if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
01134          _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
01135    }
01136    for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
01137       if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
01138          _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
01139    }
01140 }

static void _free_general_cfg ( void   )  [static]

Definition at line 553 of file misdn_config.c.

References misdn_cfg_pt::any, ast_free, and NUM_GEN_ELEMENTS.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00554 {
00555    int i;
00556 
00557    for (i = 0; i < NUM_GEN_ELEMENTS; i++)
00558       if (general_cfg[i].any)
00559          ast_free(general_cfg[i].any);
00560 }

static void _free_msn_list ( struct msn_list iter  )  [static]

Definition at line 511 of file misdn_config.c.

References ast_free, msn_list::msn, and msn_list::next.

Referenced by _free_port_cfg().

00512 {
00513    if (iter->next)
00514       _free_msn_list(iter->next);
00515    if (iter->msn)
00516       ast_free(iter->msn);
00517    ast_free(iter);
00518 }

static void _free_port_cfg ( void   )  [static]

Definition at line 520 of file misdn_config.c.

References _free_msn_list(), misdn_cfg_pt::any, ast_free, MISDN_CFG_GROUPNAME, MISDN_CTYPE_MSNLIST, misdn_cfg_pt::ml, NUM_PORT_ELEMENTS, str, and type.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00521 {
00522    int i, j;
00523    int gn = map[MISDN_CFG_GROUPNAME];
00524    union misdn_cfg_pt* free_list[max_ports + 2];
00525 
00526    memset(free_list, 0, sizeof(free_list));
00527    free_list[0] = port_cfg[0];
00528    for (i = 1; i <= max_ports; ++i) {
00529       if (port_cfg[i][gn].str) {
00530          /* we always have a groupname in the non-default case, so this is fine */
00531          for (j = 1; j <= max_ports; ++j) {
00532             if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
00533                break;
00534             else if (!free_list[j]) {
00535                free_list[j] = port_cfg[i];
00536                break;
00537             }
00538          }
00539       }
00540    }
00541    for (j = 0; free_list[j]; ++j) {
00542       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00543          if (free_list[j][i].any) {
00544             if (port_spec[i].type == MISDN_CTYPE_MSNLIST)
00545                _free_msn_list(free_list[j][i].ml);
00546             else
00547                ast_free(free_list[j][i].any);
00548          }
00549       }
00550    }
00551 }

static int _parse ( union misdn_cfg_pt dest,
const char *  value,
enum misdn_cfg_type  type,
int  boolint_def 
) [static]

Definition at line 915 of file misdn_config.c.

References ast_calloc, ast_free, ast_get_group(), ast_malloc, ast_strdupa, ast_true(), misdn_cfg_pt::grp, len(), MISDN_CTYPE_ASTGROUP, MISDN_CTYPE_BOOL, MISDN_CTYPE_BOOLINT, MISDN_CTYPE_INT, MISDN_CTYPE_MSNLIST, MISDN_CTYPE_STR, misdn_cfg_pt::ml, msn_list::msn, msn_list::next, misdn_cfg_pt::num, and misdn_cfg_pt::str.

Referenced by _build_general_config(), _build_port_config(), and _fill_defaults().

00916 {
00917    int re = 0;
00918    int len, tmp;
00919    char *valtmp;
00920    char *tmp2 = ast_strdupa(value);
00921 
00922    switch (type) {
00923    case MISDN_CTYPE_STR:
00924       if (dest->str) {
00925          ast_free(dest->str);
00926       }
00927       if ((len = strlen(value))) {
00928          dest->str = ast_malloc((len + 1) * sizeof(char));
00929          strncpy(dest->str, value, len);
00930          dest->str[len] = 0;
00931       } else {
00932          dest->str = ast_malloc(sizeof(char));
00933          dest->str[0] = 0;
00934       }
00935       break;
00936    case MISDN_CTYPE_INT:
00937    {
00938       int res;
00939 
00940       if (strchr(value,'x')) {
00941          res = sscanf(value, "%30x", &tmp);
00942       } else {
00943          res = sscanf(value, "%30d", &tmp);
00944       }
00945       if (res) {
00946          if (!dest->num) {
00947             dest->num = ast_malloc(sizeof(int));
00948          }
00949          memcpy(dest->num, &tmp, sizeof(int));
00950       } else
00951          re = -1;
00952    }
00953       break;
00954    case MISDN_CTYPE_BOOL:
00955       if (!dest->num) {
00956          dest->num = ast_malloc(sizeof(int));
00957       }
00958       *(dest->num) = (ast_true(value) ? 1 : 0);
00959       break;
00960    case MISDN_CTYPE_BOOLINT:
00961       if (!dest->num) {
00962          dest->num = ast_malloc(sizeof(int));
00963       }
00964       if (sscanf(value, "%30d", &tmp)) {
00965          memcpy(dest->num, &tmp, sizeof(int));
00966       } else {
00967          *(dest->num) = (ast_true(value) ? boolint_def : 0);
00968       }
00969       break;
00970    case MISDN_CTYPE_MSNLIST:
00971       for (valtmp = strsep(&tmp2, ","); valtmp; valtmp = strsep(&tmp2, ",")) {
00972          if ((len = strlen(valtmp))) {
00973             struct msn_list *ml = ast_malloc(sizeof(*ml));
00974             ml->msn = ast_calloc(len+1, sizeof(char));
00975             strncpy(ml->msn, valtmp, len);
00976             ml->next = dest->ml;
00977             dest->ml = ml;
00978          }
00979       }
00980       break;
00981    case MISDN_CTYPE_ASTGROUP:
00982       if (!dest->grp) {
00983          dest->grp = ast_malloc(sizeof(ast_group_t));
00984       }
00985       *(dest->grp) = ast_get_group(value);
00986       break;
00987    }
00988 
00989    return re;
00990 }

static int get_cfg_position ( const char *  name,
int  type 
) [static]

Definition at line 480 of file misdn_config.c.

References GEN_CFG, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, and PORT_CFG.

Referenced by _build_general_config(), _build_port_config(), and misdn_cfg_get_elem().

00481 {
00482    int i;
00483 
00484    switch (type) {
00485    case PORT_CFG:
00486       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00487          if (!strcasecmp(name, port_spec[i].name))
00488             return i;
00489       }
00490       break;
00491    case GEN_CFG:
00492       for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
00493          if (!strcasecmp(name, gen_spec[i].name))
00494             return i;
00495       }
00496    }
00497 
00498    return -1;
00499 }

void misdn_cfg_destroy ( void   ) 

Definition at line 1147 of file misdn_config.c.

References _free_general_cfg(), _free_port_cfg(), ast_free, ast_mutex_destroy, misdn_cfg_lock(), and misdn_cfg_unlock().

Referenced by unload_module().

01148 {
01149    misdn_cfg_lock();
01150 
01151    _free_port_cfg();
01152    _free_general_cfg();
01153 
01154    ast_free(port_cfg);
01155    ast_free(general_cfg);
01156    ast_free(ptp);
01157    ast_free(map);
01158 
01159    misdn_cfg_unlock();
01160    ast_mutex_destroy(&config_mutex);
01161 }

void misdn_cfg_get ( int  port,
enum misdn_cfg_elements  elem,
void *  buf,
int  bufsize 
)

Definition at line 562 of file misdn_config.c.

References misdn_cfg_pt::any, ast_copy_string(), ast_log(), LOG_WARNING, misdn_cfg_is_port_valid(), MISDN_CFG_LAST, misdn_cfg_lock(), MISDN_CFG_PTP, misdn_cfg_unlock(), MISDN_CTYPE_STR, S_OR, str, and type.

Referenced by add_in_calls(), add_out_calls(), cb_events(), dialtone_indicate(), load_module(), misdn_add_number_prefix(), misdn_bridge(), misdn_call(), misdn_cfg_update_ptp(), misdn_check_l2l1(), misdn_get_connected_line(), misdn_new(), misdn_request(), misdn_set_opt_exec(), process_ast_dsp(), read_config(), reload_config(), update_config(), and update_ec_config().

00563 {
00564    int place;
00565 
00566    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00567       memset(buf, 0, bufsize);
00568       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
00569       return;
00570    }
00571 
00572    misdn_cfg_lock();
00573    if (elem == MISDN_CFG_PTP) {
00574       if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
00575          memset(buf, 0, bufsize);
00576    } else {
00577       if ((place = map[elem]) < 0) {
00578          memset(buf, 0, bufsize);
00579          ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
00580       } else {
00581          if (elem < MISDN_CFG_LAST) {
00582             switch (port_spec[place].type) {
00583             case MISDN_CTYPE_STR:
00584                if (port_cfg[port][place].str) {
00585                   ast_copy_string(buf, port_cfg[port][place].str, bufsize);
00586                } else if (port_cfg[0][place].str) {
00587                   ast_copy_string(buf, port_cfg[0][place].str, bufsize);
00588                } else
00589                   memset(buf, 0, bufsize);
00590                break;
00591             default:
00592                if (port_cfg[port][place].any)
00593                   memcpy(buf, port_cfg[port][place].any, bufsize);
00594                else if (port_cfg[0][place].any)
00595                   memcpy(buf, port_cfg[0][place].any, bufsize);
00596                else
00597                   memset(buf, 0, bufsize);
00598             }
00599          } else {
00600             switch (gen_spec[place].type) {
00601             case MISDN_CTYPE_STR:
00602                ast_copy_string(buf, S_OR(general_cfg[place].str, ""), bufsize);
00603                break;
00604             default:
00605                if (general_cfg[place].any)
00606                   memcpy(buf, general_cfg[place].any, bufsize);
00607                else
00608                   memset(buf, 0, bufsize);
00609             }
00610          }
00611       }
00612    }
00613    misdn_cfg_unlock();
00614 }

void misdn_cfg_get_config_string ( int  port,
enum misdn_cfg_elements  elem,
char *  buf,
int  bufsize 
)

Definition at line 787 of file misdn_config.c.

References ast_log(), ast_print_group(), BUFFERSIZE, LOG_WARNING, MISDN_CFG_FIRST, misdn_cfg_is_port_valid(), MISDN_CFG_LAST, misdn_cfg_lock(), MISDN_CFG_PTP, misdn_cfg_unlock(), MISDN_CTYPE_ASTGROUP, MISDN_CTYPE_BOOL, MISDN_CTYPE_BOOLINT, MISDN_CTYPE_INT, MISDN_CTYPE_MSNLIST, MISDN_CTYPE_STR, MISDN_GEN_FIRST, MISDN_GEN_LAST, misdn_cfg_pt::ml, msn_list::msn, name, msn_list::next, str, and type.

Referenced by handle_cli_misdn_show_config().

00788 {
00789    int place;
00790    char tempbuf[BUFFERSIZE] = "";
00791    struct msn_list *iter;
00792 
00793    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00794       *buf = 0;
00795       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
00796       return;
00797    }
00798 
00799    place = map[elem];
00800 
00801    misdn_cfg_lock();
00802    if (elem == MISDN_CFG_PTP) {
00803       snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
00804    }
00805    else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
00806       switch (port_spec[place].type) {
00807       case MISDN_CTYPE_INT:
00808       case MISDN_CTYPE_BOOLINT:
00809          if (port_cfg[port][place].num)
00810             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
00811          else if (port_cfg[0][place].num)
00812             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
00813          else
00814             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00815          break;
00816       case MISDN_CTYPE_BOOL:
00817          if (port_cfg[port][place].num)
00818             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
00819          else if (port_cfg[0][place].num)
00820             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
00821          else
00822             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00823          break;
00824       case MISDN_CTYPE_ASTGROUP:
00825          if (port_cfg[port][place].grp)
00826             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00827                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
00828          else if (port_cfg[0][place].grp)
00829             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name,
00830                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
00831          else
00832             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00833          break;
00834       case MISDN_CTYPE_MSNLIST:
00835          if (port_cfg[port][place].ml)
00836             iter = port_cfg[port][place].ml;
00837          else
00838             iter = port_cfg[0][place].ml;
00839          if (iter) {
00840             for (; iter; iter = iter->next) {
00841                strncat(tempbuf, iter->msn, sizeof(tempbuf) - strlen(tempbuf) - 1);
00842             }
00843             if (strlen(tempbuf) > 1) {
00844                tempbuf[strlen(tempbuf)-2] = 0;
00845             }
00846          }
00847          snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
00848          break;
00849       case MISDN_CTYPE_STR:
00850          if ( port_cfg[port][place].str) {
00851             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
00852          } else if (port_cfg[0][place].str) {
00853             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
00854          } else {
00855             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00856          }
00857          break;
00858       }
00859    } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
00860       switch (gen_spec[place].type) {
00861       case MISDN_CTYPE_INT:
00862       case MISDN_CTYPE_BOOLINT:
00863          if (general_cfg[place].num)
00864             snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
00865          else
00866             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00867          break;
00868       case MISDN_CTYPE_BOOL:
00869          if (general_cfg[place].num)
00870             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
00871          else
00872             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00873          break;
00874       case MISDN_CTYPE_STR:
00875          if ( general_cfg[place].str) {
00876             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
00877          } else {
00878             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00879          }
00880          break;
00881       default:
00882          snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
00883          break;
00884       }
00885    } else {
00886       *buf = 0;
00887       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
00888    }
00889    misdn_cfg_unlock();
00890 }

void misdn_cfg_get_desc ( enum misdn_cfg_elements  elem,
void *  buf,
int  bufsize,
void *  buf_default,
int  bufsize_default 
)

Definition at line 663 of file misdn_config.c.

References ast_copy_string(), misdn_cfg_spec::def, desc, MISDN_CFG_FIRST, MISDN_CFG_GROUPNAME, MISDN_CFG_LAST, MISDN_GEN_FIRST, MISDN_GEN_LAST, and NO_DEFAULT.

Referenced by show_config_description().

00664 {
00665    int place = map[elem];
00666    struct misdn_cfg_spec *spec = NULL;
00667 
00668    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00669    if (elem == MISDN_CFG_GROUPNAME) {
00670       ast_copy_string(buf, ports_description, bufsize);
00671       if (buf_default && bufsize_default)
00672          memset(buf_default, 0, 1);
00673       return;
00674    }
00675 
00676    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00677       spec = (struct misdn_cfg_spec *)port_spec;
00678    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00679       spec = (struct misdn_cfg_spec *)gen_spec;
00680 
00681    if (!spec || !spec[place].desc)
00682       memset(buf, 0, 1);
00683    else {
00684       ast_copy_string(buf, spec[place].desc, bufsize);
00685       if (buf_default && bufsize) {
00686          if (!strcmp(spec[place].def, NO_DEFAULT))
00687             memset(buf_default, 0, 1);
00688          else
00689             ast_copy_string(buf_default, spec[place].def, bufsize_default);
00690       }
00691    }
00692 }

enum misdn_cfg_elements misdn_cfg_get_elem ( const char *  name  ) 

Definition at line 616 of file misdn_config.c.

References misdn_cfg_spec::elem, GEN_CFG, get_cfg_position(), MISDN_CFG_FIRST, MISDN_CFG_GROUPNAME, and PORT_CFG.

Referenced by handle_cli_misdn_show_config().

00617 {
00618    int pos;
00619 
00620    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00621    if (!strcmp(name, "ports"))
00622       return MISDN_CFG_GROUPNAME;
00623    if (!strcmp(name, "name"))
00624       return MISDN_CFG_FIRST;
00625 
00626    pos = get_cfg_position(name, PORT_CFG);
00627    if (pos >= 0)
00628       return port_spec[pos].elem;
00629 
00630    pos = get_cfg_position(name, GEN_CFG);
00631    if (pos >= 0)
00632       return gen_spec[pos].elem;
00633 
00634    return MISDN_CFG_FIRST;
00635 }

void misdn_cfg_get_name ( enum misdn_cfg_elements  elem,
void *  buf,
int  bufsize 
)

Definition at line 637 of file misdn_config.c.

References ast_copy_string(), MISDN_CFG_FIRST, MISDN_CFG_GROUPNAME, MISDN_CFG_LAST, MISDN_CFG_PTP, MISDN_GEN_FIRST, MISDN_GEN_LAST, and name.

Referenced by complete_show_config(), and show_config_description().

00638 {
00639    struct misdn_cfg_spec *spec = NULL;
00640    int place = map[elem];
00641 
00642    /* the ptp hack */
00643    if (elem == MISDN_CFG_PTP) {
00644       memset(buf, 0, 1);
00645       return;
00646    }
00647 
00648    /* here comes a hack to replace the (not existing) "name" element with the "ports" element */
00649    if (elem == MISDN_CFG_GROUPNAME) {
00650       if (!snprintf(buf, bufsize, "ports"))
00651          memset(buf, 0, 1);
00652       return;
00653    }
00654 
00655    if ((elem > MISDN_CFG_FIRST) && (elem < MISDN_CFG_LAST))
00656       spec = (struct misdn_cfg_spec *)port_spec;
00657    else if ((elem > MISDN_GEN_FIRST) && (elem < MISDN_GEN_LAST))
00658       spec = (struct misdn_cfg_spec *)gen_spec;
00659 
00660    ast_copy_string(buf, spec ? spec[place].name : "", bufsize);
00661 }

int misdn_cfg_get_next_port ( int  port  ) 

Definition at line 892 of file misdn_config.c.

References MISDN_CFG_GROUPNAME, misdn_cfg_lock(), misdn_cfg_unlock(), and str.

Referenced by complete_show_config(), handle_cli_misdn_show_config(), handle_cli_misdn_show_ports_stats(), handle_cli_misdn_show_stacks(), load_module(), misdn_cfg_get_next_port_spin(), misdn_check_l2l1(), misdn_new(), misdn_request(), and update_name().

00893 {
00894    int p = -1;
00895    int gn = map[MISDN_CFG_GROUPNAME];
00896 
00897    misdn_cfg_lock();
00898    for (port++; port <= max_ports; port++) {
00899       if (port_cfg[port][gn].str) {
00900          p = port;
00901          break;
00902       }
00903    }
00904    misdn_cfg_unlock();
00905 
00906    return p;
00907 }

int misdn_cfg_get_next_port_spin ( int  port  ) 

Definition at line 909 of file misdn_config.c.

References misdn_cfg_get_next_port().

Referenced by misdn_request().

00910 {
00911    int p = misdn_cfg_get_next_port(port);
00912    return (p > 0) ? p : misdn_cfg_get_next_port(0);
00913 }

void misdn_cfg_get_ports_string ( char *  ports  ) 

Generate a comma separated list of all active ports.

Definition at line 761 of file misdn_config.c.

References MISDN_CFG_GROUPNAME, misdn_cfg_lock(), misdn_cfg_unlock(), and str.

Referenced by load_module().

00762 {
00763    char tmp[16];
00764    int l, i;
00765    int gn = map[MISDN_CFG_GROUPNAME];
00766 
00767    *ports = 0;
00768 
00769    misdn_cfg_lock();
00770    for (i = 1; i <= max_ports; i++) {
00771       if (port_cfg[i][gn].str) {
00772          if (ptp[i])
00773             sprintf(tmp, "%dptp,", i);
00774          else
00775             sprintf(tmp, "%d,", i);
00776          strcat(ports, tmp);
00777       }
00778    }
00779    misdn_cfg_unlock();
00780 
00781    if ((l = strlen(ports))) {
00782       /* Strip trailing ',' */
00783       ports[l-1] = 0;
00784    }
00785 }

int misdn_cfg_init ( int  this_max_ports,
int  reload 
)

Definition at line 1163 of file misdn_config.c.

References _build_general_config(), _build_port_config(), _enum_array_map(), _fill_defaults(), _free_general_cfg(), _free_port_cfg(), ast_calloc, ast_category_browse(), ast_config_destroy(), ast_config_load2(), ast_log(), ast_mutex_init, ast_variable_browse(), config, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, global_jbconf, LOG_WARNING, misdn_cfg_lock(), misdn_cfg_unlock(), MISDN_GEN_LAST, NUM_GEN_ELEMENTS, and NUM_PORT_ELEMENTS.

Referenced by load_module(), and misdn_cfg_reload().

01164 {
01165    char config[] = "misdn.conf";
01166    char *cat, *p;
01167    int i;
01168    struct ast_config *cfg;
01169    struct ast_variable *v;
01170    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
01171 
01172    if (!(cfg = ast_config_load2(config, "chan_misdn", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
01173       ast_log(LOG_WARNING, "missing or invalid file: misdn.conf\n");
01174       return -1;
01175    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
01176       return 0;
01177 
01178    ast_mutex_init(&config_mutex);
01179 
01180    /* Copy the default jb config over global_jbconf */
01181    memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
01182 
01183    misdn_cfg_lock();
01184 
01185    if (this_max_ports) {
01186       /* this is the first run */
01187       max_ports = this_max_ports;
01188       map = ast_calloc(MISDN_GEN_LAST + 1, sizeof(int));
01189       if (_enum_array_map())
01190          return -1;
01191       p = ast_calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
01192                      + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
01193       port_cfg = (union misdn_cfg_pt **)p;
01194       p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
01195       for (i = 0; i <= max_ports; ++i) {
01196          port_cfg[i] = (union misdn_cfg_pt *)p;
01197          p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
01198       }
01199       general_cfg = ast_calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01200       ptp = ast_calloc(max_ports + 1, sizeof(int));
01201    }
01202    else {
01203       /* misdn reload */
01204       _free_port_cfg();
01205       _free_general_cfg();
01206       memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
01207       memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
01208       memset(ptp, 0, sizeof(int) * (max_ports + 1));
01209    }
01210 
01211    cat = ast_category_browse(cfg, NULL);
01212 
01213    while(cat) {
01214       v = ast_variable_browse(cfg, cat);
01215       if (!strcasecmp(cat, "general")) {
01216          _build_general_config(v);
01217       } else {
01218          _build_port_config(v, cat);
01219       }
01220       cat = ast_category_browse(cfg, cat);
01221    }
01222 
01223    _fill_defaults();
01224 
01225    misdn_cfg_unlock();
01226    ast_config_destroy(cfg);
01227 
01228    return 0;
01229 }

int misdn_cfg_is_group_method ( char *  group,
enum misdn_cfg_method  meth 
)

Definition at line 726 of file misdn_config.c.

References METHOD_ROUND_ROBIN, METHOD_STANDARD, METHOD_STANDARD_DEC, MISDN_CFG_GROUPNAME, misdn_cfg_lock(), MISDN_CFG_METHOD, misdn_cfg_unlock(), str, and misdn_cfg_pt::str.

Referenced by misdn_request().

00727 {
00728    int i, re = 0;
00729    char *method ;
00730 
00731    misdn_cfg_lock();
00732 
00733    method = port_cfg[0][map[MISDN_CFG_METHOD]].str;
00734 
00735    for (i = 1; i <= max_ports; i++) {
00736       if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
00737          if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
00738             method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ?
00739                     port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
00740       }
00741    }
00742 
00743    if (method) {
00744       switch (meth) {
00745       case METHOD_STANDARD:      re = !strcasecmp(method, "standard");
00746                            break;
00747       case METHOD_ROUND_ROBIN:   re = !strcasecmp(method, "round_robin");
00748                            break;
00749       case METHOD_STANDARD_DEC:  re = !strcasecmp(method, "standard_dec");
00750                            break;
00751       }
00752    }
00753    misdn_cfg_unlock();
00754 
00755    return re;
00756 }

int misdn_cfg_is_msn_valid ( int  port,
char *  msn 
)

Definition at line 694 of file misdn_config.c.

References ast_extension_match(), ast_log(), LOG_WARNING, misdn_cfg_is_port_valid(), misdn_cfg_lock(), MISDN_CFG_MSNS, misdn_cfg_unlock(), misdn_cfg_pt::ml, msn_list::msn, and msn_list::next.

Referenced by misdn_is_msn_valid().

00695 {
00696    int re = 0;
00697    struct msn_list *iter;
00698 
00699    if (!misdn_cfg_is_port_valid(port)) {
00700       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
00701       return 0;
00702    }
00703 
00704    misdn_cfg_lock();
00705    if (port_cfg[port][map[MISDN_CFG_MSNS]].ml)
00706       iter = port_cfg[port][map[MISDN_CFG_MSNS]].ml;
00707    else
00708       iter = port_cfg[0][map[MISDN_CFG_MSNS]].ml;
00709    for (; iter; iter = iter->next)
00710       if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
00711          re = 1;
00712          break;
00713       }
00714    misdn_cfg_unlock();
00715 
00716    return re;
00717 }

int misdn_cfg_is_port_valid ( int  port  ) 

Definition at line 719 of file misdn_config.c.

References MISDN_CFG_GROUPNAME, and str.

Referenced by handle_cli_misdn_show_config(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_is_msn_valid(), and misdn_cfg_update_ptp().

00720 {
00721    int gn = map[MISDN_CFG_GROUPNAME];
00722 
00723    return (port >= 1 && port <= max_ports && port_cfg[port][gn].str);
00724 }

static void misdn_cfg_lock ( void   )  [inline, static]
void misdn_cfg_reload ( void   ) 

Definition at line 1142 of file misdn_config.c.

References misdn_cfg_init().

Referenced by reload_config().

01143 {
01144    misdn_cfg_init(0, 1);
01145 }

static void misdn_cfg_unlock ( void   )  [inline, static]
void misdn_cfg_update_ptp ( void   ) 

Definition at line 1073 of file misdn_config.c.

References ast_log(), ast_strlen_zero(), BUFFERSIZE, errno, LOG_WARNING, misdn_cfg_get(), misdn_cfg_is_port_valid(), misdn_cfg_lock(), misdn_cfg_unlock(), and MISDN_GEN_MISDN_INIT.

Referenced by load_module(), and reload_config().

01074 {
01075 #ifndef MISDN_1_2
01076    char misdn_init[BUFFERSIZE];
01077    char line[BUFFERSIZE];
01078    FILE *fp;
01079    char *tok, *p, *end;
01080    int port;
01081 
01082    misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
01083 
01084    if (!ast_strlen_zero(misdn_init)) {
01085       fp = fopen(misdn_init, "r");
01086       if (fp) {
01087          while(fgets(line, sizeof(line), fp)) {
01088             if (!strncmp(line, "nt_ptp", 6)) {
01089                for (tok = strtok_r(line,",=", &p);
01090                    tok;
01091                    tok = strtok_r(NULL,",=", &p)) {
01092                   port = strtol(tok, &end, 10);
01093                   if (end != tok && misdn_cfg_is_port_valid(port)) {
01094                      misdn_cfg_lock();
01095                      ptp[port] = 1;
01096                      misdn_cfg_unlock();
01097                   }
01098                }
01099             }
01100          }
01101          fclose(fp);
01102       } else {
01103          ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
01104       }
01105    }
01106 #else
01107    int i;
01108    int proto;
01109    char filename[128];
01110    FILE *fp;
01111 
01112    for (i = 1; i <= max_ports; ++i) {
01113       snprintf(filename, sizeof(filename), "/sys/class/mISDN-stacks/st-%08x/protocol", i << 8);
01114       fp = fopen(filename, "r");
01115       if (!fp) {
01116          ast_log(LOG_WARNING, "Could not open %s: %s\n", filename, strerror(errno));
01117          continue;
01118       }
01119       if (fscanf(fp, "0x%08x", &proto) != 1)
01120          ast_log(LOG_WARNING, "Could not parse contents of %s!\n", filename);
01121       else
01122          ptp[i] = proto & 1<<5 ? 1 : 0;
01123       fclose(fp);
01124    }
01125 #endif
01126 }

struct ast_jb_conf* misdn_get_global_jbconf ( void   )  [read]

Definition at line 1231 of file misdn_config.c.

References global_jbconf.

Referenced by misdn_new().

01231                                               {
01232    return &global_jbconf;
01233 }


Variable Documentation

Definition at line 436 of file misdn_config.c.

struct ast_jb_conf default_jbconf [static]

Global jitterbuffer configuration - by default, jb is disabled

Note:
Values shown here match the defaults shown in misdn.conf.sample

Definition at line 56 of file misdn_config.c.

struct misdn_cfg_spec gen_spec[] [static]

Definition at line 387 of file misdn_config.c.

union misdn_cfg_pt* general_cfg [static]

Definition at line 430 of file misdn_config.c.

struct ast_jb_conf global_jbconf [static]

Definition at line 65 of file misdn_config.c.

Referenced by _build_general_config(), misdn_cfg_init(), and misdn_get_global_jbconf().

int* map [static]
int max_ports [static]

Definition at line 428 of file misdn_config.c.

union misdn_cfg_pt** port_cfg [static]

Definition at line 426 of file misdn_config.c.

struct misdn_cfg_spec port_spec[] [static]

Definition at line 102 of file misdn_config.c.

const char ports_description[] [static]
Initial value:
   "Define your ports, e.g. 1,2 (depends on mISDN-driver loading order)."

Definition at line 99 of file misdn_config.c.

int* ptp [static]

Definition at line 432 of file misdn_config.c.

Referenced by misdn_lib_init().


Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1