-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_gram.y
401 lines (378 loc) · 13.6 KB
/
config_gram.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
392
393
394
395
396
397
398
399
400
401
%{
/* ======================================================================
* Copyright (c) 2002 Theo Schlossnagle
* All rights reserved.
* The following code was written by Theo Schlossnagle <[email protected]>
* Please refer to the LICENSE file before using this software.
* ======================================================================
*/
#include "config.h"
#include "userloader.h"
#include "config_help.h"
#include "alarm.h"
#ifdef USE_EMBEDDED_PERL
#include "perl.h"
#endif
extern int semantic_errors;
int line_num;
extern int buffsize;
extern char *wacktext;
WACKSTYPE wacklval;
int wacklex();
char Spread_name[80];
int Spread_retry_interval;
char Spread_group[MAX_GROUP_NAME];
char control_socket[MAXPATHLEN] = "";
char default_library_path[MAXPATHLEN] = ".";
sp_time Maturity_timeout = { 5, 0 };
sp_time Balance_timer = { 4, 0 };
sp_time ArpRefresh_timer = { 60, 0 };
int Balance_rate = 1;
int Complete_balance = 0;
int Num_pseudo = 0;
int Num_prefer = 0;
int Num_notifications = 0;
address Prefer_address[MAX_PSEUDO];
entry Allocation_table[MAX_PSEUDO];
entry Old_table[MAX_PSEUDO];
struct notification Notification_table[MAX_NOTIF];
entry *tmpe = Allocation_table;
int iplistn=0;
static void add_prefer(int);
static void lookup_name(char *, struct in_addr *);
static void wackstype_to_interface(YYSTYPE *, struct interface *);
#define SPIT while(0) printf
%}
%start Config
%token W_SPREAD W_SPREADRETRYINTERVAL W_LOG W_GROUP W_VIFS W_IPADDR W_PREFER
%token W_NOTIFY W_NONE W_ARPCACHE W_PING W_AQPR W_ALL W_THROTTLE W_OPENBRACE
%token W_CLOSEBRACE W_EQUALS W_STRING W_MATURE W_TIMEINTERVAL W_BALANCE
%token W_NUMBER W_INTERVAL W_CONTROL W_RUNDYNAMIC W_ACTION_TYPE W_MODULEDIR
%token W_PERLUSELIB W_PERLUSE
%%
Config : Settings
{ int i, n;
SPIT("Main config:\n");
for(i=0;i<Num_pseudo;i++) {
entry *ve = &Allocation_table[i];
SPIT("\tVIF:\t[%s] %s ",
ve->pseudo_if.ifname,
inet_ntoa(ve->pseudo_if.ipaddr));
SPIT("netmask %s ",
inet_ntoa(ve->pseudo_if.netmask));
SPIT("broadcast %s\n",
inet_ntoa(ve->pseudo_if.bcast));
SPIT("living in a %s\n",
inet_ntoa(ve->pseudo_if.network));
for(n=0;
n<MAX_PSEUDO && ve->extra_ifs[n].ipaddr.s_addr;
n++) {
SPIT("\t\tVE:\t[%s] %s ",
ve->extra_ifs[n].ifname,
inet_ntoa(ve->extra_ifs[n].ipaddr));
SPIT("netmask %s ",
inet_ntoa(ve->extra_ifs[n].netmask));
SPIT("broadcast %s\n",
inet_ntoa(ve->extra_ifs[n].bcast));
SPIT("living in a %s\n",
inet_ntoa(ve->extra_ifs[n].network));
}
}
}
Settings : Setting Settings
|
;
Setting : W_SPREAD W_EQUALS W_STRING
{ snprintf(Spread_name, 80, "%s", $3.string);
SPIT("Setting Spread: %s\n", Spread_name); }
| W_SPREAD W_EQUALS W_NUMBER
{ snprintf(Spread_name, 80, "%d", $3.number);
SPIT("Setting Spread: %s\n", Spread_name); }
| W_SPREADRETRYINTERVAL W_EQUALS W_TIMEINTERVAL
{ Spread_retry_interval = $3.tv.tv_sec;
SPIT("Setting SpreadRetryInteral: %d seconds\n", Spread_retry_interval); }
| W_GROUP W_EQUALS W_STRING
{ snprintf(Spread_group, MAX_GROUP_NAME, "%s", $3.string);
SPIT("Setting Group: %s\n", Spread_group); }
| W_LOG W_EQUALS W_STRING
| W_CONTROL W_EQUALS W_STRING
{ snprintf(control_socket, MAXPATHLEN, $3.string); }
| W_MATURE W_EQUALS W_TIMEINTERVAL
{ Maturity_timeout.sec = $3.tv.tv_sec;
Maturity_timeout.usec = 0;
SPIT("Setting Maturity Interval: %ld sec\n",
(long int)Maturity_timeout.sec);
}
| W_ARPCACHE W_EQUALS W_TIMEINTERVAL
{ ArpRefresh_timer.sec = $3.tv.tv_sec;
ArpRefresh_timer.usec = 0;
SPIT("Setting Maturity Interval: %ld sec\n",
(long int)ArpRefresh_timer.sec);
}
| W_BALANCE W_OPENBRACE BParams W_CLOSEBRACE
| W_NOTIFY W_ARPCACHE
{ struct notification *ni = &Notification_table[Num_notifications++];
strcpy(ni->destination.ifname, "arp-cache");
SPIT("Adding notification %d: arp-cache\n", Num_notifications);
}
| W_NOTIFY ClosedNOTIFYList
| W_PREFER W_NONE
{ SPIT("Prefer NONE\n");
add_prefer(0);
}
| W_PREFER W_STRING
{ struct in_addr ipaddr;
lookup_name($1.string, &ipaddr);
if(ipaddr.s_addr == 0) {
wack_alarm(PRINT, "Couldn't lookup %s.\n", $1.string);
} else {
add_prefer(ipaddr.s_addr);
SPIT("Prefer (%s) %s\n",
$1.string, inet_ntoa(ipaddr));
}
}
| W_PREFER W_IPADDR
{ unsigned int mask;
address Addr;
mask = htonl((0xffffffff << (32-$2.ip.mask)));
$2.ip.addr.s_addr &= mask;
Addr = $2.ip.addr.s_addr;
while((Addr & mask) == ($2.ip.addr.s_addr & mask)) {
add_prefer($2.ip.addr.s_addr);
SPIT("Prefer %s\n", inet_ntoa($2.ip.addr));
$2.ip.addr.s_addr++;
}
}
| W_MODULEDIR W_EQUALS W_STRING
{ strncpy(default_library_path, $3.string, MAXPATHLEN); }
| W_PERLUSE W_STRING
{
#ifdef USE_EMBEDDED_PERL
perl_use($2.string);
#else
wack_alarm(EXIT, "PerlUseLib used, without embedded perl interpreter");
#endif
}
| W_PERLUSELIB W_STRING
{
#ifdef USE_EMBEDDED_PERL
perl_inc($2.string);
#else
wack_alarm(EXIT, "PerlUseLib used, without embedded perl interpreter");
#endif
}
| W_RUNDYNAMIC W_STRING W_ACTION_TYPE
{ char *path = $2.string;
if(strstr(path, "::") || !strchr(path, ':')) {
#ifdef USE_EMBEDDED_PERL
register_perl(path, $3.number);
#else
wack_alarm(EXIT, "RunDynamic with perl-style parameter, without embedded perl interpreter");
#endif
} else {
char *func = strchr(path, ':');
if(func) { *(func++) = '\0'; }
register_shared(path, func, $3.number);
}
}
| W_PREFER ClosedPIPList
| W_VIFS ClosedVIFList
BParams : BParam BParams
| BParam
BParam : W_AQPR W_EQUALS W_NUMBER
{ Balance_rate = $3.number;
SPIT("Setting Balance Rate: %d\n", Balance_rate); }
| W_AQPR W_EQUALS W_ALL
{ Complete_balance = 1;
SPIT("Setting Immediate Balance\n"); }
| W_INTERVAL W_EQUALS W_TIMEINTERVAL
{ Balance_timer.sec = $3.tv.tv_sec;
Balance_timer.usec = 0;
SPIT("Setting Balance Interval: %ld sec\n",
(long int)Balance_timer.sec); }
ClosedNOTIFYList : W_NONE
| W_OPENBRACE NIPList W_CLOSEBRACE
NIPList : NIPList W_IPADDR W_THROTTLE W_NUMBER
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($2), &(ni->destination));
ni->throttle = $4.number;
ni->ping = 0;
SPIT("Adding notification %d: %s:%s:%s (%d/sec)\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask), ni->throttle);
}
| NIPList W_IPADDR W_THROTTLE W_NUMBER W_PING
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($2), &(ni->destination));
ni->throttle = $4.number;
ni->ping = 1;
SPIT("Adding notification %d: %s:%s:%s (%d/sec) with ping\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask), ni->throttle);
}
| NIPList W_IPADDR
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($2), &(ni->destination));
ni->throttle = 0;
ni->ping = 0;
SPIT("Adding notification %d: %s:%s:%s\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask));
}
| NIPList W_IPADDR W_PING
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($2), &(ni->destination));
ni->throttle = 0;
ni->ping = 1;
SPIT("Adding notification %d: %s:%s:%s with ping\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask));
}
| NIPList W_ARPCACHE
{ struct notification *ni = &Notification_table[Num_notifications++];
strcpy(ni->destination.ifname, "arp-cache");
ni->throttle = 0;
ni->ping = 0;
SPIT("Adding notification %d: arp-cache\n", Num_notifications);
}
| NIPList W_ARPCACHE W_PING
{ struct notification *ni = &Notification_table[Num_notifications++];
strcpy(ni->destination.ifname, "arp-cache");
ni->throttle = 0;
ni->ping = 1;
SPIT("Adding notification %d: arp-cache with ping\n", Num_notifications);
}
| W_IPADDR W_THROTTLE W_NUMBER
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($1), &(ni->destination));
ni->throttle = $3.number;
ni->ping = 0;
SPIT("Adding notification %d: %s:%s:%s (%d/sec)\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask), ni->throttle);
}
| W_IPADDR W_THROTTLE W_NUMBER W_PING
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($1), &(ni->destination));
ni->throttle = $3.number;
ni->ping = 1;
SPIT("Adding notification %d: %s:%s:%s (%d/sec) with ping\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask), ni->throttle);
}
| W_IPADDR
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($1), &(ni->destination));
ni->throttle = 0;
ni->ping = 0;
SPIT("Adding notification %d: %s:%s:%s\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask));
}
| W_IPADDR W_PING
{ struct notification *ni = &Notification_table[Num_notifications++];
wackstype_to_interface(&($1), &(ni->destination));
ni->throttle = 0;
ni->ping = 1;
SPIT("Adding notification %d: %s:%s:%s with ping\n", Num_notifications, ni->destination.ifname, inet_ntoa(ni->destination.ipaddr), inet_ntoa(ni->destination.netmask));
}
| W_ARPCACHE
{ struct notification *ni = &Notification_table[Num_notifications++];
strcpy(ni->destination.ifname, "arp-cache");
ni->throttle = 0;
ni->ping = 0;
SPIT("Adding notification %d: arp-cache\n", Num_notifications);
}
| W_ARPCACHE W_PING
{ struct notification *ni = &Notification_table[Num_notifications++];
strcpy(ni->destination.ifname, "arp-cache");
ni->throttle = 0;
ni->ping = 1;
SPIT("Adding notification %d: arp-cache with ping\n", Num_notifications);
}
ClosedPIPList : W_OPENBRACE PIPList W_CLOSEBRACE
PIPList : PIPList W_IPADDR
{ unsigned int mask;
address Addr;
mask = (0xffffffff << (32-$2.ip.mask));
$2.ip.addr.s_addr &= mask;
Addr = $2.ip.addr.s_addr;
while((Addr & mask) == ($2.ip.addr.s_addr & mask)) {
add_prefer($2.ip.addr.s_addr);
SPIT("Prefer %s\n", inet_ntoa($2.ip.addr));
$2.ip.addr.s_addr++;
}
}
| W_IPADDR
{ unsigned int mask;
address Addr;
mask = (0xffffffff << (32-$1.ip.mask));
$1.ip.addr.s_addr &= mask;
Addr = $1.ip.addr.s_addr;
while((Addr & mask) == ($1.ip.addr.s_addr & mask)) {
add_prefer($1.ip.addr.s_addr);
SPIT("Prefer %s\n", inet_ntoa($1.ip.addr));
$1.ip.addr.s_addr++;
}
}
ClosedIPList : W_OPENBRACE IPList W_CLOSEBRACE
{ if(iplistn<MAX_PSEUDO) {
tmpe[Num_pseudo].extra_ifs[iplistn].ipaddr.s_addr=0;
}
iplistn=0;
}
IPList : IPList W_IPADDR
{ unsigned int mask;
mask = htonl(0xffffffff << (32-$2.ip.mask));
memcpy(&tmpe[Num_pseudo].extra_ifs[iplistn].ifname,
$2.ip.iface, IFNAMSIZ);
tmpe[Num_pseudo].extra_ifs[iplistn].ipaddr = $2.ip.addr;
tmpe[Num_pseudo].extra_ifs[iplistn].netmask.s_addr = mask;
tmpe[Num_pseudo].extra_ifs[iplistn].bcast.s_addr =
(($2.ip.addr.s_addr & mask) | (~mask));
tmpe[Num_pseudo].extra_ifs[iplistn].network.s_addr =
$2.ip.network.s_addr;
iplistn++;
}
| W_IPADDR
{ unsigned int mask;
mask = htonl(0xffffffff << (32-$1.ip.mask));
memcpy(&tmpe[Num_pseudo].pseudo_if.ifname,
$1.ip.iface, IFNAMSIZ);
tmpe[Num_pseudo].pseudo_if.ipaddr = $1.ip.addr;
tmpe[Num_pseudo].pseudo_if.netmask.s_addr = mask;
tmpe[Num_pseudo].pseudo_if.bcast.s_addr =
(($1.ip.addr.s_addr & mask) | (~mask));
tmpe[Num_pseudo].pseudo_if.network.s_addr =
$1.ip.network.s_addr;
}
ClosedVIFList : W_OPENBRACE VIFList W_CLOSEBRACE
VIFList : VIFList VIF
{ Num_pseudo++; }
| VIF
{ Num_pseudo++; }
VIF : W_IPADDR
{ unsigned int mask;
mask = htonl(0xffffffff << (32-$1.ip.mask));
memcpy(&tmpe[Num_pseudo].pseudo_if.ifname,
$1.ip.iface, IFNAMSIZ);
tmpe[Num_pseudo].pseudo_if.ipaddr = $1.ip.addr;
tmpe[Num_pseudo].pseudo_if.netmask.s_addr = mask;
tmpe[Num_pseudo].pseudo_if.bcast.s_addr =
(($1.ip.addr.s_addr & mask) | (~mask));
tmpe[Num_pseudo].pseudo_if.network.s_addr =
$1.ip.network.s_addr;
}
| ClosedIPList
%%
int wackerror(char *str) {
fprintf(stderr, "Parser error on or before line %d\n", line_num);
fprintf(stderr, "Offending token: %s\n", wacktext);
return -1;
}
static void add_prefer(int addr) {
if(Num_prefer >= MAX_PSEUDO) {
wack_alarm(PRINT, "Only %d prefered allowed.\n", MAX_PSEUDO);
return;
}
Prefer_address[Num_prefer++] = addr;
}
static void lookup_name(char *name, struct in_addr *ipaddr) {
}
static void wackstype_to_interface(YYSTYPE *in, struct interface *out) {
unsigned int mask;
mask = htonl((0xffffffff << (32-in->ip.mask)));
memcpy(out->ifname,
in->ip.iface, IFNAMSIZ);
out->ipaddr = in->ip.addr;
out->netmask.s_addr = mask;
out->bcast.s_addr =
((in->ip.addr.s_addr & mask) | (~mask));
out->network = in->ip.network;
}