Skip to content

Commit

Permalink
ADD: support for UDP connectionless packets: "status" only, at the mo…
Browse files Browse the repository at this point in the history
…ment.

ADD: serverinfo command and *version variable.
Show hostname/maxclients/*version in serverinfo.
  • Loading branch information
qqshka committed Mar 24, 2012
1 parent 259ea32 commit e17ff37
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 93 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# $Id: Makefile 58 2007-10-22 20:40:31Z d3urk $
EXTRACFLAGS=-Wall -O2
CC=gcc $(EXTRACFLAGS)
STRIP=strip
Expand All @@ -7,7 +6,7 @@ STRIPFLAGS=--strip-unneeded --remove-section=.comment

OBJS = cmd.o crc.o cvar.o forward.o forward_pending.o info.o main.o mdfour.o \
msg.o net_utils.o parse.o qw.o source.o source_cmds.o sys.o build.o token.o httpsv.o httpsv_generate.o \
cl_cmds.o fs.o ban.o
cl_cmds.o fs.o ban.o udp.o

qtv: $(OBJS) qtv.h qconst.h
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $@.db -lm
Expand Down
4 changes: 2 additions & 2 deletions ban.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ SV_FilterPacket
*/
qbool SV_IsBanned (netadr_t *addr)
{
int i;
unsigned in;
int i;
unsigned int in;

in = (((struct sockaddr_in *)addr)->sin_addr.s_addr); // FIXME: wonder is this work...

Expand Down
5 changes: 1 addition & 4 deletions cl_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,8 @@ void Proxy_ReadInput(sv_t *qtv, oproxy_t *prox)
if (prox->inbuffersize < 2)
return; // we need at least size

buf.data = (char *)prox->inbuffer;
InitNetMsg(&buf, (char *)prox->inbuffer, prox->inbuffersize);
buf.cursize = prox->inbuffersize;
buf.maxsize = prox->inbuffersize;
buf.readpos = 0;
buf.startpos = 0;

parse_end = 0;

Expand Down
6 changes: 3 additions & 3 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ void Cmd_Alias_f (void)
}

#if 0
if ( (var = Cvar_FindVar(s)) != NULL )
if ( (var = Cvar_Find(s)) != NULL )
{
if (var->flags & CVAR_USER_CREATED)
Cvar_Delete (var->name);
Expand Down Expand Up @@ -689,7 +689,7 @@ void Cmd_AddCommand (char *cmd_name, xcommand_t function)
Sys_Error ("Cmd_AddCommand after host_initialized");

// fail if the command is a variable name
if (Cvar_FindVar(cmd_name))
if (Cvar_Find(cmd_name))
{
Sys_Printf("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
return;
Expand Down Expand Up @@ -794,7 +794,7 @@ void Cmd_ExpandString (char *data, char *dest)
data++;
buf[i++] = c;
buf[i] = 0;
if ( (var = Cvar_FindVar(buf)) != NULL )
if ( (var = Cvar_Find(buf)) != NULL )
bestvar = var;

if (i >= (int)sizeof(buf)-1)
Expand Down
39 changes: 16 additions & 23 deletions cvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ static int Key (char *name)

/*
============
Cvar_FindVar
Cvar_Find
============
*/
cvar_t *Cvar_FindVar (char *var_name)
cvar_t *Cvar_Find (char *var_name)
{
cvar_t *var;
int key;
Expand All @@ -74,7 +74,7 @@ float Cvar_Value (char *var_name)
{
cvar_t *var;

var = Cvar_FindVar (var_name);
var = Cvar_Find (var_name);
if (!var)
return 0;
return atof (var->string);
Expand All @@ -90,18 +90,12 @@ char *Cvar_String (char *var_name)
{
cvar_t *var;

var = Cvar_FindVar (var_name);
var = Cvar_Find (var_name);
if (!var)
return cvar_null_string;
return var->string;
}

void SV_SendServerInfoChange(char *key, char *value)
{
Sys_Printf("SV_SendServerInfoChange FIXME\n");
}


/*
============
Cvar_Set
Expand All @@ -115,6 +109,10 @@ void Cvar_Set (cvar_t *var, char *value)
if (!var)
return;

// force serverinfo "0" vars to be "".
if ((var->flags & CVAR_SERVERINFO) && !strcmp(value, "0"))
value = "";

if (var->flags & CVAR_ROM)
return;

Expand All @@ -141,12 +139,7 @@ void Cvar_Set (cvar_t *var, char *value)

if (var->flags & CVAR_SERVERINFO)
{
char buf[MAX_INFO_KEY];
if (strcmp(var->string, Info_ValueForKey (g_cluster.info, var->name, buf, sizeof(buf))))
{
Info_SetValueForStarKey (g_cluster.info, var->name, var->string, sizeof(g_cluster.info));
SV_SendServerInfoChange(var->name, var->string);
}
SV_ServerinfoChanged (var->name, var->string);
}
}

Expand Down Expand Up @@ -177,7 +170,7 @@ void Cvar_SetByName (char *var_name, char *value)
{
cvar_t *var;

var = Cvar_FindVar (var_name);
var = Cvar_Find (var_name);
if (!var)
{ // there is an error in C code if this happens
Sys_Printf("Cvar_Set: variable %s not found\n", var_name);
Expand Down Expand Up @@ -232,7 +225,7 @@ void Cvar_Register (cvar_t *variable)
char *tmp;

// first check to see if it has already been defined
if (Cvar_FindVar (variable->name))
if (Cvar_Find (variable->name))
{
Sys_Printf("Can't register variable %s, already defined\n", variable->name);
return;
Expand Down Expand Up @@ -277,7 +270,7 @@ qbool Cvar_Command (void)
char string[1024];

// check variables
v = Cvar_FindVar (Cmd_Argv(0));
v = Cvar_Find (Cmd_Argv(0));
if (!v)
return false;

Expand Down Expand Up @@ -316,7 +309,7 @@ void Cvar_Toggle_f (void)
return;
}

var = Cvar_FindVar (Cmd_Argv(1));
var = Cvar_Find (Cmd_Argv(1));
if (!var)
{
Sys_Printf("Unknown variable \"%s\"\n", Cmd_Argv(1));
Expand Down Expand Up @@ -357,7 +350,7 @@ cvar_t *Cvar_Create (char *name, char *string, int cvarflags)
cvar_t *v;
int key;

v = Cvar_FindVar(name);
v = Cvar_Find(name);
if (v)
return v;
v = (cvar_t *) Sys_malloc (sizeof(cvar_t));
Expand Down Expand Up @@ -459,7 +452,7 @@ void Cvar_Set_f (void)
}

var_name = Cmd_Argv (1);
var = Cvar_FindVar (var_name);
var = Cvar_Find (var_name);

if (var)
{
Expand Down Expand Up @@ -495,7 +488,7 @@ void Cvar_Inc_f (void)
return;
}

var = Cvar_FindVar (Cmd_Argv(1));
var = Cvar_Find (Cmd_Argv(1));
if (!var)
{
Sys_Printf("Unknown variable \"%s\"\n", Cmd_Argv(1));
Expand Down
2 changes: 1 addition & 1 deletion cvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ qbool Cvar_Command (void);
// command. Returns true if the command was a variable reference that
// was handled. (print or change)

cvar_t *Cvar_FindVar (char *var_name);
cvar_t *Cvar_Find (char *var_name);
qbool Cvar_Delete (char *name);

cvar_t *Cvar_Create (char *name, char *string, int cvarflags);
Expand Down
4 changes: 4 additions & 0 deletions dotnet2005/qtvprox.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@
RelativePath="..\token.c"
>
</File>
<File
RelativePath="..\udp.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
Expand Down
58 changes: 38 additions & 20 deletions forward_pending.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "qtv.h"

cvar_t mvdport = {"mvdport", PROX_DEFAULT_LISTEN_PORT};
cvar_t maxclients = {"maxclients", "1000"};
cvar_t maxclients = {"maxclients", "1000", CVAR_SERVERINFO};
cvar_t allow_http = {"allow_http", "1"};

int get_maxclients(void)
Expand Down Expand Up @@ -734,7 +734,7 @@ void SV_FindProxies(SOCKET qtv_sock, cluster_t *cluster)
if (SV_IsBanned(&addr))
{
char ip[] = "xxx.xxx.xxx.xxx";
Sys_DPrintf("rejected connect from banned ip: %s\n", NET_BaseAdrToString(&addr, ip, sizeof(ip)));
Sys_DPrintf("rejected connect from banned ip: %s\n", Net_BaseAdrToString(&addr, ip, sizeof(ip)));
closesocket(sock);
return;
}
Expand Down Expand Up @@ -766,19 +766,8 @@ void SV_FindProxies(SOCKET qtv_sock, cluster_t *cluster)
cluster->pendingproxies = prox;
}

void SV_CheckMVDPort(cluster_t *cluster)
static void SV_CheckMVDPort(cluster_t *cluster, int port)
{
int newp = bound(0, mvdport.integer, 65534);

// Lets check if we should do some action with socket. open/close.
if ( (cluster->tcpsocket != INVALID_SOCKET && !newp) // we should close socket.
|| (cluster->tcpsocket == INVALID_SOCKET && newp && (cluster->mvdport_last_time_check + 30 * 1000 < cluster->curtime)) // we should open socket.
)
mvdport.modified = true; // force open/close.

if (!mvdport.modified)
return; // nothing was changed.

// remember current time so we does not hammer it too fast.
cluster->mvdport_last_time_check = cluster->curtime;

Expand All @@ -788,27 +777,56 @@ void SV_CheckMVDPort(cluster_t *cluster)
closesocket(cluster->tcpsocket);
cluster->tcpsocket = INVALID_SOCKET;

Sys_Printf("mvdport is now closed\n");
Sys_Printf("%s is now closed\n", mvdport.name);
}

// open socket if required.
if (newp)
if (port)
{
SOCKET news = Net_TCPListenPort(newp);
SOCKET news = Net_TCPListenPort(port);

if (news != INVALID_SOCKET)
{
cluster->tcpsocket = news;

Sys_Printf("mvdport %d opened\n", newp);
Sys_Printf("%s %d opened\n", mvdport.name, port);
}
else
{
Sys_Printf("mvdport %d failed to open\n", newp);
Sys_Printf("%s %d failed to open\n", mvdport.name, port);
}
}
}

void SV_CheckNETPorts(cluster_t *cluster)
{
qbool tcpport_modified = false;
qbool udpport_modified = false;
int newp = bound(0, mvdport.integer, 65534);

// Lets check if we should do some action with TCP socket. open/close.
if ( (cluster->tcpsocket != INVALID_SOCKET && !newp) // we should close socket.
|| (cluster->tcpsocket == INVALID_SOCKET && newp && (cluster->mvdport_last_time_check + 30 * 1000 < cluster->curtime)) // we should open socket.
)
tcpport_modified = true; // force open/close.

// Lets check if we should do some action with UDP socket. open/close.
if ( (cluster->udpsocket != INVALID_SOCKET && !newp) // we should close socket.
|| (cluster->udpsocket == INVALID_SOCKET && newp && (cluster->udpport_last_time_check + 30 * 1000 < cluster->curtime)) // we should open socket.
)
udpport_modified = true; // force open/close.

if (mvdport.modified)
tcpport_modified = udpport_modified = true;

if (tcpport_modified)
SV_CheckMVDPort(cluster, newp);

// As long as we use same port number for UPD/TCP I have to call SV_CheckUDPPort() from here.
if (udpport_modified)
SV_CheckUDPPort(cluster, newp);

// we apply all we want, if we dont' then repeat it later (look mvdport_last_time_check).
// we apply all we want, if we don't then repeat it later.
mvdport.modified = false;
}

Expand Down
55 changes: 55 additions & 0 deletions info.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ Manipulation with various info strings

#include "qtv.h"

//Cvar system calls this when a CVAR_SERVERINFO cvar changes
void SV_ServerinfoChanged (char *key, char *string)
{
char value[MAX_INFO_KEY] = "";

// force serverinfo "0" vars to be "".
if (!strcmp(string, "0"))
string = "";

if (strcmp(string, Info_ValueForKey (g_cluster.info, key, value, sizeof(value))))
{
Info_SetValueForStarKey (g_cluster.info, key, string, MAX_SERVERINFO_STRING);
}
}

char *Info_ValueForKey (const char *s, const char *const key, char *const buffer, size_t buffersize)
{
size_t keylen = strlen(key);
Expand Down Expand Up @@ -165,8 +180,48 @@ void Info_SetValueForStarKey (char *s, const char *key, const char *value, int m
*s = 0;
}

void Info_Print (char *s)
{
char key[512];
char value[512];
char *o;
int l;

if (*s == '\\')
s++;
while (*s)
{
o = key;
while (*s && *s != '\\')
*o++ = *s++;

l = o - key;
if (l < 20)
{
memset (o, ' ', 20-l);
key[20] = 0;
}
else
*o = 0;
Sys_Printf ("%s ", key);

if (!*s)
{
Sys_Printf ("MISSING VALUE\n");
return;
}

o = value;
s++;
while (*s && *s != '\\')
*o++ = *s++;
*o = 0;

if (*s)
s++;
Sys_Printf ("%s\n", value);
}
}

//============================================================
// Alternative variant manipulation with info strings
Expand Down
Loading

0 comments on commit e17ff37

Please sign in to comment.