Branch data Line data Source code
1 : : // $Id: Rule.h 6914 2009-09-22 00:35:24Z vern $
2 : :
3 : : #ifndef rule_h
4 : : #define rule_h
5 : :
6 : : #include <limits.h>
7 : :
8 : : #include "Obj.h"
9 : : #include "List.h"
10 : : #include "Dict.h"
11 : : #include "util.h"
12 : :
13 : : class RuleCondition;
14 : : class RuleAction;
15 : : class RuleHdrTest;
16 : :
17 : : class Rule;
18 : :
19 : 70 : declare(PList, Rule);
20 : : typedef PList(Rule) rule_list;
21 : :
22 [ # # ][ # # ]: 43 : declare(PDict, Rule);
23 : : typedef PDict(Rule) rule_dict;
24 : :
25 : : class Rule {
26 : : public:
27 : 21 : Rule(const char* arg_id, const Location& arg_location)
28 : 21 : {
29 : 21 : id = copy_string(arg_id);
30 : 21 : idx = rule_counter++;
31 : 21 : location = arg_location;
32 : 21 : active = true;
33 : 21 : }
34 : :
35 : : ~Rule();
36 : :
37 : 42 : const char* ID() const { return id; }
38 : 0 : unsigned int Index() const { return idx; }
39 : :
40 : : enum PatternType {
41 : : PAYLOAD, HTTP_REQUEST, HTTP_REQUEST_BODY, HTTP_REQUEST_HEADER,
42 : : HTTP_REPLY_BODY, HTTP_REPLY_HEADER, FTP, FINGER, TYPES,
43 : : };
44 : :
45 : 0 : bool Active() { return active; }
46 : 0 : void SetActiveStatus(bool new_status) { active = new_status; }
47 : 11 : void AddAction(RuleAction* act) { actions.append(act); }
48 : 19 : void AddCondition(RuleCondition* cond) { conditions.append(cond); }
49 : 21 : void AddHdrTest(RuleHdrTest* hdr_test) { hdr_tests.append(hdr_test); }
50 : : void AddPattern(const char* str, Rule::PatternType type,
51 : : uint32 offset = 0, uint32 depth = INT_MAX);
52 : : void AddRequires(const char* id, bool opposite_direction, bool negate);
53 : :
54 : 0 : const Location& GetLocation() const { return location; }
55 : :
56 : : void PrintDebug();
57 : :
58 : : static const char* TypeToString(Rule::PatternType type);
59 : :
60 : : private:
61 : : friend class RuleMatcher;
62 : :
63 : : void SortHdrTests();
64 : :
65 : 32 : declare(PList, RuleAction);
66 : : typedef PList(RuleAction) rule_action_list;
67 : :
68 : 40 : declare(PList, RuleCondition);
69 : : typedef PList(RuleCondition) rule_condition_list;
70 : :
71 : 42 : declare(PList, RuleHdrTest);
72 : : typedef PList(RuleHdrTest) rule_hdr_test_list;
73 : :
74 : : rule_hdr_test_list hdr_tests;
75 : : rule_condition_list conditions;
76 : : rule_action_list actions;
77 : :
78 : : // Matching of this rule can depend on the state of other rules.
79 : : struct Precond {
80 : : const char* id;
81 : : Rule* rule; // set by RuleMatcher
82 : : bool opposite_dir; // if true, rule must match other dir.
83 : : bool negate; // negate test
84 : : };
85 : :
86 : 32 : declare(PList, Precond);
87 : : typedef PList(Precond) precond_list;
88 : :
89 : : precond_list preconds;
90 : : rule_list dependents; // rules w/ us as a precondition
91 : : // (set by RuleMatcher)
92 : :
93 : : const char* id;
94 : : unsigned int idx; // unique index of this rule
95 : : bool active; // set the active status of the rule, default true
96 : :
97 : : struct Pattern {
98 : : char* pattern; // the pattern itself
99 : : PatternType type;
100 : : int id; // ID of pattern (for identifying it within regexps)
101 : : uint32 offset;
102 : : uint32 depth;
103 : : };
104 : :
105 : 42 : declare(PList, Pattern);
106 : : typedef PList(Pattern) pattern_list;
107 : : pattern_list patterns;
108 : :
109 : : Rule* next; // Linkage within RuleHdrTest tree:
110 : : // Ptr to next rule using the same RuleHdrTests
111 : :
112 : : Location location;
113 : :
114 : : // Rules and payloads are numbered individually.
115 : : static unsigned int rule_counter;
116 : : static unsigned int pattern_counter;
117 : :
118 : : // Array of rules indexed by payloadid.
119 : : static rule_list rule_table;
120 : : };
121 : :
122 : : #endif
|