]> git.stg.codes - stg.git/blob - projects/traffcounter/rules.h
Добавление исходников
[stg.git] / projects / traffcounter / rules.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.1.1.1 $
23  $Date: 2009/02/24 08:13:03 $
24  $Author: faust $
25  */
26
27
28 #ifndef __RULES_H__
29 #define __RULES_H__
30
31 #include <list>
32 #include <string>
33 #include <sstream>
34 #include <map>
35
36 #ifdef HAVE_STDINT
37     #include <stdint.h>
38 #else
39     #ifdef HAVE_INTTYPES
40         #include <inttypes.h>
41     #else
42         #error "You need either stdint.h or inttypes.h to compile this!"
43     #endif
44 #endif
45
46 namespace STG
47 {
48
49     //-----------------------------------------------------------------------------
50     struct RULE
51     {
52     uint32_t    ip;             // IP
53     uint32_t    mask;           // Netmask
54     uint16_t    port1;          // Port 1
55     uint16_t    port2;          // Port 2
56     int         proto;          // Protocol
57     int         dir;            // Direction
58     };
59     //-----------------------------------------------------------------------------
60     typedef std::list<RULE> RULES;
61     //-----------------------------------------------------------------------------
62     class RULES_PARSER
63     {
64     public:
65         RULES_PARSER();
66
67         RULES_PARSER(const std::string & fileName);
68
69         ~RULES_PARSER() {};
70
71         void SetFile(const std::string & fileName);
72
73         const RULES & GetRules() const { return rules; };
74         bool IsError() const { return error; };
75         const std::string ErrorMsg() const { return errorStream.str(); };
76
77     private:
78         RULES rules;
79         bool error;
80         mutable std::stringstream errorStream;
81         std::map<std::string, int> protocols;
82
83         bool InitProtocols();
84         bool ParseLine(std::string line);
85         bool ParseAddress(const std::string & address, RULE * rule) const;
86         bool ParseMask(const std::string & mask, RULE * rule) const;
87         bool ParsePorts(const std::string & port1,
88                         const std::string & port2,
89                         RULE * rule) const;
90     };
91
92 }
93
94 #endif // __RULES_H__