Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function srv_query_function
 
static const struct ast_datastore_info srv_result_datastore_info
 
static struct ast_custom_function srv_result_function
 

Detailed Description

SRV Functions.

Author
Mark Michelson mmich.nosp@m.elso.nosp@m.n@dig.nosp@m.ium..nosp@m.com

Definition in file func_srv.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 279 of file func_srv.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 279 of file func_srv.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 279 of file func_srv.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 265 of file func_srv.c.

References ast_custom_function_register, AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

266 {
268  if (res < 0) {
270  }
272  if (res < 0) {
274  }
275 
276  return AST_MODULE_LOAD_SUCCESS;;
277 }
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_custom_function srv_result_function
Definition: func_srv.c:250
static struct ast_custom_function srv_query_function
Definition: func_srv.c:163
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ srds_destroy_cb()

static void srds_destroy_cb ( void *  data)
static

Definition at line 81 of file func_srv.c.

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

82 {
83  struct srv_result_datastore *datastore = data;
84  ast_srv_cleanup(&datastore->context);
85  ast_free(datastore);
86 }
struct srv_context * context
Definition: func_srv.c:77
void ast_srv_cleanup(struct srv_context **context)
Cleanup resources associated with ast_srv_lookup.
Definition: srv.c:248
#define ast_free(a)
Definition: astmm.h:182

◆ srv_datastore_setup()

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

Definition at line 93 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, host, srv_result_datastore::id, LOG_NOTICE, and NULL.

Referenced by srv_query_read(), and srv_result_read().

94 {
95  struct srv_result_datastore *srds;
96  struct ast_datastore *datastore;
97  const char *host;
98  unsigned short port;
99 
100  if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
101  return NULL;
102  }
103 
104  ast_autoservice_start(chan);
105  if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
106  ast_autoservice_stop(chan);
107  ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
108  ast_free(srds);
109  return NULL;
110  }
111  ast_autoservice_stop(chan);
112 
113  strcpy(srds->id, service);
114 
115  if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
116  ast_srv_cleanup(&srds->context);
117  ast_free(srds);
118  return NULL;
119  }
120 
121  datastore->data = srds;
122  ast_channel_lock(chan);
123  ast_channel_datastore_add(chan, datastore);
124  ast_channel_unlock(chan);
125  return srds->context;
126 }
struct srv_context * context
Definition: func_srv.c:77
#define ast_channel_lock(chan)
Definition: channel.h:2945
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
void ast_srv_cleanup(struct srv_context **context)
Cleanup resources associated with ast_srv_lookup.
Definition: srv.c:248
enum ast_cc_service_type service
Definition: chan_sip.c:949
Structure for a data store object.
Definition: datastore.h:68
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
static char host[256]
Definition: muted.c:77
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
#define LOG_NOTICE
Definition: logger.h:263
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
int ast_srv_lookup(struct srv_context **context, const char *service, const char **host, unsigned short *port)
Retrieve set of SRV lookups, in order.
Definition: srv.c:202
void * data
Definition: datastore.h:70
static const struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:88
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390

◆ srv_query_read()

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

Definition at line 128 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().

129 {
130  struct ast_datastore *datastore;
131 
132  if (!chan) {
133  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
134  return -1;
135  }
136 
137  if (ast_strlen_zero(data)) {
138  ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
139  return -1;
140  }
141 
142  /* If they already called SRVQUERY for this service once,
143  * we need to kill the old datastore.
144  */
145  ast_channel_lock(chan);
147  ast_channel_unlock(chan);
148 
149  if (datastore) {
150  ast_channel_datastore_remove(chan, datastore);
151  ast_datastore_free(datastore);
152  }
153 
154  if (!srv_datastore_setup(data, chan)) {
155  return -1;
156  }
157 
159 
160  return 0;
161 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
Structure for a data store object.
Definition: datastore.h:68
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
static struct srv_context * srv_datastore_setup(const char *service, struct ast_channel *chan)
Definition: func_srv.c:93
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void * data
Definition: datastore.h:70
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static const struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:88
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2399

◆ srv_result_read()

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

Definition at line 168 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, host, LOG_ERROR, LOG_WARNING, parse(), priority, srv_datastore_setup(), and weight.

169 {
170  struct srv_result_datastore *srds;
171  struct ast_datastore *datastore;
172  struct srv_context *srv_context;
173  char *parse;
174  const char *host;
175  unsigned short port, priority, weight;
176  unsigned int num;
178  AST_APP_ARG(id);
179  AST_APP_ARG(resultnum);
180  AST_APP_ARG(field);
181  );
182 
183  if (!chan) {
184  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
185  return -1;
186  }
187 
188  if (ast_strlen_zero(data)) {
189  ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
190  return -1;
191  }
192 
193  parse = ast_strdupa(data);
194 
195  AST_STANDARD_APP_ARGS(args, parse);
196 
197  ast_channel_lock(chan);
199  ast_channel_unlock(chan);
200 
201  if (!datastore) {
202  /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
203  * No problem, we'll do the SRV lookup now.
204  */
205  srv_context = srv_datastore_setup(args.id, chan);
206  if (!srv_context) {
207  return -1;
208  }
209  } else {
210  srds = datastore->data;
211  srv_context = srds->context;
212  }
213 
214  if (!strcasecmp(args.resultnum, "getnum")) {
215  snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
216  return 0;
217  }
218 
219  if (ast_strlen_zero(args.field)) {
220  ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
221  return -1;
222  }
223 
224  if (sscanf(args.resultnum, "%30u", &num) != 1) {
225  ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
226  return -1;
227  }
228 
229  if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
230  ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
231  return -1;
232  }
233 
234  if (!strcasecmp(args.field, "host")) {
235  ast_copy_string(buf, host, len);
236  } else if (!strcasecmp(args.field, "port")) {
237  snprintf(buf, len, "%d", port);
238  } else if (!strcasecmp(args.field, "priority")) {
239  snprintf(buf, len, "%d", priority);
240  } else if (!strcasecmp(args.field, "weight")) {
241  snprintf(buf, len, "%d", weight);
242  } else {
243  ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
244  return -1;
245  }
246 
247  return 0;
248 }
struct srv_context * context
Definition: func_srv.c:77
#define ast_channel_lock(chan)
Definition: channel.h:2945
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
Structure for a data store object.
Definition: datastore.h:68
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
static struct srv_context * srv_datastore_setup(const char *service, struct ast_channel *chan)
Definition: func_srv.c:93
const char * args
static int priority
#define ast_strlen_zero(foo)
Definition: strings.h:52
int ast_srv_get_nth_record(struct srv_context *context, int record_num, const char **host, unsigned short *port, unsigned short *priority, unsigned short *weight)
Retrieve details from a specific SRV record.
Definition: srv.c:309
#define ast_log
Definition: astobj2.c:42
static char host[256]
Definition: muted.c:77
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define LOG_ERROR
Definition: logger.h:285
char weight
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
unsigned int ast_srv_get_record_count(struct srv_context *context)
Get the number of records for a given SRV context.
Definition: srv.c:304
void * data
Definition: datastore.h:70
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static const struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:88
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 255 of file func_srv.c.

References ast_custom_function_unregister().

256 {
257  int res = 0;
258 
261 
262  return res;
263 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function srv_result_function
Definition: func_srv.c:250
static struct ast_custom_function srv_query_function
Definition: func_srv.c:163

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 279 of file func_srv.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 279 of file func_srv.c.

◆ srv_query_function

struct ast_custom_function srv_query_function
static
Initial value:
= {
.name = "SRVQUERY",
.read = srv_query_read,
}
static int srv_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_srv.c:128

Definition at line 163 of file func_srv.c.

◆ srv_result_datastore_info

const struct ast_datastore_info srv_result_datastore_info
static
Initial value:
= {
.type = "SRVQUERY",
.destroy = srds_destroy_cb,
}
static void srds_destroy_cb(void *data)
Definition: func_srv.c:81

Definition at line 88 of file func_srv.c.

◆ srv_result_function

struct ast_custom_function srv_result_function
static
Initial value:
= {
.name = "SRVRESULT",
.read = srv_result_read,
}
static int srv_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_srv.c:168

Definition at line 250 of file func_srv.c.