Browse Source

apply autoformatting

Missingmew 4 years ago
parent
commit
68f7cd350c
4 changed files with 209 additions and 184 deletions
  1. 94 94
      cli/src/main.cpp
  2. 48 44
      daemon/include/Sniffer.h
  3. 11 11
      daemon/src/Sniffer.cpp
  4. 56 35
      daemon/src/main.cpp

+ 94 - 94
cli/src/main.cpp

@@ -1,6 +1,6 @@
 #include <boost/program_options.hpp>
-#include <iostream>
 #include <cctype>
+#include <iostream>
 
 #define COMMANDLEN 10
 #define sizeofarr(a) (sizeof(a) / sizeof(a[0]))
@@ -8,110 +8,110 @@
 namespace bpo = boost::program_options;
 
 typedef enum {
-	CMD_HELP,
-	CMD_CONNECT,
-	CMD_DISCONNECT,
-	CMD_PUT,
-	CMD_REMOVE,
-	CMD_GET,
-	CMD_QUERY,
-	CMD_SETUP,
-	CMD_LOG,
-	CMD_UNKNOWN
+  CMD_HELP,
+  CMD_CONNECT,
+  CMD_DISCONNECT,
+  CMD_PUT,
+  CMD_REMOVE,
+  CMD_GET,
+  CMD_QUERY,
+  CMD_SETUP,
+  CMD_LOG,
+  CMD_UNKNOWN
 } COMMANDTYPES;
 
 typedef struct {
-	COMMANDTYPES cmd;
-	const char *name;
-	const char *desc;
+  COMMANDTYPES cmd;
+  const char *name;
+  const char *desc;
 } CMD;
 
-CMD commands[] {
-	{ CMD_HELP, "help", "show help" },
-	{ CMD_CONNECT, "connect", "connect to IP" },
-	{ CMD_DISCONNECT, "disconnect", "disconnect from IP" },
-	{ CMD_PUT, "put", "upload file to IP and add to queue" },
-	{ CMD_REMOVE, "remove", "remove file from IP and queue (stops xfer if required)" },
-	{ CMD_GET, "get", "retrieve file from IP" },
-	{ CMD_QUERY, "query", "query status of IP" },
-	{ CMD_SETUP, "setup", "configure server at IP" },
-	{ CMD_LOG, "log", "show log from IP" }
-};
+CMD commands[]{{CMD_HELP, "help", "show help"},
+               {CMD_CONNECT, "connect", "connect to IP"},
+               {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
+               {CMD_PUT, "put", "upload file to IP and add to queue"},
+               {CMD_REMOVE, "remove",
+                "remove file from IP and queue (stops xfer if required)"},
+               {CMD_GET, "get", "retrieve file from IP"},
+               {CMD_QUERY, "query", "query status of IP"},
+               {CMD_SETUP, "setup", "configure server at IP"},
+               {CMD_LOG, "log", "show log from IP"}};
 
 COMMANDTYPES getCommand(char *str, unsigned int isshort) {
-	COMMANDTYPES ret = CMD_UNKNOWN;
-	char temp[11] = {0};
-	if(strlen(str) > COMMANDLEN) return ret;
-	if(isshort) temp[0] = tolower(str[0]);
-	else for(int i = 0; i < 10; i++) temp[i] = tolower(str[i]);
-	
-	for(int i = 0; i < sizeofarr(commands); i++) {
-		if(isshort) {
-			if(tolower(str[0]) == commands[i].name[0]) ret = commands[i].cmd;
-		}
-		else {
-			if(!strncmp(temp, commands[i].name, COMMANDLEN)) ret = commands[i].cmd;
-		}
-	}
-	return ret;
+  COMMANDTYPES ret = CMD_UNKNOWN;
+  char temp[11] = {0};
+  if (strlen(str) > COMMANDLEN)
+    return ret;
+  if (isshort)
+    temp[0] = tolower(str[0]);
+  else
+    for (int i = 0; i < 10; i++)
+      temp[i] = tolower(str[i]);
+
+  for (int i = 0; i < sizeofarr(commands); i++) {
+    if (isshort) {
+      if (tolower(str[0]) == commands[i].name[0])
+        ret = commands[i].cmd;
+    } else {
+      if (!strncmp(temp, commands[i].name, COMMANDLEN))
+        ret = commands[i].cmd;
+    }
+  }
+  return ret;
 }
 
 void show_help(char *exec) {
-	std::printf("ccats command line interface\n");
-	std::printf("usage: %s COMMAND IP [Options]\n", exec);
-	std::printf("available COMMANDs are:\n");
-	for(int i = 0; i < sizeofarr(commands); i++) {
-		std::printf("%10s - %s\n", commands[i].name, commands[i].desc);
-	}
-	std::printf("IP should be in the format \"xxx.xxx.xxx.xxx\"\n");
+  std::printf("ccats command line interface\n");
+  std::printf("usage: %s COMMAND IP [Options]\n", exec);
+  std::printf("available COMMANDs are:\n");
+  for (int i = 0; i < sizeofarr(commands); i++) {
+    std::printf("%10s - %s\n", commands[i].name, commands[i].desc);
+  }
+  std::printf("IP should be in the format \"xxx.xxx.xxx.xxx\"\n");
 }
 
 int main(int argc, char **argv) {
-	bpo::options_description desc{"Options"};
-	desc.add_options()
-		("cooloption", "Cooloption to use with command FOON")
-	;
-	bpo::variables_map vm;
-	
-	if(argc < 2) {
-		show_help(argv[0]);
-		exit(1);
-	}
-	COMMANDTYPES cmd = getCommand(argv[1], (strlen(argv[1]) == 1));
-	switch(cmd) {
-		case CMD_UNKNOWN:
-			std::printf("unknown command\n");
-		case CMD_HELP:
-			show_help(argv[0]);
-			std::cout << desc;
-			exit(1);
-	}
-	
-	// have enough valid arguments
-	switch(cmd) {
-		case CMD_CONNECT:
-			std::printf("connecting to %s\n", argv[2]);
-			break;
-		case CMD_DISCONNECT:
-			std::printf("disconnecting from %s\n", argv[2]);
-			break;
-		default:
-			std::printf("command %s not implemented\n", argv[1]);
-			break;
-	}
-	
-	try {
-		store(parse_command_line(argc-2, argv+2, desc), vm);
-		notify(vm);
-		
-		if(vm.count("help")) {
-			std::cout << desc;
-		}
-		else {
-			std::printf("no additional options\n");
-		}
-	}
-	catch (const bpo::error &ex) {
-		std::fprintf(stderr, "%s\n", ex.what());
-	}
+  bpo::options_description desc{"Options"};
+  desc.add_options()("cooloption", "Cooloption to use with command FOON");
+  bpo::variables_map vm;
+
+  if (argc < 2) {
+    show_help(argv[0]);
+    exit(1);
+  }
+  COMMANDTYPES cmd = getCommand(argv[1], (strlen(argv[1]) == 1));
+  switch (cmd) {
+  case CMD_UNKNOWN:
+    std::printf("unknown command\n");
+  case CMD_HELP:
+    show_help(argv[0]);
+    std::cout << desc;
+    exit(1);
+  }
+
+  // have enough valid arguments
+  switch (cmd) {
+  case CMD_CONNECT:
+    std::printf("connecting to %s\n", argv[2]);
+    break;
+  case CMD_DISCONNECT:
+    std::printf("disconnecting from %s\n", argv[2]);
+    break;
+  default:
+    std::printf("command %s not implemented\n", argv[1]);
+    break;
+  }
+
+  try {
+    store(parse_command_line(argc - 2, argv + 2, desc), vm);
+    notify(vm);
+
+    if (vm.count("help")) {
+      std::cout << desc;
+    } else {
+      std::printf("no additional options\n");
+    }
+  } catch (const bpo::error &ex) {
+    std::fprintf(stderr, "%s\n", ex.what());
+  }
 }

+ 48 - 44
daemon/include/Sniffer.h

@@ -8,59 +8,63 @@
  *
  * Sniffs the network.
  *
- * Sniffer class which will sniff on a network interface. It is supposed to forward the packets to an analyzer or
- * modifyer so we can hide data in the traffic.
+ * Sniffer class which will sniff on a network interface. It is supposed to
+ * forward the packets to an analyzer or modifyer so we can hide data in the
+ * traffic.
  */
 class Sniffer {
 public:
-    /**
-     * Creates a Sniffer.
-     *
-     * Creates a Sniffer and sets the network interface for sniffing.
-     *
-     * @param interface name of the interface for sniffing
-     */
-    Sniffer(std::string interfaceName);
+  /**
+   * Creates a Sniffer.
+   *
+   * Creates a Sniffer and sets the network interface for sniffing.
+   *
+   * @param interface name of the interface for sniffing
+   */
+  Sniffer(std::string interfaceName);
 
-    /**
-     * Destroys the Sniffer.
-     *
-     * Destructor of the Sniffer.
-     */
-    ~Sniffer();
+  /**
+   * Destroys the Sniffer.
+   *
+   * Destructor of the Sniffer.
+   */
+  ~Sniffer();
 
-    /**
-     * Start sniffing on the interface.
-     *
-     * Starts a sniffing loop which calls handle. The loop will only be stopped if handle returns false.
-     */
-    void startSniffing();
+  /**
+   * Start sniffing on the interface.
+   *
+   * Starts a sniffing loop which calls handle. The loop will only be stopped if
+   * handle returns false.
+   */
+  void startSniffing();
 
-    /**
-     * Sets a filter for the sniffer.
-     *
-     * Sets the filter for a sniffer with a pcap filter string. E.g. "ip dst 8.8.8.8".
-     *
-     * @param filterString pcap filter string
-     */
-    void setFilter(std::string filterString);
+  /**
+   * Sets a filter for the sniffer.
+   *
+   * Sets the filter for a sniffer with a pcap filter string. E.g. "ip
+   * dst 8.8.8.8".
+   *
+   * @param filterString pcap filter string
+   */
+  void setFilter(std::string filterString);
 
 private:
-    /**
-     * Handler for sniffed packets.
-     *
-     * Handles incoming connections and provides data for the package analyzer and modifyer.
-     *
-     * @param pdu sniffed packet
-     *
-     * @return false = stop loop | true = continue loop
-     */
-    bool handle(Tins::PDU& pdu);
+  /**
+   * Handler for sniffed packets.
+   *
+   * Handles incoming connections and provides data for the package analyzer and
+   * modifyer.
+   *
+   * @param pdu sniffed packet
+   *
+   * @return false = stop loop | true = continue loop
+   */
+  bool handle(Tins::PDU &pdu);
 
-    /**
-     * Tins sniffer object.
-     */
-    Tins::Sniffer sniffer;
+  /**
+   * Tins sniffer object.
+   */
+  Tins::Sniffer sniffer;
 };
 
 #endif

+ 11 - 11
daemon/src/Sniffer.cpp

@@ -2,27 +2,27 @@
 #include <iostream>
 
 Sniffer::Sniffer(std::string interfaceName) : sniffer(interfaceName) {
-    Tins::SnifferConfiguration config;
-    config.set_promisc_mode(true);
+  Tins::SnifferConfiguration config;
+  config.set_promisc_mode(true);
 
-    sniffer = Tins::Sniffer(interfaceName, config);
+  sniffer = Tins::Sniffer(interfaceName, config);
 }
 
-Sniffer::~Sniffer() {
-}
+Sniffer::~Sniffer() {}
 
 void Sniffer::startSniffing() {
-    sniffer.sniff_loop(make_sniffer_handler(this, &Sniffer::handle));
+  sniffer.sniff_loop(make_sniffer_handler(this, &Sniffer::handle));
 }
 
 void Sniffer::setFilter(std::string filterString) {
-    sniffer.set_filter(filterString);
+  sniffer.set_filter(filterString);
 }
 
-bool Sniffer::handle(Tins::PDU& pdu) {
-    // TODO implement handler for sniffed traffic
+bool Sniffer::handle(Tins::PDU &pdu) {
+  // TODO implement handler for sniffed traffic
 
-    std::cout << "packet sniffed" << std::endl;
+  std::cout << "packet sniffed" << std::endl;
 
-    return false; // will stop sniffing after the first packet because this handler returns false
+  return false; // will stop sniffing after the first packet because this
+                // handler returns false
 }

+ 56 - 35
daemon/src/main.cpp

@@ -1,7 +1,7 @@
-#include <iostream>
 #include <boost/asio.hpp>
 #include <boost/bind.hpp>
 #include <boost/enable_shared_from_this.hpp>
+#include <iostream>
 
 #include "../include/Sniffer.h"
 
@@ -10,22 +10,27 @@ 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!";
+  std::string message = "Hello From Server!";
   enum { max_length = 1024 };
   char data[max_length];
+
 public:
   typedef boost::shared_ptr<con_handler> pointer;
-  con_handler(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::executor_type& io_service): sock(io_service){}
+  con_handler(
+      boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::executor_type
+          &io_service)
+      : sock(io_service) {}
 
-  static pointer create(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::executor_type& io_service) {
+  static pointer
+  create(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::executor_type
+             &io_service) {
     return pointer(new con_handler(io_service));
   }
 
-  tcp::socket& socket(){return sock;}
+  tcp::socket &socket() { return sock; }
 
   void start() {
     sock.async_read_some(
@@ -33,60 +38,76 @@ public:
         boost::bind(&con_handler::handle_read, shared_from_this(),
                     boost::asio::placeholders::error,
                     boost::asio::placeholders::bytes_transferred));
-  
+
     sock.async_write_some(
         boost::asio::buffer(message, max_length),
         boost::bind(&con_handler::handle_write, shared_from_this(),
-                  boost::asio::placeholders::error,
-                  boost::asio::placeholders::bytes_transferred));
+                    boost::asio::placeholders::error,
+                    boost::asio::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_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();}
+  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,
-        boost::asio::placeholders::error));
+  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,
+                                       boost::asio::placeholders::error));
   }
+
 public:
-  Server(boost::asio::io_service& io_service): acceptor_(io_service, tcp::endpoint(tcp::v4(), 1234)) {
-     start_accept();
+  Server(boost::asio::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();}
+  void handle_accept(con_handler::pointer connection,
+                     const boost::system::error_code &err) {
+    if (!err) {
+      connection->start();
+    }
     start_accept();
   }
 };
 
-
 int main(int argc, char *argv[]) {
-    if(argc < 2) {
-        std::cout << "Usage: " << argv[0] << " <interface>" << std::endl << std::endl;
-        return 0;
-    }
+  if (argc < 2) {
+    std::cout << "Usage: " << argv[0] << " <interface>" << std::endl
+              << std::endl;
+    return 0;
+  }
 
-    Sniffer sniffer(argv[1]);
-    sniffer.startSniffing();
+  Sniffer sniffer(argv[1]);
+  sniffer.startSniffing();
 
   try {
-    boost::asio::io_service io_service;  
+    boost::asio::io_service io_service;
     Server server(io_service);
     io_service.run();
+  } catch (std::exception &e) {
+    std::cerr << e.what() << endl;
   }
-  catch(std::exception& e) {std::cerr << e.what() << endl;}
   return 0;
 }