]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11564: [mod_verto] Add ext-rtp-ip detection using stun.
authorAndrey Volk <andrey@signalwire.com>
Tue, 11 Dec 2018 21:33:06 +0000 (01:33 +0400)
committerAndrey Volk <andrey@signalwire.com>
Wed, 9 Jan 2019 13:19:11 +0000 (17:19 +0400)
src/include/switch_stun.h
src/mod/endpoints/mod_verto/mod_verto.c
src/switch_stun.c

index f05a6e11dfe30a7d4d732ebf24e3e8c43ecebe2b..54b03088e8133d1cd61afad718d3ee6a20a92246 100644 (file)
@@ -259,6 +259,15 @@ SWITCH_DECLARE(uint8_t) switch_stun_packet_attribute_add_priority(switch_stun_pa
 SWITCH_DECLARE(switch_status_t) switch_stun_lookup(char **ip,
                                                                                                   switch_port_t *port, char *stunip, switch_port_t stunport, char **err, switch_memory_pool_t *pool);
 
+/*!
+  \brief Perform a stun ip lookup
+  \param external_ip replaced with stun results
+  \param sourceip stun:, host: or an ip
+  \param external_pool the memory pool to use
+  \return SUCCESS or FAIL
+*/
+SWITCH_DECLARE(switch_status_t) switch_stun_ip_lookup(char **external_ip, const char *sourceip, switch_memory_pool_t *external_pool);
+
 
 /*!
   \brief Obtain the padded length of an attribute's value
index 7dc8336bd9845efde73002a1541fe299209f9bc3..6ad57d276021f2d77274b221513fa63e96b27fce 100644 (file)
@@ -31,6 +31,7 @@
  */
 #include <switch.h>
 #include <switch_json.h>
+#include <switch_stun.h>
 
 
 /* Prototypes */
@@ -4824,7 +4825,7 @@ static switch_status_t parse_config(const char *cf)
                                        if (zstr(val)) {
                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid External RTP IP.\n");
                                        } else {
-                                               profile->extrtpip = switch_core_strdup(profile->pool, val);
+                                               switch_stun_ip_lookup(&profile->extrtpip, val, profile->pool);
                                        }
                                } else if (!strcasecmp(var, "debug")) {
                                        if (val) {
index 6573d3eceb186a8d140c93e825b8d5f12b766a85..f759a2607c21d589c66a507f2383fed052e3f761 100644 (file)
@@ -851,6 +851,79 @@ SWITCH_DECLARE(switch_status_t) switch_stun_lookup(char **ip,
        return SWITCH_STATUS_FALSE;
 }
 
+SWITCH_DECLARE(switch_status_t) switch_stun_ip_lookup(char **external_ip, const char *sourceip, switch_memory_pool_t *external_pool)
+{
+       switch_status_t status = SWITCH_STATUS_FALSE;
+       char *stun_ip = NULL;
+       switch_port_t stun_port = (switch_port_t)SWITCH_STUN_DEFAULT_PORT;
+       char *p;
+       char ip_buf[256] = "";
+       char *ip = NULL;
+       switch_port_t port = 0;
+       switch_memory_pool_t *local_pool = NULL;
+       char *error = "";
+
+       if (!sourceip || !external_pool) {
+               *external_ip = NULL;
+               goto end;
+       }
+
+       ip = ip_buf;
+
+       if (!strncasecmp(sourceip, "host:", 5)) {
+               status = (*external_ip = switch_stun_host_lookup(sourceip + 5, external_pool)) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
+       }
+       else if (!strncasecmp(sourceip, "stun:", 5)) {
+
+               switch_core_new_memory_pool(&local_pool);
+
+               stun_ip = switch_core_strdup(local_pool, sourceip + 5);
+
+               switch_assert(stun_ip);
+
+               if ((p = strchr(stun_ip, ':'))) {
+                       int iport;
+                       *p++ = '\0';
+                       iport = atoi(p);
+                       if (iport > 0 && iport < 0xFFFF) {
+                               stun_port = (switch_port_t)iport;
+                       }
+               }
+               else {
+                       p = stun_ip;
+               }
+
+               switch_find_local_ip(ip_buf, sizeof(ip_buf), NULL, AF_INET);
+
+               if (zstr(stun_ip)) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "STUN Failed! NO STUN SERVER\n");
+               }
+               else {
+                       if ((switch_stun_lookup(&ip, &port, stun_ip, stun_port, &error, local_pool)) == SWITCH_STATUS_SUCCESS && ip && port) {
+                               *external_ip = switch_core_strdup(external_pool, ip);
+                               status = SWITCH_STATUS_SUCCESS;
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "External ip address detected using STUN: %s\n", ip);
+                       }
+                       else {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "STUN Failed! [%s]\n", error);
+                       }
+               }
+
+               if (status != SWITCH_STATUS_SUCCESS) {
+                       *external_ip = "";
+               }
+
+               switch_core_destroy_memory_pool(&local_pool);
+       }
+       else {
+               *external_ip = switch_core_strdup(external_pool, sourceip);
+               status = SWITCH_STATUS_SUCCESS;
+       }
+
+end:
+
+       return status;
+}