]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-3208 --resolve
authorAnthony Minessale <anthm@freeswitch.org>
Fri, 1 Apr 2011 17:30:24 +0000 (12:30 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Fri, 1 Apr 2011 17:30:24 +0000 (12:30 -0500)
src/include/switch_nat.h
src/include/switch_types.h
src/mod/applications/mod_commands/mod_commands.c
src/switch.c
src/switch_core.c
src/switch_nat.c

index 9e405463450d094447e79a8bd6e670e9796fee84..492dbfb29d9d7990aa3be038effd6bc49334709e 100644 (file)
@@ -56,7 +56,7 @@ SWITCH_DECLARE(const char *) switch_nat_get_type(void);
   \param pool the memory pool to use for long term allocations
   \note Generally called by the core_init
 */
-SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool);
+SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool, switch_bool_t mapping);
 
 /*! 
   \brief Initilize the rest of the NAT Traversal System
@@ -86,6 +86,11 @@ SWITCH_DECLARE(void) switch_nat_republish(void);
 */
 SWITCH_DECLARE(void) switch_nat_reinit(void);
 
+/*!
+ \brief Update the setting if port mapping will be created
+*/
+SWITCH_DECLARE(void) switch_nat_set_mapping(switch_bool_t mapping);
+
 /*!
  \brief Maps a port through the NAT Traversal System
  \param port Internal port to map
index 81cc3fb719301f122f3cfca2e5b24a0eb57ea750..2ab4a18b85cbcb387e6cb20abc3deea234830c99 100644 (file)
@@ -265,7 +265,8 @@ typedef enum {
        SCF_VERBOSE_EVENTS = (1 << 11),
        SCF_USE_WIN32_MONOTONIC = (1 << 12),
        SCF_AUTO_SCHEMAS = (1 << 13),
-       SCF_MINIMAL = (1 << 14)
+       SCF_MINIMAL = (1 << 14),
+       SCF_USE_NAT_MAPPING = (1 << 15)
 } switch_core_flag_enum_t;
 typedef uint32_t switch_core_flag_t;
 
index 6fb35598c22239babe55dad15e324fde8e8e2e37..ab402db8f5529b7710e83590c78bcc7aff96a246 100644 (file)
@@ -329,6 +329,7 @@ SWITCH_STANDARD_API(nat_map_function)
        switch_port_t external_port = 0;
        char *tmp = NULL;
        switch_bool_t sticky = SWITCH_FALSE;
+       switch_bool_t mapping = SWITCH_TRUE;
 
        if (!cmd) {
                goto usage;
@@ -363,6 +364,24 @@ SWITCH_STANDARD_API(nat_map_function)
                goto ok;
        }
 
+       if (argc < 2) {
+               goto usage;
+       }
+
+       if (argv[0] && switch_stristr("mapping", argv[0])) {
+               if (argv[1] && switch_stristr("enable", argv[1])) {
+                       mapping = SWITCH_TRUE;
+               } else if (argv[1] && switch_stristr("disable", argv[1])) {
+                       mapping = SWITCH_FALSE;
+               }
+
+               switch_nat_set_mapping(mapping);
+               tmp = switch_nat_status();
+               stream->write_function(stream, tmp);
+               switch_safe_free(tmp);
+               goto ok;
+       }
+
        if (argc < 3) {
                goto error;
        }
@@ -395,7 +414,7 @@ SWITCH_STANDARD_API(nat_map_function)
        goto ok;
 
  usage:
-       stream->write_function(stream, "USAGE: nat_map [status|reinit|republish] | [add|del] <port> [tcp|udp] [sticky]");
+       stream->write_function(stream, "USAGE: nat_map [status|reinit|republish] | [add|del] <port> [tcp|udp] [sticky] | [mapping] <enable|disable>");
 
   ok:
 
index ad9cdf8e6ed54859d347eafd86361509ba70295b..7d4d1a82176a5e791d1ae25380cbac37080e8f6c 100644 (file)
@@ -200,7 +200,7 @@ void WINAPI ServiceCtrlHandler(DWORD control)
 /* the main service entry point */
 void WINAPI service_main(DWORD numArgs, char **args)
 {
-       switch_core_flag_t flags = SCF_USE_SQL | SCF_USE_AUTO_NAT | SCF_CALIBRATE_CLOCK | SCF_USE_CLOCK_RT;
+       switch_core_flag_t flags = SCF_USE_SQL | SCF_USE_AUTO_NAT | SCF_USE_NAT_MAPPING | SCF_CALIBRATE_CLOCK | SCF_USE_CLOCK_RT;
        const char *err = NULL;         /* error value for return from freeswitch initialization */
 
        /* Override flags if they have been set earlier */
@@ -364,7 +364,7 @@ int main(int argc, char *argv[])
 #ifdef __sun
        switch_core_flag_t flags = SCF_USE_SQL;
 #else
-       switch_core_flag_t flags = SCF_USE_SQL | SCF_USE_AUTO_NAT | SCF_CALIBRATE_CLOCK | SCF_USE_CLOCK_RT;
+       switch_core_flag_t flags = SCF_USE_SQL | SCF_USE_AUTO_NAT | SCF_USE_NAT_MAPPING | SCF_CALIBRATE_CLOCK | SCF_USE_CLOCK_RT;
 #endif
        int ret = 0;
        switch_status_t destroy_status;
@@ -412,6 +412,7 @@ int main(int argc, char *argv[])
                "\t-nosql                 -- disable internal sql scoreboard\n"
                "\t-heavy-timer           -- Heavy Timer, possibly more accurate but at a cost\n"
                "\t-nonat                 -- disable auto nat detection\n"
+               "\t-nonatmap              -- disable auto nat port mapping\n"
                "\t-nocal                 -- disable clock calibration\n"
                "\t-nort                  -- disable clock clock_realtime\n"
                "\t-stop                  -- stop freeswitch\n"
@@ -580,6 +581,11 @@ int main(int argc, char *argv[])
                        known_opt++;
                }
 
+               if (local_argv[x] && !strcmp(local_argv[x], "-nonatmap")) {
+                       flags &= ~SCF_USE_NAT_MAPPING;
+                       known_opt++;
+               }
+
                if (local_argv[x] && !strcmp(local_argv[x], "-heavy-timer")) {
                        flags |= SCF_USE_HEAVY_TIMING;
                        known_opt++;
index 53f21dfb79bc27add13b8f835bf8ba0ac7ae585b..85e795c408f0823e7df2f646023fb73650647cb4 100644 (file)
@@ -1398,7 +1398,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc
        }
 
        if (switch_test_flag((&runtime), SCF_USE_AUTO_NAT)) {
-               switch_nat_init(runtime.memory_pool);
+               switch_nat_init(runtime.memory_pool, switch_test_flag((&runtime), SCF_USE_NAT_MAPPING));
        }
 
        switch_log_init(runtime.memory_pool, runtime.colorize_console);
index d9110efe18799e5297500102e174038341b7957a..e9d4b2b9cabe039cc9fa0644b025ff37dfb974a6 100644 (file)
@@ -51,6 +51,7 @@ typedef struct {
        char *descURL;
        char pub_addr[IP_LEN];
        char pvt_addr[IP_LEN];
+       switch_bool_t mapping;
 } nat_globals_t;
 
 static nat_globals_t nat_globals;
@@ -207,9 +208,14 @@ static int init_pmp(void)
        return get_pmp_pubaddr(nat_globals.pub_addr);
 }
 
+SWITCH_DECLARE(void) switch_nat_set_mapping(switch_bool_t mapping)
+{
+       nat_globals.mapping = mapping;
+}
+
 SWITCH_DECLARE(void) switch_nat_reinit(void)
 {
-       switch_nat_init(nat_globals_perm.pool);
+       switch_nat_init(nat_globals_perm.pool, nat_globals.mapping);
 }
 
 switch_status_t init_nat_monitor(switch_memory_pool_t *pool)
@@ -392,7 +398,7 @@ SWITCH_DECLARE(void) switch_nat_thread_stop(void)
 }
 
 
-SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool)
+SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool, switch_bool_t mapping)
 {
        /* try free dynamic data structures prior to resetting to 0 */
        FreeUPNPUrls(&nat_globals.urls);
@@ -405,6 +411,8 @@ SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool)
                nat_globals_perm.pool = pool;
        }
 
+       nat_globals.mapping = mapping;
+
        switch_find_local_ip(nat_globals.pvt_addr, sizeof(nat_globals.pvt_addr), NULL, AF_INET);
 
 
@@ -577,6 +585,11 @@ SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping_internal(switch_port_t po
        switch_status_t status = SWITCH_STATUS_FALSE;
        switch_event_t *event = NULL;
 
+       if (!nat_globals.mapping) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "NAT port mapping disabled\n");
+               return status;
+       }
+
        switch (nat_globals.nat_type) {
        case SWITCH_NAT_TYPE_PMP:
                status = switch_nat_add_mapping_pmp(port, proto, external_port);
@@ -710,6 +723,12 @@ SWITCH_DECLARE(char *) switch_nat_status(void)
                                                  (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) ? "UPNP" : (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "NAT-PMP" : "UNKNOWN"),
                                                  nat_globals.pub_addr);
 
+       if (nat_globals.mapping) {
+               stream.write_function(&stream, "NAT port mapping enabled.\n");
+       } else {
+               stream.write_function(&stream, "NAT port mapping disabled.\n");
+       }
+
        switch_api_execute("show", "nat_map", NULL, &stream);
 
        return stream.data;                     /* caller frees */