Client.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Client.h"
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/MemoryStream.h>
  29. #include <AK/String.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/StringView.h>
  32. #include <AK/Types.h>
  33. #include <LibCore/Notifier.h>
  34. #include <LibCore/TCPSocket.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. Client::Client(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd)
  38. : m_id(id)
  39. , m_socket(move(socket))
  40. , m_ptm_fd(ptm_fd)
  41. , m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Read))
  42. {
  43. m_socket->on_ready_to_read = [this] { drain_socket(); };
  44. m_ptm_notifier->on_ready_to_read = [this] { drain_pty(); };
  45. m_parser.on_command = [this](const Command& command) { handle_command(command); };
  46. m_parser.on_data = [this](const StringView& data) { handle_data(data); };
  47. m_parser.on_error = [this]() { handle_error(); };
  48. send_commands({
  49. { CMD_WILL, SUB_SUPPRESS_GO_AHEAD },
  50. { CMD_WILL, SUB_ECHO },
  51. { CMD_DO, SUB_SUPPRESS_GO_AHEAD },
  52. { CMD_DONT, SUB_ECHO },
  53. });
  54. }
  55. void Client::drain_socket()
  56. {
  57. NonnullRefPtr<Client> protect(*this);
  58. while (m_socket->can_read()) {
  59. auto buf = m_socket->read(1024);
  60. m_parser.write(buf);
  61. if (m_socket->eof()) {
  62. quit();
  63. break;
  64. }
  65. }
  66. }
  67. void Client::drain_pty()
  68. {
  69. u8 buffer[BUFSIZ];
  70. ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
  71. if (nread < 0) {
  72. perror("read(ptm)");
  73. quit();
  74. return;
  75. }
  76. if (nread == 0) {
  77. quit();
  78. return;
  79. }
  80. send_data(StringView(buffer, (size_t)nread));
  81. }
  82. void Client::handle_data(const StringView& data)
  83. {
  84. write(m_ptm_fd, data.characters_without_null_termination(), data.length());
  85. }
  86. void Client::handle_command(const Command& command)
  87. {
  88. switch (command.command) {
  89. case CMD_DO:
  90. // no response - we've already advertised our options, and none of
  91. // them can be disabled (or re-enabled) after connecting.
  92. break;
  93. case CMD_DONT:
  94. // no response - we only "support" two options (echo and suppress
  95. // go-ahead), and both of them are always enabled.
  96. break;
  97. case CMD_WILL:
  98. switch (command.subcommand) {
  99. case SUB_ECHO:
  100. // we always want to be the ones in control of the output. tell
  101. // the client to disable local echo.
  102. send_command({ CMD_DONT, SUB_ECHO });
  103. break;
  104. case SUB_SUPPRESS_GO_AHEAD:
  105. send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD });
  106. break;
  107. default:
  108. // don't respond to unknown commands
  109. break;
  110. }
  111. break;
  112. case CMD_WONT:
  113. // no response - we don't care about anything the client says they
  114. // won't do.
  115. break;
  116. }
  117. }
  118. void Client::handle_error()
  119. {
  120. quit();
  121. }
  122. void Client::send_data(StringView data)
  123. {
  124. bool fast = true;
  125. for (size_t i = 0; i < data.length(); i++) {
  126. u8 c = data[i];
  127. if (c == '\n' || c == 0xff)
  128. fast = false;
  129. }
  130. if (fast) {
  131. m_socket->write(data);
  132. return;
  133. }
  134. StringBuilder builder;
  135. for (size_t i = 0; i < data.length(); i++) {
  136. u8 c = data[i];
  137. switch (c) {
  138. case '\n':
  139. builder.append("\r\n");
  140. break;
  141. case IAC:
  142. builder.append("\xff\xff");
  143. break;
  144. default:
  145. builder.append(c);
  146. break;
  147. }
  148. }
  149. m_socket->write(builder.to_string());
  150. }
  151. void Client::send_command(Command command)
  152. {
  153. send_commands({ command });
  154. }
  155. void Client::send_commands(Vector<Command> commands)
  156. {
  157. auto buffer = ByteBuffer::create_uninitialized(commands.size() * 3);
  158. OutputMemoryStream stream { buffer };
  159. for (auto& command : commands)
  160. stream << (u8)IAC << command.command << command.subcommand;
  161. ASSERT(stream.is_end());
  162. m_socket->write(buffer.data(), buffer.size());
  163. }
  164. void Client::quit()
  165. {
  166. m_ptm_notifier->set_enabled(false);
  167. close(m_ptm_fd);
  168. m_socket->close();
  169. if (on_exit)
  170. on_exit();
  171. }