From c8af44a21a95440941a3c57e8f31d2aac170fada Mon Sep 17 00:00:00 2001 From: Dylan Mikus Date: Mon, 30 Nov 2015 18:03:35 +0000 Subject: [PATCH] Made random filename with affixes generic. Creating a random filename with a prefix and a suffix is now done through a generic function. We use this to create pcap recording files in the /tmp/ directory, and will soon use it to create metadata files. --- daemon/call.c | 73 +++++++++++++++++++++++++++++++++++++++++++-------- daemon/call.h | 1 + 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/daemon/call.c b/daemon/call.c index c5453b093..d096f8a89 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -132,6 +132,12 @@ const char * get_tag_type_text(enum tag_type t) { static void __monologue_destroy(struct call_monologue *monologue); static int monologue_destroy(struct call_monologue *ml); +/* Generate a random PCAP filepath to write recorded RTP stream. */ +void setup_recording_files(struct call *call, struct call_monologue *monologue); +/* Generates a random string sandwiched between affixes. */ +char *rand_affixed_str(int num_bytes, char *prefix, 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); /* called with call->master_lock held in R */ static int call_timer_delete_monologues(struct call *c) { @@ -2857,21 +2863,22 @@ out: return NULL; } + +/** + * Generate a random PCAP filepath to write recorded RTP stream. + */ void setup_recording_files(struct call *call, struct call_monologue *monologue) { if (call->record_call && monologue->recording_pd == NULL && monologue->recording_pdumper == NULL) { - char rec_path_prefix[16]; - char recording_path[21]; - /* - * - * create a file descriptor per monologue which can be used for writing rtp to disk - * aka call recording. - */ + int rand_bytes = 16; + str *recording_path = malloc(sizeof(str)); + char *path_chars = rand_affixed_str(rand_bytes, "/tmp/", ".pcap"); + + recording_path = str_init(recording_path, path_chars); + monologue->recording_path = recording_path; - snprintf(rec_path_prefix, 15, "/tmp/%d", rand()); - snprintf(recording_path, 20, "%s.pcap", rec_path_prefix); - call->recording_pcaps = g_slist_prepend(call->recording_pcaps, g_strdup(recording_path)); - ilog(LOG_INFO, "XXXDylan: Creating new pcap dumper for recording at path %s", recording_path); + call->recording_pcaps = g_slist_prepend(call->recording_pcaps, g_strdup(path_chars)); + /* monologue->recording_file */ monologue->recording_pd = pcap_open_dead(DLT_RAW, 65535); monologue->recording_pdumper = pcap_dump_open(monologue->recording_pd, recording_path); } else { @@ -2879,3 +2886,47 @@ void setup_recording_files(struct call *call, struct call_monologue *monologue) monologue->recording_pdumper = NULL; } } + +/** + * Generates a random string sandwiched between affixes. + * Will create the char string for you. Don't forget to clean up! + */ +char *rand_affixed_str(int num_bytes, char *prefix, char *suffix) { + int rand_len = num_bytes*2 + 1; + char rand_prefix[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); + snprintf(full_path + rand_len+prefix_len-1, suffix_len+1, "%s", suffix); + return full_path; +} + +/** + * Generates a random hexadecimal string representing n random bytes. + * rand_str length must be 2*num_bytes + 1. + */ +char *rand_hex_str(char *rand_str, int num_bytes) { + char rand_tmp[3]; + u_int8_t rand_byte; + int i, n; + // We might convert an int to a hex string shorter than 2 digits. + // This causes those strings to have leading '0' characters. + for (i=0; i