"Fossies" - the Fresh Open Source Software Archive

Member "snort3_extra-3.1.53.0/src/inspectors/mem_test/mem_test.cc" (20 Dec 2022, 5490 Bytes) of package /linux/misc/snort3_extra-3.1.53.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 //--------------------------------------------------------------------------
    2 // Copyright (C) 2015-2022 Cisco and/or its affiliates. All rights reserved.
    3 //
    4 // This program is free software; you can redistribute it and/or modify it
    5 // under the terms of the GNU General Public License Version 2 as published
    6 // by the Free Software Foundation.  You may not use, modify or distribute
    7 // this program under any other version of the GNU General Public License.
    8 //
    9 // This program is distributed in the hope that it will be useful, but
   10 // WITHOUT ANY WARRANTY; without even the implied warranty of
   11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12 // General Public License for more details.
   13 //
   14 // You should have received a copy of the GNU General Public License along
   15 // with this program; if not, write to the Free Software Foundation, Inc.,
   16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   17 //--------------------------------------------------------------------------
   18 // mem_test.cc author Russ Combs <rcombs@sourcefire.com>
   19 
   20 #include "flow/flow.h"
   21 #include "framework/inspector.h"
   22 #include "framework/module.h"
   23 #include "log/messages.h"
   24 #include "protocols/packet.h"
   25 
   26 using namespace snort;
   27 
   28 static const char* s_name = "mem_test";
   29 static const char* s_help = "for testing memory management";
   30 
   31 static THREAD_LOCAL SimpleStats mt_stats;
   32 
   33 //-------------------------------------------------------------------------
   34 // flow data stuff
   35 //-------------------------------------------------------------------------
   36 
   37 class MemTestData : public FlowData
   38 {
   39 public:
   40     MemTestData(size_t);
   41     ~MemTestData() override;
   42 
   43     static void init()
   44     { data_id = FlowData::create_flow_data_id(); }
   45 
   46     void allocate(size_t);
   47     void deallocate(size_t);
   48 
   49 public:
   50     static unsigned data_id;
   51     std::vector<char*> data;
   52     char* base;
   53     size_t size;
   54 };
   55 
   56 unsigned MemTestData::data_id = 0;
   57 
   58 MemTestData::MemTestData(size_t n) : FlowData(data_id)
   59 {
   60     base = new char[n];
   61     size = n;
   62 }
   63 
   64 MemTestData::~MemTestData()
   65 {
   66     for ( auto* p : data )
   67         delete[] p;
   68 
   69     delete[] base;
   70 }
   71 
   72 void MemTestData::allocate(size_t n)
   73 {
   74     if ( n < 32 ) n = 32;
   75     char* p = new char[n];
   76     snprintf(p, n, "%zu", n);
   77     data.push_back(p);
   78 }
   79 
   80 void MemTestData::deallocate(size_t n)
   81 {
   82     char buf[32];
   83     snprintf(buf, sizeof(buf), "%zu", n);
   84 
   85     for ( unsigned i = 0; i < data.size(); ++i )
   86     {
   87         if ( !data[i] or strcmp(buf, data[i]) )
   88             continue;
   89 
   90         delete[] data[i];
   91         data[i] = nullptr;
   92 
   93         assert(size >= n);
   94         size -= n;
   95 
   96         return;
   97     }
   98 }
   99 
  100 //-------------------------------------------------------------------------
  101 // inspector stuff
  102 //-------------------------------------------------------------------------
  103 
  104 class MemTest : public Inspector
  105 {
  106 public:
  107     MemTest() = default;
  108 
  109     void eval(Packet*) override;
  110 
  111 private:
  112     void begin(Flow*, size_t);
  113     void end(Flow*);
  114     void add(Flow*, size_t);
  115     void sub(Flow*, size_t);
  116 };
  117 
  118 // command format is <op><uint>\0
  119 // where <op> is ^, $, +, or - (new, del, add, or sub)
  120 
  121 void MemTest::eval(Packet* p)
  122 {
  123     assert(p->is_udp());
  124 
  125     if ( p->dsize < 3 or p->data[p->dsize - 1] != '\0' )
  126         return;
  127 
  128     size_t n = (size_t)atoi((const char*)(p->data) + 1);
  129     
  130     switch (p->data[0])
  131     {
  132     case '^': begin(p->flow, n); break;
  133     case '$': end(p->flow); break;
  134     case '+': add(p->flow, n); break;
  135     case '-': sub(p->flow, n); break;
  136     default: break;
  137     }
  138 }
  139 
  140 void MemTest::begin(Flow* f, size_t n)
  141 {
  142     MemTestData* d = new MemTestData(n);
  143     f->set_flow_data(d);
  144 }
  145 
  146 void MemTest::end(Flow* f)
  147 {
  148     f->free_flow_data(MemTestData::data_id);
  149 }
  150 
  151 void MemTest::add(Flow* f, size_t n)
  152 {
  153     MemTestData* d = (MemTestData*)f->get_flow_data(MemTestData::data_id);
  154     assert(d);
  155     d->allocate(n);
  156 }
  157 
  158 void MemTest::sub(Flow* f, size_t n)
  159 {
  160     MemTestData* d = (MemTestData*)f->get_flow_data(MemTestData::data_id);
  161     assert(d);
  162     d->deallocate(n);
  163 }
  164 
  165 //-------------------------------------------------------------------------
  166 // module stuff
  167 //-------------------------------------------------------------------------
  168 
  169 class MemTestModule : public Module
  170 {
  171 public:
  172     MemTestModule() : Module(s_name, s_help)
  173     { }
  174 
  175     const PegInfo* get_pegs() const override
  176     { return simple_pegs; }
  177 
  178     PegCount* get_counts() const override
  179     { return (PegCount*)&mt_stats; }
  180 
  181     Usage get_usage() const override
  182     { return INSPECT; }
  183 };
  184 
  185 //-------------------------------------------------------------------------
  186 // api stuff
  187 //-------------------------------------------------------------------------
  188 
  189 static void mt_init()
  190 { MemTestData::init(); }
  191 
  192 static Module* mod_ctor()
  193 { return new MemTestModule; }
  194 
  195 static void mod_dtor(Module* m)
  196 { delete m; }
  197 
  198 static Inspector* mt_ctor(Module*)
  199 {
  200     return new MemTest;
  201 }
  202 
  203 static void mt_dtor(Inspector* p)
  204 {
  205     delete p;
  206 }
  207 
  208 static const InspectApi mt_api
  209 {
  210     {
  211         PT_INSPECTOR,
  212         sizeof(InspectApi),
  213         INSAPI_VERSION,
  214         0,
  215         API_RESERVED,
  216         API_OPTIONS,
  217         s_name,
  218         s_help,
  219         mod_ctor,
  220         mod_dtor
  221     },
  222     IT_SERVICE,
  223     PROTO_BIT__UDP,
  224     nullptr, // buffers
  225     nullptr, // service
  226     nullptr, // pinit
  227     nullptr, // pterm
  228     mt_init, // tinit,
  229     nullptr, // tterm,
  230     mt_ctor,
  231     mt_dtor,
  232     nullptr, // ssn
  233     nullptr  // reset
  234 };
  235 
  236 SO_PUBLIC const BaseApi* snort_plugins[] =
  237 {
  238     &mt_api.base,
  239     nullptr
  240 };
  241