Wed Jan 8 2020 09:50:13

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 = "ac1f6a56484a8820659555499174e588" , .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 mmich.nosp@m.elso.nosp@m.n@dig.nosp@m.ium..nosp@m.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

Definition at line 267 of file func_srv.c.

References ast_custom_function_register, AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

268 {
270  if (res < 0) {
272  }
274  if (res < 0) {
276  }
277 
278  return AST_MODULE_LOAD_SUCCESS;;
279 }
static struct ast_custom_function srv_result_function
Definition: func_srv.c:252
static struct ast_custom_function srv_query_function
Definition: func_srv.c:165
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
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.

84 {
85  struct srv_result_datastore *datastore = data;
86  ast_srv_cleanup(&datastore->context);
87  ast_free(datastore);
88 }
struct srv_context * context
Definition: func_srv.c:79
void ast_srv_cleanup(struct srv_context **context)
Cleanup resources associated with ast_srv_lookup.
Definition: srv.c:251
#define ast_free(a)
Definition: astmm.h:97
static struct srv_context* srv_datastore_setup ( const char *  service,
struct ast_channel chan 
)
static

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

96 {
97  struct srv_result_datastore *srds;
98  struct ast_datastore *datastore;
99  const char *host;
100  unsigned short port;
101 
102  if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
103  return NULL;
104  }
105 
106  ast_autoservice_start(chan);
107  if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
108  ast_autoservice_stop(chan);
109  ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
110  ast_free(srds);
111  return NULL;
112  }
113  ast_autoservice_stop(chan);
114 
115  strcpy(srds->id, service);
116 
117  if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
118  ast_srv_cleanup(&srds->context);
119  ast_free(srds);
120  return NULL;
121  }
122 
123  datastore->data = srds;
124  ast_channel_lock(chan);
125  ast_channel_datastore_add(chan, datastore);
126  ast_channel_unlock(chan);
127  return srds->context;
128 }
struct srv_context * context
Definition: func_srv.c:79
#define ast_channel_lock(chan)
Definition: channel.h:2466
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:179
void ast_srv_cleanup(struct srv_context **context)
Cleanup resources associated with ast_srv_lookup.
Definition: srv.c:251
enum ast_cc_service_type service
Definition: chan_sip.c:821
Structure for a data store object.
Definition: datastore.h:54
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:238
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_datastore * ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
Definition: datastore.c:98
#define LOG_NOTICE
Definition: logger.h:133
#define ast_channel_unlock(chan)
Definition: channel.h:2467
#define ast_free(a)
Definition: astmm.h:97
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:206
void * data
Definition: datastore.h:56
#define ast_calloc(a, b)
Definition: astmm.h:82
static struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:90
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2590
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().

131 {
132  struct ast_datastore *datastore;
133 
134  if (!chan) {
135  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
136  return -1;
137  }
138 
139  if (ast_strlen_zero(data)) {
140  ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
141  return -1;
142  }
143 
144  /* If they already called SRVQUERY for this service once,
145  * we need to kill the old datastore.
146  */
147  ast_channel_lock(chan);
149  ast_channel_unlock(chan);
150 
151  if (datastore) {
152  ast_channel_datastore_remove(chan, datastore);
153  ast_datastore_free(datastore);
154  }
155 
156  if (!srv_datastore_setup(data, chan)) {
157  return -1;
158  }
159 
160  ast_copy_string(buf, data, len);
161 
162  return 0;
163 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define LOG_WARNING
Definition: logger.h:144
Structure for a data store object.
Definition: datastore.h:54
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:2604
static struct srv_context * srv_datastore_setup(const char *service, struct ast_channel *chan)
Definition: func_srv.c:95
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:65
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_channel_unlock(chan)
Definition: channel.h:2467
void * data
Definition: datastore.h:56
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:90
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2599
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().

171 {
172  struct srv_result_datastore *srds;
173  struct ast_datastore *datastore;
174  struct srv_context *srv_context;
175  char *parse;
176  const char *host;
177  unsigned short port, priority, weight;
178  unsigned int num;
180  AST_APP_ARG(id);
181  AST_APP_ARG(resultnum);
182  AST_APP_ARG(field);
183  );
184 
185  if (!chan) {
186  ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
187  return -1;
188  }
189 
190  if (ast_strlen_zero(data)) {
191  ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
192  return -1;
193  }
194 
195  parse = ast_strdupa(data);
196 
197  AST_STANDARD_APP_ARGS(args, parse);
198 
199  ast_channel_lock(chan);
201  ast_channel_unlock(chan);
202 
203  if (!datastore) {
204  /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
205  * No problem, we'll do the SRV lookup now.
206  */
207  srv_context = srv_datastore_setup(args.id, chan);
208  if (!srv_context) {
209  return -1;
210  }
211  } else {
212  srds = datastore->data;
213  srv_context = srds->context;
214  }
215 
216  if (!strcasecmp(args.resultnum, "getnum")) {
217  snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
218  return 0;
219  }
220 
221  if (ast_strlen_zero(args.field)) {
222  ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
223  return -1;
224  }
225 
226  if (sscanf(args.resultnum, "%30u", &num) != 1) {
227  ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
228  return -1;
229  }
230 
231  if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
232  ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
233  return -1;
234  }
235 
236  if (!strcasecmp(args.field, "host")) {
237  ast_copy_string(buf, host, len);
238  } else if (!strcasecmp(args.field, "port")) {
239  snprintf(buf, len, "%d", port);
240  } else if (!strcasecmp(args.field, "priority")) {
241  snprintf(buf, len, "%d", priority);
242  } else if (!strcasecmp(args.field, "weight")) {
243  snprintf(buf, len, "%d", weight);
244  } else {
245  ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
246  return -1;
247  }
248 
249  return 0;
250 }
struct srv_context * context
Definition: func_srv.c:79
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
Structure for a data store object.
Definition: datastore.h:54
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:2604
static struct srv_context * srv_datastore_setup(const char *service, struct ast_channel *chan)
Definition: func_srv.c:95
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:312
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_channel_unlock(chan)
Definition: channel.h:2467
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
unsigned int ast_srv_get_record_count(struct srv_context *context)
Get the number of records for a given SRV context.
Definition: srv.c:307
void * data
Definition: datastore.h:56
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
static struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:90
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
static int unload_module ( void  )
static

Definition at line 257 of file func_srv.c.

References ast_custom_function_unregister().

258 {
259  int res = 0;
260 
263 
264  return res;
265 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ast_custom_function srv_result_function
Definition: func_srv.c:252
static struct ast_custom_function srv_query_function
Definition: func_srv.c:165

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 = "ac1f6a56484a8820659555499174e588" , .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.

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:130

Definition at line 165 of file func_srv.c.

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:83

Definition at line 90 of file func_srv.c.

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:170

Definition at line 252 of file func_srv.c.