Random.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 28 Access Control */
10
11#include "squid.h"
12#include "acl/FilledChecklist.h"
13#include "acl/Random.h"
14#include "debug/Stream.h"
15#include "Parsing.h"
16#include "wordlist.h"
17
18#include <random>
19
20ACLRandom::ACLRandom(char const *theClass) : data(0.0), class_(theClass)
21{
22 memset(pattern, 0, sizeof(pattern));
23}
24
26{ }
27
28char const *
30{
31 return class_;
32}
33
34bool
36{
37 return data == 0.0;
38}
39
40bool
42{
43 return !empty();
44}
45
46/*******************/
47/* aclParseRandomList */
48/*******************/
49void
51{
52 char bufa[256], bufb[256];
53
54 char *t = ConfigParser::strtokFile();
55 if (!t) {
56 debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT), "ERROR: ACL random missing pattern");
57 return;
58 }
59
60 debugs(28, 5, "aclParseRandomData: " << t);
61
62 // seed random generator ...
63 srand(time(nullptr));
64
65 if (sscanf(t, "%[0-9]:%[0-9]", bufa, bufb) == 2) {
66 int a = xatoi(bufa);
67 int b = xatoi(bufb);
68 if (a <= 0 || b <= 0) {
69 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
70 return;
71 } else
72 data = a / (double)(a+b);
73 } else if (sscanf(t, "%[0-9]/%[0-9]", bufa, bufb) == 2) {
74 int a = xatoi(bufa);
75 int b = xatoi(bufb);
76 if (a <= 0 || b <= 0 || a > b) {
77 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
78 return;
79 } else
80 data = (double) a / (double) b;
81 } else if (sscanf(t, "0.%[0-9]", bufa) == 1) {
82 data = atof(t);
83 } else {
84 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
85 return;
86 }
87
88 // save the exact input pattern. so we can display it later.
89 memcpy(pattern, t, min(sizeof(pattern)-1,strlen(t)));
90}
91
92int
94{
95 // make up the random value.
96 // The fixed-value default seed is fine because we are
97 // actually matching whether the random value is above
98 // or below the configured threshold ratio.
99 static std::mt19937 mt;
100 static std::uniform_real_distribution<> dist(0, 1);
101
102 const double random = dist(mt);
103
104 debugs(28, 3, "ACL Random: " << name << " " << pattern << " test: " << data << " > " << random << " = " << ((data > random)?"MATCH":"NO MATCH") );
105 return (data > random)?1:0;
106}
107
110{
111 SBufList sl;
112 sl.push_back(SBuf(pattern));
113 return sl;
114}
115
int xatoi(const char *token)
Definition: Parsing.cc:44
double data
Definition: Random.h:30
ACLRandom(char const *)
Definition: Random.cc:20
bool valid() const override
Definition: Random.cc:41
~ACLRandom() override
Definition: Random.cc:25
bool empty() const override
Definition: Random.cc:35
int match(ACLChecklist *checklist) override
Matches the actual data in checklist against this ACL.
Definition: Random.cc:93
void parse() override
parses node representation in squid.conf; dies on failures
Definition: Random.cc:50
char const * class_
Definition: Random.h:32
SBufList dump() const override
Definition: Random.cc:109
char pattern[256]
Definition: Random.h:31
char const * typeString() const override
Definition: Random.cc:29
char name[ACL_NAME_SZ]
Definition: Acl.h:87
static char * strtokFile()
Definition: ConfigParser.cc:65
Definition: SBuf.h:94
A const & min(A const &lhs, A const &rhs)
#define DBG_PARSE_NOTE(x)
Definition: Stream.h:42
#define DBG_IMPORTANT
Definition: Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define DBG_CRITICAL
Definition: Stream.h:37
std::list< SBuf > SBufList
Definition: forward.h:23
void EVH void double
Definition: stub_event.cc:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors