Debian Codegraph @

socket/metaserver.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * \file
16 * Meta-server related functions.
17 * \date 2003-12-02
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 /* ---win32 exclude unix header files */
28 #include <sys/types.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <arpa/inet.h>
32
33 #endif /* end win32 */
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 * Updates our info in the metaserver
45 * Note that this is used for both metaserver1 and metaserver2 -
46 * for metaserver2, it just copies dynamic data into private
47 * data structure, doing locking in the process.
48 */
49 void metaserver_update(void) {
50 char num_players = 0;
51 player *pl;
52
53 #ifdef HAVE_LIBCURL
54 /* We could use socket_info.nconns, but that is not quite as accurate,
55 * as connections in the progress of being established, are listening
56 * but don't have a player, etc. The checks below are basically the
57 * same as for the who commands with the addition that WIZ, AFK, and BOT
58 * players are not counted.
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 /* Everything inside the pthread lock/unlock is related
77 * to metaserver2 synchronization.
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 * Start of metaserver2 logic
90 * Note: All static structures in this file should be treated as strictly
91 * private. The metaserver2 update logic runs in its own thread,
92 * so if something needs to modify these structures, proper locking
93 * is needed.
94 */
95
96 /**
97 * This is a linked list of all the metaservers -
98 * never really know how many we have.
99 */
100 typedef struct _MetaServer2 {
101 char *hostname; /**< Hostname to contact. */
102 struct _MetaServer2 *next; /**< Next element in the list. */
103 } MetaServer2;
104
105 /** Metaservers to send information to. */
106 static MetaServer2 *metaserver2;
107
108 /**
109 * LocalMeta2Info basically holds all the non server metaserver2 information
110 * that we read from the metaserver2 file. Could just have
111 * individual variables, but doing it as a structure is cleaner
112 * I think, and might be more flexible if in the future, we
113 * want to pass this information to other functions (like plugin
114 * or something)
115 */
116 typedef struct _LocalMeta2Info {
117 int notification; /**< If true, do updates to metaservers. */
118 char *hostname; /**< Hostname of this server. */
119 int portnumber; /**< Portnumber of this server. */
120 char *html_comment; /**< html comment to send to metaservers. */
121 char *text_comment; /**< text comment to send to metaservers. */
122 char *archbase; /**< Different sources for arches, maps */
123 char *mapbase; /**< and server. */
124 char *codebase;
125 char *flags; /**< Short flags to send to metaserver. */
126 } LocalMeta2Info;
127
128 /** Non volatile information on the server. */
129 static LocalMeta2Info local_info;
130
131 /** Mutex to protect access to ::metaserver2_updateinfo. */
132 pthread_mutex_t ms2_info_mutex;
133
134 /** Statistics on players and such sent to the metaserver2. */
135 MetaServer2_UpdateInfo metaserver2_updateinfo;
136
137 /**
138 * This frees any data associated with the MetaServer2 info,
139 * including the pointer itself. Caller is responsible for updating
140 * pointers (ms->next) - really only used when wanting to free
141 * all data.
142 * @param ms data to free, pointer becomes invalid.
143 */
144 static void free_metaserver2(MetaServer2 *ms) {
145 free(ms->hostname);
146 free(ms);
147 }
148
149 /**
150 * This initializes the metaserver2 logic - it reads
151 * the metaserver2 file, storing the values
152 * away. Note that it may be possible/desirable for the
153 * server to re-read the values and restart connections (for example,
154 * a new metaserver has been added and you want to start updates to
155 * it immediately and not restart the server). Because of that,
156 * there is some extra logic (has_init) to try to take that
157 * into account.
158 *
159 * @return
160 * 1 if we will be updating the metaserver, 0 if no
161 * metaserver updates
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 /* Now load up the values from the file */
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 /* eliminate newline */
216 if ((cp = strrchr(buf, '\n')) != NULL)
217 *cp = '\0';
218
219 /* Skip over empty lines */
220 if (buf[0] == 0)
221 continue;
222
223 /* Find variable pairs */
224
225 if ((cp = strpbrk(buf, " \t")) != NULL) {
226 while (isspace(*cp))
227 *cp++ = 0;
228 } else {
229 /* This makes it so we don't have to do NULL checks against
230 * cp everyplace
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 /* For the following values, it is easier to make sure
265 * the pointers are set to something, even if it is a blank
266 * string, so don't care if there is data in the string or not.
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 /* If no hostname is set, can't do updates */
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 /* As noted above, it is much easier for the rest of the code
304 * to not have to check for null pointers. So we do that
305 * here, and anything that is null, we just allocate
306 * an empty string.
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 /* Effectively true - we're not going to update the metaserver */
326 local_info.notification = 0;
327 }
328 }
329 return local_info.notification;
330 }
331
332 /**
333 * Handles writing of HTTP request data from the metaserver2.
334 * We treat the data as a string. We should really pay attention to the
335 * header data, and do something clever if we get 404 codes
336 * or the like.
337 * @param ptr actual data.
338 * @param size size of the data.
339 * @param nmemb number of elements.
340 * @param data user-provided data, unused.
341 * @return number of bytes processed, always the full size.
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 /* First, fill in the form - note that everything has to be a string,
355 * so we convert as needed with snprintf.
356 * The order of fields here really isn't important.
357 * The string after CURLFORM_COPYNAME is the name of the POST variable
358 * as the
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 /* Following few fields are global variables,
430 * but are really defines, so won't ever change.
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 * This sends an update to the various metaservers.
453 * It generates the form, and then sends it to the
454 * server
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 /* what URL that receives this POST */
465 curl_easy_setopt(curl, CURLOPT_URL, ms2->hostname);
466 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
467
468 /* Almost always, we will get HTTP data returned
469 * to us - instead of it going to stderr,
470 * we want to take care of it ourselves.
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 /* always cleanup */
483 curl_easy_cleanup(curl);
484 } else {
485 LOG(llevError, "metaserver: could not initialize curl\n");
486 }
487 }
488 /* then cleanup the formpost chain */
489 curl_formfree(formpost);
490 #endif
491 }
492
493 /**
494 * metserver2_thread is the function called from pthread_create.
495 * it is a trivial function - it just sleeps and calls
496 * the update function. The sleep time here is really
497 * quite arbitrary, but once a minute is probably often
498 * enough. A better approach might be to
499 * do a time() call and see how long the update takes,
500 * and sleep according to that.
501 *
502 * @param junk unused.
503 * @return
504 * This function should never return/exit.
505 */
506
507 void *metaserver2_thread(void *junk) {
508 while (1) {
509 metaserver2_updates();
510 sleep(60);
511 }
512 return NULL;
513 }