Files
firegex-traffic-viewer/backend/binsrc/nfqueue.cpp

65 lines
2.1 KiB
C++
Raw Normal View History

#include "classes/regex_filter.hpp"
#include "classes/netfilter.hpp"
#include "utils.hpp"
2022-07-15 10:08:54 +02:00
#include <iostream>
2022-07-16 14:22:33 +02:00
2022-07-15 10:08:54 +02:00
using namespace std;
2022-07-15 17:26:40 +02:00
shared_ptr<regex_rules> regex_config;
2022-07-16 15:24:05 +02:00
void config_updater (){
2022-07-18 23:01:24 +02:00
string line;
2022-07-16 15:24:05 +02:00
while (true){
getline(cin, line);
if (cin.eof()){
2022-07-19 15:17:34 +02:00
cerr << "[fatal] [updater] cin.eof()" << endl;
exit(EXIT_FAILURE);
}
2022-07-16 15:24:05 +02:00
if (cin.bad()){
2022-07-19 15:17:34 +02:00
cerr << "[fatal] [updater] cin.bad()" << endl;
2022-07-16 15:24:05 +02:00
exit(EXIT_FAILURE);
}
cerr << "[info] [updater] Updating configuration with line " << line << endl;
istringstream config_stream(line);
regex_rules *regex_new_config = new regex_rules();
while(!config_stream.eof()){
2022-07-18 23:01:24 +02:00
string data;
2022-07-16 15:24:05 +02:00
config_stream >> data;
if (data != "" && data != "\n"){
regex_new_config->add(data.c_str());
}
2022-07-16 15:24:05 +02:00
}
regex_config.reset(regex_new_config);
cerr << "[info] [updater] Config update done" << endl;
}
}
2022-07-16 14:22:33 +02:00
template <bool is_input>
bool filter_callback(const uint8_t *data, uint32_t len){
shared_ptr<regex_rules> current_config = regex_config;
2022-07-16 14:44:44 +02:00
return current_config->check((unsigned char *)data, len, is_input);
2022-07-16 01:04:55 +02:00
}
2022-07-15 10:08:54 +02:00
int main(int argc, char *argv[])
{
2022-07-22 00:34:57 +02:00
int n_of_threads = 1;
char * n_threads_str = getenv("NTHREADS");
if (n_threads_str != NULL) n_of_threads = ::atoi(n_threads_str);
2022-07-22 00:34:57 +02:00
if(n_of_threads <= 0) n_of_threads = 1;
if (n_of_threads % 2 != 0 ) n_of_threads++;
cerr << "[info] [main] Using " << n_of_threads << " threads" << endl;
2022-07-16 14:22:33 +02:00
regex_config.reset(new regex_rules());
2022-07-22 00:34:57 +02:00
NFQueueSequence<filter_callback<true>> input_queues(n_of_threads/2);
2022-07-16 01:04:55 +02:00
input_queues.start();
2022-07-22 00:34:57 +02:00
NFQueueSequence<filter_callback<false>> output_queues(n_of_threads/2);
2022-07-16 01:04:55 +02:00
output_queues.start();
2022-07-15 17:26:40 +02:00
2022-07-16 13:45:31 +02:00
cout << "QUEUES INPUT " << input_queues.init() << " " << input_queues.end() << " OUTPUT " << output_queues.init() << " " << output_queues.end() << endl;
cerr << "[info] [main] Input queues: " << input_queues.init() << ":" << input_queues.end() << " threads assigned: " << n_of_threads/2 << endl;
cerr << "[info] [main] Output queues: " << output_queues.init() << ":" << output_queues.end() << " threads assigned: " << n_of_threads/2 << endl;
2022-07-15 17:26:40 +02:00
config_updater();
2022-07-15 10:08:54 +02:00
}