The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
server.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License 2
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef SERVER_HPP_INCLUDED
16 #define SERVER_HPP_INCLUDED
17 
18 #include "config.hpp"
19 #include "user_handler.hpp"
20 #include "metrics.hpp"
21 #include "ban.hpp"
22 #include "player.hpp"
23 #include "simple_wml.hpp"
24 #include "player_connection.hpp"
25 
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/shared_array.hpp>
28 #include <boost/ptr_container/ptr_vector.hpp>
29 
30 #include <boost/asio/signal_set.hpp>
31 
32 namespace wesnothd
33 {
34 
35 class server
36 {
37 public:
38  server(int port, bool keep_alive, const std::string& config_file, size_t min_threads,size_t max_threads);
39  void run();
40 private:
41  boost::asio::io_service io_service_;
42  boost::asio::ip::tcp::acceptor acceptor_;
43  void serve();
44  void accept_connection(const boost::system::error_code& error, socket_ptr socket);
45 
46  union {
48  char buf[4];
50  void serverside_handshake(socket_ptr socket);
51  void handle_handshake(const boost::system::error_code& error, socket_ptr socket, boost::shared_array<char> buf);
52 
53  void request_version(const boost::system::error_code& error, socket_ptr socket);
54  void handle_version(socket_ptr socket);
56 
57  void login(socket_ptr socket);
59  void send_password_request(socket_ptr socket, const std::string& msg,
60  const std::string& user, const char* error_code = "", bool force_confirmation = false);
61 
62  void add_player(socket_ptr socket, const wesnothd::player&);
63  void read_from_player(socket_ptr socket);
67  void handle_whisper(socket_ptr socket, simple_wml::node& whisper);
69  void handle_nickserv(socket_ptr socket, simple_wml::node& nickserv);
73  void cleanup_game(game*); // deleter for shared_ptr
75  void remove_player(socket_ptr socket);
76 
79  void send_server_message_to_all(const std::string& message, socket_ptr exclude = socket_ptr()) const;
80  bool player_is_in_game(socket_ptr socket) const {
81  return bool(player_connections_.find(socket)->get_game());
82  }
83 
85 
86  struct connection_log {
87  connection_log(std::string _nick, std::string _ip, time_t _log_off) :
88  nick(_nick), ip(_ip), log_off(_log_off) {}
90  time_t log_off;
91 
92  bool operator==(const connection_log& c) const
93  {
94  // log off time does not matter to find ip-nick pairs
95  return c.nick == nick && c.ip == ip;
96  }
97  };
98 
99  std::deque<connection_log> ip_log_;
100 
101  struct login_log {
102  login_log(std::string _ip, int _attempts, time_t _first_attempt) :
103  ip(_ip), attempts(_attempts), first_attempt(_first_attempt) {}
105  int attempts;
107 
108  bool operator==(const login_log& l) const
109  {
110  // only the IP matters
111  return l.ip == ip;
112  }
113  };
114 
115  std::deque<login_log> failed_logins_;
116 
117  boost::scoped_ptr<user_handler> user_handler_;
118  std::map<long int,std::string> seeds_;
119 
121  std::deque<boost::shared_ptr<game>> games() {
122  std::deque<boost::shared_ptr<game>> result;
123  for(const auto& iter : player_connections_.get<game_t>())
124  if(result.empty() || iter.get_game() != result.back())
125  result.push_back(iter.get_game());
126  if(!result.empty() && result.front() == 0)
127  result.pop_front();
128  return result;
129  }
130 
131  /** server socket/fifo. */
132 #ifndef _WIN32
133  boost::asio::posix::stream_descriptor input_;
134 #endif
136 
139 
140  /** Read the server config from file 'config_file_'. */
141  config read_config() const;
142 
143  // settings from the server config
144  std::vector<std::string> accepted_versions_;
145  std::map<std::string,config> redirected_versions_;
146  std::map<std::string,config> proxy_versions_;
147  std::vector<std::string> disallowed_names_;
154  time_t lan_server_;
163  std::vector<std::string> tor_ip_list_;
166  std::deque<login_log>::size_type failed_login_buffer_size_;
167 
168  /** Parse the server config into local variables. */
169  void load_config();
170 
171  bool ip_exceeds_connection_limit(const std::string& ip) const;
172  std::string is_ip_banned(const std::string& ip) const;
173 
178 
180 
181  time_t last_ping_;
182  time_t last_stats_;
183  void dump_stats(const time_t& now);
184 
186  void clean_user_handler(const time_t& now);
187 
188  /** Process commands from admins and users. */
190 
191  void delete_game(int);
192 
193  void update_game_in_lobby(const wesnothd::game& g, const socket_ptr& exclude=socket_ptr());
194 
195  void start_new_server();
196 
197  void setup_fifo();
198 #ifndef _WIN32
199  void read_from_fifo();
200  void handle_read_from_fifo(const boost::system::error_code& error, std::size_t bytes_transferred);
201  boost::asio::streambuf admin_cmd_;
202 #endif
203  void setup_handlers();
204 
205  typedef std::function<void(server*, const std::string&, const std::string&, std::string&, std::ostringstream *)> cmd_handler;
206  std::map<std::string, cmd_handler> cmd_handlers_;
207 
208  void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
209  void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
210  void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
211  void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
212  void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
213  void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
214  void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
215  void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
216  void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
217  void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
218  void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
219  void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
220  void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
221  void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
222  void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
223  void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
224  void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
225  void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
226  void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
227  void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
228  void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
229  void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
230  void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
231  void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
232  void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
233  void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *);
234 
235 #ifndef _WIN32
236  boost::asio::signal_set sighup_;
237  void handle_sighup(const boost::system::error_code& error, int signal_number);
238 #endif
239  boost::asio::signal_set sigs_;
240  void handle_termination(const boost::system::error_code& error, int signal_number);
241 
242  boost::asio::deadline_timer timer_;
243  void handle_graceful_timeout(const boost::system::error_code& error);
244 };
245 
246 }
247 
248 #endif
std::string motd_
Definition: server.hpp:149
config read_config() const
Read the server config from file 'config_file_'.
Definition: server.cpp:576
std::deque< boost::shared_ptr< game > > games()
Definition: server.hpp:121
void handle_nickserv(socket_ptr socket, simple_wml::node &nickserv)
Definition: server.cpp:1250
void handle_whisper(socket_ptr socket, simple_wml::node &whisper)
Definition: server.cpp:1150
void shut_down_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2302
void serverside_handshake(socket_ptr socket)
Definition: server.cpp:758
simple_wml::document games_and_users_list_
Definition: server.hpp:177
std::deque< login_log >::size_type failed_login_buffer_size_
Definition: server.hpp:166
void handle_read_from_player(socket_ptr socket, boost::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1111
void handle_version(socket_ptr socket)
Definition: server.cpp:792
void status_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2518
boost::asio::posix::stream_descriptor input_
server socket/fifo.
Definition: server.hpp:133
void games_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2383
const GLfloat * c
Definition: glew.h:12741
int failed_login_limit_
Definition: server.hpp:164
void handle_login(socket_ptr socket, boost::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:858
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:29
bool operator==(const connection_log &c) const
Definition: server.hpp:92
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
void accept_connection(const boost::system::error_code &error, socket_ptr socket)
Definition: server.cpp:730
bool save_replays_
Definition: server.hpp:160
void kickban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2668
void handle_query(socket_ptr socket, simple_wml::node &query)
Definition: server.cpp:1173
void help_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2361
void login(socket_ptr socket)
Definition: server.cpp:851
void wml_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2388
server(int port, bool keep_alive, const std::string &config_file, size_t min_threads, size_t max_threads)
Definition: server.cpp:363
boost::asio::signal_set sighup_
Definition: server.hpp:236
void stats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2366
size_t default_max_messages_
Definition: server.hpp:150
void handle_read_from_fifo(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: server.cpp:494
void update_game_in_lobby(const wesnothd::game &g, const socket_ptr &exclude=socket_ptr())
Definition: server.cpp:2971
GLboolean GLboolean g
Definition: glew.h:7319
void requests_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2378
std::vector< std::string > accepted_versions_
Definition: server.hpp:144
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
void delete_game(int)
Definition: server.cpp:2943
void ban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2597
Definitions for the interface to Wesnoth Markup Language (WML).
void handle_sighup(const boost::system::error_code &error, int signal_number)
Definition: server.cpp:436
std::string uh_name_
Definition: server.hpp:158
boost::scoped_ptr< user_handler > user_handler_
Definition: server.hpp:117
void handle_create_game(socket_ptr socket, simple_wml::node &create_game)
Definition: server.cpp:1370
void handle_termination(const boost::system::error_code &error, int signal_number)
Definition: server.cpp:448
bool ip_exceeds_connection_limit(const std::string &ip) const
GLdouble l
Definition: glew.h:6966
bool deny_unregistered_login_
Definition: server.hpp:159
void handle_graceful_timeout(const boost::system::error_code &error)
Definition: server.cpp:460
void gban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2751
boost::asio::signal_set sigs_
Definition: server.hpp:239
simple_wml::document login_response_
Definition: server.hpp:175
GLuint64EXT * result
Definition: glew.h:10727
bool allow_remote_shutdown_
Definition: server.hpp:162
std::map< std::string, config > proxy_versions_
Definition: server.hpp:146
login_log(std::string _ip, int _attempts, time_t _first_attempt)
Definition: server.hpp:102
void msg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2485
time_t last_ping_
Definition: server.hpp:181
void add_player(socket_ptr socket, const wesnothd::player &)
Definition: server.cpp:1083
void remove_player(socket_ptr socket)
Definition: server.cpp:1882
void handle_handshake(const boost::system::error_code &error, socket_ptr socket, boost::shared_array< char > buf)
Definition: server.cpp:767
std::string restart_command
Definition: server.hpp:156
void clones_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2551
void handle_player_in_lobby(socket_ptr socket, boost::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1136
void read_version(socket_ptr socket, boost::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:799
std::string input_path_
Definition: server.hpp:135
void handle_player_in_game(socket_ptr socket, boost::shared_ptr< simple_wml::document > doc)
Definition: server.cpp:1517
boost::asio::deadline_timer timer_
Definition: server.hpp:242
void motd_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2877
GLenum GLuint GLsizei const char * buf
Definition: glew.h:2498
std::vector< std::string > tor_ip_list_
Definition: server.hpp:163
void bans_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2575
void searchlog_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2894
void request_version(const boost::system::error_code &error, socket_ptr socket)
Definition: server.cpp:782
union wesnothd::server::@25 handshake_response_
void send_password_request(socket_ptr socket, const std::string &msg, const std::string &user, const char *error_code="", bool force_confirmation=false)
Definition: server.cpp:1051
std::string is_ip_banned(const std::string &ip) const
Definition: server.cpp:702
simple_wml::document version_query_response_
Definition: server.hpp:174
std::string replay_save_path_
Definition: server.hpp:161
void kick_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2842
void clean_user_handler(const time_t &now)
Definition: server.cpp:716
size_t concurrent_connections_
Definition: server.hpp:152
void netstats_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2393
boost::uint32_t connection_num
Definition: server.hpp:47
size_t default_time_period_
Definition: server.hpp:151
bool player_is_in_game(socket_ptr socket) const
Definition: server.hpp:80
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
void send_server_message_to_all(const std::string &message, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1936
boost::asio::ip::tcp::acceptor acceptor_
Definition: server.hpp:42
void cleanup_game(game *)
Definition: server.cpp:1414
time_t last_uh_clean_
Definition: server.hpp:185
bool graceful_restart
Definition: server.hpp:153
void sample_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2343
void dump_stats(const time_t &now)
Definition: server.cpp:709
void start_new_server()
Definition: server.cpp:2237
connection_log(std::string _nick, std::string _ip, time_t _log_off)
Definition: server.hpp:87
void read_from_player(socket_ptr socket)
Definition: server.cpp:1103
void unban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2822
const std::string config_file_
Definition: server.hpp:137
multi_index_container< player_record, indexed_by< ordered_unique< tag< socket_t >, BOOST_MULTI_INDEX_CONST_MEM_FUN(player_record, const socket_ptr, socket)>, hashed_unique< tag< name_t >, BOOST_MULTI_INDEX_CONST_MEM_FUN(player_record, const std::string &, name)>, ordered_non_unique< tag< game_t >, BOOST_MULTI_INDEX_CONST_MEM_FUN(player_record, int, game_id)> >> player_connections
bool operator==(const login_log &l) const
Definition: server.hpp:108
std::function< void(server *, const std::string &, const std::string &, std::string &, std::ostringstream *)> cmd_handler
Definition: server.hpp:205
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
std::string admin_passwd_
Definition: server.hpp:148
wesnothd::ban_manager ban_manager_
Definition: server.hpp:84
std::deque< login_log > failed_logins_
Definition: server.hpp:115
void adminmsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2416
metrics metrics_
Definition: server.hpp:179
std::map< std::string, config > redirected_versions_
Definition: server.hpp:145
void handle_message(socket_ptr socket, simple_wml::node &message)
Definition: server.cpp:1363
void restart_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2322
boost::asio::io_service io_service_
Definition: server.hpp:41
time_t failed_login_ban_
Definition: server.hpp:165
std::map< long int, std::string > seeds_
Definition: server.hpp:118
void handle_join_game(socket_ptr socket, simple_wml::node &join)
Definition: server.cpp:1442
void send_to_lobby(simple_wml::document &data, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1921
void setup_handlers()
Definition: server.cpp:519
size_t max_ip_log_size_
Definition: server.hpp:157
Definition: ban.cpp:28
void read_from_fifo()
Definition: server.cpp:488
std::map< std::string, cmd_handler > cmd_handlers_
Definition: server.hpp:206
void setup_fifo()
Definition: server.cpp:473
void dul_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2926
player_connections player_connections_
Definition: server.hpp:120
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
time_t lan_server_
Definition: server.hpp:154
void load_config()
Parse the server config into local variables.
Definition: server.cpp:591
simple_wml::document join_lobby_response_
Definition: server.hpp:176
std::deque< connection_log > ip_log_
Definition: server.hpp:99
time_t last_stats_
Definition: server.hpp:182
void ungban_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2832
boost::shared_ptr< boost::asio::ip::tcp::socket > socket_ptr
std::string process_command(std::string cmd, std::string issuer_name)
Process commands from admins and users.
Definition: server.cpp:2251
void metrics_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2373
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void lobbymsg_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2502
void pm_handler(const std::string &, const std::string &, std::string &, std::ostringstream *)
Definition: server.cpp:2450
void create_game(player_record &host, simple_wml::node &create_game)
Definition: server.cpp:1393
void send_server_message_to_lobby(const std::string &message, socket_ptr exclude=socket_ptr()) const
Definition: server.cpp:1928
GLenum query
Definition: glew.h:4185
GLsizei const GLcharARB ** string
Definition: glew.h:4503
time_t last_user_seen_time_
Definition: server.hpp:155
boost::asio::streambuf admin_cmd_
Definition: server.hpp:201
std::vector< std::string > disallowed_names_
Definition: server.hpp:147