main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <boost/asio.hpp>
  2. #include <boost/bind.hpp>
  3. #include <boost/enable_shared_from_this.hpp>
  4. #include <iostream>
  5. #include "../include/Sniffer.h"
  6. using namespace boost::asio;
  7. using ip::tcp;
  8. using std::cout;
  9. using std::endl;
  10. class con_handler : public boost::enable_shared_from_this<con_handler> {
  11. private:
  12. tcp::socket sock;
  13. std::string message = "Hello From Server!";
  14. enum { max_length = 1024 };
  15. char data[max_length];
  16. public:
  17. typedef boost::shared_ptr<con_handler> pointer;
  18. con_handler(
  19. boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::executor_type
  20. &io_service)
  21. : sock(io_service) {}
  22. static pointer
  23. create(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::executor_type
  24. &io_service) {
  25. return pointer(new con_handler(io_service));
  26. }
  27. tcp::socket &socket() { return sock; }
  28. void start() {
  29. sock.async_read_some(
  30. boost::asio::buffer(data, max_length),
  31. boost::bind(&con_handler::handle_read, shared_from_this(),
  32. boost::asio::placeholders::error,
  33. boost::asio::placeholders::bytes_transferred));
  34. sock.async_write_some(
  35. boost::asio::buffer(message, max_length),
  36. boost::bind(&con_handler::handle_write, shared_from_this(),
  37. boost::asio::placeholders::error,
  38. boost::asio::placeholders::bytes_transferred));
  39. }
  40. void handle_read(const boost::system::error_code &err,
  41. size_t bytes_transferred) {
  42. if (!err) {
  43. cout << data << endl;
  44. } else {
  45. std::cerr << "error: " << err.message() << std::endl;
  46. sock.close();
  47. }
  48. }
  49. void handle_write(const boost::system::error_code &err,
  50. size_t bytes_transferred) {
  51. if (!err) {
  52. cout << "Hello World!" << endl;
  53. } else {
  54. std::cerr << "error: " << err.message() << endl;
  55. sock.close();
  56. }
  57. }
  58. };
  59. class Server {
  60. private:
  61. tcp::acceptor acceptor_;
  62. void start_accept() {
  63. auto executor = acceptor_.get_executor();
  64. con_handler::pointer connection = con_handler::create(executor);
  65. acceptor_.async_accept(connection->socket(),
  66. boost::bind(&Server::handle_accept, this, connection,
  67. boost::asio::placeholders::error));
  68. }
  69. public:
  70. Server(boost::asio::io_service &io_service)
  71. : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
  72. start_accept();
  73. }
  74. void handle_accept(con_handler::pointer connection,
  75. const boost::system::error_code &err) {
  76. if (!err) {
  77. connection->start();
  78. }
  79. start_accept();
  80. }
  81. };
  82. int main(int argc, char *argv[]) {
  83. if (argc < 2) {
  84. std::cout << "Usage: " << argv[0] << " <interface>" << std::endl
  85. << std::endl;
  86. return 0;
  87. }
  88. Sniffer sniffer(argv[1]);
  89. sniffer.startSniffing();
  90. try {
  91. boost::asio::io_service io_service;
  92. Server server(io_service);
  93. io_service.run();
  94. } catch (std::exception &e) {
  95. std::cerr << e.what() << endl;
  96. }
  97. return 0;
  98. }