1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/* $Id: metaserver.c,v 1.4 2006/04/04 21:49:19 nordi Exp $ */
/* Tenes Empanadas Graciela
*
* Copyright (C) 2000 Ricardo Quesada
*
* Author: Ricardo Calixto Quesada <riq@corest.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; only version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* client side of the metaserver
*/
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <unistd.h>
#include <string.h>
#include "client.h"
LIST_ENTRY g_list_metaserver;
/* metaserver helpers */
static void metaserver_flush_list()
{
PLIST_ENTRY tmp;
while( !IsListEmpty( &g_list_metaserver ) ) {
tmp = RemoveHeadList( &g_list_metaserver );
free( tmp );
}
return;
}
static TEG_STATUS parse_server_entry(xmlDocPtr doc, xmlNodePtr cur)
{
char *name=NULL, *port=NULL, *version=NULL, *comment=NULL;
PMETASERVER entry = NULL;
cur = xml_get_element_children( cur );
while (cur != NULL) {
/* name */
if (!xmlStrcmp(cur->name, (const xmlChar *) "name") ) {
name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if( ! name ) {
textmsg( M_ERR, "Metaserver XML: 'entry' has no 'name'\n");
goto error;
}
}
/* port */
else if (!xmlStrcmp(cur->name, (const xmlChar *) "port") ) {
port = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if( ! port ) {
textmsg( M_ERR, "Metaserver XML: 'entry' has no 'port'\n");
goto error;
}
}
/* version */
else if (!xmlStrcmp(cur->name, (const xmlChar *) "version") ) {
version = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if( ! version ) {
textmsg( M_ERR, "Metaserver XML: 'entry' has no 'version'\n");
goto error;
}
}
/* comment */
else if (!xmlStrcmp(cur->name, (const xmlChar *) "comment") ) {
comment = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if( ! comment ) {
textmsg( M_ERR, "Metaserver XML: 'entry' has no 'comment'\n");
goto error;
}
}
cur = xml_get_element_next( cur );
}
if( !name || !port || !version || !comment )
goto error;
entry = malloc (sizeof (*entry) );
if( ! entry )
goto error;
memset( entry, 0, sizeof(*entry) );
strncpy( entry->name, name, META_MAX_NAME );
strncpy( entry->version, version, META_MAX_NAME );
strncpy( entry->comment, comment, META_MAX_COMMENT );
entry->port = atoi( port );
InsertTailList( &g_list_metaserver, (PLIST_ENTRY) entry );
return TEG_STATUS_SUCCESS;
error:
if( entry )
free( entry );
return TEG_STATUS_ERROR;
}
static TEG_STATUS metaserver_parse_xml( xmlDocPtr doc )
{
xmlNodePtr cur;
char *theme_version;
/*
* Check the document is of the right kind
*/
cur = xmlDocGetRootElement(doc);
if (xmlStrcmp(cur->name, (const xmlChar *) "teg_metaserver")) {
fprintf(stderr,"Wrong type. root node != teg_metaserver\n");
goto error;
}
/* find metaserver version */
theme_version = xmlGetProp(cur, (const xmlChar *) "version");
if ( ! theme_version ) {
fprintf(stderr, "XML: 'teg_metaserver' has no 'version'\n");
goto error;
}
/* compare versions */
{
int ver_major, ver_minor;
char *ret;
ret = strchr( theme_version, '.' );
if( ret ) {
ver_minor = atoi( ret+1);
*ret = 0;
} else
ver_minor = 0;
ver_major = atoi( theme_version );
#ifdef _DEBUG
fprintf( stderr, "major:%d, minor: %d\n", ver_major, ver_minor );
#endif
if( ver_major != META_VERSION_MAJOR || ver_minor > META_VERSION_MINOR )
{
fprintf(stderr, "XML: The version of the XML is incompatible.\n . Supported metaserver version are %d.x where x is <= %d\n . Current metaserver version is:%d.%d\n"
,META_VERSION_MAJOR
,META_VERSION_MINOR
,ver_major
,ver_minor );
goto error;
}
}
/* Now, walk the tree */
cur = xml_get_element_children( cur );
if ( cur == NULL )
goto error;
while (cur != NULL) {
/* entry */
if (!xmlStrcmp(cur->name, (const xmlChar *) "entry") ) {
if( parse_server_entry( doc, cur ) != TEG_STATUS_SUCCESS )
goto error;
} else {
fprintf(stderr,"Metaserver XML: Wrong type (%s). 'entry' was expected\n", cur->name);
goto error;
}
cur = xml_get_element_next( cur );
}
return TEG_STATUS_SUCCESS;
error:
return TEG_STATUS_ERROR;
}
TEG_STATUS metaserver_init()
{
InitializeListHead( &g_list_metaserver );
return TEG_STATUS_SUCCESS;
}
TEG_STATUS metaserver_get_servers( void )
{
int s = -1;
int ret;
xmlParserCtxtPtr ctxt;
xmlDocPtr doc = NULL;
metaserver_flush_list();
s = net_connect_tcp("teg.game-server.cc",2002);
if( s < 0 )
goto error;
if( net_printf( s, "GET /listServers_xml HTTP/1.1\r\n\r\n") < 0 )
goto error;
ctxt = xmlCreateIOParserCtxt(NULL, NULL,
(xmlInputReadCallback) net_read,
(xmlInputCloseCallback) net_close,
(void *)s, XML_CHAR_ENCODING_NONE);
xmlParseDocument(ctxt);
ret = ctxt->wellFormed;
doc = ctxt->myDoc;
xmlFreeParserCtxt(ctxt);
if (!ret)
goto error;
if( metaserver_parse_xml( doc ) != TEG_STATUS_SUCCESS )
goto error;
return TEG_STATUS_SUCCESS;
error:
if( doc )
xmlFreeDoc(doc);
if( s >= 0 )
net_close( s );
return TEG_STATUS_ERROR;
}
|