The Battle for Wesnoth  1.17.0-dev
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | 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:

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 ()
 
void run ()
 
template<class SocketPtr >
void coro_send_doc (SocketPtr socket, simple_wml::document &doc, boost::asio::yield_context yield)
 Send a WML document from within a coroutine. More...
 
void coro_send_file (socket_ptr socket, const std::string &filename, 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, boost::asio::yield_context yield)
 
template<class SocketPtr >
std::unique_ptr< simple_wml::documentcoro_receive_doc (SocketPtr socket, 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 create_unsecure_nonce (int length=8)
 Create the poor security nonce for use with passwords still hashed with MD5. More...
 
std::string create_secure_nonce ()
 Create a good security nonce for use with bcrypt/crypt_blowfish hashing. More...
 
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 (boost::asio::yield_context yield, boost::asio::ip::tcp::acceptor &acceptor, 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 std::string is_ip_banned (const std::string &)
 
virtual bool ip_exceeds_connection_limit (const std::string &) const
 
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
 
void handle_termination (const boost::system::error_code &error, int signal_number)
 

Protected Attributes

unsigned short port_
 
bool keep_alive_
 
boost::asio::io_service 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_
 
boost::asio::signal_set sigs_
 

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 78 of file server_base.hpp.

Member Typedef Documentation

◆ info_table

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

Definition at line 117 of file server_base.hpp.

Constructor & Destructor Documentation

◆ server_base()

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

Definition at line 65 of file server_base.cpp.

◆ ~server_base()

virtual server_base::~server_base ( )
inlinevirtual

Definition at line 82 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 165 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 518 of file server_base.cpp.

References simple_wml::document::clone(), coro_send_doc(), io_service_, and utf8::size().

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,
boost::asio::yield_context  yield 
)

Receive WML document from a coroutine.

Parameters
socket
yieldThe function will suspend on read operation using this yield context

Definition at line 477 of file server_base.cpp.

References async_send_error(), simple_wml::document::document_size_limit, e, ERR_SERVER, log_address(), game::error::message, 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,
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 283 of file server_base.cpp.

References simple_wml::string_span::begin(), dump_wml, e, log_address(), game::error::message, simple_wml::document::output(), simple_wml::document::output_compressed(), s, utf8::size(), simple_wml::string_span::size(), and WRN_CONFIG.

Referenced by async_send_doc_queued(), campaignd::server::handle_request_campaign(), and wesnothd::server::login_client().

◆ coro_send_file() [1/2]

void server_base::coro_send_file ( socket_ptr  socket,
const std::string &  filename,
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 470 of file server_base.cpp.

References coro_send_file_userspace().

Referenced by coro_send_file_userspace(), 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,
boost::asio::yield_context  yield 
)

Definition at line 465 of file server_base.cpp.

References coro_send_file_userspace().

◆ create_secure_nonce()

std::string server_base::create_secure_nonce ( )

Create a good security nonce for use with bcrypt/crypt_blowfish hashing.

Uses 32 random Base64 characters, cryptographic-strength, 192 bits entropy

Returns
The nonce to use.

◆ create_unsecure_nonce()

std::string server_base::create_unsecure_nonce ( int  length = 8)

Create the poor security nonce for use with passwords still hashed with MD5.

Uses 8 random integer digits, 29.8 bits entropy.

Parameters
lengthHow many random numbers to generate.
Returns
The nonce to use.

◆ handle_new_client() [1/2]

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

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

Referenced by serve().

◆ handle_new_client() [2/2]

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

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

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

◆ handle_termination()

void server_base::handle_termination ( const boost::system::error_code &  error,
int  signal_number 
)
protected

Definition at line 217 of file server_base.cpp.

References LOG_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 583 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(), utils::bcrypt::is_valid_prefix(), and game::error::what().

Referenced by wesnothd::server::authenticate().

◆ ip_exceeds_connection_limit()

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

Reimplemented in wesnothd::server.

Definition at line 167 of file server_base.hpp.

Referenced by serve().

◆ is_ip_banned()

virtual std::string server_base::is_ip_banned ( const std::string &  )
inlineprotectedvirtual

Reimplemented in wesnothd::server.

Definition at line 166 of file server_base.hpp.

Referenced by serve().

◆ load_tls_config()

void server_base::load_tls_config ( const config cfg)
protected

Definition at line 566 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()

void server_base::run ( )

Definition at line 229 of file server_base.cpp.

References e, io_service_, LOG_SERVER, and game::error::what().

Referenced by main(), and run_campaignd().

◆ serve()

void server_base::serve ( boost::asio::yield_context  yield,
boost::asio::ip::tcp::acceptor &  acceptor,
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 171 of file server_base.hpp.

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

◆ handshake_response_

uint32_t server_base::handshake_response_
protected

Definition at line 160 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_service server_base::io_service_
protected

◆ keep_alive_

bool server_base::keep_alive_
protected

Definition at line 148 of file server_base.hpp.

Referenced by serve().

◆ port_

unsigned short server_base::port_
protected

◆ sighup_

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

◆ sigs_

boost::asio::signal_set server_base::sigs_
protected

Definition at line 179 of file server_base.hpp.

Referenced by start_server().

◆ tls_context_

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

Definition at line 150 of file server_base.hpp.

Referenced by load_tls_config(), and serve().

◆ tls_enabled_

bool server_base::tls_enabled_ { false }
protected

Definition at line 151 of file server_base.hpp.

Referenced by load_tls_config(), and serve().


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