From b3d6073447c40692895d2ae6dc0605567adc25ad Mon Sep 17 00:00:00 2001 From: Dylan Mikus Date: Mon, 21 Dec 2015 21:37:42 +0000 Subject: [PATCH] Prefix recording metadata and pcap files with call id We want to be able to associate call files with a call without the presence of identifying metadata within the metadata file. To accomplish this, we prepend the call-id to the start of the pcap recording files and the call metadata files. Even though call-id is supposed to be unique, because of paranoia we keep some of the random affix hex string, but we reduced it down to an 8-byte random value. Also, some minor argument ordering and name refactoring for random string generation functions. --- daemon/call.c | 2 +- daemon/call_interfaces.c | 2 +- daemon/recording.c | 22 ++++++++++++++-------- daemon/recording.h | 15 ++++++++++++--- daemon/str.c | 6 +++--- daemon/str.h | 2 +- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index 72eb7e5a4..341cc15a7 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -1524,7 +1524,7 @@ int monologue_offer_answer(struct call_monologue *other_ml, GQueue *streams, ml_media = other_ml_media = NULL; if (call->recording != NULL && call->recording->recording_pdumper == NULL) { - str *pcap_path = recording_setup_file(call->recording); + str *pcap_path = recording_setup_file(call->recording, call->callid); if (pcap_path != NULL && call->recording->recording_pdumper != NULL && call->recording->meta_fp) { // Write the location of the PCAP file to the metadata file diff --git a/daemon/call_interfaces.c b/daemon/call_interfaces.c index 98e7e9b1c..35402ffef 100644 --- a/daemon/call_interfaces.c +++ b/daemon/call_interfaces.c @@ -694,7 +694,7 @@ static const char *call_offer_answer_ng(bencode_item_t *input, struct callmaster call->recording = g_slice_alloc0(sizeof(struct recording)); call->recording->recording_pd = NULL; call->recording->recording_pdumper = NULL; - meta_setup_file(call->recording); + meta_setup_file(call->recording, call->callid); } bencode_dictionary_get_str(input, "metadata", &metadata); if (metadata.len > 0) { diff --git a/daemon/recording.c b/daemon/recording.c index 00bc06767..954975b60 100644 --- a/daemon/recording.c +++ b/daemon/recording.c @@ -97,16 +97,20 @@ int maybe_create_spool_dir(char *spoolpath) { * Create a call metadata file in a temporary location. * Attaches the filepath and the file pointer to the call struct. */ -str *meta_setup_file(struct recording *recording) { +str *meta_setup_file(struct recording *recording, str callid) { if (spooldir == NULL) { // No spool directory was created, so we cannot have metadata files. return NULL; } else { - int rand_bytes = 16; + int rand_bytes = 8; str *meta_filepath = malloc(sizeof(str)); + int mid_len = 20 + callid.len + 1 + 1; + // Length for spool directory path + "/tmp/rtpengine-meta-${CALLID}-" + char suffix_chars[mid_len]; + snprintf(suffix_chars, mid_len, "/tmp/rtpengine-meta-%s-", callid.s); // Initially file extension is ".tmp". When call is over, it changes to ".txt". - char *path_chars = rand_affixed_str(rand_bytes, "/tmp/rtpengine-meta-", ".tmp"); + char *path_chars = rand_affixed_str(suffix_chars, rand_bytes, ".tmp"); meta_filepath = str_init(meta_filepath, path_chars); recording->meta_filepath = meta_filepath; FILE *mfp = fopen(meta_filepath->s, "w"); @@ -193,17 +197,19 @@ int meta_finish_file(struct call *call) { /** * Generate a random PCAP filepath to write recorded RTP stream. + * Returns path to created file. */ -str *recording_setup_file(struct recording *recording) { +str *recording_setup_file(struct recording *recording, str callid) { str *recording_path = NULL; if (spooldir != NULL && recording != NULL && recording->recording_pd == NULL && recording->recording_pdumper == NULL) { - int rand_bytes = 16; - int rec_path_len = strlen(spooldir) + 8; // spool directory path + "/pcaps/" + int rand_bytes = 8; + // Length for spool directory path + "/pcaps/${CALLID}-" + int rec_path_len = strlen(spooldir) + 7 + callid.len + 1 + 1; char rec_path[rec_path_len]; - snprintf(rec_path, rec_path_len, "%s/pcaps/", spooldir); - char *path_chars = rand_affixed_str(rand_bytes, rec_path, ".pcap"); + snprintf(rec_path, rec_path_len, "%s/pcaps/%s-", spooldir, callid.s); + char *path_chars = rand_affixed_str(rec_path, rand_bytes, ".pcap"); recording_path = malloc(sizeof(str)); recording_path = str_init(recording_path, path_chars); diff --git a/daemon/recording.h b/daemon/recording.h index b47d26c8b..bf964a81b 100644 --- a/daemon/recording.h +++ b/daemon/recording.h @@ -44,20 +44,29 @@ void recording_fs_init(char *spooldir); * * generic metadata * + * Temporary files go in /tmp/. They will end up in + * ${RECORDING_DIR}/metadata/. They are named like: + * ${CALL_ID}-${RAND-HEX}.pcap + * */ -str *meta_setup_file(struct recording *recording); +str *meta_setup_file(struct recording *recording, str callid); /** - * Writes metadata to metafile, closes file, and renames it to finished location. + * Writes metadata to metafile, closes file, and moves it to finished location. * Returns non-zero for failure. + * + * Metadata files are moved to ${RECORDING_DIR}/metadata/ */ int meta_finish_file(struct call *call); /** * Generate a random PCAP filepath to write recorded RTP stream. * Returns path to created file. + * + * Files go in ${RECORDING_DIR}/pcaps, and are named like: + * ${CALL_ID}-${RAND-HEX}.pcap */ -str *recording_setup_file(struct recording *recording); +str *recording_setup_file(struct recording *recording, str callid); /** * Flushes PCAP file, closes the dumper and descriptors, and frees object memory. diff --git a/daemon/str.c b/daemon/str.c index 150812a96..36047e44f 100644 --- a/daemon/str.c +++ b/daemon/str.c @@ -41,13 +41,13 @@ void str_slice_free(void *p) { */ char *rand_affixed_str(int num_bytes, char *prefix, char *suffix) { int rand_len = num_bytes*2 + 1; - char rand_prefix[rand_len]; + char rand_affix[rand_len]; int prefix_len = strlen(prefix); int suffix_len = strlen(suffix); char *full_path = calloc(rand_len + prefix_len + suffix_len, sizeof(char)); - rand_hex_str(rand_prefix, num_bytes); - snprintf(full_path, rand_len+prefix_len, "%s%s", prefix, rand_prefix); + rand_hex_str(rand_affix, num_bytes); + snprintf(full_path, rand_len+prefix_len, "%s%s", prefix, rand_affix); snprintf(full_path + rand_len+prefix_len-1, suffix_len+1, "%s", suffix); return full_path; } diff --git a/daemon/str.h b/daemon/str.h index de805663f..6ccd86396 100644 --- a/daemon/str.h +++ b/daemon/str.h @@ -294,7 +294,7 @@ INLINE int str_token(str *new_token, str *ori_and_remainder, int sep) { /* Generates a random string sandwiched between affixes. */ -char *rand_affixed_str(int num_bytes, char *prefix, char *suffix); +char *rand_affixed_str(char *prefix, int num_bytes, char *suffix); /* Generates a hex string representing n random bytes. len(rand_str) = 2*num_bytes + 1 */ char *rand_hex_str(char *rand_str, int num_bytes);