2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 28 Access Control */
12 #include "acl/FilledChecklist.h"
13 #include "acl/Random.h"
14 #include "debug/Stream.h"
20 ACLRandom::ACLRandom(char const *theClass
) : data(0.0), class_(theClass
)
22 memset(pattern
, 0, sizeof(pattern
));
25 ACLRandom::~ACLRandom()
29 ACLRandom::typeString() const
35 ACLRandom::empty () const
41 ACLRandom::valid() const
47 /* aclParseRandomList */
52 char bufa
[256], bufb
[256];
54 char *t
= ConfigParser::strtokFile();
56 debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: ACL random missing pattern");
60 debugs(28, 5, "aclParseRandomData: " << t
);
62 // seed random generator ...
65 if (sscanf(t
, "%[0-9]:%[0-9]", bufa
, bufb
) == 2) {
68 if (a
<= 0 || b
<= 0) {
69 debugs(28, DBG_CRITICAL
, "ERROR: ACL random with bad pattern: '" << t
<< "'");
72 data
= a
/ (double)(a
+b
);
73 } else if (sscanf(t
, "%[0-9]/%[0-9]", bufa
, bufb
) == 2) {
76 if (a
<= 0 || b
<= 0 || a
> b
) {
77 debugs(28, DBG_CRITICAL
, "ERROR: ACL random with bad pattern: '" << t
<< "'");
80 data
= (double) a
/ (double) b
;
81 } else if (sscanf(t
, "0.%[0-9]", bufa
) == 1) {
84 debugs(28, DBG_CRITICAL
, "ERROR: ACL random with bad pattern: '" << t
<< "'");
88 // save the exact input pattern. so we can display it later.
89 memcpy(pattern
, t
, min(sizeof(pattern
)-1,strlen(t
)));
93 ACLRandom::match(ACLChecklist
*)
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);
102 const double random
= dist(mt
);
104 debugs(28, 3, "ACL Random: " << name
<< " " << pattern
<< " test: " << data
<< " > " << random
<< " = " << ((data
> random
)?"MATCH":"NO MATCH") );
105 return (data
> random
)?1:0;
109 ACLRandom::dump() const
112 sl
.push_back(SBuf(pattern
));