Wed Apr 6 11:30:05 2011

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"

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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .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 277 of file func_srv.c.

static void __unreg_module ( void   )  [static]

Definition at line 277 of file func_srv.c.

static int load_module ( void   )  [static]

Definition at line 263 of file func_srv.c.

References ast_custom_function_register, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, srv_query_function, and srv_result_function.

00264 {
00265    int res = ast_custom_function_register(&srv_query_function);
00266    if (res < 0) {
00267       return AST_MODULE_LOAD_DECLINE;
00268    }
00269    res = ast_custom_function_register(&srv_result_function);
00270    if (res < 0) {
00271       return AST_MODULE_LOAD_DECLINE;
00272    }
00273 
00274    return AST_MODULE_LOAD_SUCCESS;;
00275 }

static void srds_destroy_cb ( void *  data  )  [static]

Definition at line 79 of file func_srv.c.

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

00080 {
00081    struct srv_result_datastore *datastore = data;
00082    ast_srv_cleanup(&datastore->context);
00083    ast_free(datastore);
00084 }

static struct srv_context* srv_datastore_setup ( const char *  service,
struct ast_channel chan 
) [static]

Definition at line 91 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(), ast_datastore::data, LOG_NOTICE, and srv_result_datastore_info.

Referenced by srv_query_read(), and srv_result_read().

00092 {
00093    struct srv_result_datastore *srds;
00094    struct ast_datastore *datastore;
00095    const char *host;
00096    unsigned short port;
00097 
00098    if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
00099       return NULL;
00100    }
00101 
00102    ast_autoservice_start(chan);
00103    if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
00104       ast_autoservice_stop(chan);
00105       ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
00106       ast_free(srds);
00107       return NULL;
00108    }
00109    ast_autoservice_stop(chan);
00110 
00111    strcpy(srds->id, service);
00112 
00113    if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
00114       ast_srv_cleanup(&srds->context);
00115       ast_free(srds);
00116       return NULL;
00117    }
00118 
00119    datastore->data = srds;
00120    ast_channel_lock(chan);
00121    ast_channel_datastore_add(chan, datastore);
00122    ast_channel_unlock(chan);
00123    return srds->context;
00124 }

static int srv_query_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 126 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, srv_datastore_setup(), and srv_result_datastore_info.

00127 {
00128    struct ast_datastore *datastore;
00129 
00130    if (!chan) {
00131       ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
00132       return -1;
00133    }
00134 
00135    if (ast_strlen_zero(data)) {
00136       ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
00137       return -1;
00138    }
00139    
00140    /* If they already called SRVQUERY for this service once,
00141     * we need to kill the old datastore.
00142     */
00143    ast_channel_lock(chan);
00144    datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, data);
00145    ast_channel_unlock(chan);
00146 
00147    if (datastore) {
00148       ast_channel_datastore_remove(chan, datastore);
00149       ast_datastore_free(datastore);
00150    }
00151    
00152    if (!srv_datastore_setup(data, chan)) {
00153       return -1;
00154    }
00155 
00156    ast_copy_string(buf, data, len);
00157 
00158    return 0;
00159 }

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

Definition at line 166 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(), srv_datastore_setup(), and srv_result_datastore_info.

00167 {
00168    struct srv_result_datastore *srds;
00169    struct ast_datastore *datastore;
00170    struct srv_context *srv_context;
00171    char *parse;
00172    const char *host;
00173    unsigned short port, priority, weight;
00174    unsigned int num;
00175    AST_DECLARE_APP_ARGS(args,
00176       AST_APP_ARG(id);
00177       AST_APP_ARG(resultnum);
00178       AST_APP_ARG(field);
00179    );
00180 
00181    if (!chan) {
00182       ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
00183       return -1;
00184    }
00185 
00186    if (ast_strlen_zero(data)) {
00187       ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
00188       return -1;
00189    }
00190 
00191    parse = ast_strdupa(data);
00192 
00193    AST_STANDARD_APP_ARGS(args, parse);
00194 
00195    ast_channel_lock(chan);
00196    datastore = ast_channel_datastore_find(chan, &srv_result_datastore_info, args.id);
00197    ast_channel_unlock(chan);
00198 
00199    if (!datastore) {
00200       /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
00201        * No problem, we'll do the SRV lookup now.
00202        */
00203       srv_context = srv_datastore_setup(args.id, chan);
00204       if (!srv_context) {
00205          return -1;
00206       }
00207    } else {
00208       srds = datastore->data;
00209       srv_context = srds->context;
00210    }
00211 
00212    if (!strcasecmp(args.resultnum, "getnum")) {
00213       snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
00214       return 0;
00215    }
00216 
00217    if (ast_strlen_zero(args.field)) {
00218       ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
00219       return -1;
00220    }
00221 
00222    if (sscanf(args.resultnum, "%30u", &num) != 1) {
00223       ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
00224       return -1;
00225    }
00226 
00227    if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
00228       ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
00229       return -1;
00230    }
00231 
00232    if (!strcasecmp(args.field, "host")) {
00233       ast_copy_string(buf, host, len);
00234    } else if (!strcasecmp(args.field, "port")) {
00235       snprintf(buf, len, "%u", port);
00236    } else if (!strcasecmp(args.field, "priority")) {
00237       snprintf(buf, len, "%u", priority);
00238    } else if (!strcasecmp(args.field, "weight")) {
00239       snprintf(buf, len, "%u", weight);
00240    } else {
00241       ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
00242       return -1;
00243    }
00244 
00245    return 0;
00246 }

static int unload_module ( void   )  [static]

Definition at line 253 of file func_srv.c.

References ast_custom_function_unregister(), srv_query_function, and srv_result_function.

00254 {
00255    int res = 0;
00256 
00257    res |= ast_custom_function_unregister(&srv_query_function);
00258    res |= ast_custom_function_unregister(&srv_result_function);
00259 
00260    return res;
00261 }


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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 277 of file func_srv.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 277 of file func_srv.c.

struct ast_custom_function srv_query_function [static]

Initial value:

 {
   .name = "SRVQUERY",
   .read = srv_query_read,
}

Definition at line 161 of file func_srv.c.

Referenced by load_module(), and unload_module().

struct ast_datastore_info srv_result_datastore_info [static]

Initial value:

 {
   .type = "SRVQUERY",
   .destroy = srds_destroy_cb,
}

Definition at line 86 of file func_srv.c.

Referenced by srv_datastore_setup(), srv_query_read(), and srv_result_read().

struct ast_custom_function srv_result_function [static]

Initial value:

 {
   .name = "SRVRESULT",
   .read = srv_result_read,
}

Definition at line 248 of file func_srv.c.

Referenced by load_module(), and unload_module().


Generated on Wed Apr 6 11:30:05 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7