]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/Random.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / acl / Random.cc
CommitLineData
cb1b906f 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
cb1b906f 3 *
bbc27441
AJ
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.
cb1b906f
AJ
7 */
8
bbc27441
AJ
9/* DEBUG: section 28 Access Control */
10
582c2af2 11#include "squid.h"
cb1b906f
AJ
12#include "acl/FilledChecklist.h"
13#include "acl/Random.h"
582c2af2 14#include "Debug.h"
8a01b99e 15#include "Parsing.h"
cb1b906f
AJ
16#include "wordlist.h"
17
20b10025
AJ
18#include <random>
19
cb1b906f
AJ
20ACL *
21ACLRandom::clone() const
22{
23 return new ACLRandom(*this);
24}
25
7e10aa7b 26ACLRandom::ACLRandom(char const *theClass) : data(0.0), class_(theClass)
cb1b906f 27{
9e167fa2 28 memset(pattern, 0, sizeof(pattern));
cb1b906f
AJ
29}
30
7e10aa7b 31ACLRandom::ACLRandom(ACLRandom const & old) : data(old.data), class_(old.class_)
cb1b906f 32{
cb1b906f
AJ
33 memcpy(pattern, old.pattern, sizeof(pattern));
34}
35
36ACLRandom::~ACLRandom()
37{ }
38
39char const *
40ACLRandom::typeString() const
41{
42 return class_;
43}
44
45bool
46ACLRandom::empty () const
47{
48 return data == 0.0;
49}
50
478a0611
AJ
51bool
52ACLRandom::valid() const
53{
54 return !empty();
55}
56
cb1b906f
AJ
57/*******************/
58/* aclParseRandomList */
59/*******************/
60void
61ACLRandom::parse()
62{
cb1b906f
AJ
63 char bufa[256], bufb[256];
64
16c5ad96 65 char *t = ConfigParser::strtokFile();
726904ad
AJ
66 if (!t) {
67 debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT), "ACL random missing pattern");
478a0611 68 return;
726904ad 69 }
478a0611 70
cb1b906f
AJ
71 debugs(28, 5, "aclParseRandomData: " << t);
72
73 // seed random generator ...
74 srand(time(NULL));
75
76 if (sscanf(t, "%[0-9]:%[0-9]", bufa, bufb) == 2) {
77 int a = xatoi(bufa);
78 int b = xatoi(bufb);
726904ad 79 if (a <= 0 || b <= 0) {
478a0611
AJ
80 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
81 return;
cb1b906f
AJ
82 } else
83 data = a / (double)(a+b);
84 } else if (sscanf(t, "%[0-9]/%[0-9]", bufa, bufb) == 2) {
85 int a = xatoi(bufa);
86 int b = xatoi(bufb);
20b10025 87 if (a <= 0 || b <= 0 || a > b) {
478a0611
AJ
88 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
89 return;
cb1b906f
AJ
90 } else
91 data = (double) a / (double) b;
92 } else if (sscanf(t, "0.%[0-9]", bufa) == 1) {
93 data = atof(t);
94 } else {
478a0611
AJ
95 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
96 return;
cb1b906f
AJ
97 }
98
99 // save the exact input pattern. so we can display it later.
100 memcpy(pattern, t, min(sizeof(pattern)-1,strlen(t)));
101}
102
103int
ced8def3 104ACLRandom::match(ACLChecklist *)
cb1b906f 105{
20b10025
AJ
106 // make up the random value.
107 // The fixed-value default seed is fine because we are
108 // actually matching whether the random value is above
109 // or below the configured threshold ratio.
110 static std::mt19937 mt;
8ed8fa40 111 static xuniform_real_distribution<> dist(0, 1);
20b10025
AJ
112
113 const double random = dist(mt);
cb1b906f
AJ
114
115 debugs(28, 3, "ACL Random: " << name << " " << pattern << " test: " << data << " > " << random << " = " << ((data > random)?"MATCH":"NO MATCH") );
116 return (data > random)?1:0;
117}
118
9b859d6f 119SBufList
cb1b906f
AJ
120ACLRandom::dump() const
121{
9b859d6f
FC
122 SBufList sl;
123 sl.push_back(SBuf(pattern));
124 return sl;
cb1b906f 125}
f53969cc 126