diff options
Diffstat (limited to 'src/qcommon')
-rw-r--r-- | src/qcommon/cmd.c | 2 | ||||
-rw-r--r-- | src/qcommon/common.c | 24 | ||||
-rw-r--r-- | src/qcommon/cvar.c | 317 | ||||
-rw-r--r-- | src/qcommon/files.c | 56 | ||||
-rw-r--r-- | src/qcommon/huffman.c | 10 | ||||
-rw-r--r-- | src/qcommon/msg.c | 11 | ||||
-rw-r--r-- | src/qcommon/net_chan.c | 139 | ||||
-rw-r--r-- | src/qcommon/net_ip.c | 1183 | ||||
-rw-r--r-- | src/qcommon/parse.c | 4 | ||||
-rw-r--r-- | src/qcommon/q_platform.h | 4 | ||||
-rw-r--r-- | src/qcommon/q_shared.c | 52 | ||||
-rw-r--r-- | src/qcommon/q_shared.h | 82 | ||||
-rw-r--r-- | src/qcommon/qcommon.h | 53 | ||||
-rw-r--r-- | src/qcommon/vm.c | 33 | ||||
-rw-r--r-- | src/qcommon/vm_interpreted.c | 12 | ||||
-rw-r--r-- | src/qcommon/vm_local.h | 2 | ||||
-rw-r--r-- | src/qcommon/vm_x86_64.c | 4 |
17 files changed, 1395 insertions, 593 deletions
diff --git a/src/qcommon/cmd.c b/src/qcommon/cmd.c index 7495bf1c..97687467 100644 --- a/src/qcommon/cmd.c +++ b/src/qcommon/cmd.c @@ -145,9 +145,11 @@ void Cbuf_ExecuteText (int exec_when, const char *text) { case EXEC_NOW: if (text && strlen(text) > 0) { + Com_DPrintf(S_COLOR_YELLOW "EXEC_NOW %s\n", text); Cmd_ExecuteString (text); } else { Cbuf_Execute(); + Com_DPrintf(S_COLOR_YELLOW "EXEC_NOW %s\n", cmd_text.data); } break; case EXEC_INSERT: diff --git a/src/qcommon/common.c b/src/qcommon/common.c index c074aa74..586c334c 100644 --- a/src/qcommon/common.c +++ b/src/qcommon/common.c @@ -269,15 +269,18 @@ void QDECL Com_Error( int code, const char *fmt, ... ) { com_errorEntered = qtrue; va_start (argptr,fmt); - vsprintf (com_errorMessage,fmt,argptr); + Q_vsnprintf (com_errorMessage, sizeof(com_errorMessage),fmt,argptr); va_end (argptr); if (code != ERR_DISCONNECT && code != ERR_NEED_CD) Cvar_Set("com_errorMessage", com_errorMessage); if (code == ERR_DISCONNECT || code == ERR_SERVERDISCONNECT) { + SV_Shutdown( "Server disconnected" ); CL_Disconnect( qtrue ); + VM_Forced_Unload_Start(); CL_FlushMemory( ); + VM_Forced_Unload_Done(); // make sure we can get at our local stuff FS_PureServerSetLoadedPaks("", ""); com_errorEntered = qfalse; @@ -286,7 +289,9 @@ void QDECL Com_Error( int code, const char *fmt, ... ) { Com_Printf ("********************\nERROR: %s\n********************\n", com_errorMessage); SV_Shutdown (va("Server crashed: %s", com_errorMessage)); CL_Disconnect( qtrue ); + VM_Forced_Unload_Start(); CL_FlushMemory( ); + VM_Forced_Unload_Done(); FS_PureServerSetLoadedPaks("", ""); com_errorEntered = qfalse; longjmp (abortframe, -1); @@ -294,7 +299,9 @@ void QDECL Com_Error( int code, const char *fmt, ... ) { SV_Shutdown( "Server didn't have CD" ); if ( com_cl_running && com_cl_running->integer ) { CL_Disconnect( qtrue ); + VM_Forced_Unload_Start(); CL_FlushMemory( ); + VM_Forced_Unload_Done(); com_errorEntered = qfalse; CL_CDDialog(); } else { @@ -323,8 +330,9 @@ do the apropriate things. */ void Com_Quit_f( void ) { // don't try to shutdown if we are in a recursive error + char *p = Cmd_Args( ); if ( !com_errorEntered ) { - SV_Shutdown ("Server quit"); + SV_Shutdown (p[0] ? p : "Server quit"); CL_Shutdown (); Com_Shutdown (); FS_Shutdown(qtrue); @@ -464,10 +472,12 @@ qboolean Com_AddStartupCommands( void ) { continue; } - // set commands won't override menu startup - if ( Q_stricmpn( com_consoleLines[i], "set", 3 ) ) { - added = qtrue; + // set commands already added with Com_StartupVariable + if ( !Q_stricmpn( com_consoleLines[i], "set", 3 ) ) { + continue; } + + added = qtrue; Cbuf_AddText( com_consoleLines[i] ); Cbuf_AddText( "\n" ); } @@ -2430,9 +2440,11 @@ void Com_Init( char *commandLine ) { // get dedicated here for proper hunk megs initialization #ifdef DEDICATED - com_dedicated = Cvar_Get ("dedicated", "1", CVAR_ROM); + com_dedicated = Cvar_Get ("dedicated", "1", CVAR_INIT); + Cvar_CheckRange( com_dedicated, 1, 2, qtrue ); #else com_dedicated = Cvar_Get ("dedicated", "0", CVAR_LATCH); + Cvar_CheckRange( com_dedicated, 0, 2, qtrue ); #endif // allocate the stack based hunk allocator Com_InitHunkMemory(); diff --git a/src/qcommon/cvar.c b/src/qcommon/cvar.c index f4f0b489..cb9f2634 100644 --- a/src/qcommon/cvar.c +++ b/src/qcommon/cvar.c @@ -190,6 +190,109 @@ void Cvar_CommandCompletion( void(*callback)(const char *s) ) { } } +/* +============ +Cvar_Validate +============ +*/ +static const char *Cvar_Validate( cvar_t *var, + const char *value, qboolean warn ) +{ + static char s[ MAX_CVAR_VALUE_STRING ]; + float valuef; + qboolean changed = qfalse; + + if( !var->validate ) + return value; + + if( !value ) + return value; + + if( Q_isanumber( value ) ) + { + valuef = atof( value ); + + if( var->integral ) + { + if( !Q_isintegral( valuef ) ) + { + if( warn ) + Com_Printf( "WARNING: cvar '%s' must be integral", var->name ); + + valuef = (int)valuef; + changed = qtrue; + } + } + } + else + { + if( warn ) + Com_Printf( "WARNING: cvar '%s' must be numeric", var->name ); + + valuef = atof( var->resetString ); + changed = qtrue; + } + + if( valuef < var->min ) + { + if( warn ) + { + if( changed ) + Com_Printf( " and is" ); + else + Com_Printf( "WARNING: cvar '%s'", var->name ); + + if( Q_isintegral( var->min ) ) + Com_Printf( " out of range (min %d)", (int)var->min ); + else + Com_Printf( " out of range (min %f)", var->min ); + } + + valuef = var->min; + changed = qtrue; + } + else if( valuef > var->max ) + { + if( warn ) + { + if( changed ) + Com_Printf( " and is" ); + else + Com_Printf( "WARNING: cvar '%s'", var->name ); + + if( Q_isintegral( var->max ) ) + Com_Printf( " out of range (max %d)", (int)var->max ); + else + Com_Printf( " out of range (max %f)", var->max ); + } + + valuef = var->max; + changed = qtrue; + } + + if( changed ) + { + if( Q_isintegral( valuef ) ) + { + Com_sprintf( s, sizeof( s ), "%d", (int)valuef ); + + if( warn ) + Com_Printf( ", setting to %d\n", (int)valuef ); + } + else + { + Com_sprintf( s, sizeof( s ), "%f", valuef ); + + if( warn ) + Com_Printf( ", setting to %f\n", valuef ); + } + + return s; + } + else + return value; +} + /* ============ @@ -221,6 +324,8 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { var = Cvar_FindVar (var_name); if ( var ) { + var_value = Cvar_Validate( var, var_value, qfalse ); + // if the C code is now specifying a variable that the user already // set a value for, take the new value as the reset value if ( ( var->flags & CVAR_USER_CREATED ) && !( flags & CVAR_USER_CREATED ) @@ -229,6 +334,17 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { Z_Free( var->resetString ); var->resetString = CopyString( var_value ); + if(flags & CVAR_ROM) + { + // this variable was set by the user, + // so force it to value given by the engine. + + if(var->latchedString) + Z_Free(var->latchedString); + + var->latchedString = CopyString(var_value); + } + // ZOID--needs to be set so that cvars the game sets as // SERVERINFO get sent to clients cvar_modifiedFlags |= flags; @@ -254,13 +370,6 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { Z_Free( s ); } -// use a CVAR_SET for rom sets, get won't override -#if 0 - // CVAR_ROM always overrides - if ( flags & CVAR_ROM ) { - Cvar_Set2( var_name, var_value, qtrue ); - } -#endif return var; } @@ -279,6 +388,7 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { var->value = atof (var->string); var->integer = atoi(var->string); var->resetString = CopyString( var_value ); + var->validate = qfalse; // link the variable in var->next = cvar_vars; @@ -297,6 +407,33 @@ cvar_t *Cvar_Get( const char *var_name, const char *var_value, int flags ) { /* ============ +Cvar_Print + +Prints the value, default, and latched string of the given variable +============ +*/ +void Cvar_Print( cvar_t *v ) { + Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\"", + v->name, v->string ); + + if ( !( v->flags & CVAR_ROM ) ) { + if ( !Q_stricmp( v->string, v->resetString ) ) { + Com_Printf (", the default" ); + } else { + Com_Printf (" default:\"%s" S_COLOR_WHITE "\"", + v->resetString ); + } + } + + Com_Printf ("\n"); + + if ( v->latchedString ) { + Com_Printf( "latched: \"%s\"\n", v->latchedString ); + } +} + +/* +============ Cvar_Set2 ============ */ @@ -334,6 +471,8 @@ cvar_t *Cvar_Set2( const char *var_name, const char *value, qboolean force ) { value = var->resetString; } + value = Cvar_Validate( var, value, qtrue ); + if((var->flags & CVAR_LATCH) && var->latchedString) { if(!strcmp(value,var->latchedString)) return var; @@ -509,23 +648,7 @@ qboolean Cvar_Command( void ) { // perform a variable print or set if ( Cmd_Argc() == 1 ) { - Com_Printf ("\"%s\" is:\"%s" S_COLOR_WHITE "\"", - v->name, v->string ); - - if ( !( v->flags & CVAR_ROM ) ) { - if ( !Q_stricmp( v->string, v->resetString ) ) { - Com_Printf (", the default" ); - } else { - Com_Printf (" default:\"%s" S_COLOR_WHITE "\"", - v->resetString ); - } - } - - Com_Printf ("\n"); - - if ( v->latchedString ) { - Com_Printf( "latched: \"%s\"\n", v->latchedString ); - } + Cvar_Print( v ); return qtrue; } @@ -537,6 +660,35 @@ qboolean Cvar_Command( void ) { /* ============ +Cvar_Print_f + +Prints the contents of a cvar +(preferred over Cvar_Command where cvar names and commands conflict) +============ +*/ +void Cvar_Print_f(void) +{ + char *name; + cvar_t *cv; + + if(Cmd_Argc() != 2) + { + Com_Printf ("usage: print <variable>\n"); + return; + } + + name = Cmd_Argv(1); + + cv = Cvar_FindVar(name); + + if(cv) + Cvar_Print(cv); + else + Com_Printf ("Cvar %s does not exist.\n", name); +} + +/* +============ Cvar_Toggle_f Toggles a cvar for easy single key binding @@ -566,11 +718,18 @@ weren't declared in C code. */ void Cvar_Set_f( void ) { int i, c, l, len; - char combined[MAX_STRING_TOKENS]; + char cmd[5], combined[MAX_STRING_TOKENS]; + cvar_t *v; c = Cmd_Argc(); - if ( c < 3 ) { - Com_Printf ("usage: set <variable> <value>\n"); + Q_strncpyz( cmd, Cmd_Argv(0), sizeof( cmd ) ); + + if ( c < 2 ) { + Com_Printf ("usage: %s <variable> <value>\n", cmd); + return; + } + if ( c == 2 ) { + Cvar_Print_f(); return; } @@ -587,73 +746,24 @@ void Cvar_Set_f( void ) { } l += len; } - Cvar_Set2 (Cmd_Argv(1), combined, qfalse); -} - -/* -============ -Cvar_SetU_f - -As Cvar_Set, but also flags it as userinfo -============ -*/ -void Cvar_SetU_f( void ) { - cvar_t *v; - - if ( Cmd_Argc() != 3 ) { - Com_Printf ("usage: setu <variable> <value>\n"); - return; - } - Cvar_Set_f(); - v = Cvar_FindVar( Cmd_Argv( 1 ) ); - if ( !v ) { + v = Cvar_Set2 (Cmd_Argv(1), combined, qfalse); + if( !v ) { return; } - v->flags |= CVAR_USERINFO; -} - -/* -============ -Cvar_SetS_f - -As Cvar_Set, but also flags it as userinfo -============ -*/ -void Cvar_SetS_f( void ) { - cvar_t *v; - - if ( Cmd_Argc() != 3 ) { - Com_Printf ("usage: sets <variable> <value>\n"); - return; - } - Cvar_Set_f(); - v = Cvar_FindVar( Cmd_Argv( 1 ) ); - if ( !v ) { - return; - } - v->flags |= CVAR_SERVERINFO; -} - -/* -============ -Cvar_SetA_f - -As Cvar_Set, but also flags it as archived -============ -*/ -void Cvar_SetA_f( void ) { - cvar_t *v; - - if ( Cmd_Argc() != 3 ) { - Com_Printf ("usage: seta <variable> <value>\n"); - return; - } - Cvar_Set_f(); - v = Cvar_FindVar( Cmd_Argv( 1 ) ); - if ( !v ) { - return; + switch( cmd[3] ) { + default: + case '\0': + break; + case 'u': + v->flags |= CVAR_USERINFO; + break; + case 's': + v->flags |= CVAR_SERVERINFO; + break; + case 'a': + v->flags |= CVAR_ARCHIVE; + break; } - v->flags |= CVAR_ARCHIVE; } /* @@ -875,6 +985,22 @@ void Cvar_InfoStringBuffer( int bit, char* buff, int buffsize ) { /* ===================== +Cvar_CheckRange +===================== +*/ +void Cvar_CheckRange( cvar_t *var, float min, float max, qboolean integral ) +{ + var->validate = qtrue; + var->min = min; + var->max = max; + var->integral = integral; + + // Force an initial range check + Cvar_Set( var->name, var->string ); +} + +/* +===================== Cvar_Register basically a slightly modified Cvar_Get for the interpreted modules @@ -895,7 +1021,7 @@ void Cvar_Register( vmCvar_t *vmCvar, const char *varName, const char *defaultVa /* ===================== -Cvar_Register +Cvar_Update updates an interpreted modules' version of a cvar ===================== @@ -938,11 +1064,12 @@ Reads in all archived cvars void Cvar_Init (void) { cvar_cheats = Cvar_Get("sv_cheats", "1", CVAR_ROM | CVAR_SYSTEMINFO ); + Cmd_AddCommand ("print", Cvar_Print_f); Cmd_AddCommand ("toggle", Cvar_Toggle_f); Cmd_AddCommand ("set", Cvar_Set_f); - Cmd_AddCommand ("sets", Cvar_SetS_f); - Cmd_AddCommand ("setu", Cvar_SetU_f); - Cmd_AddCommand ("seta", Cvar_SetA_f); + Cmd_AddCommand ("sets", Cvar_Set_f); + Cmd_AddCommand ("setu", Cvar_Set_f); + Cmd_AddCommand ("seta", Cvar_Set_f); Cmd_AddCommand ("reset", Cvar_Reset_f); Cmd_AddCommand ("cvarlist", Cvar_List_f); Cmd_AddCommand ("cvar_restart", Cvar_Restart_f); diff --git a/src/qcommon/files.c b/src/qcommon/files.c index 0228a4be..53385244 100644 --- a/src/qcommon/files.c +++ b/src/qcommon/files.c @@ -382,7 +382,7 @@ static fileHandle_t FS_HandleForFile(void) { static FILE *FS_FileForHandle( fileHandle_t f ) { if ( f < 0 || f > MAX_FILE_HANDLES ) { - Com_Error( ERR_DROP, "FS_FileForHandle: out of reange" ); + Com_Error( ERR_DROP, "FS_FileForHandle: out of range" ); } if (fsh[f].zipFile == qtrue) { Com_Error( ERR_DROP, "FS_FileForHandle: can't get FILE on zip file" ); @@ -497,6 +497,24 @@ static qboolean FS_CreatePath (char *OSPath) { /* ================= +FS_FilenameIsExecutable + +ERR_FATAL if trying to maniuplate a file with the platform library extension +================= + */ +static void FS_FilenameIsExecutable( const char *filename, const char *function ) +{ + // Check if the filename ends with the library extension + if( !Q_stricmp( filename + strlen( filename ) - strlen( DLL_EXT ), DLL_EXT ) ) + { + Com_Error( ERR_FATAL, "%s: Not allowed to write '%s' due to %s extension\n", + function, filename, DLL_EXT ); + } +} + + +/* +================= FS_CopyFile Copy a fully specified file from one place to another @@ -509,6 +527,8 @@ static void FS_CopyFile( char *fromOSPath, char *toOSPath ) { Com_Printf( "copy %s to %s\n", fromOSPath, toOSPath ); + FS_FilenameIsExecutable( toOSPath, __FUNCTION__ ); + if (strstr(fromOSPath, "journal.dat") || strstr(fromOSPath, "journaldata.dat")) { Com_Printf( "Ignoring journal files\n"); return; @@ -550,6 +570,8 @@ FS_Remove =========== */ void FS_Remove( const char *osPath ) { + FS_FilenameIsExecutable( osPath, __FUNCTION__ ); + remove( osPath ); } @@ -560,6 +582,8 @@ FS_HomeRemove =========== */ void FS_HomeRemove( const char *homePath ) { + FS_FilenameIsExecutable( homePath, __FUNCTION__ ); + remove( FS_BuildOSPath( fs_homepath->string, fs_gamedir, homePath ) ); } @@ -637,6 +661,8 @@ fileHandle_t FS_SV_FOpenFileWrite( const char *filename ) { Com_Printf( "FS_SV_FOpenFileWrite: %s\n", ospath ); } + FS_FilenameIsExecutable( ospath, __FUNCTION__ ); + if( FS_CreatePath( ospath ) ) { return 0; } @@ -716,7 +742,8 @@ int FS_SV_FOpenFileRead( const char *filename, fileHandle_t *fp ) { if (f) { return FS_filelength(f); } - return 0; + + return -1; } @@ -745,6 +772,8 @@ void FS_SV_Rename( const char *from, const char *to ) { Com_Printf( "FS_SV_Rename: %s --> %s\n", from_ospath, to_ospath ); } + FS_FilenameIsExecutable( to_ospath, __FUNCTION__ ); + if (rename( from_ospath, to_ospath )) { // Failed, try copying it and deleting the original FS_CopyFile ( from_ospath, to_ospath ); @@ -777,6 +806,8 @@ void FS_Rename( const char *from, const char *to ) { Com_Printf( "FS_Rename: %s --> %s\n", from_ospath, to_ospath ); } + FS_FilenameIsExecutable( to_ospath, __FUNCTION__ ); + if (rename( from_ospath, to_ospath )) { // Failed, try copying it and deleting the original FS_CopyFile ( from_ospath, to_ospath ); @@ -838,6 +869,8 @@ fileHandle_t FS_FOpenFileWrite( const char *filename ) { Com_Printf( "FS_FOpenFileWrite: %s\n", ospath ); } + FS_FilenameIsExecutable( ospath, __FUNCTION__ ); + if( FS_CreatePath( ospath ) ) { return 0; } @@ -884,6 +917,8 @@ fileHandle_t FS_FOpenFileAppend( const char *filename ) { Com_Printf( "FS_FOpenFileAppend: %s\n", ospath ); } + FS_FilenameIsExecutable( ospath, __FUNCTION__ ); + if( FS_CreatePath( ospath ) ) { return 0; } @@ -1086,8 +1121,10 @@ int FS_FOpenFileRead( const char *filename, fileHandle_t *file, qboolean uniqueF temp = zfi->file; // set the file position in the zip file (also sets the current file info) unzSetCurrentFileInfoPosition(pak->handle, pakFile->pos); - // copy the file info into the unzip structure - Com_Memcpy( zfi, pak->handle, sizeof(unz_s) ); + if ( zfi != pak->handle ) { + // copy the file info into the unzip structure + Com_Memcpy( zfi, pak->handle, sizeof(unz_s) ); + } // we copy this back into the structure zfi->file = temp; // open the file in the zip @@ -2467,7 +2504,7 @@ qboolean FS_idPak( char *pak, char *base ) { /* ================ -FS_idPak +FS_CheckDirTraversal Check whether the string contains stuff like "../" to prevent directory traversal bugs and return qtrue if it does. @@ -2777,6 +2814,7 @@ static void FS_Startup( const char *gameName ) Com_Printf( "%d files in pk3 files\n", fs_packFiles ); } + /* ===================== FS_GamePureChecksum @@ -2981,10 +3019,10 @@ const char *FS_ReferencedPakNames( void ) { for ( search = fs_searchpaths ; search ; search = search->next ) { // is the element a pak file? if ( search->pack ) { - if (*info) { - Q_strcat(info, sizeof( info ), " " ); - } if (search->pack->referenced || Q_stricmpn(search->pack->pakGamename, BASEGAME, strlen(BASEGAME))) { + if (*info) { + Q_strcat(info, sizeof( info ), " " ); + } Q_strcat( info, sizeof( info ), search->pack->pakGamename ); Q_strcat( info, sizeof( info ), "/" ); Q_strcat( info, sizeof( info ), search->pack->pakBasename ); @@ -3258,7 +3296,7 @@ int FS_FOpenFileByMode( const char *qpath, fileHandle_t *f, fsMode_t mode ) { } break; default: - Com_Error( ERR_FATAL, "FSH_FOpenFile: bad mode" ); + Com_Error( ERR_FATAL, "FS_FOpenFileByMode: bad mode" ); return -1; } diff --git a/src/qcommon/huffman.c b/src/qcommon/huffman.c index a579944f..82aadad9 100644 --- a/src/qcommon/huffman.c +++ b/src/qcommon/huffman.c @@ -40,6 +40,16 @@ void Huff_putBit( int bit, byte *fout, int *offset) { *offset = bloc; } +int Huff_getBloc(void) +{ + return bloc; +} + +void Huff_setBloc(int _bloc) +{ + bloc = _bloc; +} + int Huff_getBit( byte *fin, int *offset) { int t; bloc = *offset; diff --git a/src/qcommon/msg.c b/src/qcommon/msg.c index 47fbc2dc..d98b6a78 100644 --- a/src/qcommon/msg.c +++ b/src/qcommon/msg.c @@ -390,6 +390,17 @@ int MSG_ReadByte( msg_t *msg ) { return c; } +int MSG_LookaheadByte( msg_t *msg ) { + const int bloc = Huff_getBloc(); + const int readcount = msg->readcount; + const int bit = msg->bit; + int c = MSG_ReadByte(msg); + Huff_setBloc(bloc); + msg->readcount = readcount; + msg->bit = bit; + return c; +} + int MSG_ReadShort( msg_t *msg ) { int c; diff --git a/src/qcommon/net_chan.c b/src/qcommon/net_chan.c index aba3b96d..246cd0b0 100644 --- a/src/qcommon/net_chan.c +++ b/src/qcommon/net_chan.c @@ -461,71 +461,6 @@ qboolean Netchan_Process( netchan_t *chan, msg_t *msg ) { //============================================================================== -/* -=================== -NET_CompareBaseAdr - -Compares without the port -=================== -*/ -qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b) -{ - if (a.type != b.type) - return qfalse; - - if (a.type == NA_LOOPBACK) - return qtrue; - - if (a.type == NA_IP) - { - if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2] && a.ip[3] == b.ip[3]) - return qtrue; - return qfalse; - } - - Com_Printf ("NET_CompareBaseAdr: bad address type\n"); - return qfalse; -} - -const char *NET_AdrToString (netadr_t a) -{ - static char s[64]; - - if (a.type == NA_LOOPBACK) { - Com_sprintf (s, sizeof(s), "loopback"); - } else if (a.type == NA_IP) { - Com_sprintf (s, sizeof(s), "%i.%i.%i.%i:%hu", - a.ip[0], a.ip[1], a.ip[2], a.ip[3], BigShort(a.port)); - } - - return s; -} - - -qboolean NET_CompareAdr (netadr_t a, netadr_t b) -{ - if (a.type != b.type) - return qfalse; - - if (a.type == NA_LOOPBACK) - return qtrue; - - if (a.type == NA_IP) - { - if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2] && a.ip[3] == b.ip[3] && a.port == b.port) - return qtrue; - return qfalse; - } - - Com_Printf ("NET_CompareAdr: bad address type\n"); - return qfalse; -} - - -qboolean NET_IsLocalAddress( netadr_t adr ) { - return adr.type == NA_LOOPBACK; -} - /* @@ -737,46 +672,68 @@ void QDECL NET_OutOfBandData( netsrc_t sock, netadr_t adr, byte *format, int len NET_StringToAdr Traps "localhost" for loopback, passes everything else to system +return 0 on address not found, 1 on address found with port, 2 on address found without port. ============= */ -qboolean NET_StringToAdr( const char *s, netadr_t *a ) { - qboolean r; - char base[MAX_STRING_CHARS]; - char *port; +int NET_StringToAdr( const char *s, netadr_t *a, netadrtype_t family ) +{ + char base[MAX_STRING_CHARS], *search; + char *port = NULL; if (!strcmp (s, "localhost")) { Com_Memset (a, 0, sizeof(*a)); a->type = NA_LOOPBACK; - return qtrue; +// as NA_LOOPBACK doesn't require ports report port was given. + return 1; } - // look for a port number Q_strncpyz( base, s, sizeof( base ) ); - port = strstr( base, ":" ); - if ( port ) { - *port = 0; - port++; + + if(*base == '[' || Q_CountChar(base, ':') > 1) + { + // This is an ipv6 address, handle it specially. + search = strchr(base, ']'); + if(search) + { + *search = '\0'; + search++; + + if(*search == ':') + port = search + 1; + } + + if(*base == '[') + search = base + 1; + else + search = base; } - - r = Sys_StringToAdr( base, a ); - - if ( !r ) { - a->type = NA_BAD; - return qfalse; + else + { + // look for a port number + port = strchr( base, ':' ); + + if ( port ) { + *port = '\0'; + port++; + } + + search = base; } - // inet_addr returns this if out of range - if ( a->ip[0] == 255 && a->ip[1] == 255 && a->ip[2] == 255 && a->ip[3] == 255 ) { + if(!Sys_StringToAdr(search, a, family)) + { a->type = NA_BAD; - return qfalse; + return 0; } - if ( port ) { - a->port = BigShort( (short)atoi( port ) ); - } else { - a->port = BigShort( PORT_SERVER ); + if(port) + { + a->port = BigShort((short) atoi(port)); + return 1; + } + else + { + a->port = BigShort(PORT_SERVER); + return 2; } - - return qtrue; } - diff --git a/src/qcommon/net_ip.c b/src/qcommon/net_ip.c index f44ff657..9ecd5e9e 100644 --- a/src/qcommon/net_ip.c +++ b/src/qcommon/net_ip.c @@ -24,9 +24,21 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "../qcommon/qcommon.h" #ifdef _WIN32 -#include <winsock.h> +#include <winsock2.h> +#include <ws2tcpip.h> +#if WINVER < 0x501 +#include <wspiapi.h> +#else +#include <ws2spi.h> +#endif typedef int socklen_t; +#ifdef ADDRESS_FAMILY +#define sa_family_t ADDRESS_FAMILY +#else +typedef unsigned short sa_family_t; +#endif + #define EAGAIN WSAEWOULDBLOCK #define EADDRNOTAVAIL WSAEADDRNOTAVAIL #define EAFNOSUPPORT WSAEAFNOSUPPORT @@ -47,17 +59,14 @@ static qboolean winsockInitialized = qfalse; #include <errno.h> #include <netdb.h> #include <netinet/in.h> -#include <sys/ioctl.h> #include <sys/socket.h> +#include <net/if.h> +#include <sys/ioctl.h> #include <sys/types.h> #include <sys/time.h> #include <unistd.h> - -#ifdef MACOS_X -#include <sys/sockio.h> -#include <net/if.h> -#include <net/if_types.h> -#include <net/if_dl.h> // for 'struct sockaddr_dl' +#if !defined(__sun) && !defined(__sgi) +#include <ifaddrs.h> #endif #ifdef __sun @@ -74,23 +83,64 @@ typedef int SOCKET; #endif static qboolean usingSocks = qfalse; -static qboolean networkingEnabled = qfalse; +static int networkingEnabled = 0; + +#define NET_ENABLEV4 0x01 +#define NET_ENABLEV6 0x02 +// if this flag is set, always attempt ipv6 connections instead of ipv4 if a v6 address is found. +#define NET_PRIOV6 0x04 +// disables ipv6 multicast support if set. +#define NET_DISABLEMCAST 0x08 -static cvar_t *net_noudp; +static cvar_t *net_enabled; static cvar_t *net_socksEnabled; static cvar_t *net_socksServer; static cvar_t *net_socksPort; static cvar_t *net_socksUsername; static cvar_t *net_socksPassword; + +static cvar_t *net_ip; +static cvar_t *net_ip6; +static cvar_t *net_port; +static cvar_t *net_port6; +static cvar_t *net_mcast6addr; +static cvar_t *net_mcast6iface; + static struct sockaddr socksRelayAddr; -static SOCKET ip_socket; -static SOCKET socks_socket; +static SOCKET ip_socket = INVALID_SOCKET; +static SOCKET ip6_socket = INVALID_SOCKET; +static SOCKET socks_socket = INVALID_SOCKET; +static SOCKET multicast6_socket = INVALID_SOCKET; + +// Keep track of currently joined multicast group. +static struct ipv6_mreq curgroup; +// And the currently bound address. +static struct sockaddr_in6 boundto; + +#ifndef IF_NAMESIZE + #define IF_NAMESIZE 16 +#endif + +// use an admin local address per default so that network admins can decide on how to handle quake3 traffic. +#define NET_MULTICAST_IP6 "ff04::696f:7175:616b:6533" + +#define MAX_IPS 32 + +typedef struct +{ + char ifname[IF_NAMESIZE]; + + netadrtype_t type; + sa_family_t family; + struct sockaddr_storage addr; + struct sockaddr_storage netmask; +} nip_localaddr_t; + +static nip_localaddr_t localIP[MAX_IPS]; +static int numIP; -#define MAX_IPS 16 -static int numIP; -static byte localIP[MAX_IPS][4]; //============================================================================= @@ -156,8 +206,6 @@ char *NET_ErrorString( void ) { } static void NetadrToSockadr( netadr_t *a, struct sockaddr *s ) { - memset( s, 0, sizeof(*s) ); - if( a->type == NA_BROADCAST ) { ((struct sockaddr_in *)s)->sin_family = AF_INET; ((struct sockaddr_in *)s)->sin_port = a->port; @@ -168,6 +216,17 @@ static void NetadrToSockadr( netadr_t *a, struct sockaddr *s ) { ((struct sockaddr_in *)s)->sin_addr.s_addr = *(int *)&a->ip; ((struct sockaddr_in *)s)->sin_port = a->port; } + else if( a->type == NA_IP6 ) { + ((struct sockaddr_in6 *)s)->sin6_family = AF_INET6; + ((struct sockaddr_in6 *)s)->sin6_addr = * ((struct in6_addr *) &a->ip6); + ((struct sockaddr_in6 *)s)->sin6_port = a->port; + } + else if(a->type == NA_MULTICAST6) + { + ((struct sockaddr_in6 *)s)->sin6_family = AF_INET6; + ((struct sockaddr_in6 *)s)->sin6_addr = curgroup.ipv6mr_multiaddr; + ((struct sockaddr_in6 *)s)->sin6_port = a->port; + } } @@ -177,32 +236,106 @@ static void SockadrToNetadr( struct sockaddr *s, netadr_t *a ) { *(int *)&a->ip = ((struct sockaddr_in *)s)->sin_addr.s_addr; a->port = ((struct sockaddr_in *)s)->sin_port; } + else if(s->sa_family == AF_INET6) + { + a->type = NA_IP6; + memcpy(a->ip6, &((struct sockaddr_in6 *)s)->sin6_addr, sizeof(a->ip6)); + a->port = ((struct sockaddr_in6 *)s)->sin6_port; + } } +static struct addrinfo *SearchAddrInfo(struct addrinfo *hints, sa_family_t family) +{ + while(hints) + { + if(hints->ai_family == family) + return hints; + + hints = hints->ai_next; + } + + return NULL; +} + /* ============= Sys_StringToSockaddr ============= */ -static qboolean Sys_StringToSockaddr( const char *s, struct sockaddr *sadr ) { - struct hostent *h; +static qboolean Sys_StringToSockaddr(const char *s, struct sockaddr *sadr, int sadr_len, sa_family_t family) +{ + struct addrinfo hints, *res = NULL, *search; + struct addrinfo *hintsp; + int retval; - memset( sadr, 0, sizeof( *sadr ) ); + memset(sadr, '\0', sizeof(*sadr)); + memset(&hints, '\0', sizeof(hints)); - ((struct sockaddr_in *)sadr)->sin_family = AF_INET; - ((struct sockaddr_in *)sadr)->sin_port = 0; + // workaround for buggy MacOSX getaddrinfo implementation that doesn't handle AF_UNSPEC in hints correctly. + if(family == AF_UNSPEC) + hintsp = NULL; + else + { + hintsp = &hints; + hintsp->ai_family = family; + } + + retval = getaddrinfo(s, NULL, hintsp, &res); - if( s[0] >= '0' && s[0] <= '9' ) { - *(int *)&((struct sockaddr_in *)sadr)->sin_addr = inet_addr(s); - } else { - if( ( h = gethostbyname( s ) ) == 0 ) { - return 0; + if(!retval) + { + if(family == AF_UNSPEC) + { + // Decide here and now which protocol family to use + if((net_enabled->integer & NET_ENABLEV6) && (net_enabled->integer & NET_PRIOV6)) + search = SearchAddrInfo(res, AF_INET6); + else + search = SearchAddrInfo(res, AF_INET); + + if(!search) + { + if((net_enabled->integer & NET_ENABLEV6) && + (net_enabled->integer & NET_PRIOV6) && + (net_enabled->integer & NET_ENABLEV4)) + search = SearchAddrInfo(res, AF_INET); + else if(net_enabled->integer & NET_ENABLEV6) + search = SearchAddrInfo(res, AF_INET6); + } } - *(int *)&((struct sockaddr_in *)sadr)->sin_addr = *(int *)h->h_addr_list[0]; + else + search = SearchAddrInfo(res, family); + + if(search) + { + if(res->ai_addrlen > sadr_len) + res->ai_addrlen = sadr_len; + + memcpy(sadr, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); + + return qtrue; + } + else + Com_Printf("Sys_StringToSockaddr: Error resolving %s: No address of required type found.\n", s); } + else + Com_Printf("Sys_StringToSockaddr: Error resolving %s: %s\n", s, gai_strerror(retval)); - return qtrue; + if(res) + freeaddrinfo(res); + + return qfalse; +} + +/* +============= +Sys_SockaddrToString +============= +*/ +static void Sys_SockaddrToString(char *dest, int destlen, struct sockaddr *input, int inputlen) +{ + getnameinfo(input, inputlen, dest, destlen, NULL, 0, NI_NUMERICHOST); } /* @@ -210,17 +343,126 @@ static qboolean Sys_StringToSockaddr( const char *s, struct sockaddr *sadr ) { Sys_StringToAdr ============= */ -qboolean Sys_StringToAdr( const char *s, netadr_t *a ) { - struct sockaddr sadr; +qboolean Sys_StringToAdr( const char *s, netadr_t *a, netadrtype_t family ) { + struct sockaddr_storage sadr; + sa_family_t fam; - if ( !Sys_StringToSockaddr( s, &sadr ) ) { + switch(family) + { + case NA_IP: + fam = AF_INET; + break; + case NA_IP6: + fam = AF_INET6; + break; + default: + fam = AF_UNSPEC; + break; + } + if( !Sys_StringToSockaddr(s, (struct sockaddr *) &sadr, sizeof(sadr), fam ) ) { return qfalse; } - SockadrToNetadr( &sadr, a ); + SockadrToNetadr( (struct sockaddr *) &sadr, a ); return qtrue; } +/* +=================== +NET_CompareBaseAdr + +Compares without the port +=================== +*/ +qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b) +{ + if (a.type != b.type) + return qfalse; + + if (a.type == NA_LOOPBACK) + return qtrue; + + if (a.type == NA_IP) + { + if(!memcmp(a.ip, b.ip, sizeof(a.ip))) + return qtrue; + + return qfalse; + } + + if (a.type == NA_IP6) + { + if(!memcmp(a.ip6, b.ip6, sizeof(a.ip6))) + return qtrue; + + return qfalse; + } + + Com_Printf ("NET_CompareBaseAdr: bad address type\n"); + return qfalse; +} + +const char *NET_AdrToString (netadr_t a) +{ + static char s[NET_ADDRSTRMAXLEN]; + + if (a.type == NA_LOOPBACK) + { + Com_sprintf (s, sizeof(s), "loopback"); + } + else if (a.type == NA_IP || a.type == NA_IP6) + { + struct sockaddr_storage sadr; + + memset(&sadr, 0, sizeof(sadr)); + NetadrToSockadr(&a, (struct sockaddr *) &sadr); + Sys_SockaddrToString(s, sizeof(s), (struct sockaddr *) &sadr, sizeof(sadr)); + } + + return s; +} + +const char *NET_AdrToStringwPort (netadr_t a) +{ + static char s[NET_ADDRSTRMAXLEN]; + + if (a.type == NA_LOOPBACK) + { + Com_sprintf (s, sizeof(s), "loopback"); + } + else if (a.type == NA_IP || a.type == NA_IP6) + { + if(a.type == NA_IP) + Com_sprintf(s, sizeof(s), "%s:%hu", NET_AdrToString(a), ntohs(a.port)); + else if(a.type == NA_IP6) + Com_sprintf(s, sizeof(s), "[%s]:%hu", NET_AdrToString(a), ntohs(a.port)); + } + + return s; +} + + +qboolean NET_CompareAdr (netadr_t a, netadr_t b) +{ + if(!NET_CompareBaseAdr(a, b)) + return qfalse; + + if (a.type == NA_IP || a.type == NA_IP6) + { + if (a.port == b.port) + return qtrue; + } + else + return qtrue; + + return qfalse; +} + + +qboolean NET_IsLocalAddress( netadr_t adr ) { + return adr.type == NA_LOOPBACK; +} + //============================================================================= /* @@ -236,56 +478,116 @@ int recvfromCount; qboolean Sys_GetPacket( netadr_t *net_from, msg_t *net_message ) { int ret; - struct sockaddr from; + struct sockaddr_storage from; socklen_t fromlen; int err; - if( !ip_socket ) { - return qfalse; - } - - fromlen = sizeof(from); #ifdef _DEBUG recvfromCount++; // performance check #endif - ret = recvfrom( ip_socket, net_message->data, net_message->maxsize, 0, (struct sockaddr *)&from, &fromlen ); - if (ret == SOCKET_ERROR) + + if(ip_socket != INVALID_SOCKET) { - err = socketError; + fromlen = sizeof(from); + ret = recvfrom( ip_socket, (void *)net_message->data, net_message->maxsize, 0, (struct sockaddr *) &from, &fromlen ); + + if (ret == SOCKET_ERROR) + { + err = socketError; - if( err == EAGAIN || err == ECONNRESET ) { - return qfalse; + if( err != EAGAIN && err != ECONNRESET ) + Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() ); + } + else + { + + memset( ((struct sockaddr_in *)&from)->sin_zero, 0, 8 ); + + if ( usingSocks && memcmp( &from, &socksRelayAddr, fromlen ) == 0 ) { + if ( ret < 10 || net_message->data[0] != 0 || net_message->data[1] != 0 || net_message->data[2] != 0 || net_message->data[3] != 1 ) { + return qfalse; + } + net_from->type = NA_IP; + net_from->ip[0] = net_message->data[4]; + net_from->ip[1] = net_message->data[5]; + net_from->ip[2] = net_message->data[6]; + net_from->ip[3] = net_message->data[7]; + net_from->port = *(short *)&net_message->data[8]; + net_message->readcount = 10; + } + else { + SockadrToNetadr( (struct sockaddr *) &from, net_from ); + net_message->readcount = 0; + } + + if( ret == net_message->maxsize ) { + Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) ); + return qfalse; + } + + net_message->cursize = ret; + return qtrue; } - Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() ); - return qfalse; } + + if(ip6_socket != INVALID_SOCKET) + { + fromlen = sizeof(from); + ret = recvfrom(ip6_socket, (void *)net_message->data, net_message->maxsize, 0, (struct sockaddr *) &from, &fromlen); + + if (ret == SOCKET_ERROR) + { + err = socketError; - memset( ((struct sockaddr_in *)&from)->sin_zero, 0, 8 ); - - if ( usingSocks && memcmp( &from, &socksRelayAddr, fromlen ) == 0 ) { - if ( ret < 10 || net_message->data[0] != 0 || net_message->data[1] != 0 || net_message->data[2] != 0 || net_message->data[3] != 1 ) { - return qfalse; + if( err != EAGAIN && err != ECONNRESET ) + Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() ); + } + else + { + SockadrToNetadr((struct sockaddr *) &from, net_from); + net_message->readcount = 0; + + if(ret == net_message->maxsize) + { + Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) ); + return qfalse; + } + + net_message->cursize = ret; + return qtrue; } - net_from->type = NA_IP; - net_from->ip[0] = net_message->data[4]; - net_from->ip[1] = net_message->data[5]; - net_from->ip[2] = net_message->data[6]; - net_from->ip[3] = net_message->data[7]; - net_from->port = *(short *)&net_message->data[8]; - net_message->readcount = 10; - } - else { - SockadrToNetadr( &from, net_from ); - net_message->readcount = 0; } - if( ret == net_message->maxsize ) { - Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) ); - return qfalse; - } + if(multicast6_socket != INVALID_SOCKET && multicast6_socket != ip6_socket) + { + fromlen = sizeof(from); + ret = recvfrom(multicast6_socket, (void *)net_message->data, net_message->maxsize, 0, (struct sockaddr *) &from, &fromlen); + + if (ret == SOCKET_ERROR) + { + err = socketError; - net_message->cursize = ret; - return qtrue; + if( err != EAGAIN && err != ECONNRESET ) + Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() ); + } + else + { + SockadrToNetadr((struct sockaddr *) &from, net_from); + net_message->readcount = 0; + + if(ret == net_message->maxsize) + { + Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) ); + return qfalse; + } + + net_message->cursize = ret; + return qtrue; + } + } + + + return qfalse; } //============================================================================= @@ -298,19 +600,25 @@ Sys_SendPacket ================== */ void Sys_SendPacket( int length, const void *data, netadr_t to ) { - int ret; - struct sockaddr addr; + int ret = SOCKET_ERROR; + struct sockaddr_storage addr; - if( to.type != NA_BROADCAST && to.type != NA_IP ) { + if( to.type != NA_BROADCAST && to.type != NA_IP && to.type != NA_IP6 && to.type != NA_MULTICAST6) + { Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" ); return; } - if( !ip_socket ) { + if( (ip_socket == INVALID_SOCKET && to.type == NA_IP) || + (ip6_socket == INVALID_SOCKET && to.type == NA_IP6) || + (ip6_socket == INVALID_SOCKET && to.type == NA_MULTICAST6) ) return; - } - NetadrToSockadr( &to, &addr ); + if(to.type == NA_MULTICAST6 && (net_enabled->integer & NET_DISABLEMCAST)) + return; + + memset(&addr, 0, sizeof(addr)); + NetadrToSockadr( &to, (struct sockaddr *) &addr ); if( usingSocks && to.type == NA_IP ) { socksBuf[0] = 0; // reserved @@ -323,7 +631,10 @@ void Sys_SendPacket( int length, const void *data, netadr_t to ) { ret = sendto( ip_socket, socksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) ); } else { - ret = sendto( ip_socket, data, length, 0, &addr, sizeof(addr) ); + if(addr.ss_family == AF_INET) + ret = sendto( ip_socket, data, length, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) ); + else if(addr.ss_family == AF_INET6) + ret = sendto( ip6_socket, data, length, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in6) ); } if( ret == SOCKET_ERROR ) { int err = socketError; @@ -353,62 +664,76 @@ LAN clients will have their rate var ignored ================== */ qboolean Sys_IsLANAddress( netadr_t adr ) { - int i; + int index, run, addrsize; + qboolean differed; + byte *compareadr, *comparemask, *compareip; if( adr.type == NA_LOOPBACK ) { return qtrue; } - if( adr.type != NA_IP ) { - return qfalse; - } - - // RFC1918: - // 10.0.0.0 - 10.255.255.255 (10/8 prefix) - // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) - // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) - if(adr.ip[0] == 10) - return qtrue; - if(adr.ip[0] == 172 && (adr.ip[1]&0xf0) == 16) - return qtrue; - if(adr.ip[0] == 192 && adr.ip[1] == 168) - return qtrue; - - // choose which comparison to use based on the class of the address being tested - // any local adresses of a different class than the address being tested will fail based on the first byte - // FIXME tma 28/08/07 Try and make this work for arbitrary subnet masks somehow + if( adr.type == NA_IP ) + { + // RFC1918: + // 10.0.0.0 - 10.255.255.255 (10/8 prefix) + // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) + // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) + if(adr.ip[0] == 10) + return qtrue; + if(adr.ip[0] == 172 && (adr.ip[1]&0xf0) == 16) + return qtrue; + if(adr.ip[0] == 192 && adr.ip[1] == 168) + return qtrue; - if( adr.ip[0] == 127 && adr.ip[1] == 0 && adr.ip[2] == 0 && adr.ip[3] == 1 ) { - return qtrue; + if(adr.ip[0] == 127) + return qtrue; } - - // Class A - if( (adr.ip[0] & 0x80) == 0x00 ) { - for ( i = 0 ; i < numIP ; i++ ) { - if( adr.ip[0] == localIP[i][0] ) { - return qtrue; - } - } - - return qfalse; + else if(adr.type == NA_IP6) + { + if(adr.ip6[0] == 0xfe && (adr.ip6[1] & 0xc0) == 0x80) + return qtrue; + if((adr.ip6[0] & 0xfe) == 0xfc) + return qtrue; } + + // Now compare against the networks this computer is member of. + for(index = 0; index < numIP; index++) + { + if(localIP[index].type == adr.type) + { + if(adr.type == NA_IP) + { + compareip = (byte *) &((struct sockaddr_in *) &localIP[index].addr)->sin_addr.s_addr; + comparemask = (byte *) &((struct sockaddr_in *) &localIP[index].netmask)->sin_addr.s_addr; + compareadr = adr.ip; + + addrsize = sizeof(adr.ip); + } + else + { + compareip = (byte *) &((struct sockaddr_in6 *) &localIP[index].addr)->sin6_addr; + comparemask = (byte *) &((struct sockaddr_in6 *) &localIP[index].netmask)->sin6_addr; + compareadr = adr.ip6; + + addrsize = sizeof(adr.ip6); + } - // Class B - if( (adr.ip[0] & 0xc0) == 0x80 ) { - for ( i = 0 ; i < numIP ; i++ ) { - if( adr.ip[0] == localIP[i][0] && adr.ip[1] == localIP[i][1] ) { - return qtrue; + differed = qfalse; + for(run = 0; run < addrsize; run++) + { + if((compareip[run] & comparemask[run]) != (compareadr[run] & comparemask[run])) + { + differed = qtrue; + break; + } } - } - return qfalse; - } + + if(!differed) + return qtrue; - // Class C - for ( i = 0 ; i < numIP ; i++ ) { - if( adr.ip[0] == localIP[i][0] && adr.ip[1] == localIP[i][1] && adr.ip[2] == localIP[i][2] ) { - return qtrue; } } + return qfalse; } @@ -419,9 +744,16 @@ Sys_ShowIP */ void Sys_ShowIP(void) { int i; + char addrbuf[NET_ADDRSTRMAXLEN]; - for (i = 0; i < numIP; i++) { - Com_Printf( "IP: %i.%i.%i.%i\n", localIP[i][0], localIP[i][1], localIP[i][2], localIP[i][3] ); + for(i = 0; i < numIP; i++) + { + Sys_SockaddrToString(addrbuf, sizeof(addrbuf), (struct sockaddr *) &localIP[i].addr, sizeof((*localIP).addr)); + + if(localIP[i].type == NA_IP) + Com_Printf( "IP: %s\n", addrbuf); + else if(localIP[i].type == NA_IP6) + Com_Printf( "IP6: %s\n", addrbuf); } } @@ -434,45 +766,53 @@ void Sys_ShowIP(void) { NET_IPSocket ==================== */ -int NET_IPSocket( char *net_interface, int port ) { +int NET_IPSocket( char *net_interface, int port, int *err ) { SOCKET newsocket; struct sockaddr_in address; qboolean _true = qtrue; int i = 1; - int err; + + *err = 0; if( net_interface ) { Com_Printf( "Opening IP socket: %s:%i\n", net_interface, port ); } else { - Com_Printf( "Opening IP socket: localhost:%i\n", port ); + Com_Printf( "Opening IP socket: 0.0.0.0:%i\n", port ); } - if( ( newsocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) { - err = socketError; - if( err != EAFNOSUPPORT ) { - Com_Printf( "WARNING: UDP_OpenSocket: socket: %s\n", NET_ErrorString() ); - } - return 0; + if( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) { + *err = socketError; + Com_Printf( "WARNING: NET_IPSocket: socket: %s\n", NET_ErrorString() ); + return newsocket; } - // make it non-blocking if( ioctlsocket( newsocket, FIONBIO, (u_long *)&_true ) == SOCKET_ERROR ) { - Com_Printf( "WARNING: UDP_OpenSocket: ioctl FIONBIO: %s\n", NET_ErrorString() ); - return 0; + Com_Printf( "WARNING: NET_IPSocket: ioctl FIONBIO: %s\n", NET_ErrorString() ); + *err = socketError; + closesocket(newsocket); + return INVALID_SOCKET; } // make it broadcast capable - if( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i) ) == SOCKET_ERROR ) { - Com_Printf( "WARNING: UDP_OpenSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() ); - return 0; + if( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *) &i, sizeof(i) ) == SOCKET_ERROR ) { + Com_Printf( "WARNING: NET_IPSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() ); + + // it is not that bad if this one fails. +// return newsocket; } - if( !net_interface || !net_interface[0] || !Q_stricmp(net_interface, "localhost") ) { + if( !net_interface || !net_interface[0]) { + address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; } - else { - Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address ); + else + { + if(!Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address, sizeof(address), AF_INET)) + { + closesocket(newsocket); + return INVALID_SOCKET; + } } if( port == PORT_ANY ) { @@ -482,17 +822,201 @@ int NET_IPSocket( char *net_interface, int port ) { address.sin_port = htons( (short)port ); } - address.sin_family = AF_INET; + if( bind( newsocket, (void *)&address, sizeof(address) ) == SOCKET_ERROR ) { + Com_Printf( "WARNING: NET_IPSocket: bind: %s\n", NET_ErrorString() ); + *err = socketError; + closesocket( newsocket ); + return INVALID_SOCKET; + } + + return newsocket; +} + +/* +==================== +NET_IP6Socket +==================== +*/ +int NET_IP6Socket( char *net_interface, int port, struct sockaddr_in6 *bindto, int *err ) { + SOCKET newsocket; + struct sockaddr_in6 address; + qboolean _true = qtrue; + + *err = 0; + + if( net_interface ) + { + // Print the name in brackets if there is a colon: + if(Q_CountChar(net_interface, ':')) + Com_Printf( "Opening IP6 socket: [%s]:%i\n", net_interface, port ); + else + Com_Printf( "Opening IP6 socket: %s:%i\n", net_interface, port ); + } + else + Com_Printf( "Opening IP6 socket: [::]:%i\n", port ); + + if( ( newsocket = socket( PF_INET6, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) { + *err = socketError; + Com_Printf( "WARNING: NET_IP6Socket: socket: %s\n", NET_ErrorString() ); + return newsocket; + } + + // make it non-blocking + if( ioctlsocket( newsocket, FIONBIO, (u_long *)&_true ) == SOCKET_ERROR ) { + Com_Printf( "WARNING: NET_IP6Socket: ioctl FIONBIO: %s\n", NET_ErrorString() ); + *err = socketError; + closesocket(newsocket); + return INVALID_SOCKET; + } + +#ifdef IPV6_V6ONLY + { + int i; + + // ipv4 addresses should not be allowed to connect via this socket. + if(setsockopt(newsocket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &i, sizeof(i)) == SOCKET_ERROR) + { + // win32 systems don't seem to support this anyways. + Com_DPrintf("WARNING: NET_IP6Socket: setsockopt IPV6_V6ONLY: %s\n", NET_ErrorString()); + } + } +#endif + + if( !net_interface || !net_interface[0]) { + address.sin6_family = AF_INET6; + address.sin6_addr = in6addr_any; + } + else + { + if(!Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address, sizeof(address), AF_INET6)) + { + closesocket(newsocket); + return INVALID_SOCKET; + } + } + + if( port == PORT_ANY ) { + address.sin6_port = 0; + } + else { + address.sin6_port = htons( (short)port ); + } if( bind( newsocket, (void *)&address, sizeof(address) ) == SOCKET_ERROR ) { - Com_Printf( "WARNING: UDP_OpenSocket: bind: %s\n", NET_ErrorString() ); + Com_Printf( "WARNING: NET_IP6Socket: bind: %s\n", NET_ErrorString() ); + *err = socketError; closesocket( newsocket ); - return 0; + return INVALID_SOCKET; } + + if(bindto) + *bindto = address; return newsocket; } +/* +==================== +NET_SetMulticast +Set the current multicast group +==================== +*/ +void NET_SetMulticast6(void) +{ + struct sockaddr_in6 addr; + + if(!*net_mcast6addr->string || !Sys_StringToSockaddr(net_mcast6addr->string, (struct sockaddr *) &addr, sizeof(addr), AF_INET6)) + { + Com_Printf("WARNING: NET_JoinMulticast6: Incorrect multicast address given, " + "please set cvar %s to a sane value.\n", net_mcast6addr->name); + + Cvar_SetValue(net_enabled->name, net_enabled->integer | NET_DISABLEMCAST); + + return; + } + + memcpy(&curgroup.ipv6mr_multiaddr, &addr.sin6_addr, sizeof(curgroup.ipv6mr_multiaddr)); + + if(*net_mcast6iface->string) + { +#ifdef _WIN32 + curgroup.ipv6mr_interface = atoi(net_mcast6iface->string); +#else + curgroup.ipv6mr_interface = if_nametoindex(net_mcast6iface->string); +#endif + } + else + curgroup.ipv6mr_interface = 0; +} + +/* +==================== +NET_JoinMulticast +Join an ipv6 multicast group +==================== +*/ +void NET_JoinMulticast6(void) +{ + int err; + + if(ip6_socket == INVALID_SOCKET || multicast6_socket != INVALID_SOCKET || (net_enabled->integer & NET_DISABLEMCAST)) + return; + + if(IN6_IS_ADDR_MULTICAST(&boundto.sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(&boundto.sin6_addr)) + { + // The way the socket was bound does not prohibit receiving multi-cast packets. So we don't need to open a new one. + multicast6_socket = ip6_socket; + } + else + { + if((multicast6_socket = NET_IP6Socket(net_mcast6addr->string, ntohs(boundto.sin6_port), NULL, &err)) == INVALID_SOCKET) + { + // If the OS does not support binding to multicast addresses, like WinXP, at least try with the normal file descriptor. + multicast6_socket = ip6_socket; + } + } + + if(curgroup.ipv6mr_interface) + { + if (setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_MULTICAST_IF, + (char *) &curgroup.ipv6mr_interface, sizeof(curgroup.ipv6mr_interface)) < 0) + { + Com_Printf("NET_JoinMulticast6: Couldn't set scope on multicast socket: %s\n", NET_ErrorString()); + + if(multicast6_socket != ip6_socket) + { + closesocket(multicast6_socket); + multicast6_socket = INVALID_SOCKET; + return; + } + } + } + + if (setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *) &curgroup, sizeof(curgroup))) + { + Com_Printf("NET_JoinMulticast6: Couldn't join multicast group: %s\n", NET_ErrorString()); + + if(multicast6_socket != ip6_socket) + { + closesocket(multicast6_socket); + multicast6_socket = INVALID_SOCKET; + return; + } + } +} + +void NET_LeaveMulticast6() +{ + if(multicast6_socket != INVALID_SOCKET) + { + if(multicast6_socket != ip6_socket) + closesocket(multicast6_socket); + else + setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (char *) &curgroup, sizeof(curgroup)); + + multicast6_socket = INVALID_SOCKET; + } +} /* ==================== @@ -559,14 +1083,14 @@ void NET_OpenSocks( int port ) { if ( rfc1929 ) { buf[2] = 2; // method #2 - method id #02: username/password } - if ( send( socks_socket, buf, len, 0 ) == SOCKET_ERROR ) { + if ( send( socks_socket, (void *)buf, len, 0 ) == SOCKET_ERROR ) { err = socketError; Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() ); return; } // get the response - len = recv( socks_socket, buf, 64, 0 ); + len = recv( socks_socket, (void *)buf, 64, 0 ); if ( len == SOCKET_ERROR ) { err = socketError; Com_Printf( "NET_OpenSocks: recv: %s\n", NET_ErrorString() ); @@ -606,14 +1130,14 @@ void NET_OpenSocks( int port ) { } // send it - if ( send( socks_socket, buf, 3 + ulen + plen, 0 ) == SOCKET_ERROR ) { + if ( send( socks_socket, (void *)buf, 3 + ulen + plen, 0 ) == SOCKET_ERROR ) { err = socketError; Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() ); return; } // get the response - len = recv( socks_socket, buf, 64, 0 ); + len = recv( socks_socket, (void *)buf, 64, 0 ); if ( len == SOCKET_ERROR ) { err = socketError; Com_Printf( "NET_OpenSocks: recv: %s\n", NET_ErrorString() ); @@ -636,14 +1160,14 @@ void NET_OpenSocks( int port ) { buf[3] = 1; // address type: IPV4 *(int *)&buf[4] = INADDR_ANY; *(short *)&buf[8] = htons( (short)port ); // port - if ( send( socks_socket, buf, 10, 0 ) == SOCKET_ERROR ) { + if ( send( socks_socket, (void *)buf, 10, 0 ) == SOCKET_ERROR ) { err = socketError; Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() ); return; } // get the response - len = recv( socks_socket, buf, 64, 0 ); + len = recv( socks_socket, (void *)buf, 64, 0 ); if( len == SOCKET_ERROR ) { err = socketError; Com_Printf( "NET_OpenSocks: recv: %s\n", NET_ErrorString() ); @@ -676,161 +1200,106 @@ void NET_OpenSocks( int port ) { NET_GetLocalAddress ===================== */ -#ifdef MACOS_X -// Don't do a forward mapping from the hostname of the machine to the IP. -// The reason is that we might have obtained an IP address from DHCP and -// there might not be any name registered for the machine. On Mac OS X, -// the machine name defaults to 'localhost' and NetInfo has 127.0.0.1 -// listed for this name. Instead, we want to get a list of all the IP -// network interfaces on the machine. This code adapted from -// OmniNetworking. - -#ifdef _SIZEOF_ADDR_IFREQ - // tjw: OSX 10.4 does not have sa_len - #define IFR_NEXT(ifr) \ - ((struct ifreq *) ((char *) ifr + _SIZEOF_ADDR_IFREQ(*ifr))) -#else - // tjw: assume that once upon a time some version did have sa_len - #define IFR_NEXT(ifr) \ - ((struct ifreq *) ((char *) (ifr) + sizeof(*(ifr)) + \ - MAX(0, (int) (ifr)->ifr_addr.sa_len - (int) sizeof((ifr)->ifr_addr)))) -#endif - -void NET_GetLocalAddress( void ) { - struct ifreq requestBuffer[MAX_IPS], *linkInterface, *inetInterface; - struct ifconf ifc; - struct ifreq ifr; - struct sockaddr_dl *sdl; - int interfaceSocket; - int family; - - // Set this early so we can just return if there is an error - numIP = 0; - - ifc.ifc_len = sizeof(requestBuffer); - ifc.ifc_buf = (caddr_t)requestBuffer; - - // Since we get at this info via an ioctl, we need a temporary little socket. - // This will only get AF_INET interfaces, but we probably don't care about - // anything else. If we do end up caring later, we should add a - // ONAddressFamily and at a -interfaces method to it. - family = AF_INET; - if ((interfaceSocket = socket(family, SOCK_DGRAM, 0)) < 0) { - Com_Printf("NET_GetLocalAddress: Unable to create temporary socket, errno = %d\n", errno); +void NET_AddLocalAddress(char *ifname, struct sockaddr *addr, struct sockaddr *netmask) +{ + int addrlen; + sa_family_t family; + + // only add addresses that have all required info. + if(!addr || !netmask || !ifname) return; - } + + family = addr->sa_family; - if (ioctl(interfaceSocket, SIOCGIFCONF, &ifc) != 0) { - Com_Printf("NET_GetLocalAddress: Unable to get list of network interfaces, errno = %d\n", errno); - return; + if(numIP < MAX_IPS) + { + if(family == AF_INET) + { + addrlen = sizeof(struct sockaddr_in); + localIP[numIP].type = NA_IP; + } + else if(family == AF_INET6) + { + addrlen = sizeof(struct sockaddr_in6); + localIP[numIP].type = NA_IP6; + } + else + return; + + Q_strncpyz(localIP[numIP].ifname, ifname, sizeof(localIP[numIP].ifname)); + + localIP[numIP].family = family; + + memcpy(&localIP[numIP].addr, addr, addrlen); + memcpy(&localIP[numIP].netmask, netmask, addrlen); + + numIP++; } +} - linkInterface = (struct ifreq *) ifc.ifc_buf; - while ((char *) linkInterface < &ifc.ifc_buf[ifc.ifc_len]) { - unsigned int nameLength; - - // The ioctl returns both the entries having the address (AF_INET) - // and the link layer entries (AF_LINK). The AF_LINK entry has the - // link layer address which contains the interface type. This is the - // only way I can see to get this information. We cannot assume that - // we will get bot an AF_LINK and AF_INET entry since the interface - // may not be configured. For example, if you have a 10Mb port on - // the motherboard and a 100Mb card, you may not configure the - // motherboard port. - - // For each AF_LINK entry... - if (linkInterface->ifr_addr.sa_family == AF_LINK) { - // if there is a matching AF_INET entry - inetInterface = (struct ifreq *) ifc.ifc_buf; - while ((char *) inetInterface < &ifc.ifc_buf[ifc.ifc_len]) { - if (inetInterface->ifr_addr.sa_family == AF_INET && - !strncmp(inetInterface->ifr_name, linkInterface->ifr_name, - sizeof(linkInterface->ifr_name))) { - - for (nameLength = 0; nameLength < IFNAMSIZ; nameLength++) - if (!linkInterface->ifr_name[nameLength]) - break; - - sdl = (struct sockaddr_dl *)&linkInterface->ifr_addr; - // Skip loopback interfaces - if (sdl->sdl_type != IFT_LOOP) { - // Get the local interface address - strncpy(ifr.ifr_name, inetInterface->ifr_name, sizeof(ifr.ifr_name)); - if (ioctl(interfaceSocket, SIOCGIFADDR, (caddr_t)&ifr) < 0) { - Com_Printf("NET_GetLocalAddress: Unable to get local address " - "for interface '%s', errno = %d\n", inetInterface->ifr_name, errno); - } else { - struct sockaddr_in *sin; - int ip; - - sin = (struct sockaddr_in *)&ifr.ifr_addr; - - ip = ntohl(sin->sin_addr.s_addr); - localIP[ numIP ][0] = (ip >> 24) & 0xff; - localIP[ numIP ][1] = (ip >> 16) & 0xff; - localIP[ numIP ][2] = (ip >> 8) & 0xff; - localIP[ numIP ][3] = (ip >> 0) & 0xff; - Com_Printf( "IP: %i.%i.%i.%i (%s)\n", - localIP[ numIP ][0], localIP[ numIP ][1], - localIP[ numIP ][2], localIP[ numIP ][3], - inetInterface->ifr_name); - numIP++; - } - } - - // We will assume that there is only one AF_INET entry per AF_LINK entry. - // What happens when we have an interface that has multiple IP addresses, or - // can that even happen? - // break; - } - inetInterface = IFR_NEXT(inetInterface); - } +#if defined(__linux__) || defined(MACOSX) || defined(__BSD__) +void NET_GetLocalAddress(void) +{ + struct ifaddrs *ifap, *search; + + if(getifaddrs(&ifap)) + Com_Printf("NET_GetLocalAddress: Unable to get list of network interfaces: %s\n", NET_ErrorString()); + else + { + for(search = ifap; search; search = search->ifa_next) + { + // Only add interfaces that are up. + if(ifap->ifa_flags & IFF_UP) + NET_AddLocalAddress(search->ifa_name, search->ifa_addr, search->ifa_netmask); } - linkInterface = IFR_NEXT(linkInterface); + + freeifaddrs(ifap); + + Sys_ShowIP(); } - - close(interfaceSocket); } #else void NET_GetLocalAddress( void ) { char hostname[256]; - struct hostent *hostInfo; - int error; - char *p; - int ip; - int n; - - if( gethostname( hostname, 256 ) == SOCKET_ERROR ) { - error = socketError; - return; - } + struct addrinfo hint; + struct addrinfo *res = NULL; + struct addrinfo *search; + struct sockaddr_in mask4; + struct sockaddr_in6 mask6; - hostInfo = gethostbyname( hostname ); - if( !hostInfo ) { - error = socketError; + if(gethostname( hostname, 256 ) == SOCKET_ERROR) return; - } - - Com_Printf( "Hostname: %s\n", hostInfo->h_name ); - n = 0; - while( ( p = hostInfo->h_aliases[n++] ) != NULL ) { - Com_Printf( "Alias: %s\n", p ); - } - if ( hostInfo->h_addrtype != AF_INET ) { - return; - } + Com_Printf( "Hostname: %s\n", hostname ); + + memset(&hint, 0, sizeof(hint)); + + hint.ai_family = AF_UNSPEC; + hint.ai_socktype = SOCK_DGRAM; + + if(getaddrinfo(hostname, NULL, &hint, &res)) + return; - numIP = 0; - while( ( p = hostInfo->h_addr_list[numIP] ) != NULL && numIP < MAX_IPS ) { - ip = ntohl( *(int *)p ); - localIP[ numIP ][0] = p[0]; - localIP[ numIP ][1] = p[1]; - localIP[ numIP ][2] = p[2]; - localIP[ numIP ][3] = p[3]; - Com_Printf( "IP: %i.%i.%i.%i\n", ( ip >> 24 ) & 0xff, ( ip >> 16 ) & 0xff, ( ip >> 8 ) & 0xff, ip & 0xff ); - numIP++; + /* On operating systems where it's more difficult to find out the configured interfaces, we'll just assume a + * netmask with all bits set. */ + + memset(&mask4, 0, sizeof(mask4)); + memset(&mask6, 0, sizeof(mask6)); + mask4.sin_family = AF_INET; + memset(&mask4.sin_addr.s_addr, 0xFF, sizeof(mask4.sin_addr.s_addr)); + mask6.sin6_family = AF_INET6; + memset(&mask6.sin6_addr, 0xFF, sizeof(mask6.sin6_addr)); + + // add all IPs from returned list. + for(search = res; search; search = search->ai_next) + { + if(search->ai_family == AF_INET) + NET_AddLocalAddress("", search->ai_addr, (struct sockaddr *) &mask4); + else if(search->ai_family == AF_INET6) + NET_AddLocalAddress("", search->ai_addr, (struct sockaddr *) &mask6); } + + Sys_ShowIP(); } #endif @@ -840,28 +1309,67 @@ NET_OpenIP ==================== */ void NET_OpenIP( void ) { - cvar_t *ip; - int port; int i; + int err; + int port; + int port6; - ip = Cvar_Get( "net_ip", "localhost", CVAR_LATCH ); - port = Cvar_Get( "net_port", va( "%i", PORT_SERVER ), CVAR_LATCH )->integer; + net_ip = Cvar_Get( "net_ip", "0.0.0.0", CVAR_LATCH ); + net_ip6 = Cvar_Get( "net_ip6", "::", CVAR_LATCH ); + net_port = Cvar_Get( "net_port", va( "%i", PORT_SERVER ), CVAR_LATCH ); + net_port6 = Cvar_Get( "net_port6", va( "%i", PORT_SERVER ), CVAR_LATCH ); + + port = net_port->integer; + port6 = net_port6->integer; + + NET_GetLocalAddress(); // automatically scan for a valid port, so multiple // dedicated servers can be started without requiring // a different net_port for each one - for( i = 0 ; i < 10 ; i++ ) { - ip_socket = NET_IPSocket( ip->string, port + i ); - if ( ip_socket ) { - Cvar_SetValue( "net_port", port + i ); - if ( net_socksEnabled->integer ) { - NET_OpenSocks( port + i ); + + if(net_enabled->integer & NET_ENABLEV6) + { + for( i = 0 ; i < 10 ; i++ ) + { + ip6_socket = NET_IP6Socket(net_ip6->string, port6 + i, &boundto, &err); + if (ip6_socket != INVALID_SOCKET) + { + Cvar_SetValue( "net_port6", port6 + i ); + break; + } + else + { + if(err == EAFNOSUPPORT) + break; + } + } + if(ip6_socket == INVALID_SOCKET) + Com_Printf( "WARNING: Couldn't bind to a v6 ip address.\n"); + } + + if(net_enabled->integer & NET_ENABLEV4) + { + for( i = 0 ; i < 10 ; i++ ) { + ip_socket = NET_IPSocket( net_ip->string, port + i, &err ); + if (ip_socket != INVALID_SOCKET) { + Cvar_SetValue( "net_port", port + i ); + + if (net_socksEnabled->integer) + NET_OpenSocks( port + i ); + + break; + } + else + { + if(err == EAFNOSUPPORT) + break; } - NET_GetLocalAddress(); - return; } + + if(ip_socket == INVALID_SOCKET) + Com_Printf( "WARNING: Couldn't bind to a v4 ip address.\n"); } - Com_Printf( "WARNING: Couldn't allocate IP port\n"); } @@ -878,11 +1386,29 @@ static qboolean NET_GetCvars( void ) { modified = qfalse; - if( net_noudp && net_noudp->modified ) { + if( net_enabled && net_enabled->modified ) { modified = qtrue; } - net_noudp = Cvar_Get( "net_noudp", "0", CVAR_LATCH | CVAR_ARCHIVE ); + +#ifdef DEDICATED + // I want server owners to explicitly turn on ipv6 support. + net_enabled = Cvar_Get( "net_enabled", "1", CVAR_LATCH | CVAR_ARCHIVE ); +#else + /* End users have it enabled so they can connect to ipv6-only hosts, but ipv4 will be + * used if available due to ping */ + net_enabled = Cvar_Get( "net_enabled", "3", CVAR_LATCH | CVAR_ARCHIVE ); +#endif + // Some cvars for configuring multicast options which facilitates scanning for servers on local subnets. + if( net_mcast6addr && net_mcast6addr->modified ) { + modified = qtrue; + } + net_mcast6addr = Cvar_Get( "net_mcast6addr", NET_MULTICAST_IP6, CVAR_LATCH | CVAR_ARCHIVE ); + + if( net_mcast6iface && net_mcast6iface->modified ) { + modified = qtrue; + } + net_mcast6iface = Cvar_Get( "net_mcast6iface", "0", CVAR_LATCH | CVAR_ARCHIVE ); if( net_socksEnabled && net_socksEnabled->modified ) { modified = qtrue; @@ -927,8 +1453,8 @@ void NET_Config( qboolean enableNetworking ) { // get any latched changes to cvars modified = NET_GetCvars(); - if( net_noudp->integer ) { - enableNetworking = qfalse; + if( !net_enabled->integer ) { + enableNetworking = 0; } // if enable state is the same and no cvars were modified, we have nothing to do @@ -959,20 +1485,37 @@ void NET_Config( qboolean enableNetworking ) { } if( stop ) { - if ( ip_socket && ip_socket != INVALID_SOCKET ) { + if ( ip_socket != INVALID_SOCKET ) { closesocket( ip_socket ); - ip_socket = 0; + ip_socket = INVALID_SOCKET; + } + + if(multicast6_socket) + { + if(multicast6_socket != ip6_socket) + closesocket(multicast6_socket); + + multicast6_socket = INVALID_SOCKET; } - if ( socks_socket && socks_socket != INVALID_SOCKET ) { + if ( ip6_socket != INVALID_SOCKET ) { + closesocket( ip6_socket ); + ip6_socket = INVALID_SOCKET; + } + + if ( socks_socket != INVALID_SOCKET ) { closesocket( socks_socket ); - socks_socket = 0; + socks_socket = INVALID_SOCKET; } + } - if( start ) { - if (! net_noudp->integer ) { + if( start ) + { + if (net_enabled->integer) + { NET_OpenIP(); + NET_SetMulticast6(); } } } @@ -1033,18 +1576,34 @@ Sleeps msec or until something happens on the network void NET_Sleep( int msec ) { struct timeval timeout; fd_set fdset; + int highestfd = -1; if (!com_dedicated->integer) return; // we're not a server, just run full speed - if (!ip_socket) + if (ip_socket == INVALID_SOCKET && ip6_socket == INVALID_SOCKET) return; if (msec < 0 ) return; FD_ZERO(&fdset); - FD_SET(ip_socket, &fdset); + + if(ip_socket != INVALID_SOCKET) + { + FD_SET(ip_socket, &fdset); + + if(ip_socket > highestfd) + highestfd = ip_socket; + } + if(ip6_socket != INVALID_SOCKET) + { + FD_SET(ip6_socket, &fdset); + + if(ip6_socket > highestfd) + highestfd = ip6_socket; + } + timeout.tv_sec = msec/1000; timeout.tv_usec = (msec%1000)*1000; select(ip_socket+1, &fdset, NULL, NULL, &timeout); diff --git a/src/qcommon/parse.c b/src/qcommon/parse.c index 892f8f4f..2c7595f0 100644 --- a/src/qcommon/parse.c +++ b/src/qcommon/parse.c @@ -1554,11 +1554,7 @@ static int Parse_ExpandBuiltinDefine(source_t *source, token_t *deftoken, define token_t **firsttoken, token_t **lasttoken) { token_t *token; -#ifdef _WIN32 - unsigned long t; // time_t t; //to prevent LCC warning -#else time_t t; -#endif char *curtime; diff --git a/src/qcommon/q_platform.h b/src/qcommon/q_platform.h index e6f50eef..4ce1fb56 100644 --- a/src/qcommon/q_platform.h +++ b/src/qcommon/q_platform.h @@ -176,6 +176,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include <sys/types.h> #include <machine/endian.h> +#ifndef __BSD__ + #define __BSD__ +#endif + #if defined(__FreeBSD__) #define OS_STRING "freebsd" #elif defined(__OpenBSD__) diff --git a/src/qcommon/q_shared.c b/src/qcommon/q_shared.c index f3220f1c..81248644 100644 --- a/src/qcommon/q_shared.c +++ b/src/qcommon/q_shared.c @@ -296,7 +296,7 @@ void COM_ParseError( char *format, ... ) static char string[4096]; va_start (argptr, format); - vsprintf (string, format, argptr); + Q_vsnprintf (string, sizeof(string), format, argptr); va_end (argptr); Com_Printf("ERROR: %s, line %d: %s\n", com_parsename, com_lines, string); @@ -308,7 +308,7 @@ void COM_ParseWarning( char *format, ... ) static char string[4096]; va_start (argptr, format); - vsprintf (string, format, argptr); + Q_vsnprintf (string, sizeof(string), format, argptr); va_end (argptr); Com_Printf("WARNING: %s, line %d: %s\n", com_parsename, com_lines, string); @@ -727,6 +727,28 @@ char* Q_strrchr( const char* string, int c ) return sp; } +qboolean Q_isanumber( const char *s ) +{ +#ifdef Q3_VM + //FIXME: implement + return qfalse; +#else + char *p; + + if( *s == '\0' ) + return qfalse; + + strtod( s, &p ); + + return *p == '\0'; +#endif +} + +qboolean Q_isintegral( float f ) +{ + return (int)f == f; +} + /* ============= Q_strncpyz @@ -922,6 +944,18 @@ char *Q_CleanStr( char *string ) { return string; } +int Q_CountChar(const char *string, char tocount) +{ + int count; + + for(count = 0; *string; string++) + { + if(*string == tocount) + count++; + } + + return count; +} void QDECL Com_sprintf( char *dest, int size, const char *fmt, ...) { int len; @@ -929,7 +963,7 @@ void QDECL Com_sprintf( char *dest, int size, const char *fmt, ...) { char bigbuffer[32000]; // big, but small enough to fit in PPC stack va_start (argptr,fmt); - len = vsprintf (bigbuffer,fmt,argptr); + len = Q_vsnprintf (bigbuffer, sizeof(bigbuffer), fmt,argptr); va_end (argptr); if ( len >= sizeof( bigbuffer ) ) { Com_Error( ERR_FATAL, "Com_sprintf: overflowed bigbuffer" ); @@ -952,20 +986,19 @@ va does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functions. -FIXME: make this buffer size safe someday ============ */ char * QDECL va( char *format, ... ) { va_list argptr; - static char string[2][32000]; // in case va is called by nested functions - static int index = 0; - char *buf; + static char string[2][32000]; // in case va is called by nested functions + static int index = 0; + char *buf; buf = string[index & 1]; index++; va_start (argptr, format); - vsprintf (buf, format,argptr); + Q_vsnprintf (buf, sizeof(*string), format, argptr); va_end (argptr); return buf; @@ -1145,7 +1178,8 @@ void Info_RemoveKey( char *s, const char *key ) { if (!strcmp (key, pkey) ) { - strcpy (start, s); // remove this part + memmove(start, s, strlen(s) + 1); // remove this part + return; } diff --git a/src/qcommon/q_shared.h b/src/qcommon/q_shared.h index 84e2c2b4..b325b49a 100644 --- a/src/qcommon/q_shared.h +++ b/src/qcommon/q_shared.h @@ -28,16 +28,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // A user mod should never modify this file #define PRODUCT_NAME "tremulous" -#define PRODUCT_VERSION "1.1.0" -#ifdef SVN_VERSION -# define Q3_VERSION PRODUCT_NAME " " SVN_VERSION -#else -# define Q3_VERSION PRODUCT_NAME " " PRODUCT_VERSION +#ifdef _MSC_VER +# define PRODUCT_VERSION "1.1.0" #endif #define CLIENT_WINDOW_TITLE "Tremulous " PRODUCT_VERSION #define CLIENT_WINDOW_MIN_TITLE "Tremulous" +#define Q3_VERSION PRODUCT_NAME " " PRODUCT_VERSION #define MAX_TEAMNAME 32 @@ -90,9 +88,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA **********************************************************************/ +#ifdef Q3_VM + #include "../game/bg_lib.h" -#ifndef Q3_VM +typedef int intptr_t; + +#else #include <assert.h> #include <math.h> @@ -104,30 +106,38 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include <ctype.h> #include <limits.h> +// vsnprintf is ISO/IEC 9899:1999 +// abstracting this to make it portable +#ifdef _WIN32 + #define Q_vsnprintf _vsnprintf + #define Q_snprintf _snprintf +#else + #define Q_vsnprintf vsnprintf + #define Q_snprintf snprintf +#endif + +#ifdef _MSC_VER + #include <io.h> + + typedef __int64 int64_t; + typedef __int32 int32_t; + typedef __int16 int16_t; + typedef __int8 int8_t; + typedef unsigned __int64 uint64_t; + typedef unsigned __int32 uint32_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int8 uint8_t; +#else + #include <stdint.h> #endif +#endif + + #include "q_platform.h" //============================================================= -#ifdef Q3_VM - typedef int intptr_t; -#else - #ifndef _MSC_VER - #include <stdint.h> - #else - #include <io.h> - typedef __int64 int64_t; - typedef __int32 int32_t; - typedef __int16 int16_t; - typedef __int8 int8_t; - typedef unsigned __int64 uint64_t; - typedef unsigned __int32 uint32_t; - typedef unsigned __int16 uint16_t; - typedef unsigned __int8 uint8_t; - #endif -#endif - typedef unsigned char byte; typedef enum {qfalse, qtrue} qboolean; @@ -727,6 +737,8 @@ int Q_isprint( int c ); int Q_islower( int c ); int Q_isupper( int c ); int Q_isalpha( int c ); +qboolean Q_isanumber( const char *s ); +qboolean Q_isintegral( float f ); // portable case insensitive compare int Q_stricmp (const char *s1, const char *s2); @@ -745,6 +757,8 @@ void Q_strcat( char *dest, int size, const char *src ); int Q_PrintStrlen( const char *string ); // removes color sequences from string char *Q_CleanStr( char *string ); +// Count the number of char tocount encountered in string +int Q_CountChar(const char *string, char tocount); //============================================= @@ -833,15 +847,19 @@ default values. // nothing outside the Cvar_*() functions should modify these fields! typedef struct cvar_s { - char *name; - char *string; - char *resetString; // cvar_restart will reset to this value - char *latchedString; // for CVAR_LATCH vars - int flags; + char *name; + char *string; + char *resetString; // cvar_restart will reset to this value + char *latchedString; // for CVAR_LATCH vars + int flags; qboolean modified; // set each time the cvar is changed - int modificationCount; // incremented each time the cvar is changed - float value; // atof( string ) - int integer; // atoi( string ) + int modificationCount; // incremented each time the cvar is changed + float value; // atof( string ) + int integer; // atoi( string ) + qboolean validate; + qboolean integral; + float min; + float max; struct cvar_s *next; struct cvar_s *hashNext; } cvar_t; diff --git a/src/qcommon/qcommon.h b/src/qcommon/qcommon.h index 58ec8745..62012914 100644 --- a/src/qcommon/qcommon.h +++ b/src/qcommon/qcommon.h @@ -91,7 +91,7 @@ char *MSG_ReadBigString (msg_t *sb); char *MSG_ReadStringLine (msg_t *sb); float MSG_ReadAngle16 (msg_t *sb); void MSG_ReadData (msg_t *sb, void *buffer, int size); - +int MSG_LookaheadByte (msg_t *msg); void MSG_WriteDeltaUsercmd( msg_t *msg, struct usercmd_s *from, struct usercmd_s *to ); void MSG_ReadDeltaUsercmd( msg_t *msg, struct usercmd_s *from, struct usercmd_s *to ); @@ -134,7 +134,10 @@ typedef enum { NA_BAD, // an address lookup failed NA_LOOPBACK, NA_BROADCAST, - NA_IP + NA_IP, + NA_IP6, + NA_MULTICAST6, + NA_UNSPEC } netadrtype_t; typedef enum { @@ -142,10 +145,12 @@ typedef enum { NS_SERVER } netsrc_t; +#define NET_ADDRSTRMAXLEN 48 // maximum length of an IPv6 address string including trailing '\0' typedef struct { netadrtype_t type; byte ip[4]; + byte ip6[16]; unsigned short port; } netadr_t; @@ -163,8 +168,11 @@ qboolean NET_CompareAdr (netadr_t a, netadr_t b); qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b); qboolean NET_IsLocalAddress (netadr_t adr); const char *NET_AdrToString (netadr_t a); -qboolean NET_StringToAdr ( const char *s, netadr_t *a); +const char *NET_AdrToStringwPort (netadr_t a); +int NET_StringToAdr ( const char *s, netadr_t *a, netadrtype_t family); qboolean NET_GetLoopPacket (netsrc_t sock, netadr_t *net_from, msg_t *net_message); +void NET_JoinMulticast6(void); +void NET_LeaveMulticast6(void); void NET_Sleep(int msec); @@ -252,7 +260,12 @@ enum svc_ops_e { svc_serverCommand, // [string] to be executed by client game module svc_download, // [short] size [size bytes] svc_snapshot, - svc_EOF + svc_EOF, + + // svc_extension follows a svc_EOF, followed by another svc_* ... + // this keeps legacy clients compatible. + svc_extension, + svc_voip, // not wrapped in USE_VOIP, so this value is reserved. }; @@ -265,7 +278,12 @@ enum clc_ops_e { clc_move, // [[usercmd_t] clc_moveNoDelta, // [[usercmd_t] clc_clientCommand, // [string] message - clc_EOF + clc_EOF, + + // clc_extension follows a clc_EOF, followed by another clc_* ... + // this keeps legacy servers compatible. + clc_extension, + clc_voip, // not wrapped in USE_VOIP, so this value is reserved. }; /* @@ -309,6 +327,8 @@ vm_t *VM_Create( const char *module, intptr_t (*systemCalls)(intptr_t *), void VM_Free( vm_t *vm ); void VM_Clear(void); +void VM_Forced_Unload_Start(void); +void VM_Forced_Unload_Done(void); vm_t *VM_Restart( vm_t *vm ); intptr_t QDECL VM_Call( vm_t *vm, int callNum, ... ); @@ -497,6 +517,7 @@ char *Cvar_InfoString_Big( int bit ); // returns an info string containing all the cvars that have the given bit set // in their flags ( CVAR_USERINFO, CVAR_SERVERINFO, CVAR_SYSTEMINFO, etc ) void Cvar_InfoStringBuffer( int bit, char *buff, int buffsize ); +void Cvar_CheckRange( cvar_t *cv, float minVal, float maxVal, qboolean shouldBeIntegral ); void Cvar_Restart_f( void ); @@ -564,6 +585,7 @@ int FS_GetFileList( const char *path, const char *extension, char *listbuf, in int FS_GetModList( char *listbuf, int bufsize ); fileHandle_t FS_FOpenFileWrite( const char *qpath ); +fileHandle_t FS_FOpenFileAppend( const char *filename ); // will properly create any needed paths and deal with seperater character issues int FS_filelength( fileHandle_t f ); @@ -662,6 +684,7 @@ void FS_HomeRemove( const char *homePath ); void FS_FilenameCompletion( const char *dir, const char *ext, qboolean stripExt, void(*callback)(const char *s) ); + /* ============================================================== @@ -689,16 +712,6 @@ MISC ============================================================== */ -// vsnprintf is ISO/IEC 9899:1999 -// abstracting this to make it portable -#ifdef _WIN32 -#define Q_vsnprintf _vsnprintf -#define Q_snprintf _snprintf -#else -#define Q_vsnprintf vsnprintf -#define Q_snprintf snprintf -#endif - // returned by Sys_GetProcessorFeatures typedef enum { @@ -1014,7 +1027,7 @@ void Sys_SetErrorText( const char *text ); void Sys_SendPacket( int length, const void *data, netadr_t to ); qboolean Sys_GetPacket( netadr_t *net_from, msg_t *net_message ); -qboolean Sys_StringToAdr( const char *s, netadr_t *a ); +qboolean Sys_StringToAdr( const char *s, netadr_t *a, netadrtype_t family ); //Does NOT parse port numbers, only base addresses. qboolean Sys_IsLANAddress (netadr_t adr); @@ -1088,14 +1101,18 @@ void Huff_offsetTransmit (huff_t *huff, int ch, byte *fout, int *offset); void Huff_putBit( int bit, byte *fout, int *offset); int Huff_getBit( byte *fout, int *offset); +// don't use if you don't know what you're doing. +int Huff_getBloc(void); +void Huff_setBloc(int _bloc); + +extern huffman_t clientHuffTables; + int Parse_AddGlobalDefine(char *string); int Parse_LoadSourceHandle(const char *filename); int Parse_FreeSourceHandle(int handle); int Parse_ReadTokenHandle(int handle, pc_token_t *pc_token); int Parse_SourceFileAndLine(int handle, char *filename, int *line); -extern huffman_t clientHuffTables; - #define SV_ENCODE_START 4 #define SV_DECODE_START 12 #define CL_ENCODE_START 12 diff --git a/src/qcommon/vm.c b/src/qcommon/vm.c index c9cac7a4..7a38b8db 100644 --- a/src/qcommon/vm.c +++ b/src/qcommon/vm.c @@ -41,6 +41,9 @@ vm_t *currentVM = NULL; vm_t *lastVM = NULL; int vm_debugLevel; +// used by Com_Error to get rid of running vm's before longjmp +static int forced_unload; + #define MAX_VM 3 vm_t vmTable[MAX_VM]; @@ -609,6 +612,19 @@ VM_Free */ void VM_Free( vm_t *vm ) { + if(!vm) { + return; + } + + if(vm->callLevel) { + if(!forced_unload) { + Com_Error( ERR_FATAL, "VM_Free(%s) on running vm", vm->name ); + return; + } else { + Com_Printf( "forcefully unloading %s vm\n", vm->name ); + } + } + if(vm->destroy) vm->destroy(vm); @@ -636,13 +652,16 @@ void VM_Free( vm_t *vm ) { void VM_Clear(void) { int i; for (i=0;i<MAX_VM; i++) { - if ( vmTable[i].dllHandle ) { - Sys_UnloadDll( vmTable[i].dllHandle ); - } - Com_Memset( &vmTable[i], 0, sizeof( vm_t ) ); + VM_Free(&vmTable[i]); } - currentVM = NULL; - lastVM = NULL; +} + +void VM_Forced_Unload_Start(void) { + forced_unload = 1; +} + +void VM_Forced_Unload_Done(void) { + forced_unload = 0; } void *VM_ArgPtr( intptr_t intValue ) { @@ -723,6 +742,7 @@ intptr_t QDECL VM_Call( vm_t *vm, int callnum, ... ) { Com_Printf( "VM_Call( %d )\n", callnum ); } + ++vm->callLevel; // if we have a dll loaded, call it directly if ( vm->entryPoint ) { //rcg010207 - see dissertation at top of VM_DllSyscall() in this file. @@ -766,6 +786,7 @@ intptr_t QDECL VM_Call( vm_t *vm, int callnum, ... ) { r = VM_CallInterpreted( vm, &a.callnum ); #endif } + --vm->callLevel; if ( oldVM != NULL ) currentVM = oldVM; diff --git a/src/qcommon/vm_interpreted.c b/src/qcommon/vm_interpreted.c index 428d56f2..f9071669 100644 --- a/src/qcommon/vm_interpreted.c +++ b/src/qcommon/vm_interpreted.c @@ -377,8 +377,6 @@ int VM_CallInterpreted( vm_t *vm, int *args ) { *(int *)&image[ programStack + 4 ] = 0; // return stack *(int *)&image[ programStack ] = -1; // will terminate the loop on return - vm->callLevel = 0; - VM_Debug(0); // vm_debugLevel=2; @@ -517,7 +515,7 @@ nextInstruction2: if ( programCounter < 0 ) { // system call int r; - int temp; +// int temp; #ifdef DEBUG_VM int stomped; @@ -526,7 +524,7 @@ nextInstruction2: } #endif // save the stack to allow recursive VM entry - temp = vm->callLevel; +// temp = vm->callLevel; vm->programStack = programStack - 4; #ifdef DEBUG_VM stomped = *(int *)&image[ programStack + 4 ]; @@ -559,7 +557,7 @@ nextInstruction2: opStack++; *opStack = r; programCounter = *(int *)&image[ programStack ]; - vm->callLevel = temp; +// vm->callLevel = temp; #ifdef DEBUG_VM if ( vm_debugLevel ) { Com_Printf( "%s<--- %s\n", DEBUGSTR, VM_ValueToSymbol( vm, programCounter ) ); @@ -600,7 +598,7 @@ nextInstruction2: // vm_debugLevel = 2; // VM_StackTrace( vm, programCounter, programStack ); } - vm->callLevel++; +// vm->callLevel++; } #endif goto nextInstruction; @@ -615,7 +613,7 @@ nextInstruction2: #ifdef DEBUG_VM profileSymbol = VM_ValueToFunctionSymbol( vm, programCounter ); if ( vm_debugLevel ) { - vm->callLevel--; +// vm->callLevel--; Com_Printf( "%s<--- %s\n", DEBUGSTR, VM_ValueToSymbol( vm, programCounter ) ); } #endif diff --git a/src/qcommon/vm_local.h b/src/qcommon/vm_local.h index 59725a11..e1545d37 100644 --- a/src/qcommon/vm_local.h +++ b/src/qcommon/vm_local.h @@ -157,7 +157,7 @@ struct vm_s { int numSymbols; struct vmSymbol_s *symbols; - int callLevel; // for debug indenting + int callLevel; // counts recursive VM_Call int breakFunction; // increment breakCount on function entry to this int breakCount; diff --git a/src/qcommon/vm_x86_64.c b/src/qcommon/vm_x86_64.c index 0e56d1fc..0eb79306 100644 --- a/src/qcommon/vm_x86_64.c +++ b/src/qcommon/vm_x86_64.c @@ -235,7 +235,7 @@ void emit(const char* fmt, ...) va_list ap; char line[4096]; va_start(ap, fmt); - vsnprintf(line, sizeof(line), fmt, ap); + Q_vsnprintf(line, sizeof(line), fmt, ap); va_end(ap); assemble_line(line, strlen(line)); } @@ -1070,7 +1070,6 @@ int VM_CallCompiled( vm_t *vm, int *args ) { currentVM = vm; - ++vm->callLevel; // Com_Printf("entering %s level %d, call %d, arg1 = 0x%x\n", vm->name, vm->callLevel, args[0], args[1]); // interpret the code @@ -1132,7 +1131,6 @@ int VM_CallCompiled( vm_t *vm, int *args ) { } // Com_Printf("exiting %s level %d\n", vm->name, vm->callLevel); - --vm->callLevel; vm->programStack = stackOnEntry; return *(int *)opStack; |