The Battle for Wesnoth  1.19.7+dev
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
server_base Class Referenceabstract

Base class for implementing servers that use gzipped-WML network protocol. More...

#include <server_base.hpp>

Inheritance diagram for server_base:

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::documentcoro_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_infois_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)
 

Detailed Description

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.

Member Typedef Documentation

◆ info_table

typedef std::map<std::string, std::string> server_base::info_table

Definition at line 122 of file server_base.hpp.

Constructor & Destructor Documentation

◆ server_base()

server_base::server_base ( unsigned short  port,
bool  keep_alive 
)

Definition at line 64 of file server_base.cpp.

◆ ~server_base()

virtual server_base::~server_base ( )
inlinevirtual

Definition at line 86 of file server_base.hpp.

Member Function Documentation

◆ accepting_connections()

virtual bool server_base::accepting_connections ( ) const
inlineprotectedvirtual

Reimplemented in wesnothd::server.

Definition at line 155 of file server_base.hpp.

Referenced by serve().

◆ async_send_doc_queued()

template<class SocketPtr >
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.

Parameters
socket
docDocument 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().

◆ async_send_error()

template<class SocketPtr >
template void server_base::async_send_error< tls_socket_ptr > ( SocketPtr  socket,
const std::string &  msg,
const char *  error_code = "",
const info_table info = {} 
)

◆ async_send_warning()

template<class SocketPtr >
template void server_base::async_send_warning< tls_socket_ptr > ( SocketPtr  socket,
const std::string &  msg,
const char *  warning_code = "",
const info_table info = {} 
)

◆ coro_receive_doc()

template<class SocketPtr >
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.

Parameters
socket
yieldThe function will suspend on read operation using this yield context
Returns
unique_ptr with doc deceived. In case of error empty unique_ptr

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().

◆ coro_send_doc()

template<class SocketPtr >
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.

Parameters
socket
doc
yieldThe 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().

◆ coro_send_file() [1/2]

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.

Parameters
socket
filename
yieldThe 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().

◆ coro_send_file() [2/2]

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.

◆ handle_new_client() [1/2]

virtual void server_base::handle_new_client ( socket_ptr  socket)
protectedpure virtual

◆ handle_new_client() [2/2]

virtual void server_base::handle_new_client ( tls_socket_ptr  socket)
protectedpure virtual

◆ handle_read_from_fifo()

virtual void server_base::handle_read_from_fifo ( const boost::system::error_code &  error,
std::size_t  bytes_transferred 
)
protectedpure virtual

Implemented in wesnothd::server, and campaignd::server.

Referenced by read_from_fifo().

◆ handle_sighup()

virtual void server_base::handle_sighup ( const boost::system::error_code &  error,
int  signal_number 
)
protectedpure virtual

Implemented in wesnothd::server, and campaignd::server.

Referenced by start_server().

◆ hash_password()

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.

Parameters
pwThe plaintext password.
saltThe salt as retrieved from the forum database.
usernameThe player attempting to log in.
Returns
The hashed password, or empty if the password couldn't be hashed.

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().

◆ ip_exceeds_connection_limit()

virtual bool server_base::ip_exceeds_connection_limit ( const std::string &  ) const
inlineprotectedvirtual

Reimplemented in wesnothd::server.

Definition at line 156 of file server_base.hpp.

Referenced by serve().

◆ is_ip_banned()

virtual utils::optional<login_ban_info> server_base::is_ip_banned ( const std::string &  )
inlineprotectedvirtual

Reimplemented in wesnothd::server.

Definition at line 165 of file server_base.hpp.

Referenced by serve().

◆ load_tls_config()

void server_base::load_tls_config ( const config cfg)
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().

◆ read_from_fifo()

void server_base::read_from_fifo ( )
protected

◆ run()

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().

◆ send_doc_queued()

template<class SocketPtr >
void server_base::send_doc_queued ( SocketPtr  socket,
std::unique_ptr< simple_wml::document > &  doc_ptr,
boost::asio::yield_context  yield 
)
private

Definition at line 569 of file server_base.cpp.

References coro_send_doc(), ON_SCOPE_EXIT, and utf8::size().

◆ serve()

void server_base::serve ( const boost::asio::yield_context &  yield,
boost::asio::ip::tcp::acceptor &  acceptor,
const boost::asio::ip::tcp::endpoint &  endpoint 
)
protected

◆ start_server()

void server_base::start_server ( )
protected

Member Data Documentation

◆ acceptor_v4_

boost::asio::ip::tcp::acceptor server_base::acceptor_v4_
protected

◆ acceptor_v6_

boost::asio::ip::tcp::acceptor server_base::acceptor_v6_
protected

◆ admin_cmd_

boost::asio::streambuf server_base::admin_cmd_
protected

◆ fifo_path_

std::string server_base::fifo_path_
protected

Definition at line 169 of file server_base.hpp.

Referenced by campaignd::server::load_config().

◆ handshake_response_

uint32_t server_base::handshake_response_
protected

Definition at line 150 of file server_base.hpp.

Referenced by serve(), and start_server().

◆ input_

boost::asio::posix::stream_descriptor server_base::input_
protected

◆ io_service_

boost::asio::io_context server_base::io_service_
protected

◆ keep_alive_

bool server_base::keep_alive_
protected

Definition at line 138 of file server_base.hpp.

Referenced by serve().

◆ port_

unsigned short server_base::port_
protected

◆ sighup_

boost::asio::signal_set server_base::sighup_
protected

◆ tls_context_

boost::asio::ssl::context server_base::tls_context_ { boost::asio::ssl::context::sslv23 }
protected

Definition at line 140 of file server_base.hpp.

Referenced by load_tls_config(), and serve().

◆ tls_enabled_

bool server_base::tls_enabled_ { false }
protected

Definition at line 141 of file server_base.hpp.

Referenced by load_tls_config(), and serve().


The documentation for this class was generated from the following files: