Fri Sep 29 11:14:43 2006

Asterisk developer's documentation


misdn_config.c File Reference

chan_misdn configuration management More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "chan_misdn_config.h"
#include <asterisk/config.h>
#include <asterisk/channel.h>
#include <asterisk/logger.h>
#include <asterisk/lock.h>
#include <asterisk/pbx.h>
#include <asterisk/strings.h>
#include <asterisk/utils.h>

Include dependency graph for misdn_config.c:

Go to the source code of this file.

Data Structures

union  misdn_cfg_pt
struct  misdn_cfg_spec
struct  msn_list

Defines

#define AST_DESTROY_CFG   ast_config_destroy
#define AST_LOAD_CFG   ast_config_load
#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 void _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, char *value, enum misdn_cfg_type type, int boolint_def)
static int get_cfg_position (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)
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)
void misdn_cfg_init (int this_max_ports)
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)

Variables

static ast_mutex_t config_mutex
static struct misdn_cfg_spec gen_spec []
static union misdn_cfg_ptgeneral_cfg
static int * map
static int max_ports
static union misdn_cfg_pt ** port_cfg
static struct misdn_cfg_spec port_spec []
static int * ptp


Detailed Description

chan_misdn configuration management

Author:
Christian Richter <crich@beronet.com>

Definition in file misdn_config.c.


Define Documentation

#define AST_DESTROY_CFG   ast_config_destroy

Definition at line 45 of file misdn_config.c.

#define AST_LOAD_CFG   ast_config_load

Definition at line 44 of file misdn_config.c.

#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 150 of file misdn_config.c.

Referenced by _build_general_config(), and _build_port_config().

#define GEN_CFG   1

Definition at line 50 of file misdn_config.c.

Referenced by _build_general_config(), and get_cfg_position().

#define NO_DEFAULT   "<>"

Definition at line 47 of file misdn_config.c.

Referenced by _fill_defaults().

#define NONE   0

Definition at line 48 of file misdn_config.c.

#define NUM_GEN_ELEMENTS   (sizeof(gen_spec) / sizeof(struct misdn_cfg_spec))

Definition at line 52 of file misdn_config.c.

Referenced by _enum_array_map(), _fill_defaults(), _free_general_cfg(), get_cfg_position(), and misdn_cfg_init().

#define NUM_PORT_ELEMENTS   (sizeof(port_spec) / sizeof(struct misdn_cfg_spec))

Definition at line 53 of file misdn_config.c.

Referenced by _build_port_config(), _enum_array_map(), _fill_defaults(), _free_port_cfg(), get_cfg_position(), and misdn_cfg_init().

#define PORT_CFG   2

Definition at line 51 of file misdn_config.c.

Referenced by _build_port_config(), and get_cfg_position().


Enumeration Type Documentation

enum misdn_cfg_type

Enumerator:
MISDN_CTYPE_STR 
MISDN_CTYPE_INT 
MISDN_CTYPE_BOOL 
MISDN_CTYPE_BOOLINT 
MISDN_CTYPE_MSNLIST 
MISDN_CTYPE_ASTGROUP 

Definition at line 55 of file misdn_config.c.


Function Documentation

static void _build_general_config ( struct ast_variable v  )  [static]

Definition at line 585 of file misdn_config.c.

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

Referenced by misdn_cfg_init().

00586 {
00587    int pos;
00588 
00589    for (; v; v = v->next) {
00590       if (((pos = get_cfg_position(v->name, GEN_CFG)) < 0) || 
00591          (_parse(&general_cfg[pos], v->value, gen_spec[pos].type, gen_spec[pos].boolint_def) < 0))
00592          CLI_ERROR(v->name, v->value, "general");
00593    }
00594 }

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

Definition at line 596 of file misdn_config.c.

References _parse(), CLI_ERROR, get_cfg_position(), max_ports, ast_variable::name, ast_variable::next, NUM_PORT_ELEMENTS, PORT_CFG, port_spec, ptp, strsep(), type, and ast_variable::value.

Referenced by misdn_cfg_init().

00597 {
00598    int pos, i;
00599    union misdn_cfg_pt cfg_tmp[NUM_PORT_ELEMENTS];
00600    int cfg_for_ports[max_ports + 1];
00601 
00602    if (!v || !cat)
00603       return;
00604 
00605    memset(cfg_tmp, 0, sizeof(cfg_tmp));
00606    memset(cfg_for_ports, 0, sizeof(cfg_for_ports));
00607 
00608    if (!strcasecmp(cat, "default")) {
00609       cfg_for_ports[0] = 1;
00610    }
00611 
00612    if (((pos = get_cfg_position("name", PORT_CFG)) < 0) || 
00613       (_parse(&cfg_tmp[pos], cat, port_spec[pos].type, port_spec[pos].boolint_def) < 0)) {
00614       CLI_ERROR(v->name, v->value, cat);
00615       return;
00616    }
00617 
00618    for (; v; v = v->next) {
00619       if (!strcasecmp(v->name, "ports")) {
00620          char *token;
00621          char ptpbuf[BUFFERSIZE] = "";
00622          int start, end;
00623          for (token = strsep(&v->value, ","); token; token = strsep(&v->value, ","), *ptpbuf = 0) { 
00624             if (!*token)
00625                continue;
00626             if (sscanf(token, "%d-%d%s", &start, &end, ptpbuf) >= 2) {
00627                for (; start <= end; start++) {
00628                   if (start <= max_ports && start > 0) {
00629                      cfg_for_ports[start] = 1;
00630                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
00631                   } else
00632                      CLI_ERROR(v->name, v->value, cat);
00633                }
00634             } else {
00635                if (sscanf(token, "%d%s", &start, ptpbuf)) {
00636                   if (start <= max_ports && start > 0) {
00637                      cfg_for_ports[start] = 1;
00638                      ptp[start] = (strstr(ptpbuf, "ptp")) ? 1 : 0;
00639                   } else
00640                      CLI_ERROR(v->name, v->value, cat);
00641                } else
00642                   CLI_ERROR(v->name, v->value, cat);
00643             }
00644          }
00645       } else {
00646          if (((pos = get_cfg_position(v->name, PORT_CFG)) < 0) || 
00647             (_parse(&cfg_tmp[pos], v->value, port_spec[pos].type, port_spec[pos].boolint_def) < 0))
00648             CLI_ERROR(v->name, v->value, cat);
00649       }
00650    }
00651 
00652    for (i = 0; i < (max_ports + 1); ++i) {
00653       if (cfg_for_ports[i]) {
00654          memcpy(port_cfg[i], cfg_tmp, sizeof(cfg_tmp));
00655       }
00656    }
00657 }

static void _enum_array_map ( void   )  [static]

Definition at line 155 of file misdn_config.c.

References gen_spec, map, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, and port_spec.

Referenced by misdn_cfg_init().

00156 {
00157    int i, j;
00158 
00159    for (i = MISDN_CFG_FIRST + 1; i < MISDN_CFG_LAST; ++i) {
00160       if (i == MISDN_CFG_PTP)
00161          continue;
00162       for (j = 0; j < NUM_PORT_ELEMENTS; ++j) {
00163          if (port_spec[j].elem == i) {
00164             map[i] = j;
00165             break;
00166          }
00167       }
00168    }
00169    for (i = MISDN_GEN_FIRST + 1; i < MISDN_GEN_LAST; ++i) {
00170       for (j = 0; j < NUM_GEN_ELEMENTS; ++j) {
00171          if (gen_spec[j].elem == i) {
00172             map[i] = j;
00173             break;
00174          }
00175       }
00176    }
00177 }

static void _fill_defaults ( void   )  [static]

Definition at line 693 of file misdn_config.c.

References _parse(), misdn_cfg_pt::any, gen_spec, general_cfg, NO_DEFAULT, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, port_cfg, port_spec, and type.

Referenced by misdn_cfg_init().

00694 {
00695    int i;
00696 
00697    for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00698       if (!port_cfg[0][i].any && strcasecmp(port_spec[i].def, NO_DEFAULT))
00699          _parse(&(port_cfg[0][i]), (char *)port_spec[i].def, port_spec[i].type, port_spec[i].boolint_def);
00700    }
00701    for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
00702       if (!general_cfg[i].any && strcasecmp(gen_spec[i].def, NO_DEFAULT))
00703          _parse(&(general_cfg[i]), (char *)gen_spec[i].def, gen_spec[i].type, gen_spec[i].boolint_def);
00704    }
00705 }

static void _free_general_cfg ( void   )  [static]

Definition at line 252 of file misdn_config.c.

References misdn_cfg_pt::any, free, general_cfg, and NUM_GEN_ELEMENTS.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00253 {
00254    int i;
00255 
00256    for (i = 0; i < NUM_GEN_ELEMENTS; i++) 
00257       if (general_cfg[i].any)
00258          free(general_cfg[i].any);
00259 }

static void _free_msn_list ( struct msn_list iter  )  [static]

Definition at line 210 of file misdn_config.c.

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

Referenced by _free_port_cfg().

00211 {
00212    if (iter->next)
00213       _free_msn_list(iter->next);
00214    if (iter->msn)
00215       free(iter->msn);
00216    free(iter);
00217 }

static void _free_port_cfg ( void   )  [static]

Definition at line 219 of file misdn_config.c.

References _free_msn_list(), misdn_cfg_pt::any, free, map, max_ports, MISDN_CTYPE_MSNLIST, misdn_cfg_pt::ml, NUM_PORT_ELEMENTS, port_cfg, port_spec, misdn_cfg_pt::str, and type.

Referenced by misdn_cfg_destroy(), and misdn_cfg_init().

00220 {
00221    int i, j;
00222    int gn = map[MISDN_CFG_GROUPNAME];
00223    union misdn_cfg_pt* free_list[max_ports + 2];
00224    
00225    memset(free_list, 0, sizeof(free_list));
00226    free_list[0] = port_cfg[0];
00227    for (i = 1; i <= max_ports; ++i) {
00228       if (port_cfg[i][gn].str) {
00229          /* we always have a groupname in the non-default case, so this is fine */
00230          for (j = 1; j <= max_ports; ++j) {
00231             if (free_list[j] && free_list[j][gn].str == port_cfg[i][gn].str)
00232                break;
00233             else if (!free_list[j]) {
00234                free_list[j] = port_cfg[i];
00235                break;
00236             }
00237          }
00238       }
00239    }
00240    for (j = 0; free_list[j]; ++j) {
00241       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00242          if (free_list[j][i].any) {
00243             if (port_spec[i].type == MISDN_CTYPE_MSNLIST)
00244                _free_msn_list(free_list[j][i].ml);
00245             else
00246                free(free_list[j][i].any);
00247          }
00248       }
00249    }
00250 }

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

Definition at line 522 of file misdn_config.c.

References ast_get_group(), ast_true(), calloc, misdn_cfg_pt::grp, malloc, 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, misdn_cfg_pt::str, and strsep().

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

00523 {
00524    int re = 0;
00525    int len, tmp;
00526    char *valtmp;
00527 
00528    switch (type) {
00529    case MISDN_CTYPE_STR:
00530       if ((len = strlen(value))) {
00531          dest->str = (char *)malloc((len + 1) * sizeof(char));
00532          strncpy(dest->str, value, len);
00533          dest->str[len] = 0;
00534       } else {
00535          dest->str = (char *)malloc( sizeof(char));
00536          dest->str[0] = 0;
00537       }
00538       break;
00539    case MISDN_CTYPE_INT:
00540    {
00541       char *pat;
00542       if (strchr(value,'x')) 
00543          pat="%x";
00544       else
00545          pat="%d";
00546       if (sscanf(value, pat, &tmp)) {
00547          dest->num = (int *)malloc(sizeof(int));
00548          memcpy(dest->num, &tmp, sizeof(int));
00549       } else
00550          re = -1;
00551    }
00552       break;
00553    case MISDN_CTYPE_BOOL:
00554       dest->num = (int *)malloc(sizeof(int));
00555       *(dest->num) = (ast_true(value) ? 1 : 0);
00556       break;
00557    case MISDN_CTYPE_BOOLINT:
00558       dest->num = (int *)malloc(sizeof(int));
00559       if (sscanf(value, "%d", &tmp)) {
00560          memcpy(dest->num, &tmp, sizeof(int));
00561       } else {
00562          *(dest->num) = (ast_true(value) ? boolint_def : 0);
00563       }
00564       break;
00565    case MISDN_CTYPE_MSNLIST:
00566       for (valtmp = strsep(&value, ","); valtmp; valtmp = strsep(&value, ",")) {
00567          if ((len = strlen(valtmp))) {
00568             struct msn_list *ml = (struct msn_list *)malloc(sizeof(struct msn_list));
00569             ml->msn = (char *)calloc(len+1, sizeof(char));
00570             strncpy(ml->msn, valtmp, len);
00571             ml->next = dest->ml;
00572             dest->ml = ml;
00573          }
00574       }
00575       break;
00576    case MISDN_CTYPE_ASTGROUP:
00577       dest->grp = (ast_group_t *)malloc(sizeof(ast_group_t));
00578       *(dest->grp) = ast_get_group(value);
00579       break;
00580    }
00581 
00582    return re;
00583 }

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

Definition at line 179 of file misdn_config.c.

References GEN_CFG, gen_spec, NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, PORT_CFG, and port_spec.

Referenced by _build_general_config(), and _build_port_config().

00180 {
00181    int i;
00182 
00183    switch (type) {
00184    case PORT_CFG:
00185       for (i = 0; i < NUM_PORT_ELEMENTS; ++i) {
00186          if (!strcasecmp(name, port_spec[i].name))
00187             return i;
00188       }
00189       break;
00190    case GEN_CFG:
00191       for (i = 0; i < NUM_GEN_ELEMENTS; ++i) {
00192          if (!strcasecmp(name, gen_spec[i].name))
00193             return i;
00194       }
00195    }
00196 
00197    return -1;
00198 }

void misdn_cfg_destroy ( void   ) 

Definition at line 712 of file misdn_config.c.

References _free_general_cfg(), _free_port_cfg(), ast_mutex_destroy(), config_mutex, free, general_cfg, map, misdn_cfg_lock(), misdn_cfg_unlock(), port_cfg, and ptp.

Referenced by unload_module().

00713 {
00714    misdn_cfg_lock();
00715 
00716    _free_port_cfg();
00717    _free_general_cfg();
00718 
00719    free(port_cfg);
00720    free(general_cfg);
00721    free(ptp);
00722    free(map);
00723 
00724    misdn_cfg_unlock();
00725    ast_mutex_destroy(&config_mutex);
00726 }

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

Definition at line 261 of file misdn_config.c.

References misdn_cfg_pt::any, ast_log(), gen_spec, general_cfg, LOG_WARNING, map, misdn_cfg_is_port_valid(), misdn_cfg_lock(), misdn_cfg_unlock(), MISDN_CTYPE_STR, port_cfg, port_spec, ptp, misdn_cfg_pt::str, and type.

Referenced by cb_events(), dialtone_indicate(), load_module(), misdn_bridge(), misdn_cfg_update_ptp(), misdn_new(), misdn_request(), read_config(), reload_config(), update_config(), and update_ec_config().

00262 {
00263    int place;
00264 
00265    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00266       memset(buf, 0, bufsize);
00267       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
00268       return;
00269    }
00270 
00271    misdn_cfg_lock();
00272    if (elem == MISDN_CFG_PTP) {
00273       if (!memcpy(buf, &ptp[port], (bufsize > ptp[port]) ? sizeof(ptp[port]) : bufsize))
00274          memset(buf, 0, bufsize);
00275    } else {
00276       if ((place = map[elem]) < 0) {
00277          memset (buf, 0, bufsize);
00278          ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Invalid element (%d) requested.\n", elem);
00279       } else {
00280          if (elem < MISDN_CFG_LAST) {
00281             switch (port_spec[place].type) {
00282             case MISDN_CTYPE_STR:
00283                if (port_cfg[port][place].str) {
00284                   if (!memccpy(buf, port_cfg[port][place].str, 0, bufsize))
00285                      memset(buf, 0, 1);
00286                } else if (port_cfg[0][place].str) {
00287                   if (!memccpy(buf, port_cfg[0][place].str, 0, bufsize))
00288                      memset(buf, 0, 1);
00289                }
00290                break;
00291             default:
00292                if (port_cfg[port][place].any)
00293                   memcpy(buf, port_cfg[port][place].any, bufsize);
00294                else if (port_cfg[0][place].any)
00295                   memcpy(buf, port_cfg[0][place].any, bufsize);
00296                else
00297                   memset(buf, 0, bufsize);
00298             }
00299          } else {
00300             switch (gen_spec[place].type) {
00301             case MISDN_CTYPE_STR:
00302                if (!general_cfg[place].str || !memccpy(buf, general_cfg[place].str, 0, bufsize))
00303                   memset(buf, 0, 1);
00304                break;
00305             default:
00306                if (general_cfg[place].any)
00307                   memcpy(buf, general_cfg[place].any, bufsize);
00308                else
00309                   memset(buf, 0, bufsize);
00310             }
00311          }
00312       }
00313    }
00314    misdn_cfg_unlock();
00315 }

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

Definition at line 397 of file misdn_config.c.

References ast_log(), ast_print_group(), gen_spec, general_cfg, LOG_WARNING, map, misdn_cfg_is_port_valid(), misdn_cfg_lock(), misdn_cfg_unlock(), 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, name, msn_list::next, port_cfg, port_spec, ptp, and type.

Referenced by misdn_show_config().

00398 {
00399    int place;
00400    char tempbuf[BUFFERSIZE] = "";
00401    struct msn_list *iter;
00402 
00403    if ((elem < MISDN_CFG_LAST) && !misdn_cfg_is_port_valid(port)) {
00404       *buf = 0;
00405       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
00406       return;
00407    }
00408 
00409    place = map[elem];
00410 
00411    misdn_cfg_lock();
00412    if (elem == MISDN_CFG_PTP) {
00413       snprintf(buf, bufsize, " -> ptp: %s", ptp[port] ? "yes" : "no");
00414    }
00415    else if (elem > MISDN_CFG_FIRST && elem < MISDN_CFG_LAST) {
00416       switch (port_spec[place].type) {
00417       case MISDN_CTYPE_INT:
00418       case MISDN_CTYPE_BOOLINT:
00419          if (port_cfg[port][place].num)
00420             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[port][place].num);
00421          else if (port_cfg[0][place].num)
00422             snprintf(buf, bufsize, " -> %s: %d", port_spec[place].name, *port_cfg[0][place].num);
00423          else
00424             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00425          break;
00426       case MISDN_CTYPE_BOOL:
00427          if (port_cfg[port][place].num)
00428             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[port][place].num ? "yes" : "no");
00429          else if (port_cfg[0][place].num)
00430             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, *port_cfg[0][place].num ? "yes" : "no");
00431          else
00432             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00433          break;
00434       case MISDN_CTYPE_ASTGROUP:
00435          if (port_cfg[port][place].grp)
00436             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, 
00437                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[port][place].grp));
00438          else if (port_cfg[0][place].grp)
00439             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, 
00440                    ast_print_group(tempbuf, sizeof(tempbuf), *port_cfg[0][place].grp));
00441          else
00442             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00443          break;
00444       case MISDN_CTYPE_MSNLIST:
00445          if (port_cfg[port][place].ml)
00446             iter = port_cfg[port][place].ml;
00447          else
00448             iter = port_cfg[0][place].ml;
00449          if (iter) {
00450             for (; iter; iter = iter->next)
00451                sprintf(tempbuf, "%s%s, ", tempbuf, iter->msn);
00452             tempbuf[strlen(tempbuf)-2] = 0;
00453          }
00454          snprintf(buf, bufsize, " -> msns: %s", *tempbuf ? tempbuf : "none");
00455          break;
00456       case MISDN_CTYPE_STR:
00457          if ( port_cfg[port][place].str) {
00458             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[port][place].str);
00459          } else if (port_cfg[0][place].str) {
00460             snprintf(buf, bufsize, " -> %s: %s", port_spec[place].name, port_cfg[0][place].str);
00461          } else {
00462             snprintf(buf, bufsize, " -> %s:", port_spec[place].name);
00463          }
00464          break;
00465       }
00466    } else if (elem > MISDN_GEN_FIRST && elem < MISDN_GEN_LAST) {
00467       switch (gen_spec[place].type) {
00468       case MISDN_CTYPE_INT:
00469       case MISDN_CTYPE_BOOLINT:
00470          if (general_cfg[place].num)
00471             snprintf(buf, bufsize, " -> %s: %d", gen_spec[place].name, *general_cfg[place].num);
00472          else
00473             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00474          break;
00475       case MISDN_CTYPE_BOOL:
00476          if (general_cfg[place].num)
00477             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, *general_cfg[place].num ? "yes" : "no");
00478          else
00479             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00480          break;
00481       case MISDN_CTYPE_STR:
00482          if ( general_cfg[place].str) {
00483             snprintf(buf, bufsize, " -> %s: %s", gen_spec[place].name, general_cfg[place].str);
00484          } else {
00485             snprintf(buf, bufsize, " -> %s:", gen_spec[place].name);
00486          }
00487          break;
00488       default:
00489          snprintf(buf, bufsize, " -> type of %s not handled yet", gen_spec[place].name);
00490          break;
00491       }
00492    } else {
00493       *buf = 0;
00494       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Invalid config element (%d) requested.\n", elem);
00495    }
00496    misdn_cfg_unlock();
00497 }

int misdn_cfg_get_next_port ( int  port  ) 

Definition at line 499 of file misdn_config.c.

References map, max_ports, misdn_cfg_lock(), misdn_cfg_unlock(), and port_cfg.

Referenced by misdn_cfg_get_next_port_spin(), misdn_request(), misdn_show_config(), and misdn_show_stacks().

00500 {
00501    int p = -1;
00502    int gn = map[MISDN_CFG_GROUPNAME];
00503    
00504    misdn_cfg_lock();
00505    for (port++; port <= max_ports; port++) {
00506       if (port_cfg[port][gn].str) {
00507          p = port;
00508          break;
00509       }
00510    }
00511    misdn_cfg_unlock();
00512 
00513    return p;
00514 }

int misdn_cfg_get_next_port_spin ( int  port  ) 

Definition at line 516 of file misdn_config.c.

References misdn_cfg_get_next_port().

Referenced by misdn_request().

00517 {
00518    int p = misdn_cfg_get_next_port(port);
00519    return (p > 0) ? p : misdn_cfg_get_next_port(0);
00520 }

void misdn_cfg_get_ports_string ( char *  ports  ) 

Definition at line 373 of file misdn_config.c.

References map, max_ports, misdn_cfg_lock(), misdn_cfg_unlock(), port_cfg, and ptp.

Referenced by load_module().

00374 {
00375    char tmp[16];
00376    int l, i;
00377    int gn = map[MISDN_CFG_GROUPNAME];
00378 
00379    *ports = 0;
00380 
00381    misdn_cfg_lock();
00382    for (i = 1; i <= max_ports; i++) {
00383       if (port_cfg[i][gn].str) {
00384          if (ptp[i])
00385             sprintf(tmp, "%dptp,", i);
00386          else
00387             sprintf(tmp, "%d,", i);
00388          strcat(ports, tmp);
00389       }
00390    }
00391    misdn_cfg_unlock();
00392 
00393    if ((l = strlen(ports)))
00394       ports[l-1] = 0;
00395 }

void misdn_cfg_init ( int  this_max_ports  ) 

Definition at line 728 of file misdn_config.c.

References _build_general_config(), _build_port_config(), _enum_array_map(), _fill_defaults(), _free_general_cfg(), _free_port_cfg(), ast_category_browse(), AST_DESTROY_CFG, AST_LOAD_CFG, ast_log(), ast_variable_browse(), calloc, cfg, config, general_cfg, LOG_WARNING, map, max_ports, misdn_cfg_lock(), misdn_cfg_unlock(), NUM_GEN_ELEMENTS, NUM_PORT_ELEMENTS, port_cfg, and ptp.

Referenced by load_module(), and misdn_cfg_reload().

00729 {
00730    char config[] = "misdn.conf";
00731    char *cat, *p;
00732    int i;
00733    struct ast_config *cfg;
00734    struct ast_variable *v;
00735 
00736    if (!(cfg = AST_LOAD_CFG(config))) {
00737       ast_log(LOG_WARNING,"no misdn.conf ?\n");
00738       return;
00739    }
00740 
00741    misdn_cfg_lock();
00742 
00743    if (this_max_ports) {
00744       /* this is the first run */
00745       max_ports = this_max_ports;
00746       p = (char *)calloc(1, (max_ports + 1) * sizeof(union misdn_cfg_pt *)
00747                      + (max_ports + 1) * NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt));
00748       port_cfg = (union misdn_cfg_pt **)p;
00749       p += (max_ports + 1) * sizeof(union misdn_cfg_pt *);
00750       for (i = 0; i <= max_ports; ++i) {
00751          port_cfg[i] = (union misdn_cfg_pt *)p;
00752          p += NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt);
00753       }
00754       general_cfg = (union misdn_cfg_pt *)calloc(1, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
00755       ptp = (int *)calloc(max_ports + 1, sizeof(int));
00756       map = (int *)calloc(MISDN_GEN_LAST + 1, sizeof(int));
00757       _enum_array_map();
00758    }
00759    else {
00760       /* misdn reload */
00761       _free_port_cfg();
00762       _free_general_cfg();
00763       memset(port_cfg[0], 0, NUM_PORT_ELEMENTS * sizeof(union misdn_cfg_pt) * (max_ports + 1));
00764       memset(general_cfg, 0, sizeof(union misdn_cfg_pt *) * NUM_GEN_ELEMENTS);
00765       memset(ptp, 0, sizeof(int) * (max_ports + 1));
00766    }
00767 
00768    cat = ast_category_browse(cfg, NULL);
00769 
00770    while(cat) {
00771       v = ast_variable_browse(cfg, cat);
00772       if (!strcasecmp(cat,"general")) {
00773          _build_general_config(v);
00774       } else {
00775          _build_port_config(v, cat);
00776       }
00777       cat = ast_category_browse(cfg,cat);
00778    }
00779 
00780    _fill_defaults();
00781 
00782    misdn_cfg_unlock();
00783    AST_DESTROY_CFG(cfg);
00784 }

int misdn_cfg_is_group_method ( char *  group,
enum misdn_cfg_method  meth 
)

Definition at line 347 of file misdn_config.c.

References map, max_ports, misdn_cfg_lock(), misdn_cfg_unlock(), and port_cfg.

Referenced by misdn_request().

00348 {
00349    int i, re = 0;
00350    char *method = NULL;
00351 
00352    misdn_cfg_lock();
00353    for (i = 1; i <= max_ports; i++) {
00354       if (port_cfg[i] && port_cfg[i][map[MISDN_CFG_GROUPNAME]].str) {
00355          if (!strcasecmp(port_cfg[i][map[MISDN_CFG_GROUPNAME]].str, group))
00356             method = (port_cfg[i][map[MISDN_CFG_METHOD]].str ? 
00357                     port_cfg[i][map[MISDN_CFG_METHOD]].str : port_cfg[0][map[MISDN_CFG_METHOD]].str);
00358       }
00359    }
00360    if (method) {
00361       switch (meth) {
00362       case METHOD_STANDARD:      re = !strcasecmp(method, "standard");
00363                            break;
00364       case METHOD_ROUND_ROBIN:   re = !strcasecmp(method, "round_robin");
00365                            break;
00366       }
00367    }
00368    misdn_cfg_unlock();
00369 
00370    return re;
00371 }

int misdn_cfg_is_msn_valid ( int  port,
char *  msn 
)

Definition at line 317 of file misdn_config.c.

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

Referenced by cb_events().

00318 {
00319    int re = 0;
00320    struct msn_list *iter;
00321 
00322    if (!misdn_cfg_is_port_valid(port)) {
00323       ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
00324       return 0;
00325    }
00326 
00327    misdn_cfg_lock();
00328    if (port_cfg[port][map[MISDN_CFG_MSNS]].ml)
00329       iter = port_cfg[port][map[MISDN_CFG_MSNS]].ml;
00330    else
00331       iter = port_cfg[0][map[MISDN_CFG_MSNS]].ml;
00332    for (; iter; iter = iter->next) 
00333       if (*(iter->msn) == '*' || ast_extension_match(iter->msn, msn)) {
00334          re = 1;
00335          break;
00336       }
00337    misdn_cfg_unlock();
00338 
00339    return re;
00340 }

int misdn_cfg_is_port_valid ( int  port  ) 

Definition at line 342 of file misdn_config.c.

References max_ports.

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

00343 {
00344    return (port >= 1 && port <= max_ports);
00345 }

static void misdn_cfg_lock ( void   )  [inline, static]

Definition at line 200 of file misdn_config.c.

References ast_mutex_lock(), and config_mutex.

Referenced by misdn_cfg_destroy(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_get_next_port(), misdn_cfg_get_ports_string(), misdn_cfg_init(), misdn_cfg_is_group_method(), misdn_cfg_is_msn_valid(), and misdn_cfg_update_ptp().

00201 {
00202    ast_mutex_lock(&config_mutex);
00203 }

void misdn_cfg_reload ( void   ) 

Definition at line 707 of file misdn_config.c.

References misdn_cfg_init().

Referenced by reload_config().

00708 {
00709    misdn_cfg_init (0);
00710 }

static void misdn_cfg_unlock ( void   )  [inline, static]

Definition at line 205 of file misdn_config.c.

References ast_mutex_unlock(), and config_mutex.

Referenced by misdn_cfg_destroy(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_get_next_port(), misdn_cfg_get_ports_string(), misdn_cfg_init(), misdn_cfg_is_group_method(), and misdn_cfg_update_ptp().

00206 {
00207    ast_mutex_unlock(&config_mutex);
00208 }

void misdn_cfg_update_ptp ( void   ) 

Definition at line 659 of file misdn_config.c.

References ast_log(), LOG_WARNING, misdn_cfg_get(), misdn_cfg_is_port_valid(), misdn_cfg_lock(), misdn_cfg_unlock(), and ptp.

Referenced by load_module(), and reload_config().

00660 {
00661    char misdn_init[BUFFERSIZE];
00662    char line[BUFFERSIZE];
00663    FILE *fp;
00664    char *tok, *p, *end;
00665    int port;
00666 
00667    misdn_cfg_get(0, MISDN_GEN_MISDN_INIT, &misdn_init, sizeof(misdn_init));
00668 
00669    if (misdn_init) {
00670       fp = fopen(misdn_init, "r");
00671       if (fp) {
00672          while(fgets(line, sizeof(line), fp)) {
00673             if (!strncmp(line, "nt_ptp", 6)) {
00674                for (tok = strtok_r(line,",=", &p);
00675                    tok;
00676                    tok = strtok_r(NULL,",=", &p)) {
00677                   port = strtol(tok, &end, 10);
00678                   if (end != tok && misdn_cfg_is_port_valid(port)) {
00679                      misdn_cfg_lock();
00680                      ptp[port] = 1;
00681                      misdn_cfg_unlock();
00682                   }
00683                }
00684             }
00685          }
00686          fclose(fp);
00687       } else {
00688          ast_log(LOG_WARNING,"Couldn't open %s: %s\n", misdn_init, strerror(errno));
00689       }
00690    }
00691 }


Variable Documentation

ast_mutex_t config_mutex [static]

Definition at line 148 of file misdn_config.c.

Referenced by misdn_cfg_destroy(), misdn_cfg_lock(), and misdn_cfg_unlock().

struct misdn_cfg_spec gen_spec[] [static]

Definition at line 123 of file misdn_config.c.

Referenced by _build_general_config(), _enum_array_map(), _fill_defaults(), get_cfg_position(), misdn_cfg_get(), and misdn_cfg_get_config_string().

union misdn_cfg_pt* general_cfg [static]

Definition at line 142 of file misdn_config.c.

Referenced by _build_general_config(), _fill_defaults(), _free_general_cfg(), misdn_cfg_destroy(), misdn_cfg_get(), misdn_cfg_get_config_string(), and misdn_cfg_init().

int* map [static]

Definition at line 146 of file misdn_config.c.

Referenced by _enum_array_map(), _free_port_cfg(), append_mapping(), build_mapping(), clear_config_maps(), conf_exec(), config_command(), destroy_map(), dundi_lookup_local(), dundi_show_mappings(), find_engine(), mark_mappings(), misdn_cfg_destroy(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_get_next_port(), misdn_cfg_get_ports_string(), misdn_cfg_init(), misdn_cfg_is_group_method(), misdn_cfg_is_msn_valid(), and prune_mappings().

int max_ports [static]

Definition at line 140 of file misdn_config.c.

union misdn_cfg_pt** port_cfg [static]

Definition at line 138 of file misdn_config.c.

Referenced by _fill_defaults(), _free_port_cfg(), misdn_cfg_destroy(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_get_next_port(), misdn_cfg_get_ports_string(), misdn_cfg_init(), misdn_cfg_is_group_method(), and misdn_cfg_is_msn_valid().

struct misdn_cfg_spec port_spec[] [static]

Definition at line 85 of file misdn_config.c.

Referenced by _build_port_config(), _enum_array_map(), _fill_defaults(), _free_port_cfg(), get_cfg_position(), misdn_cfg_get(), and misdn_cfg_get_config_string().

int* ptp [static]

Definition at line 144 of file misdn_config.c.

Referenced by _build_port_config(), misdn_cfg_destroy(), misdn_cfg_get(), misdn_cfg_get_config_string(), misdn_cfg_get_ports_string(), misdn_cfg_init(), and misdn_cfg_update_ptp().


Generated on Fri Sep 29 11:14:43 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.7