]>
Commit | Line | Data |
---|---|---|
cb1b906f | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 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 | |
13 | #include "acl/FilledChecklist.h" | |
14 | #include "acl/Random.h" | |
8a01b99e | 15 | #include "cache_cf.h" |
582c2af2 | 16 | #include "Debug.h" |
8a01b99e | 17 | #include "Parsing.h" |
cb1b906f AJ |
18 | #include "wordlist.h" |
19 | ||
20 | ACL * | |
21 | ACLRandom::clone() const | |
22 | { | |
23 | return new ACLRandom(*this); | |
24 | } | |
25 | ||
7e10aa7b | 26 | ACLRandom::ACLRandom(char const *theClass) : data(0.0), class_(theClass) |
cb1b906f AJ |
27 | { |
28 | memset(pattern, 0 , sizeof(pattern)); | |
29 | } | |
30 | ||
7e10aa7b | 31 | ACLRandom::ACLRandom(ACLRandom const & old) : data(old.data), class_(old.class_) |
cb1b906f | 32 | { |
cb1b906f AJ |
33 | memcpy(pattern, old.pattern, sizeof(pattern)); |
34 | } | |
35 | ||
36 | ACLRandom::~ACLRandom() | |
37 | { } | |
38 | ||
39 | char const * | |
40 | ACLRandom::typeString() const | |
41 | { | |
42 | return class_; | |
43 | } | |
44 | ||
45 | bool | |
46 | ACLRandom::empty () const | |
47 | { | |
48 | return data == 0.0; | |
49 | } | |
50 | ||
478a0611 AJ |
51 | bool |
52 | ACLRandom::valid() const | |
53 | { | |
54 | return !empty(); | |
55 | } | |
56 | ||
cb1b906f AJ |
57 | /*******************/ |
58 | /* aclParseRandomList */ | |
59 | /*******************/ | |
60 | void | |
61 | ACLRandom::parse() | |
62 | { | |
ca5e75a4 | 63 | char *t; |
cb1b906f AJ |
64 | char bufa[256], bufb[256]; |
65 | ||
66 | t = strtokFile(); | |
726904ad AJ |
67 | if (!t) { |
68 | debugs(28, DBG_PARSE_NOTE(DBG_IMPORTANT), "ACL random missing pattern"); | |
478a0611 | 69 | return; |
726904ad | 70 | } |
478a0611 | 71 | |
cb1b906f AJ |
72 | debugs(28, 5, "aclParseRandomData: " << t); |
73 | ||
74 | // seed random generator ... | |
75 | srand(time(NULL)); | |
76 | ||
77 | if (sscanf(t, "%[0-9]:%[0-9]", bufa, bufb) == 2) { | |
78 | int a = xatoi(bufa); | |
79 | int b = xatoi(bufb); | |
726904ad | 80 | if (a <= 0 || b <= 0) { |
478a0611 AJ |
81 | debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'"); |
82 | return; | |
cb1b906f AJ |
83 | } else |
84 | data = a / (double)(a+b); | |
85 | } else if (sscanf(t, "%[0-9]/%[0-9]", bufa, bufb) == 2) { | |
86 | int a = xatoi(bufa); | |
87 | int b = xatoi(bufb); | |
726904ad | 88 | if (a <= 0 || b <= 0) { |
478a0611 AJ |
89 | debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'"); |
90 | return; | |
cb1b906f AJ |
91 | } else |
92 | data = (double) a / (double) b; | |
93 | } else if (sscanf(t, "0.%[0-9]", bufa) == 1) { | |
94 | data = atof(t); | |
95 | } else { | |
478a0611 AJ |
96 | debugs(28, DBG_CRITICAL, "ERROR: ACL random with bad pattern: '" << t << "'"); |
97 | return; | |
cb1b906f AJ |
98 | } |
99 | ||
100 | // save the exact input pattern. so we can display it later. | |
101 | memcpy(pattern, t, min(sizeof(pattern)-1,strlen(t))); | |
102 | } | |
103 | ||
104 | int | |
105 | ACLRandom::match(ACLChecklist *cl) | |
106 | { | |
107 | // make up the random value | |
108 | double random = ((double)rand() / (double)RAND_MAX); | |
109 | ||
110 | debugs(28, 3, "ACL Random: " << name << " " << pattern << " test: " << data << " > " << random << " = " << ((data > random)?"MATCH":"NO MATCH") ); | |
111 | return (data > random)?1:0; | |
112 | } | |
113 | ||
9b859d6f | 114 | SBufList |
cb1b906f AJ |
115 | ACLRandom::dump() const |
116 | { | |
9b859d6f FC |
117 | SBufList sl; |
118 | sl.push_back(SBuf(pattern)); | |
119 | return sl; | |
cb1b906f | 120 | } |
f53969cc | 121 |