Base class for implementing servers that use gzipped-WML network protocol. More...
#include <server_base.hpp>
Classes | |
struct | login_ban_info |
Public Types | |
typedef std::map< std::string, std::string > | info_table |
Public Member Functions | |
server_base (unsigned short port, bool keep_alive) | |
virtual | ~server_base () |
int | run () |
template<class SocketPtr > | |
void | coro_send_doc (SocketPtr socket, simple_wml::document &doc, const boost::asio::yield_context &yield) |
Send a WML document from within a coroutine. More... | |
void | coro_send_file (const socket_ptr &socket, const std::string &filename, const boost::asio::yield_context &yield) |
Send contents of entire file directly to socket from within a coroutine. More... | |
void | coro_send_file (tls_socket_ptr socket, const std::string &filename, const boost::asio::yield_context &yield) |
template<class SocketPtr > | |
std::unique_ptr< simple_wml::document > | coro_receive_doc (SocketPtr socket, const boost::asio::yield_context &yield) |
Receive WML document from a coroutine. More... | |
template<class SocketPtr > | |
void | async_send_doc_queued (SocketPtr socket, simple_wml::document &doc) |
High level wrapper for sending a WML document. More... | |
template<class SocketPtr > | |
void | async_send_error (SocketPtr socket, const std::string &msg, const char *error_code="", const info_table &info={}) |
template<class SocketPtr > | |
void | async_send_warning (SocketPtr socket, const std::string &msg, const char *warning_code="", const info_table &info={}) |
std::string | hash_password (const std::string &pw, const std::string &salt, const std::string &username) |
Handles hashing the password provided by the player before comparing it to the hashed password in the forum database. More... | |
Protected Member Functions | |
void | load_tls_config (const config &cfg) |
void | start_server () |
void | serve (const boost::asio::yield_context &yield, boost::asio::ip::tcp::acceptor &acceptor, const boost::asio::ip::tcp::endpoint &endpoint) |
virtual void | handle_new_client (socket_ptr socket)=0 |
virtual void | handle_new_client (tls_socket_ptr socket)=0 |
virtual bool | accepting_connections () const |
virtual bool | ip_exceeds_connection_limit (const std::string &) const |
virtual utils::optional< login_ban_info > | is_ip_banned (const std::string &) |
void | read_from_fifo () |
virtual void | handle_read_from_fifo (const boost::system::error_code &error, std::size_t bytes_transferred)=0 |
virtual void | handle_sighup (const boost::system::error_code &error, int signal_number)=0 |
Protected Attributes | |
unsigned short | port_ |
bool | keep_alive_ |
boost::asio::io_context | io_service_ |
boost::asio::ssl::context | tls_context_ { boost::asio::ssl::context::sslv23 } |
bool | tls_enabled_ { false } |
boost::asio::ip::tcp::acceptor | acceptor_v6_ |
boost::asio::ip::tcp::acceptor | acceptor_v4_ |
uint32_t | handshake_response_ |
boost::asio::posix::stream_descriptor | input_ |
std::string | fifo_path_ |
boost::asio::streambuf | admin_cmd_ |
boost::asio::signal_set | sighup_ |
Private Member Functions | |
template<class SocketPtr > | |
void | send_doc_queued (SocketPtr socket, std::unique_ptr< simple_wml::document > &doc_ptr, boost::asio::yield_context yield) |
Base class for implementing servers that use gzipped-WML network protocol.
The protocol is based on TCP connection between client and server. Before WML payloads can be sent a handshake is required. Handshake process is as follows:
Message format is as follows:
Definition at line 80 of file server_base.hpp.
typedef std::map<std::string, std::string> server_base::info_table |
Definition at line 122 of file server_base.hpp.
server_base::server_base | ( | unsigned short | port, |
bool | keep_alive | ||
) |
Definition at line 64 of file server_base.cpp.
|
inlinevirtual |
Definition at line 86 of file server_base.hpp.
|
inlineprotectedvirtual |
Reimplemented in wesnothd::server.
Definition at line 155 of file server_base.hpp.
Referenced by serve().
void server_base::async_send_doc_queued | ( | SocketPtr | socket, |
simple_wml::document & | doc | ||
) |
High level wrapper for sending a WML document.
This function returns before send is finished. This function can be called again on same socket before previous send was finished. WML documents are kept in internal queue and sent in FIFO order.
socket | |
doc | Document to send. A copy of it will be made so there is no need to keep the reference live after the function returns. |
Definition at line 586 of file server_base.cpp.
References simple_wml::document::clone(), e, and io_service_.
Referenced by async_send_error(), async_send_warning(), wesnothd::server::handle_player(), campaignd::server::handle_request_campaign_list(), campaignd::server::handle_server_id(), wesnothd::server::login_client(), campaignd::server::send_error(), campaignd::server::send_message(), wesnothd::server::send_password_request(), wesnothd::server::send_server_message(), and wesnothd::server::send_to_player().
template void server_base::async_send_error< tls_socket_ptr > | ( | SocketPtr | socket, |
const std::string & | msg, | ||
const char * | error_code = "" , |
||
const info_table & | info = {} |
||
) |
Definition at line 598 of file server_base.cpp.
References simple_wml::node::add_child(), async_send_doc_queued(), simple_wml::document::child(), lg::info(), wfl::msg(), simple_wml::document::root(), simple_wml::node::set_attr(), and simple_wml::node::set_attr_dup().
Referenced by wesnothd::server::authenticate(), coro_receive_doc(), wesnothd::server::is_login_allowed(), wesnothd::server::kick_handler(), wesnothd::server::kickban_handler(), wesnothd::server::login_client(), and serve().
template void server_base::async_send_warning< tls_socket_ptr > | ( | SocketPtr | socket, |
const std::string & | msg, | ||
const char * | warning_code = "" , |
||
const info_table & | info = {} |
||
) |
Definition at line 612 of file server_base.cpp.
References simple_wml::node::add_child(), async_send_doc_queued(), simple_wml::document::child(), lg::info(), wfl::msg(), simple_wml::document::root(), simple_wml::node::set_attr(), and simple_wml::node::set_attr_dup().
Referenced by wesnothd::server::authenticate().
template std::unique_ptr< simple_wml::document > server_base::coro_receive_doc< tls_socket_ptr > | ( | SocketPtr | socket, |
const boost::asio::yield_context & | yield | ||
) |
Receive WML document from a coroutine.
socket | |
yield | The function will suspend on read operation using this yield context |
Definition at line 526 of file server_base.cpp.
References async_send_error(), check_error(), simple_wml::document::document_size_limit, e, ERR_SERVER, log_address(), and utf8::size().
Referenced by wesnothd::server::handle_player(), wesnothd::server::login_client(), and campaignd::server::serve_requests().
template void server_base::coro_send_doc< tls_socket_ptr > | ( | SocketPtr | socket, |
simple_wml::document & | doc, | ||
const boost::asio::yield_context & | yield | ||
) |
Send a WML document from within a coroutine.
socket | |
doc | |
yield | The function will suspend on write operation using this yield context |
Definition at line 314 of file server_base.cpp.
References check_error(), dump_wml, e, log_address(), simple_wml::document::output(), simple_wml::document::output_compressed(), s, utf8::size(), and WRN_CONFIG.
Referenced by campaignd::server::handle_request_campaign(), wesnothd::server::login_client(), and send_doc_queued().
void server_base::coro_send_file | ( | const socket_ptr & | socket, |
const std::string & | filename, | ||
const boost::asio::yield_context & | yield | ||
) |
Send contents of entire file directly to socket from within a coroutine.
socket | |
filename | |
yield | The function will suspend on write operations using this yield context |
Definition at line 519 of file server_base.cpp.
References coro_send_file_userspace(), and filename.
Referenced by campaignd::server::handle_request_campaign(), and campaignd::server::handle_request_campaign_hash().
void server_base::coro_send_file | ( | tls_socket_ptr | socket, |
const std::string & | filename, | ||
const boost::asio::yield_context & | yield | ||
) |
Definition at line 514 of file server_base.cpp.
References coro_send_file_userspace(), and filename.
|
protectedpure virtual |
Implemented in wesnothd::server, campaignd::server, wesnothd::server, and campaignd::server.
Referenced by serve().
|
protectedpure virtual |
Implemented in wesnothd::server, campaignd::server, wesnothd::server, and campaignd::server.
|
protectedpure virtual |
Implemented in wesnothd::server, and campaignd::server.
Referenced by read_from_fifo().
|
protectedpure virtual |
Implemented in wesnothd::server, and campaignd::server.
Referenced by start_server().
std::string server_base::hash_password | ( | const std::string & | pw, |
const std::string & | salt, | ||
const std::string & | username | ||
) |
Handles hashing the password provided by the player before comparing it to the hashed password in the forum database.
pw | The plaintext password. |
salt | The salt as retrieved from the forum database. |
username | The player attempting to log in. |
Definition at line 643 of file server_base.cpp.
References utils::md5::base64_digest(), lg::err(), ERR_SERVER, utils::bcrypt::from_salted_salt(), utils::md5::get_iteration_count(), utils::md5::get_salt(), utils::bcrypt::hash_pw(), utils::md5::is_valid_prefix(), and utils::bcrypt::is_valid_prefix().
Referenced by wesnothd::server::authenticate(), and campaignd::server::authenticate_forum().
|
inlineprotectedvirtual |
Reimplemented in wesnothd::server.
Definition at line 156 of file server_base.hpp.
Referenced by serve().
|
inlineprotectedvirtual |
Reimplemented in wesnothd::server.
Definition at line 165 of file server_base.hpp.
Referenced by serve().
|
protected |
Definition at line 626 of file server_base.cpp.
References tls_context_, and tls_enabled_.
Referenced by campaignd::server::load_config(), and wesnothd::server::load_config().
|
protected |
Definition at line 243 of file server_base.cpp.
References admin_cmd_, handle_read_from_fifo(), and input_.
Referenced by campaignd::server::handle_read_from_fifo(), wesnothd::server::handle_read_from_fifo(), campaignd::server::load_config(), and wesnothd::server::setup_fifo().
int server_base::run | ( | ) |
Definition at line 252 of file server_base.cpp.
References e, ERR_SERVER, io_service_, and LOG_SERVER.
Referenced by main(), and run_campaignd().
|
private |
Definition at line 569 of file server_base.cpp.
References coro_send_doc(), ON_SCOPE_EXIT, and utf8::size().
|
protected |
Definition at line 103 of file server_base.cpp.
References accepting_connections(), async_send_error(), check_error(), client_address(), DBG_SERVER, utils::decayed_is_same, e, ERR_SERVER, handle_new_client(), handshake_response_, io_service_, ip_exceeds_connection_limit(), is_ip_banned(), keep_alive_, log_address(), LOG_SERVER, tls_context_, and tls_enabled_.
Referenced by start_server().
|
protected |
Definition at line 78 of file server_base.cpp.
References acceptor_v4_, acceptor_v6_, e, handle_sighup(), handshake_response_, io_service_, port_, serve(), and sighup_.
Referenced by campaignd::server::server(), and wesnothd::server::server().
|
protected |
Definition at line 143 of file server_base.hpp.
Referenced by wesnothd::server::restart_handler(), wesnothd::server::shut_down_handler(), and start_server().
|
protected |
Definition at line 142 of file server_base.hpp.
Referenced by wesnothd::server::restart_handler(), wesnothd::server::shut_down_handler(), and start_server().
|
protected |
Definition at line 172 of file server_base.hpp.
Referenced by campaignd::server::handle_read_from_fifo(), wesnothd::server::handle_read_from_fifo(), and read_from_fifo().
|
protected |
Definition at line 169 of file server_base.hpp.
Referenced by campaignd::server::load_config().
|
protected |
Definition at line 150 of file server_base.hpp.
Referenced by serve(), and start_server().
|
protected |
Definition at line 168 of file server_base.hpp.
Referenced by campaignd::server::load_config(), wesnothd::server::load_config(), read_from_fifo(), and wesnothd::server::setup_fifo().
|
protected |
Definition at line 139 of file server_base.hpp.
Referenced by async_send_doc_queued(), campaignd::server::handle_new_client(), wesnothd::server::handle_new_client(), wesnothd::server::handle_player_in_lobby(), wesnothd::server::login_client(), run(), serve(), and start_server().
|
protected |
Definition at line 138 of file server_base.hpp.
Referenced by serve().
|
protected |
Definition at line 137 of file server_base.hpp.
Referenced by campaignd::server::load_config(), campaignd::server::server(), and start_server().
|
protected |
Definition at line 174 of file server_base.hpp.
Referenced by campaignd::server::handle_sighup(), wesnothd::server::handle_sighup(), and start_server().
|
protected |
Definition at line 140 of file server_base.hpp.
Referenced by load_tls_config(), and serve().
|
protected |
Definition at line 141 of file server_base.hpp.
Referenced by load_tls_config(), and serve().