37 #include <sys/types.h> 53 #define CONFIG "cel_odbc.conf" 55 #define ODBC_BACKEND_NAME "ODBC CEL backend" 58 #define CEL_SHOW_USERDEF_DEFAULT 0 95 const char *
tmp, *catg;
102 int lenconnection, lentable;
105 SQLHSTMT stmt =
NULL;
117 if (!strcasecmp(var->
name,
"show_user_defined")) {
125 if (!strcasecmp(catg,
"general")) {
137 lenconnection = strlen(connection);
142 ast_log(
LOG_WARNING,
"No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
151 lentable = strlen(table);
153 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->
con, &stmt);
154 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
160 res = SQLColumns(stmt,
NULL, 0,
NULL, 0, (
unsigned char *)table, SQL_NTS, (
unsigned char *)
"%", SQL_NTS);
161 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
162 ast_log(
LOG_ERROR,
"Unable to query database columns on connection '%s'. Skipping.\n", connection);
167 tableptr =
ast_calloc(
sizeof(
char),
sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
169 ast_log(
LOG_ERROR,
"Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
175 tableptr->
connection = (
char *)tableptr +
sizeof(*tableptr);
176 tableptr->
table = (
char *)tableptr +
sizeof(*tableptr) + lenconnection + 1;
194 if (strncmp(var->
name,
"filter", 6) == 0) {
199 entry =
ast_calloc(
sizeof(
char),
sizeof(*entry) + strlen(celvar) + 1 + strlen(var->
value) + 1);
201 ast_log(
LOG_ERROR,
"Out of memory creating filter entry for CEL variable '%s' in table '%s' on connection '%s'\n", celvar, table, connection);
208 entry->
celname = (
char *)entry +
sizeof(*entry);
209 entry->
filtervalue = (
char *)entry +
sizeof(*entry) + strlen(celvar) + 1;
210 strcpy(entry->
celname, celvar);
217 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
220 SQLGetData(stmt, 4, SQL_C_CHAR, columnname,
sizeof(columnname), &sqlptr);
229 if (strncmp(var->
name,
"alias", 5) == 0 && strcasecmp(var->
value, columnname) == 0) {
232 ast_verb(3,
"Found alias %s for column %s in %s@%s\n", celvar, columnname, tableptr->
table, tableptr->
connection);
234 }
else if (strncmp(var->
name,
"static", 6) == 0 && strcasecmp(var->
value, columnname) == 0) {
237 if (item[0] ==
'"' && item[strlen(item) - 1] ==
'"') {
239 item[strlen(item) - 1] =
'\0';
246 entry =
ast_calloc(
sizeof(
char),
sizeof(*entry) + strlen(columnname) + 1 + strlen(celvar) + 1 + strlen(
staticvalue) + 1);
248 ast_log(
LOG_ERROR,
"Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
252 entry->
name = (
char *)entry +
sizeof(*entry);
253 strcpy(entry->
name, columnname);
256 entry->
celname = entry->
name + strlen(columnname) + 1;
257 strcpy(entry->
celname, celvar);
259 entry->
celname = (
char *)entry +
sizeof(*entry);
267 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->
type,
sizeof(entry->
type),
NULL);
268 SQLGetData(stmt, 7, SQL_C_LONG, &entry->
size,
sizeof(entry->
size),
NULL);
270 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->
radix,
sizeof(entry->
radix),
NULL);
280 ast_verb(10,
"Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->
name, entry->
type, (
long) entry->
size, (
long) entry->
octetlen, entry->
decimals, entry->
radix);
286 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
316 SQLINTEGER nativeerror = 0, numfields = 0;
317 SQLSMALLINT diagbytes = 0;
318 unsigned char state[10], diagnostic[256];
320 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->
con, &stmt);
321 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
327 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
329 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
330 for (i = 0; i < numfields; i++) {
331 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic,
sizeof(diagnostic), &diagbytes);
332 ast_log(
LOG_WARNING,
"SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
334 ast_log(
LOG_WARNING,
"Oh, that was good. There are really %d diagnostics?\n", (
int)numfields);
338 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
345 #define LENGTHEN_BUF(size, var_sql) \ 348 if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \ 349 if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 1) / 512 + 1) * 512) != 0) { \ 350 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", tableptr->connection, tableptr->table); \ 353 AST_RWLIST_UNLOCK(&odbc_tables); \ 359 #define LENGTHEN_BUF1(size) \ 360 LENGTHEN_BUF(size, sql); 362 #define LENGTHEN_BUF2(size) \ 363 LENGTHEN_BUF(size, sql2); 372 char colbuf[1024], *colptr;
373 SQLHSTMT stmt =
NULL;
399 char *separator =
"";
412 if (strcasecmp(entry->
celname,
"eventtime") == 0) {
419 }
else if (datefield) {
421 struct ast_tm tm = { 0, };
433 ast_strftime(colbuf,
sizeof(colbuf),
"%Y-%m-%d %H:%M:%S.%6q", &tm);
436 if (strcmp(entry->
celname,
"userdeftype") == 0) {
438 }
else if (strcmp(entry->
celname,
"cid_name") == 0) {
440 }
else if (strcmp(entry->
celname,
"cid_num") == 0) {
442 }
else if (strcmp(entry->
celname,
"cid_ani") == 0) {
444 }
else if (strcmp(entry->
celname,
"cid_rdnis") == 0) {
446 }
else if (strcmp(entry->
celname,
"cid_dnid") == 0) {
448 }
else if (strcmp(entry->
celname,
"exten") == 0) {
450 }
else if (strcmp(entry->
celname,
"context") == 0) {
452 }
else if (strcmp(entry->
celname,
"channame") == 0) {
454 }
else if (strcmp(entry->
celname,
"appname") == 0) {
456 }
else if (strcmp(entry->
celname,
"appdata") == 0) {
458 }
else if (strcmp(entry->
celname,
"accountcode") == 0) {
460 }
else if (strcmp(entry->
celname,
"peeraccount") == 0) {
462 }
else if (strcmp(entry->
celname,
"uniqueid") == 0) {
464 }
else if (strcmp(entry->
celname,
"linkedid") == 0) {
466 }
else if (strcmp(entry->
celname,
"userfield") == 0) {
468 }
else if (strcmp(entry->
celname,
"peer") == 0) {
470 }
else if (strcmp(entry->
celname,
"amaflags") == 0) {
471 snprintf(colbuf,
sizeof(colbuf),
"%u", record.
amaflag);
472 }
else if (strcmp(entry->
celname,
"extra") == 0) {
474 }
else if (strcmp(entry->
celname,
"eventtype") == 0) {
475 snprintf(colbuf,
sizeof(colbuf),
"%u", record.
event_type);
483 if (colptr && !unknown) {
489 ast_verb(4,
"CEL column '%s' with value '%s' does not match filter of" 490 " '%s'. Cancelling this CEL.\n",
501 switch (entry->
type) {
504 case SQL_LONGVARCHAR:
505 #ifdef HAVE_ODBC_WCHAR 508 case SQL_WLONGVARCHAR:
512 case SQL_LONGVARBINARY:
517 if (strcasecmp(entry->
name,
"eventtype") == 0) {
518 const char *event_name;
523 snprintf(colbuf,
sizeof(colbuf),
"%s", event_name);
527 if (entry->
type != SQL_GUID) {
528 if (strlen(colptr) > entry->
octetlen) {
538 for (tmp = colptr; *
tmp; tmp++) {
553 int year = 0, month = 0, day = 0;
554 if (strcasecmp(entry->
name,
"eventdate") == 0) {
561 if (sscanf(colptr,
"%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 ||
562 month <= 0 || month > 12 || day < 0 || day > 31 ||
563 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
564 (month == 2 && year % 400 == 0 && day > 29) ||
565 (month == 2 && year % 100 == 0 && day > 28) ||
566 (month == 2 && year % 4 == 0 && day > 29) ||
567 (month == 2 && year % 4 != 0 && day > 28)) {
572 if (year > 0 && year < 100) {
579 ast_str_append(&sql2, 0,
"%s{d '%04d-%02d-%02d'}", separator, year, month, day);
586 int hour = 0, minute = 0, second = 0;
587 if (strcasecmp(entry->
name,
"eventdate") == 0) {
594 int count = sscanf(colptr,
"%2d:%2d:%2d", &hour, &minute, &second);
596 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > (tableptr->
allowleapsec ? 60 : 59)) {
604 ast_str_append(&sql2, 0,
"%s{t '%02d:%02d:%02d'}", separator, hour, minute, second);
607 case SQL_TYPE_TIMESTAMP:
621 int year = 0, month = 0, day = 0, hour = 0, minute = 0;
624 if (strcasecmp(entry->
name,
"eventdate") == 0) {
638 second += (tm.
tm_usec / 1000000.0);
644 int count = sscanf(colptr,
"%4d-%2d-%2d %2d:%2d:%lf", &year, &month, &day, &hour, &minute, &second);
646 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
647 month <= 0 || month > 12 || day < 0 || day > 31 ||
648 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
649 (month == 2 && year % 400 == 0 && day > 29) ||
650 (month == 2 && year % 100 == 0 && day > 28) ||
651 (month == 2 && year % 4 == 0 && day > 29) ||
652 (month == 2 && year % 4 != 0 && day > 28) ||
653 hour > 23 || minute > 59 || ((
int)floor(second)) > (tableptr->
allowleapsec ? 60 : 59) ||
654 hour < 0 || minute < 0 || ((
int)floor(second)) < 0) {
659 if (year > 0 && year < 100) {
666 ast_str_append(&sql2, 0,
"%s{ts '%04d-%02d-%02d %02d:%02d:%09.6lf'}", separator, year, month, day, hour, minute, second);
673 if (sscanf(colptr,
"%30d", &integer) != 1) {
687 if ((ret = sscanf(colptr,
"%30lld", &integer)) != 1) {
700 if (sscanf(colptr,
"%30hd", &integer) != 1) {
713 if (sscanf(colptr,
"%30hhd", &integer) != 1) {
726 if (sscanf(colptr,
"%30hhd", &integer) != 1) {
742 if (sscanf(colptr,
"%30lf", &number) != 1) {
757 if (sscanf(colptr,
"%30lf", &number) != 1) {
784 SQLRowCount(stmt, &rows);
785 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
859 .requires =
"cel,res_odbc",
const char * account_code
struct ast_variable * next
int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
Checks if the database natively supports backslash as an escape character.
const char * caller_id_name
Helper struct for getting the fields out of a CEL event.
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
static unsigned char cel_show_user_def
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Time-related functions and macros.
const char * user_defined_name
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
const char * application_data
const char * application_name
#define CONFIG_STATUS_FILEINVALID
int ast_odbc_prepare(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
Prepares a SQL query on a statement.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
static void odbc_log(struct ast_event *event)
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Structure for variables, used for configurations and for channel variables.
#define LENGTHEN_BUF1(size)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
static struct aco_type item
const char * caller_id_num
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
unsigned int allowleapsec
#define ast_verb(level,...)
#define ast_strlen_zero(foo)
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Configuration File Parser.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_config_load(filename, flags)
Load a config file.
static int free_config(void)
#define CEL_SHOW_USERDEF_DEFAULT
show_user_def is off by default
#define LENGTHEN_BUF2(size)
General Asterisk PBX channel definitions.
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
void ast_config_destroy(struct ast_config *config)
Destroys a config.
uint32_t version
struct ABI version
#define ast_strdupa(s)
duplicate a string in memory from the stack
A set of macros to manage forward-linked lists.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
enum ast_cel_event_type event_type
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
const char * caller_id_rdnis
#define ODBC_BACKEND_NAME
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
static struct columns columns
#define ast_calloc(num, len)
A wrapper for calloc()
#define ast_odbc_request_obj(a, b)
static struct ast_codec unknown
#define AST_RWLIST_REMOVE_HEAD
Module has failed to load, may be in an inconsistent state.
static int unload_module(void)
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
const char * caller_id_ani
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Structure used to handle boolean flags.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
const char * peer_account
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
#define AST_RWLIST_INSERT_TAIL
SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT(*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
Prepares, executes, and returns the resulting statement handle.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
struct tables::mysql_columns columns
static int load_config(void)
a user-defined event, the event name field should be set
static int load_module(void)
const char * caller_id_dnid
const char * channel_name
void ast_odbc_release_obj(struct odbc_obj *obj)
Releases an ODBC object previously allocated by ast_odbc_request_obj()
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
struct timeval event_time
int ast_cel_fill_record(const struct ast_event *event, struct ast_cel_event_record *r)
Fill in an ast_cel_event_record from a CEL event.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.