-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.l
executable file
·303 lines (245 loc) · 5.04 KB
/
config.l
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
%{
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <config.h>
#include <config_parser_context.h>
struct extra_t
{
extra_t():
line(1),
column(1)
{
}
std::string s;
int line;
int column;
};
#define YY_USER_ACTION \
yylloc->first_line = yyextra.line; \
yylloc->last_line = yyextra.line; \
yylloc->first_column = yyextra.column; \
yyextra.column += yyleng - 1; \
yylloc->last_column = yyextra.column;
#define YY_USER_INIT new (&yyextra) extra_t;
%}
%option bison-bridge
%option bison-locations
%option reentrant
%option prefix="config"
%option extra-type="extra_t"
%option nodefault
%option noyywrap
%option nounput
%x STR
int_value [0-9]+
ip4_addr {int_value}\.{int_value}\.{int_value}\.{int_value}
%%
#.* /* ignore comments */
global return ID_GLOBAL;
chain return ID_CHAIN;
proxy_dns return ID_PROXY_DNS;
trace return ID_TRACE;
off return ID_OFF;
stdout return ID_STDOUT;
stderr return ID_STDERR;
tty_only return ID_TTY_ONLY;
chain_type return ID_CHAIN_TYPE;
chain_len return ID_CHAIN_LEN;
tcp_connect_timeout return ID_TCP_CONNECT_TIMEOUT;
tcp_read_timeout return ID_TCP_READ_TIMEOUT;
default_filter_action return ID_DEFAULT_FILTER_ACTION;
proxy return ID_PROXY;
filter return ID_FILTER;
any return ID_ANY;
{int_value} {
char t[128];
if(yyleng > sizeof(t))
{
// internal buffer is too small
return 0;
}
memcpy(t, yytext, yyleng);
t[yyleng] = 0;
char* endp = 0;
yylval->i = strtol(t, &endp, 10);
if(*endp == '\0')
{
return INT;
}
else
{
// overflow
return 0;
}
}
false|no|n {
yylval->b = false;
return BOOLEAN;
}
true|yes|y {
yylval->b = true;
return BOOLEAN;
}
{ip4_addr}(\/{int_value})?(:{int_value})? {
// copy token into temporal buffer
char temp_buf[128];
if(yyleng > sizeof(temp_buf))
{
// internal buffer is too small
return 0;
}
strncpy(temp_buf, yytext, yyleng);
temp_buf[yyleng] = 0;
char* p = temp_buf;
// find IP4 address
char* ip_p = p;
// find netmask
char* net_p = strchr(p, '/');
if(net_p)
{
*net_p = 0;
net_p += 1;
p = net_p;
}
// find port
char* port_p = strchr(p, ':');
if(port_p)
{
*port_p = 0;
port_p += 1;
}
// parse IP4 address
in_addr ip;
if(!inet_aton(ip_p, &ip))
{
return 0;
}
// parse netmask
int net_mask_width;
if(net_p)
{
char* endp = 0;
unsigned long r = strtoul(net_p, &endp, 10);
if(*endp != '\0' || r > 32)
{
return 0;
}
net_mask_width = r;
}
// parse port
unsigned short port;
if(port_p)
{
char* endp = 0;
unsigned long r = strtoul(port_p, &endp, 10);
if(*endp != '\0' || r > 65535)
{
return 0;
}
port = htons(r);
}
// choose token type
if(net_p)
{
yylval->naf.ip = ip;
yylval->naf.net_mask_width = net_mask_width;
yylval->naf.port = port_p ? port : 0;
return NET_ADDR_FILTER;
}
if(port_p)
{
yylval->na.ip = ip;
yylval->na.port = port;
return NET_ADDR;
}
yylval->in = ip;
return IP_ADDR;
}
:{int_value} {
char t[128];
if(yyleng > sizeof(t))
{
// internal buffer is too small
return 0;
}
memcpy(t, yytext, yyleng);
t[yyleng] = 0;
char* endp = 0;
unsigned long r = strtoul(t+1, &endp, 10);
if(*endp == '\0' && r <= 65535)
{
yylval->p = htons(r);
return PORT;
}
else
{
// overflow
return 0;
}
}
\" {
yylval->s = NULL;
yyextra.s.clear();
BEGIN(STR);
}
<STR>\\\\ yyextra.s += '\\';
<STR>\\\" yyextra.s += '"';
<STR>\\t yyextra.s += '\t';
<STR>\\n yyextra.s += '\n';
<STR>\" {
yylval->s = strdup(yyextra.s.c_str());
BEGIN(INITIAL);
return STRING;
}
<STR>\n {
BEGIN(INITIAL);
return 0;
}
<STR>. yyextra.s += *yytext;
dynamic {
yylval->ct = DYNAMIC_TYPE;
return CHAIN_TYPE;
}
strict {
yylval->ct = STRICT_TYPE;
return CHAIN_TYPE;
}
random {
yylval->ct = RANDOM_TYPE;
return CHAIN_TYPE;
}
http {
yylval->pt = HTTP_TYPE;
return PROXY_TYPE;
}
socks4 {
yylval->pt = SOCKS4_TYPE;
return PROXY_TYPE;
}
socks5 {
yylval->pt = SOCKS5_TYPE;
return PROXY_TYPE;
}
skip {
yylval->fa = FILTER_SKIP;
return FILTER_ACTION;
}
accept {
yylval->fa = FILTER_ACCEPT;
return FILTER_ACTION;
}
refuse {
yylval->fa = FILTER_REFUSE;
return FILTER_ACTION;
}
[ \t] /* skip whitespaces */
\n {
yyextra.line += 1;
yyextra.column = 1;
}
. return *yytext;