Browse Source

Moving con_handler and Server to Server.h

anon 4 years ago
parent
commit
b566e80f71
5 changed files with 249 additions and 107 deletions
  1. 17 19
      cli/src/main.cpp
  2. 1 1
      daemon/CMakeLists.txt
  3. 143 0
      daemon/include/Server.h
  4. 81 0
      daemon/src/Server.cpp
  5. 7 87
      daemon/src/main.cpp

+ 17 - 19
cli/src/main.cpp

@@ -1,5 +1,5 @@
-#include <boost/program_options.hpp>
 #include <boost/asio.hpp>
+#include <boost/program_options.hpp>
 #include <cctype>
 #include <iostream>
 
@@ -91,7 +91,7 @@ int main(int argc, char **argv) {
     show_help(argv[0]);
     exit(1);
   }
-  
+
   // identify command, print error message if unknown
   COMMANDTYPES cmd = getCommand(argv[1], (strlen(argv[1]) == 1));
   switch (cmd) {
@@ -107,7 +107,7 @@ int main(int argc, char **argv) {
     show_help(argv[0]);
     exit(1);
   }
-  
+
   // parse additional arguments, possibly drop this in the future for full
   // interactivity
   try {
@@ -130,18 +130,17 @@ int main(int argc, char **argv) {
     socket.connect(tcp::endpoint(bai::ip::address::from_string(argv[2]), 1234));
     std::printf("connected to %s - sending hello\n", argv[2]);
     bai::write(socket, bai::buffer("Client CONNECT test\n"), err);
-    if(!err) {
-	    std::printf("OK: sent message\n");
-    }
-    else {
+    if (!err) {
+      std::printf("OK: sent message\n");
+    } else {
       std::printf("ERR: couldnt send message: %s\n", err.message().c_str());
     }
     bai::read(socket, recvbuf, bai::transfer_all(), err);
-    if(err && err != bai::error::eof) {
+    if (err && err != bai::error::eof) {
       std::printf("ERR: couldnt recieve message: %s\n", err.message().c_str());
-    }
-    else {
-	    std::printf("OK: recieved message %s\n", bai::buffer_cast<const char*>(recvbuf.data()));
+    } else {
+      std::printf("OK: recieved message %s\n",
+                  bai::buffer_cast<const char *>(recvbuf.data()));
     }
     break;
   case CMD_DISCONNECT:
@@ -149,18 +148,17 @@ int main(int argc, char **argv) {
     socket.connect(tcp::endpoint(bai::ip::address::from_string(argv[2]), 1234));
     std::printf("connected to %s - sending goodbye\n", argv[2]);
     bai::write(socket, bai::buffer("Client DISCONNECT test\n"), err);
-    if(!err) {
-	    std::printf("OK: sent message\n");
-    }
-    else {
+    if (!err) {
+      std::printf("OK: sent message\n");
+    } else {
       std::printf("ERR: couldnt send message: %s\n", err.message().c_str());
     }
     bai::read(socket, recvbuf, bai::transfer_all(), err);
-    if(err && err != bai::error::eof) {
+    if (err && err != bai::error::eof) {
       std::printf("ERR: couldnt recieve message: %s\n", err.message().c_str());
-    }
-    else {
-	    std::printf("OK: recieved message %s\n", bai::buffer_cast<const char*>(recvbuf.data()));
+    } else {
+      std::printf("OK: recieved message %s\n",
+                  bai::buffer_cast<const char *>(recvbuf.data()));
     }
     break;
   default:

+ 1 - 1
daemon/CMakeLists.txt

@@ -5,7 +5,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 
 project(ccats)
 
-add_executable(ccats src/main.cpp src/Sniffer.cpp)
+add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp)
 
 # use pkg-config to fix building on debian unstable
 find_package(PkgConfig REQUIRED)

+ 143 - 0
daemon/include/Server.h

@@ -0,0 +1,143 @@
+#ifndef SERVER_H
+#define SERVER_H
+
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+using namespace boost::asio;
+using ip::tcp;
+
+/**
+ * @class con_handler
+ *
+ * Handler for a connection.
+ *
+ * Handles and manages a connection between client and server.
+ */
+class con_handler : public boost::enable_shared_from_this<con_handler> {
+private:
+  tcp::socket sock;
+  const std::string message = "Hello From Server!";
+
+  /**
+   * max buffer length
+   */
+  enum { max_length = 1024 };
+
+  /**
+   * data buffer
+   */
+  char data[max_length];
+
+public:
+  /**
+   * Pointer to a con_handler.
+   */
+  typedef boost::shared_ptr<con_handler> pointer;
+
+  /**
+   * con_handler constructor
+   *
+   * Creates a con_handler
+   *
+   * @param io_service connection info
+   */
+  con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
+
+  /**
+   * con_handler destructor.
+   */
+  ~con_handler();
+
+  /**
+   * Creates a new con_handler.
+   *
+   * Allocates a new con_hanlder and returns the pointer.
+   *
+   * @param io_service connection info
+   *
+   * @return con_handler pointer
+   */
+  static pointer
+  create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
+
+  /**
+   * socket getter
+   *
+   * Returns the socket of the connection.
+   *
+   * @return the socket
+   */
+  tcp::socket &socket();
+
+  /**
+   * Starts a connection.
+   *
+   * Negotiates a connection and sets everything important to the connection up.
+   */
+  void start();
+
+  /**
+   * Read handler
+   *
+   * Will be called if something from the client has been received.
+   *
+   * @param err error
+   * @param bytes_transferred amount of transferred bytes
+   */
+  void handle_read(const boost::system::error_code &err,
+                   size_t bytes_transferred);
+
+  /**
+   * Write handler
+   *
+   * Will be called if the server sends something.
+   *
+   * @param err error
+   * @param bytes_transferred amount of transferred bytes
+   */
+  void handle_write(const boost::system::error_code &err,
+                    size_t bytes_transferred);
+};
+
+/**
+ * @class Server
+ *
+ * boost server
+ *
+ * boost server which creates the handler for the connections.
+ */
+class Server {
+private:
+  tcp::acceptor acceptor_;
+
+  /**
+   * Accepts traffic
+   */
+  void start_accept();
+
+public:
+  /**
+   * Creates a boost server
+   *
+   * @param io_service connection info
+   */
+  Server(io_service &io_service);
+
+  /**
+   * Destroys the server
+   */
+  ~Server();
+
+  /**
+   * Handles traffic?
+   *
+   * @param connection con_handler
+   * @param err error
+   */
+  void handle_accept(con_handler::pointer connection,
+                     const boost::system::error_code &err);
+};
+
+#endif

+ 81 - 0
daemon/src/Server.cpp

@@ -0,0 +1,81 @@
+#include "../include/Server.h"
+#include <iostream>
+
+using namespace boost::asio;
+using ip::tcp;
+
+/***************
+ * con_handler *
+ ***************/
+
+con_handler::con_handler(
+    basic_socket_acceptor<ip::tcp>::executor_type &io_service)
+    : sock(io_service) {}
+
+con_handler::~con_handler() {}
+
+con_handler::pointer
+con_handler::create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) {
+  return pointer(new con_handler(io_service));
+}
+
+tcp::socket &con_handler::socket() { return sock; }
+
+void con_handler::start() {
+  sock.async_read_some(buffer(data, max_length),
+                       boost::bind(&con_handler::handle_read,
+                                   shared_from_this(), placeholders::error,
+                                   placeholders::bytes_transferred));
+
+  sock.async_write_some(buffer(message, max_length),
+                        boost::bind(&con_handler::handle_write,
+                                    shared_from_this(), placeholders::error,
+                                    placeholders::bytes_transferred));
+}
+
+void con_handler::handle_read(const boost::system::error_code &err,
+                              size_t bytes_transferred) {
+  if (!err) {
+    std::cout << this->data << std::endl;
+  } else {
+    std::cerr << "error: " << err.message() << std::endl;
+    sock.close();
+  }
+}
+
+void con_handler::handle_write(const boost::system::error_code &err,
+                               size_t bytes_transferred) {
+  if (!err) {
+    std::cout << "Hello World!" << std::endl;
+  } else {
+    std::cerr << "error: " << err.message() << std::endl;
+    sock.close();
+  }
+}
+
+/**********
+ * Server *
+ **********/
+
+void Server::start_accept() {
+  auto executor = acceptor_.get_executor();
+  con_handler::pointer connection = con_handler::create(executor);
+  acceptor_.async_accept(connection->socket(),
+                         boost::bind(&Server::handle_accept, this, connection,
+                                     placeholders::error));
+}
+
+Server::Server(io_service &io_service)
+    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
+  start_accept();
+}
+
+Server::~Server() {}
+
+void Server::handle_accept(con_handler::pointer connection,
+                           const boost::system::error_code &err) {
+  if (!err) {
+    connection->start();
+  }
+  start_accept();
+}

+ 7 - 87
daemon/src/main.cpp

@@ -4,107 +4,27 @@
 #include <iostream>
 #include <thread>
 
+#include "../include/Server.h"
 #include "../include/Sniffer.h"
 
-using namespace boost::asio;
-using ip::tcp;
-using std::cout;
-using std::endl;
-
-class con_handler : public boost::enable_shared_from_this<con_handler> {
-private:
-  tcp::socket sock;
-  std::string message = "Hello From Server!";
-  enum { max_length = 1024 };
-  char data[max_length];
-
-public:
-  typedef boost::shared_ptr<con_handler> pointer;
-  con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service)
-      : sock(io_service) {}
-
-  static pointer
-  create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) {
-    return pointer(new con_handler(io_service));
-  }
-
-  tcp::socket &socket() { return sock; }
-
-  void start() {
-    sock.async_read_some(buffer(data, max_length),
-                         boost::bind(&con_handler::handle_read,
-                                     shared_from_this(), placeholders::error,
-                                     placeholders::bytes_transferred));
-
-    sock.async_write_some(buffer(message, max_length),
-                          boost::bind(&con_handler::handle_write,
-                                      shared_from_this(), placeholders::error,
-                                      placeholders::bytes_transferred));
-  }
-
-  void handle_read(const boost::system::error_code &err,
-                   size_t bytes_transferred) {
-    if (!err) {
-      cout << data << endl;
-    } else {
-      std::cerr << "error: " << err.message() << std::endl;
-      sock.close();
-    }
-  }
-
-  void handle_write(const boost::system::error_code &err,
-                    size_t bytes_transferred) {
-    if (!err) {
-      cout << "Hello World!" << endl;
-    } else {
-      std::cerr << "error: " << err.message() << endl;
-      sock.close();
-    }
-  }
-};
-
-class Server {
-private:
-  tcp::acceptor acceptor_;
-  void start_accept() {
-    auto executor = acceptor_.get_executor();
-    con_handler::pointer connection = con_handler::create(executor);
-    acceptor_.async_accept(connection->socket(),
-                           boost::bind(&Server::handle_accept, this, connection,
-                                       placeholders::error));
-  }
-
-public:
-  Server(io_service &io_service)
-      : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
-    start_accept();
-  }
-  void handle_accept(con_handler::pointer connection,
-                     const boost::system::error_code &err) {
-    if (!err) {
-      connection->start();
-    }
-    start_accept();
-  }
-};
+using namespace std;
 
 int main(int argc, char *argv[]) {
   if (argc < 2) {
-    std::cout << "Usage: " << argv[0] << " <interface>" << std::endl
-              << std::endl;
+    cout << "Usage: " << argv[0] << " <interface>" << endl << endl;
     return 0;
   }
 
-  const std::string interface = argv[1];
+  const string interface = argv[1];
   Sniffer sniffer(interface);
-  std::thread snifferThread(&Sniffer::startSniffing, sniffer);
+  thread snifferThread(&Sniffer::startSniffing, sniffer);
 
   try {
     io_service io_service;
     Server server(io_service);
     io_service.run();
-  } catch (std::exception &e) {
-    std::cerr << e.what() << endl;
+  } catch (exception &e) {
+    cerr << e.what() << endl;
   }
 
   snifferThread.join();