|
- //////////////////////////////////////////////////////////////////////
- // Base CLI app classes for TrafficMon tools
- // Written by Jonathan A. Foster <jon@jfpossibilities.com>
- // Started December 29th, 2021
- // Copyright JF Possibilities, Inc. All rights reserved.
- //////////////////////////////////////////////////////////////////////
- #include <stdexcept>
- #include <iostream>
- #include <libgen.h>
- #include "appbase.h"
-
-
-
- //////////////////////////////////////////////////////////////////////
- // TrafficMonBaseApp
- //////////////////////////////////////////////////////////////////////
-
- cBaseApp &TrafficMonBaseApp::init(int argc, char **argv) {
- if(!config) config = new MonitorBaseConf;
- return cBaseApp::init(argc, argv);
- }
-
-
-
- unsigned TrafficMonBaseApp::do_switch(const char *arg) {
- if(!arg[1] && *arg=='c') return 1;
- return cBaseApp::do_switch(arg);
- }
-
-
-
- void TrafficMonBaseApp::do_switch_arg(const char *sw, const std::string &val) {
- if(!sw[1] && *sw=='c') config->load(val);
- }
-
-
-
- int TrafficMonBaseApp::main() {
- int x;
- try {
- if(x=cBaseApp::main()) return x; // Parse CLI args
- if(!config->traffic_mon.vals.size()) throw CLIerror(
- "You need to load a config file with a [Traffic Mon] section"
- );
- } catch(const CLIerror &e) {
- std::cerr << e.what() << "\n\n";
- return help();
- }
- db.open("mysql:user="+qesc(config->traffic_mon.get("db user"))+
- ";password="+qesc(config->traffic_mon.get("db password"))+
- ";host="+qesc(config->traffic_mon.get("db host"))+
- ";database="+qesc(config->traffic_mon.get("db name"))+
- ";@opt_reconnect=1");
- return 0;
- }
-
-
-
- TrafficMonBaseApp::~TrafficMonBaseApp() {
- if(config) delete(config);
- }
-
-
-
- //////////////////////////////////////////////////////////////////////
- // BlackListBaseApp
- //////////////////////////////////////////////////////////////////////
-
- int BlackListBaseApp::help() {
- std::cerr << " FORMAT: " << basename(command_args[0]) << " -c {config} [-4 {address}] [-6 {address}]\n"
- << '\n'
- << "The config file must have a [Traffic Mon] section with the database\n"
- << "credentials in it. -4 & -6 set the addresses to pin blocked names to.\n"
- << "They default to the 'localhost' address in the respective family. Set\n"
- << "to '' to turn off output of that family." << std::endl;
- return ExitCode = 1;
- }
-
-
-
- unsigned BlackListBaseApp::do_switch(const char *arg) {
- if(!arg[1] && (*arg=='4' || *arg=='6')) return 1;
- return TrafficMonBaseApp::do_switch(arg);
- }
-
-
-
- void BlackListBaseApp::do_switch_arg(const char *sw, const std::string &val) {
- if(!sw[1]) switch(*sw) {
- case '4': ipv4 = val; return;
- case '6': ipv6 = val; return;
- }
- TrafficMonBaseApp::do_switch_arg(sw, val);
- }
-
-
-
- void BlackListBaseApp::do_arg(const char *arg) {
- throw CLIerror("Invalid arguments");
- }
-
-
-
- int BlackListBaseApp::main() {
- int x;
-
- if(x=TrafficMonBaseApp::main()) return x; // Parse CLI args, open conf & db
- if(ipv4=="" && ipv6=="") {
- std::cerr << "All address families turned off. Nothing to do." << std::endl;
- return 1;
- }
- return 0;
- }
|