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
+49 -77
View File
@@ -18,14 +18,14 @@ using namespace std;
DNSServer::DNSServer(uint32_t local_connect_address,
uint32_t external_connect_address) :
should_exit(false), local_connect_address(local_connect_address),
DNSServer::DNSServer(shared_ptr<struct event_base> base,
uint32_t local_connect_address, uint32_t external_connect_address) :
base(base), local_connect_address(local_connect_address),
external_connect_address(external_connect_address) { }
DNSServer::~DNSServer() {
for (int fd : this->fds) {
close(fd);
for (const auto& it : this->fd_to_receive_event) {
close(it.first);
}
}
@@ -42,94 +42,66 @@ void DNSServer::listen(int port) {
}
void DNSServer::add_socket(int fd) {
this->fds.emplace(fd);
unique_ptr<struct event, void(*)(struct event*)> e(event_new(this->base.get(),
fd, EV_READ | EV_PERSIST, &DNSServer::dispatch_on_receive_message,
this), event_free);
event_add(e.get(), NULL);
this->fd_to_receive_event.emplace(fd, move(e));
}
void DNSServer::start() {
this->t = thread(&DNSServer::run_thread, this);
void DNSServer::dispatch_on_receive_message(evutil_socket_t fd,
short events, void* ctx) {
reinterpret_cast<DNSServer*>(ctx)->on_receive_message(fd, events);
}
void DNSServer::schedule_stop() {
this->should_exit = true;
}
void DNSServer::wait_for_stop() {
this->t.join();
}
void DNSServer::run_thread() {
vector<pollfd> poll_fds;
for (int fd : this->fds) {
poll_fds.emplace_back();
auto& pfd = poll_fds.back();
pfd.fd = fd;
pfd.events = POLLIN;
pfd.revents = 0;
}
while (!this->should_exit) {
void DNSServer::on_receive_message(int fd, short event) {
for (;;) {
sockaddr_in remote;
socklen_t remote_size = sizeof(sockaddr_in);
memset(&remote, 0, remote_size);
// 10 second timeout
int num_fds = poll(poll_fds.data(), poll_fds.size(), 10000);
if (num_fds < 0) {
auto s = string_for_error(errno);
log(ERROR, "DNS server terminating due to error: %s", s.c_str());
string input(2048, 0);
ssize_t bytes = recvfrom(fd, const_cast<char*>(input.data()), input.size(),
0, reinterpret_cast<sockaddr*>(&remote), &remote_size);
if (bytes < 0) {
if (errno != EAGAIN) {
log(INFO, "[DNSServer] input error %d", errno);
throw runtime_error("cannot read from udp socket");
}
break;
}
if (num_fds == 0) {
continue;
}
} else if (bytes == 0) {
break;
for (const auto& pfd : poll_fds) {
if (!(pfd.revents & POLLIN)) {
continue;
} else { // bytes > 0
input.resize(bytes);
uint32_t remote_address = bswap32(remote.sin_addr.s_addr);
uint32_t connect_address;
if (is_local_address(remote_address)) {
connect_address = this->local_connect_address;
} else {
connect_address = this->external_connect_address;
}
string input(2048, 0);
ssize_t bytes = recvfrom(pfd.fd, const_cast<char*>(input.data()),
input.size(), 0, reinterpret_cast<sockaddr*>(&remote), &remote_size);
if (bytes > 0) {
input.resize(bytes);
if (input.size() >= 0x0C) {
string response;
size_t name_len = strlen(input.data() + 0x0C) + 1;
uint32_t remote_address = bswap32(remote.sin_addr.s_addr);
uint32_t connect_address;
if (is_local_address(remote_address)) {
connect_address = this->local_connect_address;
} else {
connect_address = this->external_connect_address;
}
uint32_t connect_address_be = bswap32(connect_address);
response.append(input.substr(0, 2));
response.append("\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00", 10);
response.append(input.substr(12, name_len));
response.append("\x00\x01\x00\x01\xC0\x0C\x00\x01\x00\x01\x00\x00\x00\x3C\x00\x04", 16);
response.append(reinterpret_cast<const char*>(&connect_address_be), 4);
string output = this->build_response(input, connect_address);
if (!output.empty()) {
sendto(pfd.fd, output.data(), output.size(), 0,
reinterpret_cast<sockaddr*>(&remote), remote_size);
}
sendto(fd, response.data(), response.size(), 0,
reinterpret_cast<const sockaddr*>(&remote), remote_size);
} else {
log(WARNING, "[DNSServer] input query too small");
print_data(stderr, input);
}
}
}
}
string DNSServer::build_response(const std::string& input,
uint32_t connect_address) {
if (input.size() < 0x0C) {
return "";
}
string ret;
size_t name_len = strlen(input.data() + 0x0C) + 1;
uint32_t connect_address_be = bswap32(connect_address);
ret.append(input.substr(0, 2));
ret.append("\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00", 10);
ret.append(input.substr(12, name_len));
ret.append("\x00\x01\x00\x01\xC0\x0C\x00\x01\x00\x01\x00\x00\x00\x3C\x00\x04", 16);
ret.append(reinterpret_cast<const char*>(&connect_address_be), 4);
return ret;
}