File: route.c

package info (click to toggle)
kamailio 4.2.0-2+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (2183 lines) | stat: -rw-r--r-- 55,560 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
/*
 * $Id$
 *
 * SIP routing engine
 *
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * History:
 * --------
 *  2003-01-28  scratchpad removed, src_port introduced (jiri)
 *  2003-02-28  scratchpad compatibility abandoned (jiri)
 *  2003-03-10  updated to the new module exports format (andrei)
 *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
 *  2003-04-01  added dst_port, proto, af; renamed comp_port to comp_no,
 *               inlined all the comp_* functions (andrei)
 *  2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
 *  2003-05-23  comp_ip fixed, now it will resolve its operand and compare
 *              the ip with all the addresses (andrei)
 *  2003-10-10  added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
 *  2004-10-19  added from_uri & to_uri (andrei)
 *  2005-12-12  added retcode support (anrei)
 *  2005-12-19  select framework (mma)
 *  2006-01-30  removed rec. protection from eval_expr (andrei)
 *  2006-02-06  added named route tables (andrei)
 *  2008-04-14  (expr1 != expr2) is evaluated true if at least one of
 *		the expressions does not exist (Miklos)
 *  2008-04-23  errors are treated as false during expression evaluation
 *  		unless the operator is DIFF_OP (Miklos)
 *  2008-12-03  fixups for rvalues in assignments (andrei)
 *  2009-05-04  switched IF_T to rval_expr (andrei)
 *  2010-06-01  special hack/support for fparam fixups so that they can handle
 *               variable RVEs (andrei)
 *  2010-06-18  ip comparison (comp_ip()) normalizes strings to
 *              ip/netmask  (andrei)
 */


/** expression evaluation, route fixups and routing lists.
 * @file route.c
 * @ingroup core
 * Module: @ref core
 */

#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "route.h"
#include "forward.h"
#include "dprint.h"
#include "proxy.h"
#include "action.h"
#include "lvalue.h"
#include "rvalue.h"
#include "sr_module.h"
#include "ip_addr.h"
#include "resolve.h"
#include "socket_info.h"
#include "parser/parse_uri.h"
#include "parser/parse_from.h"
#include "parser/parse_to.h"
#include "mem/mem.h"
#include "select.h"
#include "onsend.h"
#include "str_hash.h"
#include "ut.h"
#include "rvalue.h"
#include "switch.h"
#include "cfg/cfg_struct.h"

#define RT_HASH_SIZE	8 /* route names hash */

/* main routing script table  */
struct route_list main_rt;
struct route_list onreply_rt;
struct route_list failure_rt;
struct route_list branch_rt;
struct route_list onsend_rt;
struct route_list event_rt;

int route_type = REQUEST_ROUTE;

/** script optimization level, useful for debugging.
 *  0 - no optimization
 *  1 - optimize rval expressions
 *  2 - optimize expr elems
 */
int scr_opt_lev=9;

inline static void destroy_rlist(struct route_list* rt)
{
	struct str_hash_entry* e;
	struct str_hash_entry* tmp;

	if (rt->rlist){
		pkg_free(rt->rlist);
		rt->rlist=0;
		rt->entries=0;
	}
	if (rt->names.table){
		clist_foreach_safe(rt->names.table, e, tmp, next){
			pkg_free(e);
		}
		pkg_free(rt->names.table);
		rt->names.table=0;
		rt->names.size=0;
	}
}



void destroy_routes()
{
	destroy_rlist(&main_rt);
	destroy_rlist(&onreply_rt);
	destroy_rlist(&failure_rt);
	destroy_rlist(&branch_rt);
	destroy_rlist(&event_rt);
}



/* adds route name -> i mapping
 * WARNING: it doesn't check for pre-existing routes 
 * return -1 on error, route index on success
 */
static int route_add(struct route_list* rt, char* name, int i)
{
	struct str_hash_entry* e;
	
	e=pkg_malloc(sizeof(struct str_hash_entry));
	if (e==0){
		LM_CRIT("out of memory\n");
		goto error;
	}
	LM_DBG("mapping routing block (%p)[%s] to %d\n", rt, name, i);
	e->key.s=name;
	e->key.len=strlen(name);
	e->flags=0;
	e->u.n=i;
	str_hash_add(&rt->names, e);
	return 0;
error:
	return -1;
}



/* returns -1 on error, 0 on success */
inline  static int init_rlist(char* r_name, struct route_list* rt,
								int n_entries, int hash_size)
{
		rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
		if (rt->rlist==0){ 
			LM_CRIT("failed to allocate \"%s\" route tables: " 
					"out of memory\n", r_name); 
			goto error; 
		}
		memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
		rt->idx=1; /* idx=0 == default == reserved */
		rt->entries=n_entries;
		if (str_hash_alloc(&rt->names, hash_size)<0){
			LM_CRIT("\"%s\" route table: failed to alloc hash\n",
					r_name);
			goto error;
		}
		str_hash_init(&rt->names);
		route_add(rt, "0", 0);  /* default route */
		
		return 0;
error:
		return -1;
}



/* init route tables */
int init_routes()
{
	if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
		goto error;
	if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
		goto error;
	if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
		goto error;
	if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
		goto error;
	if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
		goto error;
	if (init_rlist("event", &event_rt, EVENT_RT_NO, RT_HASH_SIZE)<0)
		goto error;
	return 0;
error:
	destroy_routes();
	return -1;
}



static inline int route_new_list(struct route_list* rt)
{
	int ret;
	struct action** tmp;
	
	ret=-1;
	if (rt->idx >= rt->entries){
		tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
		if (tmp==0){
			LM_CRIT("out of memory\n");
			goto end;
		}
		/* init the newly allocated memory chunk */
		memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
		rt->rlist=tmp;
		rt->entries*=2;
	}
	if (rt->idx<rt->entries){
		ret=rt->idx;
		rt->idx++;
	}
end:
	return ret;
}




/* 
 * if the "name" route already exists, return its index, else
 * create a new empty route
 * return route index in rt->rlist or -1 on error
 */
int route_get(struct route_list* rt, char* name)
{
	int len;
	struct str_hash_entry* e;
	int i;
	
	len=strlen(name);
	/* check if exists an non empty*/
	e=str_hash_get(&rt->names, name, len);
	if (e){
		i=e->u.n;
	}else{
		i=route_new_list(rt);
		if (i==-1) goto error;
		if (route_add(rt, name, i)<0){
			goto error;
		}
	}
	return i;
error:
	return -1;
}



/* 
 * if the "name" route already exists, return its index, else
 * return error
 * return route index in rt->rlist or -1 on error
 */
int route_lookup(struct route_list* rt, char* name)
{
	int len;
	struct str_hash_entry* e;
	
	len=strlen(name);
	/* check if exists an non empty*/
	e=str_hash_get(&rt->names, name, len);
	if (e){
		return e->u.n;
	}else{
		return -1;
	}
}



int fix_actions(struct action* a); /*fwd declaration*/


/** optimize the left side of a struct expr.
 *  @return 1 if optimized, 0 if not and -1 on error
 */
static int exp_optimize_left(struct expr* exp)
{
	struct rval_expr* rve;
	struct rvalue* rval;
	int old_ltype, old_rtype, old_op;
	int ret;
	
	ret=0;
	if (exp->type!=ELEM_T)
		return 0;
	old_ltype=exp->l_type;
	old_rtype=exp->r_type;
	old_op=exp->op;
	if (exp->l_type==RVEXP_O){
		rve=exp->l.param;
		/* rve should be previously fixed/optimized */
		/* optimize exp (rval(val)) -> exp(val) */
		if (rve->op==RVE_RVAL_OP){
			rval=&rve->left.rval;
			switch(rval->type){
				case RV_INT:
					if (exp->op==NO_OP){
						exp->l_type=NUMBER_O;
						exp->l.param=0;
						exp->r_type=NUMBER_ST;
						exp->r.numval=rval->v.l;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}
					break;
				case RV_STR:
					/* string evaluated in expression context - not
					   supported */
					break;
				case RV_BEXPR:
					if (exp->op==NO_OP){
						/* replace the current expr. */
						*exp=*(rval->v.bexpr);
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					};
					break;
				case RV_ACTION_ST:
					if (exp->op==NO_OP){
						exp->l_type=ACTION_O;
						exp->l.param=0;
						exp->r_type=ACTION_ST;
						exp->r.param=rval->v.action;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}
					break;
				case RV_SEL:
					exp->l.select=pkg_malloc(sizeof(*exp->l.select));
					if (exp->l.select){
						exp->l_type=SELECT_O;
						*exp->l.select=rval->v.sel;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_AVP:
					exp->l.attr=pkg_malloc(sizeof(*exp->l.attr));
					if (exp->l.attr){
						exp->l_type=AVP_O;
						*exp->l.attr=rval->v.avps;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_PVAR:
					exp->l.param=pkg_malloc(sizeof(pv_spec_t));
					if (exp->l.param){
						exp->l_type=PVAR_O;
						*((pv_spec_t*)exp->l.param)=rval->v.pvs;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_NONE:
					break;
			}
		}
	}
	if (ret>0)
		DBG("left EXP optimized: op%d(_O%d_, ST%d) => op%d(_O%d_, ST%d)\n",
			old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
	return ret;
}



/** optimize the left side of a struct expr.
 *  @return 1 if optimized, 0 if not and -1 on error
 */
static int exp_optimize_right(struct expr* exp)
{
	struct rval_expr* rve;
	struct rvalue* rval;
	int old_ltype, old_rtype, old_op;
	int ret;
	
	ret=0;
	if ((exp->type!=ELEM_T) ||(exp->op==NO_OP))
		return 0;
	old_ltype=exp->l_type;
	old_rtype=exp->r_type;
	old_op=exp->op;
	if (exp->r_type==RVE_ST){
		rve=exp->r.param;
		/* rve should be previously fixed/optimized */
		/* optimize exp (rval(val)) -> exp(val) */
		if (rve->op==RVE_RVAL_OP){
			rval=&rve->left.rval;
			switch(rval->type){
				case RV_INT:
					exp->r_type=NUMBER_ST;
					exp->r.numval=rval->v.l;
					rval_destroy(rval);
					pkg_free(rve);
					ret=1;
					break;
				case RV_STR:
					exp->r.str.s=pkg_malloc(rval->v.s.len+1);
					if (exp->r.str.s){
						exp->r.str.len=rval->v.s.len;
						memcpy(exp->r.str.s, rval->v.s.s, rval->v.s.len);
						exp->r.str.s[exp->r.str.len]=0;
						exp->r_type=STRING_ST;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_BEXPR:
					/* cannot be optimized further, is an exp_elem
					   which is not constant */
					break;
				case RV_ACTION_ST:
					/* cannot be optimized further, is not constant and
					  eval_elem() does not support ACTION_ST for op!=NO_OP*/
					break;
				case RV_SEL:
					exp->r.select=pkg_malloc(sizeof(*exp->l.select));
					if (exp->r.select){
						exp->r_type=SELECT_ST;
						*exp->r.select=rval->v.sel;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_AVP:
					exp->r.attr=pkg_malloc(sizeof(*exp->l.attr));
					if (exp->r.attr){
						exp->r_type=AVP_ST;
						*exp->r.attr=rval->v.avps;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_PVAR:
					exp->r.param=pkg_malloc(sizeof(pv_spec_t));
					if (exp->r.param){
						exp->r_type=PVAR_ST;
						*((pv_spec_t*)exp->r.param)=rval->v.pvs;
						rval_destroy(rval);
						pkg_free(rve);
						ret=1;
					}else
						ret=-1;
					break;
				case RV_NONE:
					ret=-1;
					break;
			}
		}
	}
	if (ret>0)
		DBG("right EXP optimized: op%d(O%d, _ST%d_) => op%d(O%d, _ST%d_)\n",
			old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
	return ret;
}



/* traverses an expr tree and compiles the REs where necessary)
 * returns: 0 for ok, <0 if errors */
int fix_expr(struct expr* exp)
{
	regex_t* re;
	int ret;
	int len;

	ret=E_BUG;
	if (exp==0){
		LM_CRIT("null pointer\n");
		return E_BUG;
	}
	if (exp->type==EXP_T){
		switch(exp->op){
			case LOGAND_OP:
			case LOGOR_OP:
						if ((ret=fix_expr(exp->l.expr))!=0)
							return ret;
						ret=fix_expr(exp->r.expr);
						break;
			case NOT_OP:
						ret=fix_expr(exp->l.expr);
						break;
			default:
						LM_CRIT("unknown op %d\n", exp->op);
		}
	}else if (exp->type==ELEM_T){
			/* first calculate lengths of strings  (only right side, since 
			  left side can never be a string) */
			if (exp->r_type==STRING_ST) {
				if (exp->r.string) len = strlen(exp->r.string);
				else len = 0;
				exp->r.str.s = exp->r.string;
				exp->r.str.len = len;
			}
			/* then fix & optimize rve/rvals (they might be optimized
			   to non-rvals, e.g. string, avp a.s.o and needs to be done
			   before MATCH_OP and other fixups) */
			if (exp->l_type==RVEXP_O){
				if ((ret=fix_rval_expr(exp->l.param))<0){
					LM_ERR("Unable to fix left rval expression\n");
					return ret;
				}
				if (scr_opt_lev>=2)
					exp_optimize_left(exp);
			}
			if (exp->r_type==RVE_ST){
				if ((ret=fix_rval_expr(exp->r.param))<0){
					LM_ERR("Unable to fix right rval expression\n");
					return ret;
				}
				if (scr_opt_lev>=2)
					exp_optimize_right(exp);
			}
			
			
			if (exp->op==MATCH_OP){
				     /* right side either has to be string, in which case
				      * we turn it into regular expression, or it is regular
				      * expression already. In that case we do nothing
				      */
				if (exp->r_type==STRING_ST){
					re=(regex_t*)pkg_malloc(sizeof(regex_t));
					if (re==0){
						LM_CRIT("memory allocation failure\n");
						return E_OUT_OF_MEM;
					}
					if (regcomp(re, (char*) exp->r.param,
								REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
						LM_CRIT("bad re \"%s\"\n", (char*) exp->r.param);
						pkg_free(re);
						return E_BAD_RE;
					}
					/* replace the string with the re */
					pkg_free(exp->r.param);
					exp->r.re=re;
					exp->r_type=RE_ST;
				}else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST
						&& exp->r_type != SELECT_ST &&
						exp->r_type != SELECT_UNFIXED_ST &&
						exp->r_type!= RVE_ST
						&& exp->r_type != PVAR_ST){
					LM_CRIT("invalid type for match\n");
					return E_BUG;
				}
			}
			if (exp->l_type==ACTION_O){
				ret=fix_actions((struct action*)exp->r.param);
				if (ret!=0){
					LM_CRIT("fix_actions error\n");
					return ret;
				}
			}
			if (exp->l_type==SELECT_UNFIXED_O) {
				if ((ret=resolve_select(exp->l.select)) < 0) {
					LM_ERR("Unable to resolve select\n");
					print_select(exp->l.select);
					return ret;
				}
				exp->l_type=SELECT_O;
			}
			if (exp->r_type==SELECT_UNFIXED_ST) {
				if ((ret=resolve_select(exp->r.select)) < 0) {
					LM_ERR("Unable to resolve select\n");
					print_select(exp->r.select);
					return ret;
				}
				exp->r_type=SELECT_ST;
			}
			/* PVAR don't need fixing */
			ret=0;
	}
	return ret;
}



/* adds the proxies in the proxy list & resolves the hostnames */
/* returns 0 if ok, <0 on error */
int fix_actions(struct action* a)
{
	struct action *t;
	struct proxy_l* p;
	char *tmp;
	void *tmp_p;
	int ret;
	int i;
	sr31_cmd_export_t* cmd;
	str s;
	struct hostent* he;
	struct ip_addr ip;
	struct socket_info* si;
	struct lvalue* lval;
	struct rval_expr* rve;
	struct rval_expr* err_rve;
	enum rval_type rve_type, err_type, expected_type;
	struct rvalue* rv;
	int rve_param_no;

	if (a==0){
		LM_CRIT("null pointer\n");
		return E_BUG;
	}
	for(t=a; t!=0; t=t->next){
		switch(t->type){
			case FORWARD_T:
			case FORWARD_TLS_T:
			case FORWARD_TCP_T:
			case FORWARD_SCTP_T:
			case FORWARD_UDP_T:
					switch(t->val[0].type){
						case IP_ST:
							tmp=strdup(ip_addr2a(
										(struct ip_addr*)t->val[0].u.data));
							if (tmp==0){
								LM_CRIT("memory allocation failure\n");
								ret = E_OUT_OF_MEM;
								goto error;
							}
							t->val[0].type=STRING_ST;
							t->val[0].u.string=tmp;
							/* no break */
						case STRING_ST:
							s.s = t->val[0].u.string;
							s.len = strlen(s.s);
							p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
							if (p==0) { ret =E_BAD_ADDRESS; goto error; }
							t->val[0].u.data=p;
							t->val[0].type=PROXY_ST;
							break;
						case URIHOST_ST:
							break;
						default:
							LM_CRIT("invalid type %d (should be string or number)\n",
										t->type);
							ret = E_BUG;
							goto error;
					}
					break;
			case IF_T:
				if (t->val[0].type!=RVE_ST){
					LM_CRIT("invalid subtype %d for if (should be rval expr)\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}else if( (t->val[1].type!=ACTIONS_ST) &&
							(t->val[1].type!=NOSUBTYPE) ){
					LM_CRIT("invalid subtype %d for if() {...} (should be action)\n",
								t->val[1].type);
					ret = E_BUG;
					goto error;
				}else if( (t->val[2].type!=ACTIONS_ST) &&
							(t->val[2].type!=NOSUBTYPE) ){
					LM_CRIT("invalid subtype %d for if() {} else{...}(should be action)\n",
								t->val[2].type);
					ret = E_BUG;
					goto error;
				}
				rve=(struct rval_expr*)t->val[0].u.data;
				if (rve){
					err_rve=0;
					if (!rve_check_type(&rve_type, rve, &err_rve,
											&err_type, &expected_type)){
						if (err_rve)
							LM_ERR("invalid expression "
									"(%d,%d): subexpression (%d,%d) has type"
									" %s,  but %s is expected\n",
									rve->fpos.s_line, rve->fpos.s_col,
									err_rve->fpos.s_line, err_rve->fpos.s_col,
									rval_type_name(err_type),
									rval_type_name(expected_type) );
						else
							LM_ERR("invalid expression  (%d,%d): type mismatch?",
									rve->fpos.s_line, rve->fpos.s_col);
						ret = E_SCRIPT;
						goto error;
					}
					/* it's not an error anymore to have non-int in an if,
					   only a script warning (to allow backward compat. stuff
					   like if (@ruri) 
					if (rve_type!=RV_INT && rve_type!=RV_NONE){
						LM_ERR("fix_actions: invalid expression (%d,%d):"
								" bad type, integer expected\n",
								rve->fpos.s_line, rve->fpos.s_col);
						return E_UNSPEC;
					}
					*/
					if ((ret=fix_rval_expr(t->val[0].u.data))<0)
						goto error;
				}
				if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
					if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
						goto error;
				}
				if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
						if ((ret=fix_actions((struct action*)t->val[2].u.data))
								<0)
						goto error;
				}
				break;
			case SWITCH_T:
				if (t->val[0].type!=RVE_ST){
					LM_CRIT("invalid subtype %d for switch() (should be expr)\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}else if (t->val[1].type!=CASE_ST){
					LM_CRIT("invalid subtype %d for switch(...){...}(should be case)\n",
								t->val[1].type);
					ret = E_BUG;
					goto error;
				}
				if (t->val[0].u.data){
					if ((ret=fix_rval_expr(t->val[0].u.data))<0)
						goto error;
				}else{
					LM_CRIT("null switch() expression\n");
					ret = E_BUG;
					goto error;
				}
				if ((ret=fix_switch(t))<0)
					goto error;
				break;
			case WHILE_T:
				if (t->val[0].type!=RVE_ST){
					LM_CRIT("invalid subtype %d for while() (should be expr)\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}else if (t->val[1].type!=ACTIONS_ST){
					LM_CRIT("invalid subtype %d for while(...){...}(should be action)\n",
								t->val[1].type);
					ret = E_BUG;
					goto error;
				}
				rve=(struct rval_expr*)t->val[0].u.data;
				if (rve){
					err_rve=0;
					if (!rve_check_type(&rve_type, rve, &err_rve,
											&err_type, &expected_type)){
						if (err_rve)
							LM_ERR("invalid expression "
									"(%d,%d): subexpression (%d,%d) has type"
									" %s,  but %s is expected\n",
									rve->fpos.s_line, rve->fpos.s_col,
									err_rve->fpos.s_line, err_rve->fpos.s_col,
									rval_type_name(err_type),
									rval_type_name(expected_type) );
						else
							LM_ERR("invalid expression (%d,%d): type mismatch?",
									rve->fpos.s_line, rve->fpos.s_col);
						ret = E_SCRIPT;
						goto error;
					}
					if (rve_type!=RV_INT && rve_type!=RV_NONE){
						LM_ERR("invalid expression (%d,%d): bad type, integer expected\n",
								rve->fpos.s_line, rve->fpos.s_col);
						ret = E_SCRIPT;
						goto error;
					}
					if ((ret=fix_rval_expr(t->val[0].u.data))<0)
						goto error;
				}else{
					LM_CRIT("null while() expression\n");
					ret = E_BUG;
					goto error;
				}
				if ( t->val[1].u.data && 
					((ret= fix_actions((struct action*)t->val[1].u.data))<0)){
					goto error;
				}
				break;
			case DROP_T:
				/* only RVEs need fixing for drop/return/break */
				if (t->val[0].type!=RVE_ST)
					break;
				rve=(struct rval_expr*)t->val[0].u.data;
				if (rve){
					err_rve=0;
					if (!rve_check_type(&rve_type, rve, &err_rve,
											&err_type, &expected_type)){
						if (err_rve)
							LM_ERR("invalid expression "
									"(%d,%d): subexpression (%d,%d) has type"
									" %s,  but %s is expected\n",
									rve->fpos.s_line, rve->fpos.s_col,
									err_rve->fpos.s_line, err_rve->fpos.s_col,
									rval_type_name(err_type),
									rval_type_name(expected_type) );
						else
							LM_ERR("invalid expression (%d,%d): type mismatch?",
									rve->fpos.s_line, rve->fpos.s_col);
						ret = E_SCRIPT;
						goto error;
					}
					if (rve_type!=RV_INT && rve_type!=RV_NONE){
						LM_ERR("invalid expression (%d,%d): bad type, integer expected\n",
								rve->fpos.s_line, rve->fpos.s_col);
						ret = E_SCRIPT;
						goto error;
					}
					if ((ret=fix_rval_expr(t->val[0].u.data))<0)
						goto error;
				}else{
					LM_CRIT("null drop/return expression\n");
					ret = E_BUG;
					goto error;
				}
				break;
			case ASSIGN_T:
			case ADD_T:
				if (t->val[0].type !=LVAL_ST) {
					LM_CRIT("Invalid left side of assignment\n");
					ret = E_BUG;
					goto error;
				}
				if (t->val[1].type !=RVE_ST) {
					LM_CRIT("Invalid right side of assignment (%d)\n",
								t->val[1].type);
					ret = E_BUG;
					goto error;
				}
				lval=t->val[0].u.data;
				if (lval->type==LV_AVP){
					if (lval->lv.avps.type & AVP_CLASS_DOMAIN) {
						LM_ERR("You cannot change domain"
									" attributes from the script, they are"
									" read-only\n");
						ret = E_BUG;
						goto error;
					} else if (lval->lv.avps.type & AVP_CLASS_GLOBAL) {
						LM_ERR("You cannot change global"
								   " attributes from the script, they are"
								   "read-only\n");
						ret = E_BUG;
						goto error;
					}
				}
				if ((ret=fix_rval_expr(t->val[1].u.data))<0)
					goto error;
				break;

			case MODULE0_T:
			case MODULE1_T:
			case MODULE2_T:
			case MODULE3_T:
			case MODULE4_T:
			case MODULE5_T:
			case MODULE6_T:
			case MODULEX_T:
				cmd = t->val[0].u.data;
				rve_param_no = 0;
				if (cmd) {
					DBG("fixing %s()\n", cmd->name);
					if (t->val[1].u.number==0) {
						ret = call_fixup(cmd->fixup, 0, 0);
						if (ret < 0)
							goto error;
					}
					for (i=0; i < t->val[1].u.number; i++) {
						if (t->val[i+2].type == RVE_ST) {
							rve = t->val[i+2].u.data;
							if (rve_is_constant(rve)) {
								/* if expression is constant => evaluate it
								   as string and replace it with the corresp.
								   string */
								rv = rval_expr_eval(0, 0, rve);
								if (rv == 0 ||
										rval_get_str( 0, 0, &s, rv, 0) < 0 ) {
									ERR("failed to fix constant rve");
									if (rv) rval_destroy(rv);
									ret = E_BUG;
									goto error;
								}
								rval_destroy(rv);
								rve_destroy(rve);
								t->val[i+2].type = STRING_ST;/*asciiz string*/
								t->val[i+2].u.string = s.s;
								/* len is not used for now */
								t->val[i+2].u.str.len = s.len;
								tmp_p = t->val[i+2].u.data;
								ret = call_fixup(cmd->fixup,
												&t->val[i+2].u.data, i+1);
								if (t->val[i+2].u.data != tmp_p)
									t->val[i+2].type = MODFIXUP_ST;
								if (ret < 0)
									goto error;
							} else {
								/* expression is not constant => fixup &
								   optimize it */
								rve_param_no++;
								if ((ret=fix_rval_expr(t->val[i+2].u.data))
										< 0) {
									ERR("rve fixup failed\n");
									ret = E_BUG;
									goto error;
								}
							}
						} else  if (t->val[i+2].type == STRING_ST) {
							tmp_p = t->val[i+2].u.data;
							ret = call_fixup(cmd->fixup,
											&t->val[i+2].u.data, i+1);
							if (t->val[i+2].u.data != tmp_p)
								t->val[i+2].type = MODFIXUP_ST;
							if (ret < 0)
								goto error;
						} else {
							BUG("invalid module function param type %d\n",
									t->val[i+2].type);
							ret = E_BUG;
							goto error;
						}
					} /* for */
					/* here all the params are either STRING_ST
					   (constant RVEs), MODFIXUP_ST (fixed up)
					   or RVE_ST (non-ct RVEs) */
					if (rve_param_no) { /* we have to fix the type */
						if (cmd->fixup &&
							!(cmd->fixup_flags & FIXUP_F_FPARAM_RVE) &&
							cmd->free_fixup == 0) {
							BUG("non-ct RVEs (%d) in module function call"
									"that does not support them (%s)\n",
									rve_param_no, cmd->name);
							ret = E_BUG;
							goto error;
						}
						switch(t->type) {
							case MODULE1_T:
								t->type = MODULE1_RVE_T;
								break;
							case MODULE2_T:
								t->type = MODULE2_RVE_T;
								break;
							case MODULE3_T:
								t->type = MODULE3_RVE_T;
								break;
							case MODULE4_T:
								t->type = MODULE4_RVE_T;
								break;
							case MODULE5_T:
								t->type = MODULE5_RVE_T;
								break;
							case MODULE6_T:
								t->type = MODULE6_RVE_T;
								break;
							case MODULEX_T:
								t->type = MODULEX_RVE_T;
								break;
							default:
								BUG("unsupported module function type %d\n",
										t->type);
								ret = E_BUG;
								goto error;
						}
					} /* if rve_param_no */
				}
				break;
			case FORCE_SEND_SOCKET_T:
				if (t->val[0].type!=SOCKID_ST){
					LM_CRIT("invalid subtype %d for force_send_socket\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}
				he=resolvehost(
						((struct socket_id*)t->val[0].u.data)->addr_lst->name
						);
				if (he==0){
					LM_ERR("force_send_socket: could not resolve %s\n",
						((struct socket_id*)t->val[0].u.data)->addr_lst->name);
					ret = E_BAD_ADDRESS;
					goto error;
				}
				hostent2ip_addr(&ip, he, 0);
				si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
								((struct socket_id*)t->val[0].u.data)->proto);
				if (si==0){
					LM_ERR("bad force_send_socket argument: %s:%d (ser doesn't listen on it)\n",
						((struct socket_id*)t->val[0].u.data)->addr_lst->name,
							((struct socket_id*)t->val[0].u.data)->port);
					ret = E_BAD_ADDRESS;
					goto error;
				}
				t->val[0].u.data=si;
				t->val[0].type=SOCKETINFO_ST;
				break;
			case UDP_MTU_TRY_PROTO_T:
				if (t->val[0].type!=NUMBER_ST){
					LM_CRIT("invalid subtype %d for udp_mtu_try_proto\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}
				switch(t->val[0].u.number){
					case PROTO_UDP:
						t->val[0].u.number=0;
						break;
					case PROTO_TCP:
						t->val[0].u.number=FL_MTU_TCP_FB;
						break;
					case PROTO_TLS:
						t->val[0].u.number=FL_MTU_TLS_FB;
						break;
					case PROTO_SCTP:
						t->val[0].u.number=FL_MTU_SCTP_FB;
						break;
					default:
						LM_CRIT("invalid argument for udp_mtu_try_proto (%d)\n", 
									(unsigned int)t->val[0].u.number);
				}
				break;
			case APPEND_BRANCH_T:
				if (t->val[0].type!=STRING_ST){
					BUG("invalid subtype%d for append_branch_t\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}
				s.s=t->val[0].u.string;
				s.len=(s.s)?strlen(s.s):0;
				t->val[0].u.str=s;
				t->val[0].type=STR_ST;
				break;
			case ROUTE_T:
				if (t->val[0].type == RVE_ST) {
					rve=(struct rval_expr*)t->val[0].u.data;
					if (!rve_is_constant(rve)) {
						if ((ret=fix_rval_expr(t->val[0].u.data)) < 0){
							LM_ERR("route() failed to fix rve at %s:%d\n",
								(t->cfile)?t->cfile:"line", t->cline);
							ret = E_BUG;
							goto error;
						}
					} else {
						/* rve is constant => replace it with a string */
						if ((rv = rval_expr_eval(0, 0, rve)) == 0 ||
								rval_get_str(0, 0, &s, rv, 0) < 0) {
							/* out of mem. or bug ? */
							rval_destroy(rv);
							LM_ERR("route() failed to fix ct. rve at %s:%d\n",
								(t->cfile)?t->cfile:"line", t->cline);
							ret = E_BUG;
							goto error;
						}
						rval_destroy(rv);
						rve_destroy(rve);
						t->val[0].type = STRING_ST;
						t->val[0].u.string = s.s;
						t->val[0].u.str.len = s.len; /* not used */
						/* fall-through the STRING_ST if */
					}
				}
				if (t->val[0].type == STRING_ST) {
					i=route_lookup(&main_rt, t->val[0].u.string);
					if (i < 0) {
						LM_ERR("route \"%s\" not found at %s:%d\n",
								t->val[0].u.string,
								(t->cfile)?t->cfile:"line", t->cline);
						ret = E_SCRIPT;
						goto error;
					}
					t->val[0].type = NUMBER_ST;
					pkg_free(t->val[0].u.string);
					t->val[0].u.number = i;
				} else if (t->val[0].type != NUMBER_ST &&
							t->val[0].type != RVE_ST) {
					BUG("invalid subtype %d for route()\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}
				break;
			case CFG_SELECT_T:
				if (t->val[1].type == RVE_ST) {
					rve = t->val[1].u.data;
					if (rve_is_constant(rve)) {
						/* if expression is constant => evaluate it
						   as integer and replace it with the corresp.
						   int */
						rv = rval_expr_eval(0, 0, rve);
						if (rv == 0 ||
								rval_get_int( 0, 0, &i, rv, 0) < 0 ) {
							LM_ERR("failed to fix constant rve");
							if (rv) rval_destroy(rv);
							ret = E_BUG;
							goto error;
						}
						rval_destroy(rv);
						rve_destroy(rve);
						t->val[1].type = NUMBER_ST;
						t->val[1].u.number = i;
					} else {
						/* expression is not constant => fixup &
						   optimize it */
						if ((ret=fix_rval_expr(rve))
								< 0) {
							LM_ERR("rve fixup failed\n");
							ret = E_BUG;
							goto error;
						}
					}
				} else if (t->val[1].type != NUMBER_ST) {
					BUG("invalid subtype %d for cfg_select()\n",
								t->val[1].type);
					ret = E_BUG;
					goto error;
				}

			case CFG_RESET_T:
				if (t->val[0].type != STRING_ST) {
					BUG("invalid subtype %d for cfg_select() or cfg_reset()\n",
								t->val[0].type);
					ret = E_BUG;
					goto error;
				}
				tmp_p = (void *)cfg_lookup_group(t->val[0].u.string, strlen(t->val[0].u.string));
				if (!tmp_p) {
					LM_ERR("configuration group \"%s\" not found\n",
						t->val[0].u.string);
					ret = E_SCRIPT;
					goto error;
				}
				pkg_free(t->val[0].u.string);
				t->val[0].u.data = tmp_p;
				t->val[0].type = CFG_GROUP_ST;
				break;
			default:
				/* no fixup required for the rest */
				break;
		}
	}
	return 0;

error:
	LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret,
			(t->cfile)?t->cfile:"", t->cline);
	return ret;
}


/* Compare parameters as ordinary numbers
 *
 * Left and right operands can be either numbers or
 * attributes. If either of the attributes if of string type then the length of
 * its value will be used.
 */
inline static int comp_num(int op, long left, int rtype, union exp_op* r,
							struct sip_msg* msg, struct run_act_ctx* h)
{
	int_str val;
	pv_value_t pval;
	avp_t* avp;
	int right;

	if (unlikely(op==NO_OP)) return !(!left);
	switch(rtype){
		case AVP_ST:
			avp = search_avp_by_index(r->attr->type, r->attr->name,
										&val, r->attr->index);
			if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
			else return (op == DIFF_OP);
			break;
		case NUMBER_ST:
			right = r->numval;
			break;
		case RVE_ST:
			if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
				return (op == DIFF_OP); /* not found/invalid */
			break;
		case PVAR_ST:
			memset(&pval, 0, sizeof(pv_value_t));
			if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
				return (op == DIFF_OP); /* error, not found => false */
			}
			if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
				right=pval.ri;
				pv_value_destroy(&pval);
			}else{
				pv_value_destroy(&pval);
				return (op == DIFF_OP); /* not found or invalid type */
			}
			break;
		default:
			LM_CRIT("Invalid right operand (%d)\n", rtype);
			return E_BUG;
	}

	switch (op){
		case EQUAL_OP: return (long)left == (long)right;
		case DIFF_OP:  return (long)left != (long)right;
		case GT_OP:    return (long)left >  (long)right;
		case LT_OP:    return (long)left <  (long)right;
		case GTE_OP:   return (long)left >= (long)right;
		case LTE_OP:   return (long)left <= (long)right;
		default:
			LM_CRIT("unknown operator: %d\n", op);
			return E_BUG;
	}
	return E_BUG;
}

/*
 * Compare given string "left" with right side of expression
 */
inline static int comp_str(int op, str* left, int rtype, 
							union exp_op* r, struct sip_msg* msg,
							struct run_act_ctx* h)
{
	str* right;
	int_str val;
	str v;
	avp_t* avp;
	int ret;
	char backup;
	regex_t* re;
	unsigned int l;
	struct rvalue* rv;
	struct rval_cache rv_cache;
	pv_value_t pval;
	int destroy_pval;
	
	right=0; /* warning fix */
	rv=0;
	destroy_pval=0;
	if (unlikely(op==NO_OP)) return (left->s!=0);
	switch(rtype){
		case AVP_ST:
			avp = search_avp_by_index(r->attr->type, r->attr->name,
										&val, r->attr->index);
			if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
			else return (op == DIFF_OP);
			break;
		case SELECT_ST:
			ret = run_select(&v, r->select, msg);
			if (unlikely(ret != 0)) 
				return (op == DIFF_OP); /* Not found or error */
			right = &v;
			break;
		case RVE_ST:
			rval_cache_init(&rv_cache);
			rv=rval_expr_eval(h, msg, r->param);
			if (unlikely (rv==0)) 
				return (op==DIFF_OP); /* not found or error*/
			if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
				goto error;
			}
			right = &v;
			break;
		case PVAR_ST:
			memset(&pval, 0, sizeof(pv_value_t));
			if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
				return (op == DIFF_OP); /* error, not found => false */
			}
			destroy_pval=1;
			if (likely(pval.flags & PV_VAL_STR)){
				right=&pval.rs;
			}else{
				pv_value_destroy(&pval);
				return (op == DIFF_OP); /* not found or invalid type */
			}
			break;
		case RE_ST:
			if (unlikely(op != MATCH_OP)){
				LM_CRIT("Bad operator %d, ~= expected\n", op);
				goto error;
			}
			break;
		case STRING_ST: /* strings are stored as {asciiz, len } */
		case STR_ST:
			right=&r->str;
			break;
		case NUMBER_ST:
			/* "123" > 100 is not allowed by cfg.y rules
			 * but can happen as @select or $avp evaluation
			 * $test > 10
			 * the right operator MUST be number to do the conversion
			 */
			if (str2int(left,&l) < 0)
				goto error;
			return comp_num(op, l, rtype, r, msg, h);
		default:
			LM_CRIT("Bad type %d, string or RE expected\n", rtype);
			goto error;
	}

	ret=-1;
	switch(op){
		case EQUAL_OP:
			if (left->len != right->len) return 0;
			ret=(strncasecmp(left->s, right->s, left->len)==0);
			break;
		case DIFF_OP:
			if (left->len != right->len) return 1;
			ret = (strncasecmp(left->s, right->s, left->len)!=0);
			break;
		case MATCH_OP:
			/* this is really ugly -- we put a temporary zero-terminating
			 * character in the original string; that's because regexps
			 * take 0-terminated strings and our messages are not
			 * zero-terminated; it should not hurt as long as this function
			 * is applied to content of pkg mem, which is always the case
			 * with calls from route{}; the same goes for fline in 
			 * reply_route{};
			 *
			 * also, the received function should always give us an extra
			 * character, into which we can put the 0-terminator now;
			 * an alternative would be allocating a new piece of memory,
			 * which might be too slow
			 * -jiri
			 *
			 * janakj: AVPs are zero terminated too so this is not problem 
			 * either
			 */
			backup=left->s[left->len];
			left->s[left->len]='\0';
			switch(rtype){
				case AVP_ST:
				case SELECT_ST:
				case RVE_ST:
				case PVAR_ST:
				case STRING_ST:
				case STR_ST:
					/* we need to compile the RE on the fly */
					re=(regex_t*)pkg_malloc(sizeof(regex_t));
					if (re==0){
						LM_CRIT("memory allocation failure\n");
						left->s[left->len] = backup;
						goto error;
					}
					if (regcomp(re, right->s,
								REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
						pkg_free(re);
						left->s[left->len] = backup;
						goto error;
					}
					ret=(regexec(re, left->s, 0, 0, 0)==0);
					regfree(re);
					pkg_free(re);
					break;
				case RE_ST:
					ret=(regexec(r->re, left->s, 0, 0, 0)==0);
					break;
				default:
					LM_CRIT("Bad operator type %d, for ~= \n", rtype);
					goto error;
			}
			left->s[left->len] = backup;
			break;
		default:
			LM_CRIT("unknown op %d\n", op);
			goto error;
	}
	if (rv){
		rval_cache_clean(&rv_cache);
		rval_destroy(rv);
	}
	if (destroy_pval)
		pv_value_destroy(&pval);
	return ret;

error:
	if (rv){
		rval_cache_clean(&rv_cache);
		rval_destroy(rv);
	}
	if (destroy_pval)
		pv_value_destroy(&pval);
	return (op == DIFF_OP) ? 1 : -1;
}


/* eval_elem helping function, returns str op param */
inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
								struct sip_msg* msg, struct run_act_ctx* h)
{
	str s;
	
	s.s=left;
	s.len=strlen(left);
	return comp_str(op, &s, rtype, r, msg, h);
}


inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
							union exp_op* r, struct sip_msg* msg,
							struct run_act_ctx* h)
{
	avp_t* avp;
	int_str val;
	union exp_op num_val;
	str tmp;
	unsigned int uval;

	if (spec->type & AVP_INDEX_ALL) {
		avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
								NULL, NULL);
		return (avp!=0);
	}
	avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
	if (!avp) return (op == DIFF_OP);

	if (op==NO_OP){
		if (avp->flags & AVP_VAL_STR) {
			return val.s.len!=0;
		} else {
			return val.n != 0;
		}
	}
	if (avp->flags & AVP_VAL_STR) {
		return comp_str(op, &val.s, rtype, r, msg, h);
	} else {
		switch(rtype){
			case NUMBER_ST:
			case AVP_ST:
			case RVE_ST:
			case PVAR_ST:
				return comp_num(op, val.n, rtype, r, msg, h);
				break;
			case STRING_ST:
				tmp.s=r->string;
				tmp.len=strlen(r->string);
				if (str2int(&tmp, &uval)<0){
					LM_WARN("cannot convert string value to int (%s)\n",
								ZSW(r->string));
					goto error;
				}
				num_val.numval=uval;
				return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
			case STR_ST:
				if (str2int(&r->str, &uval)<0){
					LM_WARN("cannot convert str value to int (%.*s)\n",
								r->str.len, ZSW(r->str.s));
					goto error;
				}
				num_val.numval=uval;
				return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
			default:
				LM_CRIT("invalid type for numeric avp comparison (%d)\n",
								rtype);
				goto error;
		}
	}
error:
	return (op == DIFF_OP) ? 1 : -1;
}

/*
 * Left side of expression was select
 */
inline static int comp_select(int op, select_t* sel, int rtype,
								union exp_op* r, struct sip_msg* msg,
								struct run_act_ctx* h)
{
	int ret;
	str val;
	char empty_str=0;

	ret = run_select(&val, sel, msg);
	if (ret != 0) return (op == DIFF_OP);

	if (op==NO_OP) return (val.len>0);
	if (unlikely(val.len==0)) {
		/* make sure the string pointer uses accessible memory range
		 * the comp_str function might dereference it
		 */
		val.s=&empty_str;
	}
	return comp_str(op, &val, rtype, r, msg, h);
}


inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
							union exp_op* r, struct sip_msg* msg,
							struct run_act_ctx* h)
{
	int i;
	struct rvalue* rv;
	struct rvalue* rv1;
	struct rval_cache c1;
	
	rval_cache_init(&c1);
	if (unlikely(rval_expr_eval_rvint(h,  msg, &rv, &i, rve, &c1)<0)){
		LM_ERR("failure evaluating expression: bad type\n");
		i=0; /* false */
		goto int_expr;
	}
	if (unlikely(rv)){
		/* no int => str */
		rv1=rval_convert(h, msg, RV_STR, rv, &c1);
		i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
		rval_destroy(rv1);
		rval_destroy(rv);
		rval_cache_clean(&c1);
		return i;
	}
	/* expr evaluated to int */
int_expr:
	rval_cache_clean(&c1);
	if (op==NO_OP)
		return !(!i); /* transform it into { 0, 1 } */
	return comp_num(op, i, rtype, r, msg, h);
}



inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
							union exp_op* r, struct sip_msg* msg,
							struct run_act_ctx* h)
{
	pv_value_t pval;
	int ret;
	
	ret=0;
	memset(&pval, 0, sizeof(pv_value_t));
	if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
		return 0; /* error, not found => false */
	}
	if (likely(pval.flags & PV_TYPE_INT)){
		if (op==NO_OP)
			ret=!(!pval.ri);
		else
			ret=comp_num(op, pval.ri, rtype, r, msg, h);
	}else if ((pval.flags==PV_VAL_NONE) ||
			(pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
		if (op==NO_OP)
			ret=0;
		else
			ret=comp_num(op, 0, rtype, r, msg, h);
	}else{
		ret=pval.rs.len!=0;
		if (op!=NO_OP)
			ret=comp_num(op, ret, rtype, r, msg, h);
	}
	pv_value_destroy(&pval);
	return ret;
}



/* check_self wrapper -- it checks also for the op */
inline static int check_self_op(int op, str* s, unsigned short p)
{
	int ret;

	ret=check_self(s, p, 0);
	switch(op){
		case EQUAL_OP:
		case MATCH_OP:
			break;
		case DIFF_OP:
			ret=(ret > 0) ? 0 : 1;
			break;
		default:
			LM_CRIT("invalid operator %d\n", op);
			ret=-1;
	}
	return ret;
}


/* eval_elem helping function, returns an op param */
inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
							union exp_op* r, struct sip_msg* msg,
							struct run_act_ctx *ctx )
{
	struct hostent* he;
	char ** h;
	int ret;
	str tmp;
	str* right;
	struct net net;
	union exp_op r_expop;
	struct rvalue* rv;
	struct rval_cache rv_cache;
	avp_t* avp;
	int_str val;
	pv_value_t pval;
	int destroy_pval;

	right=NULL; /* warning fix */
	rv=NULL;
	destroy_pval=0;
	ret=-1;
	he=NULL; /* warning fix */
	switch(rtype){
		case NET_ST:
			switch(op){
				case EQUAL_OP:
					ret=(matchnet(ip, r->net)==1);
					break;
				case DIFF_OP:
					ret=(matchnet(ip, r->net)!=1);
					break;
				default:
					goto error_op;
			}
			return ret; /* exit directly */
		case MYSELF_ST: /* check if it's one of our addresses*/
			tmp.s=ip_addr2a(ip);
			tmp.len=strlen(tmp.s);
			ret=check_self_op(op, &tmp, 0);
			return ret;
		case STRING_ST:
		case STR_ST:
			right=&r->str;
			break;
		case RVE_ST:
			rval_cache_init(&rv_cache);
			rv=rval_expr_eval(ctx, msg, r->param);
			if (unlikely (rv==0))
				return (op==DIFF_OP); /* not found or error*/
			if (unlikely(rval_get_tmp_str(ctx, msg, &tmp, rv, 0, &rv_cache)
							< 0)){
				goto error;
			}
			right = &tmp;
			break;
		case AVP_ST:
			/* we can still have AVP_ST due to the RVE optimisations
			   (if a RVE == $avp => rve wrapper removed => pure avp) */
			avp = search_avp_by_index(r->attr->type, r->attr->name,
										&val, r->attr->index);
			if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
			else return (op == DIFF_OP);
			break;
		case SELECT_ST:
			/* see AVP_ST comment and s/AVP_ST/SELECT_ST/ */
			ret = run_select(&tmp, r->select, msg);
			if (unlikely(ret != 0))
				return (op == DIFF_OP); /* Not found or error */
			right = &tmp;
			break;
		case PVAR_ST:
			/* see AVP_ST comment and s/AVP_ST/PVAR_ST/ */
			memset(&pval, 0, sizeof(pv_value_t));
			if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
				return (op == DIFF_OP); /* error, not found => false */
			}
			destroy_pval=1;
			if (likely(pval.flags & PV_VAL_STR)){
				right=&pval.rs;
			}else{
				pv_value_destroy(&pval);
				return (op == DIFF_OP); /* not found or invalid type */
			}
			break;
		case RE_ST:
			if (unlikely(op != MATCH_OP))
				goto error_op;
			/* 1: compare with ip2str*/
			ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
			if (likely(ret==1))
				return ret;
			/* 3: (slow) rev dns the address
			* and compare with all the aliases
			* !!??!! review: remove this? */
			if (unlikely((received_dns & DO_REV_DNS) &&
				((he=rev_resolvehost(ip))!=0) )){
				/*  compare with primary host name */
				ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
				/* compare with all the aliases */
				for(h=he->h_aliases; (ret!=1) && (*h); h++){
					ret=comp_string(op, *h, rtype, r, msg, ctx);
				}
			}else{
				ret=0;
			}
			return ret;
		default:
			LM_CRIT("invalid type for src_ip or dst_ip (%d)\n", rtype);
			return -1;
	}
	/* here "right" is set to the str we compare with */
	r_expop.str=*right;
	switch(op){
		case EQUAL_OP:
			/* 0: try if ip or network (ip/mask) */
			if (mk_net_str(&net, right) == 0) {
				ret=(matchnet(ip, &net)==1);
				break;
			}
			/* 2: resolve (name) & compare w/ all the ips */
			he=resolvehost(right->s);
			if (he==0){
				DBG("comp_ip: could not resolve %s\n", r->str.s);
			}else if (he->h_addrtype==ip->af){
				for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
					ret=(memcmp(ip->u.addr, *h, ip->len)==0);
				}
				if (ret==1) break;
			}
			/* 3: (slow) rev dns the address
			 * and compare with all the aliases
			 * !!??!! review: remove this? */
			if (unlikely((received_dns & DO_REV_DNS) &&
							((he=rev_resolvehost(ip))!=0) )){
				/*  compare with primary host name */
				ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
				/* compare with all the aliases */
				for(h=he->h_aliases; (ret!=1) && (*h); h++){
					ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
				}
			}else{
				ret=0;
			}
			break;
		case MATCH_OP:
			/* 0: try if ip or network (ip/mask)
			  (one should not use MATCH for that, but try to be nice)*/
			if (mk_net_str(&net, right) == 0) {
				ret=(matchnet(ip, &net)==1);
				break;
			}
			/* 1: compare with ip2str (but only for =~)*/
			ret=comp_string(op, ip_addr2a(ip), STR_ST, &r_expop, msg, ctx);
			if (likely(ret==1)) break;
			/* 2: resolve (name) & compare w/ all the ips */
			he=resolvehost(right->s);
			if (he==0){
				DBG("comp_ip: could not resolve %s\n", r->str.s);
			}else if (he->h_addrtype==ip->af){
				for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
					ret=(memcmp(ip->u.addr, *h, ip->len)==0);
				}
				if (ret==1) break;
			}
			/* 3: (slow) rev dns the address
			 * and compare with all the aliases
			 * !!??!! review: remove this? */
			if (unlikely((received_dns & DO_REV_DNS) &&
							((he=rev_resolvehost(ip))!=0) )){
				/*  compare with primary host name */
				ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
				/* compare with all the aliases */
				for(h=he->h_aliases; (ret!=1) && (*h); h++){
					ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
				}
			}else{
				ret=0;
			}
			break;
		case DIFF_OP:
			ret=(comp_ip(EQUAL_OP, ip, STR_ST, &r_expop, msg, ctx) > 0)?0:1;
		break;
		default:
			goto error_op;
	}
	if (rv){
		rval_cache_clean(&rv_cache);
		rval_destroy(rv);
	}
	if (destroy_pval)
		pv_value_destroy(&pval);
	return ret;
error_op:
	LM_CRIT("invalid operator %d for type %d\n", op, rtype);
error:
	if (unlikely(rv)){
		rval_cache_clean(&rv_cache);
		rval_destroy(rv);
	}
	if (destroy_pval)
		pv_value_destroy(&pval);
	return -1;
}



/* returns: 0/1 (false/true) or -1 on error */
inline static int eval_elem(struct run_act_ctx* h, struct expr* e, 
								struct sip_msg* msg)
{
	struct sip_uri uri;
	int ret;
	struct onsend_info* snd_inf;
	struct ip_addr ip;
	ret=E_BUG;

	if (e->type!=ELEM_T){
		LM_CRIT("invalid type\n");
		goto error;
	}
	switch(e->l_type){
	case METHOD_O:
		if(msg->first_line.type==SIP_REQUEST)
		{
			ret=comp_str(e->op, &msg->first_line.u.request.method,
			 			e->r_type, &e->r, msg, h);
		} else {
			if(parse_headers(msg, HDR_CSEQ_F, 0)!=0 || msg->cseq==NULL)
			{
				LM_ERR("cannot parse cseq header\n");
				goto error;
			}
			ret=comp_str(e->op, &get_cseq(msg)->method,
						e->r_type, &e->r, msg, h);
		}
		break;
	case URI_O:
		if(msg->new_uri.s) {
			if (e->r_type==MYSELF_ST){
				if (parse_sip_msg_uri(msg)<0) ret=-1;
				else ret=check_self_op(e->op, &msg->parsed_uri.host,
								GET_URI_PORT(&msg->parsed_uri));
			}else{
				ret=comp_str(e->op, &msg->new_uri,
								e->r_type, &e->r, msg, h);
			}
		}else{
			if (e->r_type==MYSELF_ST){
				if (parse_sip_msg_uri(msg)<0) ret=-1;
				else ret=check_self_op(e->op, &msg->parsed_uri.host,
								GET_URI_PORT(&msg->parsed_uri));
			}else{
				ret=comp_str(e->op, &msg->first_line.u.request.uri,
								e->r_type, &e->r, msg, h);
			}
		}
		break;

	case FROM_URI_O:
		if (parse_from_header(msg)!=0){
			LM_ERR("bad or missing From: header\n");
			goto error;
		}
		if (e->r_type==MYSELF_ST){
			if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
				      &uri) < 0){
				LM_ERR("bad uri in From:\n");
				goto error;
			}
			ret=check_self_op(e->op, &uri.host, GET_URI_PORT(&uri));
		}else{
			ret=comp_str(e->op, &get_from(msg)->uri,
							e->r_type, &e->r, msg, h);
		}
		break;

	case TO_URI_O:
		if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
				     (msg->to==0))){
			LM_ERR("bad or missing To: header\n");
			goto error;
		}
		     /* to content is parsed automatically */
		if (e->r_type==MYSELF_ST){
			if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
				      &uri) < 0){
				LM_ERR("bad uri in To:\n");
				goto error;
			}
			ret=check_self_op(e->op, &uri.host, GET_URI_PORT(&uri));
		}else{
			ret=comp_str(e->op, &get_to(msg)->uri,
							e->r_type, &e->r, msg, h);
		}
		break;

	case SRCIP_O:
		ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
		break;

	case DSTIP_O:
		ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
		break;

	case SNDIP_O:
		snd_inf=get_onsend_info();
		if (likely(snd_inf && snd_inf->send_sock)){
			ret=comp_ip(e->op, &snd_inf->send_sock->address,
						e->r_type, &e->r, msg, h);
		}else{
			BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
		}
		break;

	case TOIP_O:
		snd_inf=get_onsend_info();
		if (likely(snd_inf && snd_inf->to)){
			su2ip_addr(&ip, snd_inf->to);
			ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
		}else{
			BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
		}
		break;

	case NUMBER_O:
		ret=!(!e->r.numval); /* !! to transform it in {0,1} */
		break;

	case ACTION_O:
		ret=run_actions(h, (struct action*)e->r.param, msg);
		if (ret<=0) ret=0;
		else ret=1;
		break;

	case SRCPORT_O:
		ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
		break;

	case DSTPORT_O:
		ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
		break;

	case SNDPORT_O:
		snd_inf=get_onsend_info();
		if (likely(snd_inf && snd_inf->send_sock)){
			ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
							e->r_type, &e->r, msg, h);
		}else{
			BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
		}
		break;

	case TOPORT_O:
		snd_inf=get_onsend_info();
		if (likely(snd_inf && snd_inf->to)){
			ret=comp_num(e->op, (int)su_getport(snd_inf->to),
								e->r_type, &e->r, msg, h);
		}else{
			BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
		}
		break;

	case PROTO_O:
		ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
		break;

	case SNDPROTO_O:
		snd_inf=get_onsend_info();
		if (likely(snd_inf && snd_inf->send_sock)){
			ret=comp_num(e->op, snd_inf->send_sock->proto,
							e->r_type, &e->r, msg, h);
		}else{
			BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
		}
		break;

	case AF_O:
		ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
		break;

	case SNDAF_O:
		snd_inf=get_onsend_info();
		if (likely(snd_inf && snd_inf->send_sock)){
			ret=comp_num(e->op, snd_inf->send_sock->address.af,
							e->r_type, &e->r, msg, h);
		}else{
			BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
		}
		break;

	case MSGLEN_O:
		if ((snd_inf=get_onsend_info())!=0){
			ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
		}else{
			ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
		}
		break;

	case RETCODE_O:
		ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
		break;

	case AVP_O:
		ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
		break;

	case SELECT_O:
		ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
		break;

	case RVEXP_O:
		ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
		break;

	case PVAR_O:
		ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
		break;

	case SELECT_UNFIXED_O:
		BUG("unexpected unfixed select operand %d\n", e->l_type);
		break;
/*
	default:
		LM_CRIT("invalid operand %d\n", e->l_type);
*/
	}
	return ret;
error:
	return (e->op == DIFF_OP) ? 1 : -1;
}



/* ret= 1/0 (true/false) ,  -1 on error (evaluates as false)*/
int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
{
	int ret;

	if (e->type==ELEM_T){
		ret=eval_elem(h, e, msg);
	}else if (e->type==EXP_T){
		switch(e->op){
			case LOGAND_OP:
				ret=eval_expr(h, e->l.expr, msg);
				/* if error or false stop evaluating the rest */
				if (ret <= 0) break;
				ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
				break;
			case LOGOR_OP:
				ret=eval_expr(h, e->l.expr, msg);
				/* if true stop evaluating the rest */
				if (ret > 0) break;
				ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
				break;
			case NOT_OP:
				ret=eval_expr(h, e->l.expr, msg);
				ret=(ret > 0) ? 0 : 1;
				break;
			default:
				LM_CRIT("unknown op %d\n", e->op);
				ret=-1;
		}
	}else{
		LM_CRIT("unknown type %d\n", e->type);
		ret=-1;
	}
	return ret;
}


/* adds an action list to head; a must be null terminated (last a->next=0))*/
void push(struct action* a, struct action** head)
{
	struct action *t;
	if (*head==0){
		*head=a;
		return;
	}
	for (t=*head; t->next;t=t->next);
	t->next=a;
}




int add_actions(struct action* a, struct action** head)
{
	int ret;

	LM_DBG("fixing actions...\n");
	if ((ret=fix_actions(a))!=0) goto error;
	push(a,head);
	return 0;

error:
	return ret;
}



static int fix_rl(struct route_list* rt)
{
	int i;
	int ret;
	
	for(i=0;i<rt->idx; i++){
		if(rt->rlist[i]){
			if ((ret=fix_actions(rt->rlist[i]))!=0){
				return ret;
			}
		}
	}
	return 0;
}



/* fixes all action tables */
/* returns 0 if ok , <0 on error */
int fix_rls()
{
	int ret;
	
	if ((ret=fix_rl(&main_rt))!=0)
		return ret;
	if ((ret=fix_rl(&onreply_rt))!=0)
		return ret;
	if ((ret=fix_rl(&failure_rt))!=0)
		return ret;
	if ((ret=fix_rl(&branch_rt))!=0)
		return ret;
	if ((ret=fix_rl(&onsend_rt))!=0)
		return ret;
	if ((ret=fix_rl(&event_rt))!=0)
		return ret;

	return 0;
}



static void print_rl(struct route_list* rt, char* name)
{
	int j;
	
	for(j=0; j<rt->entries; j++){
		if (rt->rlist[j]==0){
			if ((j==0) && (rt==&main_rt))
				DBG("WARNING: the main routing table is empty\n");
			continue;
		}
		DBG("%s routing table %d:\n", name, j);
		print_actions(rt->rlist[j]);
		DBG("\n");
	}
}


/* debug function, prints routing tables */
void print_rls()
{
	print_rl(&main_rt, "");
	print_rl(&onreply_rt, "onreply");
	print_rl(&failure_rt, "failure");
	print_rl(&branch_rt, "branch");
	print_rl(&onsend_rt, "onsend");
	print_rl(&event_rt, "event");
}