From 93c05d9c5f03207f590d9b22e1f82f43731fc1b4 Mon Sep 17 00:00:00 2001 From: Brian West Date: Fri, 28 Feb 2014 16:23:37 -0600 Subject: [PATCH] FS-4502 --resolve --- src/mod/applications/mod_snom/mod_snom.c | 114 +++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/mod/applications/mod_snom/mod_snom.c b/src/mod/applications/mod_snom/mod_snom.c index 36bc438ce1..89a29af00a 100644 --- a/src/mod/applications/mod_snom/mod_snom.c +++ b/src/mod/applications/mod_snom/mod_snom.c @@ -29,6 +29,7 @@ * */ #include +#include SWITCH_MODULE_LOAD_FUNCTION(mod_snom_load); SWITCH_MODULE_DEFINITION(mod_snom, mod_snom_load, NULL, NULL); @@ -104,6 +105,118 @@ SWITCH_STANDARD_API(snom_bind_key_api_function) return SWITCH_STATUS_SUCCESS; } +static size_t curl_callback(void *ptr, size_t size, size_t nmemb, void *data) +{ + register unsigned int realsize = (unsigned int) (size * nmemb); + return realsize; +} + +#define COMMAND_SYNTAX " " +SWITCH_STANDARD_API(snom_command_api_function) +{ + int argc; + long httpRes = 0; + char *url = NULL; + char *argv[5] = { 0 }; + char *argdata = NULL; + char *userpwd = NULL; + char *apiresp = NULL; + ip_t ip; + switch_CURL *curl_handle = NULL; + + if (zstr(cmd) || !(argdata = strdup(cmd))) { + goto end; + } + + argc = switch_separate_string(argdata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))); + + if (argc < 3 || (argc > 3 && argc < 5)) { + stream->write_function(stream, "-ERR %s\n", COMMAND_SYNTAX); + goto end; + } + + if (strcasecmp(argv[1],"key")) { + stream->write_function(stream, "-ERR only KEY command allowed at the moment\n"); + goto end; + } + + if (switch_inet_pton(AF_INET, argv[0], &ip)) { + url = switch_mprintf("http://%s/command.htm?%s=%s",argv[0],argv[1],argv[2]); + } else { + char *sql = NULL; + char buf[32]; + char *ret = NULL; + switch_cache_db_handle_t *db = NULL; + switch_stream_handle_t apistream = { 0 }; + switch_status_t status; + + SWITCH_STANDARD_STREAM(apistream); + if ((status = switch_api_execute("sofia_contact", argv[0], NULL, &apistream)) != SWITCH_STATUS_SUCCESS) { + stream->write_function(stream, "-ERR error executing sofia_contact\n"); + goto end; + } + apiresp = (char*) apistream.data; + + if (!zstr(apiresp)) { + if (!strcasecmp(apistream.data,"error/user_not_registered")) { + stream->write_function(stream, "-ERR user '%s' not registered\n",argv[0]); + goto end; + } + } else { + goto end; + } + + if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) { + stream->write_function(stream, "%s", "-ERR Database Error!\n"); + goto end; + } + + sql = switch_mprintf("select network_ip from registrations where url = '%s'", apiresp); + + ret = switch_cache_db_execute_sql2str(db, sql, buf, sizeof(buf), NULL); + switch_safe_free(sql); + switch_cache_db_release_db_handle(&db); + + if (!ret) { + stream->write_function(stream, "%s", "-ERR Query '%s' failed!\n", sql); + goto end; + } + + url = switch_mprintf("http://%s/command.htm?%s=%s",buf,argv[1],argv[2]); + } + + curl_handle = curl_easy_init(); + curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1); + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, curl_callback); + curl_easy_setopt(curl_handle, CURLOPT_URL, url); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-curl/1.0"); + curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 15); + + if (argc == 5) { + userpwd = switch_mprintf("%s:%s",argv[3],argv[4]); + curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY); + curl_easy_setopt(curl_handle, CURLOPT_USERPWD, userpwd); + } + curl_easy_perform(curl_handle); + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpRes); + curl_easy_cleanup(curl_handle); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "curl url %s , result %ld\n", url,httpRes); + + if (httpRes != 200) + stream->write_function(stream, "-ERR %s [HTTP:%ld]\n", cmd, httpRes); + else + stream->write_function(stream, "+OK %s\n", cmd); + +end: + switch_safe_free(apiresp); + switch_safe_free(userpwd); + switch_safe_free(argdata); + switch_safe_free(url); + + return SWITCH_STATUS_SUCCESS; +} SWITCH_MODULE_LOAD_FUNCTION(mod_snom_load) { @@ -116,6 +229,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_snom_load) SWITCH_ADD_API(commands_api_interface, "snom_bind_key", "Bind a key", snom_bind_key_api_function, KEY_BIND_SYNTAX); SWITCH_ADD_API(commands_api_interface, "snom_url", "url", snom_url_api_function, URL_SYNTAX); + SWITCH_ADD_API(commands_api_interface, "snom_command", "Sends Command over HTTP Request", snom_command_api_function, COMMAND_SYNTAX); /* indicate that the module should continue to be loaded */