From 81aba483c4757c0f41ffe17a3b6e412f6de2c532 Mon Sep 17 00:00:00 2001
From: yanmorin <yanmorin>
Date: Tue, 11 Oct 2005 19:08:33 +0000
Subject: [PATCH] Register only with no-stun (stun=0) Send register detail if
 we use stun Remove the subscribe event sending

Work now with STUN/NON-STUN and NAT/NO-NAT
---
 src/audio/audiortp.cpp           |  2 +-
 src/gui/guiframework.h           |  2 +
 src/gui/server/guiserverimpl.cpp | 12 ++++++
 src/gui/server/guiserverimpl.h   |  1 +
 src/managerimpl.cpp              | 30 ++++++++-----
 src/sipvoiplink.cpp              | 74 ++++++++++++++++++++------------
 src/sipvoiplink.h                |  2 +
 7 files changed, 84 insertions(+), 39 deletions(-)

diff --git a/src/audio/audiortp.cpp b/src/audio/audiortp.cpp
index 36f8b357f1..3dcd3ec5ec 100644
--- a/src/audio/audiortp.cpp
+++ b/src/audio/audiortp.cpp
@@ -280,7 +280,7 @@ AudioRtpRTX::receiveSessionForSpkr (int16* data_for_speakers,
 	
 	// Notify (with a beep) an incoming call when there is already a call 
 	countTime += time->getSecond();
-	if (Manager::instance().incomingCallWaiting()) {
+	if (Manager::instance().incomingCallWaiting() > 0) {
 		countTime = countTime % 4000;
 		if (countTime < 100 and countTime > 0) {
 			Manager::instance().notificationIncomingCall();
diff --git a/src/gui/guiframework.h b/src/gui/guiframework.h
index c4fc9f660d..d131db8f92 100644
--- a/src/gui/guiframework.h
+++ b/src/gui/guiframework.h
@@ -55,6 +55,8 @@ public:
   short id, 
   TokenList arg) = 0;
 
+  virtual void callFailure(short id) = 0;
+
 	/* Child class to parent class */
 	int outgoingCall (const std::string& to); 	
 	int hangupCall (short id);
diff --git a/src/gui/server/guiserverimpl.cpp b/src/gui/server/guiserverimpl.cpp
index 7fbad6455f..c91f049da6 100644
--- a/src/gui/server/guiserverimpl.cpp
+++ b/src/gui/server/guiserverimpl.cpp
@@ -408,3 +408,15 @@ GUIServerImpl::update()
 {
   
 }
+
+void
+GUIServerImpl::callFailure(short id)
+{
+  CallMap::iterator iter = _callMap.find(id);
+  if ( iter != _callMap.end() ) {
+    std::ostringstream responseMessage;
+    responseMessage << iter->second.callId() << " Wrong number";
+
+    _requestManager.sendResponse(ResponseMessage("504", iter->second.sequenceId(), responseMessage.str()));
+  }
+}
diff --git a/src/gui/server/guiserverimpl.h b/src/gui/server/guiserverimpl.h
index 51104d3eae..8b896a42f3 100644
--- a/src/gui/server/guiserverimpl.h
+++ b/src/gui/server/guiserverimpl.h
@@ -62,6 +62,7 @@ arg);
   const std::string& sequenceId, 
   short id, 
   TokenList arg);
+  void callFailure(short id);
 
   bool getEvents(const std::string& sequenceId);
   bool sendGetEventsEnd(const std::string& sequenceId);
diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 14436a3fa0..e3c3a07f7a 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -150,12 +150,10 @@ ManagerImpl::init (void)
     // If network is available
 
     if (getConfigInt(SIGNALISATION, AUTO_REGISTER) && _exist == 1) {
-      if (registerVoIPLink() != 1) {
-	_debug("Registration failed\n");
-      }
-    } 
+      registerVoIPLink();
+    }
   }
-  
+
 }
 
 void ManagerImpl::terminate()
@@ -353,6 +351,7 @@ ManagerImpl::outgoingCall (const string& to)
 int 
 ManagerImpl::hangupCall (short id)
 {
+  stopTone(); // stop tone, like a 700 error: number not found Not Found
 	Call* call;
 
 	call = getCall(id);
@@ -569,19 +568,26 @@ ManagerImpl::saveConfig (void)
 
 /**
  * Initialize action (main thread)
+ * Note that Registration is only send if STUN is not activated
+ * @return 1 if setRegister is call without failure, else return 0
  */
 int 
 ManagerImpl::registerVoIPLink (void)
 {
-	if (_voIPLinkVector.at(DFT_VOIP_LINK)->setRegister() == 0) {
-		return 1;
-	} else {
-		return 0;
-	}
+  int returnValue = 0;
+  if ( !useStun() ) {
+    if (_voIPLinkVector.at(DFT_VOIP_LINK)->setRegister() == 0) {
+      returnValue = true;
+    } else {
+      _debug("ManagerImpl::registerVoIPLink: Registration Failed\n");
+    }
+  }
+  return returnValue;
 }
 
 /**
  * Terminate action (main thread)
+ * @return 1 if the unregister method is send correctly
  */
 int 
 ManagerImpl::unregisterVoIPLink (void)
@@ -982,6 +988,10 @@ void
 ManagerImpl::callFailure(short id) {
   playATone(ZT_TONE_BUSY);
   getCall(id)->setState(Call::Error);
+
+  if (_gui) {
+    _gui->callFailure(id);
+  }
 }
 
 /**
diff --git a/src/sipvoiplink.cpp b/src/sipvoiplink.cpp
index f98c0e562b..386b159806 100644
--- a/src/sipvoiplink.cpp
+++ b/src/sipvoiplink.cpp
@@ -53,6 +53,8 @@ SipVoIPLink::SipVoIPLink (short id)
   _nMsgVoicemail = 0;
   _reg_id = -1;
   // defautlt _sipcallVector object initialization
+
+  _registrationSend = false;
 }
 
 SipVoIPLink::~SipVoIPLink(void) {
@@ -216,6 +218,9 @@ SipVoIPLink::setRegister (void)
     return -1;
   }	
 
+  osip_message_set_header (reg, "Event", "Registration");
+  osip_message_set_header (reg, "Allow-Events", "presence");
+
   int i = eXosip_register_send_register (_reg_id, reg);
   if (i == -2) {
     _debug("cannot build registration, check the setup\n"); 
@@ -232,44 +237,53 @@ SipVoIPLink::setRegister (void)
   manager.error()->setError(0);
 
   // subscribe to message one time?
-  subscribeMessageSummary();
+  // subscribeMessageSummary();
+
+  _registrationSend = true;
   return i;
 }
 
+/**
+ * setUnregister 
+ * unregister if we already send the first registration
+ * @return -1 if there is an error
+ */
 int 
 SipVoIPLink::setUnregister (void)
 {
-  int i = 0;
-  osip_message_t *reg = NULL;
+  if ( _registrationSend ) {
+    int i = 0;
+    osip_message_t *reg = NULL;
 
-  eXosip_lock();
+    eXosip_lock();
 
-  if (_reg_id > 0) {
-    _debug("UNREGISTER\n");
-    i = eXosip_register_build_register (_reg_id, 0, &reg);
-  }
-	
-  if (_reg_id < 0) {
-    eXosip_unlock();
-    return -1;
-  }	
-	
-  i = eXosip_register_send_register (_reg_id, reg);
-  if (i == -2) {
-    _debug("cannot build registration, check the setup\n"); 
-    eXosip_unlock();
-    return -1;
-  }
-  if (i == -1) {
-    _debug("Registration Failed\n");
+    if (_reg_id > 0) {
+      _debug("UNREGISTER\n");
+      i = eXosip_register_build_register (_reg_id, 0, &reg);
+    }
+    if (_reg_id < 0) {
+      eXosip_unlock();
+      return -1;
+    }	
+
+    i = eXosip_register_send_register (_reg_id, reg);
+    if (i == -2) {
+      _debug("(unregister) Cannot build registration, check the setup\n"); 
+      eXosip_unlock();
+      return -1;
+    }
+    if (i == -1) {
+      _debug("(unregister) Registration Failed\n");
+      eXosip_unlock();
+      return -1;
+    }
     eXosip_unlock();
+    Manager::instance().error()->setError(0);
+    return i;
+  } else {
+    // no registration send before
     return -1;
   }
-	
-  eXosip_unlock();
-
-  Manager::instance().error()->setError(0);
-  return i;
 }
 
 int
@@ -787,6 +801,7 @@ SipVoIPLink::getEvent (void)
     switch (event->response->status_code) {
     case AUTH_REQUIRED:
       _debug("EXOSIP_CALL_REQUESTFAILURE :: AUTH_REQUIRED\n");
+      setAuthentication();
       eXosip_lock();
       eXosip_automatic_action();
       eXosip_unlock();
@@ -930,12 +945,15 @@ SipVoIPLink::getEvent (void)
     }
     break;
 
-  case EXOSIP_SUBSCRIPTION_ANSWERED: 
+  case EXOSIP_SUBSCRIPTION_ANSWERED: // 38
     eXosip_lock();
     eXosip_automatic_action();
     eXosip_unlock();
     break;
 
+  case EXOSIP_SUBSCRIPTION_REQUESTFAILURE: //40
+    break;
+
   default:
     //Manager::instance().displayErrorText(event->type, "getEvent:default");
     returnValue = -1;
diff --git a/src/sipvoiplink.h b/src/sipvoiplink.h
index ee763d8883..285a5052ab 100644
--- a/src/sipvoiplink.h
+++ b/src/sipvoiplink.h
@@ -215,6 +215,8 @@ private:
 	int 			_localPort;
 	int 			_reg_id;
 	int 			_nMsgVoicemail;
+
+  bool _registrationSend; // unregistered 
 };
 
 #endif // __SIP_VOIP_LINK_H__
-- 
GitLab