1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
55 #include "connection.h"
68 static bool server_is_open
= FALSE
;
70 static char meta_patches
[256] = "";
71 static char meta_message
[256] = "";
73 static fc_thread
*meta_srv_thread
= NULL
;
75 /*************************************************************************
76 the default metaserver patches for this server
77 *************************************************************************/
78 const char *default_meta_patches_string(void)
83 /*************************************************************************
84 Return static string with default info line to send to metaserver.
85 *************************************************************************/
86 const char *default_meta_message_string(void)
89 return "unstable pre-" NEXT_STABLE_VERSION
": beware";
90 #else /* IS_BETA_VERSION */
92 return "development version: beware";
93 #else /* IS_DEVEL_VERSION */
95 #endif /* IS_DEVEL_VERSION */
96 #endif /* IS_BETA_VERSION */
99 /*************************************************************************
100 the metaserver patches
101 *************************************************************************/
102 const char *get_meta_patches_string(void)
107 /*************************************************************************
108 the metaserver message
109 *************************************************************************/
110 const char *get_meta_message_string(void)
115 /*************************************************************************
116 The metaserver message set by user
117 *************************************************************************/
118 const char *get_user_meta_message_string(void)
120 if (game
.server
.meta_info
.user_message_set
) {
121 return game
.server
.meta_info
.user_message
;
127 /*************************************************************************
128 Update meta message. Set it to user meta message, if it is available.
129 Otherwise use provided message.
130 It is ok to call this with NULL message. Then it only replaces current
131 meta message with user meta message if available.
132 *************************************************************************/
133 void maybe_automatic_meta_message(const char *automatic
)
135 const char *user_message
;
137 user_message
= get_user_meta_message_string();
139 if (user_message
== NULL
) {
140 /* No user message */
141 if (automatic
!= NULL
) {
142 set_meta_message_string(automatic
);
147 set_meta_message_string(user_message
);
150 /*************************************************************************
151 set the metaserver patches string
152 *************************************************************************/
153 void set_meta_patches_string(const char *string
)
155 sz_strlcpy(meta_patches
, string
);
158 /*************************************************************************
159 set the metaserver message string
160 *************************************************************************/
161 void set_meta_message_string(const char *string
)
163 sz_strlcpy(meta_message
, string
);
166 /*************************************************************************
167 set user defined metaserver message string
168 *************************************************************************/
169 void set_user_meta_message_string(const char *string
)
171 if (string
!= NULL
&& string
[0] != '\0') {
172 sz_strlcpy(game
.server
.meta_info
.user_message
, string
);
173 game
.server
.meta_info
.user_message_set
= TRUE
;
174 set_meta_message_string(string
);
176 /* Remove user meta message. We will use automatic messages instead */
177 game
.server
.meta_info
.user_message
[0] = '\0';
178 game
.server
.meta_info
.user_message_set
= FALSE
;
179 set_meta_message_string(default_meta_message_string());
183 /*************************************************************************
184 Return string describing both metaserver name and port.
185 *************************************************************************/
186 char *meta_addr_port(void)
188 return srvarg
.metaserver_addr
;
191 /*************************************************************************
192 we couldn't find or connect to the metaserver.
193 *************************************************************************/
194 static void metaserver_failed(void)
196 con_puts(C_METAERROR
, _("Not reporting to the metaserver in this game."));
202 /****************************************************************************
203 Insert a setting in the metaserver message. Return TRUE if it succeded.
204 ****************************************************************************/
205 static inline bool meta_insert_setting(struct netfile_post
*post
,
206 const char *set_name
)
208 const struct setting
*pset
= setting_by_name(set_name
);
211 fc_assert_ret_val_msg(NULL
!= pset
, FALSE
,
212 "Setting \"%s\" not found!", set_name
);
213 netfile_add_form_str(post
, "vn[]", setting_name(pset
));
214 netfile_add_form_str(post
, "vv[]",
215 setting_value_name(pset
, FALSE
, buf
, sizeof(buf
)));
219 /*************************************************************************
220 Send POST to metaserver. This runs in its own thread.
221 *************************************************************************/
222 static void send_metaserver_post(void *arg
)
224 struct netfile_post
*post
= (struct netfile_post
*) arg
;
227 if (srvarg
.bind_meta_addr
!= NULL
) {
228 addr
= srvarg
.bind_meta_addr
;
230 addr
= srvarg
.bind_addr
;
233 if (!netfile_send_post(srvarg
.metaserver_addr
, post
, NULL
, addr
)) {
234 con_puts(C_METAERROR
, _("Error connecting to metaserver"));
238 netfile_close_post(post
);
241 /*************************************************************************
242 construct the POST message and send info to metaserver.
243 *************************************************************************/
244 static bool send_to_metaserver(enum meta_flag flag
)
251 struct netfile_post
*post
;
253 switch(server_state()) {
255 sz_strlcpy(state
, "Pregame");
258 sz_strlcpy(state
, "Running");
261 sz_strlcpy(state
, "Game Ended");
266 if (srvarg
.metaserver_name
[0] != '\0') {
267 sz_strlcpy(host
, srvarg
.metaserver_name
);
268 } else if (fc_gethostname(host
, sizeof(host
)) != 0) {
269 sz_strlcpy(host
, "unknown");
272 sz_strlcpy(rs
, game
.control
.name
);
274 /* Freed in metaserver thread function send_metaserver_post() */
275 post
= netfile_start_post();
277 netfile_add_form_str(post
, "host", host
);
278 netfile_add_form_int(post
, "port", srvarg
.port
);
279 netfile_add_form_str(post
, "state", state
);
280 netfile_add_form_str(post
, "ruleset", rs
);
282 if (flag
== META_GOODBYE
) {
283 netfile_add_form_int(post
, "bye", 1);
285 netfile_add_form_str(post
, "version", VERSION_STRING
);
286 netfile_add_form_str(post
, "patches",
287 get_meta_patches_string());
288 netfile_add_form_str(post
, "capability", our_capability
);
290 netfile_add_form_str(post
, "serverid", srvarg
.serverid
);
291 netfile_add_form_str(post
, "message",
292 get_meta_message_string());
294 /* NOTE: send info for ALL players or none at all. */
295 if (normal_player_count() == 0) {
296 netfile_add_form_int(post
, "dropplrs", 1);
298 players
= 0; /* a counter for players_available */
301 players_iterate(plr
) {
302 bool is_player_available
= TRUE
;
304 struct connection
*pconn
= conn_by_user(plr
->username
);
306 if (!plr
->is_alive
) {
307 sz_strlcpy(type
, "Dead");
308 } else if (is_barbarian(plr
)) {
309 sz_strlcpy(type
, "Barbarian");
310 } else if (plr
->ai_controlled
) {
311 sz_strlcpy(type
, "A.I.");
313 sz_strlcpy(type
, "Human");
316 netfile_add_form_str(post
, "plu[]", plr
->username
);
317 netfile_add_form_str(post
, "plt[]", type
);
318 netfile_add_form_str(post
, "pll[]", player_name(plr
));
319 netfile_add_form_str(post
, "pln[]",
320 plr
->nation
!= NO_NATION_SELECTED
321 ? nation_plural_for_player(plr
)
323 netfile_add_form_str(post
, "plf[]",
324 plr
->nation
!= NO_NATION_SELECTED
325 ? nation_of_player(plr
)->flag_graphic_str
327 netfile_add_form_str(post
, "plh[]",
328 pconn
? pconn
->addr
: "");
330 /* is this player available to take?
331 * TODO: there's some duplication here with
332 * stdinhand.c:is_allowed_to_take() */
333 if (is_barbarian(plr
) && !strchr(game
.server
.allow_take
, 'b')) {
334 is_player_available
= FALSE
;
335 } else if (!plr
->is_alive
&& !strchr(game
.server
.allow_take
, 'd')) {
336 is_player_available
= FALSE
;
337 } else if (plr
->ai_controlled
338 && !strchr(game
.server
.allow_take
,
339 (game
.info
.is_new_game
? 'A' : 'a'))) {
340 is_player_available
= FALSE
;
341 } else if (!plr
->ai_controlled
342 && !strchr(game
.server
.allow_take
,
343 (game
.info
.is_new_game
? 'H' : 'h'))) {
344 is_player_available
= FALSE
;
348 is_player_available
= FALSE
;
351 if (is_player_available
) {
355 if (!plr
->ai_controlled
&& plr
->is_alive
) {
358 } players_iterate_end
;
360 /* send the number of available players. */
361 netfile_add_form_int(post
, "available", players
);
362 netfile_add_form_int(post
, "humans", humans
);
365 /* Send some variables: should be listed in inverted order? */
367 static const char *settings
[] = {
368 "timeout", "endturn", "minplayers", "maxplayers",
369 "aifill", "allowtake", "generator"
373 for (i
= 0; i
< ARRAY_SIZE(settings
); i
++) {
374 meta_insert_setting(post
, settings
[i
]);
377 /* HACK: send the most determinant setting for the map size. */
378 switch (map
.server
.mapsize
) {
379 case MAPSIZE_FULLSIZE
:
380 meta_insert_setting(post
, "size");
383 meta_insert_setting(post
, "tilesperplayer");
386 meta_insert_setting(post
, "xsize");
387 meta_insert_setting(post
, "ysize");
393 netfile_add_form_str(post
, "vn[]", "turn");
394 netfile_add_form_int(post
, "vv[]", game
.info
.turn
);
395 netfile_add_form_str(post
, "vn[]", "year");
397 if (server_state() != S_S_INITIAL
) {
398 netfile_add_form_int(post
, "vv[]", game
.info
.year
);
400 netfile_add_form_str(post
, "vv[]", "Calendar not set up");
404 if (meta_srv_thread
!= NULL
) {
405 /* Previously started thread */
406 fc_thread_wait(meta_srv_thread
);
408 meta_srv_thread
= fc_malloc(sizeof(meta_srv_thread
));
411 /* Send POST in new thread */
412 fc_thread_start(meta_srv_thread
, &send_metaserver_post
, post
);
417 /*************************************************************************
418 Stop sending updates to metaserver
419 *************************************************************************/
420 void server_close_meta(void)
422 server_is_open
= FALSE
;
425 /*************************************************************************
426 lookup the correct address for the metaserver.
427 *************************************************************************/
428 bool server_open_meta(void)
430 if (meta_patches
[0] == '\0') {
431 set_meta_patches_string(default_meta_patches_string());
433 if (meta_message
[0] == '\0') {
434 set_meta_message_string(default_meta_message_string());
437 server_is_open
= TRUE
;
442 /**************************************************************************
443 are we sending info to the metaserver?
444 **************************************************************************/
445 bool is_metaserver_open(void)
447 return server_is_open
;
450 /**************************************************************************
451 control when we send info to the metaserver.
452 **************************************************************************/
453 bool send_server_info_to_metaserver(enum meta_flag flag
)
455 static struct timer
*last_send_timer
= NULL
;
456 static bool want_update
;
458 if (!server_is_open
) {
462 /* if we're bidding farewell, ignore all timers */
463 if (flag
== META_GOODBYE
) {
464 if (last_send_timer
) {
465 timer_destroy(last_send_timer
);
466 last_send_timer
= NULL
;
468 send_to_metaserver(flag
);
470 /* Wait metaserver thread to finish */
471 fc_thread_wait(meta_srv_thread
);
472 meta_srv_thread
= NULL
;
477 /* don't allow the user to spam the metaserver with updates */
478 if (last_send_timer
&& (timer_read_seconds(last_send_timer
)
479 < METASERVER_MIN_UPDATE_INTERVAL
)) {
480 if (flag
== META_INFO
) {
481 want_update
= TRUE
; /* we couldn't update now, but update a.s.a.p. */
486 /* if we're asking for a refresh, only do so if
487 * we've exceeded the refresh interval */
488 if ((flag
== META_REFRESH
) && !want_update
&& last_send_timer
489 && (timer_read_seconds(last_send_timer
) < METASERVER_REFRESH_INTERVAL
)) {
493 /* start a new timer if we haven't already */
494 if (!last_send_timer
) {
495 last_send_timer
= timer_new(TIMER_USER
, TIMER_ACTIVE
);
498 timer_clear(last_send_timer
);
499 timer_start(last_send_timer
);
502 return send_to_metaserver(flag
);