| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#include "asterisk.h" |
| 63 |
|
| 64 |
#include "asterisk/config.h" |
| 65 |
#include "asterisk/channel.h" |
| 66 |
#include "asterisk/cdr.h" |
| 67 |
#include "asterisk/module.h" |
| 68 |
|
| 69 |
#include <sqlfront.h> |
| 70 |
#include <sybdb.h> |
| 71 |
|
| 72 |
#define DATE_FORMAT "%Y/%m/%d %T" |
| 73 |
|
| 74 |
static const char name[] = "FreeTDS (MSSQL)"; |
| 75 |
static const char config[] = "cdr_tds.conf"; |
| 76 |
|
| 77 |
struct cdr_tds_config { |
| 78 |
AST_DECLARE_STRING_FIELDS( |
| 79 |
AST_STRING_FIELD(hostname); |
| 80 |
AST_STRING_FIELD(database); |
| 81 |
AST_STRING_FIELD(username); |
| 82 |
AST_STRING_FIELD(password); |
| 83 |
AST_STRING_FIELD(table); |
| 84 |
AST_STRING_FIELD(charset); |
| 85 |
AST_STRING_FIELD(language); |
| 86 |
AST_STRING_FIELD(hrtime); |
| 87 |
); |
| 88 |
DBPROCESS *dbproc; |
| 89 |
unsigned int connected:1; |
| 90 |
unsigned int has_userfield:1; |
| 91 |
}; |
| 92 |
|
| 93 |
AST_MUTEX_DEFINE_STATIC(tds_lock); |
| 94 |
|
| 95 |
static struct cdr_tds_config *settings; |
| 96 |
|
| 97 |
static char *anti_injection(const char *, int); |
| 98 |
static void get_date(char *, size_t len, struct timeval); |
| 99 |
|
| 100 |
static int execute_and_consume(DBPROCESS *dbproc, const char *fmt, ...) |
| 101 |
__attribute__((format(printf, 2, 3))); |
| 102 |
|
| 103 |
static int mssql_connect(void); |
| 104 |
static int mssql_disconnect(void); |
| 105 |
|
| 106 |
static int tds_log(struct ast_cdr *cdr) |
| 107 |
{ |
| 108 |
char start[80], answer[80], end[80]; |
| 109 |
char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid, *userfield = NULL; |
| 110 |
RETCODE erc; |
| 111 |
int res = -1; |
| 112 |
int attempt = 1; |
| 113 |
|
| 114 |
accountcode = anti_injection(cdr->accountcode, 20); |
| 115 |
src = anti_injection(cdr->src, 80); |
| 116 |
dst = anti_injection(cdr->dst, 80); |
| 117 |
dcontext = anti_injection(cdr->dcontext, 80); |
| 118 |
clid = anti_injection(cdr->clid, 80); |
| 119 |
channel = anti_injection(cdr->channel, 80); |
| 120 |
dstchannel = anti_injection(cdr->dstchannel, 80); |
| 121 |
lastapp = anti_injection(cdr->lastapp, 80); |
| 122 |
lastdata = anti_injection(cdr->lastdata, 80); |
| 123 |
uniqueid = anti_injection(cdr->uniqueid, 32); |
| 124 |
|
| 125 |
get_date(start, sizeof(start), cdr->start); |
| 126 |
get_date(answer, sizeof(answer), cdr->answer); |
| 127 |
get_date(end, sizeof(end), cdr->end); |
| 128 |
|
| 129 |
ast_mutex_lock(&tds_lock); |
| 130 |
|
| 131 |
if (settings->has_userfield) { |
| 132 |
userfield = anti_injection(cdr->userfield, AST_MAX_USER_FIELD); |
| 133 |
} |
| 134 |
|
| 135 |
retry: |
| 136 |
|
| 137 |
if (!settings->connected) { |
| 138 |
ast_log(LOG_NOTICE, "Attempting to reconnect to %s (Attempt %d)\n", settings->hostname, attempt); |
| 139 |
if (mssql_connect()) { |
| 140 |
|
| 141 |
if (attempt++ < 3) { |
| 142 |
goto retry; |
| 143 |
} |
| 144 |
goto done; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (settings->has_userfield) { |
| 149 |
if (settings->hrtime) { |
| 150 |
double hrbillsec = 0.0; |
| 151 |
double hrduration; |
| 152 |
|
| 153 |
if (!ast_tvzero(cdr->answer)) { |
| 154 |
hrbillsec = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0); |
| 155 |
} |
| 156 |
hrduration = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0); |
| 157 |
|
| 158 |
erc = dbfcmd(settings->dbproc, |
| 159 |
"INSERT INTO %s " |
| 160 |
"(" |
| 161 |
"accountcode, src, dst, dcontext, clid, channel, " |
| 162 |
"dstchannel, lastapp, lastdata, start, answer, [end], duration, " |
| 163 |
"billsec, disposition, amaflags, uniqueid, userfield" |
| 164 |
") " |
| 165 |
"VALUES " |
| 166 |
"(" |
| 167 |
"'%s', '%s', '%s', '%s', '%s', '%s', " |
| 168 |
"'%s', '%s', '%s', %s, %s, %s, %lf, " |
| 169 |
"%lf, '%s', '%s', '%s', '%s'" |
| 170 |
")", |
| 171 |
settings->table, |
| 172 |
accountcode, src, dst, dcontext, clid, channel, |
| 173 |
dstchannel, lastapp, lastdata, start, answer, end, hrduration, |
| 174 |
hrbillsec, ast_cdr_disp2str(cdr->disposition), ast_channel_amaflags2string(cdr->amaflags), uniqueid, |
| 175 |
userfield |
| 176 |
); |
| 177 |
} else { |
| 178 |
erc = dbfcmd(settings->dbproc, |
| 179 |
"INSERT INTO %s " |
| 180 |
"(" |
| 181 |
"accountcode, src, dst, dcontext, clid, channel, " |
| 182 |
"dstchannel, lastapp, lastdata, start, answer, [end], duration, " |
| 183 |
"billsec, disposition, amaflags, uniqueid, userfield" |
| 184 |
") " |
| 185 |
"VALUES " |
| 186 |
"(" |
| 187 |
"'%s', '%s', '%s', '%s', '%s', '%s', " |
| 188 |
"'%s', '%s', '%s', %s, %s, %s, %ld, " |
| 189 |
"%ld, '%s', '%s', '%s', '%s'" |
| 190 |
")", |
| 191 |
settings->table, |
| 192 |
accountcode, src, dst, dcontext, clid, channel, |
| 193 |
dstchannel, lastapp, lastdata, start, answer, end, cdr->duration, |
| 194 |
cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_channel_amaflags2string(cdr->amaflags), uniqueid, |
| 195 |
userfield |
| 196 |
); |
| 197 |
} |
| 198 |
} else { |
| 199 |
if (settings->hrtime) { |
| 200 |
double hrbillsec = 0.0; |
| 201 |
double hrduration; |
| 202 |
|
| 203 |
if (!ast_tvzero(cdr->answer)) { |
| 204 |
hrbillsec = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0); |
| 205 |
} |
| 206 |
hrduration = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0); |
| 207 |
|
| 208 |
erc = dbfcmd(settings->dbproc, |
| 209 |
"INSERT INTO %s " |
| 210 |
"(" |
| 211 |
"accountcode, src, dst, dcontext, clid, channel, " |
| 212 |
"dstchannel, lastapp, lastdata, start, answer, [end], duration, " |
| 213 |
"billsec, disposition, amaflags, uniqueid" |
| 214 |
") " |
| 215 |
"VALUES " |
| 216 |
"(" |
| 217 |
"'%s', '%s', '%s', '%s', '%s', '%s', " |
| 218 |
"'%s', '%s', '%s', %s, %s, %s, %lf, " |
| 219 |
"%lf, '%s', '%s', '%s'" |
| 220 |
")", |
| 221 |
settings->table, |
| 222 |
accountcode, src, dst, dcontext, clid, channel, |
| 223 |
dstchannel, lastapp, lastdata, start, answer, end, hrduration, |
| 224 |
hrbillsec, ast_cdr_disp2str(cdr->disposition), ast_channel_amaflags2string(cdr->amaflags), uniqueid |
| 225 |
); |
| 226 |
} else { |
| 227 |
erc = dbfcmd(settings->dbproc, |
| 228 |
"INSERT INTO %s " |
| 229 |
"(" |
| 230 |
"accountcode, src, dst, dcontext, clid, channel, " |
| 231 |
"dstchannel, lastapp, lastdata, start, answer, [end], duration, " |
| 232 |
"billsec, disposition, amaflags, uniqueid" |
| 233 |
") " |
| 234 |
"VALUES " |
| 235 |
"(" |
| 236 |
"'%s', '%s', '%s', '%s', '%s', '%s', " |
| 237 |
"'%s', '%s', '%s', %s, %s, %s, %ld, " |
| 238 |
"%ld, '%s', '%s', '%s'" |
| 239 |
")", |
| 240 |
settings->table, |
| 241 |
accountcode, src, dst, dcontext, clid, channel, |
| 242 |
dstchannel, lastapp, lastdata, start, answer, end, cdr->duration, |
| 243 |
cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_channel_amaflags2string(cdr->amaflags), uniqueid |
| 244 |
); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if (erc == FAIL) { |
| 249 |
if (attempt++ < 3) { |
| 250 |
ast_log(LOG_NOTICE, "Failed to build INSERT statement, retrying...\n"); |
| 251 |
mssql_disconnect(); |
| 252 |
goto retry; |
| 253 |
} else { |
| 254 |
ast_log(LOG_ERROR, "Failed to build INSERT statement, no CDR was logged.\n"); |
| 255 |
goto done; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
if (dbsqlexec(settings->dbproc) == FAIL) { |
| 260 |
if (attempt++ < 3) { |
| 261 |
ast_log(LOG_NOTICE, "Failed to execute INSERT statement, retrying...\n"); |
| 262 |
mssql_disconnect(); |
| 263 |
goto retry; |
| 264 |
} else { |
| 265 |
ast_log(LOG_ERROR, "Failed to execute INSERT statement, no CDR was logged.\n"); |
| 266 |
goto done; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
while (dbresults(settings->dbproc) != NO_MORE_RESULTS) { |
| 273 |
while (dbnextrow(settings->dbproc) != NO_MORE_ROWS); |
| 274 |
} |
| 275 |
|
| 276 |
res = 0; |
| 277 |
|
| 278 |
done: |
| 279 |
ast_mutex_unlock(&tds_lock); |
| 280 |
|
| 281 |
ast_free(accountcode); |
| 282 |
ast_free(src); |
| 283 |
ast_free(dst); |
| 284 |
ast_free(dcontext); |
| 285 |
ast_free(clid); |
| 286 |
ast_free(channel); |
| 287 |
ast_free(dstchannel); |
| 288 |
ast_free(lastapp); |
| 289 |
ast_free(lastdata); |
| 290 |
ast_free(uniqueid); |
| 291 |
|
| 292 |
if (userfield) { |
| 293 |
ast_free(userfield); |
| 294 |
} |
| 295 |
|
| 296 |
return res; |
| 297 |
} |
| 298 |
|
| 299 |
static char *anti_injection(const char *str, int len) |
| 300 |
{ |
| 301 |
|
| 302 |
char *buf; |
| 303 |
char *buf_ptr, *srh_ptr; |
| 304 |
char *known_bad[] = {"select", "insert", "update", "delete", "drop", ";", "--", "\0"}; |
| 305 |
int idx; |
| 306 |
|
| 307 |
if (!(buf = ast_calloc(1, len + 1))) { |
| 308 |
ast_log(LOG_ERROR, "Out of memory\n"); |
| 309 |
return NULL; |
| 310 |
} |
| 311 |
|
| 312 |
buf_ptr = buf; |
| 313 |
|
| 314 |
|
| 315 |
for (; *str && strlen(buf) < len; str++) { |
| 316 |
if (*str == '\'') { |
| 317 |
*buf_ptr++ = '\''; |
| 318 |
} |
| 319 |
*buf_ptr++ = *str; |
| 320 |
} |
| 321 |
*buf_ptr = '\0'; |
| 322 |
|
| 323 |
|
| 324 |
for (idx = 0; *known_bad[idx]; idx++) { |
| 325 |
while ((srh_ptr = strcasestr(buf, known_bad[idx]))) { |
| 326 |
memmove(srh_ptr, srh_ptr + strlen(known_bad[idx]), strlen(srh_ptr + strlen(known_bad[idx])) + 1); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return buf; |
| 331 |
} |
| 332 |
|
| 333 |
static void get_date(char *dateField, size_t len, struct timeval when) |
| 334 |
{ |
| 335 |
|
| 336 |
if (!ast_tvzero(when)) { |
| 337 |
struct ast_tm tm; |
| 338 |
ast_localtime(&when, &tm, NULL); |
| 339 |
ast_strftime(dateField, len, "'" DATE_FORMAT "'", &tm); |
| 340 |
} else { |
| 341 |
ast_copy_string(dateField, "null", len); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
static int execute_and_consume(DBPROCESS *dbproc, const char *fmt, ...) |
| 346 |
{ |
| 347 |
va_list ap; |
| 348 |
char *buffer; |
| 349 |
|
| 350 |
va_start(ap, fmt); |
| 351 |
if (ast_vasprintf(&buffer, fmt, ap) < 0) { |
| 352 |
va_end(ap); |
| 353 |
return 1; |
| 354 |
} |
| 355 |
va_end(ap); |
| 356 |
|
| 357 |
if (dbfcmd(dbproc, buffer) == FAIL) { |
| 358 |
ast_free(buffer); |
| 359 |
return 1; |
| 360 |
} |
| 361 |
|
| 362 |
ast_free(buffer); |
| 363 |
|
| 364 |
if (dbsqlexec(dbproc) == FAIL) { |
| 365 |
return 1; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
while (dbresults(dbproc) != NO_MORE_RESULTS) { |
| 370 |
while (dbnextrow(dbproc) != NO_MORE_ROWS); |
| 371 |
} |
| 372 |
|
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static int mssql_disconnect(void) |
| 377 |
{ |
| 378 |
if (settings->dbproc) { |
| 379 |
dbclose(settings->dbproc); |
| 380 |
settings->dbproc = NULL; |
| 381 |
} |
| 382 |
|
| 383 |
settings->connected = 0; |
| 384 |
|
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
static int mssql_connect(void) |
| 389 |
{ |
| 390 |
LOGINREC *login; |
| 391 |
|
| 392 |
if ((login = dblogin()) == NULL) { |
| 393 |
ast_log(LOG_ERROR, "Unable to allocate login structure for db-lib\n"); |
| 394 |
return -1; |
| 395 |
} |
| 396 |
|
| 397 |
DBSETLAPP(login, "TSQL"); |
| 398 |
DBSETLUSER(login, (char *) settings->username); |
| 399 |
DBSETLPWD(login, (char *) settings->password); |
| 400 |
DBSETLCHARSET(login, (char *) settings->charset); |
| 401 |
DBSETLNATLANG(login, (char *) settings->language); |
| 402 |
|
| 403 |
if ((settings->dbproc = dbopen(login, (char *) settings->hostname)) == NULL) { |
| 404 |
ast_log(LOG_ERROR, "Unable to connect to %s\n", settings->hostname); |
| 405 |
dbloginfree(login); |
| 406 |
return -1; |
| 407 |
} |
| 408 |
|
| 409 |
dbloginfree(login); |
| 410 |
|
| 411 |
if (dbuse(settings->dbproc, (char *) settings->database) == FAIL) { |
| 412 |
ast_log(LOG_ERROR, "Unable to select database %s\n", settings->database); |
| 413 |
goto failed; |
| 414 |
} |
| 415 |
|
| 416 |
if (execute_and_consume(settings->dbproc, "SELECT 1 FROM [%s] WHERE 1 = 0", settings->table)) { |
| 417 |
ast_log(LOG_ERROR, "Unable to find table '%s'\n", settings->table); |
| 418 |
goto failed; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
if (execute_and_consume(settings->dbproc, "SELECT userfield FROM [%s] WHERE 1 = 0", settings->table)) { |
| 423 |
ast_log(LOG_NOTICE, "Unable to find 'userfield' column in table '%s'\n", settings->table); |
| 424 |
settings->has_userfield = 0; |
| 425 |
} else { |
| 426 |
settings->has_userfield = 1; |
| 427 |
} |
| 428 |
|
| 429 |
settings->connected = 1; |
| 430 |
|
| 431 |
return 0; |
| 432 |
|
| 433 |
failed: |
| 434 |
dbclose(settings->dbproc); |
| 435 |
settings->dbproc = NULL; |
| 436 |
return -1; |
| 437 |
} |
| 438 |
|
| 439 |
static int tds_unload_module(void) |
| 440 |
{ |
| 441 |
if (ast_cdr_unregister(name)) { |
| 442 |
return -1; |
| 443 |
} |
| 444 |
|
| 445 |
if (settings) { |
| 446 |
ast_mutex_lock(&tds_lock); |
| 447 |
mssql_disconnect(); |
| 448 |
ast_mutex_unlock(&tds_lock); |
| 449 |
|
| 450 |
ast_string_field_free_memory(settings); |
| 451 |
ast_free(settings); |
| 452 |
} |
| 453 |
|
| 454 |
dbexit(); |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static int tds_error_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr) |
| 460 |
{ |
| 461 |
ast_log(LOG_ERROR, "%s (%d)\n", dberrstr, dberr); |
| 462 |
|
| 463 |
if (oserr != DBNOERR) { |
| 464 |
ast_log(LOG_ERROR, "%s (%d)\n", oserrstr, oserr); |
| 465 |
} |
| 466 |
|
| 467 |
return INT_CANCEL; |
| 468 |
} |
| 469 |
|
| 470 |
static int tds_message_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line) |
| 471 |
{ |
| 472 |
ast_debug(1, "Msg %d, Level %d, State %d, Line %d\n", msgno, severity, msgstate, line); |
| 473 |
ast_log(LOG_NOTICE, "%s\n", msgtext); |
| 474 |
|
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 478 |
static int tds_load_module(int reload) |
| 479 |
{ |
| 480 |
struct ast_config *cfg; |
| 481 |
const char *ptr = NULL; |
| 482 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 483 |
|
| 484 |
cfg = ast_config_load(config, config_flags); |
| 485 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 486 |
ast_log(LOG_NOTICE, "Unable to load TDS config for CDRs: %s\n", config); |
| 487 |
return 0; |
| 488 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) |
| 489 |
return 0; |
| 490 |
|
| 491 |
if (!ast_variable_browse(cfg, "global")) { |
| 492 |
|
| 493 |
ast_config_destroy(cfg); |
| 494 |
return 0; |
| 495 |
} |
| 496 |
|
| 497 |
ast_mutex_lock(&tds_lock); |
| 498 |
|
| 499 |
|
| 500 |
ast_string_field_init(settings, 0); |
| 501 |
|
| 502 |
|
| 503 |
ptr = ast_variable_retrieve(cfg, "global", "connection"); |
| 504 |
if (ptr) { |
| 505 |
ast_string_field_set(settings, hostname, ptr); |
| 506 |
} else { |
| 507 |
|
| 508 |
ptr = ast_variable_retrieve(cfg, "global", "hostname"); |
| 509 |
if (ptr) { |
| 510 |
ast_string_field_set(settings, hostname, ptr); |
| 511 |
} else { |
| 512 |
ast_log(LOG_ERROR, "Failed to connect: Database server connection not specified.\n"); |
| 513 |
goto failed; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
ptr = ast_variable_retrieve(cfg, "global", "dbname"); |
| 518 |
if (ptr) { |
| 519 |
ast_string_field_set(settings, database, ptr); |
| 520 |
} else { |
| 521 |
ast_log(LOG_ERROR, "Failed to connect: Database dbname not specified.\n"); |
| 522 |
goto failed; |
| 523 |
} |
| 524 |
|
| 525 |
ptr = ast_variable_retrieve(cfg, "global", "user"); |
| 526 |
if (ptr) { |
| 527 |
ast_string_field_set(settings, username, ptr); |
| 528 |
} else { |
| 529 |
ast_log(LOG_ERROR, "Failed to connect: Database dbuser not specified.\n"); |
| 530 |
goto failed; |
| 531 |
} |
| 532 |
|
| 533 |
ptr = ast_variable_retrieve(cfg, "global", "password"); |
| 534 |
if (ptr) { |
| 535 |
ast_string_field_set(settings, password, ptr); |
| 536 |
} else { |
| 537 |
ast_log(LOG_ERROR, "Failed to connect: Database password not specified.\n"); |
| 538 |
goto failed; |
| 539 |
} |
| 540 |
|
| 541 |
ptr = ast_variable_retrieve(cfg, "global", "charset"); |
| 542 |
if (ptr) { |
| 543 |
ast_string_field_set(settings, charset, ptr); |
| 544 |
} else { |
| 545 |
ast_string_field_set(settings, charset, "iso_1"); |
| 546 |
} |
| 547 |
|
| 548 |
ptr = ast_variable_retrieve(cfg, "global", "language"); |
| 549 |
if (ptr) { |
| 550 |
ast_string_field_set(settings, language, ptr); |
| 551 |
} else { |
| 552 |
ast_string_field_set(settings, language, "us_english"); |
| 553 |
} |
| 554 |
|
| 555 |
ptr = ast_variable_retrieve(cfg, "global", "table"); |
| 556 |
if (ptr) { |
| 557 |
ast_string_field_set(settings, table, ptr); |
| 558 |
} else { |
| 559 |
ast_log(LOG_NOTICE, "Table name not specified, using 'cdr' by default.\n"); |
| 560 |
ast_string_field_set(settings, table, "cdr"); |
| 561 |
} |
| 562 |
|
| 563 |
ptr = ast_variable_retrieve(cfg, "global", "hrtime"); |
| 564 |
if (ptr && ast_true(ptr)) { |
| 565 |
ast_string_field_set(settings, hrtime, ptr); |
| 566 |
} else { |
| 567 |
ast_log(LOG_NOTICE, "High Resolution Time not found, using integers for billsec and duration fields by default.\n"); |
| 568 |
} |
| 569 |
|
| 570 |
mssql_disconnect(); |
| 571 |
|
| 572 |
if (mssql_connect()) { |
| 573 |
|
| 574 |
goto failed; |
| 575 |
} |
| 576 |
|
| 577 |
ast_mutex_unlock(&tds_lock); |
| 578 |
ast_config_destroy(cfg); |
| 579 |
|
| 580 |
return 1; |
| 581 |
|
| 582 |
failed: |
| 583 |
ast_mutex_unlock(&tds_lock); |
| 584 |
ast_config_destroy(cfg); |
| 585 |
|
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
static int reload(void) |
| 590 |
{ |
| 591 |
return tds_load_module(1); |
| 592 |
} |
| 593 |
|
| 594 |
static int load_module(void) |
| 595 |
{ |
| 596 |
if (dbinit() == FAIL) { |
| 597 |
ast_log(LOG_ERROR, "Failed to initialize FreeTDS db-lib\n"); |
| 598 |
return AST_MODULE_LOAD_DECLINE; |
| 599 |
} |
| 600 |
|
| 601 |
dberrhandle(tds_error_handler); |
| 602 |
dbmsghandle(tds_message_handler); |
| 603 |
|
| 604 |
settings = ast_calloc_with_stringfields(1, struct cdr_tds_config, 256); |
| 605 |
|
| 606 |
if (!settings) { |
| 607 |
dbexit(); |
| 608 |
return AST_MODULE_LOAD_DECLINE; |
| 609 |
} |
| 610 |
|
| 611 |
if (!tds_load_module(0)) { |
| 612 |
ast_string_field_free_memory(settings); |
| 613 |
ast_free(settings); |
| 614 |
settings = NULL; |
| 615 |
dbexit(); |
| 616 |
return AST_MODULE_LOAD_DECLINE; |
| 617 |
} |
| 618 |
|
| 619 |
ast_cdr_register(name, ast_module_info->description, tds_log); |
| 620 |
|
| 621 |
return AST_MODULE_LOAD_SUCCESS; |
| 622 |
} |
| 623 |
|
| 624 |
static int unload_module(void) |
| 625 |
{ |
| 626 |
return tds_unload_module(); |
| 627 |
} |
| 628 |
|
| 629 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "FreeTDS CDR Backend", |
| 630 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 631 |
.load = load_module, |
| 632 |
.unload = unload_module, |
| 633 |
.reload = reload, |
| 634 |
.load_pri = AST_MODPRI_CDR_DRIVER, |
| 635 |
.requires = "cdr", |
| 636 |
); |