Sun Oct 16 2011 08:43:00

Asterisk developer's documentation


pbx_realtime.c File Reference

Realtime PBX Module. More...

#include "asterisk.h"
#include <signal.h>
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/frame.h"
#include "asterisk/term.h"
#include "asterisk/manager.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/md5.h"
#include "asterisk/linkedlists.h"
#include "asterisk/chanvars.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/utils.h"
#include "asterisk/astdb.h"
#include "asterisk/app.h"
#include "asterisk/astobj2.h"
Include dependency graph for pbx_realtime.c:

Go to the source code of this file.

Data Structures

struct  cache_entry

Defines

#define EXT_DATA_SIZE   256
#define MODE_CANMATCH   2
#define MODE_MATCH   0
#define MODE_MATCHMORE   1

Enumerations

enum  option_flags {
  OPTION_A = (1 << 0), OPTION_B = (1 << 1), OPTION_C = (1 << 2), OPTION_WAIT = (1 << 0),
  OPTION_PATTERNS_DISABLED = (1 << 0)
}

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int cache_cmp (void *obj, void *arg, int flags)
static int cache_hash (const void *obj, const int flags)
static void * cleanup (void *unused)
static struct ast_variabledup_vars (struct ast_variable *v)
static void free_entry (void *obj)
static int load_module (void)
static int purge_old_fn (void *obj, void *arg, int flags)
static int realtime_canmatch (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static struct ast_variablerealtime_common (const char *context, const char *exten, int priority, const char *data, int mode)
static int realtime_exec (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static int realtime_exists (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static int realtime_matchmore (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
static struct ast_variablerealtime_switch_common (const char *table, const char *context, const char *exten, int priority, int mode, struct ast_flags flags)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime Switch" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "7a1b8b48c852d7a7061c7e499b9bd0d2" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static struct ast_module_infoast_module_info = &__mod_info
struct ao2_containercache
pthread_t cleanup_thread = 0
static struct ast_switch realtime_switch
static struct ast_app_option switch_opts [128] = { [ 'p' ] = { .flag = OPTION_PATTERNS_DISABLED },}

Detailed Description

Realtime PBX Module.

Definition in file pbx_realtime.c.


Define Documentation

#define EXT_DATA_SIZE   256

Definition at line 61 of file pbx_realtime.c.

Referenced by realtime_exec().

#define MODE_CANMATCH   2

Definition at line 59 of file pbx_realtime.c.

Referenced by realtime_switch_common(), and realtime_canmatch().

#define MODE_MATCH   0
#define MODE_MATCHMORE   1

Definition at line 58 of file pbx_realtime.c.

Referenced by realtime_switch_common(), and realtime_matchmore().


Enumeration Type Documentation

Enumerator:
OPTION_A 
OPTION_B 
OPTION_C 
OPTION_WAIT 
OPTION_PATTERNS_DISABLED 

Definition at line 63 of file pbx_realtime.c.

                  {
   OPTION_PATTERNS_DISABLED = (1 << 0),
};

Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 426 of file pbx_realtime.c.

static void __unreg_module ( void  ) [static]

Definition at line 426 of file pbx_realtime.c.

static int cache_cmp ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 88 of file pbx_realtime.c.

References f, cache_entry::priority, cache_entry::exten, cache_entry::context, and CMP_MATCH.

Referenced by load_module().

{
   struct cache_entry *e = obj, *f = arg;
   return e->priority != f->priority ? 0 :
      strcmp(e->exten, f->exten) ? 0 :
      strcmp(e->context, f->context) ? 0 :
      CMP_MATCH;
}
static int cache_hash ( const void *  obj,
const int  flags 
) [static]

Definition at line 82 of file pbx_realtime.c.

References ast_str_case_hash(), cache_entry::exten, and cache_entry::priority.

Referenced by load_module().

{
   const struct cache_entry *e = obj;
   return ast_str_case_hash(e->exten) + e->priority;
}
static void* cleanup ( void *  unused) [static]

Definition at line 126 of file pbx_realtime.c.

References ao2_container_count(), ast_tvnow(), ao2_callback, OBJ_MULTIPLE, OBJ_UNLINK, OBJ_NODATA, and purge_old_fn().

Referenced by build_user(), _sip_tcp_helper_thread(), handle_uri(), ast_sockaddr_resolve(), and load_module().

{
   struct timespec forever = { 999999999, 0 }, one_second = { 1, 0 };
   struct timeval now;

   for (;;) {
      pthread_testcancel();
      if (ao2_container_count(cache) == 0) {
         nanosleep(&forever, NULL);
      }
      pthread_testcancel();
      now = ast_tvnow();
      ao2_callback(cache, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, purge_old_fn, &now);
      pthread_testcancel();
      nanosleep(&one_second, NULL);
   }

   return NULL;
}
static struct ast_variable* dup_vars ( struct ast_variable v) [static, read]

Definition at line 97 of file pbx_realtime.c.

References ast_variable::next, ast_variable_new(), ast_variable::name, ast_variable::value, ast_variable::file, and ast_variables_destroy().

Referenced by realtime_common().

{
   struct ast_variable *new, *list = NULL;
   for (; v; v = v->next) {
      if (!(new = ast_variable_new(v->name, v->value, v->file))) {
         ast_variables_destroy(list);
         return NULL;
      }
      /* Reversed list in cache, but when we duplicate out of the cache,
       * it's back to correct order. */
      new->next = list;
      list = new;
   }
   return list;
}
static void free_entry ( void *  obj) [static]

Definition at line 113 of file pbx_realtime.c.

References ast_variables_destroy(), and cache_entry::var.

Referenced by realtime_common().

{
   struct cache_entry *e = obj;
   ast_variables_destroy(e->var);
}
static int purge_old_fn ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 119 of file pbx_realtime.c.

References ast_tvdiff_ms(), cache_entry::when, and CMP_MATCH.

Referenced by cleanup().

{
   struct cache_entry *e = obj;
   struct timeval *now = arg;
   return ast_tvdiff_ms(*now, e->when) >= 1000 ? CMP_MATCH : 0;
}
static int realtime_canmatch ( struct ast_channel chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Definition at line 292 of file pbx_realtime.c.

References realtime_common(), MODE_CANMATCH, and ast_variables_destroy().

{
   struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_CANMATCH);
   if (var) {
      ast_variables_destroy(var);
      return 1;
   }
   return 0;
}
static struct ast_variable* realtime_common ( const char *  context,
const char *  exten,
int  priority,
const char *  data,
int  mode 
) [static, read]

Definition at line 220 of file pbx_realtime.c.

References table, var, AST_MAX_EXTENSION, cache_entry::priority, ast_strdupa, S_OR, ast_strlen_zero(), ast_app_parse_options(), switch_opts, ast_copy_string(), MODE_MATCH, ao2_find, OBJ_POINTER, dup_vars(), cache_entry::var, ao2_ref, realtime_switch_common(), ao2_alloc, free_entry(), ast_variables_destroy(), cache_entry::context, cache_entry::exten, cache_entry::when, ast_tvnow(), and ao2_link.

Referenced by realtime_exists(), realtime_canmatch(), realtime_exec(), and realtime_matchmore().

{
   const char *ctx = NULL;
   char *table;
   struct ast_variable *var=NULL;
   struct ast_flags flags = { 0, };
   struct cache_entry *ce;
   struct {
      struct cache_entry ce;
      char exten[AST_MAX_EXTENSION];
   } cache_search = { { .priority = priority, .context = (char *) context }, };
   char *buf = ast_strdupa(data);
   if (buf) {
      /* "Realtime" prefix is stripped off in the parent engine.  The
       * remaining string is: [[context@]table][/opts] */
      char *opts = strchr(buf, '/');
      if (opts)
         *opts++ = '\0';
      table = strchr(buf, '@');
      if (table) {
         *table++ = '\0';
         ctx = buf;
      }
      ctx = S_OR(ctx, context);
      table = S_OR(table, "extensions");
      if (!ast_strlen_zero(opts)) {
         ast_app_parse_options(switch_opts, &flags, NULL, opts);
      }
      ast_copy_string(cache_search.exten, exten, sizeof(cache_search.exten));
      if (mode == MODE_MATCH && (ce = ao2_find(cache, &cache_search, OBJ_POINTER))) {
         var = dup_vars(ce->var);
         ao2_ref(ce, -1);
      } else {
         var = realtime_switch_common(table, ctx, exten, priority, mode, flags);
         do {
            struct ast_variable *new;
            /* Only cache matches */
            if (mode != MODE_MATCH) {
               break;
            }
            if (!(new = dup_vars(var))) {
               break;
            }
            if (!(ce = ao2_alloc(sizeof(*ce) + strlen(exten) + strlen(context), free_entry))) {
               ast_variables_destroy(new);
               break;
            }
            ce->context = ce->exten + strlen(exten) + 1;
            strcpy(ce->exten, exten); /* SAFE */
            strcpy(ce->context, context); /* SAFE */
            ce->priority = priority;
            ce->var = new;
            ce->when = ast_tvnow();
            ao2_link(cache, ce);
            pthread_kill(cleanup_thread, SIGURG);
            ao2_ref(ce, -1);
         } while (0);
      }
   }
   return var;
}
static int realtime_exec ( struct ast_channel chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Definition at line 302 of file pbx_realtime.c.

References realtime_common(), MODE_MATCH, app, ast_variable::next, ast_variable::name, ast_strdupa, ast_variable::value, ast_compat_pbx_realtime, ast_variables_destroy(), ast_strlen_zero(), pbx_findapp(), EXT_DATA_SIZE, pbx_substitute_variables_helper(), ast_verb, ast_channel::exten, ast_channel::context, ast_channel::priority, term_color(), COLOR_BRCYAN, ast_channel::name, COLOR_BRMAGENTA, S_OR, manager_event, EVENT_FLAG_DIALPLAN, ast_channel::uniqueid, pbx_exec(), ast_log(), LOG_NOTICE, and LOG_WARNING.

{
   int res = -1;
   struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);

   if (var) {
      char *tmp="";
      char *app = NULL;
      struct ast_variable *v;

      for (v = var; v ; v = v->next) {
         if (!strcasecmp(v->name, "app"))
            app = ast_strdupa(v->value);
         else if (!strcasecmp(v->name, "appdata")) {
            if (ast_compat_pbx_realtime) {
               char *ptr;
               int in = 0;
               tmp = alloca(strlen(v->value) * 2 + 1);
               for (ptr = tmp; *v->value; v->value++) {
                  if (*v->value == ',') {
                     *ptr++ = '\\';
                     *ptr++ = ',';
                  } else if (*v->value == '|' && !in) {
                     *ptr++ = ',';
                  } else {
                     *ptr++ = *v->value;
                  }

                  /* Don't escape '|', meaning 'or', inside expressions ($[ ]) */
                  if (v->value[0] == '[' && v->value[-1] == '$') {
                     in++;
                  } else if (v->value[0] == ']' && in) {
                     in--;
                  }
               }
               *ptr = '\0';
            } else {
               tmp = ast_strdupa(v->value);
            }
         }
      }
      ast_variables_destroy(var);
      if (!ast_strlen_zero(app)) {
         struct ast_app *a = pbx_findapp(app);
         if (a) {
            char appdata[512];
            char tmp1[80];
            char tmp2[80];
            char tmp3[EXT_DATA_SIZE];

            appdata[0] = 0; /* just in case the substitute var func isn't called */
            if(!ast_strlen_zero(tmp))
               pbx_substitute_variables_helper(chan, tmp, appdata, sizeof(appdata) - 1);
            ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n",
                  chan->exten, chan->context, chan->priority,
                   term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
                   term_color(tmp2, chan->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
                   term_color(tmp3, S_OR(appdata, ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
            manager_event(EVENT_FLAG_DIALPLAN, "Newexten",
                       "Channel: %s\r\n"
                       "Context: %s\r\n"
                       "Extension: %s\r\n"
                       "Priority: %d\r\n"
                       "Application: %s\r\n"
                       "AppData: %s\r\n"
                       "Uniqueid: %s\r\n",
                       chan->name, chan->context, chan->exten, chan->priority, app, !ast_strlen_zero(appdata) ? appdata : "(NULL)", chan->uniqueid);
            
            res = pbx_exec(chan, a, appdata);
         } else
            ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
      } else {
         ast_log(LOG_WARNING, "No application specified for realtime extension '%s' in context '%s'\n", exten, context);
      }
   }
   return res;
}
static int realtime_exists ( struct ast_channel chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Definition at line 282 of file pbx_realtime.c.

References realtime_common(), MODE_MATCH, and ast_variables_destroy().

{
   struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
   if (var) {
      ast_variables_destroy(var);
      return 1;
   }
   return 0;
}
static int realtime_matchmore ( struct ast_channel chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Definition at line 380 of file pbx_realtime.c.

References realtime_common(), MODE_MATCHMORE, and ast_variables_destroy().

{
   struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCHMORE);
   if (var) {
      ast_variables_destroy(var);
      return 1;
   }
   return 0;
}
static struct ast_variable* realtime_switch_common ( const char *  table,
const char *  context,
const char *  exten,
int  priority,
int  mode,
struct ast_flags  flags 
) [static, read]

Definition at line 161 of file pbx_realtime.c.

References var, AST_MAX_EXTENSION, match(), MODE_MATCHMORE, MODE_CANMATCH, MODE_MATCH, ast_copy_string(), ast_load_realtime(), SENTINEL, ast_test_flag, OPTION_PATTERNS_DISABLED, ast_load_realtime_multientry(), ast_category_browse(), ast_extension_close(), ast_extension_match(), ast_category_detach_variables(), ast_category_get(), and ast_config_destroy().

Referenced by realtime_common().

{
   struct ast_variable *var;
   struct ast_config *cfg;
   char pri[20];
   char *ematch;
   char rexten[AST_MAX_EXTENSION + 20]="";
   int match;
   /* Optimization: since we don't support hints in realtime, it's silly to
    * query for a hint here, since we won't actually do anything with it.
    * This just wastes CPU time and resources. */
   if (priority < 0) {
      return NULL;
   }
   snprintf(pri, sizeof(pri), "%d", priority);
   switch(mode) {
   case MODE_MATCHMORE:
      ematch = "exten LIKE";
      snprintf(rexten, sizeof(rexten), "%s_%%", exten);
      break;
   case MODE_CANMATCH:
      ematch = "exten LIKE";
      snprintf(rexten, sizeof(rexten), "%s%%", exten);
      break;
   case MODE_MATCH:
   default:
      ematch = "exten";
      ast_copy_string(rexten, exten, sizeof(rexten));
   }
   var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, SENTINEL);
   if (!var && !ast_test_flag(&flags, OPTION_PATTERNS_DISABLED)) {
      cfg = ast_load_realtime_multientry(table, "exten LIKE", "\\_%", "context", context, "priority", pri, SENTINEL);   
      if (cfg) {
         char *cat = ast_category_browse(cfg, NULL);

         while(cat) {
            switch(mode) {
            case MODE_MATCHMORE:
               match = ast_extension_close(cat, exten, 1);
               break;
            case MODE_CANMATCH:
               match = ast_extension_close(cat, exten, 0);
               break;
            case MODE_MATCH:
            default:
               match = ast_extension_match(cat, exten);
            }
            if (match) {
               var = ast_category_detach_variables(ast_category_get(cfg, cat));
               break;
            }
            cat = ast_category_browse(cfg, cat);
         }
         ast_config_destroy(cfg);
      }
   }
   return var;
}
static int unload_module ( void  ) [static]

Definition at line 400 of file pbx_realtime.c.

References ast_unregister_switch(), and ao2_ref.

{
   ast_unregister_switch(&realtime_switch);
   pthread_cancel(cleanup_thread);
   pthread_kill(cleanup_thread, SIGURG);
   pthread_join(cleanup_thread, NULL);
   /* Destroy all remaining entries */
   ao2_ref(cache, -1);
   return 0;
}

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime Switch" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "7a1b8b48c852d7a7061c7e499b9bd0d2" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 426 of file pbx_realtime.c.

Definition at line 426 of file pbx_realtime.c.

Definition at line 79 of file pbx_realtime.c.

pthread_t cleanup_thread = 0

Definition at line 80 of file pbx_realtime.c.

struct ast_switch realtime_switch [static]

Definition at line 390 of file pbx_realtime.c.

struct ast_app_option switch_opts[128] = { [ 'p' ] = { .flag = OPTION_PATTERNS_DISABLED },} [static]

Definition at line 69 of file pbx_realtime.c.

Referenced by realtime_common().