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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/********************************************************************/
/* */
/* L I QQ U U I DD W W A RR 555 */
/* L I Q Q U U I D D W W A A R R 5 */
/* L I Q Q U U I D D W W W AAA RR 55 */
/* L I Q Q U U I D D WW WW A A R R 5 */
/* LLL I Q Q U I DD W W A A R R 55 */
/* */
/* b */
/* bb y y */
/* b b yyy */
/* bb y */
/* yy */
/* */
/* U U FFF O O TTT */
/* U U F O O O O T */
/* U U TIRET FF O O O O T */
/* U U F O O O O T */
/* U F O O T */
/* */
/********************************************************************/
/*****************************************************************************/
/* Liquid War is a multiplayer wargame */
/* Copyright (C) 1998-2007 Christian Mauduit */
/* */
/* 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; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* */
/* Liquid War homepage : http://www.ufoot.org/liquidwar/v5 */
/* Contact author : ufoot@ufoot.org */
/*****************************************************************************/
/********************************************************************/
/* name : wwwcli.c */
/* content : client code used to contact www.ufoot.org */
/********************************************************************/
/*==================================================================*/
/* includes */
/*==================================================================*/
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "wwwcli.h"
#include "startup.h"
#include "log.h"
#include "httputil.h"
#include "init.h"
#include "server.h"
#include "macro.h"
#include "ping.h"
/*==================================================================*/
/* constants */
/*==================================================================*/
#define LW_WWWCLI_HTTP_CONTENT_SIZE 100000
#define LW_WWWCLI_HTTP_URL_SIZE (URL_SIZE + 200)
#define LW_WWWCLI_URL_LIST "list.php3"
#define LW_WWWCLI_TEMP_BUFFER_SIZE 100
/*
* If a line is shorter than this, we consider it as an empty
* line, this avoids errors with useless stupid malformatted lines
*/
#define LW_WWWCLI_MIN_LINE_SIZE 20
/*==================================================================*/
/* static functions */
/*==================================================================*/
static int parse_line (LW_WWWCLI_DATA * result, char **str);
static void skip_space (char **str);
static int read_string (char *result, char **str, int size);
static int read_int (int *result, char **str);
static int read_separator (char **str);
static int read_eol (char **str);
static int read_eol_n (char **str, int n);
static void next_line (char **str);
/*==================================================================*/
/* global functions */
/*==================================================================*/
/*------------------------------------------------------------------*/
/*
* Main function of the server thread which tries to connect
* on the metaserver
*/
int
lw_wwwcli_get_games (LW_WWWCLI_DATA * buffer, int *size)
{
int result = 0;
char buffer_content[LW_WWWCLI_HTTP_CONTENT_SIZE];
char buffer_url[LW_WWWCLI_HTTP_URL_SIZE];
char *pos, *pos_tmp;
int i = 0;
log_print_str ("Getting server list from \"");
log_print_str (STARTUP_METASERVER);
log_print_str ("\" ");
memset (&buffer_url, 0, sizeof (buffer_url));
LW_MACRO_SPRINTF3 (buffer_url,
"%s%s?protocol=%s&game=" LW_PROGRAM "&version="
LW_VERSION, STARTUP_METASERVER, LW_WWWCLI_URL_LIST,
LW_SERVER_METASERVER_PROTOCOL);
if (lw_httputil_get_page
(buffer_content, buffer_url, sizeof (buffer_content)))
{
pos = buffer_content;
while (read_eol (&pos));
while (i < (*size) && strlen (pos) > 0)
{
pos_tmp = pos;
if (read_eol_n (&pos_tmp, LW_WWWCLI_MIN_LINE_SIZE))
{
/*
* This was just an empty or too short line...
*/
pos = pos_tmp;
}
else
{
/*
* We parse the line only if there's something on it...
*/
if (parse_line (buffer + i, &pos))
{
if (buffer[i].busy_players < buffer[i].max_players)
{
/*
* we increment i only if there's some room left on the
* server. If it's full of people playing on it, it's
* of absolutely no use to present it as a working
* server... And not incrementing i will make it be
* overwritten by the next server.
*/
++i;
log_print_str ("+");
}
else
{
log_print_str ("=");
}
}
else
{
log_print_str ("-");
}
}
}
result = 1;
}
if (result == 1)
{
(*size) = i;
}
else
{
(*size) = 0;
}
display_success (result);
if (!result)
{
log_print_str ("Error: ");
log_println_str (buffer_content);
}
return result;
}
/*------------------------------------------------------------------*/
/*
* Parses a line returned by the meta-server
*/
static int
parse_line (LW_WWWCLI_DATA * result, char **str)
{
int ok = 0;
char temp[LW_WWWCLI_TEMP_BUFFER_SIZE];
memset (result, 0, sizeof (LW_WWWCLI_DATA));
if (read_string (result->ip, str, sizeof (result->ip)) &&
read_separator (str) &&
read_int (&(result->port), str) &&
read_separator (str) &&
read_string (temp, str, sizeof (temp)) &&
read_separator (str) &&
read_string (temp, str, sizeof (temp)) &&
read_separator (str) &&
read_int (&(result->uptime), str) &&
read_separator (str) &&
read_int (&(result->busy_players), str) &&
read_separator (str) &&
read_int (&(result->max_players), str) &&
read_separator (str) &&
read_int (&(result->password), str) &&
read_separator (str) &&
read_string (result->comment, str, sizeof (result->comment)))
{
LW_MACRO_SPRINTF3 (result->label,
"%s (%d/%d)",
result->ip,
result->busy_players, result->max_players);
result->ping_delay = lw_ping_try_server (result->ip, result->port);
ok = 1;
}
next_line (str);
return ok;
}
/*------------------------------------------------------------------*/
/*
* Skip spaces, tabs and the likes.
*/
static void
skip_space (char **str)
{
while ((isspace (**str) || (**str) == '\r') && (**str) != '\n')
{
++(*str);
}
}
/*------------------------------------------------------------------*/
/*
* Reads a string between ''. Double '' are interpreted as a single '
* which is the standard SQL convention
*/
static int
read_string (char *result, char **str, int size)
{
int i = 0;
int done = 0;
char c;
skip_space (str);
c = **str;
if (c == '\'')
{
++(*str);
while ((c = **str) != '\0' && c != '\n' && !done)
{
++(*str);
if (c == '\'')
{
if ((**str) == '\'')
{
++(*str);
if (i < size - 1)
{
result[i++] = '\'';
}
}
else
{
done = 1;
}
}
else
{
if (i < size - 1)
{
result[i++] = c;
}
}
}
}
result[i++] = '\0';
skip_space (str);
return done;
}
/*------------------------------------------------------------------*/
/*
* Reads an integer
*/
static int
read_int (int *result, char **str)
{
int ok = 0;
char c;
(*result) = 0;
skip_space (str);
while (isdigit (c = (**str)))
{
++(*str);
(*result) *= 10;
(*result) += c - '0';
ok = 1;
}
skip_space (str);
return ok;
}
/*------------------------------------------------------------------*/
/*
* Reads a field separator
*/
static int
read_separator (char **str)
{
int ok = 0;
char c;
skip_space (str);
c = **str;
if (c == ',')
{
++(*str);
ok = 1;
}
skip_space (str);
return ok;
}
/*------------------------------------------------------------------*/
/*
* Reads a new-line character
*/
static int
read_eol (char **str)
{
int ok = 0;
char c;
skip_space (str);
c = **str;
if (c == '\n')
{
++(*str);
ok = 1;
}
skip_space (str);
return ok;
}
/*------------------------------------------------------------------*/
/*
* Reads "less than n chars", then a "\n"
*/
static int
read_eol_n (char **str, int n)
{
int ok = 0;
char *tmp;
skip_space (str);
tmp = strchr (*str, '\n');
if (tmp != NULL)
{
if (tmp - (*str) < n)
{
ok = 1;
(*str) = tmp + 1;
skip_space (str);
}
}
return ok;
}
/*------------------------------------------------------------------*/
/*
* Reads an end of line
*/
static void
next_line (char **str)
{
char *pos;
int len;
skip_space (str);
pos = strchr (*str, '\n');
if (pos)
{
(*str) = pos + 1;
}
else
{
/*
* No '\n' found, we decided to go to the
* very end of the string
*/
len = strlen (*str);
(*str) += len;
}
skip_space (str);
}
|