Project: g15etqw
Code Location: https://code.launchpad.net/~ushimitsudoki/g15etqw/trunk
Browse
gamestat/
Download File
gamespy.c
/*
	Copyright (c) 2004 Cory Nelson

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gamestat.h"
#include "internal.h"

typedef struct {
	char *data;
	size_t len;
} GS_PACKETDATA;

// handles split packets.
static char *gs_getpacket(SOCKET s, size_t *len, clock_t *ping) {
	GS_PACKETDATA *packets=calloc(1, sizeof(GS_PACKETDATA));
	size_t count=1, recieved=0;
	char done=0;
	unsigned char gotping=0;

	*len=0;

	while(1) {
		size_t packetlen;
		char *packet=getpacket(s, &packetlen);
		char *queryidstr;
		int queryid;

		if(!packet) {
			size_t i;

			for(i=0; i<count; i++)
				if(packets[i].data) free(packets[i].data);
			free(packets);

			return NULL;
		}

		if(!gotping) {
			*ping=clock()-*ping;
			gotping=1;
		}

		queryidstr=strstr(packet, "queryid\\");
		if(!queryidstr){ 
			size_t i;

			for(i=0; i<count; i++)
				if(packets[i].data) free(packets[i].data);
			free(packets);

			return NULL;
		}

		queryidstr=strchr(queryidstr, '\\')+1;
		sscanf(queryidstr, "%*d.%d", &queryid);

		if(((size_t)queryid)>count) {
			count=queryid;
			packets=realloc(packets, count*sizeof(GS_PACKETDATA));
		}

		queryid--;
		packets[queryid].data=packet;
		packets[queryid].len=packetlen;
		(*len)+=packetlen;

		recieved++;

		if(done || strstr(packet, "\\final\\")) {
			size_t i;

			done=1;
			if(recieved!=count) continue;

			packet=malloc(*len);

			for(i=0; i<count; i++) {
				size_t j, offset=0;
				for(j=0; j<i; j++) offset+=packets[j].len;

				memcpy(packet+offset, packets[i].data, packets[i].len);
				free(packets[i].data);
			}
			free(packets);

			return packet;
		}
	}
}

static char *gs_parsestring(char **data) {
	char *end;

	(*data)++;

	end=strchr(*data, '\\');

	if(end) {
		size_t len=end-(*data);
		if(len>0) {
			char *str=malloc(len+1);

			memcpy(str, *data, len);
			str[len]='\0';

			*data=end;
			return str;
		}
		else return NULL;
	}
	else {
		size_t len=strlen(*data);
		if(len>0) {
			char *str=malloc(len+1);

			memcpy(str, *data, len);
			str[len]=0;

			(*data)+=len;
			return str;
		}
		else return NULL;
	}
}

GS_SERVERINFO *queryserver_gamespy(const char *host, unsigned short port) {
	SOCKET s=getsockudp(host, port);
	size_t packetlen;
	char *packet;
	GS_SERVERINFO *info;
	GS_PLAYERINFO **players, *curplayer=NULL;
	GS_PROPERTY *curproperty=NULL;
	char *iter, *end;
	int i, numplayers=0;
	clock_t ping;
	
	if(s==INVALID_SOCKET) return NULL;

	ping=clock();
	//if(send(s, "\\info\\\\rules\\\\players\\", 22, 0)==SOCKET_ERROR) {
	if(send(s, "\\echo\\CORY\\\\info\\\\rules\\\\players\\", 33, 0)==SOCKET_ERROR) {
		lasterror=GS_ERROR_TIMEOUT;
		closesocket(s);
		return NULL;
	}
	packet=gs_getpacket(s, &packetlen, &ping);
	if(!packet) {
		lasterror=GS_ERROR_TIMEOUT;
		closesocket(s);
		return NULL;
	}
	closesocket(s);

	dbg_dumpbuf("gamespy.bin", packet, packetlen);

	iter=packet;
	end=packet+packetlen;

	if(*iter == '\\') {
		char *echo=strstr(iter, "\\echo\\");
		if(!echo) echo=strstr(iter, "\\echoresponse\\");

		if(echo) {
			char *value=strchr(echo+1, '\\')+1;

			if(memcmp(value, "CORY", 4)) {
				lasterror=GS_ERROR_CORRUPT;
				free(packet);
				return NULL;
			}
		}
		else {
			lasterror=GS_ERROR_CORRUPT;
			free(packet);
			return NULL;
		}
	}
	
	info=calloc(1, sizeof(GS_SERVERINFO));
	info->ping=(int)ping;

	while(iter<end && *iter == '\\') {
		GS_PROPERTY *property;
		char *key=gs_parsestring(&iter), *value;
		int index;

		if(!key || iter>=end) break;

		value=gs_parsestring(&iter);

		if(!strcmp(key, "final")) {
			free(key);
			break;
		}
		else if(value) {
			if(!info->host && !stricmp(key, "hostname")) info->host=strdup(value);
			else if(!info->mod && (!stricmp(key, "mod") || !stricmp(key, "gametype"))) info->mod=strdup(value);
			else if(!info->map && !stricmp(key, "mapname")) info->map=strdup(value);
			else if(!stricmp(key, "numplayers")) info->numplayers=atoi(value);
			else if(!stricmp(key, "maxplayers")) info->maxplayers=atoi(value);
			else if(!stricmp(key, "timelimit") || !stricmp(key, "time_limit") || !stricmp(key, "NetEndTime")) info->timelimit=atoi(value);
			else if(!stricmp(key, "fraglimit") || !stricmp(key, "scorelimit") || !stricmp(key, "NetEndScore")) info->scorelimit=atoi(value);
			else if(!stricmp(key, "password")) info->passworded=(strcmp(value, "True")==0 || atoi(value)==1);
			else if(sscanf(key, "%*[^_]_%d", &index)==1) {
				char *tmp=strchr(key, '_');
				if(tmp) *tmp='\0';

				if(!numplayers) {
					numplayers=index+1;
					players=malloc(numplayers*sizeof(GS_PLAYERINFO*));

					for(i=0; i<numplayers; i++)
						players[i]=calloc(1, sizeof(GS_PLAYERINFO));
				}
				else if((index+1)>=numplayers) {
					int oldnum=numplayers;

					numplayers=index+1;
					players=realloc(players, numplayers*sizeof(GS_PLAYERINFO*));

					for(i=oldnum; i<numplayers; i++)
						players[i]=calloc(1, sizeof(GS_PLAYERINFO));
				}

				if((!stricmp(key, "player") || !stricmp(key, "playername")) && !players[index]->name) players[index]->name=value;
				else if(!stricmp(key, "score") || !stricmp(key, "frags") || !stricmp(key, "kills")) {
					players[index]->score=atoi(value);
					free(value);
				}
				else if(!strcmp(key, "ping")) {
					players[index]->ping=atoi(value);
					free(value);
				}
				else free(value);

				free(key);
				continue;
			}
		}

		property=malloc(sizeof(GS_PROPERTY));
		property->key=key;
		property->value=value;
		property->next=NULL;

		if(curproperty) curproperty=curproperty->next=property;
		else curproperty=info->properties=property;
	}
	free(packet);

	for(i=0; i<numplayers; i++) {
		if(players[i]->name) {
			if(curplayer) curplayer=curplayer->next=players[i];
			else curplayer=info->players=players[i];
		}
	}
	if(numplayers>0) free(players);

	lasterror=GS_OK;
	return info;
}