forked from snort3/snort3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflush_bucket.cc
124 lines (97 loc) · 3.51 KB
/
flush_bucket.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//--------------------------------------------------------------------------
// Copyright (C) 2014-2024 Cisco and/or its affiliates. All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License Version 2 as published
// by the Free Software Foundation. You may not use, modify or distribute
// this program under any other version of the GNU General Public License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//--------------------------------------------------------------------------
// flush_bucket.cc author Russ Combs <[email protected]>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "flush_bucket.h"
#include <cassert>
#include <random>
#include "main/snort_config.h"
#include "utils/util.h"
using namespace snort;
//-------------------------------------------------------------------------
// static base members
//-------------------------------------------------------------------------
static THREAD_LOCAL FlushBucket* s_flush_bucket = nullptr;
void FlushBucket::set(unsigned sz)
{
if ( s_flush_bucket )
return;
if ( sz )
s_flush_bucket = new ConstFlushBucket(sz);
else if ( SnortConfig::static_hash() )
s_flush_bucket = new StaticFlushBucket;
else
s_flush_bucket = new RandomFlushBucket;
assert(s_flush_bucket);
}
void FlushBucket::set()
{ set(0); }
void FlushBucket::clear()
{
delete s_flush_bucket;
s_flush_bucket = nullptr;
}
uint16_t FlushBucket::get_size()
{
return s_flush_bucket->get_next();
}
//-------------------------------------------------------------------------
// var flush points
//-------------------------------------------------------------------------
void VarFlushBucket::set_next(uint16_t pt)
{
flush_points.emplace_back(pt);
}
uint16_t VarFlushBucket::get_next()
{
if ( idx >= flush_points.size() )
idx = 0;
return flush_points[idx++];
}
//-------------------------------------------------------------------------
// static flush points
//-------------------------------------------------------------------------
#define NUM_FLUSH_POINTS 64
static const uint8_t fixed_points[NUM_FLUSH_POINTS] =
{
128, 217, 189, 130, 240, 221, 134, 129,
250, 232, 141, 131, 144, 177, 201, 130,
230, 190, 177, 142, 130, 200, 173, 129,
250, 244, 174, 151, 201, 190, 180, 198,
220, 201, 142, 185, 219, 129, 194, 140,
145, 191, 197, 183, 199, 220, 231, 245,
233, 135, 143, 158, 174, 194, 200, 180,
201, 142, 153, 187, 173, 199, 143, 201
};
StaticFlushBucket::StaticFlushBucket()
{
for ( int i = 0; i < NUM_FLUSH_POINTS; i++ )
set_next(fixed_points[i]);
}
//-------------------------------------------------------------------------
// random flush points
//-------------------------------------------------------------------------
RandomFlushBucket::RandomFlushBucket()
{
std::default_random_engine generator(get_random_seed());
std::uniform_int_distribution<int> distribution(128, 255);
for ( int i = 0; i < NUM_FLUSH_POINTS; i++ )
set_next((uint16_t)distribution(generator));
}