- sock_lists completely re-organized
[sip-router] / cfg.y
1 /*
2  * $Id$
3  *
4  *  cfg grammar
5  *
6  * Copyright (C) 2001-2003 Fhg Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
10  * ser is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version
14  *
15  * For a license to use the ser software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * ser is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License 
26  * along with this program; if not, write to the Free Software 
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29  /*
30  * History:
31  * ---------
32  * 2003-01-29  src_port added (jiri)
33  * 2003-01-23  mhomed added (jiri)
34  * 2003-03-19  replaced all mallocs/frees with pkg_malloc/pkg_free (andrei)
35  * 2003-03-19  Added support for route type in find_export (janakj)
36  * 2003-03-20  Regex support in modparam (janakj)
37  * 2003-04-01  added dst_port, proto , af (andrei)
38  * 2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
39  * 2003-04-12  added force_rport, chroot and wdir (andrei)
40  * 2003-04-15  added tcp_children, disable_tcp (andrei)
41  * 2003-04-22  strip_tail added (jiri)
42  * 2003-07-03  tls* (disable, certificate, private_key, ca_list, verify, 
43  *              require_certificate added (andrei)
44  * 2003-07-06  more tls config. vars added: tls_method, tls_port_no (andrei)
45  * 2003-10-02  added {,set_}advertised_{address,port} (andrei)
46  * 2003-10-10  added <,>,<=,>=, != operators support
47  *             added msg:len (andrei)
48  * 2003-10-11  if(){} doesn't require a ';' after it anymore (andrei)
49  * 2003-10-13  added FIFO_DIR & proto:host:port listen/alias support (andrei)
50  * 2003-10-24  converted to the new socket_info lists (andrei)
51  */
52
53
54 %{
55
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <string.h>
63 #include <errno.h>
64 #include "route_struct.h"
65 #include "globals.h"
66 #include "route.h"
67 #include "dprint.h"
68 #include "sr_module.h"
69 #include "modparam.h"
70 #include "ip_addr.h"
71 #include "socket_info.h"
72 #include "name_alias.h"
73 #include "ut.h"
74
75
76 #include "config.h"
77 #ifdef USE_TLS
78 #include "tls/tls_config.h"
79 #endif
80
81 #ifdef DEBUG_DMALLOC
82 #include <dmalloc.h>
83 #endif
84
85 /* hack to avoid alloca usage in the generated C file (needed for compiler
86  with no built in alloca, like icc*/
87 #undef _ALLOCA_H
88
89 struct id_list{
90         char* name;
91         int proto;
92         int port;
93         struct id_list* next;
94 };
95
96 extern int yylex();
97 void yyerror(char* s);
98 static char* tmp;
99 static int i_tmp;
100 static void* f_tmp;
101 static struct id_list* lst_tmp;
102 static int rt;  /* Type of route block for find_export */
103 static str* str_tmp;
104
105 void warn(char* s);
106 static struct id_list* mk_listen_id(char*, int, int);
107  
108
109 %}
110
111 %union {
112         long intval;
113         unsigned long uval;
114         char* strval;
115         struct expr* expr;
116         struct action* action;
117         struct net* ipnet;
118         struct ip_addr* ipaddr;
119         struct id_list* idlst;
120 }
121
122 /* terminals */
123
124
125 /* keywords */
126 %token FORWARD
127 %token FORWARD_TCP
128 %token FORWARD_TLS
129 %token FORWARD_UDP
130 %token SEND
131 %token SEND_TCP
132 %token DROP
133 %token LOG_TOK
134 %token ERROR
135 %token ROUTE
136 %token ROUTE_FAILURE
137 %token ROUTE_ONREPLY
138 %token EXEC
139 %token SET_HOST
140 %token SET_HOSTPORT
141 %token PREFIX
142 %token STRIP
143 %token STRIP_TAIL
144 %token APPEND_BRANCH
145 %token SET_USER
146 %token SET_USERPASS
147 %token SET_PORT
148 %token SET_URI
149 %token REVERT_URI
150 %token FORCE_RPORT
151 %token IF
152 %token ELSE
153 %token SET_ADV_ADDRESS
154 %token SET_ADV_PORT
155 %token URIHOST
156 %token URIPORT
157 %token MAX_LEN
158 %token SETFLAG
159 %token RESETFLAG
160 %token ISFLAGSET
161 %token METHOD
162 %token URI
163 %token SRCIP
164 %token SRCPORT
165 %token DSTIP
166 %token DSTPORT
167 %token PROTO
168 %token AF
169 %token MYSELF
170 %token MSGLEN 
171 %token UDP
172 %token TCP
173 %token TLS
174
175 /* config vars. */
176 %token DEBUG
177 %token FORK
178 %token LOGSTDERROR
179 %token LISTEN
180 %token ALIAS
181 %token DNS
182 %token REV_DNS
183 %token PORT
184 %token STAT
185 %token CHILDREN
186 %token CHECK_VIA
187 %token SYN_BRANCH
188 %token MEMLOG
189 %token SIP_WARNING
190 %token FIFO
191 %token FIFO_DIR
192 %token FIFO_MODE
193 %token SERVER_SIGNATURE
194 %token REPLY_TO_VIA
195 %token LOADMODULE
196 %token MODPARAM
197 %token MAXBUFFER
198 %token USER
199 %token GROUP
200 %token CHROOT
201 %token WDIR
202 %token MHOMED
203 %token DISABLE_TCP
204 %token TCP_CHILDREN
205 %token DISABLE_TLS
206 %token TLSLOG
207 %token TLS_PORT_NO
208 %token TLS_METHOD
209 %token SSLv23
210 %token SSLv2
211 %token SSLv3
212 %token TLSv1
213 %token TLS_VERIFY
214 %token TLS_REQUIRE_CERTIFICATE
215 %token TLS_CERTIFICATE
216 %token TLS_PRIVATE_KEY
217 %token TLS_CA_LIST
218 %token ADVERTISED_ADDRESS
219 %token ADVERTISED_PORT
220
221
222
223
224
225 /* operators */
226 %nonassoc EQUAL
227 %nonassoc EQUAL_T
228 %nonassoc GT
229 %nonassoc LT
230 %nonassoc GTE
231 %nonassoc LTE
232 %nonassoc DIFF
233 %nonassoc MATCH
234 %left OR
235 %left AND
236 %left NOT
237
238 /* values */
239 %token <intval> NUMBER
240 %token <strval> ID
241 %token <strval> STRING
242 %token <strval> IPV6ADDR
243
244 /* other */
245 %token COMMA
246 %token SEMICOLON
247 %token RPAREN
248 %token LPAREN
249 %token LBRACE
250 %token RBRACE
251 %token LBRACK
252 %token RBRACK
253 %token SLASH
254 %token DOT
255 %token CR
256 %token COLON
257 %token STAR
258
259
260 /*non-terminals */
261 %type <expr> exp exp_elem /*, condition*/
262 %type <action> action actions cmd if_cmd stm
263 %type <ipaddr> ipv4 ipv6 ipv6addr ip
264 %type <ipnet> ipnet
265 %type <strval> host
266 %type <strval> listen_id
267 %type <idlst>  id_lst
268 %type <idlst>  phostport
269 %type <intval> proto port
270 %type <intval> equalop strop intop
271 /*%type <route_el> rules;
272   %type <route_el> rule;
273 */
274
275
276
277 %%
278
279
280 cfg:    statements
281         ;
282
283 statements:     statements statement {}
284                 | statement {}
285                 | statements error { yyerror(""); YYABORT;}
286         ;
287
288 statement:      assign_stm 
289                 | module_stm
290                 | {rt=REQUEST_ROUTE;} route_stm 
291                 | {rt=FAILURE_ROUTE;} failure_route_stm
292                 | {rt=ONREPLY_ROUTE;} onreply_route_stm
293
294                 | CR    /* null statement*/
295         ;
296
297 listen_id:      ip                      {       tmp=ip_addr2a($1);
298                                                         if(tmp==0){
299                                                                 LOG(L_CRIT, "ERROR: cfg. parser: bad ip "
300                                                                                 "addresss.\n");
301                                                                 $$=0;
302                                                         }else{
303                                                                 $$=pkg_malloc(strlen(tmp)+1);
304                                                                 if ($$==0){
305                                                                         LOG(L_CRIT, "ERROR: cfg. parser: out of "
306                                                                                         "memory.\n");
307                                                                 }else{
308                                                                         strncpy($$, tmp, strlen(tmp)+1);
309                                                                 }
310                                                         }
311                                                 }
312                  |      STRING                  {       $$=pkg_malloc(strlen($1)+1);
313                                                         if ($$==0){
314                                                                         LOG(L_CRIT, "ERROR: cfg. parser: out of "
315                                                                                         "memory.\n");
316                                                         }else{
317                                                                         strncpy($$, $1, strlen($1)+1);
318                                                         }
319                                                 }
320                  |      host            {       $$=pkg_malloc(strlen($1)+1);
321                                                         if ($$==0){
322                                                                         LOG(L_CRIT, "ERROR: cfg. parser: out of "
323                                                                                         "memory.\n");
324                                                         }else{
325                                                                         strncpy($$, $1, strlen($1)+1);
326                                                         }
327                                                 }
328         ;
329
330 proto:    UDP   { $$=PROTO_UDP; }
331                 | TCP   { $$=PROTO_TCP; }
332                 | TLS   { $$=PROTO_TLS; }
333                 | STAR  { $$=0; }
334                 ;
335
336 port:     NUMBER        { $$=$1; }
337                 | STAR          { $$=0; }
338 ;
339
340 phostport:      listen_id                               { $$=mk_listen_id($1, 0, 0); }
341                         | listen_id COLON port  { $$=mk_listen_id($1, 0, $3); }
342                         | proto COLON listen_id { $$=mk_listen_id($3, $1, 0); }
343                         | proto COLON listen_id COLON port      { $$=mk_listen_id($3, $1, $5);}
344                         | listen_id COLON error { $$=0; yyerror(" port number expected"); }
345                         ;
346
347 id_lst:         phostport               {  $$=$1 ; }
348                 | phostport id_lst      { $$=$1; $$->next=$2; }
349                 ;
350
351
352 assign_stm:     DEBUG EQUAL NUMBER { debug=$3; }
353                 | DEBUG EQUAL error  { yyerror("number  expected"); }
354                 | FORK  EQUAL NUMBER { dont_fork= ! $3; }
355                 | FORK  EQUAL error  { yyerror("boolean value expected"); }
356                 | LOGSTDERROR EQUAL NUMBER { if (!config_check) log_stderr=$3; }
357                 | LOGSTDERROR EQUAL error { yyerror("boolean value expected"); }
358                 | DNS EQUAL NUMBER   { received_dns|= ($3)?DO_DNS:0; }
359                 | DNS EQUAL error { yyerror("boolean value expected"); }
360                 | REV_DNS EQUAL NUMBER { received_dns|= ($3)?DO_REV_DNS:0; }
361                 | REV_DNS EQUAL error { yyerror("boolean value expected"); }
362                 | PORT EQUAL NUMBER   { port_no=$3; }
363                 | STAT EQUAL STRING {
364                                         #ifdef STATS
365                                                         stat_file=$3;
366                                         #endif
367                                                         }
368                 | MAXBUFFER EQUAL NUMBER { maxbuffer=$3; }
369                 | MAXBUFFER EQUAL error { yyerror("number expected"); }
370                 | PORT EQUAL error    { yyerror("number expected"); } 
371                 | CHILDREN EQUAL NUMBER { children_no=$3; }
372                 | CHILDREN EQUAL error { yyerror("number expected"); } 
373                 | CHECK_VIA EQUAL NUMBER { check_via=$3; }
374                 | CHECK_VIA EQUAL error { yyerror("boolean value expected"); }
375                 | SYN_BRANCH EQUAL NUMBER { syn_branch=$3; }
376                 | SYN_BRANCH EQUAL error { yyerror("boolean value expected"); }
377                 | MEMLOG EQUAL NUMBER { memlog=$3; }
378                 | MEMLOG EQUAL error { yyerror("int value expected"); }
379                 | SIP_WARNING EQUAL NUMBER { sip_warning=$3; }
380                 | SIP_WARNING EQUAL error { yyerror("boolean value expected"); }
381                 | FIFO EQUAL STRING { fifo=$3; }
382                 | FIFO EQUAL error { yyerror("string value expected"); }
383                 | FIFO_DIR EQUAL STRING { fifo_dir=$3; }
384                 | FIFO_DIR EQUAL error { yyerror("string value expected"); }
385                 | FIFO_MODE EQUAL NUMBER { fifo_mode=$3; }
386                 | FIFO_MODE EQUAL error { yyerror("int value expected"); }
387                 | USER EQUAL STRING     { user=$3; }
388                 | USER EQUAL ID         { user=$3; }
389                 | USER EQUAL error      { yyerror("string value expected"); }
390                 | GROUP EQUAL STRING     { group=$3; }
391                 | GROUP EQUAL ID         { group=$3; }
392                 | GROUP EQUAL error      { yyerror("string value expected"); }
393                 | CHROOT EQUAL STRING     { chroot_dir=$3; }
394                 | CHROOT EQUAL ID         { chroot_dir=$3; }
395                 | CHROOT EQUAL error      { yyerror("string value expected"); }
396                 | WDIR EQUAL STRING     { working_dir=$3; }
397                 | WDIR EQUAL ID         { working_dir=$3; }
398                 | WDIR EQUAL error      { yyerror("string value expected"); }
399                 | MHOMED EQUAL NUMBER { mhomed=$3; }
400                 | MHOMED EQUAL error { yyerror("boolean value expected"); }
401                 | DISABLE_TCP EQUAL NUMBER {
402                                                                         #ifdef USE_TCP
403                                                                                 tcp_disable=$3;
404                                                                         #else
405                                                                                 warn("tcp support not compiled in");
406                                                                         #endif
407                                                                         }
408                 | DISABLE_TCP EQUAL error { yyerror("boolean value expected"); }
409                 | TCP_CHILDREN EQUAL NUMBER {
410                                                                         #ifdef USE_TCP
411                                                                                 tcp_children_no=$3;
412                                                                         #else
413                                                                                 warn("tcp support not compiled in");
414                                                                         #endif
415                                                                         }
416                 | TCP_CHILDREN EQUAL error { yyerror("number expected"); }
417                 | DISABLE_TLS EQUAL NUMBER {
418                                                                         #ifdef USE_TLS
419                                                                                 tls_disable=$3;
420                                                                         #else
421                                                                                 warn("tls support not compiled in");
422                                                                         #endif
423                                                                         }
424                 | DISABLE_TLS EQUAL error { yyerror("boolean value expected"); }
425                 | TLSLOG EQUAL NUMBER           { 
426                                                                         #ifdef USE_TLS
427                                                                                 tls_log=$3;
428                                                                         #else
429                                                                                 warn("tls support not compiled in");
430                                                                         #endif
431                                                                         }
432                 | TLSLOG EQUAL error { yyerror("int value expected"); }
433                 | TLS_PORT_NO EQUAL NUMBER {
434                                                                         #ifdef USE_TLS
435                                                                                 tls_port_no=$3;
436                                                                         #else
437                                                                                 warn("tls support not compiled in");
438                                                                         #endif
439                                                                         }
440                 | TLS_PORT_NO EQUAL error { yyerror("number expected"); }
441                 | TLS_METHOD EQUAL SSLv23 {
442                                                                         #ifdef USE_TLS
443                                                                                 tls_method=TLS_USE_SSLv23;
444                                                                         #else
445                                                                                 warn("tls support not compiled in");
446                                                                         #endif
447                                                                         }
448                 | TLS_METHOD EQUAL SSLv2 {
449                                                                         #ifdef USE_TLS
450                                                                                 tls_method=TLS_USE_SSLv2;
451                                                                         #else
452                                                                                 warn("tls support not compiled in");
453                                                                         #endif
454                                                                         }
455                 | TLS_METHOD EQUAL SSLv3 {
456                                                                         #ifdef USE_TLS
457                                                                                 tls_method=TLS_USE_SSLv3;
458                                                                         #else
459                                                                                 warn("tls support not compiled in");
460                                                                         #endif
461                                                                         }
462                 | TLS_METHOD EQUAL TLSv1 {
463                                                                         #ifdef USE_TLS
464                                                                                 tls_method=TLS_USE_TLSv1;
465                                                                         #else
466                                                                                 warn("tls support not compiled in");
467                                                                         #endif
468                                                                         }
469                 | TLS_METHOD EQUAL error {
470                                                                         #ifdef USE_TLS
471                                                                                 yyerror("SSLv23, SSLv2, SSLv3 or TLSv1"
472                                                                                                         " expected");
473                                                                         #else
474                                                                                 warn("tls support not compiled in");
475                                                                         #endif
476                                                                         }
477                                                                                 
478                 | TLS_VERIFY EQUAL NUMBER {
479                                                                         #ifdef USE_TLS
480                                                                                 tls_verify_cert=$3;
481                                                                         #else
482                                                                                 warn("tls support not compiled in");
483                                                                         #endif
484                                                                         }
485                 | TLS_VERIFY EQUAL error { yyerror("boolean value expected"); }
486                 | TLS_REQUIRE_CERTIFICATE EQUAL NUMBER {
487                                                                         #ifdef USE_TLS
488                                                                                 tls_require_cert=$3;
489                                                                         #else
490                                                                                 warn( "tls support not compiled in");
491                                                                         #endif
492                                                                         }
493                 | TLS_REQUIRE_CERTIFICATE EQUAL error { yyerror("boolean value"
494                                                                                                                                 " expected"); }
495                 | TLS_CERTIFICATE EQUAL STRING { 
496                                                                         #ifdef USE_TLS
497                                                                                         tls_cert_file=$3;
498                                                                         #else
499                                                                                 warn("tls support not compiled in");
500                                                                         #endif
501                                                                         }
502                 | TLS_CERTIFICATE EQUAL error { yyerror("string value expected"); }
503                 | TLS_PRIVATE_KEY EQUAL STRING { 
504                                                                         #ifdef USE_TLS
505                                                                                         tls_pkey_file=$3;
506                                                                         #else
507                                                                                 warn("tls support not compiled in");
508                                                                         #endif
509                                                                         }
510                 | TLS_PRIVATE_KEY EQUAL error { yyerror("string value expected"); }
511                 | TLS_CA_LIST EQUAL STRING { 
512                                                                         #ifdef USE_TLS
513                                                                                         tls_ca_file=$3;
514                                                                         #else
515                                                                                 warn("tls support not compiled in");
516                                                                         #endif
517                                                                         }
518                 | TLS_CA_LIST EQUAL error { yyerror("string value expected"); }
519                 | SERVER_SIGNATURE EQUAL NUMBER { server_signature=$3; }
520                 | SERVER_SIGNATURE EQUAL error { yyerror("boolean value expected"); }
521                 | REPLY_TO_VIA EQUAL NUMBER { reply_to_via=$3; }
522                 | REPLY_TO_VIA EQUAL error { yyerror("boolean value expected"); }
523                 | LISTEN EQUAL id_lst {
524                                                         for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next){
525                                                                 if (add_listen_iface(   lst_tmp->name,
526                                                                                                                 lst_tmp->port,
527                                                                                                                 lst_tmp->proto,
528                                                                                                                 0
529                                                                                                         )!=0){
530                                                                         LOG(L_CRIT,  "ERROR: cfg. parser: failed"
531                                                                                         " to add listen address\n");
532                                                                         break;
533                                                                 }
534                                                         }
535                                                          }
536                 | LISTEN EQUAL  error { yyerror("ip address or hostname"
537                                                 "expected"); }
538                 | ALIAS EQUAL  id_lst { 
539                                                         for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next)
540                                                                 add_alias(lst_tmp->name, strlen(lst_tmp->name),
541                                                                                         lst_tmp->port, lst_tmp->proto);
542                                                           }
543                 | ALIAS  EQUAL error  { yyerror(" hostname expected"); }
544                 | ADVERTISED_ADDRESS EQUAL listen_id {
545                                                                 default_global_address.s=$3;
546                                                                 default_global_address.len=strlen($3);
547                                                                 }
548                 |ADVERTISED_ADDRESS EQUAL error {yyerror("ip address or hostname "
549                                                                                                 "expected"); }
550                 | ADVERTISED_PORT EQUAL NUMBER {
551                                                                 tmp=int2str($3, &i_tmp);
552                                                                 if ((default_global_port.s=pkg_malloc(i_tmp))
553                                                                                 ==0){
554                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
555                                                                                                         " out of memory.\n");
556                                                                                 default_global_port.len=0;
557                                                                 }else{
558                                                                         default_global_port.len=i_tmp;
559                                                                         memcpy(default_global_port.s, tmp,
560                                                                                         default_global_port.len);
561                                                                 };
562                                                                 }
563                 |ADVERTISED_PORT EQUAL error {yyerror("ip address or hostname "
564                                                                                                 "expected"); }
565                 | error EQUAL { yyerror("unknown config variable"); }
566         ;
567
568 module_stm:     LOADMODULE STRING       { DBG("loading module %s\n", $2);
569                                                                   if (load_module($2)!=0){
570                                                                                 yyerror("failed to load module");
571                                                                   }
572                                                                 }
573                  | LOADMODULE error     { yyerror("string expected");  }
574                  | MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
575                          if (set_mod_param_regex($3, $5, STR_PARAM, $7) != 0) {
576                                  yyerror("Can't set module parameter");
577                          }
578                    }
579                  | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
580                          if (set_mod_param_regex($3, $5, INT_PARAM, (void*)$7) != 0) {
581                                  yyerror("Can't set module parameter");
582                          }
583                    }
584                  | MODPARAM error { yyerror("Invalid arguments"); }
585                  ;
586
587
588 ip:              ipv4  { $$=$1; }
589                 |ipv6  { $$=$1; }
590                 ;
591
592 ipv4:   NUMBER DOT NUMBER DOT NUMBER DOT NUMBER { 
593                                                                                         $$=pkg_malloc(
594                                                                                                         sizeof(struct ip_addr));
595                                                                                         if ($$==0){
596                                                                                                 LOG(L_CRIT, "ERROR: cfg. "
597                                                                                                         "parser: out of memory.\n"
598                                                                                                         );
599                                                                                         }else{
600                                                                                                 memset($$, 0, 
601                                                                                                         sizeof(struct ip_addr));
602                                                                                                 $$->af=AF_INET;
603                                                                                                 $$->len=4;
604                                                                                                 if (($1>255) || ($1<0) ||
605                                                                                                         ($3>255) || ($3<0) ||
606                                                                                                         ($5>255) || ($5<0) ||
607                                                                                                         ($7>255) || ($7<0)){
608                                                                                                         yyerror("invalid ipv4"
609                                                                                                                         "address");
610                                                                                                         $$->u.addr32[0]=0;
611                                                                                                         /* $$=0; */
612                                                                                                 }else{
613                                                                                                         $$->u.addr[0]=$1;
614                                                                                                         $$->u.addr[1]=$3;
615                                                                                                         $$->u.addr[2]=$5;
616                                                                                                         $$->u.addr[3]=$7;
617                                                                                                         /*
618                                                                                                         $$=htonl( ($1<<24)|
619                                                                                                         ($3<<16)| ($5<<8)|$7 );
620                                                                                                         */
621                                                                                                 }
622                                                                                         }
623                                                                                                 }
624         ;
625
626 ipv6addr:       IPV6ADDR {
627                                         $$=pkg_malloc(sizeof(struct ip_addr));
628                                         if ($$==0){
629                                                 LOG(L_CRIT, "ERROR: cfg. parser: out of memory.\n");
630                                         }else{
631                                                 memset($$, 0, sizeof(struct ip_addr));
632                                                 $$->af=AF_INET6;
633                                                 $$->len=16;
634                                         #ifdef USE_IPV6
635                                                 if (inet_pton(AF_INET6, $1, $$->u.addr)<=0){
636                                                         yyerror("bad ipv6 address");
637                                                 }
638                                         #else
639                                                 yyerror("ipv6 address & no ipv6 support compiled in");
640                                                 YYABORT;
641                                         #endif
642                                         }
643                                 }
644         ;
645
646 ipv6:   ipv6addr { $$=$1; }
647         | LBRACK ipv6addr RBRACK {$$=$2; }
648 ;
649
650
651 route_stm:  ROUTE LBRACE actions RBRACE { push($3, &rlist[DEFAULT_RT]); }
652
653             | ROUTE LBRACK NUMBER RBRACK LBRACE actions RBRACE { 
654                                                                                 if (($3<RT_NO) && ($3>=0)){
655                                                                                         push($6, &rlist[$3]);
656                                                                                 }else{
657                                                                                         yyerror("invalid routing"
658                                                                                                         "table number");
659                                                                                         YYABORT; }
660                                                                                 }
661                 | ROUTE error { yyerror("invalid  route  statement"); }
662         ;
663
664 failure_route_stm: ROUTE_FAILURE LBRACK NUMBER RBRACK LBRACE actions RBRACE {
665                                                                                 if (($3<FAILURE_RT_NO)&&($3>=1)){
666                                                                                         push($6, &failure_rlist[$3]);
667                                                                                 } else {
668                                                                                         yyerror("invalid reply routing"
669                                                                                                 "table number");
670                                                                                         YYABORT; }
671                                                                                 }
672                 | ROUTE_FAILURE error { yyerror("invalid failure_route statement"); }
673         ;
674
675 onreply_route_stm: ROUTE_ONREPLY LBRACK NUMBER RBRACK LBRACE actions RBRACE {
676                                                                                 if (($3<ONREPLY_RT_NO)&&($3>=1)){
677                                                                                         push($6, &onreply_rlist[$3]);
678                                                                                 } else {
679                                                                                         yyerror("invalid reply routing"
680                                                                                                 "table number");
681                                                                                         YYABORT; }
682                                                                                 }
683                 | ROUTE_ONREPLY error { yyerror("invalid failure_route statement"); }
684         ;
685 /*
686 rules:  rules rule { push($2, &$1); $$=$1; }
687         | rule {$$=$1; }
688         | rules error { $$=0; yyerror("invalid rule"); }
689          ;
690
691 rule:   condition       actions CR {
692                                                                 $$=0;
693                                                                 if (add_rule($1, $2, &$$)<0) {
694                                                                         yyerror("error calling add_rule");
695                                                                         YYABORT;
696                                                                 }
697                                                           }
698         | CR            { $$=0;}
699         | condition error { $$=0; yyerror("bad actions in rule"); }
700         ;
701
702 condition:      exp {$$=$1;}
703 */
704
705 exp:    exp AND exp     { $$=mk_exp(AND_OP, $1, $3); }
706         | exp OR  exp           { $$=mk_exp(OR_OP, $1, $3);  }
707         | NOT exp                       { $$=mk_exp(NOT_OP, $2, 0);  }
708         | LPAREN exp RPAREN     { $$=$2; }
709         | exp_elem                      { $$=$1; }
710         ;
711
712 equalop:          EQUAL_T {$$=EQUAL_OP; }
713                         | DIFF  {$$=DIFF_OP; }
714                 ;
715                 
716 intop:  equalop {$$=$1; }
717                 |  GT   {$$=GT_OP; }
718                 | LT    {$$=LT_OP; }
719                 | GTE   {$$=GTE_OP; }
720                 | LTE   {$$=LTE_OP; }
721                 ;
722                 
723 strop:  equalop {$$=$1; }
724                 | MATCH {$$=MATCH_OP; }
725                 ;
726
727 exp_elem:       METHOD strop STRING     {$$= mk_elem(   $2, STRING_ST, 
728                                                                                                         METHOD_O, $3);
729                                                                         }
730                 | METHOD strop  ID      {$$ = mk_elem(  $2, STRING_ST,
731                                                                                         METHOD_O, $3); 
732                                                         }
733                 | METHOD strop error { $$=0; yyerror("string expected"); }
734                 | METHOD error  { $$=0; yyerror("invalid operator,"
735                                                                                 "== , !=, or =~ expected");
736                                                 }
737                 | URI strop STRING      {$$ = mk_elem(  $2, STRING_ST,
738                                                                                                 URI_O, $3); 
739                                                                 }
740                 | URI strop host        {$$ = mk_elem(  $2, STRING_ST,
741                                                                                         URI_O, $3); 
742                                                         }
743                 | URI equalop MYSELF    { $$=mk_elem(   $2, MYSELF_ST,
744                                                                                                 URI_O, 0);
745                                                                 }
746                 | URI strop error { $$=0; yyerror("string or MYSELF expected"); }
747                 | URI error     { $$=0; yyerror("invalid operator,"
748                                                                         " == , != or =~ expected");
749                                         }
750                 | SRCPORT intop NUMBER  { $$=mk_elem(   $2, NUMBER_ST,
751                                                                                                 SRCPORT_O, (void *) $3 ); }
752                 | SRCPORT intop error { $$=0; yyerror("number expected"); }
753                 | SRCPORT error { $$=0; yyerror("==, !=, <,>, >= or <=  expected"); }
754                 | DSTPORT intop NUMBER  { $$=mk_elem(   $2, NUMBER_ST,
755                                                                                                 DSTPORT_O, (void *) $3 ); }
756                 | DSTPORT intop error { $$=0; yyerror("number expected"); }
757                 | DSTPORT error { $$=0; yyerror("==, !=, <,>, >= or <=  expected"); }
758                 | PROTO intop proto     { $$=mk_elem(   $2, NUMBER_ST,
759                                                                                                 PROTO_O, (void *) $3 ); }
760                 | PROTO intop error { $$=0;
761                                                                 yyerror("protocol expected (udp, tcp or tls)");
762                                                         }
763                 | PROTO error { $$=0; yyerror("equal/!= operator expected"); }
764                 | AF intop NUMBER       { $$=mk_elem(   $2, NUMBER_ST,
765                                                                                                 AF_O, (void *) $3 ); }
766                 | AF intop error { $$=0; yyerror("number expected"); }
767                 | AF error { $$=0; yyerror("equal/!= operator expected"); }
768                 | MSGLEN intop NUMBER   { $$=mk_elem(   $2, NUMBER_ST,
769                                                                                                 MSGLEN_O, (void *) $3 ); }
770                 | MSGLEN intop MAX_LEN  { $$=mk_elem(   $2, NUMBER_ST,
771                                                                                                 MSGLEN_O, (void *) BUF_SIZE); }
772                 | MSGLEN intop error { $$=0; yyerror("number expected"); }
773                 | MSGLEN error { $$=0; yyerror("equal/!= operator expected"); }
774                 | SRCIP equalop ipnet   { $$=mk_elem(   $2, NET_ST,
775                                                                                                 SRCIP_O, $3);
776                                                                 }
777                 | SRCIP strop STRING    { $$=mk_elem(   $2, STRING_ST,
778                                                                                                 SRCIP_O, $3);
779                                                                 }
780                 | SRCIP strop host      { $$=mk_elem(   $2, STRING_ST,
781                                                                                                 SRCIP_O, $3);
782                                                                 }
783                 | SRCIP equalop MYSELF  { $$=mk_elem(   $2, MYSELF_ST,
784                                                                                                 SRCIP_O, 0);
785                                                                 }
786                 | SRCIP strop error { $$=0; yyerror( "ip address or hostname"
787                                                  "expected" ); }
788                 | SRCIP error  { $$=0; 
789                                                  yyerror("invalid operator, ==, != or =~ expected");}
790                 | DSTIP equalop ipnet   { $$=mk_elem(   $2, NET_ST,
791                                                                                                 DSTIP_O, $3);
792                                                                 }
793                 | DSTIP strop STRING    { $$=mk_elem(   $2, STRING_ST,
794                                                                                                 DSTIP_O, $3);
795                                                                 }
796                 | DSTIP strop host      { $$=mk_elem(   $2, STRING_ST,
797                                                                                                 DSTIP_O, $3);
798                                                                 }
799                 | DSTIP equalop MYSELF  { $$=mk_elem(   $2, MYSELF_ST,
800                                                                                                 DSTIP_O, 0);
801                                                                 }
802                 | DSTIP strop error { $$=0; yyerror( "ip address or hostname"
803                                                                         "expected" ); }
804                 | DSTIP error { $$=0; 
805                                                 yyerror("invalid operator, ==, != or =~ expected");}
806                 | MYSELF equalop URI    { $$=mk_elem(   $2, MYSELF_ST,
807                                                                                                 URI_O, 0);
808                                                                 }
809                 | MYSELF equalop SRCIP  { $$=mk_elem(   $2, MYSELF_ST,
810                                                                                                 SRCIP_O, 0);
811                                                                 }
812                 | MYSELF equalop DSTIP  { $$=mk_elem(   $2, MYSELF_ST,
813                                                                                                 DSTIP_O, 0);
814                                                                 }
815                 | MYSELF equalop error {        $$=0; 
816                                                                         yyerror(" URI, SRCIP or DSTIP expected"); }
817                 | MYSELF error  { $$=0; 
818                                                         yyerror ("invalid operator, == or != expected");
819                                                 }
820                 | stm                           { $$=mk_elem( NO_OP, ACTIONS_ST, ACTION_O, $1 ); }
821                 | NUMBER                {$$=mk_elem( NO_OP, NUMBER_ST, NUMBER_O, (void*)$1 ); }
822         ;
823
824 ipnet:  ip SLASH ip     { $$=mk_net($1, $3); } 
825         | ip SLASH NUMBER       {       if (($3<0) || ($3>$1->len*8)){
826                                                                 yyerror("invalid bit number in netmask");
827                                                                 $$=0;
828                                                         }else{
829                                                                 $$=mk_net_bitlen($1, $3);
830                                                         /*
831                                                                 $$=mk_net($1, 
832                                                                                 htonl( ($3)?~( (1<<(32-$3))-1 ):0 ) );
833                                                         */
834                                                         }
835                                                 }
836         | ip                            { $$=mk_net_bitlen($1, $1->len*8); }
837         | ip SLASH error        { $$=0;
838                                                  yyerror("netmask (eg:255.0.0.0 or 8) expected");
839                                                 }
840         ;
841
842 host:   ID                              { $$=$1; }
843         | host DOT ID           { $$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
844                                                   if ($$==0){
845                                                         LOG(L_CRIT, "ERROR: cfg. parser: memory allocation"
846                                                                                 " failure while parsing host\n");
847                                                   }else{
848                                                         memcpy($$, $1, strlen($1));
849                                                         $$[strlen($1)]='.';
850                                                         memcpy($$+strlen($1)+1, $3, strlen($3));
851                                                         $$[strlen($1)+1+strlen($3)]=0;
852                                                   }
853                                                   pkg_free($1); pkg_free($3);
854                                                 }
855         | host DOT error { $$=0; pkg_free($1); yyerror("invalid hostname"); }
856         ;
857
858
859 stm:            cmd                                             { $$=$1; }
860                 |       if_cmd                                  { $$=$1; }
861                 |       LBRACE actions RBRACE   { $$=$2; }
862         ;
863
864 actions:        actions action  {$$=append_action($1, $2); }
865                 | action                        {$$=$1;}
866                 | actions error { $$=0; yyerror("bad command"); }
867         ;
868
869 action:         cmd SEMICOLON {$$=$1;}
870                 | if_cmd {$$=$1;}
871                 | SEMICOLON /* null action */ {$$=0;}
872                 | cmd error { $$=0; yyerror("bad command: missing ';'?"); }
873         ;
874
875 if_cmd:         IF exp stm                              { $$=mk_action3( IF_T,
876                                                                                                          EXPR_ST,
877                                                                                                          ACTIONS_ST,
878                                                                                                          NOSUBTYPE,
879                                                                                                          $2,
880                                                                                                          $3,
881                                                                                                          0);
882                                                                         }
883                 |       IF exp stm ELSE stm             { $$=mk_action3( IF_T,
884                                                                                                          EXPR_ST,
885                                                                                                          ACTIONS_ST,
886                                                                                                          ACTIONS_ST,
887                                                                                                          $2,
888                                                                                                          $3,
889                                                                                                          $5);
890                                                                         }
891         ;
892
893 cmd:            FORWARD LPAREN host RPAREN      { $$=mk_action( FORWARD_T,
894                                                                                                                 STRING_ST,
895                                                                                                                 NUMBER_ST,
896                                                                                                                 $3,
897                                                                                                                 0);
898                                                                                 }
899                 | FORWARD LPAREN STRING RPAREN  { $$=mk_action( FORWARD_T,
900                                                                                                                 STRING_ST,
901                                                                                                                 NUMBER_ST,
902                                                                                                                 $3,
903                                                                                                                 0);
904                                                                                 }
905                 | FORWARD LPAREN ip RPAREN      { $$=mk_action( FORWARD_T,
906                                                                                                                 IP_ST,
907                                                                                                                 NUMBER_ST,
908                                                                                                                 (void*)$3,
909                                                                                                                 0);
910                                                                                 }
911                 | FORWARD LPAREN host COMMA NUMBER RPAREN { $$=mk_action(FORWARD_T,
912                                                                                                                                  STRING_ST,
913                                                                                                                                  NUMBER_ST,
914                                                                                                                                 $3,
915                                                                                                                                 (void*)$5);
916                                                                                                  }
917                 | FORWARD LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(FORWARD_T,
918                                                                                                                                  STRING_ST,
919                                                                                                                                  NUMBER_ST,
920                                                                                                                                 $3,
921                                                                                                                                 (void*)$5);
922                                                                                                         }
923                 | FORWARD LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(FORWARD_T,
924                                                                                                                                  IP_ST,
925                                                                                                                                  NUMBER_ST,
926                                                                                                                                  (void*)$3,
927                                                                                                                                 (void*)$5);
928                                                                                                   }
929                 | FORWARD LPAREN URIHOST COMMA URIPORT RPAREN {
930                                                                                                         $$=mk_action(FORWARD_T,
931                                                                                                                                  URIHOST_ST,
932                                                                                                                                  URIPORT_ST,
933                                                                                                                                 0,
934                                                                                                                                 0);
935                                                                                                         }
936                                                                                                         
937                                                                         
938                 | FORWARD LPAREN URIHOST COMMA NUMBER RPAREN {
939                                                                                                         $$=mk_action(FORWARD_T,
940                                                                                                                                  URIHOST_ST,
941                                                                                                                                  NUMBER_ST,
942                                                                                                                                 0,
943                                                                                                                                 (void*)$5);
944                                                                                                         }
945                 | FORWARD LPAREN URIHOST RPAREN {
946                                                                                                         $$=mk_action(FORWARD_T,
947                                                                                                                                  URIHOST_ST,
948                                                                                                                                  NUMBER_ST,
949                                                                                                                                 0,
950                                                                                                                                 0);
951                                                                                 }
952                 | FORWARD error { $$=0; yyerror("missing '(' or ')' ?"); }
953                 | FORWARD LPAREN error RPAREN { $$=0; yyerror("bad forward"
954                                                                                 "argument"); }
955                 | FORWARD_UDP LPAREN host RPAREN        { $$=mk_action( FORWARD_UDP_T,
956                                                                                                                 STRING_ST,
957                                                                                                                 NUMBER_ST,
958                                                                                                                 $3,
959                                                                                                                 0);
960                                                                                 }
961                 | FORWARD_UDP LPAREN STRING RPAREN      { $$=mk_action( FORWARD_UDP_T,
962                                                                                                                 STRING_ST,
963                                                                                                                 NUMBER_ST,
964                                                                                                                 $3,
965                                                                                                                 0);
966                                                                                 }
967                 | FORWARD_UDP LPAREN ip RPAREN  { $$=mk_action( FORWARD_UDP_T,
968                                                                                                                 IP_ST,
969                                                                                                                 NUMBER_ST,
970                                                                                                                 (void*)$3,
971                                                                                                                 0);
972                                                                                 }
973                 | FORWARD_UDP LPAREN host COMMA NUMBER RPAREN { $$=mk_action(
974                                                                                                                                 FORWARD_UDP_T,
975                                                                                                                                  STRING_ST,
976                                                                                                                                  NUMBER_ST,
977                                                                                                                                 $3,
978                                                                                                                                 (void*)$5);
979                                                                                                  }
980                 | FORWARD_UDP LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(
981                                                                                                                                 FORWARD_UDP_T,
982                                                                                                                                  STRING_ST,
983                                                                                                                                  NUMBER_ST,
984                                                                                                                                 $3,
985                                                                                                                                 (void*)$5);
986                                                                                                         }
987                 | FORWARD_UDP LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(
988                                                                                                                                 FORWARD_UDP_T,
989                                                                                                                                  IP_ST,
990                                                                                                                                  NUMBER_ST,
991                                                                                                                                  (void*)$3,
992                                                                                                                                 (void*)$5);
993                                                                                                   }
994                 | FORWARD_UDP LPAREN URIHOST COMMA URIPORT RPAREN {
995                                                                                                         $$=mk_action(FORWARD_UDP_T,
996                                                                                                                                  URIHOST_ST,
997                                                                                                                                  URIPORT_ST,
998                                                                                                                                 0,
999                                                                                                                                 0);
1000                                                                                                         }
1001                                                                                                         
1002                                                                         
1003                 | FORWARD_UDP LPAREN URIHOST COMMA NUMBER RPAREN {
1004                                                                                                         $$=mk_action(FORWARD_UDP_T,
1005                                                                                                                                  URIHOST_ST,
1006                                                                                                                                  NUMBER_ST,
1007                                                                                                                                 0,
1008                                                                                                                                 (void*)$5);
1009                                                                                                         }
1010                 | FORWARD_UDP LPAREN URIHOST RPAREN {
1011                                                                                                         $$=mk_action(FORWARD_UDP_T,
1012                                                                                                                                  URIHOST_ST,
1013                                                                                                                                  NUMBER_ST,
1014                                                                                                                                 0,
1015                                                                                                                                 0);
1016                                                                                 }
1017                 | FORWARD_UDP error { $$=0; yyerror("missing '(' or ')' ?"); }
1018                 | FORWARD_UDP LPAREN error RPAREN { $$=0; yyerror("bad forward_udp"
1019                                                                                 "argument"); }
1020                 | FORWARD_TCP LPAREN host RPAREN        { $$=mk_action( FORWARD_TCP_T,
1021                                                                                                                 STRING_ST,
1022                                                                                                                 NUMBER_ST,
1023                                                                                                                 $3,
1024                                                                                                                 0);
1025                                                                                 }
1026                 | FORWARD_TCP LPAREN STRING RPAREN      { $$=mk_action( FORWARD_TCP_T,
1027                                                                                                                 STRING_ST,
1028                                                                                                                 NUMBER_ST,
1029                                                                                                                 $3,
1030                                                                                                                 0);
1031                                                                                 }
1032                 | FORWARD_TCP LPAREN ip RPAREN  { $$=mk_action( FORWARD_TCP_T,
1033                                                                                                                 IP_ST,
1034                                                                                                                 NUMBER_ST,
1035                                                                                                                 (void*)$3,
1036                                                                                                                 0);
1037                                                                                 }
1038                 | FORWARD_TCP LPAREN host COMMA NUMBER RPAREN { $$=mk_action(
1039                                                                                                                                 FORWARD_TCP_T,
1040                                                                                                                                  STRING_ST,
1041                                                                                                                                  NUMBER_ST,
1042                                                                                                                                 $3,
1043                                                                                                                                 (void*)$5);
1044                                                                                                  }
1045                 | FORWARD_TCP LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(
1046                                                                                                                                 FORWARD_TCP_T,
1047                                                                                                                                  STRING_ST,
1048                                                                                                                                  NUMBER_ST,
1049                                                                                                                                 $3,
1050                                                                                                                                 (void*)$5);
1051                                                                                                         }
1052                 | FORWARD_TCP LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(FORWARD_TCP_T,
1053                                                                                                                                  IP_ST,
1054                                                                                                                                  NUMBER_ST,
1055                                                                                                                                  (void*)$3,
1056                                                                                                                                 (void*)$5);
1057                                                                                                   }
1058                 | FORWARD_TCP LPAREN URIHOST COMMA URIPORT RPAREN {
1059                                                                                                         $$=mk_action(FORWARD_TCP_T,
1060                                                                                                                                  URIHOST_ST,
1061                                                                                                                                  URIPORT_ST,
1062                                                                                                                                 0,
1063                                                                                                                                 0);
1064                                                                                                         }
1065                                                                                                         
1066                                                                         
1067                 | FORWARD_TCP LPAREN URIHOST COMMA NUMBER RPAREN {
1068                                                                                                         $$=mk_action(FORWARD_TCP_T,
1069                                                                                                                                  URIHOST_ST,
1070                                                                                                                                  NUMBER_ST,
1071                                                                                                                                 0,
1072                                                                                                                                 (void*)$5);
1073                                                                                                         }
1074                 | FORWARD_TCP LPAREN URIHOST RPAREN {
1075                                                                                                         $$=mk_action(FORWARD_TCP_T,
1076                                                                                                                                  URIHOST_ST,
1077                                                                                                                                  NUMBER_ST,
1078                                                                                                                                 0,
1079                                                                                                                                 0);
1080                                                                                 }
1081                 | FORWARD_TCP error { $$=0; yyerror("missing '(' or ')' ?"); }
1082                 | FORWARD_TCP LPAREN error RPAREN { $$=0; yyerror("bad forward_tcp"
1083                                                                                 "argument"); }
1084                 | FORWARD_TLS LPAREN host RPAREN        {
1085                                                                                 #ifdef USE_TLS
1086                                                                                         $$=mk_action(   FORWARD_TLS_T,
1087                                                                                                                 STRING_ST,
1088                                                                                                                 NUMBER_ST,
1089                                                                                                                 $3,
1090                                                                                                                 0);
1091                                                                                 #else
1092                                                                                         yyerror("tls support not "
1093                                                                                                         "compiled in");
1094                                                                                 #endif
1095                                                                                 }
1096                 | FORWARD_TLS LPAREN STRING RPAREN      {
1097                                                                                 #ifdef USE_TLS
1098                                                                                         $$=mk_action(   FORWARD_TLS_T,
1099                                                                                                                         STRING_ST,
1100                                                                                                                         NUMBER_ST,
1101                                                                                                                         $3,
1102                                                                                                                         0);
1103                                                                                 #else
1104                                                                                         yyerror("tls support not "
1105                                                                                                         "compiled in");
1106                                                                                 #endif
1107                                                                                 }
1108                 | FORWARD_TLS LPAREN ip RPAREN  { 
1109                                                                                 #ifdef USE_TLS
1110                                                                                         $$=mk_action(   FORWARD_TLS_T,
1111                                                                                                                         IP_ST,
1112                                                                                                                         NUMBER_ST,
1113                                                                                                                         (void*)$3,
1114                                                                                                                         0);
1115                                                                                 #else
1116                                                                                         yyerror("tls support not "
1117                                                                                                         "compiled in");
1118                                                                                 #endif
1119                                                                                 }
1120                 | FORWARD_TLS LPAREN host COMMA NUMBER RPAREN { 
1121                                                                                 #ifdef USE_TLS
1122                                                                                         $$=mk_action(   FORWARD_TLS_T,
1123                                                                                                                          STRING_ST,
1124                                                                                                                          NUMBER_ST,
1125                                                                                                                         $3,
1126                                                                                                                         (void*)$5);
1127                                                                                 #else
1128                                                                                         yyerror("tls support not "
1129                                                                                                         "compiled in");
1130                                                                                 #endif
1131                                                                                                  }
1132                 | FORWARD_TLS LPAREN STRING COMMA NUMBER RPAREN {
1133                                                                                 #ifdef USE_TLS
1134                                                                                         $$=mk_action(   FORWARD_TLS_T,
1135                                                                                                                          STRING_ST,
1136                                                                                                                          NUMBER_ST,
1137                                                                                                                         $3,
1138                                                                                                                         (void*)$5);
1139                                                                                 #else
1140                                                                                         yyerror("tls support not "
1141                                                                                                         "compiled in");
1142                                                                                 #endif
1143                                                                                                         }
1144                 | FORWARD_TLS LPAREN ip COMMA NUMBER RPAREN {
1145                                                                                 #ifdef USE_TLS
1146                                                                                         $$=mk_action(   FORWARD_TLS_T,
1147                                                                                                                          IP_ST,
1148                                                                                                                          NUMBER_ST,
1149                                                                                                                          (void*)$3,
1150                                                                                                                         (void*)$5);
1151                                                                                 #else
1152                                                                                         yyerror("tls support not "
1153                                                                                                         "compiled in");
1154                                                                                 #endif
1155                                                                                                   }
1156                 | FORWARD_TLS LPAREN URIHOST COMMA URIPORT RPAREN {
1157                                                                                 #ifdef USE_TLS
1158                                                                                         $$=mk_action(   FORWARD_TLS_T,
1159                                                                                                                          URIHOST_ST,
1160                                                                                                                          URIPORT_ST,
1161                                                                                                                         0,
1162                                                                                                                         0);
1163                                                                                 #else
1164                                                                                         yyerror("tls support not "
1165                                                                                                         "compiled in");
1166                                                                                 #endif
1167                                                                                                         }
1168                                                                                                         
1169                                                                         
1170                 | FORWARD_TLS LPAREN URIHOST COMMA NUMBER RPAREN {
1171                                                                                 #ifdef USE_TLS
1172                                                                                         $$=mk_action(   FORWARD_TLS_T,
1173                                                                                                                          URIHOST_ST,
1174                                                                                                                          NUMBER_ST,
1175                                                                                                                         0,
1176                                                                                                                         (void*)$5);
1177                                                                                 #else
1178                                                                                         yyerror("tls support not "
1179                                                                                                         "compiled in");
1180                                                                                 #endif
1181                                                                                                         }
1182                 | FORWARD_TLS LPAREN URIHOST RPAREN {
1183                                                                                 #ifdef USE_TLS
1184                                                                                         $$=mk_action(   FORWARD_TLS_T,
1185                                                                                                                          URIHOST_ST,
1186                                                                                                                          NUMBER_ST,
1187                                                                                                                         0,
1188                                                                                                                         0);
1189                                                                                 #else
1190                                                                                         yyerror("tls support not "
1191                                                                                                         "compiled in");
1192                                                                                 #endif
1193                                                                                 }
1194                 | FORWARD_TLS error { $$=0; yyerror("missing '(' or ')' ?"); }
1195                 | FORWARD_TLS LPAREN error RPAREN { $$=0; yyerror("bad forward_tls"
1196                                                                                 "argument"); }
1197                 
1198                 | SEND LPAREN host RPAREN       { $$=mk_action( SEND_T,
1199                                                                                                         STRING_ST,
1200                                                                                                         NUMBER_ST,
1201                                                                                                         $3,
1202                                                                                                         0);
1203                                                                         }
1204                 | SEND LPAREN STRING RPAREN { $$=mk_action(     SEND_T,
1205                                                                                                         STRING_ST,
1206                                                                                                         NUMBER_ST,
1207                                                                                                         $3,
1208                                                                                                         0);
1209                                                                         }
1210                 | SEND LPAREN ip RPAREN         { $$=mk_action( SEND_T,
1211                                                                                                         IP_ST,
1212                                                                                                         NUMBER_ST,
1213                                                                                                         (void*)$3,
1214                                                                                                         0);
1215                                                                         }
1216                 | SEND LPAREN host COMMA NUMBER RPAREN  { $$=mk_action( SEND_T,
1217                                                                                                                                 STRING_ST,
1218                                                                                                                                 NUMBER_ST,
1219                                                                                                                                 $3,
1220                                                                                                                                 (void*)$5);
1221                                                                                                 }
1222                 | SEND LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action( SEND_T,
1223                                                                                                                                 STRING_ST,
1224                                                                                                                                 NUMBER_ST,
1225                                                                                                                                 $3,
1226                                                                                                                                 (void*)$5);
1227                                                                                                 }
1228                 | SEND LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(    SEND_T,
1229                                                                                                                                 IP_ST,
1230                                                                                                                                 NUMBER_ST,
1231                                                                                                                                 (void*)$3,
1232                                                                                                                                 (void*)$5);
1233                                                                                            }
1234                 | SEND error { $$=0; yyerror("missing '(' or ')' ?"); }
1235                 | SEND LPAREN error RPAREN { $$=0; yyerror("bad send"
1236                                                                                                         "argument"); }
1237                 | SEND_TCP LPAREN host RPAREN   { $$=mk_action( SEND_TCP_T,
1238                                                                                                         STRING_ST,
1239                                                                                                         NUMBER_ST,
1240                                                                                                         $3,
1241                                                                                                         0);
1242                                                                         }
1243                 | SEND_TCP LPAREN STRING RPAREN { $$=mk_action( SEND_TCP_T,
1244                                                                                                         STRING_ST,
1245                                                                                                         NUMBER_ST,
1246                                                                                                         $3,
1247                                                                                                         0);
1248                                                                         }
1249                 | SEND_TCP LPAREN ip RPAREN             { $$=mk_action( SEND_TCP_T,
1250                                                                                                         IP_ST,
1251                                                                                                         NUMBER_ST,
1252                                                                                                         (void*)$3,
1253                                                                                                         0);
1254                                                                         }
1255                 | SEND_TCP LPAREN host COMMA NUMBER RPAREN      { $$=mk_action( SEND_TCP_T,
1256                                                                                                                                 STRING_ST,
1257                                                                                                                                 NUMBER_ST,
1258                                                                                                                                 $3,
1259                                                                                                                                 (void*)$5);
1260                                                                                                 }
1261                 | SEND_TCP LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(     SEND_TCP_T,
1262                                                                                                                                 STRING_ST,
1263                                                                                                                                 NUMBER_ST,
1264                                                                                                                                 $3,
1265                                                                                                                                 (void*)$5);
1266                                                                                                 }
1267                 | SEND_TCP LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(        SEND_TCP_T,
1268                                                                                                                                 IP_ST,
1269                                                                                                                                 NUMBER_ST,
1270                                                                                                                                 (void*)$3,
1271                                                                                                                                 (void*)$5);
1272                                                                                            }
1273                 | SEND_TCP error { $$=0; yyerror("missing '(' or ')' ?"); }
1274                 | SEND_TCP LPAREN error RPAREN { $$=0; yyerror("bad send_tcp"
1275                                                                                                         "argument"); }
1276                 | DROP LPAREN RPAREN    {$$=mk_action(DROP_T,0, 0, 0, 0); }
1277                 | DROP                                  {$$=mk_action(DROP_T,0, 0, 0, 0); }
1278                 | LOG_TOK LPAREN STRING RPAREN  {$$=mk_action(  LOG_T, NUMBER_ST, 
1279                                                                                                         STRING_ST,(void*)4,$3);
1280                                                                         }
1281                 | LOG_TOK LPAREN NUMBER COMMA STRING RPAREN     {$$=mk_action(  LOG_T,
1282                                                                                                                                 NUMBER_ST, 
1283                                                                                                                                 STRING_ST,
1284                                                                                                                                 (void*)$3,
1285                                                                                                                                 $5);
1286                                                                                                 }
1287                 | LOG_TOK error { $$=0; yyerror("missing '(' or ')' ?"); }
1288                 | LOG_TOK LPAREN error RPAREN { $$=0; yyerror("bad log"
1289                                                                         "argument"); }
1290                 | SETFLAG LPAREN NUMBER RPAREN {$$=mk_action( SETFLAG_T, NUMBER_ST, 0,
1291                                                                                                         (void *)$3, 0 ); }
1292                 | SETFLAG error { $$=0; yyerror("missing '(' or ')'?"); }
1293                 | RESETFLAG LPAREN NUMBER RPAREN {$$=mk_action( RESETFLAG_T, NUMBER_ST, 0,
1294                                                                                                         (void *)$3, 0 ); }
1295                 | RESETFLAG error { $$=0; yyerror("missing '(' or ')'?"); }
1296                 | ISFLAGSET LPAREN NUMBER RPAREN {$$=mk_action( ISFLAGSET_T, NUMBER_ST, 0,
1297                                                                                                         (void *)$3, 0 ); }
1298                 | ISFLAGSET error { $$=0; yyerror("missing '(' or ')'?"); }
1299                 | ERROR LPAREN STRING COMMA STRING RPAREN {$$=mk_action(ERROR_T,
1300                                                                                                                                 STRING_ST, 
1301                                                                                                                                 STRING_ST,
1302                                                                                                                                 $3,
1303                                                                                                                                 $5);
1304                                                                                                   }
1305                 | ERROR error { $$=0; yyerror("missing '(' or ')' ?"); }
1306                 | ERROR LPAREN error RPAREN { $$=0; yyerror("bad error"
1307                                                                                                                 "argument"); }
1308                 | ROUTE LPAREN NUMBER RPAREN    { $$=mk_action(ROUTE_T, NUMBER_ST,
1309                                                                                                                 0, (void*)$3, 0);
1310                                                                                 }
1311                 | ROUTE error { $$=0; yyerror("missing '(' or ')' ?"); }
1312                 | ROUTE LPAREN error RPAREN { $$=0; yyerror("bad route"
1313                                                 "argument"); }
1314                 | EXEC LPAREN STRING RPAREN     { $$=mk_action( EXEC_T, STRING_ST, 0,
1315                                                                                                         $3, 0);
1316                                                                         }
1317                 | SET_HOST LPAREN STRING RPAREN { $$=mk_action(SET_HOST_T, STRING_ST,
1318                                                                                                                 0, $3, 0); }
1319                 | SET_HOST error { $$=0; yyerror("missing '(' or ')' ?"); }
1320                 | SET_HOST LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1321                                                                                                                 "string expected"); }
1322
1323                 | PREFIX LPAREN STRING RPAREN { $$=mk_action(PREFIX_T, STRING_ST,
1324                                                                                                                 0, $3, 0); }
1325                 | PREFIX error { $$=0; yyerror("missing '(' or ')' ?"); }
1326                 | PREFIX LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1327                                                                                                                 "string expected"); }
1328                 | STRIP_TAIL LPAREN NUMBER RPAREN { $$=mk_action(STRIP_TAIL_T, 
1329                                                                         NUMBER_ST, 0, (void *) $3, 0); }
1330                 | STRIP_TAIL error { $$=0; yyerror("missing '(' or ')' ?"); }
1331                 | STRIP_TAIL LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1332                                                                                                                 "number expected"); }
1333
1334                 | STRIP LPAREN NUMBER RPAREN { $$=mk_action(STRIP_T, NUMBER_ST,
1335                                                                                                                 0, (void *) $3, 0); }
1336                 | STRIP error { $$=0; yyerror("missing '(' or ')' ?"); }
1337                 | STRIP LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1338                                                                                                                 "number expected"); }
1339
1340                 | APPEND_BRANCH LPAREN STRING RPAREN { $$=mk_action( APPEND_BRANCH_T,
1341                                                                                                         STRING_ST, 0, $3, 0) ; }
1342                 | APPEND_BRANCH LPAREN RPAREN { $$=mk_action( APPEND_BRANCH_T,
1343                                                                                                         STRING_ST, 0, 0, 0 ) ; }
1344                 | APPEND_BRANCH {  $$=mk_action( APPEND_BRANCH_T, STRING_ST, 0, 0, 0 ) ; }
1345
1346                 | SET_HOSTPORT LPAREN STRING RPAREN { $$=mk_action( SET_HOSTPORT_T, 
1347                                                                                                                 STRING_ST, 0, $3, 0); }
1348                 | SET_HOSTPORT error { $$=0; yyerror("missing '(' or ')' ?"); }
1349                 | SET_HOSTPORT LPAREN error RPAREN { $$=0; yyerror("bad argument,"
1350                                                                                                 " string expected"); }
1351                 | SET_PORT LPAREN STRING RPAREN { $$=mk_action( SET_PORT_T, STRING_ST,
1352                                                                                                                 0, $3, 0); }
1353                 | SET_PORT error { $$=0; yyerror("missing '(' or ')' ?"); }
1354                 | SET_PORT LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1355                                                                                                                 "string expected"); }
1356                 | SET_USER LPAREN STRING RPAREN { $$=mk_action( SET_USER_T, STRING_ST,
1357                                                                                                                 0, $3, 0); }
1358                 | SET_USER error { $$=0; yyerror("missing '(' or ')' ?"); }
1359                 | SET_USER LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1360                                                                                                                 "string expected"); }
1361                 | SET_USERPASS LPAREN STRING RPAREN { $$=mk_action( SET_USERPASS_T, 
1362                                                                                                                 STRING_ST, 0, $3, 0); }
1363                 | SET_USERPASS error { $$=0; yyerror("missing '(' or ')' ?"); }
1364                 | SET_USERPASS LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1365                                                                                                                 "string expected"); }
1366                 | SET_URI LPAREN STRING RPAREN { $$=mk_action( SET_URI_T, STRING_ST, 
1367                                                                                                                 0, $3, 0); }
1368                 | SET_URI error { $$=0; yyerror("missing '(' or ')' ?"); }
1369                 | SET_URI LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1370                                                                                 "string expected"); }
1371                 | REVERT_URI LPAREN RPAREN { $$=mk_action( REVERT_URI_T, 0,0,0,0); }
1372                 | REVERT_URI { $$=mk_action( REVERT_URI_T, 0,0,0,0); }
1373                 | FORCE_RPORT LPAREN RPAREN     {$$=mk_action(FORCE_RPORT_T,0, 0, 0, 0); }
1374                 | FORCE_RPORT                           {$$=mk_action(FORCE_RPORT_T,0, 0, 0, 0); }
1375                 | SET_ADV_ADDRESS LPAREN listen_id RPAREN {
1376                                                                 $$=0;
1377                                                                 if ((str_tmp=pkg_malloc(sizeof(str)))==0){
1378                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
1379                                                                                                         " out of memory.\n");
1380                                                                 }else{
1381                                                                                 str_tmp->s=$3;
1382                                                                                 str_tmp->len=strlen($3);
1383                                                                                 $$=mk_action(SET_ADV_ADDR_T, STR_ST,
1384                                                                                              0, str_tmp, 0);
1385                                                                 }
1386                                                                                                   }
1387                 | SET_ADV_ADDRESS LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1388                                                                                                                 "string expected"); }
1389                 | SET_ADV_ADDRESS error {$$=0; yyerror("missing '(' or ')' ?"); }
1390                 | SET_ADV_PORT LPAREN NUMBER RPAREN {
1391                                                                 $$=0;
1392                                                                 tmp=int2str($3, &i_tmp);
1393                                                                 if ((str_tmp=pkg_malloc(sizeof(str)))==0){
1394                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
1395                                                                                                         " out of memory.\n");
1396                                                                 }else{
1397                                                                         if ((str_tmp->s=pkg_malloc(i_tmp))==0){
1398                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
1399                                                                                                         " out of memory.\n");
1400                                                                         }else{
1401                                                                                 memcpy(str_tmp->s, tmp, i_tmp);
1402                                                                                 str_tmp->len=i_tmp;
1403                                                                                 $$=mk_action(SET_ADV_PORT_T, STR_ST,
1404                                                                                                         0, str_tmp, 0);
1405                                                                         }
1406                                                                 }
1407                                                                             }
1408                 | SET_ADV_PORT LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1409                                                                                                                 "string expected"); }
1410                 | SET_ADV_PORT  error {$$=0; yyerror("missing '(' or ')' ?"); }
1411                 | ID LPAREN RPAREN                      { f_tmp=(void*)find_export($1, 0, rt);
1412                                                                            if (f_tmp==0){
1413                                                                                    if (find_export($1, 0, 0)) {
1414                                                                                            yyerror("Command cannot be used in the block\n");
1415                                                                                    } else {
1416                                                                                            yyerror("unknown command, missing"
1417                                                                                                    " loadmodule?\n");
1418                                                                                    }
1419                                                                                 $$=0;
1420                                                                            }else{
1421                                                                                 $$=mk_action(   MODULE_T,
1422                                                                                                                 CMDF_ST,
1423                                                                                                                 0,
1424                                                                                                                 f_tmp,
1425                                                                                                                 0
1426                                                                                                         );
1427                                                                            }
1428                                                                         }
1429                 | ID LPAREN STRING RPAREN { f_tmp=(void*)find_export($1, 1, rt);
1430                                                                         if (f_tmp==0){
1431                                                                                 if (find_export($1, 1, 0)) {
1432                                                                                         yyerror("Command cannot be used in the block\n");
1433                                                                                 } else {
1434                                                                                         yyerror("unknown command, missing"
1435                                                                                                 " loadmodule?\n");
1436                                                                                 }
1437                                                                                 $$=0;
1438                                                                         }else{
1439                                                                                 $$=mk_action(   MODULE_T,
1440                                                                                                                 CMDF_ST,
1441                                                                                                                 STRING_ST,
1442                                                                                                                 f_tmp,
1443                                                                                                                 $3
1444                                                                                                         );
1445                                                                         }
1446                                                                   }
1447                 | ID LPAREN STRING  COMMA STRING RPAREN 
1448                                                                   { f_tmp=(void*)find_export($1, 2, rt);
1449                                                                         if (f_tmp==0){
1450                                                                                 if (find_export($1, 2, 0)) {
1451                                                                                         yyerror("Command cannot be used in the block\n");
1452                                                                                 } else {
1453                                                                                         yyerror("unknown command, missing"
1454                                                                                                 " loadmodule?\n");
1455                                                                                 }
1456                                                                                 $$=0;
1457                                                                         }else{
1458                                                                                 $$=mk_action3(  MODULE_T,
1459                                                                                                                 CMDF_ST,
1460                                                                                                                 STRING_ST,
1461                                                                                                                 STRING_ST,
1462                                                                                                                 f_tmp,
1463                                                                                                                 $3,
1464                                                                                                                 $5
1465                                                                                                         );
1466                                                                         }
1467                                                                   }
1468                 | ID LPAREN error RPAREN { $$=0; yyerror("bad arguments"); }
1469         ;
1470
1471
1472 %%
1473
1474 extern int line;
1475 extern int column;
1476 extern int startcolumn;
1477 void warn(char* s)
1478 {
1479         LOG(L_WARN, "cfg. warning: (%d,%d-%d): %s\n", line, startcolumn, 
1480                         column, s);
1481         cfg_errors++;
1482 }
1483
1484 void yyerror(char* s)
1485 {
1486         LOG(L_CRIT, "parse error (%d,%d-%d): %s\n", line, startcolumn, 
1487                         column, s);
1488         cfg_errors++;
1489 }
1490
1491
1492 static struct id_list* mk_listen_id(char* host, int proto, int port)
1493 {
1494         struct id_list* l;
1495         l=pkg_malloc(sizeof(struct id_list));
1496         if (l==0){
1497                 LOG(L_CRIT,"ERROR: cfg. parser: out of memory.\n");
1498         }else{
1499                 l->name=host;
1500                 l->port=port;
1501                 l->proto=proto;
1502                 l->next=0;
1503         }
1504         return l;
1505 }
1506
1507
1508 /*
1509 int main(int argc, char ** argv)
1510 {
1511         if (yyparse()!=0)
1512                 fprintf(stderr, "parsing error\n");
1513 }
1514 */