diff --git a/src/protocol.cpp b/src/protocol.cpp index 04b5b65c..6a30637e 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -197,9 +197,9 @@ CONNECTION LESS MESSAGES - PROTMESSID_CLM_REGISTER_SERVER: Register a server, providing server information - +--------------+ ... - | 2 bytes port | ... - +--------------+ ... + +------------------------------+ ... + | 2 bytes server internal port | ... + +------------------------------+ ... ... -----------------+----------------------------------+ ... ... 2 bytes country | 1 byte maximum connected clients | ... ... -----------------+----------------------------------+ ... @@ -225,6 +225,9 @@ CONNECTION LESS MESSAGES is permanent online. - "server interal address" represents the IPv4 address as a dotted quad to be used by clients with the same external IP address as the server. + NOTE: In the PROTMESSID_CLM_SERVER_LIST list, this field will be empty + as only the initial IP address should be used by the client. Where + necessary, that value will contain the server internal address. - PROTMESSID_CLM_UNREGISTER_SERVER: Unregister a server @@ -1415,7 +1418,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, // size of current message body const int iEntrLen = - 2 /* port number */ + + 2 /* server internal port number */ + 2 /* country */ + 1 /* maximum number of connected clients */ + 1 /* is permanent flag */ + @@ -1428,7 +1431,7 @@ void CProtocol::CreateCLRegisterServerMes ( const CHostAddress& InetAddr, // port number (2 bytes) PutValOnStream ( vecData, iPos, - static_cast ( ServerInfo.iLocalPortNumber ), 2 ); + static_cast ( LInetAddr.iPort ), 2 ); // country (2 bytes) PutValOnStream ( vecData, iPos, @@ -1472,7 +1475,7 @@ bool CProtocol::EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, } // port number (2 bytes) - RecServerInfo.iLocalPortNumber = + LInetAddr.iPort = static_cast ( GetValFromStream ( vecData, iPos, 2 ) ); // country (2 bytes) @@ -1563,9 +1566,9 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr, for ( int i = 0; i < iNumServers; i++ ) { // convert server list strings to utf-8 - const QByteArray strUTF8Name = vecServerInfo[i].strName.toUtf8(); - const QByteArray strUTF8LHostAddr = vecServerInfo[i].LHostAddr.InetAddr.toString().toUtf8(); - const QByteArray strUTF8City = vecServerInfo[i].strCity.toUtf8(); + const QByteArray strUTF8Name = vecServerInfo[i].strName.toUtf8(); + const QByteArray strUTF8Empty = QString("").toUtf8(); + const QByteArray strUTF8City = vecServerInfo[i].strCity.toUtf8(); // size of current list entry const int iCurListEntrLen = @@ -1575,17 +1578,19 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr, 1 /* maximum number of connected clients */ + 1 /* is permanent flag */ + 2 /* name utf-8 string size */ + strUTF8Name.size() + - 2 /* server internal address utf-8 string size */ + strUTF8LHostAddr.size() + + 2 /* empty string */ + 2 /* city utf-8 string size */ + strUTF8City.size(); // make space for new data vecData.Enlarge ( iCurListEntrLen ); // IP address (4 bytes) + // note the Server List manager has put the internal details in HostAddr where required PutValOnStream ( vecData, iPos, static_cast ( vecServerInfo[i].HostAddr.InetAddr.toIPv4Address() ), 4 ); // port number (2 bytes) + // note the Server List manager has put the internal details in HostAddr where required PutValOnStream ( vecData, iPos, static_cast ( vecServerInfo[i].HostAddr.iPort ), 2 ); @@ -1604,8 +1609,8 @@ void CProtocol::CreateCLServerListMes ( const CHostAddress& InetAddr, // name PutStringUTF8OnStream ( vecData, iPos, strUTF8Name ); - // topic - PutStringUTF8OnStream ( vecData, iPos, strUTF8LHostAddr ); + // empty string + PutStringUTF8OnStream ( vecData, iPos, strUTF8Empty ); // city PutStringUTF8OnStream ( vecData, iPos, strUTF8City ); @@ -1661,23 +1666,12 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr, return true; // return error code } - // server internal address - QString strLHostAddr; + // empty + QString strEmpty; if ( GetStringFromStream ( vecData, iPos, MAX_LEN_IP_ADDRESS, - strLHostAddr ) ) - { - return true; // return error code - } - - CHostAddress LInetAddr; - if ( strLHostAddr.isEmpty() ) - { - // old central server, empty "topic", default to localhost - LInetAddr.InetAddr.setAddress ( QHostAddress::LocalHost ); - } - else if ( !LInetAddr.InetAddr.setAddress ( strLHostAddr ) ) + strEmpty ) ) { return true; // return error code } @@ -1695,8 +1689,7 @@ bool CProtocol::EvaluateCLServerListMes ( const CHostAddress& InetAddr, // add server information to vector vecServerInfo.Add ( CServerInfo ( CHostAddress ( QHostAddress ( iIpAddr ), iPort ), - LInetAddr, - iPort, + CHostAddress ( QHostAddress ( iIpAddr ), iPort ), strName, eCountry, strCity, diff --git a/src/serverlist.cpp b/src/serverlist.cpp index 5dba5d74..38c67051 100755 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -31,8 +31,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, const int iNumChannels, const bool bNCentServPingServerInList, CProtocol* pNConLProt ) - : iPortNumber ( iNPortNum ), - iNumPredefinedServers ( 0 ), + : iNumPredefinedServers ( 0 ), eCentralServerAddressType ( AT_MANUAL ), // must be AT_MANUAL for the "no GUI" case bCentServPingServerInList ( bNCentServPingServerInList ), pConnLessProtocol ( pNConLProt ) @@ -40,7 +39,7 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, // set the central server address SetCentralServerAddress ( sNCentServAddr ); - // set the server internal address + // set the server internal address, including internal port number SlaveCurLocalHostAddress = CHostAddress( NetworkUtil::GetLocalAddress().InetAddr, iNPortNum ); // prepare the server info information @@ -66,7 +65,6 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, // we have a permanent server. CServerListEntry ThisServerListEntry ( CHostAddress(), SlaveCurLocalHostAddress, - iPortNumber, "", QLocale::system().country(), "", @@ -115,7 +113,6 @@ CServerListManager::CServerListManager ( const quint16 iNPortNum, // registered via the command line are permanent servers CServerListEntry NewServerListEntry ( CHostAddress(), CHostAddress(), - 0, // port number not used "", QLocale::AnyCountry, "", @@ -322,49 +319,49 @@ void CServerListManager::CentralServerRegisterServer ( const CHostAddress& In { const int iCurServerListSize = ServerList.size(); - // check for maximum allowed number of servers in the server list - if ( iCurServerListSize < MAX_NUM_SERVERS_IN_SERVER_LIST ) + // define invalid index used as a flag + const int ciInvalidIdx = -1; + + // Check if server is already registered. + // 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++ ) { - // define invalid index used as a flag - const int ciInvalidIdx = -1; - - // Check if server is already registered. - // 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 ) { - if ( ServerList[iIdx].HostAddr == InetAddr ) - { - // store entry index - iSelIdx = iIdx; + // store entry index + iSelIdx = iIdx; - // entry found, leave for-loop - continue; - } + // entry found, leave for-loop + continue; } + } - // if server is not yet registered, we have to create a new entry - if ( iSelIdx == ciInvalidIdx ) + // if server is not yet registered, we have to create a new entry + if ( iSelIdx == ciInvalidIdx ) + { + // check for maximum allowed number of servers in the server list + if ( iCurServerListSize < MAX_NUM_SERVERS_IN_SERVER_LIST ) { // create a new server list entry and init with received data ServerList.append ( CServerListEntry ( InetAddr, LInetAddr, ServerInfo ) ); } - else + } + else + { + // do not update the information in the predefined servers + if ( iSelIdx > iNumPredefinedServers ) { - // do not update the information in the predefined servers - if ( iSelIdx > iNumPredefinedServers ) - { - // update all data and call update registration function - ServerList[iSelIdx].LHostAddr = LInetAddr; - ServerList[iSelIdx].strName = ServerInfo.strName; - ServerList[iSelIdx].eCountry = ServerInfo.eCountry; - ServerList[iSelIdx].strCity = ServerInfo.strCity; - ServerList[iSelIdx].iMaxNumClients = ServerInfo.iMaxNumClients; - ServerList[iSelIdx].bPermanentOnline = ServerInfo.bPermanentOnline; + // update all data and call update registration function + ServerList[iSelIdx].LHostAddr = LInetAddr; + ServerList[iSelIdx].strName = ServerInfo.strName; + ServerList[iSelIdx].eCountry = ServerInfo.eCountry; + ServerList[iSelIdx].strCity = ServerInfo.strCity; + ServerList[iSelIdx].iMaxNumClients = ServerInfo.iMaxNumClients; + ServerList[iSelIdx].bPermanentOnline = ServerInfo.bPermanentOnline; - ServerList[iSelIdx].UpdateRegistration(); - } + ServerList[iSelIdx].UpdateRegistration(); } } } @@ -431,11 +428,7 @@ void CServerListManager::CentralServerQueryServerList ( const CHostAddress& Inet // otherwise, use the supplied details if ( iIdx > iNumPredefinedServers ) { - vecServerInfo[iIdx].HostAddr.InetAddr = - ServerList[iIdx].LHostAddr.InetAddr; - - vecServerInfo[iIdx].HostAddr.iPort = - ServerList[iIdx].iLocalPortNumber; + vecServerInfo[iIdx].HostAddr = ServerList[iIdx].LHostAddr; } } else diff --git a/src/serverlist.h b/src/serverlist.h index 5a5e7984..434e3eba 100755 --- a/src/serverlist.h +++ b/src/serverlist.h @@ -76,7 +76,6 @@ public: CServerListEntry() : CServerInfo ( CHostAddress(), CHostAddress(), - 0, "", QLocale::AnyCountry, "", @@ -85,7 +84,6 @@ public: CServerListEntry ( const CHostAddress& NHAddr, const CHostAddress& NLHAddr, - const quint16 NLocPort, const QString& NsName, const QLocale::Country& NeCountry, const QString& NsCity, @@ -93,7 +91,6 @@ public: const bool NbPermOnline) : CServerInfo ( NHAddr, NLHAddr, - NLocPort, NsName, NeCountry, NsCity, @@ -105,7 +102,6 @@ public: const CServerCoreInfo& NewCoreServerInfo ) : CServerInfo ( NHAddr, NLHAddr, - NewCoreServerInfo.iLocalPortNumber, NewCoreServerInfo.strName, NewCoreServerInfo.eCountry, NewCoreServerInfo.strCity, diff --git a/src/testbench.h b/src/testbench.h index 9470ffce..0fb35fbd 100755 --- a/src/testbench.h +++ b/src/testbench.h @@ -45,6 +45,9 @@ public: sAddress ( sNewAddress ), iPort ( iNewPort ) { + sLAddress = GenRandomIPv4Address().toString(); + iLPort = static_cast ( GenRandomIntInRange ( -2, 10000 ) ); + // bind socket (try 100 port numbers) quint16 iPortIncrement = 0; // start value: port nubmer plus ten bool bSuccess = false; // initialization for while loop @@ -98,7 +101,9 @@ protected: } QString sAddress; - quint16 iPort; + quint16 iPort; + QString sLAddress; + quint16 iLPort; QTimer Timer; CProtocol Protocol; QUdpSocket UdpSocket; @@ -111,7 +116,7 @@ public slots: CServerCoreInfo ServerInfo; CVector vecServerInfo ( 1 ); CHostAddress CurHostAddress ( QHostAddress ( sAddress ), iPort ); - CHostAddress CurLocalAddress ( GenRandomIPv4Address(), iPort ); + CHostAddress CurLocalAddress ( QHostAddress ( sLAddress ), iLPort ); CChannelCoreInfo ChannelCoreInfo; ELicenceType eLicenceType; @@ -212,7 +217,6 @@ public slots: ServerInfo.eCountry = static_cast ( GenRandomIntInRange ( 0, 100 ) ); - ServerInfo.iLocalPortNumber = GenRandomIntInRange ( -2, 10000 ); ServerInfo.iMaxNumClients = GenRandomIntInRange ( -2, 10000 ); ServerInfo.strCity = GenRandomString(); ServerInfo.strName = GenRandomString(); @@ -235,7 +239,6 @@ public slots: vecServerInfo[0].HostAddr = CurHostAddress; vecServerInfo[0].LHostAddr = CurLocalAddress; - vecServerInfo[0].iLocalPortNumber = GenRandomIntInRange ( -2, 10000 ); vecServerInfo[0].iMaxNumClients = GenRandomIntInRange ( -2, 10000 ); vecServerInfo[0].strCity = GenRandomString(); vecServerInfo[0].strName = GenRandomString(); diff --git a/src/util.h b/src/util.h index fcc86d2d..cfc290ba 100755 --- a/src/util.h +++ b/src/util.h @@ -870,7 +870,6 @@ class CServerCoreInfo { public: CServerCoreInfo() : - iLocalPortNumber ( 0 ), strName ( "" ), eCountry ( QLocale::AnyCountry ), strCity ( "" ), @@ -878,22 +877,17 @@ public: bPermanentOnline ( false ) {} CServerCoreInfo ( - const quint16 NLocPort, const QString& NsName, const QLocale::Country& NeCountry, const QString& NsCity, const int NiMaxNumClients, const bool NbPermOnline) : - iLocalPortNumber ( NLocPort ), strName ( NsName ), eCountry ( NeCountry ), strCity ( NsCity ), iMaxNumClients ( NiMaxNumClients ), bPermanentOnline ( NbPermOnline ) {} - // local port number of the server - quint16 iLocalPortNumber; - // name of the server QString strName; @@ -922,14 +916,12 @@ public: CServerInfo ( const CHostAddress& NHAddr, const CHostAddress& NLAddr, - const quint16 NLocPort, const QString& NsName, const QLocale::Country& NeCountry, const QString& NsCity, const int NiMaxNumClients, const bool NbPermOnline) : - CServerCoreInfo ( NLocPort, - NsName, + CServerCoreInfo ( NsName, NeCountry, NsCity, NiMaxNumClients,