diff --git a/src/include/switch_nat.h b/src/include/switch_nat.h index 9e40546345..492dbfb29d 100644 --- a/src/include/switch_nat.h +++ b/src/include/switch_nat.h @@ -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 diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 81cc3fb719..2ab4a18b85 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -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; diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 6fb35598c2..ab402db8f5 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -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] [tcp|udp] [sticky]"); + stream->write_function(stream, "USAGE: nat_map [status|reinit|republish] | [add|del] [tcp|udp] [sticky] | [mapping] "); ok: diff --git a/src/switch.c b/src/switch.c index ad9cdf8e6e..7d4d1a8217 100644 --- a/src/switch.c +++ b/src/switch.c @@ -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++; diff --git a/src/switch_core.c b/src/switch_core.c index 53f21dfb79..85e795c408 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -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); diff --git a/src/switch_nat.c b/src/switch_nat.c index d9110efe18..e9d4b2b9ca 100644 --- a/src/switch_nat.c +++ b/src/switch_nat.c @@ -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 */