session.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef SESSION_H
  2. #define SESSION_H
  3. #include <boost/asio.hpp>
  4. #include <boost/asio/ip/basic_resolver.hpp>
  5. #include <map>
  6. #include <string>
  7. #define MAX_BUFFER 256
  8. /*
  9. The Session:
  10. */
  11. class Session : public std::enable_shared_from_this<Session> {
  12. public:
  13. Session(boost::asio::ip::tcp::socket socket,
  14. boost::asio::io_service &io_service, std::string hostname,
  15. std::string port);
  16. void start(void);
  17. ~Session();
  18. void parse_auth(void);
  19. void on_connect(const boost::system::error_code error);
  20. void server_read(void);
  21. void on_resolve(const boost::system::error_code error,
  22. const boost::asio::ip::tcp::resolver::results_type results);
  23. void client_read(void);
  24. void to_client(std::string message);
  25. void to_server(std::string message);
  26. void on_shutdown(boost::system::error_code ec);
  27. void dispatch_line(std::string line);
  28. void process_lines(std::string &received);
  29. const std::string & get_prompt(void);
  30. private:
  31. void set_timer(void);
  32. void reset_timer(void);
  33. void on_timer(const boost::system::error_code error);
  34. void stayin_alive(const boost::system::error_code error);
  35. int time_ms;
  36. int keepalive_secs;
  37. // FOR NOW: These will go into the director
  38. bool show_client = true;
  39. bool talk_direct = true;
  40. /**
  41. * The client's socket
  42. */
  43. boost::asio::ip::tcp::socket socket_;
  44. boost::asio::io_service &io_service_;
  45. boost::asio::ip::tcp::resolver resolver_;
  46. /**
  47. * The server's socket
  48. */
  49. boost::asio::ip::tcp::socket server_;
  50. /**
  51. * The time that we'll use to fire off the "prompt" event.
  52. *
  53. * The idea being that we'd receive chars, processing lines.
  54. * And if we have something in server_prompt start the timer.
  55. * If we receive more, process lines, if !server_prompt.empty()
  56. * reset the timer.
  57. *
  58. * If the timer fires (and isn't aborted), fire off the prompt.
  59. *
  60. * I'm not so sure about this -- because this adds delay to the
  61. * proxy. [It might be better to just fire off "some" text, and
  62. * have it ignored, rather then adding a delay.] Or, this might
  63. * not matter at all, I'm thinking milliseconds here!
  64. */
  65. boost::asio::high_resolution_timer timer_;
  66. /**
  67. * Keep connection alive, don't timeout.
  68. *
  69. * This gets set by to_server config[keepalive], and sends a
  70. * space ' ' if we haven't sent anything to the server in that
  71. * many seconds.
  72. */
  73. boost::asio::high_resolution_timer keep_alive_;
  74. /**
  75. * What characters have been received from the server,
  76. * that weren't \n terminated?
  77. *
  78. * This needs to be reset/cleared if there's a \r (carriage return).
  79. */
  80. std::string server_prompt;
  81. /**
  82. * FAIL-WHALE: This was supposed to hold the number of characters
  83. * already sent to the user at this point in time, but I'm having
  84. * a hard time tracking those.
  85. *
  86. */
  87. // int server_sent;
  88. /**
  89. * The client read buffer.
  90. *
  91. * This is too big, we don't get that many characters from the client.
  92. *
  93. */
  94. char read_buffer[MAX_BUFFER + 1];
  95. /**
  96. * The server read buffer.
  97. * This is MAX_BUFFER + 1 (to store the \0 to terminate the string)
  98. */
  99. char server_buffer[MAX_BUFFER + 1];
  100. /**
  101. * The rlogin information received from the client.
  102. *
  103. * We check this, and if it isn't valid, we spoof some rlogin
  104. * connection.
  105. */
  106. std::string rlogin_auth;
  107. /**
  108. * The username passed in via rlogin. We need this so we know what
  109. * name we need to store the data under.
  110. */
  111. std::string rlogin_name;
  112. std::string host;
  113. std::string port;
  114. /**
  115. * Are we connected to the server?
  116. *
  117. * Don't shutdown the server socket if we aren't connected.
  118. */
  119. bool connected = false;
  120. };
  121. /*
  122. maybe move the resolver part to the server, so I don't need io_service?
  123. I'm not sure what the socket connection part is going to need just yet,
  124. so I probably won't move that just yet. [NNY!]
  125. */
  126. class Server {
  127. public:
  128. Server(boost::asio::io_service &io_service,
  129. const boost::asio::ip::tcp::endpoint &endpoint, std::string host,
  130. std::string port);
  131. private:
  132. void do_accept(void);
  133. boost::asio::io_service &io_service_;
  134. boost::asio::ip::tcp::acceptor acceptor_;
  135. /**
  136. * The host to connect to (from config)
  137. */
  138. std::string host_;
  139. /**
  140. * The port to connect to (from config)
  141. *
  142. */
  143. std::string port_;
  144. };
  145. // The simple way to get the boost logging to log file and line number
  146. // information. [Let the BUGZ_LOG macro do it!]
  147. #include <iomanip>
  148. #include <string.h>
  149. const char *trim_path(const char *filepath);
  150. #define BUGZ_LOG(severity) \
  151. BOOST_LOG_TRIVIAL(severity) \
  152. << "(" << std::setw(15) << trim_path(__FILE__) << ":" << std::setw(4) \
  153. << std::left << __LINE__ << ") "
  154. #endif