]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/Random.cc
Boilerplate: update copyright blurbs on Squid helpers
[thirdparty/squid.git] / src / acl / Random.cc
CommitLineData
cb1b906f
AJ
1/*
2 * DEBUG: section 28 Access Control
3 * AUTHOR: Amos Jeffries
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 *
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
33 */
34
582c2af2 35#include "squid.h"
cb1b906f
AJ
36
37#include "acl/FilledChecklist.h"
38#include "acl/Random.h"
8a01b99e 39#include "cache_cf.h"
582c2af2 40#include "Debug.h"
8a01b99e 41#include "Parsing.h"
cb1b906f
AJ
42#include "wordlist.h"
43
44ACL *
45ACLRandom::clone() const
46{
47 return new ACLRandom(*this);
48}
49
7e10aa7b 50ACLRandom::ACLRandom(char const *theClass) : data(0.0), class_(theClass)
cb1b906f
AJ
51{
52 memset(pattern, 0 , sizeof(pattern));
53}
54
7e10aa7b 55ACLRandom::ACLRandom(ACLRandom const & old) : data(old.data), class_(old.class_)
cb1b906f 56{
cb1b906f
AJ
57 memcpy(pattern, old.pattern, sizeof(pattern));
58}
59
60ACLRandom::~ACLRandom()
61{ }
62
63char const *
64ACLRandom::typeString() const
65{
66 return class_;
67}
68
69bool
70ACLRandom::empty () const
71{
72 return data == 0.0;
73}
74
478a0611
AJ
75bool
76ACLRandom::valid() const
77{
78 return !empty();
79}
80
cb1b906f
AJ
81/*******************/
82/* aclParseRandomList */
83/*******************/
84void
85ACLRandom::parse()
86{
ca5e75a4 87 char *t;
cb1b906f
AJ
88 char bufa[256], bufb[256];
89
90 t = strtokFile();
726904ad
AJ
91 if (!t) {
92 debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT), "ACL random missing pattern");
478a0611 93 return;
726904ad 94 }
478a0611 95
cb1b906f
AJ
96 debugs(28, 5, "aclParseRandomData: " << t);
97
98 // seed random generator ...
99 srand(time(NULL));
100
101 if (sscanf(t, "%[0-9]:%[0-9]", bufa, bufb) == 2) {
102 int a = xatoi(bufa);
103 int b = xatoi(bufb);
726904ad 104 if (a <= 0 || b <= 0) {
478a0611
AJ
105 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
106 return;
cb1b906f
AJ
107 } else
108 data = a / (double)(a+b);
109 } else if (sscanf(t, "%[0-9]/%[0-9]", bufa, bufb) == 2) {
110 int a = xatoi(bufa);
111 int b = xatoi(bufb);
726904ad 112 if (a <= 0 || b <= 0) {
478a0611
AJ
113 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
114 return;
cb1b906f
AJ
115 } else
116 data = (double) a / (double) b;
117 } else if (sscanf(t, "0.%[0-9]", bufa) == 1) {
118 data = atof(t);
119 } else {
478a0611
AJ
120 debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'");
121 return;
cb1b906f
AJ
122 }
123
124 // save the exact input pattern. so we can display it later.
125 memcpy(pattern, t, min(sizeof(pattern)-1,strlen(t)));
126}
127
128int
129ACLRandom::match(ACLChecklist *cl)
130{
131 // make up the random value
132 double random = ((double)rand() / (double)RAND_MAX);
133
134 debugs(28, 3, "ACL Random: " << name << " " << pattern << " test: " << data << " > " << random << " = " << ((data > random)?"MATCH":"NO MATCH") );
135 return (data > random)?1:0;
136}
137
9b859d6f 138SBufList
cb1b906f
AJ
139ACLRandom::dump() const
140{
9b859d6f
FC
141 SBufList sl;
142 sl.push_back(SBuf(pattern));
143 return sl;
cb1b906f 144}