]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
do_patches
authorBrian West <brian@freeswitch.org>
Sat, 21 Mar 2009 23:24:48 +0000 (23:24 +0000)
committerBrian West <brian@freeswitch.org>
Sat, 21 Mar 2009 23:24:48 +0000 (23:24 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12706 d0543943-73ff-0310-b7d9-9358b9ac24b2

patches/pa.diff [deleted file]
patches/sofia.diff [new file with mode: 0644]

diff --git a/patches/pa.diff b/patches/pa.diff
deleted file mode 100644 (file)
index 9a82698..0000000
+++ /dev/null
@@ -1,254 +0,0 @@
-Index: src/mod/endpoints/mod_portaudio/mod_portaudio.c
-===================================================================
---- src/mod/endpoints/mod_portaudio/mod_portaudio.c    (revision 12655)
-+++ src/mod/endpoints/mod_portaudio/mod_portaudio.c    (working copy)
-@@ -124,7 +124,8 @@
-       private_t *call_list;
-       int ring_interval;
-       GFLAGS flags;
--      switch_timer_t timer;
-+      switch_timer_t read_timer;
-+      switch_timer_t write_timer;
-       switch_timer_t hold_timer;
-       int dual_streams;
-       time_t deactivate_timer;
-@@ -249,7 +250,7 @@
-               }
-               while (switch_channel_get_state(channel) == CS_INIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
--                      switch_size_t olen = globals.timer.samples;
-+                      switch_size_t olen = globals.read_timer.samples;
-                       if (switch_micro_time_now() - last >= waitsec) {
-                               char buf[512];
-@@ -268,7 +269,7 @@
-                       }
-                       if (ring_file) {
--                              if (switch_core_timer_next(&globals.timer) != SWITCH_STATUS_SUCCESS) {
-+                              if (switch_core_timer_next(&globals.read_timer) != SWITCH_STATUS_SUCCESS) {
-                                       switch_core_file_close(&fh);
-                                       break;
-                               }
-@@ -279,7 +280,7 @@
-                               }
-                               if (globals.ring_stream) {
--                                      WriteAudioStream(globals.ring_stream, abuf, (long) olen, &globals.timer);
-+                                      WriteAudioStream(globals.ring_stream, abuf, (long) olen, &globals.write_timer);
-                               }
-                       } else {
-                               switch_yield(10000);
-@@ -355,10 +356,14 @@
-               switch_core_codec_destroy(&globals.write_codec);
-       }
--      if (globals.timer.timer_interface) {
--              switch_core_timer_destroy(&globals.timer);
-+      if (globals.read_timer.timer_interface) {
-+              switch_core_timer_destroy(&globals.read_timer);
-       }
-+      if (globals.write_timer.timer_interface) {
-+              switch_core_timer_destroy(&globals.write_timer);
-+      }
-+
-       if (globals.hold_timer.timer_interface) {
-               switch_core_timer_destroy(&globals.hold_timer);
-       }
-@@ -597,14 +602,13 @@
-       
-       switch_mutex_lock(globals.device_lock);
-       samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
--                                                        globals.read_codec.implementation->samples_per_packet, &globals.timer);
-+                                                        globals.read_codec.implementation->samples_per_packet, &globals.read_timer);
-       switch_mutex_unlock(globals.device_lock);
-       
-       if (samples) {
-               globals.read_frame.datalen = samples * 2;
-               globals.read_frame.samples = samples;
--              //switch_core_timer_check(&globals.timer, SWITCH_TRUE);
-               *frame = &globals.read_frame;
-               if (!switch_test_flag((&globals), GFLAG_MOUTH)) {
-@@ -649,7 +653,7 @@
-       if (globals.audio_stream) {
-               if (switch_test_flag((&globals), GFLAG_EAR)) {
--                      WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)), &globals.timer);
-+                      WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)), &globals.write_timer);
-               }
-               status = SWITCH_STATUS_SUCCESS;
-       }
-@@ -1278,10 +1282,14 @@
-               destroy_codecs();
-       }
--      if (globals.timer.timer_interface) {
--              switch_core_timer_sync(&globals.timer);
-+      if (globals.read_timer.timer_interface) {
-+              switch_core_timer_sync(&globals.read_timer);
-       }
-+      if (globals.write_timer.timer_interface) {
-+              switch_core_timer_sync(&globals.write_timer);
-+      }
-+
-       if (globals.audio_stream) {
-               return SWITCH_STATUS_SUCCESS;
-       }
-@@ -1310,8 +1318,8 @@
-               }
-       }
--      if (!globals.timer.timer_interface) {
--              if (switch_core_timer_init(&globals.timer,
-+      if (!globals.read_timer.timer_interface) {
-+              if (switch_core_timer_init(&globals.read_timer,
-                                                                  globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
-                                                                  module_pool) != SWITCH_STATUS_SUCCESS) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
-@@ -1321,6 +1329,19 @@
-               }
-       }
-+
-+      if (!globals.write_timer.timer_interface) {
-+              if (switch_core_timer_init(&globals.write_timer,
-+                                                                 globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
-+                                                                 module_pool) != SWITCH_STATUS_SUCCESS) {
-+                      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
-+                      switch_core_codec_destroy(&globals.read_codec);
-+                      switch_core_codec_destroy(&globals.write_codec);
-+                      switch_core_timer_destroy(&globals.read_timer);
-+                      return SWITCH_STATUS_FALSE;
-+              }
-+      }
-+
-       if (!globals.hold_timer.timer_interface) {
-               if (switch_core_timer_init(&globals.hold_timer,
-                                                                  globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
-@@ -1328,7 +1349,8 @@
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup hold timer failed!\n");
-                       switch_core_codec_destroy(&globals.read_codec);
-                       switch_core_codec_destroy(&globals.write_codec);
--                      switch_core_timer_destroy(&globals.timer);
-+                      switch_core_timer_destroy(&globals.read_timer);
-+                      switch_core_timer_destroy(&globals.write_timer);
-                       return SWITCH_STATUS_FALSE;
-               }
-       }
-@@ -1366,7 +1388,8 @@
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't open audio device\n");
-               switch_core_codec_destroy(&globals.read_codec);
-               switch_core_codec_destroy(&globals.write_codec);
--              switch_core_timer_destroy(&globals.timer);
-+              switch_core_timer_destroy(&globals.read_timer);
-+              switch_core_timer_destroy(&globals.write_timer);
-               switch_core_timer_destroy(&globals.hold_timer);
-               return SWITCH_STATUS_FALSE;
-       }
-@@ -1963,8 +1986,8 @@
-                                                                          playfile, seconds, samples, globals.read_codec.implementation->actual_samples_per_second);
-                               
-                               while (switch_core_file_read(&fh, abuf, &olen) == SWITCH_STATUS_SUCCESS) {
--                                      WriteAudioStream(globals.audio_stream, abuf, (long) olen, &globals.timer);
--                                      switch_core_timer_next(&globals.timer);
-+                                      WriteAudioStream(globals.audio_stream, abuf, (long) olen, &globals.read_timer);
-+                                      switch_core_timer_next(&globals.read_timer);
-                                       samples -= (int) olen;
-                                       if (samples <= 0) {
-                                               break;
-@@ -1993,8 +2016,8 @@
-                       int i;
-                       for(i = 0; i < 400; i++) {
-                               if ((samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
--                                              globals.read_codec.implementation->samples_per_packet, &globals.timer))) {
--                                      WriteAudioStream(globals.audio_stream, globals.read_frame.data, (long) samples, &globals.timer);
-+                                              globals.read_codec.implementation->samples_per_packet, &globals.read_timer))) {
-+                                      WriteAudioStream(globals.audio_stream, globals.read_frame.data, (long) samples, &globals.write_timer);
-                                       success = 1;
-                               }
-                               switch_yield(10000);
-Index: src/mod/endpoints/mod_portaudio/pablio.c
-===================================================================
---- src/mod/endpoints/mod_portaudio/pablio.c   (revision 12655)
-+++ src/mod/endpoints/mod_portaudio/pablio.c   (working copy)
-@@ -146,17 +146,15 @@
-       char *p = (char *) data;
-       long numBytes = aStream->bytesPerFrame * numFrames;
--      while (numBytes > 0) {
--              bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
--              numBytes -= bytesWritten;
--              p += bytesWritten;
--              if (numBytes > 0) {
--                      if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
--                              PaUtil_FlushRingBuffer(&aStream->outFIFO);
--                              return 0;
--                      }
--                      switch_cond_next();
--              }
-+      switch_core_timer_next(timer);
-+
-+      bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
-+      numBytes -= bytesWritten;
-+      p += bytesWritten;
-+      
-+      if (numBytes > 0) {
-+              PaUtil_FlushRingBuffer(&aStream->outFIFO);
-+              return 0;
-       }
-       return numFrames;
- }
-@@ -170,31 +168,32 @@
-       long bytesRead = 0;
-       char *p = (char *) data;
-       long avail, totalBytes = 0, neededBytes = aStream->bytesPerFrame * numFrames;
-+      int max = 5000;
--      for (;;) {
--              avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO);
--              
--              if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
--                      break;
--              }
-+      switch_core_timer_next(timer);
-+      while(totalBytes < neededBytes && --max > 0) {
-+
-+              avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO);
-+              //printf("AVAILABLE BYTES %ld pass %d\n", avail, 5000 - max);
-               if (avail >= neededBytes * 6) {
-                       PaUtil_FlushRingBuffer(&aStream->inFIFO);
-                       avail = 0;
--              }
-+              } else {
--              bytesRead = 0;
-+                      bytesRead = 0;
-+                      
-+                      if (totalBytes < neededBytes && avail >= neededBytes) {
-+                              bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes);
-+                              totalBytes += bytesRead;
-+                      }
--              if (totalBytes < neededBytes && avail >= neededBytes) {
--                      bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes);
--                      totalBytes += bytesRead;
-+                      if (bytesRead) {
-+                              p += bytesRead;
-+                      } else {
-+                              switch_cond_next();
-+                      }
-               }
--
--              if (bytesRead) {
--                      p += bytesRead;
--              } else {
--                      switch_cond_next();
--              }
-       }
-       return totalBytes / aStream->bytesPerFrame;
diff --git a/patches/sofia.diff b/patches/sofia.diff
new file mode 100644 (file)
index 0000000..82e39d1
--- /dev/null
@@ -0,0 +1,383 @@
+Index: src/mod/endpoints/mod_sofia/sofia_reg.c
+===================================================================
+--- src/mod/endpoints/mod_sofia/sofia_reg.c    (revision 12705)
++++ src/mod/endpoints/mod_sofia/sofia_reg.c    (working copy)
+@@ -37,26 +37,56 @@
+  */
+ #include "mod_sofia.h"
+-static void sofia_reg_kill_reg(sofia_gateway_t *gateway_ptr, int unreg)
++static void sofia_reg_new_handle(sofia_gateway_t *gateway_ptr)
+ {
++      int ss_state = nua_callstate_authenticating;
++
+       if (gateway_ptr->nh) {
+-              if (unreg) {
+-                      nua_unregister(gateway_ptr->nh,
+-                                                 NUTAG_URL(gateway_ptr->register_url),
+-                                                 SIPTAG_FROM_STR(gateway_ptr->register_from),
+-                                                 SIPTAG_TO_STR(gateway_ptr->register_from),
+-                                                 SIPTAG_CONTACT_STR(gateway_ptr->register_contact),
+-                                                 SIPTAG_EXPIRES_STR(gateway_ptr->expires_str),
+-                                                 NUTAG_REGISTRAR(gateway_ptr->register_proxy),
+-                                                 NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
+-              }
+               nua_handle_bind(gateway_ptr->nh, NULL);
+               nua_handle_destroy(gateway_ptr->nh);
+               gateway_ptr->nh = NULL;
++              sofia_private_free(gateway_ptr->sofia_private);
+       }
++      gateway_ptr->nh = nua_handle(gateway_ptr->profile->nua, NULL,
++                                                               SIPTAG_CALL_ID_STR(gateway_ptr->uuid_str),
++                                                               NUTAG_URL(gateway_ptr->register_proxy),
++                                                               SIPTAG_TO_STR(gateway_ptr->register_to),
++                                                               NUTAG_CALLSTATE_REF(ss_state), SIPTAG_FROM_STR(gateway_ptr->register_from), TAG_END());
++
++
++      if (!gateway_ptr->sofia_private) {
++              gateway_ptr->sofia_private = malloc(sizeof(*gateway_ptr->sofia_private));
++              switch_assert(gateway_ptr->sofia_private);
++      }
++      memset(gateway_ptr->sofia_private, 0, sizeof(*gateway_ptr->sofia_private));
++      
++      gateway_ptr->sofia_private->gateway = gateway_ptr;
++      nua_handle_bind(gateway_ptr->nh, gateway_ptr->sofia_private);
+ }
++static void sofia_reg_kill_reg(sofia_gateway_t *gateway_ptr)
++{
++
++      if (!gateway_ptr->nh) {
++              sofia_reg_new_handle(gateway_ptr);
++
++      }
++
++      if (gateway_ptr->nh) {
++              switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "UNRegistering %s\n", gateway_ptr->name);
++              nua_unregister(gateway_ptr->nh,
++                                         NUTAG_URL(gateway_ptr->register_url),
++                                         SIPTAG_FROM_STR(gateway_ptr->register_from),
++                                         SIPTAG_TO_STR(gateway_ptr->register_from),
++                                         SIPTAG_CONTACT_STR(gateway_ptr->register_contact),
++                                         SIPTAG_EXPIRES_STR(gateway_ptr->expires_str),
++                                         NUTAG_REGISTRAR(gateway_ptr->register_proxy),
++                                         NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
++      }
++      
++}
++
+ static void sofia_reg_fire_custom_gateway_state_event(sofia_gateway_t *gateway) {
+       switch_event_t *s_event;
+       if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_GATEWAY_STATE) == SWITCH_STATUS_SUCCESS) {
+@@ -71,10 +101,9 @@
+       sofia_gateway_t *gateway_ptr;
+       for (gateway_ptr = profile->gateways; gateway_ptr; gateway_ptr = gateway_ptr->next) {
+               if (gateway_ptr->sofia_private) {
+-                      free(gateway_ptr->sofia_private);
+-                      gateway_ptr->sofia_private = NULL;
++                      sofia_private_free(gateway_ptr->sofia_private);
+               }
+-              sofia_reg_kill_reg(gateway_ptr, 1);
++              sofia_reg_kill_reg(gateway_ptr);
+       }
+ }
+@@ -108,7 +137,7 @@
+                               break;
+                       case SUB_STATE_UNSUBSCRIBE:
+                               gw_sub_ptr->state = SUB_STATE_NOSUB;
+-                              
++
+                               /* not tested .. */
+                               nua_unsubscribe(gateway_ptr->nh,
+                                               NUTAG_URL(gateway_ptr->register_url),
+@@ -121,24 +150,23 @@
+                               
+                               break;
+                       case SUB_STATE_UNSUBED:
+-                              if ((gateway_ptr->nh = nua_handle(gateway_ptr->profile->nua, NULL,
+-                                                                                                NUTAG_URL(gateway_ptr->register_proxy),
+-                                                                                                SIPTAG_TO_STR(gateway_ptr->register_to),
+-                                                                                                NUTAG_CALLSTATE_REF(ss_state), 
+-                                                                                                SIPTAG_FROM_STR(gateway_ptr->register_from), TAG_END()))) {
+-                                      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "subscribing to [%s] on gateway [%s]\n", gw_sub_ptr->event, gateway_ptr->name);
+-                              }
++                              gateway_ptr->sub_nh = nua_handle(gateway_ptr->profile->nua, NULL,
++                                                                                               NUTAG_URL(gateway_ptr->register_proxy),
++                                                                                               SIPTAG_TO_STR(gateway_ptr->register_to),
++                                                                                               NUTAG_CALLSTATE_REF(ss_state), 
++                                                                                               SIPTAG_FROM_STR(gateway_ptr->register_from), TAG_END());
++                              switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "subscribing to [%s] on gateway [%s]\n", gw_sub_ptr->event, gateway_ptr->name);
+                               
+                               gateway_ptr->sofia_private = malloc(sizeof(*gateway_ptr->sofia_private));
+                               switch_assert(gateway_ptr->sofia_private);
+-
++                              
+                               memset(gateway_ptr->sofia_private, 0, sizeof(*gateway_ptr->sofia_private));
+                               gateway_ptr->sofia_private->gateway = gateway_ptr;
+                               nua_handle_bind(gateway_ptr->nh, gateway_ptr->sofia_private);
+                               if (now) {
+-                                      nua_subscribe(gateway_ptr->nh,
++                                      nua_subscribe(gateway_ptr->sub_nh,
+                                                       NUTAG_URL(gateway_ptr->register_url),
+                                                       SIPTAG_EVENT_STR(gw_sub_ptr->event),
+                                                       SIPTAG_ACCEPT_STR(gw_sub_ptr->content_type),  
+@@ -149,7 +177,7 @@
+                                                       TAG_NULL());
+                                       gw_sub_ptr->retry = now + gw_sub_ptr->retry_seconds;
+                               } else {
+-                                      nua_unsubscribe(gateway_ptr->nh,
++                                      nua_unsubscribe(gateway_ptr->sub_nh,
+                                                       NUTAG_URL(gateway_ptr->register_url),
+                                                       SIPTAG_EVENT_STR(gw_sub_ptr->event),
+                                                       SIPTAG_ACCEPT_STR(gw_sub_ptr->content_type),
+@@ -207,7 +235,6 @@
+       }
+       for (gateway_ptr = profile->gateways; gateway_ptr; gateway_ptr = gateway_ptr->next) {
+-              int ss_state = nua_callstate_authenticating;
+               reg_state_t ostate = gateway_ptr->state;
+               if (!now) {
+@@ -223,6 +250,7 @@
+                       switch_assert(pvt);
+                       memset(pvt, 0, sizeof(*pvt));
+                       pvt->destroy_nh = 1;
++                      pvt->destroy_me = 1;
+                       switch_copy_string(pvt->gateway_name, gateway_ptr->name, sizeof(pvt->gateway_name));
+                       nua_handle_bind(nh, pvt);
+@@ -254,64 +282,46 @@
+                       break;
+               case REG_STATE_UNREGISTER:
+-                      sofia_reg_kill_reg(gateway_ptr, 1);
++                      sofia_reg_kill_reg(gateway_ptr);
+                       gateway_ptr->state = REG_STATE_NOREG;
+                       break;
+               case REG_STATE_UNREGED:
+                       gateway_ptr->status = SOFIA_GATEWAY_DOWN;
+-                      sofia_reg_kill_reg(gateway_ptr, 0);
++
++                      sofia_reg_new_handle(gateway_ptr);
+                       
+-                      if ((gateway_ptr->nh = nua_handle(gateway_ptr->profile->nua, NULL,
+-                                                                                        SIPTAG_CALL_ID_STR(gateway_ptr->uuid_str),
+-                                                                                        NUTAG_URL(gateway_ptr->register_proxy),
+-                                                                                        SIPTAG_TO_STR(gateway_ptr->register_to),
+-                                                                                        NUTAG_CALLSTATE_REF(ss_state), SIPTAG_FROM_STR(gateway_ptr->register_from), TAG_END()))) {
+-                              switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Registering %s\n", gateway_ptr->name);
+-
+-                              if (!gateway_ptr->sofia_private) {
+-                                      gateway_ptr->sofia_private = malloc(sizeof(*gateway_ptr->sofia_private));
+-                                      switch_assert(gateway_ptr->sofia_private);
+-                              }
+-                              memset(gateway_ptr->sofia_private, 0, sizeof(*gateway_ptr->sofia_private));
+-
+-                              gateway_ptr->sofia_private->gateway = gateway_ptr;
+-                              nua_handle_bind(gateway_ptr->nh, gateway_ptr->sofia_private);
+-
+-                              if (now) {
+-                                      nua_register(gateway_ptr->nh,
+-                                                               NUTAG_URL(gateway_ptr->register_url),
+-                                                               TAG_IF(gateway_ptr->register_sticky_proxy, NUTAG_PROXY(gateway_ptr->register_sticky_proxy)),
+-                                                               SIPTAG_TO_STR(gateway_ptr->register_from),
+-                                                               SIPTAG_FROM_STR(gateway_ptr->register_from),
+-                                                               SIPTAG_CONTACT_STR(gateway_ptr->register_contact),
+-                                                               SIPTAG_EXPIRES_STR(gateway_ptr->expires_str),
+-                                                               NUTAG_REGISTRAR(gateway_ptr->register_proxy),
+-                                                               NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
+-                                      gateway_ptr->retry = now + gateway_ptr->retry_seconds;
+-                              } else {
+-                                      nua_unregister(gateway_ptr->nh,
+-                                                                 NUTAG_URL(gateway_ptr->register_url),
+-                                                                 SIPTAG_FROM_STR(gateway_ptr->register_from),
+-                                                                 SIPTAG_TO_STR(gateway_ptr->register_from),
+-                                                                 SIPTAG_CONTACT_STR(gateway_ptr->register_contact),
+-                                                                 SIPTAG_EXPIRES_STR(gateway_ptr->expires_str),
+-                                                                 NUTAG_REGISTRAR(gateway_ptr->register_proxy),
+-                                                                 NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
+-                              }
++                      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Registering %s\n", gateway_ptr->name);
++                      
++                      if (now) {
++                              nua_register(gateway_ptr->nh,
++                                                       NUTAG_URL(gateway_ptr->register_url),
++                                                       TAG_IF(gateway_ptr->register_sticky_proxy, NUTAG_PROXY(gateway_ptr->register_sticky_proxy)),
++                                                       SIPTAG_TO_STR(gateway_ptr->register_from),
++                                                       SIPTAG_FROM_STR(gateway_ptr->register_from),
++                                                       SIPTAG_CONTACT_STR(gateway_ptr->register_contact),
++                                                       SIPTAG_EXPIRES_STR(gateway_ptr->expires_str),
++                                                       NUTAG_REGISTRAR(gateway_ptr->register_proxy),
++                                                       NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
+                               gateway_ptr->retry = now + gateway_ptr->retry_seconds;
+-                              gateway_ptr->state = REG_STATE_TRYING;
+-
+                       } else {
+-                              switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error registering %s failure #%d\n", gateway_ptr->name, ++gateway_ptr->failures);
+-                              gateway_ptr->state = REG_STATE_FAILED;
++                              nua_unregister(gateway_ptr->nh,
++                                                         NUTAG_URL(gateway_ptr->register_url),
++                                                         SIPTAG_FROM_STR(gateway_ptr->register_from),
++                                                         SIPTAG_TO_STR(gateway_ptr->register_from),
++                                                         SIPTAG_CONTACT_STR(gateway_ptr->register_contact),
++                                                         SIPTAG_EXPIRES_STR(gateway_ptr->expires_str),
++                                                         NUTAG_REGISTRAR(gateway_ptr->register_proxy),
++                                                         NUTAG_OUTBOUND("no-options-keepalive"), NUTAG_OUTBOUND("no-validate"), NUTAG_KEEPALIVE(0), TAG_NULL());
+                       }
++                      gateway_ptr->retry = now + gateway_ptr->retry_seconds;
++                      gateway_ptr->state = REG_STATE_TRYING;
++                      
+                       break;
+               case REG_STATE_FAILED:
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s Failed Registration, setting retry to %d seconds.\n", 
+                                                                 gateway_ptr->name, gateway_ptr->retry_seconds * (gateway_ptr->failures + 1));
+                       gateway_ptr->retry =  now + (gateway_ptr->retry_seconds * (gateway_ptr->failures + 1));
+-                      sofia_reg_kill_reg(gateway_ptr, 0);
+                       gateway_ptr->status = SOFIA_GATEWAY_DOWN;
+                       gateway_ptr->state = REG_STATE_FAIL_WAIT;
+                       break;
+@@ -408,6 +418,7 @@
+                                               SIPTAG_CONTACT_STR(profile->url), 
+                                               TAG_END());
++              nua_handle_bind(nh, &mod_sofia_globals.destroy_private);
+               nua_notify(nh,
+                                  NUTAG_NEWSUB(1),
+                                  SIPTAG_EVENT_STR(event), 
+@@ -1275,6 +1286,23 @@
+               if (ostate != sofia_private->gateway->state) {
+                       sofia_reg_fire_custom_gateway_state_event(sofia_private->gateway);
+               }
++
++
++              if (status >= 200) {
++                      if (sofia_private) {
++                              if (sofia_private->gateway) {
++                                      nua_handle_destroy(sofia_private->gateway->nh);
++                                      sofia_private->gateway->nh = NULL;
++                                      nua_handle_bind(sofia_private->gateway->nh, NULL);
++                                      sofia_private->gateway->sofia_private = NULL;
++                              } else {
++                                      nua_handle_destroy(nh);
++                              }
++                              sofia_private_free(sofia_private);
++                      } else {
++                              nua_handle_destroy(nh);
++                      }
++              }
+       }
+ }
+Index: src/mod/endpoints/mod_sofia/mod_sofia.c
+===================================================================
+--- src/mod/endpoints/mod_sofia/mod_sofia.c    (revision 12705)
++++ src/mod/endpoints/mod_sofia/mod_sofia.c    (working copy)
+@@ -2703,6 +2703,8 @@
+                                       SIPTAG_CONTACT_STR(profile->url), 
+                                       TAG_END());
+                               
++      nua_handle_bind(nh, &mod_sofia_globals.destroy_private);
++
+       nua_notify(nh,
+                          NUTAG_NEWSUB(1),
+                          SIPTAG_EVENT_STR(es), 
+@@ -2902,6 +2904,8 @@
+       memset(&mod_sofia_globals, 0, sizeof(mod_sofia_globals));
+       mod_sofia_globals.destroy_private.destroy_nh = 1;
++      mod_sofia_globals.destroy_private.is_static = 1;
++      mod_sofia_globals.keep_private.is_static = 1;
+       mod_sofia_globals.pool = pool;
+       switch_mutex_init(&mod_sofia_globals.mutex, SWITCH_MUTEX_NESTED, mod_sofia_globals.pool);
+Index: src/mod/endpoints/mod_sofia/sofia.c
+===================================================================
+--- src/mod/endpoints/mod_sofia/sofia.c        (revision 12705)
++++ src/mod/endpoints/mod_sofia/sofia.c        (working copy)
+@@ -111,6 +111,7 @@
+                       sofia_sla_handle_sip_i_notify(nua, profile, nh, sip, tags);
+                       if (sub_state == nua_substate_terminated) {
++                              sofia_private_free(sofia_private);
+                               nua_handle_bind(nh, NULL);
+                               nua_handle_destroy(nh);
+                       }
+@@ -544,8 +545,7 @@
+                       nua_handle_bind(nh, NULL);
+               }
+               sofia_private->destroy_me = 12;
+-              free(sofia_private);
+-              sofia_private = NULL;
++              sofia_private_free(sofia_private);
+       }
+       if (gateway) {
+@@ -4721,7 +4721,7 @@
+       }
+       nua_handle_bind(nh, NULL);
+-      free(sofia_private);
++      sofia_private_free(sofia_private);
+       switch_core_session_destroy(&session);
+       nua_respond(nh, 503, "Maximum Calls In Progress", SIPTAG_RETRY_AFTER_STR("300"), TAG_END());
+ }
+Index: src/mod/endpoints/mod_sofia/mod_sofia.h
+===================================================================
+--- src/mod/endpoints/mod_sofia/mod_sofia.h    (revision 12705)
++++ src/mod/endpoints/mod_sofia/mod_sofia.h    (working copy)
+@@ -122,10 +122,12 @@
+       int destroy_nh;
+       int destroy_me;
+       int is_call;
++      int is_static;
+ };
+ #define set_param(ptr,val) if (ptr) {free(ptr) ; ptr = NULL;} if (val) {ptr = strdup(val);}
+ #define set_anchor(t,m) if (t->Anchor) {delete t->Anchor;} t->Anchor = new SipMessage(m);
++#define sofia_private_free(_pvt) if (_pvt && ! _pvt->is_static) {free(_pvt); _pvt = NULL;}
+ /* Local Structures */
+ /*************************************************************************************************************************************************************/
+@@ -321,6 +323,7 @@
+ struct sofia_gateway {
+       sofia_private_t *sofia_private;
+       nua_handle_t *nh;
++      nua_handle_t *sub_nh;
+       sofia_profile_t *profile;
+       char *name;
+       char *register_scheme;
+Index: src/mod/endpoints/mod_sofia/sofia_presence.c
+===================================================================
+--- src/mod/endpoints/mod_sofia/sofia_presence.c       (revision 12705)
++++ src/mod/endpoints/mod_sofia/sofia_presence.c       (working copy)
+@@ -177,7 +177,7 @@
+                                               SIPTAG_FROM_STR(from), NUTAG_URL(contact),
+                                               SIPTAG_TO_STR(clean_to), SIPTAG_CONTACT_STR(profile->url),
+                                               TAG_END());
+-
++      nua_handle_bind(msg_nh, &mod_sofia_globals.destroy_private);
+       nua_message(msg_nh, SIPTAG_CONTENT_TYPE_STR(ct), SIPTAG_PAYLOAD_STR(body), TAG_END());
+  end:
+@@ -1860,6 +1860,16 @@
+       default:
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "status (%d) != 200, updated state to SUB_STATE_FAILED.\n", status);
+               gw_sub_ptr->state = SUB_STATE_FAILED;
++              
++              if (sofia_private) {
++                      nua_handle_destroy(sofia_private->gateway->sub_nh);
++                      sofia_private->gateway->sub_nh = NULL;
++                      nua_handle_bind(sofia_private->gateway->sub_nh, NULL);
++                      sofia_private_free(sofia_private);
++              } else {
++                      nua_handle_destroy(nh);
++              }
++
+               break;
+       }
+ }