ODBCHandler::ODBCHandler(const char *datasource) { urlCon = datasource; if(datasource != 0 ) { fprintf(stderr,"Init datasource %s",datasource); odbc_init(datasource); } }
static int odbc_log(struct ast_cdr *cdr) { int res = 0; char timestr[150]; ast_mutex_lock(&odbc_lock); build_query(cdr, timestr, sizeof(timestr)); if (connected) { res = odbc_do_query(); if (res < 0) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn); odbc_disconnect(); res = odbc_init(); if (res < 0) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn); odbc_disconnect(); } else { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n"); SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); build_query(cdr, timestr, sizeof(timestr)); /* what a waste. If we have to reconnect, we have to build a new query */ res = odbc_do_query(); if (res < 0) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n"); } } } } else { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n"); } SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); ast_mutex_unlock(&odbc_lock); return 0; }
static int odbc_log(struct ast_cdr *cdr) { int ODBC_res; char sqlcmd[2048] = "", timestr[128]; int res = 0; struct tm tm; if (usegmtime) gmtime_r(&cdr->start.tv_sec,&tm); else ast_localtime(&cdr->start.tv_sec, &tm, NULL); ast_mutex_lock(&odbc_lock); strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); memset(sqlcmd,0,2048); if (loguniqueid) { snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s " "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp," "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) " "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table); } else { snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s " "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata," "duration,billsec,disposition,amaflags,accountcode) " "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table); } if (!connected) { res = odbc_init(); if (res < 0) { odbc_disconnect(); ast_mutex_unlock(&odbc_lock); return 0; } } ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt); if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res); SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); odbc_disconnect(); ast_mutex_unlock(&odbc_lock); return 0; } /* We really should only have to do this once. But for some strange reason if I don't it blows holes in memory like like a shotgun. So we just do this so its safe. */ ODBC_res = SQLPrepare(ODBC_stmt, (unsigned char *)sqlcmd, SQL_NTS); if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res); SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); odbc_disconnect(); ast_mutex_unlock(&odbc_lock); return 0; } SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(timestr), 0, ×tr, 0, NULL); SQLBindParameter(ODBC_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL); SQLBindParameter(ODBC_stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL); SQLBindParameter(ODBC_stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL); SQLBindParameter(ODBC_stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL); SQLBindParameter(ODBC_stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL); SQLBindParameter(ODBC_stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL); SQLBindParameter(ODBC_stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL); SQLBindParameter(ODBC_stmt, 9, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL); SQLBindParameter(ODBC_stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL); SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL); if (dispositionstring) SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(ast_cdr_disp2str(cdr->disposition)) + 1, 0, ast_cdr_disp2str(cdr->disposition), 0, NULL); else SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL); SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL); SQLBindParameter(ODBC_stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL); if (loguniqueid) { SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL); SQLBindParameter(ODBC_stmt, 16, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL); } if (connected) { res = odbc_do_query(); if (res < 0) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n"); if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn); SQLDisconnect(ODBC_con); res = odbc_init(); if (res < 0) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn); odbc_disconnect(); } else { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n"); res = odbc_do_query(); if (res < 0) { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n"); } } } } else { if (option_verbose > 10) ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n"); } SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); ast_mutex_unlock(&odbc_lock); return 0; }
static int odbc_load_module(void) { int res = 0; struct ast_config *cfg; struct ast_variable *var; const char *tmp; ast_mutex_lock(&odbc_lock); cfg = ast_config_load(config); if (!cfg) { ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config); res = AST_MODULE_LOAD_DECLINE; goto out; } var = ast_variable_browse(cfg, "global"); if (!var) { /* nothing configured */ goto out; } tmp = ast_variable_retrieve(cfg,"global","dsn"); if (tmp == NULL) { ast_log(LOG_WARNING,"cdr_odbc: dsn not specified. Assuming asteriskdb\n"); tmp = "asteriskdb"; } dsn = strdup(tmp); if (dsn == NULL) { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } tmp = ast_variable_retrieve(cfg,"global","dispositionstring"); if (tmp) { dispositionstring = ast_true(tmp); } else { dispositionstring = 0; } tmp = ast_variable_retrieve(cfg,"global","username"); if (tmp) { username = strdup(tmp); if (username == NULL) { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } } tmp = ast_variable_retrieve(cfg,"global","password"); if (tmp) { password = strdup(tmp); if (password == NULL) { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } } tmp = ast_variable_retrieve(cfg,"global","loguniqueid"); if (tmp) { loguniqueid = ast_true(tmp); if (loguniqueid) { ast_log(LOG_DEBUG,"cdr_odbc: Logging uniqueid\n"); } else { ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n"); } } else { ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n"); loguniqueid = 0; } tmp = ast_variable_retrieve(cfg,"global","usegmtime"); if (tmp) { usegmtime = ast_true(tmp); if (usegmtime) { ast_log(LOG_DEBUG,"cdr_odbc: Logging in GMT\n"); } else { ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n"); } } else { ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n"); usegmtime = 0; } tmp = ast_variable_retrieve(cfg,"global","table"); if (tmp == NULL) { ast_log(LOG_WARNING,"cdr_odbc: table not specified. Assuming cdr\n"); tmp = "cdr"; } table = strdup(tmp); if (table == NULL) { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } if (option_verbose > 2) { ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn); if (username) { ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: username is %s\n",username); ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: password is [secret]\n"); } else ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: retreiving username and password from odbc config\n"); ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: table is %s\n",table); } res = odbc_init(); if (res < 0) { ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn); if (option_verbose > 2) { ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: Unable to connect to datasource: %s\n", dsn); } } res = ast_cdr_register(name, ast_module_info->description, odbc_log); if (res) { ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n"); } out: if (cfg) ast_config_destroy(cfg); ast_mutex_unlock(&odbc_lock); return res; }
static int odbc_load_module(void) { int res = 0; struct ast_config *cfg; struct ast_variable *var; char *tmp; ast_mutex_lock(&odbc_lock); cfg = ast_load(config); if (!cfg) { ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config); goto out; } var = ast_variable_browse(cfg, "global"); if (!var) { /* nothing configured */ goto out; } tmp = ast_variable_retrieve(cfg,"global","dsn"); if (tmp) { dsn = malloc(strlen(tmp) + 1); if (dsn != NULL) { memset(dsn, 0, strlen(tmp) + 1); dsn_alloc = 1; strncpy(dsn, tmp, strlen(tmp)); } else { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } } else { ast_log(LOG_WARNING,"cdr_odbc: dsn not specified. Assuming asteriskdb\n"); dsn = "asteriskdb"; } tmp = ast_variable_retrieve(cfg,"global","username"); if (tmp) { username = malloc(strlen(tmp) + 1); if (username != NULL) { memset(username, 0, strlen(tmp) + 1); username_alloc = 1; strncpy(username, tmp, strlen(tmp)); } else { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } } else { ast_log(LOG_WARNING,"cdr_odbc: username not specified. Assuming root\n"); username = "******"; } tmp = ast_variable_retrieve(cfg,"global","password"); if (tmp) { password = malloc(strlen(tmp) + 1); if (password != NULL) { memset(password, 0, strlen(tmp) + 1); password_alloc = 1; strncpy(password, tmp, strlen(tmp)); } else { ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n"); res = -1; goto out; } } else { ast_log(LOG_WARNING,"cdr_odbc: database password not specified. Assuming blank\n"); password = ""; } tmp = ast_variable_retrieve(cfg,"global","loguniqueid"); if (tmp) { loguniqueid = ast_true(tmp); if (loguniqueid) { ast_log(LOG_NOTICE,"cdr_odbc: Logging uniqueid\n"); } else { ast_log(LOG_ERROR,"cdr_odbc: Not logging uniqueid\n"); } } else { ast_log(LOG_WARNING,"cdr_odbc: Not logging uniqueid\n"); loguniqueid = 0; } ast_destroy(cfg); if (option_verbose > 2) { ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn); ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: username is %s\n",username); ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: password is [secret]\n"); } res = odbc_init(); if (res < 0) { ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn); if (option_verbose > 2) { ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: Unable to connect to datasource: %s\n", dsn); } } res = ast_cdr_register(name, desc, odbc_log); if (res) { ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n"); } out: ast_mutex_unlock(&odbc_lock); return res; }