38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
42 #include <arpa/inet.h>
50 #ifdef HAVE_CURL_CURL_H
51 #include <curl/curl.h>
52 #include <curl/types.h>
53 #include <curl/easy.h>
58 static struct sockaddr_in
sock;
70 struct hostent *hostbn;
88 memcpy(&
sock.sin_addr, hostbn->h_addr, hostbn->h_length);
91 ioctlsocket(
metafd, FIONBIO , &temp);
93 fcntl(
metafd, F_SETFL, O_NONBLOCK);
95 if ((
metafd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
96 LOG(
llevDebug,
"metaserver_init: Unable to create socket, err %d\n", errno);
99 sock.sin_family = AF_INET;
106 if (gethostname(hostname,
MAX_BUF-1)) {
107 LOG(
llevDebug,
"metaserver_init: gethostname failed - will not report hostname\n");
112 hostbn = gethostbyname(hostname);
113 if (hostbn != (
struct hostent *)NULL)
114 memcpy(domain, hostbn->h_addr, hostbn->h_length);
116 if (hostbn == (
struct hostent *)NULL) {
118 if (getdomainname(domain,
MAX_BUF-1)) {
120 LOG(
llevDebug,
"metaserver_init: getdomainname failed - will not report hostname\n");
135 char data[
MAX_BUF], num_players = 0;
145 if (pl->
ob->
map == NULL)
166 if (sendto(
metafd, data, strlen(data), 0, (
struct sockaddr *)&
sock,
sizeof(
sock)) < 0) {
167 LOG(
llevDebug,
"metaserver_update: sendto failed, err = %d\n", errno);
256 static int has_init = 0;
263 #ifdef HAVE_CURL_CURL_H
270 pthread_mutex_init(&ms2_info_mutex, NULL);
271 curl_global_init(CURL_GLOBAL_ALL);
286 if (local_info.
flags)
288 for (ms2 = metaserver2; ms2; ms2 = msnext) {
303 while (fgets(buf,
MAX_BUF-1, fp) != NULL) {
307 if ((cp = strrchr(buf,
'\n')) != NULL)
316 if ((cp = strpbrk(buf,
" \t")) != NULL) {
326 if (!
strcasecmp(buf,
"metaserver2_notification")) {
332 LOG(
llevError,
"metaserver2: Unknown value for metaserver2_notification: %s\n", cp);
334 }
else if (!
strcasecmp(buf,
"metaserver2_server")) {
341 LOG(
llevError,
"metaserver2: metaserver2_server must have a value.\n");
343 }
else if (!
strcasecmp(buf,
"localhostname")) {
347 LOG(
llevError,
"metaserver2: localhostname must have a value.\n");
353 LOG(
llevError,
"metaserver2: portnumber must have a value.\n");
359 }
else if (!
strcasecmp(buf,
"html_comment")) {
361 }
else if (!
strcasecmp(buf,
"text_comment")) {
366 local_info.
mapbase = strdup(cp);
370 local_info.
flags = strdup(cp);
372 LOG(
llevError,
"Unknown value in metaserver2 file: %s\n", buf);
381 #ifndef HAVE_CURL_CURL_H
383 LOG(
llevError,
"metaserver2 file is set to do notification, but libcurl is not found.\n");
384 LOG(
llevError,
"Either fix your compilation, or turn of metaserver2 notification in \n");
404 local_info.
mapbase = strdup(
"");
407 if (!local_info.
flags)
408 local_info.
flags = strdup(
"");
412 LOG(
llevError,
"metaserver2_init: return code from pthread_create() is %d\n", comp);
428 size_t realsize = size*nmemb;
430 LOG(
llevDebug,
"metaserver2_writer- Start of text:\n%s\n", (
const char*)ptr);
443 #ifdef HAVE_CURL_CURL_H
445 struct curl_httppost *formpost = NULL;
446 struct curl_httppost *lastptr = NULL;
455 curl_formadd(&formpost, &lastptr,
456 CURLFORM_COPYNAME,
"hostname",
457 CURLFORM_COPYCONTENTS, local_info.
hostname,
461 curl_formadd(&formpost, &lastptr,
462 CURLFORM_COPYNAME,
"port",
463 CURLFORM_COPYCONTENTS, buf,
466 curl_formadd(&formpost, &lastptr,
467 CURLFORM_COPYNAME,
"html_comment",
471 curl_formadd(&formpost, &lastptr,
472 CURLFORM_COPYNAME,
"text_comment",
476 curl_formadd(&formpost, &lastptr,
477 CURLFORM_COPYNAME,
"archbase",
478 CURLFORM_COPYCONTENTS, local_info.
archbase,
481 curl_formadd(&formpost, &lastptr,
482 CURLFORM_COPYNAME,
"mapbase",
483 CURLFORM_COPYCONTENTS, local_info.
mapbase,
486 curl_formadd(&formpost, &lastptr,
487 CURLFORM_COPYNAME,
"codebase",
488 CURLFORM_COPYCONTENTS, local_info.
codebase,
491 curl_formadd(&formpost, &lastptr,
492 CURLFORM_COPYNAME,
"flags",
493 CURLFORM_COPYCONTENTS, local_info.
flags,
496 pthread_mutex_lock(&ms2_info_mutex);
499 curl_formadd(&formpost, &lastptr,
500 CURLFORM_COPYNAME,
"num_players",
501 CURLFORM_COPYCONTENTS, buf,
505 curl_formadd(&formpost, &lastptr,
506 CURLFORM_COPYNAME,
"in_bytes",
507 CURLFORM_COPYCONTENTS, buf,
511 curl_formadd(&formpost, &lastptr,
512 CURLFORM_COPYNAME,
"out_bytes",
513 CURLFORM_COPYCONTENTS, buf,
517 curl_formadd(&formpost, &lastptr,
518 CURLFORM_COPYNAME,
"uptime",
519 CURLFORM_COPYCONTENTS, buf,
522 pthread_mutex_unlock(&ms2_info_mutex);
527 curl_formadd(&formpost, &lastptr,
528 CURLFORM_COPYNAME,
"version",
533 curl_formadd(&formpost, &lastptr,
534 CURLFORM_COPYNAME,
"sc_version",
535 CURLFORM_COPYCONTENTS, buf,
539 curl_formadd(&formpost, &lastptr,
540 CURLFORM_COPYNAME,
"cs_version",
541 CURLFORM_COPYCONTENTS, buf,
544 for (ms2 = metaserver2; ms2; ms2 = ms2->
next) {
548 curl = curl_easy_init();
551 curl_easy_setopt(curl, CURLOPT_URL, ms2->
hostname);
552 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
559 res = curl_easy_perform(curl);
562 fprintf(stderr,
"easy_perform got error %d\n", res);
565 curl_easy_cleanup(curl);
569 curl_formfree(formpost);
#define ST_GET_PARTY_PASSWORD
void close_and_delete(FILE *fp, int compressed)
char meta_server[MAX_BUF]
char meta_comment[MAX_BUF]
#define QUERY_FLAG(xyz, p)
char * strdup_local(const char *str)
int snprintf(char *dest, int max, const char *format,...)
EXTERN player * first_player
int strcasecmp(const char *s1, const char *s2)
void LOG(LogLevel logLevel, const char *format,...)
#define FREE_AND_CLEAR(xyz)
FILE * open_and_uncompress(const char *name, int flag, int *compressed)