Asterisk - The Open Source Telephony Project  21.4.1
Functions | Variables
res_config_curl.c File Reference

curl plugin for portable configuration engine More...

#include "asterisk.h"
#include <curl/curl.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/threadstorage.h"

Go to the source code of this file.

Functions

static void __init_query_buf (void)
 
static void __init_result_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static struct ast_configconfig_curl (const char *url, const char *unused, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *sugg_incl, const char *who_asked)
 
static int destroy_curl (const char *url, const char *unused, const char *keyfield, const char *lookup, const struct ast_variable *fields)
 Execute an DELETE query. More...
 
static int load_module (void)
 
static struct ast_variablerealtime_curl (const char *url, const char *unused, const struct ast_variable *fields)
 Execute a curl query and return ast_variable list. More...
 
static struct ast_configrealtime_multi_curl (const char *url, const char *unused, const struct ast_variable *fields)
 Execute an Select query and return ast_config list. More...
 
static int reload_module (void)
 
static int require_curl (const char *url, const char *unused, va_list ap)
 
static int store_curl (const char *url, const char *unused, const struct ast_variable *fields)
 Execute an INSERT query. More...
 
static int unload_module (void)
 
static int update2_curl (const char *url, const char *unused, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
 
static int update_curl (const char *url, const char *unused, const char *keyfield, const char *lookup, const struct ast_variable *fields)
 Execute an UPDATE query. More...
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Realtime Curl configuration" , .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 = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, .requires = "extconfig,res_curl,func_curl", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_config_engine curl_engine
 
static struct ast_threadstorage query_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_query_buf , .custom_init = NULL , }
 
static struct ast_threadstorage result_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_result_buf , .custom_init = NULL , }
 

Detailed Description

curl plugin for portable configuration engine

Author
Tilghman Lesher res_c.nosp@m.onfi.nosp@m.g_cur.nosp@m.l_v1.nosp@m.@the-.nosp@m.tilg.nosp@m.hman..nosp@m.com

Depends on the CURL library - http://curl.haxx.se/

Definition in file res_config_curl.c.

Function Documentation

static int destroy_curl ( const char *  url,
const char *  unused,
const char *  keyfield,
const char *  lookup,
const struct ast_variable fields 
)
static

Execute an DELETE query.

Parameters
url
unused
keyfieldwhere clause field
lookupvalue of field for where clause
fieldslist containing one or more field/value set(s)

Delete a row from a database table, prepare the sql statement using keyfield and lookup control the number of records to change. Additional params to match rows are stored in ap list. Sub-in the values to the prepared statement and execute it.

Return values
numberof rows affected
-1on failure

Definition at line 415 of file res_config_curl.c.

References ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_substitute_variables(), ast_str_thread_get(), ast_uri_encode(), ast_variable::name, ast_variable::next, and ast_variable::value.

416 {
417  struct ast_str *query, *buffer;
418  char buf1[200], buf2[200];
419  const struct ast_variable *field;
420  char *stringp;
421  int start = 1, rowcount = -1;
422 
423  if (!ast_custom_function_find("CURL")) {
424  ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
425  return -1;
426  }
427 
428  if (!(query = ast_str_thread_get(&query_buf, 1000))) {
429  return -1;
430  }
431 
432  if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
433  return -1;
434  }
435 
436  ast_uri_encode(keyfield, buf1, sizeof(buf1), ast_uri_http);
437  ast_uri_encode(lookup, buf2, sizeof(buf2), ast_uri_http);
438  ast_str_set(&query, 0, "${CURL(%s/destroy,%s=%s&", url, buf1, buf2);
439 
440  for (field = fields; field; field = field->next) {
441  ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http);
442  ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http);
443  ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2);
444  start = 0;
445  }
446 
447  ast_str_append(&query, 0, ")}");
448  ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
449 
450  /* Line oriented output */
451  stringp = ast_str_buffer(buffer);
452  while (*stringp <= ' ') {
453  stringp++;
454  }
455  sscanf(stringp, "%30d", &rowcount);
456 
457  if (rowcount >= 0) {
458  return (int)rowcount;
459  }
460 
461  return -1;
462 }
struct ast_variable * next
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
Structure for variables, used for configurations and for channel variables.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
Definition: utils.c:723
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
Support for dynamic strings.
Definition: strings.h:623
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
static struct ast_variable* realtime_curl ( const char *  url,
const char *  unused,
const struct ast_variable fields 
)
static

Execute a curl query and return ast_variable list.

Parameters
urlThe base URL from which to retrieve data
unusedNot currently used
fieldslist containing one or more field/operator/value set.
Return values
varon success
NULLon failure

Definition at line 61 of file res_config_curl.c.

References ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_substitute_variables(), ast_str_thread_get(), ast_uri_decode(), ast_uri_encode(), ast_variable::name, ast_variable::next, S_OR, and ast_variable::value.

62 {
63  struct ast_str *query, *buffer;
64  char buf1[256], buf2[256];
65  const struct ast_variable *field;
66  char *stringp, *pair, *key;
67  unsigned int start = 1;
68  struct ast_variable *var = NULL, *prev = NULL;
69 
70  if (!ast_custom_function_find("CURL")) {
71  ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
72  return NULL;
73  }
74 
75  if (!(query = ast_str_thread_get(&query_buf, 16))) {
76  return NULL;
77  }
78 
79  if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
80  return NULL;
81  }
82 
83  ast_str_set(&query, 0, "${CURL(%s/single,", url);
84 
85  for (field = fields; field; field = field->next) {
86  ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http);
87  ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http);
88  ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2);
89  start = 0;
90  }
91 
92  ast_str_append(&query, 0, ")}");
93  ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
94 
95  /* Remove any trailing newline characters */
96  if ((stringp = strchr(ast_str_buffer(buffer), '\r')) || (stringp = strchr(ast_str_buffer(buffer), '\n'))) {
97  *stringp = '\0';
98  }
99 
100  stringp = ast_str_buffer(buffer);
101  while ((pair = strsep(&stringp, "&"))) {
102  key = strsep(&pair, "=");
103  ast_uri_decode(key, ast_uri_http);
104  if (pair) {
105  ast_uri_decode(pair, ast_uri_http);
106  }
107 
108  if (!ast_strlen_zero(key)) {
109  if (prev) {
110  prev->next = ast_variable_new(key, S_OR(pair, ""), "");
111  if (prev->next) {
112  prev = prev->next;
113  }
114  } else {
115  prev = var = ast_variable_new(key, S_OR(pair, ""), "");
116  }
117  }
118  }
119 
120  return var;
121 }
void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
Definition: utils.c:762
struct ast_variable * next
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
Structure for variables, used for configurations and for channel variables.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
Definition: utils.c:723
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
Support for dynamic strings.
Definition: strings.h:623
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
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
static struct ast_config* realtime_multi_curl ( const char *  url,
const char *  unused,
const struct ast_variable fields 
)
static

Execute an Select query and return ast_config list.

Parameters
url
unused
fieldslist containing one or more field/operator/value set.
Return values
structast_config pointer on success
NULLon failure

Definition at line 132 of file res_config_curl.c.

References ast_category_append(), ast_category_new_anonymous, ast_config_new(), ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_substitute_variables(), ast_str_thread_get(), ast_strdup, ast_uri_decode(), ast_uri_encode(), ast_variable::name, ast_variable::next, S_OR, and ast_variable::value.

133 {
134  struct ast_str *query, *buffer;
135  char buf1[256], buf2[256];
136  const struct ast_variable *field;
137  char *stringp, *line, *pair, *key, *initfield = NULL;
138  int start = 1;
139  struct ast_variable *var = NULL;
140  struct ast_config *cfg = NULL;
141  struct ast_category *cat = NULL;
142 
143  if (!ast_custom_function_find("CURL")) {
144  ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
145  return NULL;
146  }
147 
148  if (!(query = ast_str_thread_get(&query_buf, 16))) {
149  return NULL;
150  }
151 
152  if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
153  return NULL;
154  }
155 
156  ast_str_set(&query, 0, "${CURL(%s/multi,", url);
157 
158  for (field = fields; field; field = field->next) {
159  if (start) {
160  char *op;
161  initfield = ast_strdup(field->name);
162  if ((op = strchr(initfield, ' ')))
163  *op = '\0';
164  }
165  ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http);
166  ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http);
167  ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2);
168  start = 0;
169  }
170 
171  ast_str_append(&query, 0, ")}");
172 
173  /* Do the CURL query */
174  ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
175 
176  if (!(cfg = ast_config_new())) {
177  ast_free(initfield);
178  return NULL;
179  }
180 
181  /* Line oriented output */
182  stringp = ast_str_buffer(buffer);
183  while ((line = strsep(&stringp, "\r\n"))) {
184  if (ast_strlen_zero(line)) {
185  continue;
186  }
187 
189  if (!cat) {
190  continue;
191  }
192 
193  while ((pair = strsep(&line, "&"))) {
194  key = strsep(&pair, "=");
195  ast_uri_decode(key, ast_uri_http);
196  if (pair) {
197  ast_uri_decode(pair, ast_uri_http);
198  }
199 
200  if (!strcasecmp(key, initfield) && pair) {
201  ast_category_rename(cat, pair);
202  }
203 
204  if (!ast_strlen_zero(key)) {
205  var = ast_variable_new(key, S_OR(pair, ""), "");
206  ast_variable_append(cat, var);
207  }
208  }
209  ast_category_append(cfg, cat);
210  }
211 
212  ast_free(initfield);
213 
214  return cfg;
215 }
void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
Definition: utils.c:762
struct ast_variable * next
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
Structure for variables, used for configurations and for channel variables.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
Definition: utils.c:723
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
void ast_category_append(struct ast_config *config, struct ast_category *category)
Appends a category to a config.
Definition: extconf.c:2833
#define ast_category_new_anonymous()
Create a nameless category that is not backed by a file.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
struct ast_config * ast_config_new(void)
Create a new base configuration structure.
Definition: extconf.c:3274
Support for dynamic strings.
Definition: strings.h:623
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
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
static int store_curl ( const char *  url,
const char *  unused,
const struct ast_variable fields 
)
static

Execute an INSERT query.

Parameters
url
unused
fieldslist containing one or more field/value set(s)

Insert a new record into database table, prepare the sql statement. All values to be changed are stored in ap list. Sub-in the values to the prepared statement and execute it.

Return values
numberof rows affected
-1on failure

Definition at line 354 of file res_config_curl.c.

References ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_substitute_variables(), ast_str_thread_get(), ast_uri_encode(), ast_variable::name, ast_variable::next, and ast_variable::value.

355 {
356  struct ast_str *query, *buffer;
357  char buf1[256], buf2[256];
358  const struct ast_variable *field;
359  char *stringp;
360  int start = 1, rowcount = -1;
361 
362  if (!ast_custom_function_find("CURL")) {
363  ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
364  return -1;
365  }
366 
367  if (!(query = ast_str_thread_get(&query_buf, 1000))) {
368  return -1;
369  }
370 
371  if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
372  return -1;
373  }
374 
375  ast_str_set(&query, 0, "${CURL(%s/store,", url);
376 
377  for (field = fields; field; field = field->next) {
378  ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http);
379  ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http);
380  ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2);
381  start = 0;
382  }
383 
384  ast_str_append(&query, 0, ")}");
385  ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
386 
387  stringp = ast_str_buffer(buffer);
388  while (*stringp <= ' ') {
389  stringp++;
390  }
391  sscanf(stringp, "%30d", &rowcount);
392 
393  if (rowcount >= 0) {
394  return rowcount;
395  }
396 
397  return -1;
398 }
struct ast_variable * next
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
Structure for variables, used for configurations and for channel variables.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
Definition: utils.c:723
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
Support for dynamic strings.
Definition: strings.h:623
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
static int update_curl ( const char *  url,
const char *  unused,
const char *  keyfield,
const char *  lookup,
const struct ast_variable fields 
)
static

Execute an UPDATE query.

Parameters
url
unused
keyfieldwhere clause field
lookupvalue of field for where clause
fieldslist containing one or more field/value set(s).

Update a database table, prepare the sql statement using keyfield and lookup control the number of records to change. All values to be changed are stored in ap list. Sub-in the values to the prepared statement and execute it.

Return values
numberof rows affected
-1on failure

Definition at line 232 of file res_config_curl.c.

References ast_str_append(), ast_str_buffer(), ast_str_set(), ast_str_substitute_variables(), ast_str_thread_get(), ast_uri_encode(), ast_variable::name, ast_variable::next, and ast_variable::value.

233 {
234  struct ast_str *query, *buffer;
235  char buf1[256], buf2[256];
236  const struct ast_variable *field;
237  char *stringp;
238  int start = 1, rowcount = -1;
239 
240  if (!ast_custom_function_find("CURL")) {
241  ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
242  return -1;
243  }
244 
245  if (!(query = ast_str_thread_get(&query_buf, 16))) {
246  return -1;
247  }
248 
249  if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
250  return -1;
251  }
252 
253  ast_uri_encode(keyfield, buf1, sizeof(buf1), ast_uri_http);
254  ast_uri_encode(lookup, buf2, sizeof(buf2), ast_uri_http);
255  ast_str_set(&query, 0, "${CURL(%s/update?%s=%s,", url, buf1, buf2);
256 
257  for (field = fields; field; field = field->next) {
258  ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http);
259  ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http);
260  ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2);
261  start = 0;
262  }
263 
264  ast_str_append(&query, 0, ")}");
265  ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
266 
267  /* Line oriented output */
268  stringp = ast_str_buffer(buffer);
269  while (*stringp <= ' ') {
270  stringp++;
271  }
272  sscanf(stringp, "%30d", &rowcount);
273 
274  if (rowcount >= 0) {
275  return (int)rowcount;
276  }
277 
278  return -1;
279 }
struct ast_variable * next
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
Structure for variables, used for configurations and for channel variables.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
Definition: utils.c:723
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
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
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
Support for dynamic strings.
Definition: strings.h:623
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