From b36f2a23bfc04bd0c18fe1c072a0bb0b9b51a269 Mon Sep 17 00:00:00 2001
From: Rupa Schomaker <rupa@rupa.com>
Date: Sun, 20 Jun 2010 19:21:42 -0500
Subject: [PATCH] timer based nat_map refresh

---
 src/include/switch_nat.h |  8 ++++++++
 src/switch_core.c        |  2 ++
 src/switch_nat.c         | 17 +++++++++++++++++
 3 files changed, 27 insertions(+)
 mode change 100644 => 100755 src/include/switch_nat.h
 mode change 100644 => 100755 src/switch_core.c
 mode change 100644 => 100755 src/switch_nat.c

diff --git a/src/include/switch_nat.h b/src/include/switch_nat.h
old mode 100644
new mode 100755
index ced175ba47..09118c10eb
--- a/src/include/switch_nat.h
+++ b/src/include/switch_nat.h
@@ -57,6 +57,14 @@ typedef enum {
   \note Generally called by the core_init
 */
 SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool);
+
+/*! 
+  \brief Initilize the rest of the NAT Traversal System
+  \note nat_init is called prior to some other modules being loaded.  
+        This method allows us to init the rest of the NAT system.
+*/
+SWITCH_DECLARE(void) switch_nat_late_init(void);
+
 /*!
  \brief Shuts down the NAT Traversal System
 */
diff --git a/src/switch_core.c b/src/switch_core.c
old mode 100644
new mode 100755
index d24ea835b9..37dabbc6e9
--- a/src/switch_core.c
+++ b/src/switch_core.c
@@ -1325,6 +1325,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc
 
 	switch_scheduler_task_thread_start();
 
+	switch_nat_late_init();
+
 	switch_rtp_init(runtime.memory_pool);
 
 	runtime.running = 1;
diff --git a/src/switch_nat.c b/src/switch_nat.c
old mode 100644
new mode 100755
index 0f618efeac..843e139b24
--- a/src/switch_nat.c
+++ b/src/switch_nat.c
@@ -41,6 +41,7 @@
 
 #define MULTICAST_BUFFSIZE 65536
 #define IP_LEN 16
+#define NAT_REFRESH_INTERVAL 900
 
 typedef struct {
 	switch_nat_type_t nat_type;
@@ -677,6 +678,22 @@ SWITCH_DECLARE(void) switch_nat_republish(void)
 	switch_xml_free(natxml);
 }
 
+SWITCH_STANDARD_SCHED_FUNC(switch_nat_republish_sched)
+{
+	switch_nat_republish();
+	if (nat_globals_perm.running == 1) {
+		task->runtime = switch_epoch_time_now(NULL) + NAT_REFRESH_INTERVAL;
+	}
+}
+
+SWITCH_DECLARE(void) switch_nat_late_init(void)
+{
+	if (nat_globals_perm.running == 1) {
+		switch_scheduler_add_task(switch_epoch_time_now(NULL) + NAT_REFRESH_INTERVAL, switch_nat_republish_sched, "nat_republish", "core", 0, NULL,
+								  SSHF_NONE);
+	}
+}
+
 SWITCH_DECLARE(char *) switch_nat_status(void)
 {
 	switch_stream_handle_t stream = { 0 };