From 93eeb98b3f019823e7ded0aec4717f510c010508 Mon Sep 17 00:00:00 2001
From: Yun Liu <yun@yun.(none)>
Date: Mon, 6 Oct 2008 15:06:34 -0400
Subject: [PATCH] Assign the last non-5060 port number of accounts to useragent
 for registeration

---
 sflphone-gtk/src/accountwindow.c |  1 +
 src/managerimpl.cpp              | 13 +++++++++++--
 src/sipvoiplink.cpp              |  2 +-
 src/useragent.cpp                |  2 +-
 src/useragent.h                  | 10 +++++++++-
 5 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/sflphone-gtk/src/accountwindow.c b/sflphone-gtk/src/accountwindow.c
index 4308d94c2c..660eca97a8 100644
--- a/sflphone-gtk/src/accountwindow.c
+++ b/sflphone-gtk/src/accountwindow.c
@@ -65,6 +65,7 @@ change_protocol (account_t * currentAccount)
   else if (strcmp(proto, "IAX") == 0) {
     gtk_widget_set_sensitive( GTK_WIDGET(stunEnable),   FALSE);
     gtk_widget_set_sensitive( GTK_WIDGET(stunServer),   FALSE);
+    gtk_widget_set_sensitive( GTK_WIDGET(entryPort),   FALSE);
   }
   else {
     // Should not get here.
diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 0738d4c14e..7102d2a273 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -26,7 +26,7 @@
 #include <cstdlib>
 #include <iostream>
 #include <fstream>
-
+#include <sstream>  
 #include <sys/types.h> // mkdir(2)
 #include <sys/stat.h>	// mkdir(2)
 
@@ -50,6 +50,7 @@
 
 #include "user_cfg.h"
 
+#define DEFAULT_SIP_PORT  5060
 
 #ifdef USE_ZEROCONF
 #include "zeroconf/DNSService.h"
@@ -2231,6 +2232,8 @@ ManagerImpl::loadAccountMap()
   TokenList sections = _config.getSections();
   std::string accountType;
   Account* tmpAccount;
+  std::string port;
+  unsigned int iPort;
 
   TokenList::iterator iter = sections.begin();
   while(iter != sections.end()) {
@@ -2246,6 +2249,7 @@ ManagerImpl::loadAccountMap()
         // Initialize the SIP Manager
         _userAgent = new UserAgent();
         _userAgentInitlized = true;
+        _userAgent->setRegPort(DEFAULT_SIP_PORT);
       }
 
       tmpAccount = AccountCreator::createAccount(AccountCreator::SIP_ACCOUNT, *iter);
@@ -2257,7 +2261,12 @@ ManagerImpl::loadAccountMap()
         _userAgent->setStunServer(Manager::instance().getConfigString(tmpAccount->getAccountID(), SIP_STUN_SERVER).data());
       }
       
-      
+      // Set registration port for all accounts, The last non-5060 port will be recorded in _userAgent.
+      port = Manager::instance().getConfigString(tmpAccount->getAccountID(), SIP_PORT);
+      std::istringstream is(port);
+      is >> iPort;
+      if (iPort != DEFAULT_SIP_PORT)
+      	_userAgent->setRegPort(iPort);  
     }
     else if (accountType == "IAX") {
       tmpAccount = AccountCreator::createAccount(AccountCreator::IAX_ACCOUNT, *iter);
diff --git a/src/sipvoiplink.cpp b/src/sipvoiplink.cpp
index 6334e686e1..942f8bb698 100644
--- a/src/sipvoiplink.cpp
+++ b/src/sipvoiplink.cpp
@@ -23,7 +23,7 @@
 #include "sipvoiplink.h"
 #include "eventthread.h"
 #include "sipcall.h"
-#include <sstream> // for ostringstream
+#include <sstream> // for istringstream
 #include "sipaccount.h"
 #include "useragent.h"
 #include "audio/audiortp.h"
diff --git a/src/useragent.cpp b/src/useragent.cpp
index f13a120f32..dcc3cec4f1 100644
--- a/src/useragent.cpp
+++ b/src/useragent.cpp
@@ -104,7 +104,7 @@ pj_status_t UserAgent::sipInit() {
         return false;
     }
     int errPjsip = 0;
-    int port = DEFAULT_SIP_PORT;
+    int port = _regPort;
 
     //_debug("stun host is %s\n", _stunHost.ptr);
     if (_useStun && !Manager::instance().behindNat(_stunServer, port)) {
diff --git a/src/useragent.h b/src/useragent.h
index f5cf104f81..783e86eca5 100644
--- a/src/useragent.h
+++ b/src/useragent.h
@@ -65,6 +65,9 @@ private:
     /** Local Extern Port is the port seen by peers for SIP listener */
     unsigned int _localExternPort;
     unsigned int _localPort;
+
+    /** For registration use only */
+    unsigned int _regPort;
     
     pj_thread_t *_thread;
     
@@ -89,8 +92,13 @@ public:
 
     /** Set whether it will use stun server */
     void setStunServer(const char *server); 
+
+    /** Set the port number user designated */
+    void setRegPort(unsigned int port) { _regPort = port; }
+ 
+    unsigned int getRegPort() { return _regPort; }
     
-    pj_str_t getStunServer() {return _stunHost;}
+    pj_str_t getStunServer() { return _stunHost; }
     
     bool addAccount(AccountID id, pjsip_regc **regc, const std::string& server, const std::string& user, const std::string& passwd
 						   , const int& timeout,  const unsigned int& port);
-- 
GitLab