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