File: xmlrpc.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • 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 (2645 lines) | stat: -rw-r--r-- 76,932 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
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
/*
 * Copyright (C) 2005 iptelorg GmbH
 * Written by Jan Janak <jan@iptel.org>
 *
 * 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., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
/*This define breaks on Solaris OS */
#ifndef __OS_solaris
	#define _XOPEN_SOURCE 4           /* strptime */
#endif
#define _XOPEN_SOURCE_EXTENDED 1  /* solaris */
#define _SVID_SOURCE 1            /* timegm */

#include <strings.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <signal.h>
#include <libxml/xmlreader.h>
#include "../../str.h"
#include "../../sr_module.h"
#include "../../error.h"
#include "../../usr_avp.h"
#include "../../mem/mem.h"
#include "../../parser/parse_uri.h"
#include "../../parser/msg_parser.h"
#include "../../ut.h"
#include "../../dset.h"
#include "../../str.h"
#include "../../dprint.h"
#include "../../data_lump.h"
#include "../../data_lump_rpl.h"
#include "../../msg_translator.h"
#include "../../select.h"
#include "../../receive.h" /* needed by process_rpc / receive_msg() */
#include "../../modules/sl/sl.h"
#include "../../nonsip_hooks.h"
#include "../../action.h" /* run_actions */
#include "../../script_cb.h" /* exec_*_script_cb */
#include "../../route.h" /* route_get */
#include "../../sip_msg_clone.h" /* sip_msg_shm_clone */
#include "http.h"

/** @addtogroup xmlrpc
 * @ingroup modules
 * @{
 *
 * <h1>Overview of Operation</h1> 
 * This module provides XML-RPC based interface to management functions in
 * SER. You can send XML-RPC requests to SER when the module is loaded and
 * configured and it will send XML-RPC replies back.  XML-RPC requests are
 * encoded as XML documents in the body of HTTP requests. Due to similarity
 * between HTTP and SIP SER can easily parse HTTP requests and extract the XML
 * document from their body.
 *
 * When you load this module into SER, it will register a callback function
 * that will be called whenever the SER core receives a request with method it
 * does not understand. The main callback function is process_xmlrpc(). The
 * function first verifies if the protocol identifier inside the request is
 * HTTP and whether the request method is either GET or POST. If both
 * conditions are met then it will signal to the SER core that it is
 * processing the request, otherwise it will reject the request and the SER
 * core will pass the requests to other callbacks if they exist.
 *
 * As the next step the request will be converted from HTTP request to a SIP
 * request to ensure that it can be processed by SER and its modules. The
 * conversion will modify the URI in the Request-URI of the request, the new
 * URI will be a SIP URI. In addition to that it will add a fake Via header
 * field and copy all remaining header fields from the original HTTP request.
 * The conversion is implemented in http_xmlrpc2sip() function.
 * 
 * After the conversion the module will execute the route statement whose
 * number is configured in "route" module parameter. That route stament may
 * perform additional security checks and when it ensures that the client is
 * authorized to execute management functions then it will call dispatch_rpc()
 * module function provided by this module.
 *
 * dispatch_rpc() function extracts the XML-RPC document from the body of the
 * request to determine the name of the method to be called and then it
 * searches through the list of all management functions to find a function
 * with matching name. If such a function is found then dispatch_rpc() will
 * pass control to the function to handle the request. dispatch_rpc() will
 * send a reply back to the client when the management function terminates, if
 * the function did not do that explicitly.
 * 
 * <h2>Memory Management</h2> 
 * The module provides implementation for all the functions required by the
 * management interface in SER, such as rpc->rpl_printf, rpc->add, rpc->struct_add
 * and so on. Whenever the management function calls one of the functions then
 * corresponding function in this module will be called to handle the request.
 *
 * The implementation functions build the reply, that will be sent to the
 * client, as they execute and they need to allocate memory to do that. That
 * memory must be freed again after the reply has been sent to the client. To
 * remember all the memory regions allocated during the execution of the
 * management function all functions within this module record all allocated
 * memory in the global variable called waste_bin. dispatch_rpc() functions
 * executes function collect_garbage() after the reply has been sent to the
 * client to free all memory that was allocated from the management function.
 * that was executed.
 *
 * <h2>Request Context</h2> 
 * Before the module calls a management function it prepares a structure
 * called context. The context is defined in structure rpc_ctx and it is
 * passed as one of parameter to the management function being called. The
 * context contains all the data that is needed during the execution of the
 * management function, such as the pointer to the request being processed, a
 * pointer to the reply being built, and so on.
 *
 * Another parameter to the management function being called is a structure
 * that contains pointers to all implementation functions. This structure is
 * of type rpc_t, this module keeps one global variable of that type called
 * func_param and a pointer to that variable is passed to all management
 * functions. The global variable is initialized in mod_init().
 */

/** @file 
 *
 * This is the main file of XMLRPC SER module which contains all the functions
 * related to XML-RPC processing, as well as the module interface.
 */

/*
 * FIXME: Decouple code and reason phrase from reply body
 *        Escape special characters in strings
 */

MODULE_VERSION

#if defined (__OS_darwin) || defined (__OS_freebsd)
/* redeclaration of functions from stdio.h throws errors */
#else
int snprintf(char *str, size_t size, const char *format, ...);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
#endif

static int process_xmlrpc(sip_msg_t* msg);
static int dispatch_rpc(sip_msg_t* msg, char* s1, char* s2);
static int xmlrpc_reply(sip_msg_t* msg, char* code, char* reason);
static int mod_init(void);

/* first line (w/o the version) of the sip msg created from the http xmlrpc */
#define XMLRPC_URI "sip:127.0.0.1:9"
#define XMLRPC_URI_LEN (sizeof(XMLRPC_URI)-1)

#define HTTP_GET		"GET"
#define HTTP_GET_LEN	(sizeof(HTTP_GET)-1)
#define HTTP_POST		"POST"
#define HTTP_POST_LEN	(sizeof(HTTP_POST)-1)
#define N_HTTP_GET		0x00746567U
#define N_HTTP_POST		0x74736f70U

#define LF "\n"

/** The beginning of XML document indicating an error.
 *
 * This is the beginning of the XML document that will be sent back to the
 * client when the server encountered an error.  It will be immediately
 * followed by a reason phrase.
 */
#define FAULT_PREFIX         \
"<?xml version=\"1.0\"?>" LF \
"<methodResponse>" LF        \
"<fault>" LF                 \
"<value>" LF                 \
"<struct>" LF                \
"<member>" LF                \
"<name>faultCode</name>" LF  \
"<value><int>"


/** The text of XML document indicating error that goes between reason code
 * and reason phrase.
 */
#define FAULT_BODY            \
"</int></value>" LF           \
"</member>" LF                \
"<member>" LF                 \
"<name>faultString</name>" LF \
"<value><string>"


/** The end of XML document that indicates an error.  
 *
 * This is the closing part of the XML-RPC document that indicates an error on
 * the server.
 */
#define FAULT_SUFFIX   \
"</string></value>" LF \
"</member>" LF         \
"</struct>" LF         \
"</value>" LF          \
"</fault>" LF          \
"</methodResponse>"


/** The beginning of XML-RPC reply sent to the client.
 */
#define SUCCESS_PREFIX       \
"<?xml version=\"1.0\"?>" LF \
"<methodResponse>" LF        \
"<params>" LF                \
"<param>" LF                 \
"<value>"


/** The closing part of XML-RPC reply document sent to
 * the client.
 */
#define SUCCESS_SUFFIX \
"</value>" LF          \
"</param>" LF          \
"</params>" LF         \
"</methodResponse>"

static str fault_prefix   = STR_STATIC_INIT(FAULT_PREFIX);
static str fault_body     = STR_STATIC_INIT(FAULT_BODY);
static str fault_suffix   = STR_STATIC_INIT(FAULT_SUFFIX);
static str success_prefix = STR_STATIC_INIT(SUCCESS_PREFIX);
static str success_suffix = STR_STATIC_INIT(SUCCESS_SUFFIX);
static str lf             = STR_STATIC_INIT(LF);
static str int_prefix     = STR_STATIC_INIT("<int>");
static str int_suffix     = STR_STATIC_INIT("</int>");
static str double_prefix  = STR_STATIC_INIT("<double>");
static str double_suffix  = STR_STATIC_INIT("</double>");
static str string_prefix  = STR_STATIC_INIT("<string>");
static str string_suffix  = STR_STATIC_INIT("</string>");
static str date_prefix    = STR_STATIC_INIT("<dateTime.iso8601>");
static str date_suffix    = STR_STATIC_INIT("</dateTime.iso8601>");
static str bool_prefix    = STR_STATIC_INIT("<boolean>");
static str bool_suffix    = STR_STATIC_INIT("</boolean>");
static str value_prefix   = STR_STATIC_INIT("<value>");
static str value_suffix   = STR_STATIC_INIT("</value>");
static str array_prefix   = STR_STATIC_INIT("<array><data>" LF);
static str array_suffix   = STR_STATIC_INIT("</data></array>");
static str struct_prefix  = STR_STATIC_INIT("<struct>");
static str struct_suffix  = STR_STATIC_INIT("</struct>");
static str member_prefix  = STR_STATIC_INIT("<member>");
static str member_suffix  = STR_STATIC_INIT("</member>");
static str name_prefix    = STR_STATIC_INIT("<name>");
static str name_suffix    = STR_STATIC_INIT("</name>");

/** Garbage collection data structure.
 *
 * This is the data structure used by the garbage collector in this module.
 * When the xmlrpc SER module identifies the management function to be called,
 * it calls corresponding function in SER. The function being called adds data
 * to the reply, that will be later sent to the client, as it executes. This
 * module needs to allocate memory for such data and the memory will be
 * re-claimed after the reply was sent out.  All the memory allocated this way
 * is recorded in this data structure so that it can be identified and
 * re-claimed later (when the reply is being sent out).
 * 
 */
static struct garbage {
	enum {
		JUNK_XMLCHAR,
		JUNK_RPCSTRUCT,    /**< This type indicates that the memory block was
						   * allocated for the RPC structure data type, this
						   * type needs to be freed differently as it may
						   * contain more allocated memory blocks
						   */
		JUNK_PKGCHAR 	  /** This type indicates a mxr_malloc'ed string */
	} type;               /**< Type of the memory block */
	void* ptr;            /**< Pointer to the memory block obtained from
							 mxr_malloc */
	struct garbage* next; /**< The linked list of all allocated memory
							 blocks */
} *waste_bin = 0;


/** Representation of the XML-RPC reply being constructed.
 *  
 * This data structure describes the XML-RPC reply that is being constructed
 * and will be sent to the client.
 */
struct xmlrpc_reply {
	int code;     /**< Reply code which indicates the type of the reply */
	char* reason; /**< Reason phrase text which provides human-readable
				   * description that augments the reply code */
	str body;     /**< The XML-RPC document body built so far */
	str buf;      /**< The memory buffer allocated for the reply, this is
				   * where the body attribute of the structure points to
				   */
};


/** The context of the XML-RPC request being processed.
 * 
 * This is the data structure that contains all data related to the XML-RPC
 * request being processed, such as the reply code and reason, data to be sent
 * to the client in the reply, and so on.
 *
 * There is always one context per XML-RPC request.
 */
typedef struct rpc_ctx {
	sip_msg_t* msg;        /**< The SIP/HTTP through which the RPC has been
							  received */
	struct xmlrpc_reply reply;  /**< XML-RPC reply to be sent to the client */
	struct rpc_struct* structs; /**< Structures to be added to the reply */
	int msg_shm_block_size; /**< non-zero for delayed reply contexts with
								shm cloned msgs */
	int reply_sent;             /**< The flag is set after a reply is sent,
								   this prevents a single reply being sent
								   twice */
	char* method;               /**< Name of the management function to be
								   called */
	unsigned int flags;         /**< Various flags, such as return value
								   type */
	xmlDocPtr doc;              /**< Pointer to the XML-RPC request
								   document */
	xmlNodePtr act_param;       /**< Pointer to the parameter being processed
								   in the XML-RPC request document */
} rpc_ctx_t;


/* extra rpc_ctx_t flags */
/* first 8 bits reserved for rpc flags (e.g. RET_ARRAY) */
#define XMLRPC_DELAYED_CTX_F	256
#define XMLRPC_DELAYED_REPLY_F	512

/** The structure represents a XML-RPC document structure.
 *
 * This is the data structure that represents XML-RPC structures that are sent
 * to the client in the XML-RPC reply documents. A XML-RPC document structure
 * is compound consting of name-value pairs.
 * @sa http://www.xml-rpc.com
 */
struct rpc_struct {
	int vtype;
	xmlNodePtr struct_in;           /**< Pointer to the structure parameter */
	struct xmlrpc_reply struct_out; /**< Structure to be sent in reply */
	struct xmlrpc_reply* reply;     /**< Print errors here */
	int n;                          /**< Number of structure members
									   created */
	xmlDocPtr doc;                  /**< XML-RPC document */
	int offset;                     /**< Offset in the reply where the
									   structure should be printed */
	struct rpc_struct* nnext;	/**< nested structure support - a recursive list of nested structrures */
	struct rpc_struct* parent;	/**< access to parent structure - used for flattening structure before reply */
	struct rpc_struct* next;
};


/** The context of the XML-RPC request being processed.
 *
 * This is a global variable that records the context of the XML-RPC request
 * being currently processed.  
 * @sa rpc_ctx
 */
static rpc_ctx_t ctx;

static void close_doc(rpc_ctx_t* ctx);
static void set_fault(struct xmlrpc_reply* reply, int code, char* fmt, ...);
static int fixup_xmlrpc_reply(void** param, int param_no);

/** Pointers to the functions that implement the RPC interface
 * of xmlrpc SER module
 */
static rpc_t func_param;

/** Enable/disable additional introspection methods.  If set to 1 then the
 * functions defined in http://scripts.incutio.com/xmlrpc/introspection.html
 * will be available on the server. If set to 0 then the functions will be
 * disabled.
 */
static char* xmlrpc_route=0; /* default is the main route */


/** Reference to the sl (stateless replies) module of SER The sl module of SER
 * is needed so that the xmlrpc SER module can send replies back to clients
 */
sl_api_t slb;

static int xmlrpc_route_no=DEFAULT_RT;
/* if set, try autoconverting to the requested type if possible
  (e.g. convert 1 to "1" if string is requested) */
static int autoconvert=0;
/* in replies, escape CR to &#xD (according to the xml specs) */
static int escape_cr=1; /* default on */
/* convert double LF to CR LF (when on, LFLF becomes an escape for CRLF, needed
 with some xmlrpc clients that are not escaping CR to &#xD; )*/
static int lflf2crlf=0; /* default off */
/* do not register for non-sip requests */
static int xmlrpc_mode = 0;

static char* xmlrpc_url_match = NULL;
static regex_t xmlrpc_url_match_regexp;
static char* xmlrpc_url_skip = NULL;
static regex_t xmlrpc_url_skip_regexp;


/*
 * Exported functions
 */
static cmd_export_t cmds[] = {
	{"dispatch_rpc", dispatch_rpc, 0, 0,                  REQUEST_ROUTE},
	{"xmlrpc_reply", xmlrpc_reply, 2, fixup_xmlrpc_reply, REQUEST_ROUTE},
	{0, 0, 0, 0, 0}
};


/*
 * Exported parameters
 */
static param_export_t params[] = {
	{"route",             PARAM_STRING, &xmlrpc_route},
	{"autoconversion",    PARAM_INT,    &autoconvert},
	{"escape_cr",         PARAM_INT,    &escape_cr},
	{"double_lf_to_crlf", PARAM_INT,    &lflf2crlf},
	{"mode",              PARAM_INT,    &xmlrpc_mode},
	{"url_match",         PARAM_STRING, &xmlrpc_url_match},
	{"url_skip",          PARAM_STRING, &xmlrpc_url_skip},
	{0, 0, 0}
};


struct module_exports exports = {
	"xmlrpc",
	cmds,           /* Exported commands */
	0,              /* Exported RPC methods */
	params,         /* Exported parameters */
	mod_init,       /* module initialization function */
	0,              /* response function*/
	0,              /* destroy function */
	0,              /* oncancel function */
	0               /* per-child init function */
};

/* XML-RPC reply helper functions */

#define ESC_LT "&lt;"
#define ESC_AMP "&amp;"
#define ESC_CR  "&#xD;"


static void clean_context(rpc_ctx_t* ctx);


/** Adds arbitrary text to the XML-RPC reply being constructed, special
 * characters < and & will be escaped.
 *
 * This function adds arbitrary text to the body of the XML-RPC reply being
 * constructed. Note well that the function does not check whether the XML
 * document being constructed is well-formed or valid. Use with care.
 *
 * @param reply Pointer to the structure representing the XML-RPC reply
 *              being constructed.
 * @param text The text to be appended to the XML-RPC reply.
 * @return -1 on error, 0 if the text was added successfuly.
 * @sa add_xmlrpc_reply()
 */
static int add_xmlrpc_reply_esc(struct xmlrpc_reply* reply, str* text)
{
    char* p;
    int i;

    for(i = 0; i < text->len; i++) {
		/* 10 must be bigger than size of longest escape sequence */
		if (reply->body.len >= reply->buf.len - 10) { 
			p = mxr_malloc(reply->buf.len + 1024);
			if (!p) {
				set_fault(reply, 500, 
						  "Internal Server Error (No memory left)");
				ERR("No memory left: %d\n", reply->body.len + 1024);
				return -1;
			}
			memcpy(p, reply->body.s, reply->body.len);
			mxr_free(reply->buf.s);
			reply->buf.s = p;
			reply->buf.len += 1024;
			reply->body.s = p;
		}
		
		switch(text->s[i]) {
		case '<':
			memcpy(reply->body.s + reply->body.len, ESC_LT, 
				   sizeof(ESC_LT) - 1);
			reply->body.len += sizeof(ESC_LT) - 1;
			break;
			
		case '&':
			memcpy(reply->body.s + reply->body.len, ESC_AMP, 
				   sizeof(ESC_AMP) - 1);
			reply->body.len += sizeof(ESC_AMP) - 1;
			break;
			
		case '\r':
			if (likely(escape_cr)){
				memcpy(reply->body.s + reply->body.len, ESC_CR,
					sizeof(ESC_CR) - 1);
				reply->body.len += sizeof(ESC_CR) - 1;
				break;
			}
			/* no break */
		default:
			reply->body.s[reply->body.len] = text->s[i];
			reply->body.len++;
			break;
		}
    }
    return 0;
}

/** Add arbitrary text to the XML-RPC reply being constructed, no escaping
 * done.
 * 
 * This is a more efficient version of add_xmlrpc_reply_esc(), the function
 * appends arbitrary text to the end of the XML-RPC reply being constructed,
 * but the text must not contain any characters that need to be escaped in
 * XML, such as < and & (or the characters must be escaped already).
 *
 * @param reply Pointer to the structure representing the XML-RPC reply
 *              being constructed.
 * @param text The text to be appended to the XML-RPC reply.
 * @return -1 on error, 0 if the text was added successfuly.
 * @sa add_xmlrpc_reply_esc()
 */
static int add_xmlrpc_reply(struct xmlrpc_reply* reply, str* text)
{
	char* p;
	if (text->len > (reply->buf.len - reply->body.len)) {
		p = mxr_malloc(reply->buf.len + text->len + 1024);
		if (!p) {
			set_fault(reply, 500, "Internal Server Error (No memory left)");
			ERR("No memory left: %d\n", reply->buf.len + text->len + 1024);
			return -1;
		}
		memcpy(p, reply->body.s, reply->body.len);
		mxr_free(reply->buf.s);
		reply->buf.s = p;
		reply->buf.len += text->len + 1024;
		reply->body.s = p;
	}
	memcpy(reply->body.s + reply->body.len, text->s, text->len);
	reply->body.len += text->len;
	return 0;
}

/** Adds arbitrary text to the XML-RPC reply being constructed, the text will
 * be inserted at a specified offset within the XML-RPC reply.
 *
 * This function inserts arbitrary text in the XML-RPC reply that is being
 * constructed, unlike add_xmlrp_reply(), this function will not append the
 * text at the end of the reply, but it will insert the text in the middle of
 * the reply at the position provided to the function in "offset"
 * parameter. The function does not escape special characters and thus the
 * text must not contain such characters (or the must be escaped already).
 *
 * @param reply The XML-RPC reply structure representing the reply being
 *              constructed.
 * @param offset The position of the first character where the text should be
 *               inserted. 
 * @param text The text to be inserted.
 * @return 0 of the text was inserted successfuly, a negative number on error.
 */
static int add_xmlrpc_reply_offset(struct xmlrpc_reply* reply, unsigned int offset, str* text)
{
	char* p;
	if (text->len > (reply->buf.len - reply->body.len)) {
		p = mxr_malloc(reply->buf.len + text->len + 1024);
		if (!p) {
			set_fault(reply, 500, "Internal Server Error (No memory left)");
			ERR("No memory left: %d\n", reply->buf.len + text->len + 1024);
			return -1;
		}
		memcpy(p, reply->body.s, reply->body.len);
		mxr_free(reply->buf.s);
		reply->buf.s = p;
		reply->buf.len += text->len + 1024;
		reply->body.s = p;
	}
	memmove(reply->body.s + offset + text->len, reply->body.s + offset, 
			reply->body.len - offset);
	memcpy(reply->body.s + offset, text->s, text->len);
	reply->body.len += text->len;
	return 0;
}


/** Returns the current length of the XML-RPC reply body.
 *
 * @param reply The XML-RPC reply being constructed
 * @return Number of bytes of the XML-RPC reply body.
 */
static unsigned int get_reply_len(struct xmlrpc_reply* reply)
{
	return reply->body.len;
}


/* Resets XMLRPC reply body.
 *
 * This function discards everything that has been written so far and starts
 * constructing the XML-RPC reply body from the beginning.
 *
 * @param reply The XML-RPC reply being constructed.
 */
static void reset_xmlrpc_reply(struct xmlrpc_reply* reply)
{
	reply->body.len = 0;
}

/** Initialize XML-RPC reply data structure.
 *
 * This function initializes the data structure that contains all data related
 * to the XML-RPC reply being created. The function must be called before any
 * other function that adds data to the reply.
 * @param reply XML-RPC reply structure to be initialized.
 * @return 0 on success, a negative number on error.
 */
static int init_xmlrpc_reply(struct xmlrpc_reply* reply)
{
	reply->code = 200;
	reply->reason = "OK";
	reply->buf.s = mxr_malloc(1024);
	if (!reply->buf.s) {
		set_fault(reply, 500, "Internal Server Error (No memory left)");
		ERR("No memory left\n");
		return -1;
	}
	reply->buf.len = 1024;
	reply->body.s = reply->buf.s;
	reply->body.len = 0;
	return 0;
}

/** Clear the XML-RPC reply code and sets it back to a success reply.
 *
 * @param reply XML-RPC reply structure to be cleared.
 */
static void clear_xmlrpc_reply(struct xmlrpc_reply* reply)
{
	reply->code = 200;
	reply->reason = "OK";
}


/* if this a delayed reply context, and it's never been use before, fix it */
static int fix_delayed_reply_ctx(rpc_ctx_t* ctx)
{
	if  ((ctx->flags & XMLRPC_DELAYED_CTX_F) && (ctx->reply.buf.s==0)){
		if (init_xmlrpc_reply(&ctx->reply) <0) return -1;
		add_xmlrpc_reply(&ctx->reply, &success_prefix);
		if (ctx->flags & RET_ARRAY)
			return add_xmlrpc_reply(&ctx->reply, &array_prefix);
	}
	return 0;
}



/** Free all memory used by the XML-RPC reply structure. */
static void clean_xmlrpc_reply(struct xmlrpc_reply* reply)
{
	if (reply->buf.s) mxr_free(reply->buf.s);
}

/** Create XML-RPC reply that indicates an error to the caller.
 *
 * This function is used to build the XML-RPC reply body that indicates that
 * an error ocurred on the server. It is called when a management function in
 * SER reports an error. The reply will contain the reason code and reason
 * phrase text provided by the management function that indicated the error.
 */
static int build_fault_reply(struct xmlrpc_reply* reply)
{
	str reason_s, code_s;

	reason_s.s = reply->reason;
	reason_s.len = strlen(reply->reason);
	code_s.s = int2str(reply->code, &code_s.len);
	reset_xmlrpc_reply(reply);
	if (add_xmlrpc_reply(reply, &fault_prefix) < 0) return -1;
	if (add_xmlrpc_reply_esc(reply, &code_s) < 0) return -1;
	if (add_xmlrpc_reply(reply, &fault_body) < 0) return -1;
	if (add_xmlrpc_reply_esc(reply, &reason_s) < 0) return -1;
	if (add_xmlrpc_reply(reply, &fault_suffix) < 0) return -1;
	return 0;
}


/** Add a memory registion to the list of memory blocks that
 * need to be re-claimed later.
 *
 * @param type The type of the memory block (ordinary text or structure).
 * @param ptr A pointer to the memory block.
 * @param reply The XML-RPC the memory block is associated with.
 * @return 0 on success, a negative number on error.
 * @sa collect_garbage()
 */
static int add_garbage(int type, void* ptr, struct xmlrpc_reply* reply)
{
	struct garbage* p;

	p = (struct garbage*)mxr_malloc(sizeof(struct garbage));
	if (!p) {
		set_fault(reply, 500, "Internal Server Error (No memory left)");
		ERR("Not enough memory\n");
		return -1;
	}

	p->type = type;
	p->ptr = ptr;
	p->next = waste_bin;
        waste_bin = p;
	return 0;
}

/** Re-claims all memory allocated in the process of building XML-RPC
 * reply.
 */
static void collect_garbage(void)
{
	struct rpc_struct* s;
	struct garbage* p;
	     /* Collect garbage */
	while(waste_bin) {
		p = waste_bin;
		waste_bin = waste_bin->next;
		switch(p->type) {
		case JUNK_XMLCHAR:
			if (p->ptr) xmlFree(p->ptr);
			break;

		case JUNK_RPCSTRUCT:
			s = (struct rpc_struct*)p->ptr;
			if (s && s->struct_out.buf.s) mxr_free(s->struct_out.buf.s);
			if (s) mxr_free(s);
			break;

		case JUNK_PKGCHAR:
			if (p->ptr){
				mxr_free(p->ptr);
				p->ptr=0;
			}
			break;

		default:
			ERR("BUG: Unsupported junk type\n");
		}
		mxr_free(p);
	}
}


/** Extract XML-RPC query from a SIP/HTTP message.
 *
 * @param doc A pointer to string descriptor that will be filled
 *            with the pointer to the beginning of the XML-RPC
 *            document and length of the document.
 * @param msg A structure representing the SIP/HTTP message 
 *            carrying the XML-RPC document in body.
 */
static int get_rpc_document(str* doc, sip_msg_t* msg)
{
 	doc->s = get_body(msg);
	if (!doc->s) {
	        ERR("Error while extracting message body\n");
		return -1;
	}
	doc->len = strlen(doc->s);
	return 0;
}


/** Send a reply to the client with given body.
 *
 * This function sends a 200 OK reply back to the client, the body of the
 * reply will contain text provided to the function in "body" parameter.
 *
 * @param msg The request that generated the reply.
 * @param body The text that will be put in the body of the reply.
 */
static int send_reply(sip_msg_t* msg, str* body)
{
	if (add_lump_rpl(msg, body->s, body->len, LUMP_RPL_BODY) < 0) {
		ERR("Error while adding reply lump\n");
		return -1;
	}

	if (slb.zreply(msg, 200, "OK") == -1) {
		ERR("Error while sending reply\n");
		return -1;
	}

	return 0;
}

static int flatten_nests(struct rpc_struct* st, struct xmlrpc_reply* reply) {
	if (!st)
		return 1;

	if (!st->nnext) {
		if(st->vtype == RET_ARRAY) {
			if (add_xmlrpc_reply(&st->struct_out, &array_suffix) < 0) return -1;
		} else {
			if (add_xmlrpc_reply(&st->struct_out, &struct_suffix) < 0) return -1;
		}
		if (add_xmlrpc_reply_offset(&st->parent->struct_out, st->offset, &st->struct_out.body) < 0) return -1;
	} else {
		flatten_nests(st->nnext, reply);
		if(st->vtype == RET_ARRAY) {
			if (add_xmlrpc_reply(&st->struct_out, &array_suffix) < 0) return -1;
		} else {
			if (add_xmlrpc_reply(&st->struct_out, &struct_suffix) < 0) return -1;
		}
		if (add_xmlrpc_reply_offset(&st->parent->struct_out, st->offset, &st->struct_out.body) < 0) return -1;
	}
	return 1;
}

static int print_structures(struct xmlrpc_reply* reply, 
							struct rpc_struct* st)
{
	while(st) {
		     /* Close the structure first */
		if(st->vtype == RET_ARRAY) {
			if (add_xmlrpc_reply(&st->struct_out, &array_suffix) < 0) return -1;
		} else {
			if (add_xmlrpc_reply(&st->struct_out, &struct_suffix) < 0) return -1;
		}
		if (flatten_nests(st->nnext, &st->struct_out) < 0) return -1;
		if (add_xmlrpc_reply_offset(reply, st->offset, &st->struct_out.body) < 0) return -1;
		st = st->next;
	}
	return 0;
}

/** Implementation of rpc_send function required by the management API in SER.
 *
 * This is the function that will be called whenever a management function in
 * SER asks the management interface to send the reply to the client. The
 * function will generate the XML-RPC document, put it in body of a SIP
 * response and send the response to the client. The SIP/HTTP reply sent to
 * the client will be always 200 OK, if an error ocurred on the server then it
 * will be indicated in the XML document in body.
 *
 * @param ctx A pointer to the context structure of the XML-RPC request that
 *            generated the reply.  
 * @return 1 if the reply was already sent, 0 on success, a negative number on
 *            error
 */
static int rpc_send(rpc_ctx_t* ctx)
{
	struct xmlrpc_reply* reply;

	if (ctx->reply_sent) return 1;

	reply = &ctx->reply;
	if (reply->code >= 300) {
		if (build_fault_reply(reply) < 0) return -1;
	} else {
		if (ctx->flags & RET_ARRAY && 
			add_xmlrpc_reply(reply, &array_suffix) < 0) return -1;
		if (ctx->structs && 
			print_structures(reply, ctx->structs) < 0) return -1;
		if (add_xmlrpc_reply(reply, &success_suffix) < 0) return -1;
	}
	if (send_reply(ctx->msg, &reply->body) < 0) return -1;
	ctx->reply_sent = 1;
	return 0;
}


#define REASON_BUF_LEN 1024

static void set_fault(struct xmlrpc_reply* reply, int code, char* fmt, ...)
{
	static char buf[REASON_BUF_LEN];
	va_list ap;

	reply->code = code;
	va_start(ap, fmt);
	vsnprintf(buf, REASON_BUF_LEN, fmt, ap);
	va_end(ap);
	reply->reason = buf;
}

/** Implementation of rpc_fault function required by the management API in 
 * SER.
 *
 * This function will be called whenever a management function in SER
 * indicates that an error ocurred while it was processing the request. The
 * function takes the reply code and reason phrase as parameters, these will
 * be put in the body of the reply.
 *
 * @param ctx A pointer to the context structure of the request being 
 *            processed.
 * @param code Reason code.
 * @param fmt Formatting string used to build the reason phrase.
 */
static void rpc_fault(rpc_ctx_t* ctx, int code, char* fmt, ...)
{
	static char buf[REASON_BUF_LEN];
	va_list ap;

	ctx->reply.code = code;
	va_start(ap, fmt);
	vsnprintf(buf, REASON_BUF_LEN, fmt, ap);
	va_end(ap);
	ctx->reply.reason = buf;
}

/** Create and initialize a new rpc_structure data structure.
 *
 * This function allocates and initializes memory for a new rpc_struct
 * structure. If the caller provided non-NULL pointers in doc and structure
 * parameters then the structure is coming from an XML-RPC request. If either
 * of the pointers is NULL then we are creating a structure that will be
 * attached to a XML-RPC reply sent to the client. The memory allocated in
 * this function will be added to the garbage collection list.
 *
 * @param doc A pointer to the XML-RPC request document or NULL if we create
 *            a structure that will be put in a reply.
 * @param structure A pointer to opening tag of the structure in the XML-RPC
 *                  request document or NULL if we create a structure that
 *                  will be put in a XML-RPC reply.
 * @param reply A pointer to xml_reply structure, NULL if it is a structure
 *              coming from a XML-RPC request.
 */
static struct rpc_struct* new_rpcstruct(xmlDocPtr doc, xmlNodePtr structure, 
										struct xmlrpc_reply* reply, int vtype)
{
	struct rpc_struct* p;

	p = (struct rpc_struct*)mxr_malloc(sizeof(struct rpc_struct));
	if (!p) {
		set_fault(reply, 500, "Internal Server Error (No Memory Left");
		return 0;
	}
	memset(p, 0, sizeof(struct rpc_struct));
	p->struct_in = structure;

	p->reply = reply;
	p->n = 0;
	p->vtype = vtype;
	if (doc && structure) {
		     /* We will be parsing structure from request */
		p->doc = doc;
		p->struct_in = structure;
	} else {
		     /* We will build a reply structure */
		if (init_xmlrpc_reply(&p->struct_out) < 0) goto err;
		if(vtype==RET_ARRAY) {
			if (add_xmlrpc_reply(&p->struct_out, &array_prefix) < 0) goto err;
		} else {
			if (add_xmlrpc_reply(&p->struct_out, &struct_prefix) < 0) goto err;
		}
	}
	if (add_garbage(JUNK_RPCSTRUCT, p, reply) < 0) goto err;
	return p;

 err:
	if (p->struct_out.buf.s) mxr_free(p->struct_out.buf.s);
	mxr_free(p);
	return 0;
}

/** Converts the variables provided in parameter ap according to formatting
 * string provided in parameter fmt into parameters in XML-RPC format.
 *
 * This function takes the parameters provided in ap parameter and creates
 * XML-RPC formatted parameters that will be put in the document in res
 * parameter. The format of input parameters is described in formatting string
 * fmt which follows the syntax of the management API in SER. In the case of
 * an error the function will generate an error reply in err_reply parameter
 * instead.
 * @param res A pointer to the XML-RPC result structure where the parameters
 *            will be written.
 * @param err_reply An error reply document will be generated here if the
 *                  function encounters a problem while processing input
 *                  parameters.
 * @param fmt Formatting string of the management API in SER.
 * @param ap A pointer to the array of input parameters.
 *
 */
static int print_value(struct xmlrpc_reply* res, 
					   struct xmlrpc_reply* err_reply, char fmt, va_list* ap)
{
	str prefix, body, suffix;
	str* sp;
	char buf[256];
	time_t dt;
	struct tm* t;

	switch(fmt) {
	case 'd':
		prefix = int_prefix;
		suffix = int_suffix;
		body.s = sint2str(va_arg(*ap, int), &body.len);
		break;

	case 'u':
		prefix = int_prefix;
		suffix = int_suffix;
		body.s = int2str(va_arg(*ap, unsigned int), &body.len);
		break;

	case 'f':
		prefix = double_prefix;
		suffix = double_suffix;
		body.s = buf;
		body.len = snprintf(buf, 256, "%f", va_arg(*ap, double));
		if (body.len < 0) {
			set_fault(err_reply, 400, "Error While Converting double");
			ERR("Error while converting double\n");
			goto err;
		}
		break;

	case 'b':
		prefix = bool_prefix;
		suffix = bool_suffix;
		body.len = 1;
		body.s = ((va_arg(*ap, int) == 0) ? "0" : "1");
		break;

	case 't':
		prefix = date_prefix;
		suffix = date_suffix;
		body.s = buf;
		body.len = sizeof("19980717T14:08:55") - 1;
		dt = va_arg(*ap, time_t);
		t = gmtime(&dt);
		if (strftime(buf, 256, "%Y%m%dT%H:%M:%S", t) == 0) {
			set_fault(err_reply, 400, "Error While Converting datetime");
			ERR("Error while converting time\n");
			goto err;
		}
		break;

	case 's':
		prefix = string_prefix;
		suffix = string_suffix;
		body.s = va_arg(*ap, char*);
		body.len = strlen(body.s);
		break;

	case 'S':
		prefix = string_prefix;
		suffix = string_suffix;
		sp = va_arg(*ap, str*);
		body = *sp;
		break;

	default:
		set_fault(err_reply, 500, "Bug In SER (Invalid formatting character)");
		ERR("Invalid formatting character [%c]\n", fmt);
		goto err;
	}

	if (add_xmlrpc_reply(res, &prefix) < 0) goto err;
	if (add_xmlrpc_reply_esc(res, &body) < 0) goto err;
	if (add_xmlrpc_reply(res, &suffix) < 0) goto err;
	return 0;
 err:
	return -1;
}

/** Implementation of rpc_add function required by the management API in SER.
 *
 * This function will be called when a management function in SER calls
 * rpc->add to add a parameter to the XML-RPC reply being generated.
 */
static int rpc_add(rpc_ctx_t* ctx, char* fmt, ...)
{
	void* void_ptr;
	va_list ap;
	struct xmlrpc_reply* reply;
	struct rpc_struct* p;

	fix_delayed_reply_ctx(ctx);
	va_start(ap, fmt);
	reply = &ctx->reply;

	while(*fmt) {
		if (ctx->flags & RET_ARRAY && 
			add_xmlrpc_reply(reply, &value_prefix) < 0) goto err;
		if (*fmt == '{' || *fmt == '[') {
			void_ptr = va_arg(ap, void**);
			p = new_rpcstruct(0, 0, reply, (*fmt=='[')?RET_ARRAY:0);
			if (!p) goto err;
			*(struct rpc_struct**)void_ptr = p;
			p->offset = get_reply_len(reply);
			p->next = ctx->structs;
			ctx->structs = p;
		} else {
			if (print_value(reply, reply, *fmt, &ap) < 0) goto err;
		}

		if (ctx->flags & RET_ARRAY && 
			add_xmlrpc_reply(reply, &value_suffix) < 0) goto err;
		if (add_xmlrpc_reply(reply, &lf) < 0) goto err;
		fmt++;
	}
	va_end(ap);
	return 0;
 err:
	va_end(ap);
	return -1;
}


/** Convert time in XML-RPC format to time_t */
static time_t xmlrpc2time(const char* str)
{
	struct tm time;

	memset(&time, '\0', sizeof(struct tm));
	strptime(str, "%Y%m%dT%H:%M:%S", &time);
	time.tm_isdst = -1;
#ifdef HAVE_TIMEGM
	return timegm(&time);
#else
	return _timegm(&time);
#endif /* HAVE_TIMEGM */
}



/* get_* flags: */
#define GET_X_AUTOCONV 1
#define GET_X_NOREPLY 2
#define GET_X_LFLF2CRLF 4  /* replace "\n\n" with "\r\n" */

/* xml value types */
enum xmlrpc_val_type{
	XML_T_STR,
	XML_T_TXT,
	XML_T_INT,
	XML_T_BOOL,
	XML_T_DATE,
	XML_T_DOUBLE,
	XML_T_ERR=-1
};



/** Returns the XML-RPC value type.
 * @return value type (>= on success, XML_T_ERR on error/unknown type)
 */
static enum xmlrpc_val_type xml_get_type(xmlNodePtr value)
{
	if (!xmlStrcmp(value->name, BAD_CAST "string")){
		return XML_T_STR;
	} else if (!xmlStrcmp(value->name, BAD_CAST "text")) {
		return XML_T_TXT;
	} else if ( !xmlStrcmp(value->name, BAD_CAST "i4") ||
				!xmlStrcmp(value->name, BAD_CAST "int")) {
		return XML_T_INT;
	} else if (!xmlStrcmp(value->name, BAD_CAST "boolean")) {
		return XML_T_BOOL;
	} else if (!xmlStrcmp(value->name, BAD_CAST "dateTime.iso8601")) {
		return XML_T_DATE;
	}else if (!(xmlStrcmp(value->name, BAD_CAST "double"))){
		return XML_T_DOUBLE;
	}
	return XML_T_ERR;
}



/** Converts an XML-RPC encoded parameter into integer if possible.
 *
 * This function receives a pointer to a parameter encoded in XML-RPC format
 * and tries to convert the value of the parameter into integer.  Only
 * &lt;i4&gt;, &lt;int&gt;, &lt;boolean&gt;, &lt;dateTime.iso8601&gt; XML-RPC
 * parameters can be converted to integer, attempts to conver other types will
 * fail.
 * @param val A pointer to an integer variable where the result will be 
 *            stored.  
 * @param reply A pointer to XML-RPC reply being constructed (used to 
 *        indicate conversion errors).  
 * @param doc A pointer to the XML-RPC request document.  
 * @param value A pointer to the element containing the parameter to be 
 *              converted within the document.
 * @param flags : GET_X_AUTOCONV - try autoconverting
 *                GET_X_NOREPLY - do not reply
 * @return <0 on error, 0 on success
 */
static int get_int(int* val, struct xmlrpc_reply* reply, 
				   xmlDocPtr doc, xmlNodePtr value, int flags)
{
	enum xmlrpc_val_type type;
	int ret;
	xmlNodePtr i4;
	char* val_str;
	char* end_ptr;

	if (!value || xmlStrcmp(value->name, BAD_CAST "value")) {
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Invalid parameter value");
		return -1;
	}

	i4 = value->xmlChildrenNode;
	if (!i4){
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Invalid Parameter Type");
		return -1;
	}
	type=xml_get_type(i4);
	switch(type){
		case XML_T_INT:
		case XML_T_BOOL:
		case XML_T_DATE:
			break;
		case XML_T_DOUBLE:
		case XML_T_STR:
		case XML_T_TXT:
			if (flags & GET_X_AUTOCONV)
				break;
		case XML_T_ERR:
			if (!(flags & GET_X_NOREPLY))
				set_fault(reply, 400, "Invalid Parameter Type");
			return -1;
	}
	if (type == XML_T_TXT)
		val_str = (char*)i4->content;
	else
		val_str = (char*)xmlNodeListGetString(doc, i4->xmlChildrenNode, 1);
	if (!val_str) {
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Empty Parameter Value");
		return -1;
	}
	ret=0;
	switch(type){
		case XML_T_INT:
		case XML_T_BOOL:
		case XML_T_STR:
		case XML_T_TXT:
			/* Integer/bool conversion */
			*val = strtol(val_str, &end_ptr, 10);
			if (val_str==end_ptr)
				ret=-1;
			break;
		case XML_T_DATE:
			*val = xmlrpc2time(val_str);
			break;
		case XML_T_DOUBLE:
			*val = (int)strtod(val_str, &end_ptr);
			if (val_str==end_ptr)
				ret=-1;
			break;
		case XML_T_ERR:
			*val=0;
			ret=-1;
			break;
	}
	xmlFree(val_str);
	if (ret==-1 && !(flags & GET_X_NOREPLY))
		set_fault(reply, 400, "Invalid Value");
	return ret;
}



/** Converts an XML-RPC encoded parameter into double if possible.
 *
 * This function receives a pointer to a parameter encoded in XML-RPC format
 * and tries to convert the value of the parameter into double.  Only
 * &lt;i4&gt;, &lt;int&gt;, &lt;double&gt; XML-RPC parameters can be converted
 * to double, attempts to conver other types will fail.
 * @param val A pointer to an integer variable where the result will be 
 *            stored.
 * @param reply A pointer to XML-RPC reply being constructed (used to indicate
 *              conversion errors).
 * @param doc A pointer to the XML-RPC request document.
 * @param value A pointer to the element containing the parameter to be 
 *              converted within the document.
 * @param flags : GET_X_AUTOCONV - try autoconverting
 *                GET_X_NOREPLY - do not reply
 * @return <0 on error, 0 on success
 */
static int get_double(double* val, struct xmlrpc_reply* reply, 
					  xmlDocPtr doc, xmlNodePtr value, int flags)
{
	xmlNodePtr dbl;
	char* val_str;
	char* end_ptr;
	enum xmlrpc_val_type type;
	int ret;

	if (!value || xmlStrcmp(value->name, BAD_CAST "value")) {
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Invalid Parameter Value");
		return -1;
	}

	dbl = value->xmlChildrenNode;
	if (!dbl){
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Invalid Parameter Type");
		return -1;
	}
	type=xml_get_type(dbl);
	switch(type){
		case XML_T_DOUBLE:
		case XML_T_INT:
			break;
		case XML_T_BOOL:
		case XML_T_DATE:
		case XML_T_STR:
		case XML_T_TXT:
			if (flags & GET_X_AUTOCONV)
				break;
		case XML_T_ERR:
			if (!(flags & GET_X_NOREPLY))
				set_fault(reply, 400, "Invalid Parameter Type");
			return -1;
	}
	if (type == XML_T_TXT)
		val_str = (char*)dbl->content;
	else
		val_str = (char*)xmlNodeListGetString(doc, dbl->xmlChildrenNode, 1);
	if (!val_str) {
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Empty Double Parameter");
		return -1;
	}
	ret=0;
	switch(type){
		case XML_T_DOUBLE:
		case XML_T_INT:
		case XML_T_BOOL:
		case XML_T_STR:
		case XML_T_TXT:
			*val = strtod(val_str, &end_ptr);
			if (val_str==end_ptr)
				ret=-1;
			break;
		case XML_T_DATE:
			*val = (double)xmlrpc2time(val_str);
			break;
		case XML_T_ERR:
			*val=0;
			ret=-1;
			break;
	}
	xmlFree(val_str);
	if (ret==-1 && !(flags & GET_X_NOREPLY))
		set_fault(reply, 400, "Invalid Value");
	return ret;
}


/** Convert a parameter encoded in XML-RPC to a zero terminated string.
 *
 * @param val A pointer to a char* variable where the result will be 
 *            stored (the result is dynamically allocated, but it's garbage
 *            collected, so it doesn't have to be freed)
 * @param reply A pointer to XML-RPC reply being constructed (used to indicate
 *              conversion errors).
 * @param doc A pointer to the XML-RPC request document.
 * @param value A pointer to the element containing the parameter to be 
 *              converted within the document.
 * @param flags 
 *              - GET_X_AUTOCONV - try autoconverting
 *              - GET_X_LFLF2CRLF - replace double '\\n' with `\\r\\n'
 *              - GET_X_NOREPLY - do not reply
 * @return <0 on error, 0 on success
 */
static int get_string(char** val, struct xmlrpc_reply* reply, 
					  xmlDocPtr doc, xmlNodePtr value, int flags)
{
	static char* null_str = "";
	xmlNodePtr dbl;
	char* val_str;
	char* end_ptr;
	char* s;
	char* p;
	int i;
	int len;
	enum xmlrpc_val_type type;
	int ret;

	if (!value || xmlStrcmp(value->name, BAD_CAST "value")) {
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Invalid Parameter Value");
		return -1;
	}

	dbl = value->xmlChildrenNode;
	if (!dbl){
		if (!(flags & GET_X_NOREPLY))
			set_fault(reply, 400, "Invalid Parameter Type");
		return -1;
	}
	type=xml_get_type(dbl);
	switch(type){
		case XML_T_STR:
		case XML_T_TXT:
			break;
		case XML_T_INT:
		case XML_T_BOOL:
		case XML_T_DATE:
		case XML_T_DOUBLE:
			if (flags & GET_X_AUTOCONV)
				break;
		case XML_T_ERR:
			if (!(flags & GET_X_NOREPLY))
				set_fault(reply, 400, "Invalid Parameter Type");
			return -1;
	}
	if (type == XML_T_TXT)
		val_str = (char*)dbl->content;
	else
		val_str = (char*)xmlNodeListGetString(doc, dbl->xmlChildrenNode, 1);

	if (!val_str) {
		if (type==XML_T_STR || type==XML_T_TXT){
			*val = null_str;
			return 0;
		}else{
			if (!(flags & GET_X_NOREPLY))
				set_fault(reply, 400, "Empty Parameter Value");
			return -1;
		}
	}
	ret=0;
	switch(type){
		case XML_T_STR:
		case XML_T_TXT:
			if (flags & GET_X_LFLF2CRLF){
				p=val_str;
				while(*p){
					if (*p=='\n' && *(p+1)=='\n'){
						*p='\r';
						p+=2;
						continue;
					}
					p++;
				}
			}
			/* no break */
		case XML_T_DATE:  /* no special conversion */
		case XML_T_DOUBLE: /* no special conversion */
			if (add_garbage(JUNK_XMLCHAR, val_str, reply) < 0){
				xmlFree(val_str);
				return -1;
			}
			*val = val_str;
			break;
		case XML_T_INT:
		case XML_T_BOOL:
			/* convert str to int an back to str */
			i = strtol(val_str, &end_ptr, 10);
			if (val_str==end_ptr){
				ret=-1;
			}else{
				s=sint2str(i, &len);
				p=mxr_malloc(len+1);
				if (p && add_garbage(JUNK_PKGCHAR, p, reply) == 0){
					memcpy(p, s, len);
					p[len]=0;
					*val=p;
				}else{
					ret=-1;
					if (p) mxr_free(p);
				}
			}
			xmlFree(val_str);
			break;
		case XML_T_ERR:
			xmlFree(val_str);
			ret=-1;
			break;
	}
	return ret;
}



/** Implementation of rpc->scan function required by the management API in
 * SER.
 *
 * This is the function that will be called whenever a management function in
 * SER calls rpc->scan to get the value of parameter from the XML-RPC
 * request. This function will extract the current parameter from the XML-RPC
 * document and attempts to convert it to the type requested by the management
 * function that called it.
 */
static int rpc_scan(rpc_ctx_t* ctx, char* fmt, ...)
{
	int read;
	int ival;
	int* int_ptr;
	unsigned int* uint_ptr;
	char** char_ptr;
	str* str_ptr;
	double* double_ptr;
	void** void_ptr;
	xmlNodePtr value;
	struct xmlrpc_reply* reply;
	struct rpc_struct* p;
	int modifiers;
	int f;
	va_list ap;
	int nofault;

	reply = &ctx->reply;
	/* clear the previously saved error code */
	clear_xmlrpc_reply(reply);

	va_start(ap, fmt);
	modifiers=0;
	read = 0;
	nofault = 0;
	f=(autoconvert?GET_X_AUTOCONV:0) |
		(lflf2crlf?GET_X_LFLF2CRLF:0);
	while(*fmt) {
		if (!ctx->act_param) goto error;
		value = ctx->act_param->xmlChildrenNode;

		switch(*fmt) {
		case '*': /* start of optional parameters */
			modifiers++;
			read++;
			fmt++;
			nofault=1;
			f|=GET_X_NOREPLY;
			continue; /* do not advance ctx->act-param */
		case '.': /* autoconvert */
			modifiers++;
			read++;
			fmt++;
			f|=GET_X_AUTOCONV;
			continue; /* do not advance ctx->act-param */
		case 'b': /* Bool */
		case 't': /* Date and time */
		case 'd': /* Integer */
			int_ptr = va_arg(ap, int*);
			if (get_int(int_ptr, reply, ctx->doc, value, f) < 0) goto error;
			break;

		case 'u': /* Integer */
			uint_ptr = va_arg(ap, unsigned int*);
			if (get_int(&ival, reply, ctx->doc, value, f) < 0) goto error;
			*uint_ptr = (unsigned int)ival;
			break;
			
		case 'f': /* double */
			double_ptr = va_arg(ap, double*);
			if (get_double(double_ptr, reply, ctx->doc, value, f) < 0) {
				goto error;
			}
			break;

		case 's': /* zero terminated string */
			char_ptr = va_arg(ap, char**);
			if (get_string(char_ptr, reply, ctx->doc, value, f) < 0)
				goto error;
			break;

		case 'S': /* str structure */
			str_ptr = va_arg(ap, str*);
			if (get_string(&str_ptr->s, reply, ctx->doc, value, f) < 0) {
				goto error;
			}
			str_ptr->len = strlen(str_ptr->s);
			break;

		case '{':
			void_ptr = va_arg(ap, void**);
			if (!value->xmlChildrenNode) goto error;
			p = new_rpcstruct(ctx->doc, value->xmlChildrenNode, reply, 0);
			if (!p) goto error;
			*void_ptr = p;
			break;

		default:
			ERR("Invalid parameter type in formatting string: %c\n", *fmt);
			set_fault(reply, 500, 
					  "Server Internal Error (Invalid Formatting String)");
			goto error;
		}
		ctx->act_param = ctx->act_param->next;
		/* clear autoconv if not globally on */
		f=autoconvert?GET_X_AUTOCONV:(f&~GET_X_AUTOCONV);
		read++;
		fmt++;
	}
	va_end(ap);
	return read-modifiers;

 error:
	va_end(ap);
	if(nofault==0)
		return -(read-modifiers);
	else
		return read-modifiers;
}

#define RPC_BUF_SIZE 1024


/** Implementation of rpc_rpl_printf function required by the management API in
 *	SER.
 *
 * This function will be called whenever a management function in SER calls
 * rpc-printf to add a parameter to the XML-RPC reply being constructed.
 */
static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...)
{
	int n, buf_size;
	char* buf;
	va_list ap;
	str s;
	struct xmlrpc_reply* reply;

	fix_delayed_reply_ctx(ctx);
	reply = &ctx->reply;
	buf = (char*)mxr_malloc(RPC_BUF_SIZE);
	if (!buf) {
		set_fault(reply, 500, "Internal Server Error (No memory left)");
		ERR("No memory left\n");
		return -1;
	}

	buf_size = RPC_BUF_SIZE;
	while (1) {
		     /* Try to print in the allocated space. */
		va_start(ap, fmt);
		n = vsnprintf(buf, buf_size, fmt, ap);
		va_end(ap);
		     /* If that worked, return the string. */
		if (n > -1 && n < buf_size) {
			s.s = buf;
			s.len = n;
			if (ctx->flags & RET_ARRAY && 
				add_xmlrpc_reply(reply, &value_prefix) < 0) goto err;
			if (add_xmlrpc_reply(reply, &string_prefix) < 0) goto err;
			if (add_xmlrpc_reply_esc(reply, &s) < 0) goto err;
			if (add_xmlrpc_reply(reply, &string_suffix) < 0) goto err;
			if (ctx->flags & RET_ARRAY && 
				add_xmlrpc_reply(reply, &value_suffix) < 0) goto err;
			if (add_xmlrpc_reply(reply, &lf) < 0) goto err;
			mxr_free(buf);
			return 0;
		}
		     /* Else try again with more space. */
		if (n > -1) {   /* glibc 2.1 */
			buf_size = n + 1; /* precisely what is needed */
		} else {          /* glibc 2.0 */
			buf_size *= 2;  /* twice the old size */
		}
		if ((buf = mxr_realloc(buf, buf_size)) == 0) {
			set_fault(reply, 500, "Internal Server Error (No memory left)");
			ERR("No memory left\n");
			goto err;
		}
	}
	return 0;
 err:
	if (buf) mxr_free(buf);
	return -1;
}

/* Structure manipulation functions */

/** Find a structure member by name.
 */
static int find_member(xmlNodePtr* value, xmlDocPtr doc, xmlNodePtr structure,
					   struct xmlrpc_reply* reply, char* member_name)
{
	char* name_str;
	xmlNodePtr member, name;

	if (!structure) {
		set_fault(reply, 400, "Invalid Structure Parameter");
		return -1;
	}

	member = structure->xmlChildrenNode;
	while(member) {
		name = member->xmlChildrenNode;
		     /* Find <name> node in the member */
		while(name) {
			if (!xmlStrcmp(name->name, BAD_CAST "name")) break;
			name = name->next;
		}
		if (!name) {
			set_fault(reply, 400, "Member Name Not Found In Structure");
			return -1;
		}

		     /* Check the value of <name> node in the structure member */
		name_str = (char*)xmlNodeListGetString(doc, name->xmlChildrenNode, 1);
		if (!name_str) {
			set_fault(reply, 400, "Empty name Element of Structure Parameter");
			return -1;
		}
		if (strcmp(name_str, member_name)) {
			xmlFree(name_str);
			goto skip;
		}
		xmlFree(name_str);

		*value = member->xmlChildrenNode;
		while(*value) {
			if (!xmlStrcmp((*value)->name, BAD_CAST "value")) break;
			(*value) = (*value)->next;
		}
		if (!(*value)) {
			set_fault(reply, 400, "Member Value Not Found In Structure");
			return -1;
		}
		return 0;
	skip:
		member = member->next;
	}
	return 1;
}

/** Adds a new member to structure.
 */
static int rpc_struct_add(struct rpc_struct* s, char* fmt, ...)
{
	va_list ap;
	str member_name;
	struct xmlrpc_reply* reply;
	void* void_ptr;
	struct rpc_struct* p, *tmp;

	reply = &s->struct_out;

	va_start(ap, fmt);
	while(*fmt) {
		member_name.s = va_arg(ap, char*);
		member_name.len = (member_name.s ? strlen(member_name.s) : 0);

		if(s->vtype==RET_ARRAY && *fmt == '{') {
			if (add_xmlrpc_reply(reply, &value_prefix) < 0) goto err;
			if (add_xmlrpc_reply(reply, &struct_prefix) < 0) goto err;
		}
		if (add_xmlrpc_reply(reply, &member_prefix) < 0) goto err;
		if (add_xmlrpc_reply(reply, &name_prefix) < 0) goto err;
		if (add_xmlrpc_reply_esc(reply, &member_name) < 0) goto err;
		if (add_xmlrpc_reply(reply, &name_suffix) < 0) goto err;
		if (add_xmlrpc_reply(reply, &value_prefix) < 0) goto err;
		if (*fmt == '{' || *fmt == '[') {
			void_ptr = va_arg(ap, void**);
			p = new_rpcstruct(0, 0, s->reply, (*fmt=='[')?RET_ARRAY:0);
			if (!p)
				goto err;
			*(struct rpc_struct**) void_ptr = p;
			p->offset = get_reply_len(reply);
			p->parent = s;
			if (!s->nnext) {
				s->nnext = p;
			} else {
				for (tmp = s; tmp->nnext; tmp=tmp->nnext);
				tmp->nnext = p;
			}
		} else {
			if (print_value(reply, reply, *fmt, &ap) < 0) goto err;
		}
		if (add_xmlrpc_reply(reply, &value_suffix) < 0) goto err;
		if (add_xmlrpc_reply(reply, &member_suffix) < 0) goto err;
		if(s->vtype==RET_ARRAY && *fmt == '{') {
			if (add_xmlrpc_reply(reply, &struct_suffix) < 0) goto err;
			if (add_xmlrpc_reply(reply, &value_suffix) < 0) goto err;
		}
		fmt++;
	}

	va_end(ap);
	return 0;
 err:
	va_end(ap);
	return -1;
}

/** Adds a new value to an array.
 */
static int rpc_array_add(struct rpc_struct* s, char* fmt, ...)
{
	va_list ap;
	struct xmlrpc_reply* reply;
	void* void_ptr;
	struct rpc_struct* p, *tmp;

	reply = &s->struct_out;
	if(s->vtype!=RET_ARRAY) {
		LM_ERR("parent structure is not an array\n");
		goto err;
	}

	va_start(ap, fmt);
	while(*fmt) {
		if (*fmt == '{' || *fmt == '[') {
			void_ptr = va_arg(ap, void**);
			p = new_rpcstruct(0, 0, s->reply, (*fmt=='[')?RET_ARRAY:0);
			if (!p)
				goto err;
			*(struct rpc_struct**) void_ptr = p;
			p->offset = get_reply_len(reply);
			p->parent = s;
			if (!s->nnext) {
				s->nnext = p;
			} else {
				for (tmp = s; tmp->nnext; tmp=tmp->nnext);
				tmp->nnext = p;
			}
		} else {
			if (print_value(reply, reply, *fmt, &ap) < 0) goto err;
		}
		fmt++;
	}

	va_end(ap);
	return 0;
 err:
	va_end(ap);
	return -1;
}

/** Create a new member from formatting string and add it to a structure.
 */
static int rpc_struct_printf(struct rpc_struct* s, char* member_name, 
							 char* fmt, ...)
{
	int n, buf_size;
	char* buf;
	va_list ap;
	str st, name;
	struct xmlrpc_reply* reply;
	struct xmlrpc_reply* out;

	out = &s->struct_out;
	buf = (char*)mxr_malloc(RPC_BUF_SIZE);
	reply = s->reply;
	if (!buf) {
		set_fault(reply, 500, "Internal Server Error (No memory left)");
		ERR("No memory left\n");
		return -1;
	}

	buf_size = RPC_BUF_SIZE;
	while (1) {
		     /* Try to print in the allocated space. */
		va_start(ap, fmt);
		n = vsnprintf(buf, buf_size, fmt, ap);
		va_end(ap);
		     /* If that worked, return the string. */
		if (n > -1 && n < buf_size) {
			st.s = buf;
			st.len = n;

			name.s = member_name;
			name.len = strlen(member_name);

			if (add_xmlrpc_reply(out, &member_prefix) < 0) goto err;
			if (add_xmlrpc_reply(out, &name_prefix) < 0) goto err;
			if (add_xmlrpc_reply_esc(out, &name) < 0) goto err;
			if (add_xmlrpc_reply(out, &name_suffix) < 0) goto err;
			if (add_xmlrpc_reply(out, &value_prefix) < 0) goto err;

			if (add_xmlrpc_reply(out, &string_prefix) < 0) goto err;
			if (add_xmlrpc_reply_esc(out, &st) < 0) goto err;
			if (add_xmlrpc_reply(out, &string_suffix) < 0) goto err;

			if (add_xmlrpc_reply(out, &value_suffix) < 0) goto err;
			if (add_xmlrpc_reply(out, &member_suffix) < 0) goto err;

			return 0;
		}
		     /* Else try again with more space. */
		if (n > -1) {   /* glibc 2.1 */
			buf_size = n + 1; /* precisely what is needed */
		} else {          /* glibc 2.0 */
			buf_size *= 2;  /* twice the old size */
		}
		if ((buf = mxr_realloc(buf, buf_size)) == 0) {
			set_fault(reply, 500, "Internal Server Error (No memory left)");
			ERR("No memory left\n");
			goto err;
		}
	}
	return 0;
 err:
	if (buf) mxr_free(buf);
	return -1;

}


static int rpc_struct_scan(struct rpc_struct* s, char* fmt, ...)
{
	int read;
	int ival;
	va_list ap;
	int* int_ptr;
	unsigned int* uint_ptr;
	double* double_ptr;
	char** char_ptr;
	str* str_ptr;
	xmlNodePtr value;
	char* member_name;
	struct xmlrpc_reply* reply;
	int ret;
	int f;

	read = 0;
	f=(autoconvert?GET_X_AUTOCONV:0) |
		(lflf2crlf?GET_X_LFLF2CRLF:0);
	va_start(ap, fmt);
	while(*fmt) {
		member_name = va_arg(ap, char*);
		reply = s->reply;
		/* clear the previously saved error code */
		clear_xmlrpc_reply(reply);
		ret = find_member(&value, s->doc, s->struct_in, reply, member_name);
		if (ret != 0) goto error;

		switch(*fmt) {
		case 'b': /* Bool */
		case 't': /* Date and time */
		case 'd': /* Integer */
			int_ptr = va_arg(ap, int*);
			if (get_int(int_ptr, reply, s->doc, value, f) < 0) goto error;
			break;

		case 'u': /* Integer */
			uint_ptr = va_arg(ap, unsigned int*);
			if (get_int(&ival, reply, s->doc, value, f) < 0) goto error;
			*uint_ptr = (unsigned int)ival;
			break;
			
		case 'f': /* double */
			double_ptr = va_arg(ap, double*);
			if (get_double(double_ptr, reply, s->doc, value, f) < 0)
				goto error;
			break;

		case 's': /* zero terminated string */
			char_ptr = va_arg(ap, char**);
			if (get_string(char_ptr, reply, s->doc, value, f) < 0) goto error;
			break;

		case 'S': /* str structure */
			str_ptr = va_arg(ap, str*);
			if (get_string(&str_ptr->s, reply, s->doc, value, f) < 0)
				goto error;
			str_ptr->len = strlen(str_ptr->s);
			break;
		default:
			ERR("Invalid parameter type in formatting string: %c\n", *fmt);
			return -1;
		}
		fmt++;
		read++;
	}
	va_end(ap);
	return read;
 error:
	va_end(ap);
	return -read;
}


/** Returns the RPC capabilities supported by the xmlrpc driver.
 */
static rpc_capabilities_t rpc_capabilities(rpc_ctx_t* ctx)
{
	return RPC_DELAYED_REPLY;
}


/** Returns a new "delayed reply" context.
 * Creates a new delayed reply context in shm and returns it.
 * @return 0 - not supported, already replied, or no more memory;
 *         !=0 pointer to the special delayed ctx.
 * Note1: one should use the returned ctx reply context to build a reply and
 *  when finished call rpc_delayed_ctx_close().
 * Note2: adding pieces to the reply in different processes is not supported.
 */
static struct rpc_delayed_ctx* rpc_delayed_ctx_new(rpc_ctx_t* ctx)
{
	struct rpc_delayed_ctx* ret;
	int size;
	rpc_ctx_t* r_ctx;
	struct sip_msg* shm_msg;
	int len;
	
	ret=0;
	shm_msg=0;
	
	if (ctx->reply_sent)
		return 0; /* no delayed reply if already replied */
	/* clone the sip msg */
	shm_msg=sip_msg_shm_clone(ctx->msg, &len, 1);
	if (shm_msg==0)
		goto error;
	
	/* alloc into one block */
	size=ROUND_POINTER(sizeof(*ret))+sizeof(rpc_ctx_t);
	if ((ret=shm_malloc(size))==0)
		goto error;
	memset(ret, 0, size);
	ret->rpc=func_param;
	ret->reply_ctx=(char*)ret+ROUND_POINTER(sizeof(*ret));
	r_ctx=ret->reply_ctx;
	r_ctx->flags=ctx->flags | XMLRPC_DELAYED_CTX_F;
	ctx->flags |= XMLRPC_DELAYED_REPLY_F;
	r_ctx->msg=shm_msg;
	r_ctx->msg_shm_block_size=len;
	
	return ret;
error:
	if (shm_msg)
		shm_free(shm_msg);
	if (ret)
		shm_free(ret);
	return 0;
}



/** Closes a "delayed reply" context and sends the reply.
 * If no reply has been sent the reply will be built and sent automatically.
 * See the notes from rpc_new_delayed_ctx()
 */
static void rpc_delayed_ctx_close(struct rpc_delayed_ctx* dctx)
{
	rpc_ctx_t* r_ctx;
	struct hdr_field* hdr;
	
	r_ctx=dctx->reply_ctx;
	if (unlikely(!(r_ctx->flags & XMLRPC_DELAYED_CTX_F))){
		BUG("reply ctx not marked as async/delayed\n");
		goto error;
	}
	if (fix_delayed_reply_ctx(r_ctx)<0)
		goto error;
	if (!r_ctx->reply_sent){
		rpc_send(r_ctx);
	}
error:
	clean_context(r_ctx);
	/* collect possible garbage (e.g. generated by structures) */
	collect_garbage();
	/* free added lumps (rpc_send adds a body lump) */
	del_nonshm_lump( &(r_ctx->msg->add_rm) );
	del_nonshm_lump( &(r_ctx->msg->body_lumps) );
	del_nonshm_lump_rpl( &(r_ctx->msg->reply_lump) );
	/* free header's parsed structures that were added by failure handlers */
	for( hdr=r_ctx->msg->headers ; hdr ; hdr=hdr->next ) {
		if ( hdr->parsed && hdr_allocs_parse(hdr) &&
		(hdr->parsed<(void*)r_ctx->msg ||
		hdr->parsed>=(void*)(r_ctx->msg+r_ctx->msg_shm_block_size))) {
			/* header parsed filed doesn't point inside uas.request memory
			 * chunck -> it was added by failure funcs.-> free it as pkg */
			DBG("DBG:free_faked_req: removing hdr->parsed %d\n",
					hdr->type);
			clean_hdr_field(hdr);
			hdr->parsed = 0;
		}
	}
	shm_free(r_ctx->msg);
	r_ctx->msg=0;
	dctx->reply_ctx=0;
	shm_free(dctx);
}


/** Starts parsing XML-RPC document, get the name of the method to be called
 * and position the cursor at the first parameter in the document.
 */
static int open_doc(rpc_ctx_t* ctx, sip_msg_t* msg)
{
	str doc = {NULL,0};
	xmlNodePtr root;
	xmlNodePtr cur;
	struct xmlrpc_reply* reply;

	reply = &ctx->reply;
	if (get_rpc_document(&doc, msg) < 0) {
		set_fault(reply, 400, "Malformed Message Body");
		ERR("Error extracting message body\n");
		return -1;
	}

	ctx->doc = xmlReadMemory(doc.s, doc.len, 0, 0,
				 XML_PARSE_NOBLANKS |
				 XML_PARSE_NONET |
				 XML_PARSE_NOCDATA);

	if (!ctx->doc) {
		set_fault(reply, 400, "Invalid XML-RPC Document");
		ERR("Invalid XML-RPC document: \n[%.*s]\n", doc.len, doc.s);
		goto err;
	}

	root = xmlDocGetRootElement(ctx->doc);
	if (!root) {
		set_fault(reply, 400, "Empty XML-RPC Document");
		ERR("Empty XML-RPC document\n");
		goto err;
	}

	if (xmlStrcmp(root->name, (const xmlChar*)"methodCall")) {
		set_fault(reply, 400, "Root Element Is Not methodCall");
		ERR("Root element is not methodCall\n");
		goto err;
	}

	cur = root->xmlChildrenNode;
	while(cur) {
		if (!xmlStrcmp(cur->name, (const xmlChar*)"methodName")) {
			ctx->method = (char*)xmlNodeListGetString(ctx->doc, cur->xmlChildrenNode, 1);
			if (!ctx->method) {
				set_fault(reply, 400, "Cannot Extract Method Name");
				ERR("Cannot extract method name\n");
				goto err;
			}
			break;
		}
		cur = cur->next;
	}
	if (!cur) {
		set_fault(reply, 400, "Method Name Not Found");
		ERR("Method name not found\n");
		goto err;
	}
	cur = root->xmlChildrenNode;
	while(cur) {
		if (!xmlStrcmp(cur->name, (const xmlChar*)"params")) {
			ctx->act_param = cur->xmlChildrenNode;
			break;
		}
		cur = cur->next;
	}
	if (!cur) ctx->act_param = 0;
	return 0;

 err:
	close_doc(ctx);
	return -1;
}

static void close_doc(rpc_ctx_t* ctx)
{
	if (ctx->method) xmlFree(ctx->method);
	if (ctx->doc) xmlFreeDoc(ctx->doc);
	ctx->method = 0;
	ctx->doc = 0;
}

static int init_context(rpc_ctx_t* ctx, sip_msg_t* msg)
{
	ctx->msg = msg;
	ctx->msg_shm_block_size=0;
	ctx->method = 0;
	ctx->reply_sent = 0;
	ctx->act_param = 0;
	ctx->doc = 0;
	ctx->structs = 0;
	if (init_xmlrpc_reply(&ctx->reply) < 0) return -1;
	add_xmlrpc_reply(&ctx->reply, &success_prefix);
	if (open_doc(ctx, msg) < 0) return -1;
	return 0;
}


static void clean_context(rpc_ctx_t* ctx)
{
	if (!ctx) return;
	clean_xmlrpc_reply(&ctx->reply);
	close_doc(ctx);
}



/** Creates a SIP message (in "buffer" form) from a HTTP XML-RPC request).
 * 
 * NOTE: the result must be mxr_free()'ed when not needed anymore.  
 * @return 0 on error, buffer allocated using mxr_malloc on success.
 */
static char* http_xmlrpc2sip(sip_msg_t* msg, int* new_msg_len)
{
	unsigned int len, via_len;
	char* via, *new_msg, *p;
	str ip, port;
	struct hostport hp;
	struct dest_info dst;
	
	/* create a via */
	ip.s = ip_addr2a(&msg->rcv.src_ip);
	ip.len = strlen(ip.s);
	port.s = int2str(msg->rcv.src_port, &port.len);
	hp.host = &ip;
	hp.port = &port;
	init_dst_from_rcv(&dst, &msg->rcv);
	via = via_builder(&via_len, &dst, 0, 0, &hp);
	if (via == 0) {
		DEBUG("failed to build via\n");
		return 0;
	}
	len = msg->first_line.u.request.method.len + 1 /* space */ + 
		XMLRPC_URI_LEN + 1 /* space */ + 
		msg->first_line.u.request.version.len + CRLF_LEN + via_len + 
		(msg->len-msg->first_line.len);
	p = new_msg = mxr_malloc(len + 1);
	if (new_msg == 0) {
		DEBUG("memory allocation failure (%d bytes)\n", len);
		pkg_free(via);
		return 0;
	}

	/* new message:
	 * <orig_http_method> sip:127.0.0.1:9 HTTP/1.x 
	 * Via: <faked via>
	 * <orig. http message w/o the first line>
	 */
	memcpy(p, msg->first_line.u.request.method.s, 
		   msg->first_line.u.request.method.len);
	p += msg->first_line.u.request.method.len;
	*p = ' ';
	p++;
	memcpy(p, XMLRPC_URI, XMLRPC_URI_LEN);
	p += XMLRPC_URI_LEN;
	*p = ' ';
	p++;
	memcpy(p, msg->first_line.u.request.version.s,
		   msg->first_line.u.request.version.len);
	p += msg->first_line.u.request.version.len;
	memcpy(p, CRLF, CRLF_LEN);
	p += CRLF_LEN;
	memcpy(p, via, via_len);
	p += via_len;
	memcpy(p,  SIP_MSG_START(msg) + msg->first_line.len, 
		   msg->len - msg->first_line.len);
	new_msg[len] = 0; /* null terminate, required by receive_msg() */
	pkg_free(via);
	*new_msg_len = len;
	return new_msg;
}



/** Emulate receive_msg for an XML-RPC request .
 */
static int em_receive_request(sip_msg_t* orig_msg, 
							  char* new_buf, unsigned int new_len)
{
	int ret;
	sip_msg_t tmp_msg, *msg;
	struct run_act_ctx ra_ctx;
	
	ret=0;
	if (new_buf && new_len) {
		memset(&tmp_msg, 0, sizeof(sip_msg_t));
		tmp_msg.buf = new_buf;
		tmp_msg.len = new_len;
		tmp_msg.rcv = orig_msg->rcv;
		tmp_msg.id = orig_msg->id;
		tmp_msg.set_global_address = orig_msg->set_global_address;
		tmp_msg.set_global_port = orig_msg->set_global_port;
		if (parse_msg(new_buf, new_len, &tmp_msg) != 0) {
			ERR("xmlrpc: parse_msg failed\n");
			goto error;
		}
		msg = &tmp_msg;
	} else {
		msg = orig_msg;
	}
	
	/* not needed, performed by the "real" receive_msg()
	   clear_branches();
	   reset_static_buffer();
	*/
	if ((msg->first_line.type != SIP_REQUEST) || (msg->via1 == 0) ||
		(msg->via1->error != PARSE_OK)) {
		BUG("xmlrpc: strange message: %.*s\n", msg->len, msg->buf);
		goto error;
	}
	if (exec_pre_script_cb(msg, REQUEST_CB_TYPE) == 0) {
		goto end; /* drop request */
	}
	/* exec routing script */
	init_run_actions_ctx(&ra_ctx);
	if (run_actions(&ra_ctx, main_rt.rlist[xmlrpc_route_no], msg) < 0) {
		ret=-1;
		DBG("xmlrpc: error while trying script\n");
		goto end;
	}
 end:
	exec_post_script_cb(msg, REQUEST_CB_TYPE); /* needed for example if tm is used */
	/* reset_avps(); non needed, performed by the real receive_msg */
	if (msg != orig_msg) { /* avoid double free (freed from receive_msg
							  too) */
		free_sip_msg(msg);
	}
	return ret;
 error:
	return -1;
}


/** The main handler that will be called when SER core receives a non-SIP
 * request (i.e. HTTP request carrying XML-RPC document in the body).
 */
static int process_xmlrpc(sip_msg_t* msg)
{
	int ret;
	char* fake_msg;
	int fake_msg_len;
	unsigned char* method;
	unsigned int method_len, n_method;
	regmatch_t pmatch;
	char c;
	
	ret=NONSIP_MSG_DROP;
	if (!IS_HTTP(msg))
		return NONSIP_MSG_PASS;

	if(xmlrpc_url_skip!=NULL || xmlrpc_url_match!=NULL)
	{
		c = msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len];
		msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
			= '\0';
		if (xmlrpc_url_skip!=NULL &&
			regexec(&xmlrpc_url_skip_regexp, msg->first_line.u.request.uri.s,
					1, &pmatch, 0)==0)
		{
			LM_DBG("URL matched skip re\n");
			msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
				= c;
			return NONSIP_MSG_PASS;
		}
		if (xmlrpc_url_match!=NULL &&
			regexec(&xmlrpc_url_match_regexp, msg->first_line.u.request.uri.s,
					1, &pmatch, 0)!=0)
		{
			LM_DBG("URL not matched\n");
			msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len]
				= c;
			return NONSIP_MSG_PASS;
		}
		msg->first_line.u.request.uri.s[msg->first_line.u.request.uri.len] = c;
	}

	method = (unsigned char*)msg->first_line.u.request.method.s;
	method_len = msg->first_line.u.request.method.len;
	/* first line is always > 4, so it's always safe to try to read the
	 * 1st 4 bytes from method, even if method is shorter*/
	n_method = method[0] + (method[1] << 8) + (method[2] << 16) +
			(method[3] << 24);
	n_method |= 0x20202020;
	n_method &= ((method_len < 4) * (1U << method_len * 8) - 1);
	/* accept only GET or POST */
	if ((n_method == N_HTTP_GET) ||
			((n_method == N_HTTP_POST) && (method_len == HTTP_POST_LEN))) {
		if (msg->via1 == 0){
			/* create a fake sip message */
			fake_msg = http_xmlrpc2sip(msg, &fake_msg_len);
			if (fake_msg == 0) {
				ERR("xmlrpc: out of memory\n");
				ret=NONSIP_MSG_ERROR;
			} else {
			/* send it */
				DBG("new fake xml msg created (%d bytes):\n<%.*s>\n",
					fake_msg_len, fake_msg_len, fake_msg);
				if (em_receive_request(msg, fake_msg, fake_msg_len)<0)
					ret=NONSIP_MSG_ERROR;
				mxr_free(fake_msg);
			}
			return ret; /* we "ate" the message, stop processing */
		} else { /* the message has a via */
			DBG("http xml msg unchanged (%d bytes):\n<%.*s>\n",
				msg->len, msg->len, msg->buf);
			if (em_receive_request(msg, 0, 0)<0)
				ret=NONSIP_MSG_ERROR;
			return ret;
		}
	} else {
		ERR("xmlrpc: bad HTTP request method: \"%.*s\"\n",
			msg->first_line.u.request.method.len,
			msg->first_line.u.request.method.s);
		/* the message was for us, but it is an error */
		return NONSIP_MSG_ERROR;
	}
	return NONSIP_MSG_PASS; /* message not for us, maybe somebody 
								   else needs it */
}


/** The main processing function of xmlrpc module.
 *
 * This is the main function of this module. It extracts the name
 * of the method to be called from XML-RPC request and then it
 * searches through the list of all available management function,
 * when a function with matching name is found then it will be
 * executed.
 */
static int dispatch_rpc(sip_msg_t* msg, char* s1, char* s2)
{
	rpc_export_t* exp;
	int ret = 1;

	if (init_context(&ctx, msg) < 0) goto skip;

	exp = find_rpc_export(ctx.method, 0);
	if (!exp || !exp->function) {
		rpc_fault(&ctx, 500, "Method Not Found");
		goto skip;
	}
	ctx.flags = exp->flags;
	if (exp->flags & RET_ARRAY && 
		add_xmlrpc_reply(&ctx.reply, &array_prefix) < 0) goto skip;
	exp->function(&func_param, &ctx);

 skip:
	     /* The function may have sent the reply itself */
	if (!ctx.reply_sent && !(ctx.flags&XMLRPC_DELAYED_REPLY_F)) {
		ret = rpc_send(&ctx);
	}
	clean_context(&ctx);
	collect_garbage();
	if (ret < 0) return -1;
	else return 1;
}


/** This function can be called from SER scripts to generate
 * an XML-RPC reply.
 */
static int xmlrpc_reply(sip_msg_t* msg, char* p1, char* p2)
{
        str reason;
	static str succ = STR_STATIC_INIT("1");
	struct xmlrpc_reply reply;

	memset(&reply, 0, sizeof(struct xmlrpc_reply));
	if (init_xmlrpc_reply(&reply) < 0) return -1;

	if (get_int_fparam(&reply.code, msg, (fparam_t*)p1) < 0) return -1;
	if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) return -1;

	reply.reason = as_asciiz(&reason);
	if (reply.reason == NULL) {
	    ERR("No memory left\n");
	    return -1;
	}

	if (reply.code >= 300) { 
		if (build_fault_reply(&reply) < 0) goto error;
	} else {
		if (add_xmlrpc_reply(&reply, &success_prefix) < 0) goto error;
		if (add_xmlrpc_reply(&reply, &int_prefix) < 0) goto error;
		if (add_xmlrpc_reply_esc(&reply, &succ) < 0) goto error;
		if (add_xmlrpc_reply(&reply, &int_suffix) < 0) goto error;
		if (add_xmlrpc_reply(&reply, &success_suffix) < 0) return -1;
	}
	if (send_reply(msg, &reply.body) < 0) goto error;
	if (reply.reason) pkg_free(reply.reason);
	clean_xmlrpc_reply(&reply);
	return 1;
 error:
	if (reply.reason) pkg_free(reply.reason);
	clean_xmlrpc_reply(&reply);
	return -1;
}


/** Implementation of \@xmlrpc.method select that can be used in
 * SER scripts to retrieve the method string from XML-RPC documents
 */
static int select_method(str* res, struct select* s, sip_msg_t* msg)
{
	static char buf[1024];
	str doc = {NULL,0};
	xmlDocPtr xmldoc;
	xmlNodePtr cur;
	char* method;

	xmldoc = 0;
	method = 0;

	if (get_rpc_document(&doc, msg) < 0) goto err;
	xmldoc = xmlReadMemory(doc.s, doc.len, 0, 0, 
						   XML_PARSE_NOBLANKS | 
						   XML_PARSE_NONET | 
						   XML_PARSE_NOCDATA);
	
	if (!xmldoc) goto err;
	cur = xmlDocGetRootElement(xmldoc);
	if (!cur) goto err;
	if (xmlStrcmp(cur->name, (const xmlChar*)"methodCall")) goto err;
	cur = cur->xmlChildrenNode;
	while(cur) {
		if (!xmlStrcmp(cur->name, (const xmlChar*)"methodName")) {
			method = (char*)xmlNodeListGetString(xmldoc, cur->xmlChildrenNode,
												 1);
			if (!method) goto err;
			break;
		}
		cur = cur->next;
	}
	if (!cur) goto err;
	res->len = strlen(method);
	if (res->len >= 1024) goto err;
	memcpy(buf, method, res->len);
	res->s = buf;
	return 0;
 err:
	if (method) xmlFree(method);
	if (xmldoc) xmlFreeDoc(xmldoc);
	return -1;
}

static ABSTRACT_F(select_xmlrpc);

select_row_t xmlrpc_sel[] = {
        { NULL,          SEL_PARAM_STR, STR_STATIC_INIT("xmlrpc"), select_xmlrpc, SEL_PARAM_EXPECTED},
        { select_xmlrpc, SEL_PARAM_STR, STR_STATIC_INIT("method"), select_method, 0},
        { NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
};


static int mod_init(void)
{
	struct nonsip_hook nsh;
	int route_no;
	
	/* try to fix the xmlrpc route */
	if (xmlrpc_route){
		route_no=route_get(&main_rt, xmlrpc_route);
		if (route_no==-1){
			ERR("xmlrpc: failed to fix route \"%s\": route_get() failed\n",
					xmlrpc_route);
			return -1;
		}
		if (main_rt.rlist[route_no]==0){
			WARN("xmlrpc: xmlrpc route \"%s\" is empty / doesn't exist\n",
					xmlrpc_route);
		}
		xmlrpc_route_no=route_no;
	}

	/* bind the SL API */
	if (sl_load_api(&slb)!=0) {
		LM_ERR("cannot bind to SL API\n");
		return -1;
	}

	memset(&func_param, 0, sizeof(func_param));
	func_param.send = (rpc_send_f)rpc_send;
	func_param.fault = (rpc_fault_f)rpc_fault;
	func_param.add = (rpc_add_f)rpc_add;
	func_param.scan = (rpc_scan_f)rpc_scan;
	func_param.rpl_printf = (rpc_rpl_printf_f)rpc_rpl_printf;
	func_param.struct_add = (rpc_struct_add_f)rpc_struct_add;
	func_param.array_add = (rpc_array_add_f)rpc_array_add;
	func_param.struct_scan = (rpc_struct_scan_f)rpc_struct_scan;
	func_param.struct_printf = (rpc_struct_printf_f)rpc_struct_printf;
	func_param.capabilities = (rpc_capabilities_f)rpc_capabilities;
	func_param.delayed_ctx_new = (rpc_delayed_ctx_new_f)rpc_delayed_ctx_new;
	func_param.delayed_ctx_close =
		(rpc_delayed_ctx_close_f)rpc_delayed_ctx_close;
	register_select_table(xmlrpc_sel);
	
	/* register non-sip hooks */
	if(xmlrpc_mode==0)
	{
		memset(&nsh, 0, sizeof(nsh));
		nsh.name="xmlrpc";
		nsh.destroy=0;
		nsh.on_nonsip_req=process_xmlrpc;
		if (register_nonsip_msg_hook(&nsh)<0){
			ERR("Failed to register non sip msg hooks\n");
			return -1;
		}
	}
	if(xmlrpc_url_match!=NULL)
	{
		memset(&xmlrpc_url_match_regexp, 0, sizeof(regex_t));
		if (regcomp(&xmlrpc_url_match_regexp, xmlrpc_url_match, REG_EXTENDED)!=0) {
			LM_ERR("bad match re %s\n", xmlrpc_url_match);
			return E_BAD_RE;
		}
	}
	if(xmlrpc_url_skip!=NULL)
	{
		memset(&xmlrpc_url_skip_regexp, 0, sizeof(regex_t));
		if (regcomp(&xmlrpc_url_skip_regexp, xmlrpc_url_skip, REG_EXTENDED)!=0) {
			LM_ERR("bad skip re %s\n", xmlrpc_url_skip);
			return E_BAD_RE;
		}
	}

	return 0;
}


/**
 * advertise that sip workers handle rpc commands
 */
int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
	set_child_sip_rpc_mode();
	return 0;
}


static int fixup_xmlrpc_reply(void** param, int param_no)
{
	int ret;

	if (param_no == 1) {
		ret = fix_param(FPARAM_AVP, param);
		if (ret <= 0) return ret;		
	    if (fix_param(FPARAM_INT, param) != 0) return -1;
	} else if (param_no == 2) {
	        return fixup_var_str_12(param, 2);
	}
	return 0;
}

/** @} */