]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11568: [core] Add stun-set command to X-PRE-PROCESS.
authorAndrey Volk <andrey@signalwire.com>
Thu, 13 Dec 2018 18:55:43 +0000 (22:55 +0400)
committerAndrey Volk <andrey@signalwire.com>
Wed, 9 Jan 2019 13:19:22 +0000 (17:19 +0400)
src/switch_xml.c

index 37400a13b5184ff7eb9637c58998e23a9a3c8fe6..26e4481b53982aee24f661c54209d5e3890437d1 100644 (file)
@@ -56,6 +56,7 @@
  */
 
 #include <switch.h>
+#include <switch_stun.h>
 #ifndef WIN32
 #include <sys/wait.h>
 #include <switch_private.h>
@@ -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")) {