| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "global.h" |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <pthread.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#ifndef WIN32 |
| 28 |
#include <sys/types.h> |
| 29 |
#include <netinet/in.h> |
| 30 |
#include <netdb.h> |
| 31 |
#include <arpa/inet.h> |
| 32 |
|
| 33 |
#endif |
| 34 |
|
| 35 |
#include "metaserver2.h" |
| 36 |
#include "version.h" |
| 37 |
|
| 38 |
#ifdef HAVE_LIBCURL |
| 39 |
#include <curl/curl.h> |
| 40 |
#include <curl/easy.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
void metaserver_update(void) { |
| 50 |
char num_players = 0; |
| 51 |
player *pl; |
| 52 |
|
| 53 |
#ifdef HAVE_LIBCURL |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
for (pl = first_player; pl != NULL; pl = pl->next) { |
| 61 |
if (pl->ob->map == NULL) |
| 62 |
continue; |
| 63 |
if (pl->hidden) |
| 64 |
continue; |
| 65 |
if (QUERY_FLAG(pl->ob, FLAG_WIZ)) |
| 66 |
continue; |
| 67 |
if (QUERY_FLAG(pl->ob, FLAG_AFK)) |
| 68 |
continue; |
| 69 |
if (pl->state != ST_PLAYING && pl->state != ST_GET_PARTY_PASSWORD) |
| 70 |
continue; |
| 71 |
if (pl->socket.is_bot) |
| 72 |
continue; |
| 73 |
num_players++; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
pthread_mutex_lock(&ms2_info_mutex); |
| 80 |
metaserver2_updateinfo.num_players = num_players; |
| 81 |
metaserver2_updateinfo.in_bytes = cst_tot.ibytes; |
| 82 |
metaserver2_updateinfo.out_bytes = cst_tot.obytes; |
| 83 |
metaserver2_updateinfo.uptime = (long)time(NULL)-cst_tot.time_start; |
| 84 |
pthread_mutex_unlock(&ms2_info_mutex); |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
typedef struct _MetaServer2 { |
| 101 |
char *hostname; |
| 102 |
struct _MetaServer2 *next; |
| 103 |
} MetaServer2; |
| 104 |
|
| 105 |
|
| 106 |
static MetaServer2 *metaserver2; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
typedef struct _LocalMeta2Info { |
| 117 |
int notification; |
| 118 |
char *hostname; |
| 119 |
int portnumber; |
| 120 |
char *html_comment; |
| 121 |
char *text_comment; |
| 122 |
char *archbase; |
| 123 |
char *mapbase; |
| 124 |
char *codebase; |
| 125 |
char *flags; |
| 126 |
} LocalMeta2Info; |
| 127 |
|
| 128 |
|
| 129 |
static LocalMeta2Info local_info; |
| 130 |
|
| 131 |
|
| 132 |
pthread_mutex_t ms2_info_mutex; |
| 133 |
|
| 134 |
|
| 135 |
MetaServer2_UpdateInfo metaserver2_updateinfo; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static void free_metaserver2(MetaServer2 *ms) { |
| 145 |
free(ms->hostname); |
| 146 |
free(ms); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
int metaserver2_init(void) { |
| 164 |
static int has_init = 0; |
| 165 |
FILE *fp; |
| 166 |
char buf[MAX_BUF], *cp, dummy[1]; |
| 167 |
MetaServer2 *ms2, *msnext; |
| 168 |
pthread_t thread_id; |
| 169 |
|
| 170 |
dummy[0] = '\0'; |
| 171 |
|
| 172 |
#ifdef HAVE_LIBCURL |
| 173 |
if (!has_init) { |
| 174 |
memset(&local_info, 0, sizeof(LocalMeta2Info)); |
| 175 |
memset(&metaserver2_updateinfo, 0, sizeof(MetaServer2_UpdateInfo)); |
| 176 |
|
| 177 |
local_info.portnumber = settings.csport; |
| 178 |
metaserver2 = NULL; |
| 179 |
pthread_mutex_init(&ms2_info_mutex, NULL); |
| 180 |
curl_global_init(CURL_GLOBAL_ALL); |
| 181 |
} else { |
| 182 |
local_info.notification = 0; |
| 183 |
if (local_info.hostname) |
| 184 |
FREE_AND_CLEAR(local_info.hostname); |
| 185 |
if (local_info.html_comment) |
| 186 |
FREE_AND_CLEAR(local_info.html_comment); |
| 187 |
if (local_info.text_comment) |
| 188 |
FREE_AND_CLEAR(local_info.text_comment); |
| 189 |
if (local_info.archbase) |
| 190 |
FREE_AND_CLEAR(local_info.archbase); |
| 191 |
if (local_info.mapbase) |
| 192 |
FREE_AND_CLEAR(local_info.mapbase); |
| 193 |
if (local_info.codebase) |
| 194 |
FREE_AND_CLEAR(local_info.codebase); |
| 195 |
if (local_info.flags) |
| 196 |
FREE_AND_CLEAR(local_info.flags); |
| 197 |
for (ms2 = metaserver2; ms2; ms2 = msnext) { |
| 198 |
msnext = ms2->next; |
| 199 |
free_metaserver2(ms2); |
| 200 |
} |
| 201 |
metaserver2 = NULL; |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
|
| 206 |
snprintf(buf, sizeof(buf), "%s/metaserver2", settings.confdir); |
| 207 |
|
| 208 |
if ((fp = fopen(buf, "r")) == NULL) { |
| 209 |
LOG(llevError, "Warning: No metaserver2 file found\n"); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 213 |
if (buf[0] == '#') |
| 214 |
continue; |
| 215 |
|
| 216 |
if ((cp = strrchr(buf, '\n')) != NULL) |
| 217 |
*cp = '\0'; |
| 218 |
|
| 219 |
|
| 220 |
if (buf[0] == 0) |
| 221 |
continue; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if ((cp = strpbrk(buf, " \t")) != NULL) { |
| 226 |
while (isspace(*cp)) |
| 227 |
*cp++ = 0; |
| 228 |
} else { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
cp = dummy; |
| 233 |
} |
| 234 |
|
| 235 |
if (!strcasecmp(buf, "metaserver2_notification")) { |
| 236 |
if (!strcasecmp(cp, "on") || !strcasecmp(cp, "true")) { |
| 237 |
local_info.notification = TRUE; |
| 238 |
} else if (!strcasecmp(cp, "off") || !strcasecmp(cp, "false")) { |
| 239 |
local_info.notification = FALSE; |
| 240 |
} else { |
| 241 |
LOG(llevError, "metaserver2: Unknown value for metaserver2_notification: %s\n", cp); |
| 242 |
} |
| 243 |
} else if (!strcasecmp(buf, "metaserver2_server")) { |
| 244 |
if (*cp != 0) { |
| 245 |
ms2 = calloc(1, sizeof(MetaServer2)); |
| 246 |
ms2->hostname = strdup(cp); |
| 247 |
ms2->next = metaserver2; |
| 248 |
metaserver2 = ms2; |
| 249 |
} else { |
| 250 |
LOG(llevError, "metaserver2: metaserver2_server must have a value.\n"); |
| 251 |
} |
| 252 |
} else if (!strcasecmp(buf, "localhostname")) { |
| 253 |
if (*cp != 0) { |
| 254 |
local_info.hostname = strdup_local(cp); |
| 255 |
} else { |
| 256 |
LOG(llevError, "metaserver2: localhostname must have a value.\n"); |
| 257 |
} |
| 258 |
} else if (!strcasecmp(buf, "portnumber")) { |
| 259 |
if (*cp != 0) { |
| 260 |
local_info.portnumber = atoi(cp); |
| 261 |
} else { |
| 262 |
LOG(llevError, "metaserver2: portnumber must have a value.\n"); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
} else if (!strcasecmp(buf, "html_comment")) { |
| 269 |
local_info.html_comment = strdup(cp); |
| 270 |
} else if (!strcasecmp(buf, "text_comment")) { |
| 271 |
local_info.text_comment = strdup(cp); |
| 272 |
} else if (!strcasecmp(buf, "archbase")) { |
| 273 |
local_info.archbase = strdup(cp); |
| 274 |
} else if (!strcasecmp(buf, "mapbase")) { |
| 275 |
local_info.mapbase = strdup(cp); |
| 276 |
} else if (!strcasecmp(buf, "codebase")) { |
| 277 |
local_info.codebase = strdup(cp); |
| 278 |
} else if (!strcasecmp(buf, "flags")) { |
| 279 |
local_info.flags = strdup(cp); |
| 280 |
} else { |
| 281 |
LOG(llevError, "Unknown value in metaserver2 file: %s\n", buf); |
| 282 |
} |
| 283 |
} |
| 284 |
fclose(fp); |
| 285 |
|
| 286 |
|
| 287 |
if (!local_info.hostname) |
| 288 |
local_info.notification = 0; |
| 289 |
|
| 290 |
#ifndef HAVE_LIBCURL |
| 291 |
if (local_info.notification) { |
| 292 |
LOG(llevError, "metaserver2 file is set to do notification, but libcurl is not found.\n"); |
| 293 |
LOG(llevError, "Either fix your compilation, or turn off metaserver2 notification in \n"); |
| 294 |
LOG(llevError, "the %s/metaserver2 file.\n", settings.confdir); |
| 295 |
LOG(llevError, "Exiting program.\n"); |
| 296 |
exit(1); |
| 297 |
} |
| 298 |
#endif |
| 299 |
|
| 300 |
if (local_info.notification) { |
| 301 |
int ret; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (!local_info.html_comment) |
| 309 |
local_info.html_comment = strdup(""); |
| 310 |
if (!local_info.text_comment) |
| 311 |
local_info.text_comment = strdup(""); |
| 312 |
if (!local_info.archbase) |
| 313 |
local_info.archbase = strdup(""); |
| 314 |
if (!local_info.mapbase) |
| 315 |
local_info.mapbase = strdup(""); |
| 316 |
if (!local_info.codebase) |
| 317 |
local_info.codebase = strdup(""); |
| 318 |
if (!local_info.flags) |
| 319 |
local_info.flags = strdup(""); |
| 320 |
|
| 321 |
ret = pthread_create(&thread_id, NULL, metaserver2_thread, NULL); |
| 322 |
if (ret) { |
| 323 |
LOG(llevError, "metaserver2_init: return code from pthread_create() is %d\n", ret); |
| 324 |
|
| 325 |
|
| 326 |
local_info.notification = 0; |
| 327 |
} |
| 328 |
} |
| 329 |
return local_info.notification; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
static size_t metaserver2_writer(void *ptr, size_t size, size_t nmemb, void *data) { |
| 344 |
size_t realsize = size*nmemb; |
| 345 |
LOG(llevError, "Message from metaserver:\n%s\n", (const char*)ptr); |
| 346 |
return realsize; |
| 347 |
} |
| 348 |
|
| 349 |
#ifdef HAVE_LIBCURL |
| 350 |
static void metaserver2_build_form(struct curl_httppost **formpost) { |
| 351 |
struct curl_httppost *lastptr = NULL; |
| 352 |
char buf[MAX_BUF]; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
curl_formadd(formpost, &lastptr, |
| 361 |
CURLFORM_COPYNAME, "hostname", |
| 362 |
CURLFORM_COPYCONTENTS, local_info.hostname, |
| 363 |
CURLFORM_END); |
| 364 |
|
| 365 |
snprintf(buf, sizeof(buf), "%d", local_info.portnumber); |
| 366 |
curl_formadd(formpost, &lastptr, |
| 367 |
CURLFORM_COPYNAME, "port", |
| 368 |
CURLFORM_COPYCONTENTS, buf, |
| 369 |
CURLFORM_END); |
| 370 |
|
| 371 |
curl_formadd(formpost, &lastptr, |
| 372 |
CURLFORM_COPYNAME, "html_comment", |
| 373 |
CURLFORM_COPYCONTENTS, local_info.html_comment, |
| 374 |
CURLFORM_END); |
| 375 |
|
| 376 |
curl_formadd(formpost, &lastptr, |
| 377 |
CURLFORM_COPYNAME, "text_comment", |
| 378 |
CURLFORM_COPYCONTENTS, local_info.text_comment, |
| 379 |
CURLFORM_END); |
| 380 |
|
| 381 |
curl_formadd(formpost, &lastptr, |
| 382 |
CURLFORM_COPYNAME, "archbase", |
| 383 |
CURLFORM_COPYCONTENTS, local_info.archbase, |
| 384 |
CURLFORM_END); |
| 385 |
|
| 386 |
curl_formadd(formpost, &lastptr, |
| 387 |
CURLFORM_COPYNAME, "mapbase", |
| 388 |
CURLFORM_COPYCONTENTS, local_info.mapbase, |
| 389 |
CURLFORM_END); |
| 390 |
|
| 391 |
curl_formadd(formpost, &lastptr, |
| 392 |
CURLFORM_COPYNAME, "codebase", |
| 393 |
CURLFORM_COPYCONTENTS, local_info.codebase, |
| 394 |
CURLFORM_END); |
| 395 |
|
| 396 |
curl_formadd(formpost, &lastptr, |
| 397 |
CURLFORM_COPYNAME, "flags", |
| 398 |
CURLFORM_COPYCONTENTS, local_info.flags, |
| 399 |
CURLFORM_END); |
| 400 |
|
| 401 |
pthread_mutex_lock(&ms2_info_mutex); |
| 402 |
|
| 403 |
snprintf(buf, sizeof(buf), "%d", metaserver2_updateinfo.num_players); |
| 404 |
curl_formadd(formpost, &lastptr, |
| 405 |
CURLFORM_COPYNAME, "num_players", |
| 406 |
CURLFORM_COPYCONTENTS, buf, |
| 407 |
CURLFORM_END); |
| 408 |
|
| 409 |
snprintf(buf, sizeof(buf), "%d", metaserver2_updateinfo.in_bytes); |
| 410 |
curl_formadd(formpost, &lastptr, |
| 411 |
CURLFORM_COPYNAME, "in_bytes", |
| 412 |
CURLFORM_COPYCONTENTS, buf, |
| 413 |
CURLFORM_END); |
| 414 |
|
| 415 |
snprintf(buf, sizeof(buf), "%d", metaserver2_updateinfo.out_bytes); |
| 416 |
curl_formadd(formpost, &lastptr, |
| 417 |
CURLFORM_COPYNAME, "out_bytes", |
| 418 |
CURLFORM_COPYCONTENTS, buf, |
| 419 |
CURLFORM_END); |
| 420 |
|
| 421 |
snprintf(buf, sizeof(buf), "%ld", (long)metaserver2_updateinfo.uptime); |
| 422 |
curl_formadd(formpost, &lastptr, |
| 423 |
CURLFORM_COPYNAME, "uptime", |
| 424 |
CURLFORM_COPYCONTENTS, buf, |
| 425 |
CURLFORM_END); |
| 426 |
|
| 427 |
pthread_mutex_unlock(&ms2_info_mutex); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
curl_formadd(formpost, &lastptr, |
| 433 |
CURLFORM_COPYNAME, "version", |
| 434 |
CURLFORM_COPYCONTENTS, FULL_VERSION, |
| 435 |
CURLFORM_END); |
| 436 |
|
| 437 |
snprintf(buf, sizeof(buf), "%d", VERSION_SC); |
| 438 |
curl_formadd(formpost, &lastptr, |
| 439 |
CURLFORM_COPYNAME, "sc_version", |
| 440 |
CURLFORM_COPYCONTENTS, buf, |
| 441 |
CURLFORM_END); |
| 442 |
|
| 443 |
snprintf(buf, sizeof(buf), "%d", VERSION_CS); |
| 444 |
curl_formadd(formpost, &lastptr, |
| 445 |
CURLFORM_COPYNAME, "cs_version", |
| 446 |
CURLFORM_COPYCONTENTS, buf, |
| 447 |
CURLFORM_END); |
| 448 |
} |
| 449 |
#endif |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
static void metaserver2_updates(void) { |
| 457 |
#ifdef HAVE_LIBCURL |
| 458 |
struct curl_httppost *formpost = NULL; |
| 459 |
metaserver2_build_form(&formpost); |
| 460 |
|
| 461 |
for (MetaServer2 *ms2 = metaserver2; ms2; ms2 = ms2->next) { |
| 462 |
CURL *curl = curl_easy_init(); |
| 463 |
if (curl) { |
| 464 |
|
| 465 |
curl_easy_setopt(curl, CURLOPT_URL, ms2->hostname); |
| 466 |
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, metaserver2_writer); |
| 473 |
|
| 474 |
char errbuf[CURL_ERROR_SIZE]; |
| 475 |
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); |
| 476 |
|
| 477 |
CURLcode res = curl_easy_perform(curl); |
| 478 |
if (res) { |
| 479 |
LOG(llevError, "metaserver update failed: %s\n", errbuf); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
curl_easy_cleanup(curl); |
| 484 |
} else { |
| 485 |
LOG(llevError, "metaserver: could not initialize curl\n"); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
curl_formfree(formpost); |
| 490 |
#endif |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
void *metaserver2_thread(void *junk) { |
| 508 |
while (1) { |
| 509 |
metaserver2_updates(); |
| 510 |
sleep(60); |
| 511 |
} |
| 512 |
return NULL; |
| 513 |
} |