|
| 1 | + |
| 2 | +#include <cstdlib> |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +#include <unistd.h> |
| 6 | + |
| 7 | +#include <libfds.h> |
| 8 | + |
| 9 | +#include "common.hpp" |
| 10 | +#include "filelist.hpp" |
| 11 | +#include "flowProvider.hpp" |
| 12 | +#include "options.hpp" |
| 13 | +#include "printer.hpp" |
| 14 | +#include "storageSorted.hpp" |
| 15 | + |
| 16 | + |
| 17 | +static unique_iemgr iemgr_prepare(const std::string &path) |
| 18 | +{ |
| 19 | + unique_iemgr iemgr {fds_iemgr_create(), &fds_iemgr_destroy}; |
| 20 | + int ret; |
| 21 | + |
| 22 | + if (!iemgr) { |
| 23 | + throw std::runtime_error("fds_iemgr_create() has failed"); |
| 24 | + } |
| 25 | + |
| 26 | + ret = fds_iemgr_read_dir(iemgr.get(), path.c_str()); |
| 27 | + if (ret != FDS_OK) { |
| 28 | + const std::string err_msg = fds_iemgr_last_err(iemgr.get()); |
| 29 | + throw std::runtime_error("fds_iemgr_read_dir() failed: " + err_msg); |
| 30 | + } |
| 31 | + |
| 32 | + return iemgr; |
| 33 | +} |
| 34 | + |
| 35 | +static void fds_iemgr_deleter(fds_iemgr_t *mgr) { |
| 36 | + if (mgr != nullptr) { |
| 37 | + fds_iemgr_destroy(mgr); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static void |
| 42 | +mode_list_unordered(const shared_iemgr &iemgr, const Options &opts, FlowProvider &flows) |
| 43 | +{ |
| 44 | + auto printer = printer_factory(iemgr, opts.get_output_specifier()); |
| 45 | + const size_t rec_limit = opts.get_output_limit(); |
| 46 | + size_t rec_printed = 0; |
| 47 | + |
| 48 | + printer->print_prologue(); |
| 49 | + |
| 50 | + while (rec_limit == 0 || rec_printed < rec_limit) { |
| 51 | + Flow *flow = flows.next_record(); |
| 52 | + |
| 53 | + if (!flow) { |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + rec_printed += printer->print_record(flow); |
| 58 | + } |
| 59 | + |
| 60 | + printer->print_epilogue(); |
| 61 | +} |
| 62 | + |
| 63 | +static void |
| 64 | +mode_list_ordered(const shared_iemgr &iemgr, const Options &opts, FlowProvider &flows) |
| 65 | +{ |
| 66 | + StorageSorter sorter {opts.get_order_by(), iemgr}; |
| 67 | + StorageSorted storage {sorter, opts.get_output_limit()}; |
| 68 | + auto printer = printer_factory(iemgr, opts.get_output_specifier()); |
| 69 | + |
| 70 | + while (true) { |
| 71 | + Flow *flow = flows.next_record(); |
| 72 | + |
| 73 | + if (!flow) { |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + storage.insert(flow); |
| 78 | + } |
| 79 | + |
| 80 | + printer->print_prologue(); |
| 81 | + |
| 82 | + for (const auto &rec : storage) { |
| 83 | + const Flow *flow = &rec.get_flow_const(); |
| 84 | + printer->print_record(const_cast<Flow *>(flow)); |
| 85 | + } |
| 86 | + |
| 87 | + printer->print_epilogue(); |
| 88 | +} |
| 89 | + |
| 90 | +static void |
| 91 | +mode_list(const shared_iemgr &iemgr, const Options &opts) |
| 92 | +{ |
| 93 | + FlowProvider flows {iemgr}; |
| 94 | + |
| 95 | + flows.set_biflow_autoignore(opts.get_biflow_autoignore()); |
| 96 | + |
| 97 | + if (!opts.get_input_filter().empty()) { |
| 98 | + flows.set_filter(opts.get_input_filter()); |
| 99 | + } |
| 100 | + |
| 101 | + for (const auto &it : opts.get_input_files()) { |
| 102 | + flows.add_file(it); |
| 103 | + } |
| 104 | + |
| 105 | + if (opts.get_order_by().empty()) { |
| 106 | + mode_list_unordered(iemgr, opts, flows); |
| 107 | + } else { |
| 108 | + mode_list_ordered(iemgr, opts, flows); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +int |
| 113 | +main(int argc, char *argv[]) |
| 114 | +{ |
| 115 | + try { |
| 116 | + Options options {argc, argv}; |
| 117 | + shared_iemgr iemgr {nullptr, fds_iemgr_deleter}; |
| 118 | + |
| 119 | + iemgr = iemgr_prepare(std::string(fds_api_cfg_dir())); |
| 120 | + |
| 121 | + switch (options.get_mode()) { |
| 122 | + case Options::Mode::list: |
| 123 | + mode_list(iemgr, options); |
| 124 | + break; |
| 125 | + |
| 126 | + default: |
| 127 | + throw std::runtime_error("Invalid mode"); |
| 128 | + } |
| 129 | + } catch (const std::exception &ex) { |
| 130 | + std::cerr << "ERROR: " << ex.what() << std::endl; |
| 131 | + return EXIT_FAILURE; |
| 132 | + } |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
0 commit comments