Browse code

New script element, Jan's select function framework (@xxx), extended for modular use. In the script it can be used as value assigned to AVP and in the string comparision, RE matching, like: $my_avp=@my.function[1]; if (@via.protocol=="UDP")...

The name to called function conversion is implemented as tree in a table,
modules can register their own tables in module initialization function.

Description of the construction of the table expected to follow soon in the
documentation files...

Michal Matyska authored on 20/12/2005 00:49:32
Showing 10 changed files
... ...
@@ -770,6 +770,20 @@ int do_action(struct action* a, struct sip_msg* msg)
770 770
 				}
771 771
 				ret = 1;
772 772
 				break;
773
+			} else if (a->p2_type == SELECT_ST) {
774
+				int r;
775
+				r = run_select(&value.s, a->p2.select, msg);
776
+				if (r < 0) {
777
+					ret=E_UNSPEC;
778
+					break;
779
+				} else if (r > 0) {
780
+					value.s.s = "";
781
+					value.s.len = 0;
782
+				}
783
+
784
+				flags = a->p1.attr->type | AVP_VAL_STR;
785
+				name = a->p1.attr->name;
786
+				ret = 1;
773 787
 			} else {
774 788
 				LOG(L_CRIT, "BUG: do_action: Bad right side of avp assignment\n");
775 789
 				ret=E_BUG;
... ...
@@ -70,6 +70,7 @@
70 70
 	#include <stdlib.h>
71 71
 	#include "ip_addr.h"
72 72
 	#include "usr_avp.h"
73
+	#include "select.h"
73 74
 	#include "cfg.tab.h"
74 75
 
75 76
 	/* states */
... ...
@@ -78,6 +79,7 @@
78 78
 	#define COMMENT_LN_S	        2
79 79
 	#define STRING_S		3
80 80
 	#define ATTR_S                  4
81
+        #define SELECT_S                5
81 82
 
82 83
 	#define STR_BUF_ALLOC_UNIT	128
83 84
 	struct str_buf{
... ...
@@ -102,7 +104,7 @@
102 102
 %}
103 103
 
104 104
 /* start conditions */
105
-%x STRING1 STRING2 COMMENT COMMENT_LN ATTR
105
+%x STRING1 STRING2 COMMENT COMMENT_LN ATTR SELECT
106 106
 
107 107
 /* action keywords */
108 108
 FORWARD	forward
... ...
@@ -194,7 +196,8 @@ PLUS	"+"
194 194
 MINUS	"-"
195 195
 
196 196
 /* Attribute specification */
197
-ATTR_MARK   "$"|"%"|"@"
197
+ATTR_MARK   "$"|"%"
198
+SELECT_MARK  "@"
198 199
 ATTR_FROM   "from"|"f"
199 200
 ATTR_TO     "to"|"t"
200 201
 ATTR_USER   "user"|"u"
... ...
@@ -501,6 +504,22 @@ EAT_ABLE	[\ \t\b\r]
501 501
 <INITIAL>{PLUS}		{ count(); return PLUS; }
502 502
 <INITIAL>{MINUS}	{ count(); return MINUS; }
503 503
 
504
+<INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
505
+<SELECT>{ID}		{ count(); addstr(&s_buf, yytext, yyleng); 
506
+                          yylval.strval=s_buf.s;
507
+                          memset(&s_buf, 0, sizeof(s_buf));
508
+                          return ID; 
509
+                        }
510
+<SELECT>{DOT}           { count(); return DOT; }
511
+<SELECT>{LBRACK}        { count(); return LBRACK; }
512
+<SELECT>{RBRACK}        { count(); return RBRACK; }
513
+<SELECT>{DECNUMBER}	{ count(); yylval.intval=atoi(yytext);return NUMBER; }
514
+<SELECT>{HEXNUMBER}	{ count(); yylval.intval=(int)strtol(yytext, 0, 16); return NUMBER; }
515
+<SELECT>{OCTNUMBER}	{ count(); yylval.intval=(int)strtol(yytext, 0, 8); return NUMBER; }
516
+<SELECT>{BINNUMBER}     { count(); yylval.intval=(int)strtol(yytext, 0, 2); return NUMBER; }
517
+<SELECT>.               { unput(yytext[0]); state = INITIAL_S; BEGIN(INITIAL); } /* Rescan the token in INITIAL state */
518
+
519
+
504 520
 <INITIAL>{ATTR_MARK}    { count(); state = ATTR_S; BEGIN(ATTR); return ATTR_MARK; }
505 521
 <ATTR>{ATTR_FROM}       { count(); return ATTR_FROM; }
506 522
 <ATTR>{ATTR_TO}         { count(); return ATTR_TO; }
... ...
@@ -91,6 +91,7 @@
91 91
 #include "name_alias.h"
92 92
 #include "ut.h"
93 93
 #include "dset.h"
94
+#include "select.h"
94 95
 
95 96
 #include "config.h"
96 97
 #ifdef USE_TLS
... ...
@@ -121,6 +122,9 @@ static str* str_tmp;
121 121
 static str s_tmp;
122 122
 static struct ip_addr* ip_tmp;
123 123
 static struct avp_spec* s_attr;
124
+static select_t sel;
125
+static select_t* sel_ptr;
126
+
124 127
 
125 128
 static void warn(char* s);
126 129
 static struct socket_id* mk_listen_id(char*, int, int);
... ...
@@ -137,6 +141,7 @@ static struct socket_id* mk_listen_id(char*, int, int);
137 137
 	struct ip_addr* ipaddr;
138 138
 	struct socket_id* sockid;
139 139
 	struct avp_spec* attr;
140
+	select_t* select;
140 141
 }
141 142
 
142 143
 /* terminals */
... ...
@@ -279,6 +284,7 @@ static struct socket_id* mk_listen_id(char*, int, int);
279 279
 %token TOS
280 280
 
281 281
 %token ATTR_MARK
282
+%token SELECT_MARK
282 283
 %token ATTR_FROM
283 284
 %token ATTR_TO
284 285
 %token ATTR_USER
... ...
@@ -340,6 +346,7 @@ static struct socket_id* mk_listen_id(char*, int, int);
340 340
 %type <attr> attr_id
341 341
 %type <intval> class_id
342 342
 %type <intval> assign_op
343
+%type <select> select_id
343 344
 /*%type <route_el> rules;
344 345
   %type <route_el> rule;
345 346
 */
... ...
@@ -979,6 +986,7 @@ uri_type:	URI			{$$=URI_O;}
979 979
 
980 980
 exp_elem:	METHOD strop STRING	{$$= mk_elem($2, METHOD_O, 0, STRING_ST, $3);}
981 981
                 | METHOD strop attr_id  {$$ = mk_elem($2, METHOD_O, 0, AVP_ST, $3); }
982
+                | METHOD strop select_id {$$ = mk_elem($2, METHOD_O, 0, SELECT_ST, $3); }
982 983
 		| METHOD strop  ID	{$$ = mk_elem($2, METHOD_O, 0, STRING_ST,$3); }
983 984
 		| METHOD strop error { $$=0; yyerror("string expected"); }
984 985
 		| METHOD error	{ $$=0; yyerror("invalid operator,"
... ...
@@ -987,6 +995,7 @@ exp_elem:	METHOD strop STRING	{$$= mk_elem($2, METHOD_O, 0, STRING_ST, $3);}
987 987
 		| uri_type strop STRING	{$$ = mk_elem($2, $1, 0, STRING_ST, $3); }
988 988
                 | uri_type strop host 	{$$ = mk_elem($2, $1, 0, STRING_ST, $3); }
989 989
                 | uri_type strop attr_id {$$ = mk_elem($2, $1, 0, AVP_ST, $3); }
990
+                | uri_type strop select_id {$$ = mk_elem($2, $1, 0, SELECT_ST, $3); }
990 991
                 | uri_type equalop MYSELF {$$=mk_elem($2, $1, 0, MYSELF_ST, 0); }
991 992
 		| uri_type strop error { $$=0; yyerror("string or MYSELF expected"); }
992 993
 		| uri_type error	{ $$=0; yyerror("invalid operator,"
... ...
@@ -1193,9 +1202,15 @@ exp_elem:	METHOD strop STRING	{$$= mk_elem($2, METHOD_O, 0, STRING_ST, $3);}
1193 1193
 
1194 1194
 		| attr_id		{$$=mk_elem( NO_OP, AVP_O, (void*)$1, 0, 0); }
1195 1195
 		| attr_id strop STRING	{$$=mk_elem( $2, AVP_O, (void*)$1, STRING_ST, $3); }
1196
+		| attr_id strop select_id {$$=mk_elem( $2, AVP_O, (void*)$1, SELECT_ST, $3); }
1196 1197
 		| attr_id intop NUMBER	{$$=mk_elem( $2, AVP_O, (void*)$1, NUMBER_ST, (void*)$3); }
1197 1198
 		| attr_id binop NUMBER	{$$=mk_elem( $2, AVP_O, (void*)$1, NUMBER_ST, (void*)$3); }
1198
-		| attr_id strop attr_id {$$=mk_elem( $2, AVP_O, (void*)$1, AVP_ST, (void*)$3); }
1199
+                | attr_id strop attr_id {$$=mk_elem( $2, AVP_O, (void*)$1, AVP_ST, (void*)$3); }
1200
+
1201
+                | select_id                 { $$=mk_elem( NO_OP, SELECT_O, $1, 0, 0); }
1202
+		| select_id strop STRING    { $$=mk_elem( $2, SELECT_O, $1, STRING_ST, $3); }
1203
+		| select_id strop attr_id   { $$=mk_elem( $2, SELECT_O, $1, AVP_ST, (void*)$3); }
1204
+		| select_id strop select_id { $$=mk_elem( $2, SELECT_O, $1, SELECT_ST, $3); }
1199 1205
 ;
1200 1206
 
1201 1207
 
... ...
@@ -1310,39 +1325,77 @@ class_id : LBRACK ATTR_USER RBRACK { $$ = AVP_CLASS_USER; }
1310 1310
          | LBRACK ATTR_GLOBAL RBRACK { $$ = AVP_CLASS_GLOBAL; }
1311 1311
 ;
1312 1312
 
1313
+select_param : ID { 
1314
+		    if (sel.n >= MAX_SELECT_PARAMS-1) {
1315
+			    yyerror("Select identifier too long\n");
1316
+		    }
1317
+		    sel.params[sel.n].type = PARAM_STR; 
1318
+		    sel.params[sel.n].v.s.s = $1;
1319
+		    sel.params[sel.n].v.s.len = strlen($1);
1320
+		    sel.n++;
1321
+                  }
1322
+             | ID LBRACK NUMBER RBRACK {
1323
+		     if (sel.n >= MAX_SELECT_PARAMS-2) {
1324
+			    yyerror("Select identifier too long\n");
1325
+		     }
1326
+		     sel.params[sel.n].type = PARAM_STR;
1327
+		     sel.params[sel.n].v.s.s = $1;
1328
+		     sel.params[sel.n].v.s.len = strlen($1);
1329
+		     sel.n++;
1330
+		     sel.params[sel.n].type = PARAM_INT;
1331
+		     sel.params[sel.n].v.i = $3;
1332
+		     sel.n++;
1333
+	     }
1334
+;
1335
+
1336
+select_params : select_params DOT select_param
1337
+              | select_param
1338
+;
1339
+
1340
+select_id : SELECT_MARK { sel.n = 0; sel.f = 0; } select_params {
1341
+//	if (resolve_select(&sel) < 0) yyerror("Unable to resolve select");
1342
+	sel_ptr = (select_t*)pkg_malloc(sizeof(select_t));
1343
+	if (!sel_ptr) {
1344
+		yyerror("No memory left to allocate select structure\n");
1345
+	}
1346
+	memcpy(sel_ptr, &sel, sizeof(select_t));
1347
+	$$ = sel_ptr;
1348
+}
1349
+;
1350
+
1313 1351
 attr_id : ATTR_MARK ID { s_attr = (struct avp_spec*)pkg_malloc(sizeof(struct avp_spec));
1314
-                         if (!s_attr) { LOG(L_CRIT, "No memory left"); return 0;}
1352
+                         if (!s_attr) { yyerror("No memory left"); }
1315 1353
                          s_attr->type = AVP_NAME_STR;                   
1316 1354
                          s_attr->name.s.s = $2; s_attr->name.s.len = strlen($2); 
1317 1355
                          $$ = s_attr; 
1318 1356
                        }
1319 1357
         | ATTR_MARK class_id DOT ID { s_attr = (struct avp_spec*)pkg_malloc(sizeof(struct avp_spec));
1320
-                                      if (!s_attr) { LOG(L_CRIT, "No memory left"); return 0;}
1358
+                                      if (!s_attr) { yyerror("No memory left"); }
1321 1359
                                       s_attr->type = AVP_NAME_STR | $2;
1322 1360
                                       s_attr->name.s.s = $4; s_attr->name.s.len = strlen($4); 
1323 1361
                                       $$ = s_attr; 
1324 1362
                                     }
1325 1363
         | ATTR_MARK ATTR_FROM DOT ID { s_attr = (struct avp_spec*)pkg_malloc(sizeof(struct avp_spec));
1326
-                                       if (!s_attr) { LOG(L_CRIT, "No memory left"); return 0;}
1364
+                                       if (!s_attr) { yyerror("No memory left"); }
1327 1365
                                        s_attr->type = AVP_NAME_STR | AVP_TRACK_FROM;
1328 1366
                                        s_attr->name.s.s = $4; s_attr->name.s.len = strlen($4);
1329 1367
                                        $$ = s_attr;
1330 1368
                                      }
1331 1369
         | ATTR_MARK ATTR_TO DOT ID { s_attr = (struct avp_spec*)pkg_malloc(sizeof(struct avp_spec));
1332
-                                     if (!s_attr) { LOG(L_CRIT, "No memory left"); return 0;}
1370
+                                     if (!s_attr) { yyerror("No memory left"); }
1333 1371
                                      s_attr->type = AVP_NAME_STR | AVP_TRACK_TO; 
1334 1372
                                      s_attr->name.s.s = $4; s_attr->name.s.len = strlen($4); 
1335 1373
                                      $$ = s_attr;
1336 1374
                                    }
1337 1375
         | ATTR_MARK ATTR_FROM class_id DOT ID { s_attr = (struct avp_spec*)pkg_malloc(sizeof(struct avp_spec));
1338
-                                               if (!s_attr) { LOG(L_CRIT, "No memory left"); return 0;}
1376
+                                               if (!s_attr) { yyerror("No memory left"); }
1339 1377
                                                s_attr->type = AVP_NAME_STR | AVP_TRACK_FROM | $3; 
1340 1378
                                                s_attr->name.s.s = $5; 
1341 1379
                                                s_attr->name.s.len = strlen($5);
1342 1380
                                                $$ = s_attr;
1343 1381
                                               }
1344 1382
         | ATTR_MARK ATTR_TO class_id DOT ID { s_attr = (struct avp_spec*)pkg_malloc(sizeof(struct avp_spec));
1345
-                                              if (!s_attr) { LOG(L_CRIT, "No memory left"); return 0;}
1383
+                                              if (!s_attr) { yyerror("No memory left"); }
1346 1384
                                               s_attr->type = AVP_NAME_STR | AVP_TRACK_TO | $3;
1347 1385
                                               s_attr->name.s.s = $5; s_attr->name.s.len = strlen($5);
1348 1386
                                              $$ = s_attr;
... ...
@@ -1357,6 +1410,7 @@ assign_action:   attr_id assign_op STRING  { $$=mk_action($2, AVP_ST, STRING_ST,
1357 1357
                | attr_id assign_op NUMBER  { $$=mk_action($2, AVP_ST, NUMBER_ST, $1, (void*)$3); }
1358 1358
                | attr_id assign_op fcmd    { $$=mk_action($2, AVP_ST, ACTION_ST, $1, $3); }
1359 1359
                | attr_id assign_op attr_id { $$=mk_action($2, AVP_ST, AVP_ST, $1, $3); }
1360
+               | attr_id assign_op select_id { $$=mk_action($2, AVP_ST, SELECT_ST, (void*)$1, (void*)$3); }
1360 1361
                | attr_id assign_op LPAREN exp RPAREN { $$ = mk_action($2, AVP_ST, EXPR_ST, $1, $4); }
1361 1362
 ;
1362 1363
 
... ...
@@ -67,9 +67,9 @@
67 67
 #include "parser/parse_from.h"
68 68
 #include "parser/parse_to.h"
69 69
 #include "mem/mem.h"
70
+#include "select.h"
70 71
 #include "onsend.h"
71 72
 
72
-
73 73
 /* main routing script table  */
74 74
 struct action* rlist[RT_NO];
75 75
 /* reply routing table */
... ...
@@ -132,7 +132,7 @@ static int fix_expr(struct expr* exp)
132 132
 					pkg_free(exp->r.param);
133 133
 					exp->r.re=re;
134 134
 					exp->r_type=RE_ST;
135
-				}else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST){
135
+				}else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST && exp->r_type != SELECT_ST){
136 136
 					LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
137 137
 					return E_BUG;
138 138
 				}
... ...
@@ -159,6 +159,20 @@ static int fix_expr(struct expr* exp)
159 159
 				exp->r.str.s = exp->r.string;
160 160
 				exp->r.str.len = len;
161 161
 			}
162
+			if (exp->l_type==SELECT_O) {
163
+				if ((ret=resolve_select(exp->l.select)) < 0) {
164
+					BUG("Unable to resolve select\n");
165
+					print_select(exp->l.select);
166
+					return ret;
167
+				}
168
+			}
169
+			if ((exp->r_type==SELECT_O)||(exp->r_type==SELECT_ST)) {
170
+				if ((ret=resolve_select(exp->r.select)) < 0) {
171
+					BUG("Unable to resolve select\n");
172
+					print_select(exp->l.select);
173
+					return ret;
174
+				}
175
+			}
162 176
 			ret=0;
163 177
 	}
164 178
 	return ret;
... ...
@@ -280,6 +294,12 @@ static int fix_actions(struct action* a)
280 280
 					len = strlen(t->p2.data);
281 281
 					t->p2.str.s = t->p2.data;
282 282
 					t->p2.str.len = len;
283
+				} else if (t->p2_type == SELECT_ST) {
284
+					if ((ret=resolve_select(t->p2.select)) < 0) {
285
+						BUG("Unable to resolve select\n");
286
+						print_select(t->p2.select);
287
+						return ret;
288
+					}
283 289
 				}
284 290
 				break;
285 291
 
... ...
@@ -372,10 +392,11 @@ inline static int comp_num(int op, long left, int rtype, union exp_op* r)
372 372
 /*
373 373
  * Compare given string "left" with right side of expression
374 374
  */
375
-inline static int comp_str(int op, str* left, int rtype, union exp_op* r)
375
+inline static int comp_str(int op, str* left, int rtype, union exp_op* r, struct sip_msg* msg)
376 376
 {
377 377
 	str* right;
378 378
 	int_str val;
379
+	str v;
379 380
 	avp_t* avp;
380 381
 	int ret;
381 382
 	char backup;
... ...
@@ -387,6 +408,11 @@ inline static int comp_str(int op, str* left, int rtype, union exp_op* r)
387 387
 		avp = search_first_avp(r->attr->type, r->attr->name, &val, 0);
388 388
 		if (avp && (avp->flags & AVP_VAL_STR)) right = &val.s;
389 389
 		else return 0;
390
+	} else if (rtype == SELECT_ST) {
391
+		ret = run_select(&v, r->select, msg);
392
+		if (ret > 0) return 0;       /* Not found */
393
+		else if (ret < 0) goto error; /* Error */
394
+		right = &v;
390 395
 	} else if ((op == MATCH_OP && rtype == RE_ST)) {
391 396
 	} else if (op != MATCH_OP && rtype == STRING_ST) {
392 397
 		right = &r->str;
... ...
@@ -424,7 +450,7 @@ inline static int comp_str(int op, str* left, int rtype, union exp_op* r)
424 424
 			      */
425 425
 			backup=left->s[left->len];
426 426
 			left->s[left->len]='\0';
427
-			if (rtype == AVP_ST) {
427
+			if (rtype == AVP_ST || rtype == SELECT_ST) {
428 428
 				     /* For AVPs we need to compile the RE on the fly */
429 429
 				re=(regex_t*)pkg_malloc(sizeof(regex_t));
430 430
 				if (re==0){
... ...
@@ -499,7 +525,7 @@ error:
499 499
 }
500 500
 
501 501
 
502
-inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r)
502
+inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r, struct sip_msg* msg)
503 503
 {
504 504
 	avp_t* avp;
505 505
 	int_str val;
... ...
@@ -526,14 +552,33 @@ inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r)
526 526
 	}
527 527
 
528 528
 	if (avp->flags & AVP_VAL_STR) {
529
-		return comp_str(op, &val.s, rtype, r);
529
+		return comp_str(op, &val.s, rtype, r, msg);
530 530
 	} else {
531 531
 		return comp_num(op, val.n, rtype, r);
532 532
 	}
533 533
 }
534 534
 
535
+/*
536
+ * Left side of expression was select
537
+ */
538
+inline static int comp_select(int op, select_t* sel, int rtype, union exp_op* r, struct sip_msg* msg)
539
+{
540
+	int ret;
541
+	str val;
535 542
 
543
+	ret = run_select(&val, sel, msg);
544
+	if (ret < 0) return -1;
545
+	if (ret > 0) return 0;
536 546
 
547
+	switch(op) {
548
+	case NO_OP: return 1;
549
+	case BINOR_OP:
550
+	case BINAND_OP:  
551
+		ERR("Binary operators cannot be used with string selects\n");
552
+		return -1;
553
+	}
554
+	return comp_str(op, &val, rtype, r, msg);
555
+}
537 556
 
538 557
 /* check_self wrapper -- it checks also for the op */
539 558
 inline static int check_self_op(int op, str* s, unsigned short p)
... ...
@@ -657,7 +702,7 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
657 657
 	switch(e->l_type){
658 658
 	case METHOD_O:
659 659
 		ret=comp_str(e->op, &msg->first_line.u.request.method, 
660
-			     e->r_type, &e->r);
660
+			     e->r_type, &e->r, msg);
661 661
 		break;
662 662
 	case URI_O:
663 663
 		if(msg->new_uri.s) {
... ...
@@ -668,7 +713,7 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
668 668
 						       msg->parsed_uri.port_no:SIP_PORT);
669 669
 			}else{
670 670
 				ret=comp_str(e->op, &msg->new_uri, 
671
-					     e->r_type, &e->r);
671
+					     e->r_type, &e->r, msg);
672 672
 			}
673 673
 		}else{
674 674
 			if (e->r_type==MYSELF_ST){
... ...
@@ -678,7 +723,7 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
678 678
 						       msg->parsed_uri.port_no:SIP_PORT);
679 679
 			}else{
680 680
 				ret=comp_str(e->op, &msg->first_line.u.request.uri,
681
-					     e->r_type, &e->r);
681
+					     e->r_type, &e->r, msg);
682 682
 			}
683 683
 		}
684 684
 		break;
... ...
@@ -699,7 +744,7 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
699 699
 					  uri.port_no?uri.port_no:SIP_PORT);
700 700
 		}else{
701 701
 			ret=comp_str(e->op, &get_from(msg)->uri,
702
-				     e->r_type, &e->r);
702
+				     e->r_type, &e->r, msg);
703 703
 		}
704 704
 		break;
705 705
 
... ...
@@ -721,7 +766,7 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
721 721
 					  uri.port_no?uri.port_no:SIP_PORT);
722 722
 		}else{
723 723
 			ret=comp_str(e->op, &get_to(msg)->uri,
724
-				     e->r_type, &e->r);
724
+				     e->r_type, &e->r, msg);
725 725
 		}
726 726
 		break;
727 727
 		
... ...
@@ -837,9 +882,13 @@ static int eval_elem(struct expr* e, struct sip_msg* msg)
837 837
 		break;
838 838
 
839 839
 	case AVP_O:
840
-		ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r);
840
+		ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg);
841 841
 		break;
842 842
 		
843
+	case SELECT_O:
844
+		ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg);
845
+		break;
846
+
843 847
 	default:
844 848
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
845 849
 		    e->l_type);
... ...
@@ -174,6 +174,9 @@ void print_expr(struct expr* exp)
174 174
 		        case AVP_ST:
175 175
 				DBG("attr");
176 176
 				break;
177
+		        case SELECT_ST:
178
+			        DBG("select");
179
+				break;
177 180
 			
178 181
 			default:
179 182
 				DBG("UNKNOWN");
... ...
@@ -228,9 +231,11 @@ void print_expr(struct expr* exp)
228 228
 					DBG("_myself_");
229 229
 					break;
230 230
 		        case AVP_ST:
231
-				DBG("attr");
232
-				break;
233
-			
231
+				        DBG("attr");
232
+			 	        break;
233
+		        case SELECT_ST:
234
+				        DBG("select");
235
+				        break;
234 236
 			default:
235 237
 					DBG("type<%d>", exp->r_type);
236 238
 		}
... ...
@@ -407,6 +412,9 @@ void print_action(struct action* t)
407 407
 	case AVP_ST:
408 408
 		DBG("avp(%u,%.*s)", t->p1.attr->type, t->p1.attr->name.s.len, ZSW(t->p1.attr->name.s.s));
409 409
 		break;
410
+	case SELECT_ST:
411
+		DBG("select");
412
+		break;
410 413
 	default:
411 414
 		DBG("type<%d>", t->p1_type);
412 415
 	}
... ...
@@ -438,6 +446,9 @@ void print_action(struct action* t)
438 438
 	case AVP_ST:
439 439
 		DBG(", avp(%u,%.*s)", t->p2.attr->type, t->p2.attr->name.s.len, ZSW(t->p2.attr->name.s.s));
440 440
 		break;
441
+	case SELECT_ST:
442
+		DBG("select");
443
+		break;
441 444
 	default:
442 445
 		DBG(", type<%d>", t->p2_type);
443 446
 	}
... ...
@@ -41,6 +41,7 @@
41 41
 #define route_struct_h
42 42
 
43 43
 #include <regex.h>
44
+#include "select.h"
44 45
 #include "usr_avp.h"
45 46
 
46 47
 #define EXPR_DROP -127  /* used only by the expression and if evaluator */
... ...
@@ -62,7 +63,7 @@ enum { EQUAL_OP=10, MATCH_OP, GT_OP, LT_OP, GTE_OP, LTE_OP, DIFF_OP, NO_OP };
62 62
 enum { METHOD_O=1, URI_O, FROM_URI_O, TO_URI_O, SRCIP_O, SRCPORT_O,
63 63
 	   DSTIP_O, DSTPORT_O, PROTO_O, AF_O, MSGLEN_O, DEFAULT_O, ACTION_O,
64 64
 	   NUMBER_O, AVP_O, SNDIP_O, SNDPORT_O, TOIP_O, TOPORT_O, SNDPROTO_O, 
65
-	   SNDAF_O, RETCODE_O};
65
+	   SNDAF_O, RETCODE_O, SELECT_O};
66 66
 
67 67
 enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
68 68
 		SET_HOST_T, SET_HOSTPORT_T, SET_USER_T, SET_USERPASS_T, 
... ...
@@ -88,14 +89,13 @@ enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
88 88
 };
89 89
 enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
90 90
 		EXPR_ST, ACTIONS_ST, CMDF_ST, MODFIXUP_ST, URIHOST_ST, URIPORT_ST,
91
-		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
91
+		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST, SELECT_ST,
92 92
 		RETCODE_ST};
93 93
 
94 94
 /* run flags */
95 95
 #define EXIT_R_F   1
96 96
 #define RETURN_R_F 2
97 97
 
98
-
99 98
 /* Expression operand */
100 99
 union exp_op {
101 100
 	struct expr* expr;
... ...
@@ -104,6 +104,7 @@ union exp_op {
104 104
 	void* param;
105 105
 	int intval;
106 106
 	avp_spec_t* attr;
107
+	select_t* select;
107 108
 	regex_t* re;
108 109
 	struct net* net;
109 110
 };
... ...
@@ -122,6 +123,7 @@ typedef union {
122 122
 	str str;
123 123
 	void* data;
124 124
 	avp_spec_t* attr;
125
+	select_t* select;
125 126
 } action_u_t;
126 127
 
127 128
 struct action{
128 129
new file mode 100644
... ...
@@ -0,0 +1,129 @@
0
+#include "select.h"
1
+#include "dprint.h"
2
+#include "select_core.h"
3
+#include "mem/mem.h"
4
+
5
+/*
6
+ * The main parser table list placeholder
7
+ * at startup use core table, modules can
8
+ * add their own via register_select_table call
9
+ */
10
+static select_table_t *select_list = &select_core_table;
11
+
12
+int resolve_select(select_t* s)
13
+{
14
+	select_f f, pf;
15
+	int param_idx = 0;
16
+	int table_idx = 0;
17
+	select_table_t* t = NULL;;
18
+	int accept = 0;
19
+	
20
+	f = pf = NULL;
21
+	while (param_idx<s->n) {
22
+		accept = 0;
23
+		for (t=select_list; t; t=t->next) {
24
+			table_idx = 0;	
25
+			if (!t->table) continue;
26
+			while (t->table[table_idx].curr_f || t->table[table_idx].new_f) {
27
+				if (t->table[table_idx].curr_f == f) {
28
+					if (t->table[table_idx].type == s->params[param_idx].type) {
29
+						switch (t->table[table_idx].type) {
30
+						case PARAM_INT:
31
+							accept = 1;
32
+							break;
33
+							case PARAM_STR:
34
+							accept = (((t->table[table_idx].name.len == s->params[param_idx].v.s.len) || !t->table[table_idx].name.len)
35
+								   && (!t->table[table_idx].name.s || !strncasecmp(t->table[table_idx].name.s, s->params[param_idx].v.s.s, s->params[param_idx].v.s.len)));
36
+							break;
37
+						default:
38
+							break;
39
+						}
40
+					};
41
+					if ((t->table[table_idx].flags & IS_ALIAS)&&(!pf)) {
42
+						accept = 1;
43
+					}
44
+				}
45
+				if (accept) goto accepted;
46
+				table_idx++;
47
+			}
48
+		}
49
+		goto not_found;
50
+
51
+		accepted:
52
+		if (t->table[table_idx].flags & CONSUME_NEXT_STR) {
53
+			if ((param_idx<s->n-1) && (s->params[param_idx+1].type == PARAM_STR)) {
54
+				param_idx++;
55
+			} else if (!(t->table[table_idx].flags & OPTIONAL)) {
56
+				goto not_found;
57
+			}
58
+		}
59
+		if (t->table[table_idx].flags & CONSUME_NEXT_INT) {
60
+			if ((param_idx<s->n-1) && (s->params[param_idx+1].type == PARAM_INT)) {
61
+				param_idx++;
62
+			} else if (!(t->table[table_idx].flags & OPTIONAL)) {
63
+				goto not_found;
64
+			}
65
+		}
66
+		if (t->table[table_idx].flags & IS_ALIAS) {
67
+			pf = f;
68
+		} else {
69
+			param_idx++;
70
+		}
71
+		f = t->table[table_idx].new_f;
72
+	}
73
+
74
+	if (t->table[table_idx].flags & PARAM_EXPECTED) goto not_found;
75
+	s->f = f;
76
+	s->parent_f = pf;
77
+	return 0;
78
+	
79
+not_found:
80
+	return -1;
81
+}
82
+
83
+int run_select(str* res, select_t* s, struct sip_msg* msg)
84
+{
85
+	if (res == NULL) {
86
+		BUG("Select unprepared result space\n");
87
+		return -1;
88
+	}
89
+	if (s == 0) {
90
+		BUG("Select structure is NULL\n");
91
+		return -1;
92
+	}
93
+	if (s->f == 0) {
94
+		BUG("Select structure has not been resolved\n");
95
+		return -1;
96
+	}
97
+DBG("Calling SELECT %p \n", s->f);
98
+	return s->f(res, s, msg);
99
+}
100
+
101
+void print_select(select_t* s)
102
+{
103
+	int i;
104
+	DBG("select(");
105
+	for(i = 0; i < s->n; i++) {
106
+		if (s->params[i].type == PARAM_INT) {
107
+			DBG("%d,", s->params[i].v.i);
108
+		} else {
109
+			DBG("%.*s,", s->params[i].v.s.len, s->params[i].v.s.s);
110
+		}
111
+	}
112
+	DBG(")\n");
113
+}
114
+
115
+int register_select_table(select_row_t* mod_tab)
116
+{
117
+	select_table_t* t;
118
+	t=(select_table_t*)pkg_malloc(sizeof(select_table_t));
119
+	if (!t) {
120
+		ERR("No memory for new select_table structure\n");
121
+		return -1;
122
+	}
123
+	
124
+	t->table=mod_tab;
125
+	t->next=select_list;
126
+	select_list=t;
127
+	return 0;
128
+}
0 129
new file mode 100644
... ...
@@ -0,0 +1,95 @@
0
+#ifndef _SELECT_H
1
+#define _SELECT_H
2
+
3
+#include "str.h"
4
+#include "parser/msg_parser.h"
5
+
6
+#define MAX_SELECT_PARAMS 32
7
+
8
+// Flags for parser table FLAG bitfiels
9
+#define DIVERSION_MASK   0x00FF
10
+// if DIVERSION is set and the function is accepted and has STR param
11
+// the param is changed into PARAM_DIV and the value is set to (flags & DIVERSION_MASK)
12
+#define DIVERSION        1<<8
13
+// if any parameter is expected at this stage
14
+#define PARAM_EXPECTED   1<<9
15
+// accept if following parameter is STR (any)
16
+#define CONSUME_NEXT_STR 1<<10
17
+// accept if following parameter is INT
18
+#define CONSUME_NEXT_INT 1<<11
19
+// next parameter is optional (use with CONSUME_NEXT_STR or CONSUME_NEXT_INT
20
+#define OPTIONAL         1<<12
21
+// if conversion to common alias is needed
22
+// up-to now parsed function would be stored in parent_f
23
+// NOTE: the parameter is not consumed for ALIAS, 
24
+// so you can leave it as ..,PARAM_INT, STR_NULL,..
25
+#define IS_ALIAS         1<<13
26
+
27
+/*
28
+ * Selector call parameter
29
+ */
30
+typedef enum {
31
+	PARAM_INT,  /* Integer parameter */
32
+	PARAM_STR,  /* String parameter */
33
+	PARAM_DIV,  /* Integer value got from parsing table */
34
+} select_param_type_t;
35
+	
36
+typedef union {
37
+	int i;  /* Integer value */
38
+	str s;  /* String value */
39
+} select_param_value_t;
40
+	
41
+typedef struct sel_param {
42
+        select_param_type_t type;
43
+        select_param_value_t v;
44
+} select_param_t;
45
+
46
+struct select;
47
+
48
+typedef int (*select_f)(str* res, struct select* s, struct sip_msg* msg);
49
+
50
+typedef struct select {
51
+	select_f f;
52
+	select_f parent_f;
53
+	select_param_t params[MAX_SELECT_PARAMS];
54
+	int n;
55
+} select_t;
56
+
57
+typedef struct {
58
+	select_f curr_f;
59
+	select_param_type_t type;
60
+	str name;
61
+	select_f new_f;
62
+	int flags;
63
+} select_row_t;
64
+
65
+typedef struct select_table {
66
+  select_row_t *table;
67
+  struct select_table *next;
68
+} select_table_t;
69
+
70
+/*
71
+ * Lookup corresponding select function based on
72
+ * the select parameters
73
+ */
74
+int resolve_select(select_t* s);
75
+
76
+/*
77
+ * Run the select function
78
+ */
79
+int run_select(str* res, select_t* s, struct sip_msg* msg);
80
+
81
+/*
82
+ * Print select for debugging purposes 
83
+ */
84
+void print_select(select_t* s);
85
+
86
+/*
87
+ * Register modules' own select parser table
88
+ */
89
+int register_select_table(select_row_t *table);
90
+
91
+#define SELECT_F(function) extern int function (str* res, select_t* s, struct sip_msg* msg);
92
+#define ABSTRACT_F(function) int function (str* res, select_t* s, struct sip_msg* msg) {return -1;}
93
+
94
+#endif /* _SELECT_H */
0 95
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+#include "select.h"
1
+#include "select_core.h"
2
+
0 3
new file mode 100644
... ...
@@ -0,0 +1,14 @@
0
+#ifndef _SELECT_CORE_H
1
+#define _SELECT_CORE_H
2
+
3
+#include "str.h"
4
+#include "parser/msg_parser.h"
5
+#include "select.h"
6
+
7
+static select_row_t select_core[] = {
8
+	{ NULL, PARAM_INT, STR_NULL, NULL, 0}
9
+};
10
+
11
+static select_table_t select_core_table = {select_core, NULL};
12
+
13
+#endif // _SELECT_CORE_H