From 6d50b0c7b528244ee9fcc063f6334dbae820547d Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 7 Dec 2006 22:56:17 +0000 Subject: [PATCH] improve gateway stuff and minor tweak to event serialize git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3570 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../mod_dialplan_xml/mod_dialplan_xml.c | 100 ++++++++++++++++-- src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c | 32 +++--- src/switch_event.c | 23 ++-- 3 files changed, 123 insertions(+), 32 deletions(-) diff --git a/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c b/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c index 5897b66861..c5d65455e3 100644 --- a/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c +++ b/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c @@ -177,7 +177,12 @@ static switch_caller_extension_t *dialplan_hunt(switch_core_session_t *session) switch_channel_t *channel; switch_xml_t cfg, xml, xcontext, xexten; char *context = NULL; - char params[1024]; + switch_stream_handle_t stream = {0}; + switch_size_t encode_len = 1024, new_len = 0; + char *encode_buf = NULL; + char *prof[11] = {0}, *prof_names[11] = {0}, *e = NULL; + switch_hash_index_t *hi; + uint32_t x = 0; channel = switch_core_session_get_channel(session); if ((caller_profile = switch_channel_get_caller_profile(channel))) { @@ -189,22 +194,95 @@ static switch_caller_extension_t *dialplan_hunt(switch_core_session_t *session) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Processing %s->%s!\n", caller_profile->caller_id_name, caller_profile->destination_number); - - snprintf(params, sizeof(params), "context=%s&dest=%s&cid_name=%s&cid_num=%s&netaddr=%s&ani=%s&aniii=%s&rdnis=%s&source=%s&chan_name=%s&uuid=%s", - caller_profile->context, caller_profile->destination_number, - caller_profile->caller_id_name, caller_profile->caller_id_number, - caller_profile->network_addr?caller_profile->network_addr:"", - caller_profile->ani?caller_profile->ani:"", - caller_profile->aniii?caller_profile->aniii:"", - caller_profile->rdnis?caller_profile->rdnis:"", - caller_profile->source, caller_profile->chan_name, caller_profile->uuid); - if (switch_xml_locate("dialplan", NULL, NULL, NULL, &xml, &cfg, params) != SWITCH_STATUS_SUCCESS) { + SWITCH_STANDARD_STREAM(stream); + + if (!(encode_buf = malloc(encode_len))) { + return NULL; + } + + prof[0] = caller_profile->context; + prof[1] = caller_profile->destination_number; + prof[2] = caller_profile->caller_id_name; + prof[3] = caller_profile->caller_id_number; + prof[4] = caller_profile->network_addr; + prof[5] = caller_profile->ani; + prof[6] = caller_profile->aniii; + prof[7] = caller_profile->rdnis; + prof[8] = caller_profile->source; + prof[9] = caller_profile->chan_name; + prof[10] = caller_profile->uuid; + + prof_names[0] = "context"; + prof_names[1] = "destination_number"; + prof_names[2] = "caller_id_name"; + prof_names[3] = "caller_id_number"; + prof_names[4] = "network_addr"; + prof_names[5] = "ani"; + prof_names[6] = "aniii"; + prof_names[7] = "rdnis"; + prof_names[8] = "source"; + prof_names[9] = "chan_name"; + prof_names[10] = "uuid"; + + for (x = 0; prof[x]; x++) { + new_len = (strlen(prof[x]) * 3) + 1; + if (encode_len < new_len) { + char *tmp; + + encode_len = new_len; + + if (!(tmp = realloc(encode_buf, encode_len))) { + switch_safe_free(encode_buf); + return NULL; + } + + encode_buf = tmp; + } + switch_url_encode(prof[x], encode_buf, encode_len - 1); + stream.write_function(&stream, "%s=%s&", prof_names[x], encode_buf); + } + + + for (hi = switch_channel_variable_first(channel, switch_core_session_get_pool(session)); hi; hi = switch_hash_next(hi)) { + void *val; + const void *var; + switch_hash_this(hi, &var, NULL, &val); + + new_len = (strlen((char *) var) * 3) + 1; + if (encode_len < new_len) { + char *tmp; + + encode_len = new_len; + + if (!(tmp = realloc(encode_buf, encode_len))) { + switch_safe_free(encode_buf); + return NULL; + } + + encode_buf = tmp; + } + + switch_url_encode((char *) val, encode_buf, encode_len - 1); + stream.write_function(&stream, "%s=%s&", (char *) var, encode_buf); + + } + + e = (char *)stream.data + (strlen((char *)stream.data) - 1); + + if (e && *e == '&') { + *e = '\0'; + } + + if (switch_xml_locate("dialplan", NULL, NULL, NULL, &xml, &cfg, stream.data) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of dialplan failed\n"); switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); return NULL; } + switch_safe_free(stream.data); + switch_safe_free(encode_buf); + if (!(xcontext = switch_xml_find_child(cfg, "context", "name", context))) { if (!(xcontext = switch_xml_find_child(cfg, "context", "name", "global"))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "context %s not found\n", context); diff --git a/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c b/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c index 95873e3cc2..2e7310ae09 100644 --- a/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c +++ b/src/mod/xml_int/mod_xml_rpc/mod_xml_rpc.c @@ -82,30 +82,36 @@ static switch_xml_t xml_url_fetch(char *section, char *key_value, char *params) { - char url[1024] = "", filename[1024] = ""; + char filename[1024] = ""; CURL *curl_handle = NULL; struct config_data config_data; switch_xml_t xml = NULL; - - snprintf(url, sizeof(url), "%s?section=%s&tag_name=%s&key_name=%s&&key_value=%s%s%s\n", - globals.url, - section, - tag_name ? tag_name : "", - key_name ? key_name : "", - key_value ? key_value : "", - params ? "&" : "", params ? params : ""); + char *data = NULL; - srand((unsigned int)(time(NULL) + strlen(url))); + if (!(data = switch_mprintf("section=%s&tag_name=%s&key_name=%s&key_value=%s%s%s\n", + section, + tag_name ? tag_name : "", + key_name ? key_name : "", + key_value ? key_value : "", + params ? "&" : "", params ? params : ""))) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n"); + return NULL; + } + + srand((unsigned int)(time(NULL) + strlen(globals.url))); snprintf(filename, sizeof(filename), "%s%04x.tmp", SWITCH_GLOBAL_dirs.temp_dir, (rand() & 0xffff)); curl_handle = curl_easy_init(); - if (!strncasecmp(url, "https", 5)) { + if (!strncasecmp(globals.url, "https", 5)) { curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0); } config_data.name = filename; if ((config_data.fd = open(filename, O_CREAT | O_RDWR | O_TRUNC)) > -1) { - curl_easy_setopt(curl_handle, CURLOPT_URL, url); + curl_easy_setopt(curl_handle, CURLOPT_POST, 1); + curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data); + curl_easy_setopt(curl_handle, CURLOPT_URL, globals.url); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&config_data); curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-xml/1.0"); @@ -116,6 +122,8 @@ static switch_xml_t xml_url_fetch(char *section, switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error!\n"); } + switch_safe_free(data); + if (!(xml = switch_xml_parse_file(filename))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result!\n"); } diff --git a/src/switch_event.c b/src/switch_event.c index e6e53aebe1..c30c5c5ff5 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -600,7 +600,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch { switch_size_t len = 0; switch_event_header_t *hp; - switch_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1024; + switch_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1024, new_len = 0; char *buf; char *encode_buf = NULL; /* used for url encoding of variables to make sure unsafe things stay out of the serialzed copy */ @@ -626,19 +626,24 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch * the memory, allocate and only reallocate if we need more. This avoids an alloc, free CPU * destroying loop. */ - if(encode_len < ((strlen(hp->value) * 3) + 1)) { + + new_len = (strlen(hp->value) * 3) + 1; + + if(encode_len < new_len) { char* tmp; //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len); /* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */ - if(!(tmp = realloc(encode_buf, ((strlen(hp->value) * 3) + 1)))) { - /* oh boy, ram's gone, give back what little we grabbed and bail */ - switch_safe_free(buf); - return SWITCH_STATUS_MEMERR; - } /* keep track of the size of our allocation */ - encode_len = (strlen(hp->value) * 3) + 1; - + encode_len = new_len; + + if(!(tmp = realloc(encode_buf, encode_len))) { + /* oh boy, ram's gone, give back what little we grabbed and bail */ + switch_safe_free(buf); + switch_safe_free(encode_buf); + return SWITCH_STATUS_MEMERR; + } + encode_buf = tmp; }