"Fossies" - the Fresh Open Source Software Archive

Member "snort3_extra-3.0.3-1/src/inspectors/mem_test/mem_test.cc" (23 Sep 2020, 5675 Bytes) of package /linux/misc/snort3_extra-3.0.3-1.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-2020 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     size_t size_of() override
   47     { return size; }
   48 
   49     void allocate(size_t);
   50     void deallocate(size_t);
   51 
   52 public:
   53     static unsigned data_id;
   54     std::vector<char*> data;
   55     char* base;
   56     size_t size;
   57 };
   58 
   59 unsigned MemTestData::data_id = 0;
   60 
   61 MemTestData::MemTestData(size_t n) : FlowData(data_id)
   62 {
   63     base = new char[n];
   64     size = n;
   65 }
   66 
   67 MemTestData::~MemTestData()
   68 {
   69     for ( auto* p : data )
   70     {
   71         size_t n = (size_t)atoi(p);
   72         delete[] p;
   73         update_deallocations(n);
   74     }
   75     delete[] base;
   76 }
   77 
   78 void MemTestData::allocate(size_t n)
   79 {
   80     if ( n < 32 ) n = 32;
   81     update_allocations(n);
   82     char* p = new char[n];
   83     snprintf(p, n, "%zu", n);
   84     data.push_back(p);
   85 }
   86 
   87 void MemTestData::deallocate(size_t n)
   88 {
   89     char buf[32];
   90     snprintf(buf, sizeof(buf), "%zu", n);
   91 
   92     for ( unsigned i = 0; i < data.size(); ++i )
   93     {
   94         if ( !data[i] or strcmp(buf, data[i]) )
   95             continue;
   96 
   97         delete[] data[i];
   98         data[i] = nullptr;
   99 
  100         assert(size >= n);
  101         size -= n;
  102         update_deallocations(n);
  103 
  104         return;
  105     }
  106 }
  107 
  108 //-------------------------------------------------------------------------
  109 // inspector stuff
  110 //-------------------------------------------------------------------------
  111 
  112 class MemTest : public Inspector
  113 {
  114 public:
  115     MemTest() { }
  116 
  117     void eval(Packet*) override;
  118 
  119 private:
  120     void begin(Flow*, size_t);
  121     void end(Flow*);
  122     void add(Flow*, size_t);
  123     void sub(Flow*, size_t);
  124 };
  125 
  126 // command format is <op><uint>\0
  127 // where <op> is ^, $, +, or - (new, del, add, or sub)
  128 
  129 void MemTest::eval(Packet* p)
  130 {
  131     assert(p->is_udp());
  132 
  133     if ( p->dsize < 3 or p->data[p->dsize - 1] != '\0' )
  134         return;
  135 
  136     size_t n = (size_t)atoi((const char*)(p->data) + 1);
  137     
  138     switch (p->data[0])
  139     {
  140     case '^': begin(p->flow, n); break;
  141     case '$': end(p->flow); break;
  142     case '+': add(p->flow, n); break;
  143     case '-': sub(p->flow, n); break;
  144     default: break;
  145     }
  146 }
  147 
  148 void MemTest::begin(Flow* f, size_t n)
  149 {
  150     MemTestData* d = new MemTestData(n);
  151     f->set_flow_data(d);
  152 }
  153 
  154 void MemTest::end(Flow* f)
  155 {
  156     f->free_flow_data(MemTestData::data_id);
  157 }
  158 
  159 void MemTest::add(Flow* f, size_t n)
  160 {
  161     MemTestData* d = (MemTestData*)f->get_flow_data(MemTestData::data_id);
  162     assert(d);
  163     d->allocate(n);
  164 }
  165 
  166 void MemTest::sub(Flow* f, size_t n)
  167 {
  168     MemTestData* d = (MemTestData*)f->get_flow_data(MemTestData::data_id);
  169     assert(d);
  170     d->deallocate(n);
  171 }
  172 
  173 //-------------------------------------------------------------------------
  174 // module stuff
  175 //-------------------------------------------------------------------------
  176 
  177 class MemTestModule : public Module
  178 {
  179 public:
  180     MemTestModule() : Module(s_name, s_help)
  181     { }
  182 
  183     const PegInfo* get_pegs() const override
  184     { return simple_pegs; }
  185 
  186     PegCount* get_counts() const override
  187     { return (PegCount*)&mt_stats; }
  188 
  189     Usage get_usage() const override
  190     { return INSPECT; }
  191 };
  192 
  193 //-------------------------------------------------------------------------
  194 // api stuff
  195 //-------------------------------------------------------------------------
  196 
  197 static void mt_init()
  198 { MemTestData::init(); }
  199 
  200 static Module* mod_ctor()
  201 { return new MemTestModule; }
  202 
  203 static void mod_dtor(Module* m)
  204 { delete m; }
  205 
  206 static Inspector* mt_ctor(Module*)
  207 {
  208     return new MemTest;
  209 }
  210 
  211 static void mt_dtor(Inspector* p)
  212 {
  213     delete p;
  214 }
  215 
  216 static const InspectApi mt_api
  217 {
  218     {
  219         PT_INSPECTOR,
  220         sizeof(InspectApi),
  221         INSAPI_VERSION,
  222         0,
  223         API_RESERVED,
  224         API_OPTIONS,
  225         s_name,
  226         s_help,
  227         mod_ctor,
  228         mod_dtor
  229     },
  230     IT_SERVICE,
  231     PROTO_BIT__UDP,
  232     nullptr, // buffers
  233     nullptr, // service
  234     nullptr, // pinit
  235     nullptr, // pterm
  236     mt_init, // tinit,
  237     nullptr, // tterm,
  238     mt_ctor,
  239     mt_dtor,
  240     nullptr, // ssn
  241     nullptr  // reset
  242 };
  243 
  244 SO_PUBLIC const BaseApi* snort_plugins[] =
  245 {
  246     &mt_api.base,
  247     nullptr
  248 };
  249