Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
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 = ASTERISK_GPL_KEY , .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 295 of file func_srv.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 295 of file func_srv.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 295 of file func_srv.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 281 of file func_srv.c.

282{
284 if (res < 0) {
286 }
288 if (res < 0) {
290 }
291
293}
static struct ast_custom_function srv_result_function
Definition func_srv.c:266
static struct ast_custom_function srv_query_function
Definition func_srv.c:179
@ 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:1562

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

98{
99 struct srv_result_datastore *datastore = data;
100 ast_srv_cleanup(&datastore->context);
101 ast_free(datastore);
102}
#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:93

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

110{
111 struct srv_result_datastore *srds;
112 struct ast_datastore *datastore;
113 const char *host;
114 unsigned short port;
115
116 if (!(srds = ast_calloc(1, sizeof(*srds) + strlen(service)))) {
117 return NULL;
118 }
119
121 if (ast_srv_lookup(&srds->context, service, &host, &port) < 0) {
123 ast_log(LOG_NOTICE, "Error performing lookup of service '%s'\n", service);
124 ast_free(srds);
125 return NULL;
126 }
128
129 strcpy(srds->id, service);
130
131 if (!(datastore = ast_datastore_alloc(&srv_result_datastore_info, srds->id))) {
132 ast_srv_cleanup(&srds->context);
133 ast_free(srds);
134 return NULL;
135 }
136
137 datastore->data = srds;
138 ast_channel_lock(chan);
139 ast_channel_datastore_add(chan, datastore);
140 ast_channel_unlock(chan);
141 return srds->context;
142}
#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:389
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition channel.c:2357
#define ast_channel_lock(chan)
Definition channel.h:2982
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
#define ast_channel_unlock(chan)
Definition channel.h:2983
#define ast_datastore_alloc(info, uid)
Definition datastore.h:85
static const struct ast_datastore_info srv_result_datastore_info
Definition func_srv.c:104
#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 144 of file func_srv.c.

145{
146 struct ast_datastore *datastore;
147
148 if (!chan) {
149 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
150 return -1;
151 }
152
153 if (ast_strlen_zero(data)) {
154 ast_log(LOG_WARNING, "%s requires a service as an argument\n", cmd);
155 return -1;
156 }
157
158 /* If they already called SRVQUERY for this service once,
159 * we need to kill the old datastore.
160 */
161 ast_channel_lock(chan);
163 ast_channel_unlock(chan);
164
165 if (datastore) {
166 ast_channel_datastore_remove(chan, datastore);
167 ast_datastore_free(datastore);
168 }
169
170 if (!srv_datastore_setup(data, chan)) {
171 return -1;
172 }
173
175
176 return 0;
177}
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition channel.c:2366
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:2371
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:109
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 184 of file func_srv.c.

185{
186 struct srv_result_datastore *srds;
187 struct ast_datastore *datastore;
188 struct srv_context *srv_context;
189 char *parse;
190 const char *host;
191 unsigned short port, priority, weight;
192 unsigned int num;
194 AST_APP_ARG(id);
195 AST_APP_ARG(resultnum);
196 AST_APP_ARG(field);
197 );
198
199 if (!chan) {
200 ast_log(LOG_WARNING, "%s cannot be used without a channel\n", cmd);
201 return -1;
202 }
203
204 if (ast_strlen_zero(data)) {
205 ast_log(LOG_WARNING, "%s requires two arguments (id and resultnum)\n", cmd);
206 return -1;
207 }
208
209 parse = ast_strdupa(data);
210
212
213 ast_channel_lock(chan);
215 ast_channel_unlock(chan);
216
217 if (!datastore) {
218 /* They apparently decided to call SRVRESULT without first calling SRVQUERY.
219 * No problem, we'll do the SRV lookup now.
220 */
222 if (!srv_context) {
223 return -1;
224 }
225 } else {
226 srds = datastore->data;
227 srv_context = srds->context;
228 }
229
230 if (!strcasecmp(args.resultnum, "getnum")) {
231 snprintf(buf, len, "%u", ast_srv_get_record_count(srv_context));
232 return 0;
233 }
234
235 if (ast_strlen_zero(args.field)) {
236 ast_log(LOG_ERROR, "A field must be provided when requesting SRV data\n");
237 return -1;
238 }
239
240 if (sscanf(args.resultnum, "%30u", &num) != 1) {
241 ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to %s\n", args.resultnum, cmd);
242 return -1;
243 }
244
245 if (ast_srv_get_nth_record(srv_context, num, &host, &port, &priority, &weight)) {
246 ast_log(LOG_ERROR, "Failed to get record number %u for %s\n", num, cmd);
247 return -1;
248 }
249
250 if (!strcasecmp(args.field, "host")) {
251 ast_copy_string(buf, host, len);
252 } else if (!strcasecmp(args.field, "port")) {
253 snprintf(buf, len, "%d", port);
254 } else if (!strcasecmp(args.field, "priority")) {
255 snprintf(buf, len, "%d", priority);
256 } else if (!strcasecmp(args.field, "weight")) {
257 snprintf(buf, len, "%d", weight);
258 } else {
259 ast_log(LOG_WARNING, "Unrecognized SRV field '%s'\n", args.field);
260 return -1;
261 }
262
263 return 0;
264}
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
static struct @519 args
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

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

272{
273 int res = 0;
274
277
278 return res;
279}
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 = ASTERISK_GPL_KEY , .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 295 of file func_srv.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 295 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:144

Definition at line 179 of file func_srv.c.

179 {
180 .name = "SRVQUERY",
181 .read = srv_query_read,
182};

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

Definition at line 104 of file func_srv.c.

104 {
105 .type = "SRVQUERY",
106 .destroy = srds_destroy_cb,
107};

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

Definition at line 266 of file func_srv.c.

266 {
267 .name = "SRVRESULT",
268 .read = srv_result_read,
269};

Referenced by load_module(), and unload_module().