Client.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Client.h"
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/MemoryStream.h>
  9. #include <AK/String.h>
  10. #include <AK/StringBuilder.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Types.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibCore/Notifier.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. Client::Client(int id, NonnullOwnPtr<Core::Stream::TCPSocket> socket, int ptm_fd)
  18. : m_id(id)
  19. , m_socket(move(socket))
  20. , m_ptm_fd(ptm_fd)
  21. , m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Read))
  22. {
  23. m_socket->on_ready_to_read = [this] {
  24. auto result = drain_socket();
  25. if (result.is_error()) {
  26. dbgln("Failed to drain the socket: {}", result.error());
  27. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  28. }
  29. };
  30. m_ptm_notifier->on_ready_to_read = [this] {
  31. auto result = drain_pty();
  32. if (result.is_error()) {
  33. dbgln("Failed to drain the PTY: {}", result.error());
  34. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  35. }
  36. };
  37. m_parser.on_command = [this](const Command& command) {
  38. auto result = handle_command(command);
  39. if (result.is_error()) {
  40. dbgln("Failed to handle the command: {}", result.error());
  41. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  42. }
  43. };
  44. m_parser.on_data = [this](StringView data) { handle_data(data); };
  45. m_parser.on_error = [this]() { handle_error(); };
  46. }
  47. ErrorOr<NonnullRefPtr<Client>> Client::create(int id, NonnullOwnPtr<Core::Stream::TCPSocket> socket, int ptm_fd)
  48. {
  49. auto client = adopt_ref(*new Client(id, move(socket), ptm_fd));
  50. auto result = client->send_commands({
  51. { CMD_WILL, SUB_SUPPRESS_GO_AHEAD },
  52. { CMD_WILL, SUB_ECHO },
  53. { CMD_DO, SUB_SUPPRESS_GO_AHEAD },
  54. { CMD_DONT, SUB_ECHO },
  55. });
  56. if (result.is_error()) {
  57. client->quit();
  58. return result.release_error();
  59. }
  60. return client;
  61. }
  62. ErrorOr<void> Client::drain_socket()
  63. {
  64. NonnullRefPtr<Client> protect(*this);
  65. auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
  66. if (!maybe_buffer.has_value())
  67. return ENOMEM;
  68. auto buffer = maybe_buffer.release_value();
  69. while (TRY(m_socket->can_read_without_blocking())) {
  70. auto nread = TRY(m_socket->read(buffer));
  71. m_parser.write({ buffer.data(), nread });
  72. if (m_socket->is_eof()) {
  73. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  74. break;
  75. }
  76. }
  77. return {};
  78. }
  79. ErrorOr<void> Client::drain_pty()
  80. {
  81. u8 buffer[BUFSIZ];
  82. ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
  83. if (nread < 0) {
  84. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  85. return static_cast<ErrnoCode>(errno);
  86. }
  87. if (nread == 0) {
  88. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  89. return {};
  90. }
  91. return send_data({ buffer, (size_t)nread });
  92. }
  93. void Client::handle_data(StringView data)
  94. {
  95. write(m_ptm_fd, data.characters_without_null_termination(), data.length());
  96. }
  97. ErrorOr<void> Client::handle_command(const Command& command)
  98. {
  99. switch (command.command) {
  100. case CMD_DO:
  101. // no response - we've already advertised our options, and none of
  102. // them can be disabled (or re-enabled) after connecting.
  103. break;
  104. case CMD_DONT:
  105. // no response - we only "support" two options (echo and suppress
  106. // go-ahead), and both of them are always enabled.
  107. break;
  108. case CMD_WILL:
  109. switch (command.subcommand) {
  110. case SUB_ECHO:
  111. // we always want to be the ones in control of the output. tell
  112. // the client to disable local echo.
  113. TRY(send_command({ CMD_DONT, SUB_ECHO }));
  114. break;
  115. case SUB_SUPPRESS_GO_AHEAD:
  116. TRY(send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD }));
  117. break;
  118. default:
  119. // don't respond to unknown commands
  120. break;
  121. }
  122. break;
  123. case CMD_WONT:
  124. // no response - we don't care about anything the client says they
  125. // won't do.
  126. break;
  127. }
  128. return {};
  129. }
  130. void Client::handle_error()
  131. {
  132. Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
  133. }
  134. ErrorOr<void> Client::send_data(StringView data)
  135. {
  136. bool fast = true;
  137. for (size_t i = 0; i < data.length(); i++) {
  138. u8 c = data[i];
  139. if (c == '\n' || c == 0xff)
  140. fast = false;
  141. }
  142. if (fast) {
  143. TRY(m_socket->write({ data.characters_without_null_termination(), data.length() }));
  144. return {};
  145. }
  146. StringBuilder builder;
  147. for (size_t i = 0; i < data.length(); i++) {
  148. u8 c = data[i];
  149. switch (c) {
  150. case '\n':
  151. builder.append("\r\n");
  152. break;
  153. case IAC:
  154. builder.append("\xff\xff");
  155. break;
  156. default:
  157. builder.append(c);
  158. break;
  159. }
  160. }
  161. auto builder_contents = builder.to_byte_buffer();
  162. TRY(m_socket->write(builder_contents));
  163. return {};
  164. }
  165. ErrorOr<void> Client::send_command(Command command)
  166. {
  167. return send_commands({ command });
  168. }
  169. ErrorOr<void> Client::send_commands(Vector<Command> commands)
  170. {
  171. auto maybe_buffer = ByteBuffer::create_uninitialized(commands.size() * 3);
  172. if (!maybe_buffer.has_value())
  173. return ENOMEM;
  174. auto buffer = maybe_buffer.release_value();
  175. OutputMemoryStream stream { buffer };
  176. for (auto& command : commands)
  177. stream << (u8)IAC << command.command << command.subcommand;
  178. VERIFY(stream.is_end());
  179. TRY(m_socket->write({ buffer.data(), buffer.size() }));
  180. return {};
  181. }
  182. void Client::quit()
  183. {
  184. m_ptm_notifier->set_enabled(false);
  185. close(m_ptm_fd);
  186. m_socket->close();
  187. if (on_exit)
  188. on_exit();
  189. }