Ejemplo n.º 1
0
std::vector<ServerList_st>& cConfig::serverList()
{
	static unsigned int lastIpCheck = 0;
	static bool dynamicIP = false;
	if ( serverList_.empty() || ( dynamicIP && lastIpCheck <= Server::instance()->time() ) ) // Empty? Try to load
	{
		serverList_.clear();

		bool bKeepLooping = true;
		unsigned int i = 1;
		do
		{
			QString tmp = getString( "LoginServer", QString( "Shard %1" ).arg( i++ ), QString::null, false ).simplifyWhiteSpace();
			bKeepLooping = !tmp.isEmpty();
			if ( bKeepLooping ) // valid data.
			{
				QStringList strList = QStringList::split( "=", tmp );
				if ( strList.size() == 2 )
				{
					ServerList_st server;
					server.sServer = strList[0];
					QStringList strList2 = QStringList::split( ",", strList[1].stripWhiteSpace() );
					QHostAddress host;
					host.setAddress( strList2[0] );
					server.sIP = strList2[0];
					server.ip = resolveName( server.sIP );

					bool ok = false;
					server.uiPort = strList2[1].toUShort( &ok );
					if ( !ok )
						server.uiPort = 2593; // Unspecified defaults to 2593

					// This code will retrieve the first
					// valid Internet IP it finds
					// and replace a 0.0.0.0 with it
					if ( ( ( server.ip == 0 ) && ( lastIpCheck <= Server::instance()->time() ) ) )
					{
						dynamicIP = true;
						hostent* hostinfo;
						char name[256];

						// We check for a new IP max. every 30 minutes
						// So we have a max. of 30 minutes downtime
						lastIpCheck = Server::instance()->time() + ( MY_CLOCKS_PER_SEC * 30 * 60 );

						// WSA Is needed for this :/
						if ( !gethostname( name, sizeof( name ) ) )
						{
							hostinfo = gethostbyname( name );

							if ( hostinfo )
							{
								Q_UINT32 i = 0;

								while ( hostinfo->h_addr_list[i] )
								{
									// Check if it's an INTERNET ADDRESS
									char* hIp = inet_ntoa( *( struct in_addr* ) hostinfo->h_addr_list[i++] );
									host.setAddress( hIp );
									Q_UINT32 ip = host.ip4Addr();
									Q_UINT8 part1 = ( ip & 0xFF000000 ) >> 24;
									Q_UINT8 part2 = ( ip & 0x00FF0000 ) >> 16;

									if ( ( part1 == 127 ) || 	//this one is class A too.
										( part1 == 10 ) || ( ( part1 == 192 ) && ( part2 == 168 ) ) || ( ( part1 == 172 ) && ( part2 >= 16 ) && ( part2 <= 31 ) ) || ( ( part1 == 169 ) && ( part2 == 254 ) ) // DHCP Space Stuff
									   )
									{
										continue;
									}

									// We are now certain that it's a valid INET ip
									server.ip = ip;
									break;
								}
							}
						}

						// Fall back to localhost
						if ( !server.ip )
						{
							server.ip = 0x7F000001;
							server.sIP = "127.0.0.1";
						}
					}
					serverList_.push_back( server );
				}
std::vector<ServerList_st>& cSrvParams::serverList()
{
	// We need a way to get this stuff for linux too
#ifndef __unix__
	static UINT32 lastIpCheck = 0;
	static UINT32 inetIp = 0;
#endif

	if ( serverList_.empty() ) // Empty? Try to load
	{
		setGroup("LoginServer");
		bool bKeepLooping = true;
		unsigned int i = 1;
		do
		{
			QString tmp = getString(QString("Shard %1").arg(i++), "").simplifyWhiteSpace();
			bKeepLooping = ( tmp != "" );
			if ( bKeepLooping ) // valid data.
			{
				QStringList strList = QStringList::split("=", tmp);
				if ( strList.size() == 2 )
				{
					ServerList_st server;
					server.sServer = strList[0];
					QStringList strList2 = QStringList::split(",", strList[1].stripWhiteSpace());
					QHostAddress host;
					host.setAddress( strList2[0] );
					server.sIP = strList2[0];
					server.ip = resolveName( server.sIP );

					bool ok = false;
					server.uiPort = strList2[1].toUShort(&ok);
					if ( !ok )
						server.uiPort = 2593; // Unspecified defaults to 2593

					// This code will retrieve the first
					// valid Internet IP it finds
					// and replace a 0.0.0.0 with it
#ifndef __unix__
					if( ( server.ip == 0 ) && ( lastIpCheck <= uiCurrentTime ) )
					{
						hostent *hostinfo;
						char name[256];

						// We check for a new IP max. every 30 minutes
						// So we have a max. of 30 minutes downtime
						lastIpCheck = uiCurrentTime + (MY_CLOCKS_PER_SEC*30*60);

						// WSA Is needed for this :/
						if( !gethostname( name, sizeof( name ) ) )
						{
							hostinfo = gethostbyname( name );

							if( hostinfo )
							{
								UINT32 i = 0;
								while( hostinfo->h_addr_list[i] )
								{
									// Check if it's an INTERNET ADDRESS
									char *hIp = inet_ntoa( *(struct in_addr *)hostinfo->h_addr_list[i++] );
									host.setAddress( hIp );
									UINT32 ip = host.ip4Addr();
									UINT8 part1 = ( ip & 0xFF000000 ) >> 24;
									UINT8 part2 = ( ip & 0x00FF0000 ) >> 16;

									if	( 
										( part1 == 127 ) || //this one is class A too.
										( part1 == 10 ) || 
										( ( part1 == 192 ) && ( part2 == 168 ) ) || 
										( ( part1 == 172 ) && ( part2 == 16 ) ) 
										)
										continue;

									// We are now certain that it's a valid INET ip
									server.ip = ip;
									inetIp = ip;
								}
							}
						}
						else if( inetIp )
							server.sIP = inetIp;
					}