From 743002cfb878af29e59bbdfaa4757f72b85e0ab2 Mon Sep 17 00:00:00 2001
From: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
Date: Tue, 15 Apr 2008 17:35:55 -0400
Subject: [PATCH] Registration Error Handling: add host unreachable

But introduces a bug: sometimes seg fault
---
 sflphone-gtk/src/accountlist.c |  3 +++
 sflphone-gtk/src/accountlist.h |  3 ++-
 sflphone-gtk/src/actions.c     |  4 ++++
 src/managerimpl.cpp            | 24 ++++++++++--------------
 src/sipvoiplink.cpp            |  9 +++++----
 src/voiplink.cpp               |  3 +++
 src/voiplink.h                 |  2 +-
 7 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/sflphone-gtk/src/accountlist.c b/sflphone-gtk/src/accountlist.c
index be319e06e7..447b10486f 100644
--- a/sflphone-gtk/src/accountlist.c
+++ b/sflphone-gtk/src/accountlist.c
@@ -170,6 +170,9 @@ const gchar * account_state_name(account_state_t s)
   case ACCOUNT_STATE_ERROR_NETWORK:
     state = _("Network unreachable");
     break;
+  case ACCOUNT_STATE_ERROR_HOST:
+    state = _("Host unreachable");
+    break;
   default:
     state = _("Invalid");
     break;
diff --git a/sflphone-gtk/src/accountlist.h b/sflphone-gtk/src/accountlist.h
index 5bbdbadf7a..b4f13aeb92 100644
--- a/sflphone-gtk/src/accountlist.h
+++ b/sflphone-gtk/src/accountlist.h
@@ -37,7 +37,8 @@ typedef enum
    ACCOUNT_STATE_TRYING, 
    ACCOUNT_STATE_ERROR,
    ACCOUNT_STATE_ERROR_AUTH,
-   ACCOUNT_STATE_ERROR_NETWORK
+   ACCOUNT_STATE_ERROR_NETWORK,
+   ACCOUNT_STATE_ERROR_HOST
 } account_state_t;
 
 /** @struct account_t
diff --git a/sflphone-gtk/src/actions.c b/sflphone-gtk/src/actions.c
index 543d092f13..9aa08e127a 100644
--- a/sflphone-gtk/src/actions.c
+++ b/sflphone-gtk/src/actions.c
@@ -171,6 +171,10 @@ sflphone_fill_account_list()
 		{
 		  a->state = ACCOUNT_STATE_ERROR_NETWORK;
 		}
+		else if(strcmp( status , "ERROR_HOST") == 0 )
+		{
+		  a->state = ACCOUNT_STATE_ERROR_HOST;
+		}
 		else
 		{
 			a->state = ACCOUNT_STATE_INVALID;
diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 808e358860..725e38b3b8 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -1970,7 +1970,8 @@ ManagerImpl::getAccountDetails(const AccountID& accountID)
 	(state == VoIPLink::Trying ? "TRYING":
 	(state == VoIPLink::ErrorAuth ? "ERROR_AUTH": 
 	(state == VoIPLink::ErrorNetwork ? "ERROR_NETWORK": 
-	(state == VoIPLink::Error ? "ERROR": "ERROR"))))))
+	(state == VoIPLink::ErrorHost ? "ERROR_HOST": 
+	(state == VoIPLink::Error ? "ERROR": "ERROR")))))))
 	)
       );
   a.insert(
@@ -2098,21 +2099,15 @@ ManagerImpl::setAccountDetails( const ::DBus::String& accountID,
   }
 
   saveConfig();
+  
   Account* acc = getAccount(accountID);
   acc->loadConfig();
-  if (acc->isEnabled()) {
-    // Verify we aren't already registered, then register
-    if (acc->getRegistrationState() != VoIPLink::Registered) {
-      _debug("SET ACCOUNTS DETAILS - non registered - > registered\n");
-      acc->registerVoIPLink();
-    }
-  } else {
-    // Verify we are already registered, then unregister
-    //if (acc->getRegistrationState() == VoIPLink::Registered) {
-      _debug("SET ACCOUNTS DETAILS - registered - > non registered\n");
-      acc->unregisterVoIPLink();
-    //}
-  }    
+  if (acc->isEnabled()){ 
+    acc->unregisterVoIPLink();
+    acc->registerVoIPLink();}
+  else 
+    acc->unregisterVoIPLink();
+
   // Update account details
   if (_dbus) _dbus->getConfigurationManager()->accountsChanged();
 }
@@ -2129,6 +2124,7 @@ ManagerImpl::sendRegister( const ::DBus::String& accountID , bool expire )
   // Update the active field
   setConfig( accountID, CONFIG_ACCOUNT_ENABLE, expire );
   
+ 
   Account* acc = getAccount(accountID);
   acc->loadConfig();
   // Test on the value freshly updated
diff --git a/src/sipvoiplink.cpp b/src/sipvoiplink.cpp
index b33c4042df..3709044e58 100644
--- a/src/sipvoiplink.cpp
+++ b/src/sipvoiplink.cpp
@@ -87,7 +87,6 @@ SIPVoIPLink::init()
     // Pour éviter qu'on refasse l'init sans avoir considéré l'erreur,
     // s'il y en a une ?
     _initDone = true;
-
     // check networking capabilities
     if ( !checkNetwork() ) {
       _debug("! SIP FAILURE: Unable to determine network capabilities\n");
@@ -155,7 +154,6 @@ void
 SIPVoIPLink::terminate()
 {
   terminateSIPCall(); 
-  if (_initDone) {
     // TODO The next line makes the daemon crash on 
     // account delete if at least one account is registered.
     // It should called only when the last account 
@@ -242,7 +240,6 @@ SIPVoIPLink::getEvent()
 		if(_eXosipRegID == EXOSIP_ERROR_STD){
 		  _debug("Successfully Unregister account ID = %s\n" , getAccountID().c_str());
 		  setRegistrationState(Unregistered);
-		  //if( _evThread ) _evThread->stop();
 		}
 		else{
 		  _debug("Successfully Register account ID = %s\n" , getAccountID().c_str());
@@ -1463,7 +1460,11 @@ SIPVoIPLink::SIPCallServerFailure(eXosip_event_t *event)
 void
 SIPVoIPLink::SIPRegistrationFailure( eXosip_event_t* event )
 {
-  if(!event->response)	{return ;}
+  if(!event->response){
+    setRegistrationState(ErrorHost);
+    return ;
+  }
+
   switch(  event->response->status_code ) {
     case SIP_FORBIDDEN:
       _debug("SIP forbidden\n");
diff --git a/src/voiplink.cpp b/src/voiplink.cpp
index a6cbabe52a..84a44a9cca 100644
--- a/src/voiplink.cpp
+++ b/src/voiplink.cpp
@@ -112,6 +112,9 @@ VoIPLink::setRegistrationState(const enum RegistrationState state, const int& er
   case Unregistered:
     Manager::instance().unregistrationSucceed(acc_ID);
     break;
+  case ErrorHost:
+    Manager::instance().registrationFailed(acc_ID);
+    break;
   }
 }
 
diff --git a/src/voiplink.h b/src/voiplink.h
index 0af1ddc08f..ad7ee15a7e 100644
--- a/src/voiplink.h
+++ b/src/voiplink.h
@@ -44,7 +44,7 @@ public:
   VoIPLink(const AccountID& accountID);
   virtual ~VoIPLink (void);
 
-  enum RegistrationState {Unregistered, Trying, Registered, Error, ErrorAuth , ErrorNetwork};
+  enum RegistrationState {Unregistered, Trying, Registered, Error, ErrorAuth , ErrorNetwork , ErrorHost};
 
   // Pure virtual functions
   virtual void getEvent (void) = 0;
-- 
GitLab