-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cfparse.y
391 lines (332 loc) · 7.57 KB
/
cfparse.y
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
%{
/*
* Configuration file parser for pimd-dense, based on the original from
* mrouted, by Bill Fenner (1994)
*/
#include <stdio.h>
#include <stdarg.h>
#include <netdb.h>
#include <ifaddrs.h>
#include "defs.h"
/*
* Local function declarations
*/
static void fatal(const char *fmt, ...);
static void warn(const char *fmt, ...);
static void yyerror(char *s);
static char *next_word(void);
static int yylex(void);
int yyparse(void);
static FILE *fp;
static int lineno;
static struct uvif *v;
static struct uvif scrap;
struct addrmask {
uint32_t addr;
int mask;
};
%}
%union
{
int num;
char *ptr;
struct addrmask addrmask;
uint32_t addr;
};
%token ASSERT_TIMEOUT
%token DEFAULT_ROUTE_PREF DEFAULT_ROUTE_METRIC
%token NO
%token PHYINT
%token DISABLE ENABLE IGMPV1 IGMPV2 IGMPV3
%token DISTANCE METRIC THRESHOLD NETMASK
%token <num> BOOLEAN
%token <num> NUMBER
%token <ptr> STRING
%token <addrmask> ADDRMASK
%token <addr> ADDR
%type <addr> interface addrname
%start conf
%%
conf : stmts
;
stmts : /* Empty */
| stmts stmt
;
stmt : error
| ASSERT_TIMEOUT NUMBER
{
if ($2 < 5 || $2 > 210)
fatal("Invalid PIM assert-timeout (5-210)");
assert_timeout = $2;
}
| DEFAULT_ROUTE_PREF NUMBER
{
if ($2 < 1 || $2 > 255)
fatal("Invalid default-route-distance (1-255)");
default_route_distance = $2;
}
| DEFAULT_ROUTE_METRIC NUMBER
{
if ($2 < 1 || $2 > 1024)
fatal("Invalid default-route-metric (1-1024)");
default_route_metric = $2;
}
| NO PHYINT { config_set_ifflag(VIFF_DISABLED); }
| PHYINT interface
{
v = config_find_ifaddr($2);
if (!v) {
warn("phyint %s not available, continuing ...", inet_fmt($2, s1));
v = &scrap;
}
}
ifmods
;
ifmods : /* empty */
| ifmods ifmod
;
ifmod : mod
| DISABLE { v->uv_flags |= VIFF_DISABLED; }
| ENABLE { v->uv_flags &= ~VIFF_DISABLED; }
| IGMPV1 { v->uv_flags &= ~VIFF_IGMPV2; v->uv_flags |= VIFF_IGMPV1; }
| IGMPV2 { v->uv_flags &= ~VIFF_IGMPV1; v->uv_flags |= VIFF_IGMPV2; }
| IGMPV3 { v->uv_flags &= ~VIFF_IGMPV1; v->uv_flags &= ~VIFF_IGMPV2; }
| NETMASK addrname
{
uint32_t subnet, mask;
mask = $2;
subnet = v->uv_lcl_addr & mask;
if (!inet_valid_subnet(subnet, mask))
fatal("Invalid netmask");
v->uv_subnet = subnet;
v->uv_subnetmask = mask;
v->uv_subnetbcast = subnet | ~mask;
}
| NETMASK
{
warn("Expected address after netmask keyword, ignored");
}
;
mod : THRESHOLD NUMBER
{
if ($2 < 1 || $2 > 255)
fatal("Invalid threshold %d",$2);
v->uv_threshold = $2;
}
| THRESHOLD
{
warn("Expected number after threshold keyword, ignored");
}
| DISTANCE NUMBER
{
if ($2 < 1 || $2 > 255)
fatal("Invalid distance %d",$2);
v->uv_local_pref = $2;
}
| DISTANCE
{
warn("Expected number after distance keyword, ignored");
}
| METRIC NUMBER
{
if ($2 < 1 || $2 > 1024)
fatal("Invalid metric %d",$2);
v->uv_metric = $2;
}
| METRIC
{
warn("Expected number after metric keyword, ignored");
}
;
interface: ADDR
{
$$ = $1;
}
| STRING
{
struct uvif *v;
/*
* Looks a little weird, but the orig. code was based around
* the addresses being used to identify interfaces.
*/
v = config_find_ifname($1);
if (!v) {
warn("phyint %s not available, continuing ...", $1);
$$ = 0;
} else
$$ = v->uv_lcl_addr;
}
;
addrname: ADDR
{
$$ = $1;
}
| STRING
{
struct sockaddr_in *sin;
struct addrinfo *result;
struct addrinfo hints;
int rc;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
rc = getaddrinfo($1, NULL, &hints, &result);
if (rc) {
fatal("No such host %s", $1);
return 0; /* Never reached */
}
if (result->ai_next)
fatal("Hostname %s does not %s", $1, "map to a unique address");
sin = (struct sockaddr_in *)result->ai_addr;
$$ = sin->sin_addr.s_addr;
freeaddrinfo(result);
}
;
%%
static void fatal(const char *fmt, ...)
{
va_list ap;
char buf[MAXHOSTNAMELEN + 100];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
logit(LOG_ERR, 0, "%s:%d: %s", config_file, lineno, buf);
}
static void warn(const char *fmt, ...)
{
va_list ap;
char buf[200];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
logit(LOG_WARNING, 0, "%s:%d: %s", config_file, lineno, buf);
}
static void yyerror(char *msg)
{
logit(LOG_ERR, 0, "%s:%d: %s", config_file, lineno, msg);
}
static char *next_word(void)
{
static char buf[1024];
static char *p = NULL;
char *q;
while (1) {
if (!p || !*p) {
lineno++;
if (fgets(buf, sizeof(buf), fp) == NULL)
return NULL;
p = buf;
}
/* skip whitespace */
while (*p && (*p == ' ' || *p == '\t'))
p++;
if (*p == '#') {
p = NULL; /* skip comments */
continue;
}
q = p;
while (*p && *p != ' ' && *p != '\t' && *p != '\n')
p++; /* find next whitespace */
*p++ = '\0'; /* null-terminate string */
if (!*q) {
p = NULL;
continue; /* if 0-length string, read another line */
}
return q;
}
}
/*
* List of keywords. Must have an empty record at the end to terminate
* list. If a second value is specified, the first is used at the beginning
* of the file and the second is used while parsing interfaces (e.g. after
* the first "phyint" or "tunnel" keyword).
*/
static struct keyword {
char *word;
int val;
} words[] = {
{ "assert-timeout", ASSERT_TIMEOUT },
{ "default-route-distance", DEFAULT_ROUTE_PREF },
{ "default-route-metric", DEFAULT_ROUTE_METRIC },
{ "no", NO },
{ "phyint", PHYINT },
{ "disable", DISABLE },
{ "enable", ENABLE },
{ "distance", DISTANCE },
{ "metric", METRIC },
{ "threshold", THRESHOLD },
{ "netmask", NETMASK },
{ "igmpv1", IGMPV1 },
{ "igmpv2", IGMPV2 },
{ "igmpv2", IGMPV3 },
{ NULL, 0 }
};
static int yylex(void)
{
struct keyword *w;
uint32_t addr, n;
char *q;
q = next_word();
if (!q)
return 0;
for (w = words; w->word; w++) {
if (!strcmp(q, w->word))
return w->val;
}
if (!strcmp(q, "on") || !strcmp(q, "yes")) {
yylval.num = 1;
return BOOLEAN;
}
if (!strcmp(q, "off") || !strcmp(q, "no")) {
yylval.num = 0;
return BOOLEAN;
}
if (!strcmp(q, "default")) {
yylval.addrmask.mask = 0;
yylval.addrmask.addr = 0;
return ADDRMASK;
}
if (sscanf(q,"%[.0-9]/%u%c", s1, &n, s2) == 2) {
addr = inet_parse(s1, 1);
if (addr != 0xffffffff) {
yylval.addrmask.mask = n;
yylval.addrmask.addr = addr;
return ADDRMASK;
}
/* fall through to returning STRING */
}
if (sscanf(q,"%[.0-9]%c", s1, s2) == 1) {
addr = inet_parse(s1, 4);
if (addr != 0xffffffff && inet_valid_host(addr)) {
yylval.addr = addr;
return ADDR;
}
}
if (sscanf(q,"0x%8x%c", &n, s1) == 1) {
yylval.addr = n;
return ADDR;
}
if (sscanf(q,"%u%c", &n, s1) == 1) {
yylval.num = n;
return NUMBER;
}
yylval.ptr = q;
return STRING;
}
void config_vifs_from_file(void)
{
lineno = 0;
fp = fopen(config_file, "r");
if (!fp) {
if (errno != ENOENT)
logit(LOG_ERR, errno, "Cannot open %s", config_file);
return;
}
yyparse();
fclose(fp);
}
/**
* Local Variables:
* c-file-style: "cc-mode"
* End:
*/