File: RelParser.y

package info (click to toggle)
dibbler 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,352 kB
  • sloc: cpp: 60,323; ansic: 12,235; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (377 lines) | stat: -rw-r--r-- 8,775 bytes parent folder | download | duplicates (3)
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
%name RelParser

%header{
#include <string.h>
#include <iostream>
#include <string>
#include <malloc.h>
#include "DHCPConst.h"
#include "SmartPtr.h"
#include "Container.h"
#include "RelParser.h"
#include "RelParsGlobalOpt.h"
#include "RelParsIfaceOpt.h"
#include "RelCfgIface.h"
#include "RelCfgMgr.h"
#include "OptVendorData.h"
#include "OptDUID.h"
#include "DUID.h"
#include "Logger.h"
#include "Portable.h"

using namespace std;

#define YY_USE_CLASS
%}

%{
#include "FlexLexer.h"
%}

// class definition
%define MEMBERS FlexLexer * lex;                                                     \
List(TRelParsGlobalOpt) ParserOptStack;    /* list of parsed interfaces/IAs/addrs */ \
List(TRelCfgIface) RelCfgIfaceLst;         /* list of RelCfg interfaces */           \
List(TIPv6Addr) PresentAddrLst;            /* address list (used for DNS,NTP,etc.)*/ \
List(std::string) PresentStringLst;             /* string list */                    \
SPtr<TRelOptEcho> EchoOpt;                 /* echo request option */                 \
/*method check whether interface with id=ifaceNr has been already declared */        \
bool CheckIsIface(int ifaceNr);                                                      \
/*method check whether interface with id=ifaceName has been already declared*/       \
bool CheckIsIface(string ifaceName);                                                 \
void StartIfaceDeclaration();                                                        \
bool EndIfaceDeclaration();                                                          \
TRelCfgMgr* CfgMgr;                                                                  \
virtual ~RelParser();

// constructor
%define CONSTRUCTOR_PARAM yyFlexLexer * lex
%define CONSTRUCTOR_CODE                                                          \
    ParserOptStack.append(new TRelParsGlobalOpt());                               \
    this->lex = lex;                                                              \
    yynerrs = 0;                                                                  \
    yychar = 0;

%union    
{
    unsigned int ival;
    char *strval;
    char addrval[16];
    struct SDuid
    {
        int length;
        char* duid;
    } duidval;
}

%token IFACE_, CLIENT_, SERVER_, UNICAST_, MULTICAST_, IFACE_ID_, IFACE_ID_ORDER_
%token LOGNAME_, LOGLEVEL_, LOGMODE_, WORKDIR_
%token DUID_, OPTION_, REMOTE_ID_, ECHO_REQUEST_, RELAY_ID_, LINK_LAYER_
%token GUESS_MODE_

%token <strval>     STRING_
%token <ival>       HEXNUMBER_
%token <ival>       INTNUMBER_
%token <addrval>    IPV6ADDR_
%type  <ival>       Number
%token <duidval>    DUID_

%%

/////////////////////////////////////////////////////////////////////////////
// rules section
/////////////////////////////////////////////////////////////////////////////

Grammar
: GlobalList
;

GlobalList
: GlobalOptionsList IfaceList
;

GlobalOptionsList
: GlobalOption
| GlobalOptionsList GlobalOption
;

GlobalOption
: LogModeOption
| LogLevelOption
| LogNameOption
| WorkDirOption
| GuessMode
| IfaceIDOrder
| RemoteID
| RelayID
| LinkLayerOption
| EchoRequest
;

IfaceList
: Iface
| IfaceList Iface
;

IfaceOptionList
: IfaceOptions
| IfaceOptionList IfaceOptions
;

IfaceOptions
: ClientUnicastOption
| ServerUnicastOption
| ClientMulticastOption
| ServerMulticast
| IfaceID
;

Iface
:IFACE_ STRING_ '{' 
{
    CheckIsIface(string($2)); //If no - everything is ok
    StartIfaceDeclaration();
}
IfaceOptionList '}'
{
    //Information about new interface has been read
    //Add it to list of read interfaces
    RelCfgIfaceLst.append(new TRelCfgIface($2));
    delete [] $2;
    EndIfaceDeclaration();
}
|IFACE_ Number '{' 
{
    CheckIsIface($2);   //If no - everything is ok
    StartIfaceDeclaration();
}
IfaceOptionList '}'
{
    RelCfgIfaceLst.append(new TRelCfgIface($2));
    EndIfaceDeclaration();
}


/////////////////////////////////////////////////////////////////////////////
// Now Options and their parameters
/////////////////////////////////////////////////////////////////////////////
Number
:  HEXNUMBER_ {$$=$1;}
|  INTNUMBER_ {$$=$1;}
;

ServerUnicastOption
: SERVER_ UNICAST_ IPV6ADDR_
{
    ParserOptStack.getLast()->setServerUnicast(new TIPv6Addr($3));
}
;

ClientUnicastOption
: CLIENT_ UNICAST_ IPV6ADDR_
{
    ParserOptStack.getLast()->setClientUnicast(new TIPv6Addr($3));
}
;

ServerMulticast
: SERVER_ MULTICAST_ Number
{ 
    ParserOptStack.getLast()->setServerMulticast($3);
}
| SERVER_ MULTICAST_
{
    ParserOptStack.getLast()->setServerMulticast(true);
}
;

ClientMulticastOption
:   CLIENT_ MULTICAST_ Number
{ 
    ParserOptStack.getLast()->setClientMulticast($3);
}
| CLIENT_ MULTICAST_
{
    ParserOptStack.getLast()->setClientMulticast(true);
}
;
    
LogLevelOption
: LOGLEVEL_ Number {
    logger::setLogLevel($2);
}
;

LogModeOption
: LOGMODE_ STRING_ {
    logger::setLogMode($2);
}

LogNameOption
: LOGNAME_ STRING_
{
    logger::setLogName($2);
}
;

WorkDirOption
:   WORKDIR_ STRING_
{
    ParserOptStack.getLast()->setWorkDir($2);
}
;

GuessMode
: GUESS_MODE_
{
    ParserOptStack.getLast()->setGuessMode(true);
};

IfaceID
:IFACE_ID_ Number
{
    ParserOptStack.getLast()->setInterfaceID($2);
}
;

RemoteID
:OPTION_ REMOTE_ID_ Number '-' DUID_
{
    Log(Debug) << "RemoteID set: enterprise-number=" << $3 << ", remote-id length=" << $5.length << LogEnd;
    ParserOptStack.getLast()->setRemoteID( new TOptVendorData(OPTION_REMOTE_ID, $3, $5.duid, $5.length, 0));
};

RelayID
:OPTION_ RELAY_ID_ DUID_
{
    Log(Debug) << "Relay-id set: length=" << $3.length << LogEnd;
    CfgMgr->setRelayID(new TOptDUID(OPTION_RELAY_ID, $3.duid, $3.length, NULL));
}

LinkLayerOption
:OPTION_ LINK_LAYER_
{
    Log(Debug) << "Client link-local address option (RFC6939) enabled." << LogEnd;
    CfgMgr->setClientLinkLayerAddress(true);
}

EchoRequest
:OPTION_ ECHO_REQUEST_ 
{
    EchoOpt = new TRelOptEcho(0);
    ParserOptStack.getLast()->setEcho(EchoOpt);
    Log(Debug) << "Echo Request option will be added with opt(s): ";
} OptionIdList
{
    Log(Cont) << ", " << EchoOpt->count() << " opt(s) total." << LogEnd;
};

OptionIdList
: Number
{
    EchoOpt->addOption($1);
    Log(Cont) << " " << $1;
} 
|OptionIdList ',' Number
{
    EchoOpt->addOption($3);
    Log(Cont) << " " << $3;
};

IfaceIDOrder
:IFACE_ID_ORDER_ STRING_
{
    if (!strncasecmp($2,"before",6)) 
    {
	ParserOptStack.getLast()->setInterfaceIDOrder(REL_IFACE_ID_ORDER_BEFORE);
    } else 
    if (!strncasecmp($2,"after",5)) 
    {
	ParserOptStack.getLast()->setInterfaceIDOrder(REL_IFACE_ID_ORDER_AFTER);
    } else
    if (!strncasecmp($2,"omit",4)) 
    {
	ParserOptStack.getLast()->setInterfaceIDOrder(REL_IFACE_ID_ORDER_NONE);
    } else 
    {
	Log(Crit) << "Invalid interface-id-order specified. Allowed values: before, after, none" << LogEnd;
	YYABORT;
    }
};


%%

/////////////////////////////////////////////////////////////////////////////
// programs section

//method check whether interface with id=ifaceNr has been 
//already declared
bool RelParser::CheckIsIface(int ifaceNr)
{
    SPtr<TRelCfgIface> ptr;
    RelCfgIfaceLst.first();
    while (ptr=RelCfgIfaceLst.get()) {
	if ((ptr->getID())==ifaceNr) {
	    Log(Crit) << "Interface with ID=" << ifaceNr << " is already defined." << LogEnd;
	    YYABORT;
	}
    }
    return true;
}
    
//method check whether interface with id=ifaceName has been
//already declared 
bool RelParser::CheckIsIface(string ifaceName)
{
    SPtr<TRelCfgIface> ptr;
    RelCfgIfaceLst.first();
    while (ptr=RelCfgIfaceLst.get())
    {
	string presName=ptr->getName();
	if (presName==ifaceName) {
	    Log(Crit) << "Interface " << ifaceName << " is already defined." << LogEnd;
	    YYABORT;
	}
    }
    return true;
}

//method creates new scope appropriately for interface options and declarations
//clears all lists except the list of interfaces and adds new group
void RelParser::StartIfaceDeclaration()
{
    //Interface scope, so parameters associated with global scope are pushed on stack
    ParserOptStack.append(new TRelParsGlobalOpt(*ParserOptStack.getLast()));
}

bool RelParser::EndIfaceDeclaration()
{
    //setting interface options on the basis of just read information
    RelCfgIfaceLst.getLast()->setOptions(ParserOptStack.getLast());
    ParserOptStack.delLast();
    return true;
}   

namespace std {
    extern yy_RelParser_stype yylval;
}

int RelParser::yylex()
{
    memset(&std::yylval,0, sizeof(std::yylval));
    memset(&this->yylval,0, sizeof(this->yylval));
    int x = this->lex->yylex();
    this->yylval=std::yylval;
    return x;
}

void RelParser::yyerror(char *m)
{
    Log(Crit) << "Config parse error: line " << lex->lineno() 
              << ", unexpected [" << lex->YYText() << "] token." << LogEnd;
}

RelParser::~RelParser() {
    
}