Change packet source fields of Options to std::optional

This commit is contained in:
Jon Siwek 2020-01-31 15:40:15 -08:00
parent b0a5eb27b1
commit 2f36113743
5 changed files with 22 additions and 20 deletions

View file

@ -145,34 +145,34 @@ void net_update_time(double new_network_time)
PLUGIN_HOOK_VOID(HOOK_UPDATE_NETWORK_TIME, HookUpdateNetworkTime(new_network_time)); PLUGIN_HOOK_VOID(HOOK_UPDATE_NETWORK_TIME, HookUpdateNetworkTime(new_network_time));
} }
void net_init(const std::string& interface, void net_init(const std::optional<std::string>& interface,
const std::string& pcap_input_file, const std::optional<std::string>& pcap_input_file,
const std::optional<std::string>& pcap_output_file, const std::optional<std::string>& pcap_output_file,
bool do_watchdog) bool do_watchdog)
{ {
if ( ! pcap_input_file.empty() ) if ( pcap_input_file )
{ {
reading_live = pseudo_realtime > 0.0; reading_live = pseudo_realtime > 0.0;
reading_traces = true; reading_traces = true;
iosource::PktSrc* ps = iosource_mgr->OpenPktSrc(pcap_input_file, false); iosource::PktSrc* ps = iosource_mgr->OpenPktSrc(*pcap_input_file, false);
assert(ps); assert(ps);
if ( ! ps->IsOpen() ) if ( ! ps->IsOpen() )
reporter->FatalError("problem with trace file %s (%s)", reporter->FatalError("problem with trace file %s (%s)",
pcap_input_file.c_str(), ps->ErrorMsg()); pcap_input_file->c_str(), ps->ErrorMsg());
} }
else if ( ! interface.empty() ) else if ( interface )
{ {
reading_live = true; reading_live = true;
reading_traces = false; reading_traces = false;
iosource::PktSrc* ps = iosource_mgr->OpenPktSrc(interface, true); iosource::PktSrc* ps = iosource_mgr->OpenPktSrc(*interface, true);
assert(ps); assert(ps);
if ( ! ps->IsOpen() ) if ( ! ps->IsOpen() )
reporter->FatalError("problem with interface %s (%s)", reporter->FatalError("problem with interface %s (%s)",
interface.c_str(), ps->ErrorMsg()); interface->c_str(), ps->ErrorMsg());
} }
else else

View file

@ -19,8 +19,8 @@ namespace iosource {
class Packet; class Packet;
extern void net_init(const std::string& interfaces, extern void net_init(const std::optional<std::string>& interfaces,
const std::string& pcap_input_file, const std::optional<std::string>& pcap_input_file,
const std::optional<std::string>& pcap_output_file, const std::optional<std::string>& pcap_output_file,
bool do_watchdog); bool do_watchdog);
extern void net_run(); extern void net_run();

View file

@ -268,12 +268,13 @@ zeek::Options zeek::parse_cmdline(int argc, char** argv)
rval.print_usage = true; rval.print_usage = true;
break; break;
case 'i': case 'i':
if ( ! rval.interface.empty() ) if ( rval.interface )
{ {
fprintf(stderr, "ERROR: Only a single interface option (-i) is allowed.\n"); fprintf(stderr, "ERROR: Only a single interface option (-i) is allowed.\n");
exit(1); exit(1);
} }
else if ( ! rval.pcap_file.empty() )
if ( rval.pcap_file )
{ {
fprintf(stderr, "ERROR: Using -i is not allow when reading a pcap file.\n"); fprintf(stderr, "ERROR: Using -i is not allow when reading a pcap file.\n");
exit(1); exit(1);
@ -294,16 +295,18 @@ zeek::Options zeek::parse_cmdline(int argc, char** argv)
rval.script_prefixes.emplace_back(optarg); rval.script_prefixes.emplace_back(optarg);
break; break;
case 'r': case 'r':
if ( ! rval.pcap_file.empty() ) if ( rval.pcap_file )
{ {
fprintf(stderr, "ERROR: Only a single readfile option (-r) is allowed.\n"); fprintf(stderr, "ERROR: Only a single readfile option (-r) is allowed.\n");
exit(1); exit(1);
} }
else if ( ! rval.interface.empty() )
if ( rval.interface )
{ {
fprintf(stderr, "Using -r is not allowed when reading a live interface.\n"); fprintf(stderr, "Using -r is not allowed when reading a live interface.\n");
exit(1); exit(1);
} }
rval.pcap_file = optarg; rval.pcap_file = optarg;
break; break;
case 's': case 's':

View file

@ -58,8 +58,8 @@ struct Options {
std::vector<std::string> doctest_args; std::vector<std::string> doctest_args;
std::optional<std::string> pcap_filter; std::optional<std::string> pcap_filter;
std::string interface; std::optional<std::string> interface;
std::string pcap_file; std::optional<std::string> pcap_file;
std::vector<std::string> signature_files; std::vector<std::string> signature_files;
std::optional<std::string> pcap_output_file; std::optional<std::string> pcap_output_file;

View file

@ -565,8 +565,7 @@ int main(int argc, char** argv)
if ( options.plugins_to_load.empty() && options.scripts_to_load.empty() && if ( options.plugins_to_load.empty() && options.scripts_to_load.empty() &&
options.script_options_to_set.empty() && options.script_options_to_set.empty() &&
options.pcap_file.empty() && ! options.pcap_file && ! options.interface &&
options.interface.empty() &&
! options.identifier_to_print && ! options.identifier_to_print &&
! command_line_policy && ! options.print_plugins && ! command_line_policy && ! options.print_plugins &&
! options.supervisor_mode && ! zeek::Supervisor::ThisNode() ) ! options.supervisor_mode && ! zeek::Supervisor::ThisNode() )
@ -596,7 +595,7 @@ int main(int argc, char** argv)
log_mgr = new logging::Manager(); log_mgr = new logging::Manager();
input_mgr = new input::Manager(); input_mgr = new input::Manager();
file_mgr = new file_analysis::Manager(); file_mgr = new file_analysis::Manager();
broker_mgr = new bro_broker::Manager(! options.pcap_file.empty()); broker_mgr = new bro_broker::Manager(options.pcap_file.has_value());
trigger_mgr = new trigger::Manager(); trigger_mgr = new trigger::Manager();
plugin_mgr->InitPreScript(); plugin_mgr->InitPreScript();
@ -745,7 +744,7 @@ int main(int argc, char** argv)
// ### Add support for debug command file. // ### Add support for debug command file.
dbg_init_debugger(0); dbg_init_debugger(0);
if ( options.pcap_file.empty() && options.interface.empty() ) if ( ! options.pcap_file && ! options.interface )
{ {
Val* interfaces_val = internal_val("interfaces"); Val* interfaces_val = internal_val("interfaces");
if ( interfaces_val ) if ( interfaces_val )