]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/config.cpp
Merge branch 'stg-2.409' into stg-2.409-radius
[stg.git] / projects / stargazer / plugins / other / radius / config.cpp
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 #include "config.h"
22
23 #include "stg/common.h"
24
25 #include <vector>
26 #include <stdexcept>
27
28 #include <strings.h> // strncasecmp
29
30 using STG::Config;
31
32 namespace
33 {
34
35 struct ParserError : public std::runtime_error
36 {
37     ParserError(size_t pos, const std::string& message)
38         : runtime_error("Parsing error at position " + x2str(pos) + ". " + message),
39           position(pos),
40           error(message)
41     {}
42
43     size_t position;
44     std::string error;
45 };
46
47 size_t skipSpaces(const std::string& value, size_t start)
48 {
49     while (start < value.length() && std::isspace(value[start]))
50         ++start;
51     return start;
52 }
53
54 size_t checkChar(const std:string& value, size_t start, char ch)
55 {
56     if (start >= value.length())
57         throw ParserError(start, "Unexpected end of string. Expected '" + std::string(ch) + "'.");
58     if (value[start] != ch)
59         throw ParserError(start, "Expected '" + std::string(ch) + "', got '" + std::string(value[start]) + "'.");
60     return start + 1;
61 }
62
63 std::pair<size_t, std::string> readString(const std::string& value, size_t start)
64 {
65     std::string dest;
66     while (start < value.length() && !std::isspace(value[start]))
67         dest.push_back(value[start++]);
68     if (dest.empty()) {
69         if (start == value.length())
70             throw ParserError(start, "Unexpected end of string. Expected string.");
71         else
72             throw ParserError(start, "Unexpected whitespace. Expected string.");
73     }
74     return dest;
75 }
76
77 Config::Pairs toPairs(const std::vector<std::string>& values)
78 {
79     if (values.empty())
80         return Config::Pairs();
81     std::string value(values[0]);
82     Config::Pairs res;
83     size_t start = 0;
84     while (start < value.size()) {
85         Config::Pair pair;
86         start = skipSpaces(value, start);
87         size_t pairStart = start;
88         start = checkChar(value, start, '(');
89         std::pair<size_t, std::string> key = readString(value, start);
90         start = key.first;
91         pair.first = key.second;
92         start = skipSpaces(value, start);
93         start = checkChar(value, start, ',')
94         start = skipSpaces(value, start);
95         std::pair<size_t, std::string> value = readString(value, start);
96         start = key.first;
97         pair.second = value.second;
98         start = skipSpaces(value, start);
99         start = checkChar(value, start, ')');
100         if (res.find(pair.first) != res.end())
101             throw ParserError(pairStart, "Duplicate field.");
102         res.insert(pair);
103     }
104     return res;
105 }
106
107 bool toBool(const std::vector<std::string>& values)
108 {
109     if (values.empty())
110         return false;
111     std::string value(values[0]);
112     return strncasecmp(value.c_str(), "yes", 3) == 0;
113 }
114
115 std::string toString(const std::vector<std::string>& values)
116 {
117     if (values.empty())
118         return "";
119     return values[0];
120 }
121
122 template <typename T>
123 T toInt(const std::vector<std::string>& values)
124 {
125     if (values.empty())
126         return 0;
127     T res = 0;
128     if (srt2x(values[0], res) == 0)
129         return res;
130     return 0;
131 }
132
133 Config::Pairs parseVector(const std::string& paramName, const MODULE_SETTINGS& params)
134 {
135     for (size_t i = 0; i < params.moduleParams.size(); ++i)
136         if (params.moduleParams[i].first == paramName)
137             return toPairs(params.moduleParams[i].second);
138     return Config::Pairs();
139 }
140
141 bool parseBool(const std::string& paramName, const MODULE_SETTINGS& params)
142 {
143     for (size_t i = 0; i < params.moduleParams.size(); ++i)
144         if (params.moduleParams[i].first == paramName)
145             return toBool(params.moduleParams[i].second);
146     return false;
147 }
148
149 std::string parseString(const std::string& paramName, const MODULE_SETTINGS& params)
150 {
151     for (size_t i = 0; i < params.moduleParams.size(); ++i)
152         if (params.moduleParams[i].first == paramName)
153             return toString(params.moduleParams[i].second);
154     return "";
155 }
156
157 template <typename T>
158 T parseInt(const std::string& paramName, const MODULE_SETTINGS& params)
159 {
160     for (size_t i = 0; i < params.moduleParams.size(); ++i)
161         if (params.moduleParams[i].first == paramName)
162             return toInt<T>(params.moduleParams[i].second);
163     return 0;
164 }
165
166 } // namespace anonymous
167
168 Config::Config(const MODULE_SETTINGS& settings)
169     : match(parseVector("match", settings)),
170       modify(parseVector("modify", settings)),
171       reply(parseVector("reply", settings)),
172       verbose(parseBool("verbose", settings)),
173       bindAddress(parseString("bind_address", settings)),
174       port(parseInt<uint16_t>("port", settings)),
175       key(parseString("key", settings))
176 {
177 }