From 0629f95cff12b270e912194d9f01fbb44cb42371 Mon Sep 17 00:00:00 2001 From: Andrey Volk Date: Thu, 13 Dec 2018 22:55:43 +0400 Subject: [PATCH] FS-11568: [core] Add stun-set command to X-PRE-PROCESS. --- src/switch_xml.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/switch_xml.c b/src/switch_xml.c index 37400a13b5..26e4481b53 100644 --- a/src/switch_xml.c +++ b/src/switch_xml.c @@ -56,6 +56,7 @@ */ #include +#include #ifndef WIN32 #include #include @@ -141,6 +142,46 @@ static void preprocess_exec_set(char *keyval) } } +static void preprocess_stun_set(char *keyval) +{ + char *key = keyval; + char *val = strchr(key, '='); + + if (val) { + char *ve = val++; + while (*val && *val == ' ') { + val++; + } + *ve-- = '\0'; + while (*ve && *ve == ' ') { + *ve-- = '\0'; + } + } + + if (key && val) { + char *external_ip = NULL; + switch_memory_pool_t *pool; + + switch_core_new_memory_pool(&pool); + + if (switch_stun_ip_lookup(&external_ip, val, pool) == SWITCH_STATUS_SUCCESS) { + if (!zstr(external_ip)) { + char *tmp = external_ip; + tmp = &tmp[strlen(tmp) - 1]; + while (tmp >= external_ip && (tmp[0] == ' ' || tmp[0] == '\n')) { + tmp[0] = '\0'; /* remove trailing spaces and newlines */ + tmp--; + } + switch_core_set_variable(key, external_ip); + } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "stun-set failed.\n"); + } + + switch_core_destroy_memory_pool(&pool); + } +} + static void preprocess_env_set(char *keyval) { char *key = keyval; @@ -1487,6 +1528,8 @@ static int preprocess(const char *cwd, const char *file, FILE *write_fd, int rle } else if (!strcasecmp(tcmd, "exec-set")) { preprocess_exec_set(targ); + } else if (!strcasecmp(tcmd, "stun-set")) { + preprocess_stun_set(targ); } else if (!strcasecmp(tcmd, "env-set")) { preprocess_env_set(targ); } else if (!strcasecmp(tcmd, "include")) { @@ -1549,6 +1592,8 @@ static int preprocess(const char *cwd, const char *file, FILE *write_fd, int rle } else if (!strcasecmp(cmd, "exec-set")) { preprocess_exec_set(arg); + } else if (!strcasecmp(cmd, "stun-set")) { + preprocess_stun_set(arg); } else if (!strcasecmp(cmd, "include")) { preprocess_glob(cwd, arg, write_fd, rlevel + 1); } else if (!strcasecmp(cmd, "exec")) {