diff options
Diffstat (limited to 'Userland/Services/TelnetServer/Client.cpp')
-rw-r--r-- | Userland/Services/TelnetServer/Client.cpp | 115 |
1 files changed, 82 insertions, 33 deletions
diff --git a/Userland/Services/TelnetServer/Client.cpp b/Userland/Services/TelnetServer/Client.cpp index 2839773bde..2a3e6b866d 100644 --- a/Userland/Services/TelnetServer/Client.cpp +++ b/Userland/Services/TelnetServer/Client.cpp @@ -11,59 +11,100 @@ #include <AK/StringBuilder.h> #include <AK/StringView.h> #include <AK/Types.h> +#include <LibCore/EventLoop.h> #include <LibCore/Notifier.h> -#include <LibCore/TCPSocket.h> #include <stdio.h> #include <unistd.h> -Client::Client(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd) +Client::Client(int id, Core::Stream::TCPSocket socket, int ptm_fd) : m_id(id) , m_socket(move(socket)) , m_ptm_fd(ptm_fd) , m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Read)) { - m_socket->on_ready_to_read = [this] { drain_socket(); }; - m_ptm_notifier->on_ready_to_read = [this] { drain_pty(); }; - m_parser.on_command = [this](const Command& command) { handle_command(command); }; + m_socket.on_ready_to_read = [this] { + auto result = drain_socket(); + if (result.is_error()) { + dbgln("Failed to drain the socket: {}", result.error()); + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); + } + }; + + m_ptm_notifier->on_ready_to_read = [this] { + auto result = drain_pty(); + if (result.is_error()) { + dbgln("Failed to drain the PTY: {}", result.error()); + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); + } + }; + + m_parser.on_command = [this](const Command& command) { + auto result = handle_command(command); + if (result.is_error()) { + dbgln("Failed to handle the command: {}", result.error()); + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); + } + }; + m_parser.on_data = [this](StringView data) { handle_data(data); }; m_parser.on_error = [this]() { handle_error(); }; - send_commands({ +} + +ErrorOr<NonnullRefPtr<Client>> Client::create(int id, Core::Stream::TCPSocket socket, int ptm_fd) +{ + auto client = adopt_ref(*new Client(id, move(socket), ptm_fd)); + + auto result = client->send_commands({ { CMD_WILL, SUB_SUPPRESS_GO_AHEAD }, { CMD_WILL, SUB_ECHO }, { CMD_DO, SUB_SUPPRESS_GO_AHEAD }, { CMD_DONT, SUB_ECHO }, }); + if (result.is_error()) { + client->quit(); + return result.release_error(); + } + + return client; } -void Client::drain_socket() +ErrorOr<void> Client::drain_socket() { NonnullRefPtr<Client> protect(*this); - while (m_socket->can_read()) { - auto buf = m_socket->read(1024); - m_parser.write(buf); + auto maybe_buffer = ByteBuffer::create_uninitialized(1024); + if (!maybe_buffer.has_value()) + return ENOMEM; + auto buffer = maybe_buffer.release_value(); + + while (TRY(m_socket.can_read_without_blocking())) { + auto nread = TRY(m_socket.read(buffer)); - if (m_socket->eof()) { - quit(); + m_parser.write({ buffer.data(), nread }); + + if (m_socket.is_eof()) { + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); break; } } + + return {}; } -void Client::drain_pty() +ErrorOr<void> Client::drain_pty() { u8 buffer[BUFSIZ]; ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer)); if (nread < 0) { - perror("read(ptm)"); - quit(); - return; + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); + return static_cast<ErrnoCode>(errno); } if (nread == 0) { - quit(); - return; + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); + return {}; } - send_data(StringView(buffer, (size_t)nread)); + + return send_data({ buffer, (size_t)nread }); } void Client::handle_data(StringView data) @@ -71,7 +112,7 @@ void Client::handle_data(StringView data) write(m_ptm_fd, data.characters_without_null_termination(), data.length()); } -void Client::handle_command(const Command& command) +ErrorOr<void> Client::handle_command(const Command& command) { switch (command.command) { case CMD_DO: @@ -87,10 +128,10 @@ void Client::handle_command(const Command& command) case SUB_ECHO: // we always want to be the ones in control of the output. tell // the client to disable local echo. - send_command({ CMD_DONT, SUB_ECHO }); + TRY(send_command({ CMD_DONT, SUB_ECHO })); break; case SUB_SUPPRESS_GO_AHEAD: - send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD }); + TRY(send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD })); break; default: // don't respond to unknown commands @@ -102,14 +143,16 @@ void Client::handle_command(const Command& command) // won't do. break; } + + return {}; } void Client::handle_error() { - quit(); + Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); }); } -void Client::send_data(StringView data) +ErrorOr<void> Client::send_data(StringView data) { bool fast = true; for (size_t i = 0; i < data.length(); i++) { @@ -119,8 +162,8 @@ void Client::send_data(StringView data) } if (fast) { - m_socket->write(data); - return; + TRY(m_socket.write({ data.characters_without_null_termination(), data.length() })); + return {}; } StringBuilder builder; @@ -140,31 +183,37 @@ void Client::send_data(StringView data) } } - m_socket->write(builder.to_string()); + auto builder_contents = builder.to_byte_buffer(); + TRY(m_socket.write(builder_contents)); + return {}; } -void Client::send_command(Command command) +ErrorOr<void> Client::send_command(Command command) { - send_commands({ command }); + return send_commands({ command }); } -void Client::send_commands(Vector<Command> commands) +ErrorOr<void> Client::send_commands(Vector<Command> commands) { - auto buffer = ByteBuffer::create_uninitialized(commands.size() * 3).release_value(); // FIXME: Handle possible OOM situation. + auto maybe_buffer = ByteBuffer::create_uninitialized(commands.size() * 3); + if (!maybe_buffer.has_value()) + return ENOMEM; + auto buffer = maybe_buffer.release_value(); OutputMemoryStream stream { buffer }; for (auto& command : commands) stream << (u8)IAC << command.command << command.subcommand; VERIFY(stream.is_end()); - m_socket->write(buffer.data(), buffer.size()); + TRY(m_socket.write({ buffer.data(), buffer.size() })); + return {}; } void Client::quit() { m_ptm_notifier->set_enabled(false); close(m_ptm_fd); - m_socket->close(); + m_socket.close(); if (on_exit) on_exit(); } |