fix a lot of issues on psogc; add proxy module

- $ann implemented
- concurrency removed; server is now single-threaded, event-driven and much more stable
- rare seed is no longer the game id; ids are sequential from server startup so they weren't random at all before
- supports dropping privileges; now you can run it as root so it can open a sockets on low ports, then it will switch to the given user before serving any traffic
- newserv now behaves like a proxy if you run it with the --proxy-destination=<IP_OR_HOSTNAME> argument; there's also an (invisible) shell in this mode where you can inject commands to the server or client. e.g. it can always be christmas in the lobby if you do `sc DA 01 00 00`
- increased the mtu on PSODolphinConfig's tap0 configuration; this seems to make the connection more stable
- fixed some uninitialized memory bugs
- the shell is now event-driven and now uses libevent too; unfortunately this means readline doesn't work anymore (no history and vim-like shortcuts)
- made network command display consistent for input vs. output (the header appears in both cases now)
- fixed bugs in some subcommand handling (the BB logic was being applied to non-BB clients erroneously, causing most item drops not to work at all)
- fixed player tags in the short lobby data struct. unclear if this was actually a problem but it was inconsistent with other servers
- fixed "unused" field in game join command (actually it appears to be disable_udp and should be 1, not 0)
- cleaned up Server abstraction a bit
- rewrote some text functions; asan was complaining about the built-in ones for some reason
- added an optional welcome message
This commit is contained in:
Martin Michelsen
2020-02-16 15:03:47 -08:00
parent 76c810c1e6
commit 0d4b0b2279
39 changed files with 1487 additions and 975 deletions
+82 -181
View File
@@ -19,7 +19,6 @@
#include <phosg/Network.hh>
#include <phosg/Strings.hh>
#include <phosg/Time.hh>
#include <thread>
#include "PSOProtocol.hh"
#include "ReceiveCommands.hh"
@@ -28,22 +27,14 @@ using namespace std;
Server::WorkerThread::WorkerThread(Server* server, int worker_num) :
server(server), worker_num(worker_num),
base(event_base_new(), event_base_free), t() {
this->thread_name = string_printf("Server::run_thread (worker_num=%d)",
worker_num);
void Server::disconnect_client(struct bufferevent* bev) {
this->disconnect_client(this->bev_to_client.at(bev));
}
void Server::WorkerThread::disconnect_client(struct bufferevent* bev) {
{
auto client = this->bev_to_client.at(bev);
this->bev_to_client.erase(bev);
this->server->client_count--;
rw_guard g(client->lock, true);
client->bev = NULL;
}
void Server::disconnect_client(shared_ptr<Client> c) {
this->bev_to_client.erase(c->bev);
struct bufferevent* bev = c->bev;
c->bev = NULL;
// if the output buffer is not empty, move the client into the draining pool
// instead of disconnecting it, to make sure all the data gets sent
@@ -54,71 +45,52 @@ void Server::WorkerThread::disconnect_client(struct bufferevent* bev) {
// the callbacks will free it when all the data is sent or the client
// disconnects
bufferevent_setcb(bev, NULL,
Server::WorkerThread::dispatch_on_disconnecting_client_output,
Server::WorkerThread::dispatch_on_disconnecting_client_error, this);
Server::dispatch_on_disconnecting_client_output,
Server::dispatch_on_disconnecting_client_error, this);
bufferevent_disable(bev, EV_READ);
}
process_disconnect(this->state, c);
}
void Server::WorkerThread::dispatch_on_listen_accept(
struct evconnlistener *listener, evutil_socket_t fd,
struct sockaddr *address, int socklen, void *ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->on_listen_accept(*wt, listener, fd, address, socklen);
void Server::dispatch_on_listen_accept(
struct evconnlistener* listener, evutil_socket_t fd,
struct sockaddr* address, int socklen, void* ctx) {
reinterpret_cast<Server*>(ctx)->on_listen_accept(listener, fd, address,
socklen);
}
void Server::WorkerThread::dispatch_on_listen_error(
struct evconnlistener *listener, void *ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->on_listen_error(*wt, listener);
void Server::dispatch_on_listen_error(struct evconnlistener* listener,
void* ctx) {
reinterpret_cast<Server*>(ctx)->on_listen_error(listener);
}
void Server::WorkerThread::dispatch_on_client_input(
struct bufferevent *bev, void *ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->on_client_input(*wt, bev);
void Server::dispatch_on_client_input(struct bufferevent* bev, void* ctx) {
reinterpret_cast<Server*>(ctx)->on_client_input(bev);
}
void Server::WorkerThread::dispatch_on_client_error(
struct bufferevent *bev, short events, void *ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->on_client_error(*wt, bev, events);
void Server::dispatch_on_client_error(struct bufferevent* bev, short events,
void* ctx) {
reinterpret_cast<Server*>(ctx)->on_client_error(bev, events);
}
void Server::WorkerThread::dispatch_on_disconnecting_client_output(
struct bufferevent *bev, void *ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->on_disconnecting_client_output(*wt, bev);
void Server::dispatch_on_disconnecting_client_output(struct bufferevent* bev,
void* ctx) {
reinterpret_cast<Server*>(ctx)->on_disconnecting_client_output(bev);
}
void Server::WorkerThread::dispatch_on_disconnecting_client_error(
struct bufferevent *bev, short events, void *ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->on_disconnecting_client_error(*wt, bev, events);
void Server::dispatch_on_disconnecting_client_error(struct bufferevent* bev,
short events, void* ctx) {
reinterpret_cast<Server*>(ctx)->on_disconnecting_client_error(bev, events);
}
void Server::WorkerThread::dispatch_check_for_thread_exit(
evutil_socket_t fd, short what, void* ctx) {
WorkerThread* wt = (WorkerThread*)ctx;
wt->server->check_for_thread_exit(*wt, fd, what);
}
void Server::on_listen_accept(Server::WorkerThread& wt,
struct evconnlistener *listener, evutil_socket_t fd,
struct sockaddr *address, int socklen) {
int fd_flags = fcntl(fd, F_GETFD, 0);
if (fd_flags >= 0) {
fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC);
}
void Server::on_listen_accept(struct evconnlistener* listener,
evutil_socket_t fd, struct sockaddr* address, int socklen) {
int listen_fd = evconnlistener_get_fd(listener);
GameVersion version;
ServerBehavior initial_state;
ListeningSocket* listening_socket;
try {
auto p = this->listen_fd_to_version_and_state.at(listen_fd);
version = p.first;
initial_state = p.second;
listening_socket = &this->listening_sockets.at(listen_fd);
} catch (const out_of_range& e) {
log(WARNING, "[Server] can\'t determine version for socket %d; disconnecting client",
listen_fd);
@@ -126,88 +98,76 @@ void Server::on_listen_accept(Server::WorkerThread& wt,
return;
}
struct bufferevent *bev = bufferevent_socket_new(wt.base.get(), fd,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE | BEV_OPT_DEFER_CALLBACKS | BEV_OPT_UNLOCK_CALLBACKS);
shared_ptr<Client> c(new Client(bev, version, initial_state));
auto emplace_ret = wt.bev_to_client.emplace(make_pair(bev, c));
this->client_count++;
log(INFO, "[Server] client connected via fd %d", listen_fd);
bufferevent_setcb(bev, &WorkerThread::dispatch_on_client_input, NULL,
&WorkerThread::dispatch_on_client_error, &wt);
struct bufferevent *bev = bufferevent_socket_new(this->base.get(), fd,
BEV_OPT_CLOSE_ON_FREE | BEV_OPT_DEFER_CALLBACKS);
shared_ptr<Client> c(new Client(bev, listening_socket->version,
listening_socket->behavior));
this->bev_to_client.emplace(make_pair(bev, c));
bufferevent_setcb(bev, &Server::dispatch_on_client_input, NULL,
&Server::dispatch_on_client_error, this);
bufferevent_enable(bev, EV_READ | EV_WRITE);
this->process_client_connect(emplace_ret.first->second);
process_connect(this->state, c);
}
void Server::on_listen_error(Server::WorkerThread& wt,
struct evconnlistener *listener) {
void Server::on_listen_error(struct evconnlistener* listener) {
int err = EVUTIL_SOCKET_ERROR();
log(ERROR, "[Server] failure on listening socket %d: %d (%s)\n",
evconnlistener_get_fd(listener), err,
evutil_socket_error_to_string(err));
event_base_loopexit(wt.base.get(), NULL);
log(ERROR, "[Server] failure on listening socket %d: %d (%s)",
evconnlistener_get_fd(listener), err, evutil_socket_error_to_string(err));
event_base_loopexit(this->base.get(), NULL);
}
void Server::on_client_input(Server::WorkerThread& wt,
struct bufferevent *bev) {
void Server::on_client_input(struct bufferevent* bev) {
shared_ptr<Client> c;
try {
c = wt.bev_to_client.at(bev);
c = this->bev_to_client.at(bev);
} catch (const out_of_range& e) {
log(WARNING, "[Server] received message from client with no configuration");
// ignore all the data
// TODO: we probably should disconnect them or something
struct evbuffer* in_buffer = bufferevent_get_input(bev);
evbuffer_drain(in_buffer, evbuffer_get_length(in_buffer));
return;
}
if (c->should_disconnect) {
wt.disconnect_client(bev);
this->process_client_disconnect(c);
this->disconnect_client(bev);
return;
}
c->last_recv_time = now();
this->receive_and_process_commands(c, bev);
this->receive_and_process_commands(c);
if (c->should_disconnect) {
wt.disconnect_client(bev);
this->process_client_disconnect(c);
this->disconnect_client(bev);
return;
}
}
void Server::on_disconnecting_client_output(Server::WorkerThread& wt,
struct bufferevent *bev) {
void Server::on_disconnecting_client_output(struct bufferevent* bev) {
bufferevent_free(bev);
}
void Server::on_client_error(Server::WorkerThread& wt,
struct bufferevent *bev, short events) {
shared_ptr<Client> c;
try {
c = wt.bev_to_client.at(bev);
} catch (const out_of_range& e) { }
void Server::on_client_error(struct bufferevent* bev, short events) {
if (events & BEV_EVENT_ERROR) {
int err = EVUTIL_SOCKET_ERROR();
log(WARNING, "[Server] client caused %d (%s)\n", err,
log(WARNING, "[Server] client caused %d (%s)", err,
evutil_socket_error_to_string(err));
}
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
wt.disconnect_client(bev);
if (c) {
this->process_client_disconnect(c);
}
this->disconnect_client(bev);
}
}
void Server::on_disconnecting_client_error(Server::WorkerThread& wt,
struct bufferevent *bev, short events) {
void Server::on_disconnecting_client_error(struct bufferevent* bev,
short events) {
if (events & BEV_EVENT_ERROR) {
int err = EVUTIL_SOCKET_ERROR();
log(WARNING, "[Server] disconnecting client caused %d (%s)\n", err,
log(WARNING, "[Server] disconnecting client caused %d (%s)", err,
evutil_socket_error_to_string(err));
}
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
@@ -215,15 +175,8 @@ void Server::on_disconnecting_client_error(Server::WorkerThread& wt,
}
}
void Server::check_for_thread_exit(Server::WorkerThread& wt,
evutil_socket_t fd, short what) {
if (this->should_exit) {
event_base_loopexit(wt.base.get(), NULL);
}
}
void Server::receive_and_process_commands(shared_ptr<Client> c, struct bufferevent* bev) {
struct evbuffer* buf = bufferevent_get_input(bev);
void Server::receive_and_process_commands(shared_ptr<Client> c) {
struct evbuffer* buf = bufferevent_get_input(c->bev);
size_t header_size = (c->version == GameVersion::BB) ? 8 : 4;
// read as much data into recv_buffer as we can and decrypt it
@@ -274,92 +227,40 @@ void Server::receive_and_process_commands(shared_ptr<Client> c, struct buffereve
c->recv_buffer = c->recv_buffer.substr(offset);
}
void Server::process_client_connect(std::shared_ptr<Client> c) {
process_connect(this->state, c);
}
Server::Server(shared_ptr<struct event_base> base,
shared_ptr<ServerState> state) : base(base), state(state) { }
void Server::process_client_disconnect(std::shared_ptr<Client> c) {
process_disconnect(this->state, c);
}
void Server::run_thread(int worker_num) {
WorkerThread& wt = this->threads[worker_num];
struct timeval tv = usecs_to_timeval(2000000);
struct event* ev = event_new(wt.base.get(), -1, EV_PERSIST,
&WorkerThread::dispatch_check_for_thread_exit, &wt);
event_add(ev, &tv);
event_base_dispatch(wt.base.get());
event_del(ev);
}
Server::Server(shared_ptr<ServerState> state) :
should_exit(false), client_count(0), state(state) {
for (size_t x = 0; x < this->state->num_threads; x++) {
this->threads.emplace_back(this, x);
}
}
void Server::listen(const string& socket_path, GameVersion version, ServerBehavior initial_state) {
void Server::listen(const string& socket_path, GameVersion version,
ServerBehavior behavior) {
int fd = ::listen(socket_path, 0, SOMAXCONN);
log(INFO, "[Server] listening on unix socket %s (version %s) on fd %d",
socket_path.c_str(), name_for_version(version), fd);
this->add_socket(fd, version, initial_state);
this->add_socket(fd, version, behavior);
}
void Server::listen(const string& addr, int port, GameVersion version, ServerBehavior initial_state) {
void Server::listen(const string& addr, int port, GameVersion version,
ServerBehavior behavior) {
int fd = ::listen(addr, port, SOMAXCONN);
string netloc_str = render_netloc(addr, port);
log(INFO, "[Server] listening on tcp interface %s (version %s) on fd %d",
netloc_str.c_str(), name_for_version(version), fd);
this->add_socket(fd, version, initial_state);
this->add_socket(fd, version, behavior);
}
void Server::listen(int port, GameVersion version, ServerBehavior initial_state) {
this->listen("", port, version, initial_state);
void Server::listen(int port, GameVersion version, ServerBehavior behavior) {
this->listen("", port, version, behavior);
}
void Server::add_socket(int fd, GameVersion version, ServerBehavior initial_state) {
this->listen_fd_to_version_and_state.emplace(piecewise_construct,
forward_as_tuple(fd), forward_as_tuple(version, initial_state));
Server::ListeningSocket::ListeningSocket(Server* s, int fd,
GameVersion version, ServerBehavior behavior) :
fd(fd), version(version), behavior(behavior), listener(
evconnlistener_new(s->base.get(), Server::dispatch_on_listen_accept, s,
LEV_OPT_REUSEABLE, 0, this->fd), evconnlistener_free) {
evconnlistener_set_error_cb(this->listener.get(),
Server::dispatch_on_listen_error);
}
void Server::start() {
for (auto& wt : this->threads) {
for (const auto& it : this->listen_fd_to_version_and_state) {
struct evconnlistener* listener = evconnlistener_new(wt.base.get(),
WorkerThread::dispatch_on_listen_accept, &wt, LEV_OPT_REUSEABLE, 0,
it.first);
if (!listener) {
throw runtime_error("can\'t create evconnlistener");
}
evconnlistener_set_error_cb(listener, WorkerThread::dispatch_on_listen_error);
wt.listeners.emplace(listener, evconnlistener_free);
}
wt.t = thread(&Server::run_thread, this, wt.worker_num);
}
}
void Server::schedule_stop() {
log(INFO, "[Server] scheduling exit for all threads");
this->should_exit = true;
for (const auto& it : listen_fd_to_version_and_state) {
log(INFO, "[Server] closing listening fd %d", it.first);
close(it.first);
}
}
void Server::wait_for_stop() {
for (auto& wt : this->threads) {
if (!wt.t.joinable()) {
continue;
}
log(INFO, "[Server] waiting for worker %d to terminate", wt.worker_num);
wt.t.join();
}
log(INFO, "[Server] shutdown complete");
void Server::add_socket(int fd, GameVersion version, ServerBehavior behavior) {
this->listening_sockets.emplace(piecewise_construct, forward_as_tuple(fd),
forward_as_tuple(this, fd, version, behavior));
}