Index: cdr_addon_mysql.c =================================================================== --- cdr_addon_mysql.c (revision 320) +++ cdr_addon_mysql.c (working copy) @@ -47,12 +47,18 @@ #define AST_MODULE "cdr_addon_mysql" #define DATE_FORMAT "%Y-%m-%d %T" - static char *desc = "MySQL CDR Backend"; static char *name = "mysql"; static char *config = "cdr_mysql.conf"; static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL; +static char *calldate_field = NULL, *clid_field = NULL, *src_field = NULL, *dst_field = NULL, *dcontext_field = NULL, *channel_field = NULL; +static char *dstchannel_field = NULL, *lastapp_field = NULL, *lastdata_field = NULL, *duration_field = NULL, *billsec_field =NULL; +static char *disposition_field = NULL, *amaflags_field = NULL, *accountcode_field = NULL, *userfield_field = NULL, *uniqueid_field = NULL; static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0; +static int calldate_field_alloc = 0, clid_field_alloc = 0, src_field_alloc = 0, dst_field_alloc = 0, dcontext_field_alloc = 0; +static int channel_field_alloc = 0, dstchannel_field_alloc = 0, lastapp_field_alloc = 0, lastdata_field_alloc = 0, duration_field_alloc = 0; +static int billsec_field_alloc = 0, disposition_field_alloc = 0, amaflags_field_alloc = 0, userfield_field_alloc = 0, uniqueid_field_alloc = 0; +static int accountcode_field_alloc = 0; static int dbport = 0; static int connected = 0; static time_t connect_time = 0; @@ -211,15 +217,15 @@ if (userfield && userfielddata) { #ifdef MYSQL_LOGUNIQUEID - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid, userfielddata); + sprintf(sqlcmd, "INSERT INTO %s (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, calldate_field, clid_field, src_field, dst_field, dcontext_field, channel_field, dstchannel_field, lastapp_field, lastdata_field, duration_field, billsec_field, disposition_field, amaflags_field, accountcode_field, uniqueid_field, userfield_field, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid, userfielddata); #else - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata); + sprintf(sqlcmd, "INSERT INTO %s (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, calldate_field, clid_field, src_field, dst_field, dcontext_field, channel_field, dstchannel_field, lastapp_field, lastdata_field, duration_field, billsec_field, disposition_field, amaflags_field, accountcode_field, userfield_field, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata); #endif } else { #ifdef MYSQL_LOGUNIQUEID - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid); + sprintf(sqlcmd, "INSERT INTO %s (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, calldate_field, clid_field, src_field, dst_field, dcontext_field, channel_field, dstchannel_field, lastapp_field, lastdata_field, duration_field, billsec_field, disposition_field, amaflags_field, accountcode_field, uniqueid_field, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, uniqueid); #else - sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode); + sprintf(sqlcmd, "INSERT INTO %s (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, calldate_field, clid_field, src_field, dst_field, dcontext_field, channel_field, dstchannel_field, lastapp_field, lastdata_field, duration_field, billsec_field, disposition_field, amaflags_field, accountcode_field, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode); #endif } @@ -277,6 +283,93 @@ password = NULL; password_alloc = 0; } + + if (calldate_field && calldate_field_alloc) { + free(calldate_field); + calldate_field = NULL; + calldate_field_alloc = 0; + } + if (clid_field && clid_field_alloc) { + free(clid_field); + clid_field = NULL; + clid_field_alloc = 0; + } + + if (src_field && src_field_alloc) { + free(src_field); + src_field = NULL; + src_field_alloc = 0; + } + if (dst_field && dst_field_alloc) { + free(dst_field); + dst_field = NULL; + dst_field_alloc = 0; + } + if (dcontext_field && dcontext_field_alloc) { + free(dcontext_field); + dcontext_field = NULL; + dcontext_field_alloc = 0; + } + if (channel_field && channel_field_alloc) { + free(channel_field); + channel_field = NULL; + channel_field_alloc = 0; + } + if (dstchannel_field && dstchannel_field_alloc) { + free(dstchannel_field); + dstchannel_field = NULL; + dstchannel_field_alloc = 0; + } + + if (lastapp_field && lastapp_field_alloc) { + free(lastapp_field); + lastapp_field = NULL; + lastapp_field_alloc = 0; + } + if (lastdata_field && lastdata_field_alloc) { + free(lastdata_field); + lastdata_field = NULL; + lastdata_field_alloc = 0; + } + if (duration_field && duration_field_alloc) { + free(duration_field); + duration_field = NULL; + duration_field_alloc = 0; + } + if (billsec_field && billsec_field_alloc) { + free(billsec_field); + billsec_field = NULL; + billsec_field_alloc = 0; + } + if (disposition_field && disposition_field_alloc) { + free(disposition_field); + disposition_field = NULL; + disposition_field_alloc = 0; + } + + if (amaflags_field && amaflags_field_alloc) { + free(amaflags_field); + amaflags_field = NULL; + amaflags_field_alloc = 0; + } + if (accountcode_field && accountcode_field_alloc) { + free(accountcode_field); + accountcode_field = NULL; + accountcode_field_alloc = 0; + } + if (userfield_field && userfield_field_alloc) { + free(userfield_field); + userfield_field = NULL; + userfield_field_alloc = 0; + } +#ifdef MYSQL_LOGUNIQUEID + if (uniqueid_field && uniqueid_field_alloc) { + free(uniqueid_field); + uniqueid_field = NULL; + uniqueid_field_alloc = 0; + } +#endif + dbport = 0; ast_cdr_unregister(name); return 0; @@ -415,6 +508,250 @@ } } + + tmp = ast_variable_retrieve(cfg, "global", "calldate_field"); + if (tmp) { + calldate_field = malloc(strlen(tmp) + 1); + if (calldate_field != NULL) { + calldate_field_alloc = 1; + strcpy(calldate_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL Calldate field not specified. Assuming default\n"); + calldate_field = "calldate"; + } + + tmp = ast_variable_retrieve(cfg, "global", "clid_field"); + if (tmp) { + clid_field = malloc(strlen(tmp) + 1); + if (clid_field != NULL) { + clid_field_alloc = 1; + strcpy(clid_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field clid not specified. Assuming default\n"); + clid_field = "clid"; + } + + tmp = ast_variable_retrieve(cfg, "global", "src_field"); + if (tmp) { + src_field = malloc(strlen(tmp) + 1); + if (src_field != NULL) { + src_field_alloc = 1; + strcpy(src_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field src not specified. Assuming default\n"); + src_field = "src"; + } + + + tmp = ast_variable_retrieve(cfg, "global", "dst_field"); + if (tmp) { + dst_field = malloc(strlen(tmp) + 1); + if (dst_field != NULL) { + dst_field_alloc = 1; + strcpy(dst_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field dst not specified. Assuming default\n"); + dst_field = "dst"; + } + + tmp = ast_variable_retrieve(cfg, "global", "dcontext"); + if (tmp) { + dcontext_field = malloc(strlen(tmp) + 1); + if (dcontext_field != NULL) { + dcontext_field_alloc = 1; + strcpy(dcontext_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field dcontext not specified. Assuming default\n"); + dcontext_field = "dcontext"; + } + + tmp = ast_variable_retrieve(cfg, "global", "channel_field"); + if (tmp) { + channel_field = malloc(strlen(tmp) + 1); + if (channel_field != NULL) { + channel_field_alloc = 1; + strcpy(channel_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field channel not specified. Assuming default\n"); + channel_field = "channel"; + } + + tmp = ast_variable_retrieve(cfg, "global", "dstchannel_field"); + if (tmp) { + dstchannel_field = malloc(strlen(tmp) + 1); + if (dstchannel_field != NULL) { + dstchannel_field_alloc = 1; + strcpy(dstchannel_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field dstchannel not specified. Assuming default\n"); + dstchannel_field = "dstchannel"; + } + + tmp = ast_variable_retrieve(cfg, "global", "lastapp_field"); + if (tmp) { + lastapp_field = malloc(strlen(tmp) + 1); + if (lastapp_field != NULL) { + lastapp_field_alloc = 1; + strcpy(lastapp_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field lastapp not specified. Assuming default\n"); + lastapp_field = "lastapp"; + } + + tmp = ast_variable_retrieve(cfg, "global", "lastdata_field"); + if (tmp) { + lastdata_field = malloc(strlen(tmp) + 1); + if (lastdata_field != NULL) { + lastdata_field_alloc = 1; + strcpy(lastdata_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field lastdata not specified. Assuming default\n"); + lastdata_field = "lastdata"; + } + + tmp = ast_variable_retrieve(cfg, "global", "duration_field"); + if (tmp) { + duration_field = malloc(strlen(tmp) + 1); + if (duration_field != NULL) { + duration_field_alloc = 1; + strcpy(duration_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field duration not specified. Assuming default\n"); + duration_field = "duration"; + } + + tmp = ast_variable_retrieve(cfg, "global", "billsec_field"); + if (tmp) { + billsec_field = malloc(strlen(tmp) + 1); + if (billsec_field != NULL) { + billsec_field_alloc = 1; + strcpy(billsec_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field billsec not specified. Assuming default\n"); + billsec_field = "billsec"; + } + + tmp = ast_variable_retrieve(cfg, "global", "disposition_field"); + if (tmp) { + disposition_field = malloc(strlen(tmp) + 1); + if (disposition_field != NULL) { + disposition_field_alloc = 1; + strcpy(disposition_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field disposition not specified. Assuming default\n"); + disposition_field = "disposition"; + } + + tmp = ast_variable_retrieve(cfg, "global", "amaflags_field"); + if (tmp) { + amaflags_field = malloc(strlen(tmp) + 1); + if (amaflags_field != NULL) { + amaflags_field_alloc = 1; + strcpy(amaflags_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field amaflags not specified. Assuming default\n"); + amaflags_field = "amaflags"; + } + + tmp = ast_variable_retrieve(cfg, "global", "accountcode_field"); + if (tmp) { + accountcode_field = malloc(strlen(tmp) + 1); + if (accountcode_field != NULL) { + accountcode_field_alloc = 1; + strcpy(accountcode_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field accountcode not specified. Assuming default\n"); + accountcode_field = "accountcode"; + } + + tmp = ast_variable_retrieve(cfg, "global", "userfield_field"); + if (tmp) { + userfield_field = malloc(strlen(tmp) + 1); + if (userfield_field != NULL) { + userfield_field_alloc = 1; + strcpy(userfield_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field userfield not specified. Assuming default\n"); + userfield_field = "userfield"; + } + +#ifdef MYSQL_LOGUNIQUEID + tmp = ast_variable_retrieve(cfg, "global", "uniqueid_field"); + if (tmp) { + uniqueid_field = malloc(strlen(tmp) + 1); + if (uniqueid_field != NULL) { + uniqueid_field_alloc = 1; + strcpy(uniqueid_field, tmp); + } else { + ast_log(LOG_ERROR, "Out of memory error.\n"); + return -1; + } + } else { + ast_log(LOG_WARNING, "MySQL database field uniqueid not specified. Assuming default\n"); + uniqueid_field = "uniqueid"; + } +#endif + ast_config_destroy(cfg); ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname); @@ -426,6 +763,25 @@ ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname); ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password); + ast_log(LOG_DEBUG, "cdr_mysql: got calldate field of %s\n", calldate_field); + ast_log(LOG_DEBUG, "cdr_mysql: got clid field of %s\n", clid_field); + ast_log(LOG_DEBUG, "cdr_mysql: got src field of %s\n", src_field); + ast_log(LOG_DEBUG, "cdr_mysql: got dst field of %s\n", dst_field); + ast_log(LOG_DEBUG, "cdr_mysql: got dcontext field of %s\n", dcontext_field); + ast_log(LOG_DEBUG, "cdr_mysql: got channel field of %s\n", channel_field); + ast_log(LOG_DEBUG, "cdr_mysql: got dstchannel field of %s\n", dstchannel_field); + ast_log(LOG_DEBUG, "cdr_mysql: got lastapp field of %s\n", lastapp_field); + ast_log(LOG_DEBUG, "cdr_mysql: got lastdata field of %s\n", lastdata_field); + ast_log(LOG_DEBUG, "cdr_mysql: got duration field of %s\n", duration_field); + ast_log(LOG_DEBUG, "cdr_mysql: got billsec field of %s\n", billsec_field); + ast_log(LOG_DEBUG, "cdr_mysql: got amaflags field of %s\n", amaflags_field); + ast_log(LOG_DEBUG, "cdr_mysql: got accountcode field of %s\n", accountcode_field); + ast_log(LOG_DEBUG, "cdr_mysql: got userfield field of %s\n", userfield_field); + +#ifdef MYSQL_LOGUNIQUEID + ast_log(LOG_DEBUG, "cdr_mysql: got uniqueid field of %s\n", uniqueid_field); +#endif + mysql_init(&mysql); if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout)!=0) {