Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Functions | Variables
func_realtime.c File Reference

REALTIME dialplan function. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
Include dependency graph for func_realtime.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_THREADSTORAGE_CUSTOM_SCOPE (buf1, NULL, ast_free_ptr, static)
 
 AST_THREADSTORAGE_CUSTOM_SCOPE (buf2, NULL, ast_free_ptr, static)
 
 AST_THREADSTORAGE_CUSTOM_SCOPE (buf3, NULL, ast_free_ptr, static)
 
static int function_realtime_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int function_realtime_readdestroy (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int function_realtime_store (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int function_realtime_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int function_realtime_writedestroy (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 Wrapper to execute REALTIME_DESTROY from a write operation. Allows execution even if live_dangerously is disabled.
 
static int load_module (void)
 
static int realtimefield_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 = "Read/Write/Store/Destroy values from a RealTime repository" , .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 realtime_destroy_function
 
static struct ast_custom_function realtime_function
 
static struct ast_custom_function realtime_store_function
 
static struct ast_custom_function realtimefield_function
 
static struct ast_custom_function realtimehash_function
 

Detailed Description

REALTIME dialplan function.

Author
BJ Weschke bwesc.nosp@m.hke@.nosp@m.btwte.nosp@m.ch.c.nosp@m.om

Definition in file func_realtime.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 562 of file func_realtime.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 562 of file func_realtime.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 562 of file func_realtime.c.

◆ AST_THREADSTORAGE_CUSTOM_SCOPE() [1/3]

AST_THREADSTORAGE_CUSTOM_SCOPE ( buf1  ,
NULL  ,
ast_free_ptr  ,
static   
)

◆ AST_THREADSTORAGE_CUSTOM_SCOPE() [2/3]

AST_THREADSTORAGE_CUSTOM_SCOPE ( buf2  ,
NULL  ,
ast_free_ptr  ,
static   
)

◆ AST_THREADSTORAGE_CUSTOM_SCOPE() [3/3]

AST_THREADSTORAGE_CUSTOM_SCOPE ( buf3  ,
NULL  ,
ast_free_ptr  ,
static   
)

◆ function_realtime_read()

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

Definition at line 197 of file func_realtime.c.

198{
199 struct ast_variable *var, *head;
200 struct ast_str *out;
201 size_t resultslen;
202 int n;
204 AST_APP_ARG(family);
205 AST_APP_ARG(fieldmatch);
207 AST_APP_ARG(delim1);
208 AST_APP_ARG(delim2);
209 );
210
211 if (ast_strlen_zero(data)) {
212 ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
213 return -1;
214 }
215
217
218 if (!args.delim1)
219 args.delim1 = ",";
220 if (!args.delim2)
221 args.delim2 = "=";
222
223 if (chan)
225
226 head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
227
228 if (!head) {
229 if (chan)
231 return -1;
232 }
233
234 resultslen = 0;
235 n = 0;
236 for (var = head; var; n++, var = var->next)
237 resultslen += strlen(var->name) + strlen(var->value);
238 /* add space for delimiters and final '\0' */
239 resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
240
241 if (resultslen > len) {
242 ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
243 if (chan) {
245 }
246 return -1;
247 }
248
249 /* len is going to be sensible, so we don't need to check for stack
250 * overflows here. */
251 out = ast_str_alloca(resultslen);
252 for (var = head; var; var = var->next)
253 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
255
257
258 if (chan)
260
261 return 0;
262}
#define var
Definition ast_expr2f.c:605
#define ast_log
Definition astobj2.c:42
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
#define SENTINEL
Definition compiler.h:87
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#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.
struct ast_variable * ast_load_realtime_all(const char *family,...) attribute_sentinel
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
#define LOG_WARNING
static struct @519 args
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition strings.h:1139
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
#define ast_str_alloca(init_len)
Definition strings.h:848
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
Support for dynamic strings.
Definition strings.h:623
Structure for variables, used for configurations and for channel variables.
int value
Definition syslog.c:37
FILE * out
Definition utils/frame.c:33

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), AST_DECLARE_APP_ARGS, ast_load_realtime_all(), ast_log, AST_STANDARD_APP_ARGS, ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_strlen_zero(), ast_variables_destroy(), buf, len(), LOG_WARNING, ast_variable::next, out, SENTINEL, value, and var.

◆ function_realtime_readdestroy()

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

Definition at line 427 of file func_realtime.c.

428{
429 struct ast_variable *var, *head;
430 struct ast_str *out;
431 size_t resultslen;
432 int n;
434 AST_APP_ARG(family);
435 AST_APP_ARG(fieldmatch);
437 AST_APP_ARG(delim1);
438 AST_APP_ARG(delim2);
439 );
440
441 if (ast_strlen_zero(data)) {
442 ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
443 return -1;
444 }
445
447
448 if (!args.delim1)
449 args.delim1 = ",";
450 if (!args.delim2)
451 args.delim2 = "=";
452
453 if (chan)
455
456 head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
457
458 if (!head) {
459 if (chan)
461 return -1;
462 }
463
464 if (len > 0) {
465 resultslen = 0;
466 n = 0;
467 for (var = head; var; n++, var = var->next) {
468 resultslen += strlen(var->name) + strlen(var->value);
469 }
470 /* add space for delimiters and final '\0' */
471 resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
472
473 if (resultslen > len) {
474 /* Unfortunately this does mean that we cannot destroy
475 * the row anymore. But OTOH, we're not destroying
476 * someones data without giving him the chance to look
477 * at it. */
478 ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
479 if (chan) {
481 }
482 return -1;
483 }
484
485 /* len is going to be sensible, so we don't need to check for
486 * stack overflows here. */
487 out = ast_str_alloca(resultslen);
488 for (var = head; var; var = var->next) {
489 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
490 }
492 }
493
494 ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
496
497 if (chan)
499
500 return 0;
501}
int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Destroy realtime configuration.

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), AST_DECLARE_APP_ARGS, ast_destroy_realtime(), ast_load_realtime_all(), ast_log, AST_STANDARD_APP_ARGS, ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_strlen_zero(), ast_variables_destroy(), buf, len(), LOG_WARNING, ast_variable::next, out, SENTINEL, value, and var.

Referenced by function_realtime_writedestroy().

◆ function_realtime_store()

static int function_realtime_store ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 379 of file func_realtime.c.

380{
381 int res = 0;
382 char storeid[32];
383 char *valcopy;
385 AST_APP_ARG(family);
386 AST_APP_ARG(f)[30]; /* fields */
387 );
388
390 AST_APP_ARG(v)[30]; /* values */
391 );
392
393 if (ast_strlen_zero(data)) {
394 ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
395 return -1;
396 }
397
398 if (chan)
400
401 valcopy = ast_strdupa(value);
403 AST_STANDARD_APP_ARGS(v, valcopy);
404
405 res = ast_store_realtime(a.family,
406 a.f[0], v.v[0], a.f[1], v.v[1], a.f[2], v.v[2], a.f[3], v.v[3], a.f[4], v.v[4],
407 a.f[5], v.v[5], a.f[6], v.v[6], a.f[7], v.v[7], a.f[8], v.v[8], a.f[9], v.v[9],
408 a.f[10], v.v[10], a.f[11], v.v[11], a.f[12], v.v[12], a.f[13], v.v[13], a.f[14], v.v[14],
409 a.f[15], v.v[15], a.f[16], v.v[16], a.f[17], v.v[17], a.f[18], v.v[18], a.f[19], v.v[19],
410 a.f[20], v.v[20], a.f[21], v.v[21], a.f[22], v.v[22], a.f[23], v.v[23], a.f[24], v.v[24],
411 a.f[25], v.v[25], a.f[26], v.v[26], a.f[27], v.v[27], a.f[28], v.v[28], a.f[29], v.v[29], SENTINEL
412 );
413
414 if (res < 0) {
415 ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
416 } else {
417 snprintf(storeid, sizeof(storeid), "%d", res);
418 pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
419 }
420
421 if (chan)
423
424 return 0;
425}
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
int ast_store_realtime(const char *family,...) attribute_sentinel
Create realtime configuration.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
static struct test_val a

References a, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), AST_DECLARE_APP_ARGS, ast_log, AST_STANDARD_APP_ARGS, ast_store_realtime(), ast_strdupa, ast_strlen_zero(), LOG_WARNING, pbx_builtin_setvar_helper(), SENTINEL, and value.

◆ function_realtime_write()

static int function_realtime_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 264 of file func_realtime.c.

265{
266 int res = 0;
268 AST_APP_ARG(family);
269 AST_APP_ARG(fieldmatch);
271 AST_APP_ARG(field);
272 );
273
274 if (ast_strlen_zero(data)) {
275 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
276 return -1;
277 }
278
280
281 if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
282 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
283 return -1;
284 }
285
286 if (chan) {
288 }
289
290 res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);
291
292 if (res < 0) {
293 ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
294 }
295
296 if (chan) {
298 }
299
300 return res;
301}
int ast_update_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Update realtime configuration.

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), AST_DECLARE_APP_ARGS, ast_log, AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_update_realtime(), LOG_WARNING, SENTINEL, and value.

◆ function_realtime_writedestroy()

static int function_realtime_writedestroy ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Wrapper to execute REALTIME_DESTROY from a write operation. Allows execution even if live_dangerously is disabled.

Definition at line 507 of file func_realtime.c.

508{
509 return function_realtime_readdestroy(chan, cmd, data, NULL, 0);
510}
static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
#define NULL
Definition resample.c:96

References function_realtime_readdestroy(), and NULL.

◆ load_module()

static int load_module ( void  )
static

Definition at line 551 of file func_realtime.c.

552{
553 int res = 0;
559 return res;
560}
static struct ast_custom_function realtime_store_function
static struct ast_custom_function realtime_destroy_function
static struct ast_custom_function realtimehash_function
static struct ast_custom_function realtime_function
static struct ast_custom_function realtimefield_function
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition pbx.h:1571
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562
@ AST_CFE_READ
Definition pbx.h:1554

References AST_CFE_READ, ast_custom_function_register, ast_custom_function_register_escalating, realtime_destroy_function, realtime_function, realtime_store_function, realtimefield_function, and realtimehash_function.

◆ realtimefield_read()

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

Definition at line 303 of file func_realtime.c.

304{
305 struct ast_variable *var, *head;
306 struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
307 struct ast_str *fields = ast_str_thread_get(&buf2, 16);
308 struct ast_str *values = ast_str_thread_get(&buf3, 16);
309 int first = 0;
310 enum { rtfield, rthash } which;
312 AST_APP_ARG(family);
313 AST_APP_ARG(fieldmatch);
315 AST_APP_ARG(fieldname);
316 );
317
318 if (!strcmp(cmd, "REALTIME_FIELD")) {
319 which = rtfield;
320 } else {
321 which = rthash;
322 }
323
324 if (ast_strlen_zero(data)) {
325 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
326 return -1;
327 }
328
330
331 if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
332 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
333 return -1;
334 }
335
336 if (chan) {
338 }
339
340 if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
341 if (chan) {
343 }
344 return -1;
345 }
346
347 ast_str_reset(fields);
349
350 for (var = head; var; var = var->next) {
351 if (which == rtfield) {
352 ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
353 if (!strcasecmp(var->name, args.fieldname)) {
354 ast_debug(1, "Match! Value is %s\n", var->value);
355 ast_copy_string(buf, var->value, len);
356 break;
357 }
358 } else if (which == rthash) {
359 ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
360 ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
361 ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
362 first = 0;
363 }
364 }
366
367 if (which == rthash) {
368 pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
370 }
371
372 if (chan) {
374 }
375
376 return 0;
377}
struct sla_ringing_trunk * first
Definition app_sla.c:338
static const char name[]
Definition format_mp3.c:68
#define ast_debug(level,...)
Log a DEBUG message.
char * ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Set a dynamic string to a non-NULL terminated substring, with escaping of commas.
Definition strings.h:1069
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition strings.h:693
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition strings.h:909

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), ast_debug, AST_DECLARE_APP_ARGS, ast_load_realtime_all(), ast_log, AST_STANDARD_APP_ARGS, ast_str_append(), ast_str_buffer(), ast_str_reset(), ast_str_set_escapecommas(), ast_str_thread_get(), ast_strlen_zero(), ast_variables_destroy(), buf, first, len(), LOG_WARNING, ast_variable::next, pbx_builtin_setvar_helper(), SENTINEL, value, and var.

◆ unload_module()

static int unload_module ( void  )
static

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Read/Write/Store/Destroy values from a RealTime repository" , .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 562 of file func_realtime.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 562 of file func_realtime.c.

◆ realtime_destroy_function

struct ast_custom_function realtime_destroy_function
static
Initial value:
= {
.name = "REALTIME_DESTROY",
}
static int function_realtime_writedestroy(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Wrapper to execute REALTIME_DESTROY from a write operation. Allows execution even if live_dangerously...

Definition at line 534 of file func_realtime.c.

534 {
535 .name = "REALTIME_DESTROY",
538};

Referenced by load_module(), and unload_module().

◆ realtime_function

struct ast_custom_function realtime_function
static
Initial value:
= {
.name = "REALTIME",
}
static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)

Definition at line 512 of file func_realtime.c.

512 {
513 .name = "REALTIME",
516};

Referenced by load_module(), and unload_module().

◆ realtime_store_function

struct ast_custom_function realtime_store_function
static
Initial value:
= {
.name = "REALTIME_STORE",
}
static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)

Definition at line 529 of file func_realtime.c.

529 {
530 .name = "REALTIME_STORE",
532};

Referenced by load_module(), and unload_module().

◆ realtimefield_function

struct ast_custom_function realtimefield_function
static
Initial value:
= {
.name = "REALTIME_FIELD",
}
static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)

Definition at line 518 of file func_realtime.c.

518 {
519 .name = "REALTIME_FIELD",
520 .read = realtimefield_read,
522};

Referenced by load_module(), and unload_module().

◆ realtimehash_function

struct ast_custom_function realtimehash_function
static
Initial value:
= {
.name = "REALTIME_HASH",
}

Definition at line 524 of file func_realtime.c.

524 {
525 .name = "REALTIME_HASH",
526 .read = realtimefield_read,
527};

Referenced by load_module(), and unload_module().