-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConfig.hpp
118 lines (103 loc) · 4.33 KB
/
Config.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef RKNOCKD_CONFIG_HPP
#define RKNOCKD_CONFIG_HPP
#include <stdexcept>
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <cstdint>
#include <libxml++/libxml++.h>
#include "common.h"
namespace Rknockd
{
char bintohex(uint8_t c);
uint8_t hextobin(char c);
class ConfigException : public std::runtime_error
{
public:
ConfigException(const std::string& d);
};
class Protocol
{
public:
static const Protocol TCP;
static const Protocol UDP;
Protocol(const Protocol& p);
Protocol& operator=(const Protocol& p);
bool operator==(unsigned num) const;
bool operator==(const Protocol& p) const;
bool operator!=(unsigned num) const;
bool operator!=(const Protocol& p) const;
unsigned getNumber() const;
const std::string& getName() const;
friend std::ostream& operator<<(std::ostream&, const Protocol&);
private:
Protocol(unsigned num, const std::string& n);
unsigned number;
std::string name;
};
std::ostream& operator<<(std::ostream& out, const Protocol& p);
class Config
{
public:
Config(const std::string& filename);
virtual ~Config();
const std::string& getFile() const;
const std::string& getRandomDevice() const;
uint16_t getBasePort() const;
unsigned getChallengeBytes() const;
uint16_t getNfQueueNum() const;
uint32_t getOverrideServerAddr() const;
virtual void printConfig(std::ostream& os) const;
protected:
void readFile() THROW((ConfigException));
virtual void parseRknockdAttrs(const xmlpp::Element* elmt) THROW((ConfigException));
void parseRknockdChildren(const xmlpp::Element* elmt) THROW((ConfigException));
virtual void addRequest(const xmlpp::Element* elmt) THROW((ConfigException)) = 0;
std::string file; // the name of the configuration file
uint16_t basePort; // the low-numbered port of the knock range
unsigned challengeBytes; // the number of bytes to send in a challenge
std::string randomDevice; // the name of the random number device
uint16_t nfQueueNum;
bool overrideServerAddr;
uint32_t serverAddr;
};
class RequestBase
{
public:
RequestBase();
virtual ~RequestBase();
const Protocol& getProtocol() const;
uint16_t getPort() const;
uint32_t getAddr() const;
uint16_t getTTL() const;
bool getIgnoreClientAddr() const;
const std::string getSecret() const;
virtual void printRequest(std::ostream& os) const;
//RequestBase& operator=(const RequestBase& req);
protected:
void parseRequest(const xmlpp::Element* elmt, const Config* config) THROW((ConfigException));
virtual void parseRequestString(const std::string& str, const Config* config) THROW((ConfigException)) = 0;
Protocol proto;
uint16_t port;
uint16_t ttl;
bool ignoreClientAddr;
std::string secret;
};
template <typename RequestStrType, typename RequestPrinterType, typename RequestParserType, typename ConfigType>
class Request : public RequestBase
{
public:
typedef RequestStrType RequestString;
Request(const xmlpp::Element* elmt, const ConfigType& config) THROW((ConfigException));
const RequestStrType& getRequestString() const;
void printRequest(std::ostream& os) const;
private:
void parseRequestString(const std::string& str, const Config* config) THROW((ConfigException));
RequestStrType requestStr;
static const RequestPrinterType requestPrinter;
static const RequestParserType requestParser;
};
} // namespace Rknockd
#include "Config_impl.cpp"
#endif // RKNOCKD_CONFIG_HPP