Mon Mar 12 2012 21:40:01

Asterisk developer's documentation


func_srv.c File Reference

SRV Functions. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/srv.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/datastore.h"
#include "asterisk/channel.h"
Include dependency graph for func_srv.c:

Go to the source code of this file.

Data Structures

struct  srv_result_datastore

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int load_module (void)
static void srds_destroy_cb (void *data)
static struct srv_contextsrv_datastore_setup (const char *service, struct ast_channel *chan)
static int srv_query_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int srv_result_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SRV related dialplan functions" , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function srv_query_function
static struct ast_datastore_info srv_result_datastore_info
static struct ast_custom_function srv_result_function

Detailed Description

SRV Functions.

Author:
Mark Michelson <mmichelson@digium.com>

Definition in file func_srv.c.


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 281 of file func_srv.c.

static void __unreg_module ( void  ) [static]

Definition at line 281 of file func_srv.c.

static int load_module ( void  ) [static]
static void srds_destroy_cb ( void *  data) [static]

Definition at line 83 of file func_srv.c.

References ast_free, ast_srv_cleanup(), and srv_result_datastore::context.

{
   struct srv_result_datastore *datastore = data;
   ast_srv_cleanup(&datastore->context);
   ast_free(datastore);
}
static struct srv_context* srv_datastore_setup ( const char *  service,
struct ast_channel chan 
) [static, read]

Definition at line 95 of file func_srv.c.

References ast_autoservice_start(), ast_autoservice_stop(), ast_calloc, ast_channel_datastore_add(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc(), ast_free, ast_log(), ast_srv_cleanup(), ast_srv_lookup(), srv_result_datastore::context, ast_datastore::data, srv_result_datastore::id, and LOG_NOTICE.

Referenced by srv_query_read(), and srv_result_read().

{
   struct srv_result_datastore *srds;
   struct ast_datastore *datastore;
   const char *host;
   unsigned short port;

   if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
      return NULL;
   }

   ast_autoservice_start(chan);
   if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
      ast_autoservice_stop(chan);
      ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
      ast_free(srds);
      return NULL;
   }
   ast_autoservice_stop(chan);

   strcpy(srds->id, service);

   if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
      ast_srv_cleanup(&srds->context);
      ast_free(srds);
      return NULL;
   }

   datastore->data = srds;
   ast_channel_lock(chan);
   ast_channel_datastore_add(chan, datastore);
   ast_channel_unlock(chan);
   return srds->context;
}
static int srv_query_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 130 of file func_srv.c.

References ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_channel_lock, ast_channel_unlock, ast_copy_string(), ast_datastore_free(), ast_log(), ast_strlen_zero(), LOG_WARNING, and srv_datastore_setup().

{
   struct ast_datastore *datastore;

   if (!chan) {
      ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
      return -1;
   }

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
      return -1;
   }
   
   /* If they already called SRVQUERY for this service once,
    * we need to kill the old datastore.
    */
   ast_channel_lock(chan);
   datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
   ast_channel_unlock(chan);

   if (datastore) {
      ast_channel_datastore_remove(chan, datastore);
      ast_datastore_free(datastore);
   }
   
   if (!srv_datastore_setup(data, chan)) {
      return -1;
   }

   ast_copy_string(buf, data, len);

   return 0;
}
static int srv_result_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 170 of file func_srv.c.

References args, AST_APP_ARG, ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_log(), ast_srv_get_nth_record(), ast_srv_get_record_count(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), srv_result_datastore::context, ast_datastore::data, LOG_ERROR, LOG_WARNING, parse(), and srv_datastore_setup().

{
   struct srv_result_datastore *srds;
   struct ast_datastore *datastore;
   struct srv_context *srv_context;
   char *parse;
   const char *host;
   unsigned short port, priority, weight;
   unsigned int num;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(id);
      AST_APP_ARG(resultnum);
      AST_APP_ARG(field);
   );

   if (!chan) {
      ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
      return -1;
   }

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
      return -1;
   }

   parse = ast_strdupa(data);

   AST_STANDARD_APP_ARGS(args, parse);

   ast_channel_lock(chan);
   datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
   ast_channel_unlock(chan);

   if (!datastore) {
      /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
       * No problem, we'll do the SRV lookup now.
       */
      srv_context = srv_datastore_setup(args.id, chan);
      if (!srv_context) {
         return -1;
      }
   } else {
      srds = datastore->data;
      srv_context = srds->context;
   }

   if (!strcasecmp(args.resultnum, "getnum")) {
      snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
      return 0;
   }

   if (ast_strlen_zero(args.field)) {
      ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
      return -1;
   }

   if (sscanf(args.resultnum, "%30u", &num) != 1) {
      ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
      return -1;
   }

   if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
      ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
      return -1;
   }

   if (!strcasecmp(args.field, "host")) {
      ast_copy_string(buf, host, len);
   } else if (!strcasecmp(args.field, "port")) {
      snprintf(buf, len, "%u", port);
   } else if (!strcasecmp(args.field, "priority")) {
      snprintf(buf, len, "%u", priority);
   } else if (!strcasecmp(args.field, "weight")) {
      snprintf(buf, len, "%u", weight);
   } else {
      ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
      return -1;
   }

   return 0;
}
static int unload_module ( void  ) [static]

Definition at line 257 of file func_srv.c.

References ast_custom_function_unregister().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SRV related dialplan functions" , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 281 of file func_srv.c.

Definition at line 281 of file func_srv.c.

Initial value:
 {
   .name = "SRVQUERY",
   .read = srv_query_read,
}

Definition at line 165 of file func_srv.c.

Initial value:
 {
   .type = "SRVQUERY",
   .destroy = srds_destroy_cb,
}

Definition at line 90 of file func_srv.c.

Initial value:
 {
   .name = "SRVRESULT",
   .read = srv_result_read,
}

Definition at line 252 of file func_srv.c.