Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
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 = AST_BUILDOPT_SUM, .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 289 of file func_srv.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 289 of file func_srv.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 289 of file func_srv.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 275 of file func_srv.c.

276{
278 if (res < 0) {
280 }
282 if (res < 0) {
284 }
285
287}
static struct ast_custom_function srv_result_function
Definition: func_srv.c:260
static struct ast_custom_function srv_query_function
Definition: func_srv.c:173
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558

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

◆ srds_destroy_cb()

static void srds_destroy_cb ( void *  data)
static

Definition at line 91 of file func_srv.c.

92{
93 struct srv_result_datastore *datastore = data;
94 ast_srv_cleanup(&datastore->context);
95 ast_free(datastore);
96}
#define ast_free(a)
Definition: astmm.h:180
void ast_srv_cleanup(struct srv_context **context)
Cleanup resources associated with ast_srv_lookup.
Definition: srv.c:248
struct srv_context * context
Definition: func_srv.c:87

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

◆ srv_datastore_setup()

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

Definition at line 103 of file func_srv.c.

104{
105 struct srv_result_datastore *srds;
106 struct ast_datastore *datastore;
107 const char *host;
108 unsigned short port;
109
110 if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
111 return NULL;
112 }
113
115 if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
117 ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
118 ast_free(srds);
119 return NULL;
120 }
122
123 strcpy(srds->id, service);
124
125 if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
126 ast_srv_cleanup(&srds->context);
127 ast_free(srds);
128 return NULL;
129 }
130
131 datastore->data = srds;
132 ast_channel_lock(chan);
133 ast_channel_datastore_add(chan, datastore);
134 ast_channel_unlock(chan);
135 return srds->context;
136}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
enum ast_cc_service_type service
Definition: ccss.c:383
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2404
#define ast_channel_lock(chan)
Definition: channel.h:2968
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
#define ast_channel_unlock(chan)
Definition: channel.h:2969
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
static const struct ast_datastore_info srv_result_datastore_info
Definition: func_srv.c:98
#define LOG_NOTICE
#define NULL
Definition: resample.c:96
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
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66

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, LOG_NOTICE, NULL, service, and srv_result_datastore_info.

Referenced by srv_query_read(), and srv_result_read().

◆ 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 138 of file func_srv.c.

139{
140 struct ast_datastore *datastore;
141
142 if (!chan) {
143 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
144 return -1;
145 }
146
147 if (ast_strlen_zero(data)) {
148 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
149 return -1;
150 }
151
152 /* If they already called SRVQUERY for this service once,
153 * we need to kill the old datastore.
154 */
155 ast_channel_lock(chan);
157 ast_channel_unlock(chan);
158
159 if (datastore) {
160 ast_channel_datastore_remove(chan, datastore);
161 ast_datastore_free(datastore);
162 }
163
164 if (!srv_datastore_setup(data, chan)) {
165 return -1;
166 }
167
169
170 return 0;
171}
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2413
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:2418
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static struct srv_context * srv_datastore_setup(const char *service, struct ast_channel *chan)
Definition: func_srv.c:103
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define LOG_WARNING
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425

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(), buf, ast_datastore::data, len(), LOG_WARNING, srv_datastore_setup(), and srv_result_datastore_info.

◆ 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 178 of file func_srv.c.

179{
180 struct srv_result_datastore *srds;
181 struct ast_datastore *datastore;
182 struct srv_context *srv_context;
183 char *parse;
184 const char *host;
185 unsigned short port, priority, weight;
186 unsigned int num;
188 AST_APP_ARG(id);
189 AST_APP_ARG(resultnum);
190 AST_APP_ARG(field);
191 );
192
193 if (!chan) {
194 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
195 return -1;
196 }
197
198 if (ast_strlen_zero(data)) {
199 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
200 return -1;
201 }
202
203 parse = ast_strdupa(data);
204
206
207 ast_channel_lock(chan);
209 ast_channel_unlock(chan);
210
211 if (!datastore) {
212 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
213 * No problem, we'll do the SRV lookup now.
214 */
216 if (!srv_context) {
217 return -1;
218 }
219 } else {
220 srds = datastore->data;
221 srv_context = srds->context;
222 }
223
224 if (!strcasecmp(args.resultnum, "getnum")) {
225 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
226 return 0;
227 }
228
229 if (ast_strlen_zero(args.field)) {
230 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
231 return -1;
232 }
233
234 if (sscanf(args.resultnum, "%30u", &num) != 1) {
235 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
236 return -1;
237 }
238
239 if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
240 ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
241 return -1;
242 }
243
244 if (!strcasecmp(args.field, "host")) {
245 ast_copy_string(buf, host, len);
246 } else if (!strcasecmp(args.field, "port")) {
247 snprintf(buf, len, "%d", port);
248 } else if (!strcasecmp(args.field, "priority")) {
249 snprintf(buf, len, "%d", priority);
250 } else if (!strcasecmp(args.field, "weight")) {
251 snprintf(buf, len, "%d", weight);
252 } else {
253 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
254 return -1;
255 }
256
257 return 0;
258}
char weight
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
static int priority
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_ERROR
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
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
const char * args

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(), buf, srv_result_datastore::context, ast_datastore::data, len(), LOG_ERROR, LOG_WARNING, priority, srv_datastore_setup(), srv_result_datastore_info, and weight.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 265 of file func_srv.c.

266{
267 int res = 0;
268
271
272 return res;
273}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), srv_query_function, and srv_result_function.

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

Definition at line 289 of file func_srv.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 289 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:138

Definition at line 173 of file func_srv.c.

Referenced by load_module(), and unload_module().

◆ 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:91

Definition at line 98 of file func_srv.c.

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

◆ 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:178

Definition at line 260 of file func_srv.c.

Referenced by load_module(), and unload_module().