Automatic IP update for domains

Want to discuss changes to the UOX3 source code? Got a code-snippet you'd like to post? Anything related to coding/programming goes here!
Post Reply
Grimson
Developer
Posts: 802
Joined: Sat Jun 04, 2005 1:52 am
Location: Germany
Has thanked: 0
Been thanked: 0

Automatic IP update for domains

Post by Grimson »

I did notice that when you use a domain name in the server list UOX3 resolves it's IP and stores it until you force it to read the whole ini again. While this is ok for normal domains, it's quite a bit annoying if you run the server with an dyndns domain and your IP changes often. So I have added a little bit of code to update the stored IPs. Here it comes:

In cServerData.h search for:

Code: Select all

void		ResetDefaults( void );
and add this directly after it:

Code: Select all

void		RefreshIPs( void );
In cServerData.cpp search for:

Code: Select all

CServerData::~CServerData()
{
}
and add this directly after it:

Code: Select all

void CServerData::RefreshIPs( void )
{
	struct hostent *lpHostEntry=NULL;

	for( size_t cnt = 0; cnt < serverList.size(); ++cnt )
	{
		if( !serverList[cnt].getDomain().empty() )
		{
			if( ( lpHostEntry = gethostbyname( serverList[cnt].getDomain().c_str() ) ) != NULL )
			{
				struct in_addr *pinaddr;
				pinaddr = ((struct in_addr*)lpHostEntry->h_addr);
				serverList[cnt].setIP( inet_ntoa(*pinaddr) );
			}
		}
	}

}
In worldmain.h search for:

Code: Select all

	// Worldsave
	UI32		oldtime, newtime;
	bool		autosaved;
	SaveStatus	worldSaveProgress;
and add this directly after it:

Code: Select all

	// IP Update
	UI32		oldIPtime, newIPtime;
	bool		ipupdated;
also search for:

Code: Select all

	// Worldsave
	void		SetNewTime( UI32 newVal );
	UI32		GetNewTime( void ) const;
	void		SetOldTime( UI32 newVal );
	UI32		GetOldTime( void ) const;
	void		SetAutoSaved( bool newVal );
	bool		GetAutoSaved( void ) const;
	void		SetWorldSaveProgress( SaveStatus newVal );
	SaveStatus	GetWorldSaveProgress( void ) const;
and add this directly after it:

Code: Select all

	// IP update
	UI32		GetNewIPTime( void ) const;
	void		SetNewIPTime( UI32 newVal );
	UI32		GetOldIPTime( void ) const;
	void		SetOldIPTime( UI32 newVal );
	void		SetIPUpdated( bool newVal );
	bool		GetIPUpdated( void ) const;
In worldmain.cpp search for:

Code: Select all

void CWorldMain::SetOldTime( UI32 newVal )
{
	oldtime = newVal;
}
and add this directly after it:

Code: Select all

//o--------------------------------------------------------------------------o
//|	Function		-	UI32 NewIPTime()
//o--------------------------------------------------------------------------o
//|	Purpose			-	Time for next auto IP update
//o--------------------------------------------------------------------------o
UI32 CWorldMain::GetNewIPTime( void ) const
{
	return newIPtime;
}
void CWorldMain::SetNewIPTime( UI32 newVal )
{
	newIPtime = newVal;
}

//o--------------------------------------------------------------------------o
//|	Function		-	UI32 OldIPTime()
//o--------------------------------------------------------------------------o
//|	Purpose			-	Time of last auto IP update
//o--------------------------------------------------------------------------o
UI32 CWorldMain::GetOldIPTime( void ) const
{
	return oldIPtime;
}
void CWorldMain::SetOldIPTime( UI32 newVal )
{
	oldIPtime = newVal;
}

//o--------------------------------------------------------------------------o
//|	Function		-	bool IPUpdated()
//o--------------------------------------------------------------------------o
//|	Purpose			-	have IPs been updated
//o--------------------------------------------------------------------------o
bool CWorldMain::GetIPUpdated( void ) const
{
	return ipupdated;
}
void CWorldMain::SetIPUpdated( bool newVal )
{
	ipupdated = newVal;
}
In uox3.cpp search for:

Code: Select all

			SetAutoSaved( false );
			SaveNewWorld( false );
		}
	}
and add this directly after it:

Code: Select all

	UI32 oldIPTime = GetOldIPTime();
	if( !GetIPUpdated() )
	{
		SetIPUpdated( true );
		time((time_t *)&oldIPTime);
		SetOldIPTime( oldIPTime );
	}
	UI32 newIPTime = GetNewIPTime();
	time((time_t *)&newIPTime);
	SetNewIPTime( newIPTime );

	if( difftime( GetNewIPTime(), GetOldIPTime() ) >= 120 )
	{
		ServerData()->RefreshIPs();
		SetIPUpdated( false );
	}
This will update the IPs stored for domains every 120 seconds. Currently it isn't possible to set the time in the ini, because I didn't manage to add this in without breaking the ini parsing (I hate the string.find function).
Sydius
UOX3 Apprentice
Posts: 171
Joined: Thu Mar 25, 2004 3:22 am
Has thanked: 0
Been thanked: 0

Post by Sydius »

Is serverList an STL container? If so, it might be better if you used iterator instead of random access.

You might want to inline the time setting and getting functions since you did not define them within the declaration. Most good compilers would inline them automatically anyway, and specifying “inline” really would not make that much of a difference since it is only a hint, but it would make it more conceptually clear that the definition does not do much.
Grimson
Developer
Posts: 802
Joined: Sat Jun 04, 2005 1:52 am
Location: Germany
Has thanked: 0
Been thanked: 0

Post by Grimson »

Tbh. I don't have much of clue about C++. In fact UOX3 is the first time I ever looked at C++ code. I do a little bit of C from time to time, but I'm nowhere near to a good C/C++ programmer. Additionally my time is very limited, so I really can't invest much time into quickly improving my C++ knowledge at the moment.

If you look close at the UOX3 code and my code you'll see that my code is put together from other functions that were already there. The "timer" code is from the worldsave timer, the code the resolve the IP is from the initial ready of the .ini file and the code to go through the serverlist is from the .ini saving part.
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

Looks pretty good.

I'd have to agree with Sydius, the use of proper STL containers and iteration would be preferrable (something EviL and I have been working to accomplish in UOX3 for years now :P ) But overall looks like it would be a helpful addition to UOX.

I'll have to run it by EviL, before addition to the main branch, if that is what you would like, as ultimately I let him give the thumbs up/thumbs down on new features.

Thanks for the contribution, keep 'em comin. :)
Scott
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

I have merged this addition in with the current source. It will be released with build 0.98-3.0e. Note I made some minor changes, making use of STL iterators and modifying a section in RefreshIPs() that was performing an assignment inside an if() statement.

We will need to add in the option to choose the refresh rate (or use 0 to disable it alltogether), but I'm not sure if I will have the chance to add that in with this next release or not.
Scott
Post Reply