From dfa5296de0c385852ec848573a617fc6c2984123 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Wed, 4 May 2011 18:18:31 +0000 Subject: [PATCH] add local port number in server registering struct to allow a connection to the local host server with different port than the default one --- src/protocol.cpp | 23 ++++++++++++++++++----- src/server.cpp | 3 ++- src/serverlist.cpp | 24 ++++++++++++++---------- src/serverlist.h | 10 +++++++++- src/util.h | 13 +++++++++++-- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/protocol.cpp b/src/protocol.cpp index 192c709c..c7bdb629 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -174,9 +174,12 @@ CONNECTION LESS MESSAGES - PROTMESSID_CLM_REGISTER_SERVER: Register a server, providing server information - +-----------------+----------------------------------+ ... - | 2 bytes country | 1 byte maximum connected clients | ... - +-----------------+----------------------------------+ ... + +--------------+ ... + | 2 bytes port | ... + +--------------+ ... + ... -----------------+----------------------------------+ ... + ... 2 bytes country | 1 byte maximum connected clients | ... + ... -----------------+----------------------------------+ ... ... ---------------------+------------------+ ... ... 1 byte is permanent | 2 bytes number n | ... ... ---------------------+------------------+ ... @@ -1224,6 +1227,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, // size of current message body const int iEntrLen = + 2 /* port number */ + 2 /* country */ + 1 /* maximum number of connected clients */ + 1 /* is permanent flag */ + @@ -1234,6 +1238,10 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, // build data vector CVector vecData ( iEntrLen ); + // port number (2 bytes) + PutValOnStream ( vecData, iPos, + static_cast ( ServerInfo.iLocalPortNumber ), 2 ); + // country (2 bytes) PutValOnStream ( vecData, iPos, static_cast ( ServerInfo.eCountry ), 2 ); @@ -1267,12 +1275,16 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, const int iDataLen = vecData.Size(); CServerCoreInfo RecServerInfo; - // check size (the first 4 bytes) - if ( iDataLen < 4 ) + // check size (the first 6 bytes) + if ( iDataLen < 6 ) { return true; // return error code } + // port number (2 bytes) + RecServerInfo.iLocalPortNumber = + static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); + // country (2 bytes) RecServerInfo.eCountry = static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); @@ -1467,6 +1479,7 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr, // add server information to vector vecServerInfo.Add ( CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ), + iPort, strName, strTopic, eCountry, diff --git a/src/server.cpp b/src/server.cpp index 9a38a5df..a56807bf 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -173,7 +173,8 @@ CServer::CServer ( const QString& strLoggingFileName, const QString& strServerInfo ) : Socket ( this, iPortNumber ), bWriteStatusHTMLFile ( false ), - ServerListManager ( strCentralServer, + ServerListManager ( iPortNumber, + strCentralServer, strServerInfo, &ConnLessProtocol ) { diff --git a/src/serverlist.cpp b/src/serverlist.cpp index 2b25fe69..11723b06 100755 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -26,10 +26,12 @@ /* Implementation *************************************************************/ -CServerListManager::CServerListManager ( const QString& sNCentServAddr, +CServerListManager::CServerListManager ( const quint16 iNPortNum, + const QString& sNCentServAddr, const QString& strServerInfo, CProtocol* pNConLProt ) - : iNumPredefinedServers ( 0 ), + : iPortNumber ( iNPortNum ), + iNumPredefinedServers ( 0 ), bUseDefaultCentralServerAddress ( false ), pConnLessProtocol ( pNConLProt ) { @@ -58,6 +60,7 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr, // itself for his server list CServerListEntry ThisServerListEntry ( CHostAddress(), + iPortNumber, "", "", QLocale::system().country(), @@ -105,6 +108,7 @@ CServerListManager::CServerListManager ( const QString& sNCentServAddr, // create a new server list entry CServerListEntry NewServerListEntry ( CHostAddress(), + 0, // port number not used "", "", QLocale::AnyCountry, @@ -258,13 +262,13 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In // define invalid index used as a flag const int ciInvalidIdx = -1; - // Check if server is already registered. Use IP number to identify + // Check if server is already registered. Use address to identify // a server. The very first list entry must not be checked since // this is per definition the central server (i.e., this server) int iSelIdx = ciInvalidIdx; // initialize with an illegal value for ( int iIdx = 1; iIdx < iCurServerListSize; iIdx++ ) { - if ( ServerList[iIdx].HostAddr.InetAddr == InetAddr.InetAddr ) + if ( ServerList[iIdx].HostAddr == InetAddr ) { // store entry index iSelIdx = iIdx; @@ -283,7 +287,6 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In else { // update all data and call update registration function - ServerList[iSelIdx].HostAddr = InetAddr; // because of port number ServerList[iSelIdx].strName = ServerInfo.strName; ServerList[iSelIdx].strTopic = ServerInfo.strTopic; ServerList[iSelIdx].eCountry = ServerInfo.eCountry; @@ -344,16 +347,17 @@ void CServerListManager::CentralServerQueryServerList ( const CHostAddress& Inet { // check if the address of the client which is requesting the // list is the same address as one server in the list -> in this - // case he has to connect to the local host address, - // unfortunately, the port number is not known (because of NAT - // port translating) so we assume that it is the default llcon - // port number + // case he has to connect to the local host address if ( vecServerInfo[iIdx].HostAddr.InetAddr == InetAddr.InetAddr ) { vecServerInfo[iIdx].HostAddr.InetAddr = QHostAddress ( QHostAddress::LocalHost ); - vecServerInfo[iIdx].HostAddr.iPort = LLCON_DEFAULT_PORT_NUMBER; + // take the local port number instead of the received port + // number since some NAT (network address translation) might + // have changed the port + vecServerInfo[iIdx].HostAddr.iPort = + ServerList[iIdx].iLocalPortNumber; } else { diff --git a/src/serverlist.h b/src/serverlist.h index 25545ad2..c6e7f777 100755 --- a/src/serverlist.h +++ b/src/serverlist.h @@ -76,6 +76,7 @@ class CServerListEntry : public CServerInfo public: CServerListEntry() : CServerInfo ( CHostAddress(), + 0, "", "", QLocale::AnyCountry, @@ -84,6 +85,7 @@ public: false ) { UpdateRegistration(); } CServerListEntry ( const CHostAddress& NHAddr, + const quint16 NLocPort, const QString& NsName, const QString& NsTopic, const QLocale::Country& NeCountry, @@ -91,6 +93,7 @@ public: const int NiMaxNumClients, const bool NbPermOnline) : CServerInfo ( NHAddr, + NLocPort, NsName, NsTopic, NeCountry, @@ -101,6 +104,7 @@ public: CServerListEntry ( const CHostAddress& NHAddr, const CServerCoreInfo& NewCoreServerInfo ) : CServerInfo ( NHAddr, + NewCoreServerInfo.iLocalPortNumber, NewCoreServerInfo.strName, NewCoreServerInfo.strTopic, NewCoreServerInfo.eCountry, @@ -121,7 +125,8 @@ class CServerListManager : public QObject Q_OBJECT public: - CServerListManager ( const QString& sNCentServAddr, + CServerListManager ( const quint16 iNPortNum, + const QString& sNCentServAddr, const QString& strServerInfo, CProtocol* pNConLProt ); @@ -176,7 +181,10 @@ protected: QTimer TimerPollList; QTimer TimerRegistering; QMutex Mutex; + QList ServerList; + + quint16 iPortNumber; QString strCentralServerAddress; int iNumPredefinedServers; bool bEnabled; diff --git a/src/util.h b/src/util.h index ee402395..8a01c056 100755 --- a/src/util.h +++ b/src/util.h @@ -483,6 +483,7 @@ class CServerCoreInfo { public: CServerCoreInfo() : + iLocalPortNumber ( 0 ), strName ( "" ), strTopic ( "" ), eCountry ( QLocale::AnyCountry ), @@ -491,12 +492,14 @@ public: bPermanentOnline ( false ) {} CServerCoreInfo ( + const quint16 NLocPort, const QString& NsName, const QString& NsTopic, const QLocale::Country& NeCountry, const QString& NsCity, const int NiMaxNumClients, const bool NbPermOnline) : + iLocalPortNumber ( NLocPort ), strName ( NsName ), strTopic ( NsTopic ), eCountry ( NeCountry ), @@ -505,6 +508,9 @@ public: bPermanentOnline ( NbPermOnline ) {} public: + // local port number of the server + quint16 iLocalPortNumber; + // name of the server QString strName; @@ -529,7 +535,8 @@ class CServerInfo : public CServerCoreInfo { public: CServerInfo() : - CServerCoreInfo ( "", + CServerCoreInfo ( 0, + "", "", QLocale::AnyCountry, "", @@ -538,13 +545,15 @@ public: CServerInfo ( const CHostAddress& NHAddr, + const quint16 NLocPort, const QString& NsName, const QString& NsTopic, const QLocale::Country& NeCountry, const QString& NsCity, const int NiMaxNumClients, const bool NbPermOnline) : - CServerCoreInfo ( NsName, + CServerCoreInfo ( NLocPort, + NsName, NsTopic, NeCountry, NsCity,