Mon Aug 31 12:30:37 2015

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

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"SRV related dialplan functions")
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_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

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"SRV related dialplan functions"   
)
static int load_module ( void   )  [static]

Definition at line 267 of file func_srv.c.

References ast_custom_function_register, AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

00268 {
00269    int res = ast_custom_function_register(&srv_query_function);
00270    if (res < 0) {
00271       return AST_MODULE_LOAD_DECLINE;
00272    }
00273    res = ast_custom_function_register(&srv_result_function);
00274    if (res < 0) {
00275       return AST_MODULE_LOAD_DECLINE;
00276    }
00277 
00278    return AST_MODULE_LOAD_SUCCESS;;
00279 }

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.

00084 {
00085    struct srv_result_datastore *datastore = data;
00086    ast_srv_cleanup(&datastore->context);
00087    ast_free(datastore);
00088 }

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().

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

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().

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

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().

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

static int unload_module ( void   )  [static]

Definition at line 257 of file func_srv.c.

References ast_custom_function_unregister().

00258 {
00259    int res = 0;
00260 
00261    res |= ast_custom_function_unregister(&srv_query_function);
00262    res |= ast_custom_function_unregister(&srv_result_function);
00263 
00264    return res;
00265 }


Variable Documentation

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.


Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1