| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "asterisk.h" |
| 43 |
|
| 44 |
#include <sys/types.h> |
| 45 |
#include <time.h> |
| 46 |
|
| 47 |
#include <sql.h> |
| 48 |
#include <sqlext.h> |
| 49 |
#include <sqltypes.h> |
| 50 |
|
| 51 |
#include "asterisk/config.h" |
| 52 |
#include "asterisk/channel.h" |
| 53 |
#include "asterisk/lock.h" |
| 54 |
#include "asterisk/linkedlists.h" |
| 55 |
#include "asterisk/res_odbc.h" |
| 56 |
#include "asterisk/cdr.h" |
| 57 |
#include "asterisk/module.h" |
| 58 |
|
| 59 |
#define CONFIG "cdr_adaptive_odbc.conf" |
| 60 |
|
| 61 |
static const char name[] = "Adaptive ODBC"; |
| 62 |
|
| 63 |
static int maxsize = 512, maxsize2 = 512; |
| 64 |
|
| 65 |
struct columns { |
| 66 |
char *name; |
| 67 |
char *cdrname; |
| 68 |
char *filtervalue; |
| 69 |
char *staticvalue; |
| 70 |
SQLSMALLINT type; |
| 71 |
SQLINTEGER size; |
| 72 |
SQLSMALLINT decimals; |
| 73 |
SQLSMALLINT radix; |
| 74 |
SQLSMALLINT nullable; |
| 75 |
SQLINTEGER octetlen; |
| 76 |
AST_LIST_ENTRY(columns) list; |
| 77 |
unsigned int negatefiltervalue:1; |
| 78 |
}; |
| 79 |
|
| 80 |
struct tables { |
| 81 |
char *connection; |
| 82 |
char *table; |
| 83 |
char *schema; |
| 84 |
char quoted_identifiers; |
| 85 |
unsigned int usegmtime:1; |
| 86 |
AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns; |
| 87 |
AST_RWLIST_ENTRY(tables) list; |
| 88 |
}; |
| 89 |
|
| 90 |
static AST_RWLIST_HEAD_STATIC(odbc_tables, tables); |
| 91 |
|
| 92 |
static int load_config(void) |
| 93 |
{ |
| 94 |
struct ast_config *cfg; |
| 95 |
struct ast_variable *var; |
| 96 |
const char *tmp, *catg; |
| 97 |
struct tables *tableptr; |
| 98 |
struct columns *entry; |
| 99 |
struct odbc_obj *obj; |
| 100 |
char columnname[80]; |
| 101 |
char connection[40]; |
| 102 |
char table[40]; |
| 103 |
char schema[40]; |
| 104 |
char quoted_identifiers; |
| 105 |
int lenconnection, lentable, lenschema, usegmtime = 0; |
| 106 |
SQLLEN sqlptr; |
| 107 |
int res = 0; |
| 108 |
SQLHSTMT stmt = NULL; |
| 109 |
struct ast_flags config_flags = { 0 }; |
| 110 |
|
| 111 |
cfg = ast_config_load(CONFIG, config_flags); |
| 112 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 113 |
ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n"); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
|
| 117 |
for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) { |
| 118 |
var = ast_variable_browse(cfg, catg); |
| 119 |
if (!var) |
| 120 |
continue; |
| 121 |
|
| 122 |
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) { |
| 123 |
ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg); |
| 124 |
continue; |
| 125 |
} |
| 126 |
ast_copy_string(connection, tmp, sizeof(connection)); |
| 127 |
lenconnection = strlen(connection); |
| 128 |
|
| 129 |
if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) { |
| 130 |
usegmtime = ast_true(tmp); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
obj = ast_odbc_request_obj(connection, 1); |
| 135 |
if (!obj) { |
| 136 |
ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg); |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) { |
| 141 |
ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n"); |
| 142 |
tmp = "cdr"; |
| 143 |
} |
| 144 |
ast_copy_string(table, tmp, sizeof(table)); |
| 145 |
lentable = strlen(table); |
| 146 |
|
| 147 |
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "schema"))) { |
| 148 |
tmp = ""; |
| 149 |
} |
| 150 |
ast_copy_string(schema, tmp, sizeof(schema)); |
| 151 |
lenschema = strlen(schema); |
| 152 |
|
| 153 |
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "quoted_identifiers"))) { |
| 154 |
tmp = ""; |
| 155 |
} |
| 156 |
quoted_identifiers = tmp[0]; |
| 157 |
if (strlen(tmp) > 1) { |
| 158 |
ast_log(LOG_ERROR, "The quoted_identifiers setting only accepts a single character," |
| 159 |
" while a value of '%s' was provided. This option has been disabled as a result.\n", tmp); |
| 160 |
quoted_identifiers = '\0'; |
| 161 |
} |
| 162 |
|
| 163 |
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
| 164 |
if (!SQL_SUCCEEDED(res)) { |
| 165 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection); |
| 166 |
ast_odbc_release_obj(obj); |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
res = SQLColumns(stmt, NULL, 0, lenschema == 0 ? NULL : (unsigned char *)schema, SQL_NTS, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS); |
| 171 |
if (!SQL_SUCCEEDED(res)) { |
| 172 |
ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection); |
| 173 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 174 |
ast_odbc_release_obj(obj); |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1 + lenschema + 1 + 1); |
| 179 |
if (!tableptr) { |
| 180 |
ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'%s%s%s\n", table, connection, |
| 181 |
lenschema ? " (schema '" : "", lenschema ? schema : "", lenschema ? "')" : ""); |
| 182 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 183 |
ast_odbc_release_obj(obj); |
| 184 |
res = -1; |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
tableptr->usegmtime = usegmtime; |
| 189 |
tableptr->connection = (char *)tableptr + sizeof(*tableptr); |
| 190 |
tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1; |
| 191 |
tableptr->schema = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1 + lentable + 1; |
| 192 |
ast_copy_string(tableptr->connection, connection, lenconnection + 1); |
| 193 |
ast_copy_string(tableptr->table, table, lentable + 1); |
| 194 |
ast_copy_string(tableptr->schema, schema, lenschema + 1); |
| 195 |
tableptr->quoted_identifiers = quoted_identifiers; |
| 196 |
|
| 197 |
ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection); |
| 198 |
|
| 199 |
|
| 200 |
for (var = ast_variable_browse(cfg, catg); var; var = var->next) { |
| 201 |
if (strncmp(var->name, "filter", 6) == 0) { |
| 202 |
int negate = 0; |
| 203 |
char *cdrvar = ast_strdupa(var->name + 6); |
| 204 |
cdrvar = ast_strip(cdrvar); |
| 205 |
if (cdrvar[strlen(cdrvar) - 1] == '!') { |
| 206 |
negate = 1; |
| 207 |
cdrvar[strlen(cdrvar) - 1] = '\0'; |
| 208 |
ast_trim_blanks(cdrvar); |
| 209 |
} |
| 210 |
|
| 211 |
ast_verb(3, "Found filter %s'%s' for CDR variable %s in %s@%s\n", negate ? "!" : "", var->value, cdrvar, tableptr->table, tableptr->connection); |
| 212 |
|
| 213 |
entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(cdrvar) + 1 + strlen(var->value) + 1); |
| 214 |
if (!entry) { |
| 215 |
ast_log(LOG_ERROR, "Out of memory creating filter entry for CDR variable '%s' in table '%s' on connection '%s'\n", cdrvar, table, connection); |
| 216 |
res = -1; |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
entry->name = NULL; |
| 222 |
entry->cdrname = (char *)entry + sizeof(*entry); |
| 223 |
entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(cdrvar) + 1; |
| 224 |
strcpy(entry->cdrname, cdrvar); |
| 225 |
strcpy(entry->filtervalue, var->value); |
| 226 |
entry->negatefiltervalue = negate; |
| 227 |
|
| 228 |
AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) { |
| 233 |
char *cdrvar = "", *staticvalue = ""; |
| 234 |
|
| 235 |
SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
for (var = ast_variable_browse(cfg, catg); var; var = var->next) { |
| 244 |
if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) { |
| 245 |
char *alias = ast_strdupa(var->name + 5); |
| 246 |
cdrvar = ast_strip(alias); |
| 247 |
ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection); |
| 248 |
break; |
| 249 |
} else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) { |
| 250 |
char *item = ast_strdupa(var->name + 6); |
| 251 |
item = ast_strip(item); |
| 252 |
if (item[0] == '"' && item[strlen(item) - 1] == '"') { |
| 253 |
|
| 254 |
item[strlen(item) - 1] = '\0'; |
| 255 |
item++; |
| 256 |
} |
| 257 |
staticvalue = item; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1); |
| 262 |
if (!entry) { |
| 263 |
ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection); |
| 264 |
res = -1; |
| 265 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 266 |
break; |
| 267 |
} |
| 268 |
entry->name = (char *)entry + sizeof(*entry); |
| 269 |
strcpy(entry->name, columnname); |
| 270 |
|
| 271 |
if (!ast_strlen_zero(cdrvar)) { |
| 272 |
entry->cdrname = entry->name + strlen(columnname) + 1; |
| 273 |
strcpy(entry->cdrname, cdrvar); |
| 274 |
} else { |
| 275 |
entry->cdrname = (char *)entry + sizeof(*entry); |
| 276 |
} |
| 277 |
|
| 278 |
if (!ast_strlen_zero(staticvalue)) { |
| 279 |
entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1; |
| 280 |
strcpy(entry->staticvalue, staticvalue); |
| 281 |
} |
| 282 |
|
| 283 |
SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL); |
| 284 |
SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL); |
| 285 |
SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL); |
| 286 |
SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL); |
| 287 |
SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL); |
| 288 |
SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if (entry->octetlen == 0) |
| 294 |
entry->octetlen = entry->size; |
| 295 |
|
| 296 |
ast_verb(4, "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); |
| 297 |
|
| 298 |
AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list); |
| 299 |
res = 0; |
| 300 |
} |
| 301 |
|
| 302 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 303 |
ast_odbc_release_obj(obj); |
| 304 |
|
| 305 |
if (AST_LIST_FIRST(&(tableptr->columns))) |
| 306 |
AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list); |
| 307 |
else |
| 308 |
ast_free(tableptr); |
| 309 |
} |
| 310 |
ast_config_destroy(cfg); |
| 311 |
return res; |
| 312 |
} |
| 313 |
|
| 314 |
static int free_config(void) |
| 315 |
{ |
| 316 |
struct tables *table; |
| 317 |
struct columns *entry; |
| 318 |
while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) { |
| 319 |
while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) { |
| 320 |
ast_free(entry); |
| 321 |
} |
| 322 |
ast_free(table); |
| 323 |
} |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
#define LENGTHEN_BUF(size, var_sql) \ |
| 328 |
do { \ |
| 329 |
\ |
| 330 |
if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \ |
| 331 |
if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 1) / 512 + 1) * 512) != 0) { \ |
| 332 |
ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \ |
| 333 |
ast_free(sql); \ |
| 334 |
ast_free(sql2); \ |
| 335 |
AST_RWLIST_UNLOCK(&odbc_tables); \ |
| 336 |
return -1; \ |
| 337 |
} \ |
| 338 |
} \ |
| 339 |
} while (0) |
| 340 |
|
| 341 |
#define LENGTHEN_BUF1(size) \ |
| 342 |
LENGTHEN_BUF(size, sql); |
| 343 |
#define LENGTHEN_BUF2(size) \ |
| 344 |
LENGTHEN_BUF(size, sql2); |
| 345 |
|
| 346 |
static int odbc_log(struct ast_cdr *cdr) |
| 347 |
{ |
| 348 |
struct tables *tableptr; |
| 349 |
struct columns *entry; |
| 350 |
struct odbc_obj *obj; |
| 351 |
struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2); |
| 352 |
char *tmp; |
| 353 |
char colbuf[1024], *colptr; |
| 354 |
SQLHSTMT stmt = NULL; |
| 355 |
char *separator; |
| 356 |
int quoted = 0; |
| 357 |
int res; |
| 358 |
|
| 359 |
if (!sql || !sql2) { |
| 360 |
if (sql) |
| 361 |
ast_free(sql); |
| 362 |
if (sql2) |
| 363 |
ast_free(sql2); |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
if (AST_RWLIST_RDLOCK(&odbc_tables)) { |
| 368 |
ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n"); |
| 369 |
ast_free(sql); |
| 370 |
ast_free(sql2); |
| 371 |
return -1; |
| 372 |
} |
| 373 |
|
| 374 |
AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) { |
| 375 |
separator = ""; |
| 376 |
|
| 377 |
quoted = 0; |
| 378 |
if (tableptr->quoted_identifiers != '\0'){ |
| 379 |
quoted = 1; |
| 380 |
} |
| 381 |
|
| 382 |
if (ast_strlen_zero(tableptr->schema)) { |
| 383 |
if (quoted) { |
| 384 |
ast_str_set(&sql, 0, "INSERT INTO %c%s%c (", |
| 385 |
tableptr->quoted_identifiers, tableptr->table, tableptr->quoted_identifiers ); |
| 386 |
}else{ |
| 387 |
ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table); |
| 388 |
} |
| 389 |
} else { |
| 390 |
if (quoted) { |
| 391 |
ast_str_set(&sql, 0, "INSERT INTO %c%s%c.%c%s%c (", |
| 392 |
tableptr->quoted_identifiers, tableptr->schema, tableptr->quoted_identifiers, |
| 393 |
tableptr->quoted_identifiers, tableptr->table, tableptr->quoted_identifiers); |
| 394 |
}else{ |
| 395 |
ast_str_set(&sql, 0, "INSERT INTO %s.%s (", tableptr->schema, tableptr->table); |
| 396 |
} |
| 397 |
} |
| 398 |
ast_str_set(&sql2, 0, " VALUES ("); |
| 399 |
|
| 400 |
|
| 401 |
if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) { |
| 402 |
ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql)); |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) { |
| 407 |
int datefield = 0; |
| 408 |
if (strcasecmp(entry->cdrname, "start") == 0) { |
| 409 |
datefield = 1; |
| 410 |
} else if (strcasecmp(entry->cdrname, "answer") == 0) { |
| 411 |
datefield = 2; |
| 412 |
} else if (strcasecmp(entry->cdrname, "end") == 0) { |
| 413 |
datefield = 3; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
if (entry->staticvalue) { |
| 418 |
colptr = ast_strdupa(entry->staticvalue); |
| 419 |
} else if (datefield && tableptr->usegmtime) { |
| 420 |
struct timeval date_tv = (datefield == 1) ? cdr->start : (datefield == 2) ? cdr->answer : cdr->end; |
| 421 |
struct ast_tm tm = { 0, }; |
| 422 |
ast_localtime(&date_tv, &tm, "UTC"); |
| 423 |
ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S", &tm); |
| 424 |
colptr = colbuf; |
| 425 |
} else { |
| 426 |
ast_cdr_format_var(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), datefield ? 0 : 1); |
| 427 |
} |
| 428 |
|
| 429 |
if (colptr) { |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if ((entry->filtervalue && !entry->negatefiltervalue && strcasecmp(colptr, entry->filtervalue) != 0) || |
| 435 |
(entry->filtervalue && entry->negatefiltervalue && strcasecmp(colptr, entry->filtervalue) == 0)) { |
| 436 |
ast_verb(4, "CDR column '%s' with value '%s' does not match filter of" |
| 437 |
" %s'%s'. Cancelling this CDR.\n", |
| 438 |
entry->cdrname, colptr, entry->negatefiltervalue ? "!" : "", entry->filtervalue); |
| 439 |
goto early_release; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
if (ast_strlen_zero(entry->name)) |
| 444 |
continue; |
| 445 |
|
| 446 |
LENGTHEN_BUF1(strlen(entry->name)); |
| 447 |
|
| 448 |
switch (entry->type) { |
| 449 |
case SQL_CHAR: |
| 450 |
case SQL_VARCHAR: |
| 451 |
case SQL_LONGVARCHAR: |
| 452 |
#ifdef HAVE_ODBC_WCHAR |
| 453 |
case SQL_WCHAR: |
| 454 |
case SQL_WVARCHAR: |
| 455 |
case SQL_WLONGVARCHAR: |
| 456 |
#endif |
| 457 |
case SQL_BINARY: |
| 458 |
case SQL_VARBINARY: |
| 459 |
case SQL_LONGVARBINARY: |
| 460 |
case SQL_GUID: |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
if (strcasecmp(entry->name, "disposition") == 0) { |
| 465 |
ast_cdr_format_var(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0); |
| 466 |
} else if (strcasecmp(entry->name, "amaflags") == 0) { |
| 467 |
ast_cdr_format_var(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
if (entry->type != SQL_GUID) { |
| 472 |
if (strlen(colptr) > entry->octetlen) { |
| 473 |
colptr[entry->octetlen] = '\0'; |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
LENGTHEN_BUF2(strlen(colptr)); |
| 478 |
|
| 479 |
|
| 480 |
ast_str_append(&sql2, 0, "%s'", separator); |
| 481 |
for (tmp = colptr; *tmp; tmp++) { |
| 482 |
if (*tmp == '\'') { |
| 483 |
ast_str_append(&sql2, 0, "''"); |
| 484 |
} else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) { |
| 485 |
ast_str_append(&sql2, 0, "\\\\"); |
| 486 |
} else { |
| 487 |
ast_str_append(&sql2, 0, "%c", *tmp); |
| 488 |
} |
| 489 |
} |
| 490 |
ast_str_append(&sql2, 0, "'"); |
| 491 |
break; |
| 492 |
case SQL_TYPE_DATE: |
| 493 |
if (ast_strlen_zero(colptr)) { |
| 494 |
continue; |
| 495 |
} else { |
| 496 |
int year = 0, month = 0, day = 0; |
| 497 |
if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 || |
| 498 |
month <= 0 || month > 12 || day < 0 || day > 31 || |
| 499 |
((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) || |
| 500 |
(month == 2 && year % 400 == 0 && day > 29) || |
| 501 |
(month == 2 && year % 100 == 0 && day > 28) || |
| 502 |
(month == 2 && year % 4 == 0 && day > 29) || |
| 503 |
(month == 2 && year % 4 != 0 && day > 28)) { |
| 504 |
ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr); |
| 505 |
continue; |
| 506 |
} |
| 507 |
|
| 508 |
if (year > 0 && year < 100) { |
| 509 |
year += 2000; |
| 510 |
} |
| 511 |
|
| 512 |
LENGTHEN_BUF2(17); |
| 513 |
ast_str_append(&sql2, 0, "%s{ d '%04d-%02d-%02d' }", separator, year, month, day); |
| 514 |
} |
| 515 |
break; |
| 516 |
case SQL_TYPE_TIME: |
| 517 |
if (ast_strlen_zero(colptr)) { |
| 518 |
continue; |
| 519 |
} else { |
| 520 |
int hour = 0, minute = 0, second = 0; |
| 521 |
int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second); |
| 522 |
|
| 523 |
if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) { |
| 524 |
ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr); |
| 525 |
continue; |
| 526 |
} |
| 527 |
|
| 528 |
LENGTHEN_BUF2(15); |
| 529 |
ast_str_append(&sql2, 0, "%s{ t '%02d:%02d:%02d' }", separator, hour, minute, second); |
| 530 |
} |
| 531 |
break; |
| 532 |
case SQL_TYPE_TIMESTAMP: |
| 533 |
case SQL_TIMESTAMP: |
| 534 |
case SQL_DATETIME: |
| 535 |
if (ast_strlen_zero(colptr)) { |
| 536 |
continue; |
| 537 |
} else { |
| 538 |
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; |
| 539 |
int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second); |
| 540 |
|
| 541 |
if ((count != 3 && count != 5 && count != 6) || year <= 0 || |
| 542 |
month <= 0 || month > 12 || day < 0 || day > 31 || |
| 543 |
((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) || |
| 544 |
(month == 2 && year % 400 == 0 && day > 29) || |
| 545 |
(month == 2 && year % 100 == 0 && day > 28) || |
| 546 |
(month == 2 && year % 4 == 0 && day > 29) || |
| 547 |
(month == 2 && year % 4 != 0 && day > 28) || |
| 548 |
hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) { |
| 549 |
ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr); |
| 550 |
continue; |
| 551 |
} |
| 552 |
|
| 553 |
if (year > 0 && year < 100) { |
| 554 |
year += 2000; |
| 555 |
} |
| 556 |
|
| 557 |
LENGTHEN_BUF2(26); |
| 558 |
ast_str_append(&sql2, 0, "%s{ ts '%04d-%02d-%02d %02d:%02d:%02d' }", separator, year, month, day, hour, minute, second); |
| 559 |
} |
| 560 |
break; |
| 561 |
case SQL_INTEGER: |
| 562 |
if (ast_strlen_zero(colptr)) { |
| 563 |
continue; |
| 564 |
} else { |
| 565 |
int integer = 0; |
| 566 |
if (sscanf(colptr, "%30d", &integer) != 1) { |
| 567 |
ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name); |
| 568 |
continue; |
| 569 |
} |
| 570 |
|
| 571 |
LENGTHEN_BUF2(12); |
| 572 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 573 |
} |
| 574 |
break; |
| 575 |
case SQL_BIGINT: |
| 576 |
if (ast_strlen_zero(colptr)) { |
| 577 |
continue; |
| 578 |
} else { |
| 579 |
long long integer = 0; |
| 580 |
if (sscanf(colptr, "%30lld", &integer) != 1) { |
| 581 |
ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name); |
| 582 |
continue; |
| 583 |
} |
| 584 |
|
| 585 |
LENGTHEN_BUF2(24); |
| 586 |
ast_str_append(&sql2, 0, "%s%lld", separator, integer); |
| 587 |
} |
| 588 |
break; |
| 589 |
case SQL_SMALLINT: |
| 590 |
if (ast_strlen_zero(colptr)) { |
| 591 |
continue; |
| 592 |
} else { |
| 593 |
short integer = 0; |
| 594 |
if (sscanf(colptr, "%30hd", &integer) != 1) { |
| 595 |
ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name); |
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
LENGTHEN_BUF2(6); |
| 600 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 601 |
} |
| 602 |
break; |
| 603 |
case SQL_TINYINT: |
| 604 |
if (ast_strlen_zero(colptr)) { |
| 605 |
continue; |
| 606 |
} else { |
| 607 |
signed char integer = 0; |
| 608 |
if (sscanf(colptr, "%30hhd", &integer) != 1) { |
| 609 |
ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name); |
| 610 |
continue; |
| 611 |
} |
| 612 |
|
| 613 |
LENGTHEN_BUF2(4); |
| 614 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 615 |
} |
| 616 |
break; |
| 617 |
case SQL_BIT: |
| 618 |
if (ast_strlen_zero(colptr)) { |
| 619 |
continue; |
| 620 |
} else { |
| 621 |
signed char integer = 0; |
| 622 |
if (sscanf(colptr, "%30hhd", &integer) != 1) { |
| 623 |
ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name); |
| 624 |
continue; |
| 625 |
} |
| 626 |
if (integer != 0) |
| 627 |
integer = 1; |
| 628 |
|
| 629 |
LENGTHEN_BUF2(2); |
| 630 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 631 |
} |
| 632 |
break; |
| 633 |
case SQL_NUMERIC: |
| 634 |
case SQL_DECIMAL: |
| 635 |
if (ast_strlen_zero(colptr)) { |
| 636 |
continue; |
| 637 |
} else { |
| 638 |
double number = 0.0; |
| 639 |
|
| 640 |
if (!strcasecmp(entry->cdrname, "billsec")) { |
| 641 |
if (!ast_tvzero(cdr->answer)) { |
| 642 |
snprintf(colbuf, sizeof(colbuf), "%lf", |
| 643 |
(double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0)); |
| 644 |
} else { |
| 645 |
ast_copy_string(colbuf, "0", sizeof(colbuf)); |
| 646 |
} |
| 647 |
} else if (!strcasecmp(entry->cdrname, "duration")) { |
| 648 |
snprintf(colbuf, sizeof(colbuf), "%lf", |
| 649 |
(double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0)); |
| 650 |
|
| 651 |
if (!ast_strlen_zero(colbuf)) { |
| 652 |
colptr = colbuf; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
if (sscanf(colptr, "%30lf", &number) != 1) { |
| 657 |
ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name); |
| 658 |
continue; |
| 659 |
} |
| 660 |
|
| 661 |
LENGTHEN_BUF2(entry->decimals); |
| 662 |
ast_str_append(&sql2, 0, "%s%*.*lf", separator, entry->decimals, entry->radix, number); |
| 663 |
} |
| 664 |
break; |
| 665 |
case SQL_FLOAT: |
| 666 |
case SQL_REAL: |
| 667 |
case SQL_DOUBLE: |
| 668 |
if (ast_strlen_zero(colptr)) { |
| 669 |
continue; |
| 670 |
} else { |
| 671 |
double number = 0.0; |
| 672 |
|
| 673 |
if (!strcasecmp(entry->cdrname, "billsec")) { |
| 674 |
if (!ast_tvzero(cdr->answer)) { |
| 675 |
snprintf(colbuf, sizeof(colbuf), "%lf", |
| 676 |
(double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0)); |
| 677 |
} else { |
| 678 |
ast_copy_string(colbuf, "0", sizeof(colbuf)); |
| 679 |
} |
| 680 |
} else if (!strcasecmp(entry->cdrname, "duration")) { |
| 681 |
snprintf(colbuf, sizeof(colbuf), "%lf", |
| 682 |
(double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0)); |
| 683 |
|
| 684 |
if (!ast_strlen_zero(colbuf)) { |
| 685 |
colptr = colbuf; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
if (sscanf(colptr, "%30lf", &number) != 1) { |
| 690 |
ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name); |
| 691 |
continue; |
| 692 |
} |
| 693 |
|
| 694 |
LENGTHEN_BUF2(entry->decimals); |
| 695 |
ast_str_append(&sql2, 0, "%s%lf", separator, number); |
| 696 |
} |
| 697 |
break; |
| 698 |
default: |
| 699 |
ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name); |
| 700 |
continue; |
| 701 |
} |
| 702 |
if (quoted) { |
| 703 |
ast_str_append(&sql, 0, "%s%c%s%c", separator, tableptr->quoted_identifiers, entry->name, tableptr->quoted_identifiers); |
| 704 |
} else { |
| 705 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 706 |
} |
| 707 |
separator = ", "; |
| 708 |
} else if (entry->filtervalue |
| 709 |
&& ((!entry->negatefiltervalue && entry->filtervalue[0] != '\0') |
| 710 |
|| (entry->negatefiltervalue && entry->filtervalue[0] == '\0'))) { |
| 711 |
ast_verb(4, "CDR column '%s' was not set and does not match filter of" |
| 712 |
" %s'%s'. Cancelling this CDR.\n", |
| 713 |
entry->cdrname, entry->negatefiltervalue ? "!" : "", |
| 714 |
entry->filtervalue); |
| 715 |
goto early_release; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
LENGTHEN_BUF1(ast_str_strlen(sql2)); |
| 721 |
ast_str_append(&sql, 0, ")"); |
| 722 |
ast_str_append(&sql2, 0, ")"); |
| 723 |
ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2)); |
| 724 |
|
| 725 |
ast_debug(3, "Executing [%s]\n", ast_str_buffer(sql)); |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
| 730 |
if (!SQL_SUCCEEDED(res)) { |
| 731 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", |
| 732 |
tableptr->connection); |
| 733 |
goto early_release; |
| 734 |
} |
| 735 |
|
| 736 |
res = ast_odbc_execute_sql(obj, stmt, ast_str_buffer(sql)); |
| 737 |
if (!SQL_SUCCEEDED(res)) { |
| 738 |
ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql)); |
| 739 |
} |
| 740 |
|
| 741 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 742 |
|
| 743 |
early_release: |
| 744 |
ast_odbc_release_obj(obj); |
| 745 |
} |
| 746 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 747 |
|
| 748 |
|
| 749 |
if (ast_str_strlen(sql) > maxsize) { |
| 750 |
maxsize = ast_str_strlen(sql); |
| 751 |
} |
| 752 |
if (ast_str_strlen(sql2) > maxsize2) { |
| 753 |
maxsize2 = ast_str_strlen(sql2); |
| 754 |
} |
| 755 |
|
| 756 |
ast_free(sql); |
| 757 |
ast_free(sql2); |
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
static int unload_module(void) |
| 762 |
{ |
| 763 |
if (ast_cdr_unregister(name)) { |
| 764 |
return -1; |
| 765 |
} |
| 766 |
|
| 767 |
if (AST_RWLIST_WRLOCK(&odbc_tables)) { |
| 768 |
ast_cdr_register(name, ast_module_info->description, odbc_log); |
| 769 |
ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n"); |
| 770 |
return -1; |
| 771 |
} |
| 772 |
|
| 773 |
free_config(); |
| 774 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
static int load_module(void) |
| 779 |
{ |
| 780 |
if (AST_RWLIST_WRLOCK(&odbc_tables)) { |
| 781 |
ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n"); |
| 782 |
return 0; |
| 783 |
} |
| 784 |
|
| 785 |
load_config(); |
| 786 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 787 |
ast_cdr_register(name, ast_module_info->description, odbc_log); |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
static int reload(void) |
| 792 |
{ |
| 793 |
if (AST_RWLIST_WRLOCK(&odbc_tables)) { |
| 794 |
ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n"); |
| 795 |
return -1; |
| 796 |
} |
| 797 |
|
| 798 |
free_config(); |
| 799 |
load_config(); |
| 800 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 801 |
return 0; |
| 802 |
} |
| 803 |
|
| 804 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Adaptive ODBC CDR backend", |
| 805 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 806 |
.load = load_module, |
| 807 |
.unload = unload_module, |
| 808 |
.reload = reload, |
| 809 |
.load_pri = AST_MODPRI_CDR_DRIVER, |
| 810 |
.requires = "cdr,res_odbc", |
| 811 |
); |