00001 //--------------------------------------------------------------------------
00002 // Copyright (C) 2014-2017 Cisco and/or its affiliates. All rights reserved.
00003 //
00004 // This program is free software; you can redistribute it and/or modify it
00005 // under the terms of the GNU General Public License Version 2 as published
00006 // by the Free Software Foundation. You may not use, modify or distribute
00007 // this program under any other version of the GNU General Public License.
00008 //
00009 // This program is distributed in the hope that it will be useful, but
00010 // WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program; if not, write to the Free Software Foundation, Inc.,
00016 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 //--------------------------------------------------------------------------
00018 // module.cc author Russ Combs <rucombs@cisco.com>
00019
00020 #ifdef HAVE_CONFIG_H
00021 #include "config.h"
00022 #endif
00023
00024 #include "module.h"
00025
00026 static const Parameter defaults[] =
00027 {
00028 { "trace", Parameter::PT_INT, nullptr, nullptr,
00029 "mask for enabling debug traces in module" },
00030
00031 { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr }
00032 };
00033
00034 std::string Command::get_arg_list() const
00035 {
00036 std::string args = "(";
00037 const Parameter* p = params;
00038
00039 while ( p and p->name )
00040 {
00041 if ( p != params )
00042 args += ", ";
00043 args += p->name;
00044 ++p;
00045 }
00046 args += ")";
00047 return args;
00048 }
00049
00050 void Module::init(const char* s, const char* h)
00051 {
00052 name = s;
00053 help = h;
00054 params = &defaults[(sizeof(defaults) / sizeof(Parameter)) - 1];
00055 default_params = params;
00056 list = false;
00057 num_counts = -1;
00058 }
00059
00060 Module::Module(const char* s, const char* h)
00061 { init(s, h); }
00062
00063 Module::Module(const char* s, const char* h, const Parameter* p, bool is_list, Trace* t)
00064 {
00065 init(s, h);
00066 list = is_list;
00067 trace = t;
00068 params = p;
00069
00070 // FIXIT-L: This will not be valid after adding more default options
00071 if ( t )
00072 default_params = defaults;
00073 }
00074
00075 bool Module::set(const char*, Value& v, SnortConfig*)
00076 {
00077 if ( v.is("trace") )
00078 {
00079 if ( trace )
00080 *trace = v.get_long();
00081 }
00082 else
00083 return false;
00084 return true;
00085 }
00086
00087 void Module::sum_stats(bool accumulate_now_stats)
00088 {
00089 if ( num_counts < 0 )
00090 reset_stats();
00091
00092 PegCount* p = get_counts();
00093 const PegInfo* q = get_pegs();
00094
00095 if ( !p )
00096 return;
00097
00098 assert(q);
00099
00100 if ( global_stats() )
00101 {
00102 for ( int i = 0; i < num_counts; i++ )
00103 set_peg_count(i, p[i]);
00104 }
00105 else
00106 {
00107 for ( int i = 0; i < num_counts; i++ )
00108 {
00109 switch ( q[i].type )
00110 {
00111 case CountType::END:
00112 break;
00113
00114 case CountType::SUM:
00115 add_peg_count(i, p[i]);
00116 p[i] = 0;
00117 break;
00118
00119 case CountType::NOW:
00120 if ( accumulate_now_stats )
00121 add_peg_count(i, p[i]);
00122 break;
00123
00124 case CountType::MAX:
00125 set_max_peg_count(i, p[i]);
00126 break;
00127 }
00128 }
00129 }
00130 }
00131
00132 void Module::show_interval_stats(IndexVec& peg_idxs, FILE* fh)
00133 {
00134 if ( num_counts > 0 )
00135 ::show_stats(get_counts(), get_pegs(), peg_idxs, get_name(), fh);
00136 }
00137
00138 void Module::show_stats()
00139 {
00140 if ( num_counts > 0 )
00141 ::show_stats(&counts[0], get_pegs(), num_counts, get_name());
00142 }
00143
00144 void Module::reset_stats()
00145 {
00146 if ( num_counts <= 0 )
00147 {
00148 num_counts = 0;
00149 const PegInfo* pegs = get_pegs();
00150
00151 if ( !pegs )
00152 return;
00153
00154 while ( pegs[num_counts].name )
00155 ++num_counts;
00156
00157 counts.resize(num_counts);
00158 }
00159
00160 for ( int i = 0; i < num_counts; i++ )
00161 counts[i] = 0;
00162 }
00163
00164 bool Module::verified_begin(const char* fqn, int idx, SnortConfig* c)
00165 {
00166 table_level++;
00167 return begin(fqn, idx, c);
00168 }
00169
00170 bool Module::verified_set(const char* fqn, Value& v, SnortConfig* c)
00171 {
00172 if ( list and table_level < 2 )
00173 return false;
00174
00175 return set(fqn, v, c);
00176 }
00177
00178 bool Module::verified_end(const char* fqn, int idx, SnortConfig* c)
00179 {
00180 table_level--;
00181 return end(fqn, idx, c);
00182 }
00183
00184 const PegInfo simple_pegs[] =
00185 {
00186 { CountType::SUM, "packets", "total packets" },
00187 { CountType::END, nullptr, nullptr }
00188 };
00189
END OF CODE