2 * Copyright (C) 1996-2017 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"
21 ACLRandom::clone() const
23 return new ACLRandom(*this);
26 ACLRandom::ACLRandom(char const *theClass
) : data(0.0), class_(theClass
)
28 memset(pattern
, 0 , sizeof(pattern
));
31 ACLRandom::ACLRandom(ACLRandom
const & old
) : data(old
.data
), class_(old
.class_
)
33 memcpy(pattern
, old
.pattern
, sizeof(pattern
));
36 ACLRandom::~ACLRandom()
40 ACLRandom::typeString() const
46 ACLRandom::empty () const
52 ACLRandom::valid() const
58 /* aclParseRandomList */
63 char bufa
[256], bufb
[256];
65 char *t
= ConfigParser::strtokFile();
67 debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ACL random missing pattern");
71 debugs(28, 5, "aclParseRandomData: " << t
);
73 // seed random generator ...
76 if (sscanf(t
, "%[0-9]:%[0-9]", bufa
, bufb
) == 2) {
79 if (a
<= 0 || b
<= 0) {
80 debugs(28, DBG_CRITICAL
, "ERROR: ACL random with bad pattern: '" << t
<< "'");
83 data
= a
/ (double)(a
+b
);
84 } else if (sscanf(t
, "%[0-9]/%[0-9]", bufa
, bufb
) == 2) {
87 if (a
<= 0 || b
<= 0 || a
> b
) {
88 debugs(28, DBG_CRITICAL
, "ERROR: ACL random with bad pattern: '" << t
<< "'");
91 data
= (double) a
/ (double) b
;
92 } else if (sscanf(t
, "0.%[0-9]", bufa
) == 1) {
95 debugs(28, DBG_CRITICAL
, "ERROR: ACL random with bad pattern: '" << t
<< "'");
99 // save the exact input pattern. so we can display it later.
100 memcpy(pattern
, t
, min(sizeof(pattern
)-1,strlen(t
)));
104 ACLRandom::match(ACLChecklist
*)
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
;
111 static xuniform_real_distribution
<> dist(0, 1);
113 const double random
= dist(mt
);
115 debugs(28, 3, "ACL Random: " << name
<< " " << pattern
<< " test: " << data
<< " > " << random
<< " = " << ((data
> random
)?"MATCH":"NO MATCH") );
116 return (data
> random
)?1:0;
120 ACLRandom::dump() const
123 sl
.push_back(SBuf(pattern
));