* Bret McDanel <trixter AT 0xdecafbad.com>
* Cesar Cepeda <cesar@auronix.com>
* Massimo Cetra <devel@navynet.it>
+ * Rupa Schomaker <rupa@rupa.com>
*
*
* mod_commands.c -- Misc. Command Module
SWITCH_STANDARD_API(nat_map_function)
{
int argc;
- char *mydata = NULL, *argv[4];
+ char *mydata = NULL, *argv[5];
switch_nat_ip_proto_t proto = SWITCH_NAT_UDP;
switch_port_t external_port = 0;
+ char *tmp = NULL;
+ switch_bool_t sticky = SWITCH_FALSE;
if (!cmd) {
goto error;
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+ if (argc < 1) {
+ goto error;
+ }
+ if (argv[0] && switch_stristr("status", argv[0])) {
+ tmp = switch_nat_status();
+ stream->write_function(stream, tmp);
+ switch_safe_free(tmp);
+ goto ok;
+ } else if (argv[0] && switch_stristr("republish", argv[0])) {
+ switch_nat_republish();
+ stream->write_function(stream, "true");
+ goto ok;
+ } else if (argv[0] && switch_stristr("reinit", argv[0])) {
+ switch_nat_reinit();
+ stream->write_function(stream, "true");
+ tmp = switch_nat_status();
+ stream->write_function(stream, tmp);
+ switch_safe_free(tmp);
+ goto ok;
+ }
+
if (argc < 3) {
goto error;
}
} else if (argv[2] && switch_stristr("udp", argv[2])) {
proto = SWITCH_NAT_UDP;
}
+
+ if (argv[3] && switch_stristr("sticky", argv[3])) {
+ sticky = SWITCH_TRUE;
+ }
if (argv[0] && switch_stristr("add", argv[0])) {
- if (switch_nat_add_mapping((switch_port_t)atoi(argv[1]), proto, &external_port) == SWITCH_STATUS_SUCCESS) {
+ if (switch_nat_add_mapping((switch_port_t)atoi(argv[1]), proto, &external_port, sticky) == SWITCH_STATUS_SUCCESS) {
stream->write_function(stream, "%d", (int)external_port);
goto ok;
}
return SWITCH_STATUS_SUCCESS;
}
-#define SHOW_SYNTAX "codec|endpoint|application|api|dialplan|file|timer|calls [count]|channels [count]|distinct_channels|aliases|complete|chat|endpoint|management|modules|say|interfaces|interface_types"
+#define SHOW_SYNTAX "codec|endpoint|application|api|dialplan|file|timer|calls [count]|channels [count]|distinct_channels|aliases|complete|chat|endpoint|management|modules|nat_map|say|interfaces|interface_types"
SWITCH_STANDARD_API(show_function)
{
char sql[1024];
} else {
switch_snprintf(sql, sizeof(sql) - 1, "select name, syntax, description, key from interfaces where type = 'api' order by name");
}
+ } else if (!strcasecmp(command, "nat_map")) {
+ switch_snprintf(sql, sizeof(sql) - 1,
+ "SELECT port, "
+ " CASE proto "
+ " WHEN 0 THEN 'udp' "
+ " WHEN 1 THEN 'tcp' "
+ " ELSE 'unknown' "
+ " END AS proto, "
+ " proto AS proto_num, "
+ " sticky "
+ " FROM nat ORDER BY port, proto"
+ );
} else {
stream->write_function(stream, "-USAGE: %s\n", SHOW_SYNTAX);
goto end;
switch_xml_set_attr(switch_xml_set_flag(holder.xml, SWITCH_XML_DUP), strdup("row_count"), strdup(count));
xmlstr = switch_xml_toxml(holder.xml, SWITCH_FALSE);
+ switch_xml_free(holder.xml);
if (xmlstr) {
holder.stream->write_function(holder.stream, "%s", xmlstr);
SWITCH_ADD_API(commands_api_interface, "stun", "stun", stun_function, "<stun_server>[:port]");
SWITCH_ADD_API(commands_api_interface, "system", "Execute a system command", system_function, SYSTEM_SYNTAX);
SWITCH_ADD_API(commands_api_interface, "time_test", "time_test", time_test_function, "<mss>");
- SWITCH_ADD_API(commands_api_interface, "nat_map", "nat_map", nat_map_function, "[add|del] <port> [tcp|udp]");
+ SWITCH_ADD_API(commands_api_interface, "nat_map", "nat_map", nat_map_function, "[status|republish|reinit] | [add|del] <port> [tcp|udp] [static]");
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_NOUNLOAD;
switch_event_get_header_nil(event, "caller-unique-id"));
break;
}
+ case SWITCH_EVENT_NAT:
+ {
+ const char *op = switch_event_get_header_nil(event, "op");
+ switch_bool_t sticky = switch_true(switch_event_get_header_nil(event, "sticky"));
+ if (!strcmp("add", op)) {
+ sql = switch_mprintf("insert into nat (port, proto, sticky) values (%s, %s, %d)",
+ switch_event_get_header_nil(event, "port"),
+ switch_event_get_header_nil(event, "proto"),
+ sticky);
+ } else if (!strcmp("del", op)) {
+ sql = switch_mprintf("delete from nat where port=%s and proto=%s",
+ switch_event_get_header_nil(event, "port"),
+ switch_event_get_header_nil(event, "proto"));
+ } else if (!strcmp("status", op)) {
+ /* call show nat api */
+ } else if (!strcmp("status_response", op)) {
+ /* ignore */
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown op for SWITCH_EVENT_NAT: %s\n", op);
+ }
+ break;
+ }
default:
break;
}
" task_group VARCHAR(255),\n"
" task_sql_manager INTEGER(8)\n"
");\n";
+ char create_nat_sql[] =
+ "CREATE TABLE nat (\n"
+ " sticky INTEGER,\n"
+ " port INTEGER,\n"
+ " proto INTEGER\n"
+ ");\n";
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Opening DB\n");
switch_core_db_exec(sql_manager.db, "drop table channels", NULL, NULL, NULL);
switch_core_db_test_reactive(sql_manager.db, "select sticky from complete", "DROP TABLE complete", create_complete_sql);
switch_core_db_test_reactive(sql_manager.db, "select sticky from aliases", "DROP TABLE aliases", create_alias_sql);
+ switch_core_db_test_reactive(sql_manager.db, "select sticky from nat", "DROP TABLE nat", create_nat_sql);
switch_core_db_exec(sql_manager.db, "delete from complete where sticky=0", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "delete from aliases where sticky=0", NULL, NULL, NULL);
+ switch_core_db_exec(sql_manager.db, "delete from nat where sticky=0", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "create index if not exists alias1 on aliases (alias)", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "create index if not exists complete1 on complete (a1)", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "create index if not exists complete2 on complete (a2)", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "create index if not exists complete8 on complete (a8)", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "create index if not exists complete9 on complete (a9)", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, "create index if not exists complete10 on complete (a10)", NULL, NULL, NULL);
+ switch_core_db_exec(sql_manager.db, "create unique index if not exists nat_map_port_proto on nat (port,proto)", NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, create_channels_sql, NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, create_calls_sql, NULL, NULL, NULL);
switch_core_db_exec(sql_manager.db, create_interfaces_sql, NULL, NULL, NULL);
*
* Anthony Minessale II <anthm@freeswitch.org>
* Brian K. West <brian@freeswitch.org>
+ * Rupa Schomaker <rupa@rupa.com>
*
*
* switch_nat.c NAT Traversal via NAT-PMP or uPNP
#include "../libs/miniupnpc/upnperrors.h"
#include "../libs/libnatpmp/natpmp.h"
+#define MULTICAST_BUFFSIZE 65536
+
typedef struct {
- switch_memory_pool_t *pool;
switch_nat_type_t nat_type;
struct UPNPUrls urls;
struct IGDdatas data;
+ char *descURL;
char pub_addr[16];
char pvt_addr[16];
-
} nat_globals_t;
static nat_globals_t nat_globals;
+typedef struct {
+ switch_memory_pool_t *pool;
+ int running;
+ switch_sockaddr_t *maddress;
+ switch_socket_t *msocket;
+} nat_globals_perm_t;
+
+static nat_globals_perm_t nat_globals_perm;
+
+static switch_bool_t first_init = SWITCH_TRUE;
+
+static switch_status_t get_upnp_pubaddr(char *pub_addr)
+{
+ if (UPNP_GetExternalIPAddress(nat_globals.urls.controlURL,
+ nat_globals.data.servicetype,
+ pub_addr) == UPNPCOMMAND_SUCCESS) {
+ if (!strcmp(pub_addr, "0.0.0.0") || switch_strlen_zero(pub_addr)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+ "uPNP Device (url: %s) returned an invalid external address of '%s'. Disabling uPNP\n", nat_globals.urls.controlURL, pub_addr);
+ return SWITCH_STATUS_GENERR;
+ }
+ }
+ return SWITCH_STATUS_SUCCESS;
+}
+
static int init_upnp (void)
{
struct UPNPDev *devlist;
int descXMLsize = 0;
const char *multicastif = 0;
const char *minissdpdpath = 0;
- int r = -2;
memset(&nat_globals.urls, 0, sizeof(struct UPNPUrls));
memset(&nat_globals.data, 0, sizeof(struct IGDdatas));
}
descXML = miniwget(dev->descURL, &descXMLsize);
-
+
+ nat_globals.descURL = strdup(dev->descURL);
+
if (descXML) {
parserootdesc (descXML, descXMLsize, &nat_globals.data);
free (descXML); descXML = 0;
freeUPNPDevlist(devlist);
}
- if ((r = UPNP_GetExternalIPAddress(nat_globals.urls.controlURL,
- nat_globals.data.servicetype,
- nat_globals.pub_addr)) == UPNPCOMMAND_SUCCESS) {
-
- if (!strcmp(nat_globals.pub_addr, "0.0.0.0")) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
- "uPNP Device (url: %s) returned an invalid external address of 0.0.0.0. Disabling uPNP\n", nat_globals.urls.controlURL);
- return -2;
- }
-
+ if (get_upnp_pubaddr(nat_globals.pub_addr) == SWITCH_STATUS_SUCCESS) {
nat_globals.nat_type = SWITCH_NAT_TYPE_UPNP;
return 0;
}
return -2;
}
-static int init_pmp(void)
+static int get_pmp_pubaddr(char *pub_addr)
{
int r = 0, i = 0, max = 5;
natpmpresp_t response;
}
pubaddr = inet_ntoa(response.pnu.publicaddress.addr);
- switch_set_string(nat_globals.pub_addr, pubaddr);
+ switch_set_string(pub_addr, pubaddr);
nat_globals.nat_type = SWITCH_NAT_TYPE_PMP;
closenatpmp(&natpmp);
return r;
}
+static int init_pmp(void)
+{
+ return get_pmp_pubaddr(nat_globals.pub_addr);
+}
+
+SWITCH_DECLARE(void) switch_nat_reinit(void)
+{
+ switch_nat_init(nat_globals_perm.pool);
+}
+
+switch_status_t init_nat_monitor(switch_memory_pool_t *pool)
+{
+ char *addr = NULL;
+ switch_port_t port = 0;
+
+ if (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) {
+ addr = "239.255.255.250";
+ port = 1900;
+ } else if (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP) {
+ addr = "224.0.0.1";
+ port = 5350;
+ }
+
+ if (switch_sockaddr_info_get(&nat_globals_perm.maddress, addr, SWITCH_UNSPEC, port, 0, pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find address\n");
+ return SWITCH_STATUS_TERM;
+ }
+
+ if (switch_socket_create(&nat_globals_perm.msocket, AF_INET, SOCK_DGRAM, 0, pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error\n");
+ return SWITCH_STATUS_TERM;
+ }
+
+ if (switch_socket_opt_set(nat_globals_perm.msocket, SWITCH_SO_REUSEADDR, 1) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Option Error\n");
+ switch_socket_close(nat_globals_perm.msocket);
+ return SWITCH_STATUS_TERM;
+ }
+
+ if (switch_mcast_join(nat_globals_perm.msocket, nat_globals_perm.maddress, NULL, NULL) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Multicast Error\n");
+ switch_socket_close(nat_globals_perm.msocket);
+ return SWITCH_STATUS_TERM;
+ }
+
+ if (switch_socket_bind(nat_globals_perm.msocket, nat_globals_perm.maddress) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Bind Error\n");
+ switch_socket_close(nat_globals_perm.msocket);
+ return SWITCH_STATUS_TERM;
+ }
+
+ switch_socket_opt_set(nat_globals_perm.msocket, SWITCH_SO_NONBLOCK, TRUE);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NAT thread configured\n");
+ return SWITCH_STATUS_SUCCESS;
+}
+
+static void *SWITCH_THREAD_FUNC switch_nat_multicast_runtime(switch_thread_t *thread, void *obj)
+{
+ char *buf = NULL;
+ char newip[16];
+ switch_event_t *event = NULL;
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NAT thread started\n");
+
+ buf = (char *) malloc(MULTICAST_BUFFSIZE);
+ switch_assert(buf);
+ nat_globals_perm.running = 1;
+
+ while (nat_globals_perm.running == 1) {
+ size_t len = MULTICAST_BUFFSIZE;
+ switch_status_t status;
+ switch_bool_t do_repub = SWITCH_FALSE;
+ memset(buf, 0, len);
+
+ status = switch_socket_recvfrom(nat_globals_perm.maddress, nat_globals_perm.msocket, 0, buf, &len);
+
+ if (!len) {
+ if (SWITCH_STATUS_IS_BREAK(status)) {
+ switch_yield(5000000);
+ continue;
+ }
+
+ break;
+ }
+
+ if (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) {
+ /* look for our desc URL and servicetype in the packet */
+ if (strstr(buf, nat_globals.descURL) && strstr(buf, nat_globals.data.servicetype)) {
+ if (strstr(buf, "NTS: ssdp:alive")) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got UPnP keep alive packet: \n%s\n", buf);
+ /* did pub ip change */
+ if (get_upnp_pubaddr(newip) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to get current pubaddr after receiving UPnP keep alive packet.\n");
+ }
+ } else if (strstr(buf, "NTS: ssdp:byebye")) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "got UPnP signoff packet. Your NAT gateway is probably going offline.\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got UPnP signoff packet: \n%s\n", buf);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got UNKNOWN UPnP keep alive packet: \n%s\n", buf);
+ }
+ }
+ } else {
+ /* got some data in NAT-PMP mode, treat any data as a republish event */
+ if (get_pmp_pubaddr(newip) < 0) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to get current pubaddr after receiving UPnP keep alive packet.\n");
+ }
+ }
+
+ if ((strlen(newip) > 0) && strcmp(newip, "0.0.0.0") && strcmp(newip, nat_globals.pub_addr)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Public IP changed from '%s' to '%s'.\n", nat_globals.pub_addr, newip);
+ do_repub = SWITCH_TRUE;
+
+ switch_event_create(&event, SWITCH_EVENT_TRAP);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "condition", "network-address-change");
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-previous-v4", nat_globals.pub_addr);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-change-v4", newip);
+ switch_event_fire(&event);
+
+ switch_set_string(nat_globals.pub_addr, newip);
+ switch_nat_reinit();
+ }
+
+ if (do_repub) {
+ switch_nat_republish();
+ }
+ }
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NAT thread ending\n");
+ nat_globals_perm.running = 0;
+
+ switch_safe_free(buf);
+
+ return NULL;
+}
+
+switch_thread_t *nat_thread_p = NULL;
+
+SWITCH_DECLARE(void) switch_nat_thread_start(void)
+{
+
+ switch_threadattr_t *thd_attr;
+
+ if (init_nat_monitor(nat_globals_perm.pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to initialize NAT thread\n");
+ return;
+ }
+
+ switch_threadattr_create(&thd_attr, nat_globals_perm.pool);
+ switch_threadattr_detach_set(thd_attr, 1);
+ switch_thread_create(&nat_thread_p, thd_attr, switch_nat_multicast_runtime, NULL, nat_globals_perm.pool);
+}
+
+SWITCH_DECLARE(void) switch_nat_thread_stop(void)
+{
+ /* don't do anything if no thread ptr */
+ if (!nat_thread_p ) {
+ return;
+ }
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping NAT Task Thread\n");
+ if (nat_globals_perm.running == 1) {
+ int sanity = 0;
+ switch_status_t st;
+
+ nat_globals_perm.running = -1;
+
+ switch_thread_join(&st, nat_thread_p);
+
+ while (nat_globals_perm.running) {
+ switch_yield(1000000); /* can take up to 5s for the thread to terminate, so wait for 10 */
+ if (++sanity > 10) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Timed out waiting for NAT Task Thread to stop\n");
+ break;
+ }
+ }
+ }
+
+ nat_thread_p = NULL;
+}
+
+
SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool)
{
- memset(&nat_globals, 0, sizeof(nat_globals));
- nat_globals.pool = pool;
+ /* try free dynamic data structures prior to resetting to 0 */
+ FreeUPNPUrls(&nat_globals.urls);
+ switch_safe_free(nat_globals.descURL);
+ memset(&nat_globals, 0, sizeof(nat_globals));
+
+ if (first_init) {
+ memset(&nat_globals_perm, 0, sizeof(nat_globals_perm));
+ nat_globals_perm.pool = pool;
+ }
+
switch_find_local_ip(nat_globals.pvt_addr, sizeof(nat_globals.pvt_addr), NULL, AF_INET);
switch_core_set_variable("nat_public_addr", nat_globals.pub_addr);
switch_core_set_variable("nat_private_addr", nat_globals.pvt_addr);
switch_core_set_variable("nat_type", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp");
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "NAT detected type: %s\n", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "NAT detected type: %s, ExtIP: '%s'\n", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp", nat_globals.pub_addr);
+
+ if (!nat_thread_p) {
+ switch_nat_thread_start();
+ }
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No PMP or UPnP NAT detected!\n");
}
+ first_init = SWITCH_FALSE;
}
static switch_status_t switch_nat_add_mapping_pmp(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port)
return status;
}
-SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port)
+SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping_internal(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port, switch_bool_t sticky, switch_bool_t publish)
{
switch_status_t status = SWITCH_STATUS_FALSE;
+ switch_event_t *event = NULL;
+ char key[1024] = "";
switch (nat_globals.nat_type) {
case SWITCH_NAT_TYPE_PMP:
default:
break;
}
+
+ if (publish && status == SWITCH_STATUS_SUCCESS) {
+ switch_event_create(&event, SWITCH_EVENT_NAT);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "op", "add");
+ switch_snprintf(key, sizeof(key), "%d", port);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "port", key);
+ switch_snprintf(key, sizeof(key), "%d", proto);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", key);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "sticky", (sticky ? "true" : "false"));
+ switch_event_fire(&event);
+ }
return status;
}
+SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port, switch_bool_t sticky)
+{
+ return switch_nat_add_mapping_internal(port, proto, external_port, sticky, SWITCH_TRUE);
+}
+
SWITCH_DECLARE(switch_status_t) switch_nat_del_mapping(switch_port_t port, switch_nat_ip_proto_t proto)
{
switch_status_t status = SWITCH_STATUS_FALSE;
+ switch_event_t *event = NULL;
+ char key[1024] = "";
switch (nat_globals.nat_type) {
case SWITCH_NAT_TYPE_PMP:
break;
}
+ if (status == SWITCH_STATUS_SUCCESS) {
+ switch_event_create(&event, SWITCH_EVENT_NAT);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "op", "del");
+ switch_snprintf(key, sizeof(key), "%d", port);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "port", key);
+ switch_snprintf(key, sizeof(key), "%d", proto);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", key);
+ switch_event_fire(&event);
+ }
+
return status;
}
-SWITCH_DECLARE(void) switch_nat_shutdown(void)
+SWITCH_DECLARE(void) switch_nat_republish(void)
{
+ switch_xml_t natxml = NULL;
+ switch_xml_t row = NULL;
+ switch_xml_t child = NULL;
+ switch_stream_handle_t stream = { 0 };
+ SWITCH_STANDARD_STREAM(stream);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Refreshing nat maps\n");
+
+ switch_api_execute("show", "nat_map as xml", NULL, &stream);
+
+ if (!(natxml = switch_xml_parse_str_dup(stream.data))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse XML: %s\n", (char *) stream.data);
+ switch_safe_free(stream.data);
+ return;
+ }
+
+ /* iterate the xml and publish the mappings */
+ row = switch_xml_find_child(natxml, "row", "row_id", "1");
+ while (row != NULL) {
+ char *sport = NULL;
+ char *sproto = NULL;
+ switch_port_t port;
+ switch_nat_ip_proto_t proto;
+
+ if ((child = switch_xml_child(row, "port"))) {
+ sport = child->txt;
+ }
+ if ((child = switch_xml_child(row, "proto_num"))) {
+ sproto = child->txt;
+ }
+
+ if (sport && sproto) {
+ port = (switch_port_t)(atoi(sport));
+ proto = (switch_nat_ip_proto_t)(atoi(sproto));
+ switch_nat_add_mapping_internal(port, proto, NULL, SWITCH_FALSE, SWITCH_FALSE);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse port/proto info: XML: %s\n", (char *) stream.data);
+ }
+
+ row = switch_xml_next(row);
+ }
+
+ switch_safe_free(stream.data);
+ switch_xml_free(natxml);
+}
+
+SWITCH_DECLARE(char *) switch_nat_status(void)
+{
+ switch_stream_handle_t stream = { 0 };
+ SWITCH_STANDARD_STREAM(stream);
+
+ stream.write_function(&stream, "Nat Type: %s, ExtIP: %s\n",
+ (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) ? "UPNP" : (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "NAT-PMP" : "UNKNOWN"),
+ nat_globals.pub_addr);
+
+ switch_api_execute("show", "nat_map", NULL, &stream);
+
+ return stream.data; /* caller frees */
+}
+
+
+SWITCH_DECLARE(void) switch_nat_shutdown(void)
+{
+ switch_nat_thread_stop();
+ FreeUPNPUrls(&nat_globals.urls);
+ switch_safe_free(nat_globals.descURL);
}