diff --git a/mediator.h b/mediator.h index d469309..143d941 100644 --- a/mediator.h +++ b/mediator.h @@ -82,6 +82,7 @@ typedef struct { char callid[256]; char timestamp[24]; double unix_timestamp; + char branch_id[3]; uint8_t valid; med_method_t method; char sip_method[32]; diff --git a/medmysql.c b/medmysql.c index 0767c8d..4df629e 100644 --- a/medmysql.c +++ b/medmysql.c @@ -15,16 +15,16 @@ " group by a.callid limit 0,200000" #define MED_FETCH_QUERY "(select distinct sip_code, sip_reason, method, callid, time, time_hires, " \ - "src_leg, dst_leg " \ + "src_leg, dst_leg, branch_id " \ "from acc where method = 'INVITE' and callid = '%s' order by time_hires asc) " \ "union all " \ "(select distinct sip_code, sip_reason, method, callid, time, time_hires, " \ - "src_leg, dst_leg " \ + "src_leg, dst_leg, branch_id " \ "from acc where method = 'BYE' and callid in ('%s', '%s"PBXSUFFIX"') " \ "order by length(callid) asc, time_hires asc) " \ "union all " \ "(select distinct sip_code, sip_reason, method, callid, time, time_hires, " \ - "src_leg, dst_leg " \ + "src_leg, dst_leg, branch_id " \ "from acc where method = 'BYE' and callid in ('%s', '%s"XFERSUFFIX"') " \ "order by length(callid) asc, time_hires asc)" @@ -85,9 +85,9 @@ static int medmysql_handler_transaction(medmysql_handler *h); static const medmysql_batch_definition medmysql_trash_def = { .sql_init_string = "insert into acc_trash (method, from_tag, to_tag, callid, sip_code, " \ "sip_reason, time, time_hires, src_leg, dst_leg, dst_user, dst_ouser, " \ - "dst_domain, src_user, src_domain) select method, from_tag, to_tag, " \ + "dst_domain, src_user, src_domain, branch_id) select method, from_tag, to_tag, " \ "callid, sip_code, sip_reason, time, time_hires, src_leg, dst_leg, " \ - "dst_user, dst_ouser, dst_domain, src_user, src_domain from acc " \ + "dst_user, dst_ouser, dst_domain, src_user, src_domain, branch_id from acc " \ "where callid in (", .sql_finish_string = ")", .single_flush_func = medmysql_flush_medlist, @@ -96,9 +96,9 @@ static const medmysql_batch_definition medmysql_trash_def = { static const medmysql_batch_definition medmysql_backup_def = { .sql_init_string = "insert into acc_backup (method, from_tag, to_tag, callid, sip_code, " \ "sip_reason, time, time_hires, src_leg, dst_leg, dst_user, dst_ouser, " \ - "dst_domain, src_user, src_domain) select method, from_tag, to_tag, " \ + "dst_domain, src_user, src_domain, branch_id) select method, from_tag, to_tag, " \ "callid, sip_code, sip_reason, time, time_hires, src_leg, dst_leg, " \ - "dst_user, dst_ouser, dst_domain, src_user, src_domain from acc " \ + "dst_user, dst_ouser, dst_domain, src_user, src_domain, branch_id from acc " \ "where callid in (", .sql_finish_string = ")", .single_flush_func = medmysql_flush_medlist, @@ -429,7 +429,7 @@ int medmysql_insert_records(med_entry_t *records, uint64_t count, const char *ta return -1; } BUFPRINT("INSERT INTO kamailio.acc_%s " \ - "(sip_code,sip_reason,method,callid,time,time_hires,src_leg,dst_leg) VALUES ", + "(sip_code,sip_reason,method,callid,time,time_hires,src_leg,dst_leg,branch_id) VALUES ", table); for (uint64_t i = 0; i < count; ++i) { @@ -439,23 +439,25 @@ int medmysql_insert_records(med_entry_t *records, uint64_t count, const char *ta if (!e->redis) continue; - BUFPRINT("('"); - BUFESCAPE(e->sip_code); - BUFPRINT("','"); - BUFESCAPE(e->sip_reason); - BUFPRINT("','"); - BUFESCAPE(e->sip_method); - BUFPRINT("','"); - BUFESCAPE(e->callid); - BUFPRINT("','"); - BUFESCAPE(e->timestamp); - BUFPRINT("','%f','", e->unix_timestamp); - BUFESCAPE(e->src_leg); - BUFPRINT("','"); - BUFESCAPE(e->dst_leg); - BUFPRINT("'),"); - - entries++; + BUFPRINT("('"); + BUFESCAPE(e->sip_code); + BUFPRINT("','"); + BUFESCAPE(e->sip_reason); + BUFPRINT("','"); + BUFESCAPE(e->sip_method); + BUFPRINT("','"); + BUFESCAPE(e->callid); + BUFPRINT("','"); + BUFESCAPE(e->timestamp); + BUFPRINT("','%f','", e->unix_timestamp); + BUFESCAPE(e->src_leg); + BUFPRINT("','"); + BUFESCAPE(e->dst_leg); + BUFPRINT("','"); + BUFESCAPE(e->branch_id); + BUFPRINT("'),"); + + entries++; } if (!entries) goto out; @@ -606,6 +608,7 @@ int medmysql_fetch_records(med_callid_t *callid, e->unix_timestamp = atof(row[5]); g_strlcpy(e->src_leg, row[6], sizeof(e->src_leg)); g_strlcpy(e->dst_leg, row[7], sizeof(e->dst_leg)); + g_strlcpy(e->branch_id, row[8], sizeof(e->branch_id)); e->valid = 1; if (check_shutdown()) diff --git a/medredis.c b/medredis.c index 234618c..b068228 100644 --- a/medredis.c +++ b/medredis.c @@ -315,7 +315,7 @@ static med_entry_t *medredis_reply_to_entry(redisReply *reply, const char* cid, med_entry_t *entry; uint8_t all_null = 1; - if (reply->elements != 8) { + if (reply->elements != 9) { L_ERROR("Invalid number of redis reply elements for acc record with cid '%s' and key '%s', expected 8, got %lu, trashing record\n", cid, key, reply->elements); medredis_remove_mappings(cid, key); @@ -367,6 +367,7 @@ static med_entry_t *medredis_reply_to_entry(redisReply *reply, const char* cid, } medredis_check_reply_string(entry, reply, entry->src_leg, "src_leg", sizeof(entry->src_leg), 6, cid, key); medredis_check_reply_string(entry, reply, entry->dst_leg, "dst_leg", sizeof(entry->dst_leg), 7, cid, key); + medredis_check_reply_string(entry, reply, entry->branch_id, "branch_id", sizeof(entry->branch_id), 8, cid, key); L_DEBUG("Converted record with cid '%s' and method '%s'\n", entry->callid, entry->sip_method); @@ -531,12 +532,16 @@ med_callid_t *medredis_fetch_callids(uint64_t *count) { L_DEBUG("Got entry '%s'\n", entry->str); - // strip leading "acc:entry::" and trailing ":" + // strip leading "acc:entry::" and trailing "::" cid = strdup(entry->str + strlen("acc:entry::")); tmp = strrchr(cid, ':'); if (tmp) { *tmp = '\0'; } + tmp = strrchr(cid, ':'); + if (tmp) { + *tmp = '\0'; + } // strip (potentially chained) suffices if ((tmp = strstr(entry->str, PBXSUFFIX)) || @@ -584,8 +589,8 @@ static void medredis_append_key(gpointer data, gpointer user_data) { L_DEBUG("Appending key '%s' to keys list\n", key); - size_t entry_argc = 10; - char *entry_argv[10]; + size_t entry_argc = 11; + char *entry_argv[11]; entry_argv[0] = "HMGET"; entry_argv[1] = key; @@ -597,6 +602,7 @@ static void medredis_append_key(gpointer data, gpointer user_data) { entry_argv[7] = "time_hires"; entry_argv[8] = "src_leg"; entry_argv[9] = "dst_leg"; + entry_argv[10] = "branch_id"; if (medredis_append_command_argv(entry_argc, entry_argv, 1) != 0) { @@ -796,7 +802,7 @@ static int medredis_cleanup_entries(med_entry_t *records, uint64_t count, const if (!e->redis) continue; - snprintf(buffer, sizeof(buffer), "acc:entry::%s:%f", e->callid, e->unix_timestamp); + snprintf(buffer, sizeof(buffer), "acc:entry::%s:%f:%s", e->callid, e->unix_timestamp, e->branch_id); L_DEBUG("Cleaning up redis entry for %s\n", buffer); if (medredis_remove_mappings(e->callid, buffer) != 0) {