Compile MasterServer ConnTester with VS2008

Hi,

Few tips to get the masterserver ConnTester compiled under Visual Studio 2008.

The major problem is that ‘curses.h’ is missing.

To solve this (without finding a curses.h that compiled under WIN32 I just imported/implemented the missing functions with:

#ifdef WIN32 
#include <time.h>     //time
#include <stdio.h>    //FILE
#include <windows.h>  //Sleep
void sleep(DWORD secs) {
	//Win32 sleeps in milliseconds
	Sleep(secs*1000);
}
void setlinebuf(FILE * f) {
	//?
}
#else
#include "curses.h" //Original Code
#endif

Above code isadded to Conntester.cpp.

This way the replacement is only used when compiling for WIN32 and uses curses.h when being compiled for *NIX.

Hi,

To silence warnings and make the code more secure I opted for changing most of the affected functions to the safe ‘_s’ versions instead of tellng MSVC to ingore them completly (warnings are warnings and should not be igonered imho).

To do this i added two files to the root of the project callled Fixups.h and Fixups.cpp.

Fixups.h:

#ifndef FIXUPS
#define FIXUPS

#ifndef WIN32 

errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource );
errno_t strcat_s(char * strDestination, size_t numberOfElements, const char * strSource );
errno_t strncpy_s(char * strDest, size_t numberOfElements, const char *strSource, size_t count);
int sprintf_s(char *buffer, size_t, sizeOfBuffer, const char *format, ... );
//FILE *fopen(const char *filename, const char *mode);
#else

#define countof(_Array) sizeof(_Array)

#include <stdlib.h>
#endif

#endif FIXUPS

Fixups.cpp:

#include "Fixups.h"

#ifndef WIN32
#include <stdio.h>    //printf
#include <stdarg.h>	  //va_list, va_start
#include <string.h>

	//The FixupCode is not tested as I cannot compile for *Nix.

errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource ) {
	//Call unsafe version (and maybe add some safety here..)
	strcpy_s(strDestination, strSource);
	
	//Assume no error...
	return 0;
}
errno_t strcat_s(char * strDestination, size_t numberOfElements, const char * strSource )
{
	//Call unsafe version (and maybe add some safety here..)
	strcat(strDestination, strSource);
	
	//Assume no error...
	return 0;
}

errno_t strncpy_s(char * strDest, size_t numberOfElements, const char *strSource, size_t count)
{
	//Call unsafe version (and maybe add some safety here..)
	strncpy(strDest, strSource, count);
	
	return 0;
}

int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ... )
{
	va_list arguments; 
	va_start(arguments, format); 

	//Call unsafe version (and maybe add some safety here..)
	return sprintf(buffer, format, arguments);
}

//errno_t fopen_s(FILE** pFile, const char *filename, const char *mode) {
//	pFile = fopen(filename, mode);
//}
#endif

To use these in the RakNet sources, add

#include "../../Fixups.h"

to the includes at the top of files that emit warnings.

To change the function calls causing the warning, add the ‘_s’ suffix to the name and insert a second function parameter either:

_countof('first parameter name')

or

sizeof('first parameter name').

The _countof() macro only works on arrays (and is element size aware so is unicode proof), sizeof can be used on all others that cannot use _countof()

Note that I did not change function like fopen, mkdir and vsnprintf but that should not be to hard to add.

For *Nix the Fixup code just calls the ‘unsafe’ versions as before (but safety checks could be added easily).

Hi,

Changed the print_log function to:

void print_log(const char* format, ...)
{
	time_t rawtime;
#ifdef WIN32
	struct tm timeinfo;
#else
	struct tm * timeinfo;
#endif
	time ( &rawtime );

#ifdef WIN32
	localtime_s (&timeinfo, &rawtime );
	printf("%02d-%02d-%d %02d:%02d:%02d\t",timeinfo.tm_mday, 1+timeinfo.tm_mon, 1900+timeinfo.tm_year, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
#else
	timeinfo = localtime ( &rawtime );
	printf("%02d-%02d-%d %02d:%02d:%02d\t",timeinfo->tm_mday, 1+timeinfo->tm_mon, 1900+timeinfo->tm_year, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
#endif

	va_list va;
	va_start( va, format );
	vprintf(format, va);
}

Hi,

Another safe version:

int vsnprintf_s(char *buffer, size_t, sizeOfBuffer size_t count, const char *format, va_list argptr);

and

int vsnprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, va_list argptr)
{
	return vsnprintf(buffer, count, format, argptr);
}

Hi,

Results of all previous changes:

16 warnings left in MasterServer
and
15 warnings left in ConnTester

Both showed way above 150 warnings when compiling the orignal code.

Hi,

Small code update (started debugging the server code):

in ‘DS_Table.cpp’ the function ‘void Table::Cell::Set(const char *input)’ contains a line with strcpy. Here the sizeof() function cannot be used. The correct code is:

strcpy_s(c, i, input);