- changed sip_msg (new rcv member containing all the ips, ports, protocol)
[sip-router] / cfg.lex
1 /*
2  * $Id$
3  *
4  * scanner for cfg files
5  */
6
7
8 %{
9         #include "cfg.tab.h"
10         #include "dprint.h"
11         #include "globals.h"
12         #include <string.h>
13         #include <stdlib.h>
14
15 #ifdef DEBUG_DMALLOC
16 #include <dmalloc.h>
17 #endif
18
19         /* states */
20         #define INITIAL_S               0
21         #define COMMENT_S               1
22         #define COMMENT_LN_S    2
23         #define STRING_S                3
24
25         
26         static int comment_nest=0;
27         static int state=0;
28         static char* tstr=0;
29         int line=1;
30         int column=1;
31         int startcolumn=1;
32
33         static char* addstr(char*, char**);
34         static void count();
35
36
37 %}
38
39 /* start conditions */
40 %x STRING1 STRING2 COMMENT COMMENT_LN
41
42 /* action keywords */
43 FORWARD forward
44 FORWARD_TCP     forward_tcp
45 FORWARD_UDP     forward_udp
46 DROP    "drop"|"break"
47 SEND    send
48 SEND_TCP        send_tcp
49 LOG             log
50 ERROR   error
51 ROUTE   route
52 REPLY_ROUTE reply_route
53 EXEC    exec
54 SETFLAG         setflag
55 RESETFLAG       resetflag
56 ISFLAGSET       isflagset
57 LEN_GT          len_gt
58 SET_HOST                "rewritehost"|"sethost"|"seth"
59 SET_HOSTPORT    "rewritehostport"|"sethostport"|"sethp"
60 SET_USER                "rewriteuser"|"setuser"|"setu"
61 SET_USERPASS    "rewriteuserpass"|"setuserpass"|"setup"
62 SET_PORT                "rewriteport"|"setport"|"setp"
63 SET_URI                 "rewriteuri"|"seturi"
64 REVERT_URI              "revert_uri"
65 PREFIX                  "prefix"
66 STRIP                   "strip"
67 APPEND_BRANCH   "append_branch"
68 IF                              "if"
69 ELSE                    "else"
70
71 /*ACTION LVALUES*/
72 URIHOST                 "uri:host"
73 URIPORT                 "uri:port"
74
75 MAX_LEN                 "max_len"
76
77
78 /* condition keywords */
79 METHOD  method
80 URI             uri
81 SRCIP   src_ip
82 DSTIP   dst_ip
83 MYSELF  myself
84 /* operators */
85 EQUAL   =
86 EQUAL_T ==
87 MATCH   =~
88 NOT             !|"not"
89 AND             "and"|"&&"|"&"
90 OR              "or"|"||"|"|"
91
92 /* config vars. */
93 DEBUG   debug
94 FORK    fork
95 LOGSTDERROR     log_stderror
96 LISTEN          listen
97 ALIAS           alias
98 DNS              dns
99 REV_DNS  rev_dns
100 PORT    port
101 STAT    statistics
102 MAXBUFFER maxbuffer
103 CHILDREN children
104 CHECK_VIA       check_via
105 SYN_BRANCH syn_branch
106 MEMLOG  memlog
107 SIP_WARNING sip_warning
108 FIFO fifo
109 FIFO_MODE fifo_mode
110 SERVER_SIGNATURE server_signature
111 REPLY_TO_VIA reply_to_via
112 USER            "user"|"uid"
113 GROUP           "group"|"gid"
114
115 LOADMODULE      loadmodule
116 MODPARAM        modparam
117
118 /* values */
119 YES                     "yes"|"true"|"on"|"enable"
120 NO                      "no"|"false"|"off"|"disable"
121
122 LETTER          [a-zA-Z]
123 DIGIT           [0-9]
124 ALPHANUM        {LETTER}|{DIGIT}|[_]
125 NUMBER          {DIGIT}+
126 ID                      {LETTER}{ALPHANUM}*
127 HEX                     [0-9a-fA-F]
128 HEX4            {HEX}{1,4}
129 IPV6ADDR        ({HEX4}":"){7}{HEX4}|({HEX4}":"){1,7}(":"{HEX4}){1,7}|":"(":"{HEX4}){1,7}|({HEX4}":"){1,7}":"|"::"
130 QUOTES          \"
131 TICK            \'
132 SLASH           "/"
133 SEMICOLON       ;
134 RPAREN          \)
135 LPAREN          \(
136 LBRACE          \{
137 RBRACE          \}
138 LBRACK          \[
139 RBRACK          \]
140 COMMA           ","
141 DOT                     \.
142 CR                      \n
143
144
145
146 COM_LINE        #
147 COM_START       "/\*"
148 COM_END         "\*/"
149
150 EAT_ABLE        [\ \t\b\r]
151
152 %%
153
154
155 <INITIAL>{EAT_ABLE}     { count(); }
156
157 <INITIAL>{FORWARD}      {count(); yylval.strval=yytext; return FORWARD; }
158 <INITIAL>{FORWARD_TCP}  {count(); yylval.strval=yytext; return FORWARD_TCP; }
159 <INITIAL>{FORWARD_UDP}  {count(); yylval.strval=yytext; return FORWARD_UDP; }
160 <INITIAL>{DROP} { count(); yylval.strval=yytext; return DROP; }
161 <INITIAL>{SEND} { count(); yylval.strval=yytext; return SEND; }
162 <INITIAL>{SEND_TCP}     { count(); yylval.strval=yytext; return SEND_TCP; }
163 <INITIAL>{LOG}  { count(); yylval.strval=yytext; return LOG_TOK; }
164 <INITIAL>{ERROR}        { count(); yylval.strval=yytext; return ERROR; }
165 <INITIAL>{SETFLAG}      { count(); yylval.strval=yytext; return SETFLAG; }
166 <INITIAL>{RESETFLAG}    { count(); yylval.strval=yytext; return RESETFLAG; }
167 <INITIAL>{ISFLAGSET}    { count(); yylval.strval=yytext; return ISFLAGSET; }
168 <INITIAL>{LEN_GT}       { count(); yylval.strval=yytext; return LEN_GT; }
169 <INITIAL>{ROUTE}        { count(); yylval.strval=yytext; return ROUTE; }
170 <INITIAL>{REPLY_ROUTE}  { count(); yylval.strval=yytext; return REPLY_ROUTE; }
171 <INITIAL>{EXEC} { count(); yylval.strval=yytext; return EXEC; }
172 <INITIAL>{SET_HOST}     { count(); yylval.strval=yytext; return SET_HOST; }
173 <INITIAL>{SET_HOSTPORT} { count(); yylval.strval=yytext; return SET_HOSTPORT; }
174 <INITIAL>{SET_USER}     { count(); yylval.strval=yytext; return SET_USER; }
175 <INITIAL>{SET_USERPASS} { count(); yylval.strval=yytext; return SET_USERPASS; }
176 <INITIAL>{SET_PORT}     { count(); yylval.strval=yytext; return SET_PORT; }
177 <INITIAL>{SET_URI}      { count(); yylval.strval=yytext; return SET_URI; }
178 <INITIAL>{REVERT_URI}   { count(); yylval.strval=yytext; return REVERT_URI; }
179 <INITIAL>{PREFIX}       { count(); yylval.strval=yytext; return PREFIX; }
180 <INITIAL>{STRIP}        { count(); yylval.strval=yytext; return STRIP; }
181 <INITIAL>{APPEND_BRANCH}        { count(); yylval.strval=yytext; return APPEND_BRANCH; }
182 <INITIAL>{IF}   { count(); yylval.strval=yytext; return IF; }
183 <INITIAL>{ELSE} { count(); yylval.strval=yytext; return ELSE; }
184
185 <INITIAL>{URIHOST}      { count(); yylval.strval=yytext; return URIHOST; }
186 <INITIAL>{URIPORT}      { count(); yylval.strval=yytext; return URIPORT; }
187
188 <INITIAL>{MAX_LEN}      { count(); yylval.strval=yytext; return MAX_LEN; }
189
190 <INITIAL>{METHOD}       { count(); yylval.strval=yytext; return METHOD; }
191 <INITIAL>{URI}  { count(); yylval.strval=yytext; return URI; }
192 <INITIAL>{SRCIP}        { count(); yylval.strval=yytext; return SRCIP; }
193 <INITIAL>{DSTIP}        { count(); yylval.strval=yytext; return DSTIP; }
194 <INITIAL>{MYSELF}       { count(); yylval.strval=yytext; return MYSELF; }
195
196 <INITIAL>{DEBUG}        { count(); yylval.strval=yytext; return DEBUG; }
197 <INITIAL>{FORK}         { count(); yylval.strval=yytext; return FORK; }
198 <INITIAL>{LOGSTDERROR}  { yylval.strval=yytext; return LOGSTDERROR; }
199 <INITIAL>{LISTEN}       { count(); yylval.strval=yytext; return LISTEN; }
200 <INITIAL>{ALIAS}        { count(); yylval.strval=yytext; return ALIAS; }
201 <INITIAL>{DNS}  { count(); yylval.strval=yytext; return DNS; }
202 <INITIAL>{REV_DNS}      { count(); yylval.strval=yytext; return REV_DNS; }
203 <INITIAL>{PORT} { count(); yylval.strval=yytext; return PORT; }
204 <INITIAL>{STAT} { count(); yylval.strval=yytext; return STAT; }
205 <INITIAL>{MAXBUFFER}    { count(); yylval.strval=yytext; return MAXBUFFER; }
206 <INITIAL>{CHILDREN}     { count(); yylval.strval=yytext; return CHILDREN; }
207 <INITIAL>{CHECK_VIA}    { count(); yylval.strval=yytext; return CHECK_VIA; }
208 <INITIAL>{SYN_BRANCH}   { count(); yylval.strval=yytext; return SYN_BRANCH; }
209 <INITIAL>{MEMLOG}       { count(); yylval.strval=yytext; return MEMLOG; }
210 <INITIAL>{SIP_WARNING}  { count(); yylval.strval=yytext; return SIP_WARNING; }
211 <INITIAL>{USER}         { count(); yylval.strval=yytext; return USER; }
212 <INITIAL>{GROUP}        { count(); yylval.strval=yytext; return GROUP; }
213 <INITIAL>{FIFO} { count(); yylval.strval=yytext; return FIFO; }
214 <INITIAL>{FIFO_MODE}    { count(); yylval.strval=yytext; return FIFO_MODE; }
215 <INITIAL>{SERVER_SIGNATURE}     { count(); yylval.strval=yytext; return SERVER_SIGNATURE; }
216 <INITIAL>{REPLY_TO_VIA} { count(); yylval.strval=yytext; return REPLY_TO_VIA; }
217 <INITIAL>{LOADMODULE}   { count(); yylval.strval=yytext; return LOADMODULE; }
218 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
219
220 <INITIAL>{EQUAL}        { count(); return EQUAL; }
221 <INITIAL>{EQUAL_T}      { count(); return EQUAL_T; }
222 <INITIAL>{MATCH}        { count(); return MATCH; }
223 <INITIAL>{NOT}          { count(); return NOT; }
224 <INITIAL>{AND}          { count(); return AND; }
225 <INITIAL>{OR}           { count(); return OR;  }
226
227
228
229 <INITIAL>{IPV6ADDR}             { count(); yylval.strval=yytext; return IPV6ADDR; }
230 <INITIAL>{NUMBER}               { count(); yylval.intval=atoi(yytext);return NUMBER; }
231 <INITIAL>{YES}                  { count(); yylval.intval=1; return NUMBER; }
232 <INITIAL>{NO}                   { count(); yylval.intval=0; return NUMBER; }
233
234 <INITIAL>{COMMA}                { count(); return COMMA; }
235 <INITIAL>{SEMICOLON}    { count(); return SEMICOLON; }
236 <INITIAL>{RPAREN}       { count(); return RPAREN; }
237 <INITIAL>{LPAREN}       { count(); return LPAREN; }
238 <INITIAL>{LBRACE}       { count(); return LBRACE; }
239 <INITIAL>{RBRACE}       { count(); return RBRACE; }
240 <INITIAL>{LBRACK}       { count(); return LBRACK; }
241 <INITIAL>{RBRACK}       { count(); return RBRACK; }
242 <INITIAL>{SLASH}        { count(); return SLASH; }
243 <INITIAL>{DOT}          { count(); return DOT; }
244 <INITIAL>\\{CR}         {count(); } /* eat the escaped CR */
245 <INITIAL>{CR}           { count();/* return CR;*/ }
246
247
248 <INITIAL>{QUOTES} { count(); state=STRING_S; BEGIN(STRING1); }
249 <INITIAL>{TICK} { count(); state=STRING_S; BEGIN(STRING2); }
250
251
252 <STRING1>{QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL); 
253                                                 yytext[yyleng-1]=0; yyleng--;
254                                                 addstr(yytext, &tstr);
255                                                 yylval.strval=tstr; tstr=0;
256                                                 return STRING;
257                                         }
258 <STRING2>{TICK}  { count(); state=INITIAL_S; BEGIN(INITIAL); 
259                                                 yytext[yyleng-1]=0; yyleng--;
260                                                 addstr(yytext, &tstr);
261                                                 yylval.strval=tstr;
262                                                 tstr=0;
263                                                 return STRING;
264                                         }
265 <STRING2>.|{EAT_ABLE}|{CR}      { yymore(); }
266
267 <STRING1>\\n            { count(); yytext[yyleng-2]='\n';yytext[yyleng-1]=0; 
268                                                 yyleng--; addstr(yytext, &tstr); }
269 <STRING1>\\r            { count(); yytext[yyleng-2]='\r';yytext[yyleng-1]=0; 
270                                                 yyleng--; addstr(yytext, &tstr); }
271 <STRING1>\\a            { count(); yytext[yyleng-2]='\a';yytext[yyleng-1]=0; 
272                                                 yyleng--; addstr(yytext, &tstr); }
273 <STRING1>\\t            { count(); yytext[yyleng-2]='\t';yytext[yyleng-1]=0; 
274                                                 yyleng--; addstr(yytext, &tstr); }
275 <STRING1>\\\\           { count(); yytext[yyleng-2]='\\';yytext[yyleng-1]=0; 
276                                                 yyleng--; addstr(yytext, &tstr); } 
277 <STRING1>.|{EAT_ABLE}|{CR}      { yymore(); }
278
279
280 <INITIAL,COMMENT>{COM_START}    { count(); comment_nest++; state=COMMENT_S;
281                                                                                 BEGIN(COMMENT); }
282 <COMMENT>{COM_END}                              { count(); comment_nest--;
283                                                                                 if (comment_nest==0){
284                                                                                         state=INITIAL_S;
285                                                                                         BEGIN(INITIAL);
286                                                                                 }
287                                                                 }
288 <COMMENT>.|{EAT_ABLE}|{CR}                              { count(); };
289
290 <INITIAL>{COM_LINE}.*{CR}       { count(); } 
291
292 <INITIAL>{ID}                   { count(); addstr(yytext, &tstr);
293                                                   yylval.strval=tstr; tstr=0; return ID; }
294
295
296 <<EOF>>                                                 {
297                                                                         switch(state){
298                                                                                 case STRING_S: 
299                                                                                         LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF in"
300                                                                                                                 " unclosed string\n");
301                                                                                         if (tstr) {free(tstr); tstr=0;}
302                                                                                         break;
303                                                                                 case COMMENT_S:
304                                                                                         LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF:"
305                                                                                                                 " %d comments open\n", comment_nest);
306                                                                                         break;
307                                                                                 case COMMENT_LN_S:
308                                                                                         LOG(L_CRIT, "ERROR: unexpected EOF:"
309                                                                                                                 "comment line open\n");
310                                                                                         break;
311                                                                         }
312                                                                         return 0;
313                                                                 }
314                         
315 %%
316
317 static char* addstr(char * src, char ** dest)
318 {
319         char *tmp;
320         unsigned len1, len2;
321         
322         if (*dest==0){
323                 *dest=strdup(src);
324         }else{
325                 len1=strlen(*dest);
326                 len2=strlen(src);
327                 tmp=malloc(len1+len2+1);
328                 if (tmp==0) goto error;
329                 memcpy(tmp, *dest, len1);
330                 memcpy(tmp+len1, src, len2);
331                 tmp[len1+len2]=0;
332                 free(*dest);
333                 *dest=tmp;
334         }
335         return *dest;
336 error:
337         LOG(L_CRIT, "ERROR:lex:addstr: memory allocation error\n");
338         return 0;
339 }
340
341
342
343 static void count()
344 {
345         int i;
346         
347         startcolumn=column;
348         for (i=0; i<yyleng;i++){
349                 if (yytext[i]=='\n'){
350                         line++;
351                         column=startcolumn=1;
352                 }else if (yytext[i]=='\t'){
353                         column++;
354                         /*column+=8 -(column%8);*/
355                 }else{
356                         column++;
357                 }
358         }
359 }
360
361