blob: 222cf2e2cd235e8a80ca65f69f2a2760e566b0c6 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* Yacc extension's configuration parser.
37 */
38
39/* For development only : */
40%debug
41%error-verbose
42
43/* The parser receives the configuration file filename as parameter */
44%parse-param {char * conffile}
45
46/* Keep track of location */
47%locations
48%pure-parser
49
50%{
51#include "test_netemul.h"
52#include "test_netemul.tab.h" /* YYLTYPE */
53
54/* Forward declaration */
55int yyparse(char * conffile);
56
57/* Parse the configuration file */
58int tne_conf_handle(char * conffile)
59{
60 extern FILE * test_netemulin;
61 int ret;
62
63 TRACE_ENTRY("%p", conffile);
64
65 TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
66
67 test_netemulin = fopen(conffile, "r");
68 if (test_netemulin == NULL) {
69 ret = errno;
70 fd_log_debug("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret));
71 TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file.");
72 return ret;
73 }
74
75 ret = yyparse(conffile);
76
77 fclose(test_netemulin);
78
79 if (ret != 0) {
80 TRACE_DEBUG (INFO, "Unable to parse the configuration file.");
81 return EINVAL;
82 } else {
83 TRACE_DEBUG(FULL, "[test_netemul] latency: %lu ms (var:%u%%) duplicates: %G probability.", tne_conf.lat_avg, tne_conf.lat_dev, tne_conf.dupl_proba);
84 }
85
86 return 0;
87}
88
89/* The Lex parser prototype */
90int test_netemullex(YYSTYPE *lvalp, YYLTYPE *llocp);
91
92/* Function to report the errors */
93void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
94{
95 TRACE_DEBUG(INFO, "Error in configuration parsing");
96
97 if (ploc->first_line != ploc->last_line)
98 fd_log_debug("%s:%d.%d-%d.%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
99 else if (ploc->first_column != ploc->last_column)
100 fd_log_debug("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
101 else
102 fd_log_debug("%s:%d.%d : %s", conffile, ploc->first_line, ploc->first_column, s);
103}
104
105%}
106
107/* Values returned by lex for token */
108%union {
109 unsigned long ulong;
110 float decimal;
111}
112
113/* In case of error in the lexical analysis */
114%token LEX_ERROR
115
116%token <ulong> ULONG
117%token <decimal> FLOAT
118
119/* Tokens */
120%token LATENCY_AVERAGE
121%token LATENCY_DEVIATION
122%token DUPL_PROBA
123%token UNIT_SEC
124%token UNIT_MSEC
125
126
127/* -------------------------------------- */
128%%
129
130 /* The grammar definition */
131conffile: /* empty */
132 | conffile latency_average
133 | conffile latency_deviation
134 | conffile dupl_proba
135 ;
136
137 /* a server entry */
138latency_average: LATENCY_AVERAGE '=' ULONG UNIT_SEC ';'
139 {
140 tne_conf.lat_avg = $3 * 1000;
141 }
142 |
143 LATENCY_AVERAGE '=' ULONG UNIT_MSEC ';'
144 {
145 tne_conf.lat_avg = $3;
146 }
147 ;
148
149latency_deviation: LATENCY_DEVIATION '=' ULONG '%' ';'
150 {
151 tne_conf.lat_dev = (int)$3;
152 if ((tne_conf.lat_dev < 0) || (tne_conf.lat_dev > 100)) {
153 yyerror (&yylloc, conffile, "Latency_Deviation must be comprised between 0 and 100.");
154 YYERROR;
155 }
156 }
157 ;
158
159dupl_proba: DUPL_PROBA '=' ULONG ';'
160 {
161 tne_conf.dupl_proba = (float) $3;
162 }
163 | DUPL_PROBA '=' FLOAT ';'
164 {
165 tne_conf.dupl_proba = $3;
166 }
167 | DUPL_PROBA '=' ULONG '/' ULONG ';'
168 {
169 tne_conf.dupl_proba = ((float)$3) / ((float)$5) ;
170 }
171 ;