File: router.c

package info (click to toggle)
carbon-c-relay 2.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 684 kB
  • sloc: ansic: 7,204; sh: 113; python: 112; makefile: 52
file content (3471 lines) | stat: -rw-r--r-- 89,597 bytes parent folder | download | duplicates (3)
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
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
/*
 * Copyright 2013-2017 Fabian Groffen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <glob.h>
#include <regex.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>

#include "fnv1a.h"
#include "consistent-hash.h"
#include "server.h"
#include "queue.h"
#include "aggregator.h"
#include "relay.h"
#include "router.h"

enum clusttype {
	BLACKHOLE,  /* /dev/null-like destination */
	GROUP,      /* pseudo type to create a matching tree */
	AGGRSTUB,   /* pseudo type to have stub matches for aggregation returns */
	STATSTUB,   /* pseudo type to have stub matches for collector returns */
	VALIDATION, /* pseudo type to perform additional data validation */
	FORWARD,
	FILELOG,    /* like forward, write metric to file */
	FILELOGIP,  /* like forward, write ip metric to file */
	CARBON_CH,  /* original carbon-relay.py consistent-hash */
	FNV1A_CH,   /* FNV1a-based consistent-hash */
	JUMP_CH,    /* jump consistent hash with fnv1a input */
	ANYOF,      /* FNV1a-based hash, but with backup by others */
	FAILOVER,   /* ordered attempt delivery list */
	AGGREGATION,
	REWRITE
};

typedef struct _servers {
	server *server;
	int refcnt;
	struct _servers *next;
} servers;

typedef struct {
	unsigned char repl_factor;
	ch_ring *ring;
	servers *servers;
} chashring;

typedef struct {
	unsigned short count;
	server **servers;
	servers *list;
} serverlist;

typedef struct _validate {
	struct _route *rule;
	enum { VAL_LOG, VAL_DROP } action;
} validate;

typedef struct _cluster {
	char *name;
	enum clusttype type;
	union {
		chashring *ch;
		servers *forward;
		serverlist *anyof;
		aggregator *aggregation;
		struct _route *routes;
		char *replacement;
		struct _validate *validation;
	} members;
	struct _cluster *next;
} cluster;

typedef struct _destinations {
	cluster *cl;
	struct _destinations *next;
} destinations;

typedef struct _route {
	char *pattern;    /* original regex input, used for printing only */
	regex_t rule;     /* regex on metric, only if type == REGEX */
	size_t nmatch;    /* number of match groups */
	char *strmatch;   /* string to search for if type not REGEX or MATCHALL */
	destinations *dests; /* where matches should go */
	char stop:1;      /* whether to continue matching rules after this one */
	enum {
		MATCHALL,     /* the '*', don't do anything, just match everything */
		REGEX,        /* a regex match */
		CONTAINS,     /* find string occurrence */
		STARTS_WITH,  /* metric must start with string */
		ENDS_WITH,    /* metric must end with string */
		MATCHES       /* metric matches string exactly */
	} matchtype;      /* how to interpret the pattern */
	struct _route *next;
} route;

struct _router {
	cluster *clusters;
	route *routes;
	aggregator *aggregators;
	servers *srvrs;
	char *collector_stub;
	struct _router_allocator {
		void *memory_region;
		void *nextp;
		size_t sz;
		struct _router_allocator *next;
	} *allocator;
};

/* custom constant, meant to force regex mode matching */
#define REG_FORCE   01000000

/* catch string used for aggrgator destinations */
#define STUB_AGGR   "_aggregator_stub_"
/* catch string used for collector output */
#define STUB_STATS  "_collector_stub_"

/**
 * Free the allocators associated to this router.
 */
static void
ra_free(router *rtr)
{
	struct _router_allocator *ra;
	struct _router_allocator *ra_next;

	for (ra = rtr->allocator; ra != NULL; ra = ra_next) {
		free(ra->memory_region);
		ra_next = ra->next;
		free(ra);
		ra = NULL;
	}
}

/**
 * malloc() in one of the allocators for this router.  If insufficient
 * memory is available in the allocator, a new one is allocated.  If
 * that fails, NULL is returned, else a pointer that can be written to
 * up to sz bytes.
 */
static void *
ra_malloc(router *rtr, size_t sz)
{
	struct _router_allocator *ra;
	void *retp = NULL;
	size_t nsz;

#define ra_alloc(RA, SZ) { \
		assert(SZ >= 0); \
		nsz = 256 * 1024; \
		if (SZ > nsz) \
			nsz = ((SZ / 1024) + 1) * 1024; \
		RA = malloc(sizeof(struct _router_allocator)); \
		if (RA == NULL) \
			return NULL; \
		RA->memory_region = malloc(sizeof(char) * nsz); \
		if (RA->memory_region == NULL) { \
			free(RA); \
			RA = NULL; \
			return NULL; \
		} \
		RA->nextp = RA->memory_region; \
		RA->sz = nsz; \
		RA->next = NULL; \
	}

	if (rtr->allocator == NULL)
		ra_alloc(rtr->allocator, 0);

	for (ra = rtr->allocator; ra != NULL; ra = ra->next) {
		if (ra->sz - (ra->nextp - ra->memory_region) >= sz) {
			retp = ra->nextp;
			ra->nextp += sz;
			return retp;
		}
		if (ra->next == NULL)
			ra_alloc(ra->next, sz);
	}

	/* this should be unreachable code */
	return NULL;
}

/**
 * strdup using ra_malloc, e.g. get memory from the allocator associated
 * to the given router.
 */
static char *
ra_strdup(router *rtr, const char *s)
{
	size_t sz = strlen(s) + 1;
	char *m = ra_malloc(rtr, sz);
	if (m == NULL)
		return m;
	memcpy(m, s, sz);
	return m;
}

/**
 * Frees routes and clusters.
 */
static void
router_free_intern(cluster *clusters, route *routes)
{
	while (routes != NULL) {
		if (routes->matchtype == REGEX)
			regfree(&routes->rule);

		if (routes->next == NULL || routes->next->dests != routes->dests) {
			while (routes->dests != NULL) {
				if (routes->dests->cl->type == GROUP ||
						routes->dests->cl->type == AGGRSTUB ||
						routes->dests->cl->type == STATSTUB)
					router_free_intern(NULL, routes->dests->cl->members.routes);
				if (routes->dests->cl->type == VALIDATION)
					router_free_intern(NULL,
							routes->dests->cl->members.validation->rule);

				routes->dests = routes->dests->next;
			}
		}

		routes = routes->next;
	}

	while (clusters != NULL) {
		switch (clusters->type) {
			case CARBON_CH:
			case FNV1A_CH:
			case JUMP_CH:
				ch_free(clusters->members.ch->ring);
				break;
			case FORWARD:
			case FILELOG:
			case FILELOGIP:
			case BLACKHOLE:
			case ANYOF:
			case FAILOVER:
				/* no special structures to free */
				break;
			case GROUP:
			case AGGRSTUB:
			case STATSTUB:
			case VALIDATION:
				/* handled at the routes above */
				break;
			case AGGREGATION:
				/* aggregators starve when they get no more input */
				break;
			case REWRITE:
				/* everything is in the allocators */
				break;
		}

		clusters = clusters->next;
	}
}

/**
 * Examines pattern and sets matchtype and rule or strmatch in route.
 */
static int
determine_if_regex(route *r, char *pat, int flags)
{
	/* try and see if we can avoid using a regex match, for
	 * it is simply very slow/expensive to do so: most of
	 * the time, people don't need fancy matching rules */
	char patbuf[8192];
	char *e = pat;
	char *pb = patbuf;
	char escape = 0;
	r->matchtype = CONTAINS;
	r->nmatch = 0;

	if (flags & REG_FORCE) {
		flags &= ~REG_NOSUB;
		r->matchtype = REGEX;
	}

	if (*e == '^' && r->matchtype == CONTAINS) {
		e++;
		r->matchtype = STARTS_WITH;
	}
	for (; *e != '\0'; e++) {
		switch (*e) {
			case '\\':
				if (escape)
					*pb++ = *e;
				escape = !escape;
				break;
			case '.':
			case '^':
			case '*':
			case '+':
				if (!escape)
					r->matchtype = REGEX;
				*pb++ = *e;
				escape = 0;
				break;
			case '$':
				if (!escape && e[1] == '\0') {
					if (r->matchtype == STARTS_WITH) {
						r->matchtype = MATCHES;
					} else {
						r->matchtype = ENDS_WITH;
					}
				} else {
					r->matchtype = REGEX;
				}
				escape = 0;
				break;
			default:
				if (
						!escape && (
							(*e == '_') || (*e == '-') ||
							(*e >= '0' && *e <= '9') ||
							(*e >= 'a' && *e <= 'z') ||
							(*e >= 'A' && *e <= 'Z')
						)
				   )
				{
					*pb++ = *e;
				} else {
					r->matchtype = REGEX;
				}
				escape = 0;
				break;
		}
		if (pb - patbuf == sizeof(patbuf))
			r->matchtype = REGEX;
		if (r->matchtype == REGEX)
			break;
	}
	if (r->matchtype != REGEX) {
		*pb = '\0';
		r->strmatch = strdup(patbuf);
		r->pattern = strdup(pat);
	} else {
		int ret = regcomp(&r->rule, pat, flags & ~REG_FORCE);
		if (ret != 0)
			return ret;  /* allow use of regerror */
		r->strmatch = NULL;
		r->pattern = strdup(pat);
		if (((flags & REG_NOSUB) == 0 && r->rule.re_nsub > 0) ||
				flags & REG_FORCE)
		{
			/* we need +1 because position 0 contains the entire
			 * expression */
			r->nmatch = r->rule.re_nsub + 1;
			if (r->nmatch > RE_MAX_MATCHES) {
				logerr("determine_if_regex: too many match groups, "
						"please increase RE_MAX_MATCHES in router.h\n");
				free(r->pattern);
				return REG_ESPACE;  /* lie closest to the truth */
			}
		}
	}

	return 0;
}

/**
 * Populates the routing tables by reading the config file.
 */
router *
router_readconfig(router *orig,
		const char *path,
		size_t queuesize,
		size_t batchsize,
		int maxstalls,
		unsigned short iotimeout,
		unsigned int sockbufsize)
{
	FILE *cnf;
	char *buf;
	size_t len = 0;
	char *p;
	struct stat st;
	struct addrinfo *saddrs;
	char matchcatchallfound = 0;
	router *ret = NULL;
	cluster *cl = NULL;
	aggregator *a = NULL;
	route *r = NULL;

	/* if there is no config, don't try anything */
	if (stat(path, &st) == -1) {
		logerr("unable to stat file '%s': %s\n", path, strerror(errno));
		return NULL;
	}

	if (orig == NULL) {
		/* get a return structure (and allocator) in place */
		if ((ret = malloc(sizeof(router))) == NULL) {
			logerr("malloc failed for router return struct\n");
			return NULL;
		}
		ret->allocator = NULL;
		ret->collector_stub = NULL;
		ret->routes = NULL;
		ret->aggregators = NULL;
		ret->srvrs = NULL;
		ret->clusters = NULL;

		/* create virtual blackhole cluster */
		cl = ra_malloc(ret, sizeof(cluster));
		if (cl == NULL) {
			logerr("malloc failed for blackhole cluster\n");
			router_free(ret);
			return NULL;
		}
		cl->name = ra_strdup(ret, "blackhole");
		cl->type = BLACKHOLE;
		cl->members.forward = NULL;
		cl->next = NULL;
		ret->clusters = cl;
	} else {
		ret = orig;
		/* position a, cl and r at the end of chains */
		for (a = ret->aggregators; a != NULL && a->next != NULL; a = a->next)
			;
		for (cl = ret->clusters; cl->next != NULL; cl = cl->next)
			;
		for (r = ret->routes; r != NULL && r->next != NULL; r = r->next)
			;
	}

	if ((buf = ra_malloc(ret, st.st_size + 1)) == NULL) {
		logerr("malloc failed for config file buffer\n");
		return NULL;
	}
	if ((cnf = fopen(path, "r")) == NULL) {
		logerr("failed to open config file '%s': %s\n", path, strerror(errno));
		return NULL;
	}
	while ((len = fread(buf + len, 1, st.st_size - len, cnf)) != 0)
		;
	buf[st.st_size] = '\0';
	fclose(cnf);

	/* remove all comments to ease parsing below */
	p = buf;
	for (; *p != '\0'; p++)
		if (*p == '#')
			for (; *p != '\0' && *p != '\n'; p++)
				*p = ' ';

	p = buf;
	do {
		for (; *p != '\0' && isspace(*p); p++)
			;
		if (*p == '\0')
			break;

		if (strncmp(p, "cluster", 7) == 0 && isspace(*(p + 7))) {
			/* group config */
			servers *w;
			char *name;
			char useall = 0;
			p += 8;
			for (; *p != '\0' && isspace(*p); p++)
				;
			name = p;
			for (; *p != '\0' && !isspace(*p); p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of file after 'cluster'\n");
				router_free(ret);
				return NULL;
			}
			*p++ = '\0';

			if ((cl = cl->next = ra_malloc(ret, sizeof(cluster))) == NULL) {
				logerr("malloc failed for cluster '%s'\n", name);
				router_free(ret);
				return NULL;
			}
			cl->name = ra_strdup(ret, name);
			cl->next = NULL;

			for (; *p != '\0' && isspace(*p); p++)
				;
			if (
					(strncmp(p, "carbon_ch", 9) == 0 && isspace(*(p + 9))) ||
					(strncmp(p, "fnv1a_ch", 8) == 0 && isspace(*(p + 8))) ||
					(strncmp(p, "jump_fnv1a_ch", 13) == 0 && isspace(*(p + 13)))
			   )
			{
				int replcnt = 1;
				enum clusttype chtype =
					*p == 'c' ? CARBON_CH : *p == 'f' ? FNV1A_CH : JUMP_CH;

				p += chtype == CARBON_CH ? 10 : chtype == FNV1A_CH ? 9 : 14;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "replication", 11) == 0 && isspace(*(p + 11))) {
					char *repl;
					p += 12;
					for (; *p != '\0' && isspace(*p); p++)
						;
					repl = p;
					/* parse int */
					for (; *p != '\0' && !isspace(*p); p++)
						;
					if (*p == '\0') {
						logerr("unexpected end of file after "
								"'replication %s' for cluster %s\n",
								repl, name);
						router_free(ret);
						return NULL;
					}
					*p++ = '\0';
					if ((replcnt = atoi(repl)) == 0)
						replcnt = 1;
				}

				cl->type = chtype;
				cl->members.ch = ra_malloc(ret, sizeof(chashring));
				if (cl->members.ch == NULL) {
					logerr("malloc failed for ch in cluster '%s'\n", name);
					router_free(ret);
					return NULL;
				}
				cl->members.ch->repl_factor = (unsigned char)replcnt;
				cl->members.ch->ring =
					ch_new(chtype == CARBON_CH ? CARBON :
							chtype == FNV1A_CH ? FNV1a : JUMP_FNV1a);
			} else if (strncmp(p, "forward", 7) == 0 && isspace(*(p + 7))) {
				p += 8;

				cl->type = FORWARD;
				cl->members.forward = NULL;
			} else if (strncmp(p, "any_of", 6) == 0 && isspace(*(p + 6))) {
				p += 7;

				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "useall", 6) == 0 && isspace(*(p + 6))) {
					p += 7;
					useall = 1;
				}

				cl->type = ANYOF;
				cl->members.anyof = NULL;
			} else if (strncmp(p, "failover", 8) == 0 && isspace(*(p + 8))) {
				p += 9;

				cl->type = FAILOVER;
				cl->members.anyof = NULL;
			} else if (strncmp(p, "file", 4) == 0 && isspace(*(p + 4))) {
				p += 5;

				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "ip", 2) == 0 && isspace(*(p + 2))) {
					p += 3;
					for (; *p != '\0' && isspace(*p); p++)
						;
					cl->type = FILELOGIP;
				} else {
					cl->type = FILELOG;
				}
				cl->members.forward = NULL;
			} else {
				char *type = p;
				for (; *p != '\0' && !isspace(*p); p++)
					;
				*p = 0;
				logerr("unknown cluster type '%s' for cluster %s\n",
						type, name);
				router_free(ret);
				return NULL;
			}

			/* parse ips */
			for (; *p != '\0' && isspace(*p); p++)
				;
			w = NULL;
			do {
				char termchr;
				char *lastcolon = NULL;
				char *ip = p;
				char *inst = NULL;
				char *proto = "tcp";
				int port = 2003;
				server *newserver = NULL;
				struct addrinfo hint;
				char sport[8];
				int err;
				struct addrinfo *walk = NULL;
				struct addrinfo *next = NULL;
				char hnbuf[256];

				for (; *p != '\0' && !isspace(*p) && *p != ';'; p++) {
					if (*p == ':' && inst == NULL)
						lastcolon = p;
					if (*p == '=' && inst == NULL)
						inst = p;
				}

				if (*p == '\0') {
					logerr("unexpected end of file at '%s' "
							"for cluster %s\n", ip, name);
					router_free(ret);
					return NULL;
				}

				termchr = *p;
				*p = '\0';

				if (cl->type == CARBON_CH ||
						cl->type == FNV1A_CH ||
						cl->type == JUMP_CH)
				{
					if (inst != NULL) {
						*inst = '\0';
						p = inst++;
					}
					if (inst == ip)
						inst = NULL;
				}

				if (*(p - 1) == ']')
					lastcolon = NULL;
				if (lastcolon != NULL) {
					char *endp = NULL;
					*lastcolon = '\0';
					port = (int)strtol(lastcolon + 1, &endp, 10);
					if (port == 0 || endp != p) {
						logerr("expected port, or unexpected data at "
								"'%s' for cluster %s\n", lastcolon + 1, name);
						router_free(ret);
						return NULL;
					}
				}
				if (*ip == '[') {
					ip++;
					if (lastcolon != NULL && *(lastcolon - 1) == ']') {
						*(lastcolon - 1) = '\0';
					} else if (lastcolon == NULL && *(p - 1) == ']') {
						*(p - 1) = '\0';
					} else {
						logerr("expected ']' at '%s' "
								"for cluster %s\n", ip, name);
						router_free(ret);
						return NULL;
					}
				}

				if (inst != NULL)
					p = inst + strlen(inst);

				if (isspace(termchr)) {
					p++;
					for (; *p != '\0' && isspace(*p); p++)
						;
					if (strncmp(p, "proto", 5) == 0 && isspace(*(p + 5))) {
						p += 6;

						for (; *p != '\0' && isspace(*p); p++)
							;
						proto = p;
						for (; *p != '\0' && !isspace(*p) && *p != ';'; p++)
							;
						termchr = *p;
						*p = '\0';

						if (strcmp(proto, "tcp") != 0
								&& strcmp(proto, "udp") != 0)
						{
							logerr("expected 'udp' or 'tcp' after "
									"'proto' at '%s' for cluster %s\n",
									proto, name);
							router_free(ret);
							return NULL;
						}
					} else {
						termchr = *p;
					}
				}

				if (cl->type != FILELOG && cl->type != FILELOGIP) {
					/* resolve host/IP */
					memset(&hint, 0, sizeof(hint));

					hint.ai_family = PF_UNSPEC;
					hint.ai_socktype = *proto == 'u' ? SOCK_DGRAM : SOCK_STREAM;
					hint.ai_protocol = *proto == 'u' ? IPPROTO_UDP : IPPROTO_TCP;
					hint.ai_flags = AI_NUMERICSERV;
					snprintf(sport, sizeof(sport), "%u", port);  /* for default */

					if ((err = getaddrinfo(ip, sport, &hint, &saddrs)) != 0) {
						logerr("failed to resolve server %s:%s (%s) "
								"for cluster %s: %s\n",
								ip, sport, proto, name, gai_strerror(err));
						router_free(ret);
						return NULL;
					}

					if (!useall && saddrs->ai_next != NULL) {
						/* take first result only */
						freeaddrinfo(saddrs->ai_next);
						saddrs->ai_next = NULL;
					}
				} else {
					/* TODO: try to create/append to file */

					proto = "file";
					saddrs = (void *)1;
				}

				walk = saddrs;
				while (walk != NULL) {
					servers *s;

					/* disconnect from the rest to avoid double
					 * frees by freeaddrinfo() in server_destroy() */
					if (walk != (void *)1) {
						next = walk->ai_next;
						walk->ai_next = NULL;
					}

					if (useall) {
						/* unfold whatever we resolved, for human
						 * readability issues */
						if (walk->ai_family == AF_INET) {
							if (inet_ntop(walk->ai_family,
									&((struct sockaddr_in *)walk->ai_addr)->sin_addr,
									hnbuf, sizeof(hnbuf)) != NULL)
								ip = hnbuf;
						} else if (walk->ai_family == AF_INET6) {
							if (inet_ntop(walk->ai_family,
									&((struct sockaddr_in6 *)walk->ai_addr)->sin6_addr,
									hnbuf + 1, sizeof(hnbuf) - 2) != NULL)
							{
								hnbuf[0] = '[';
								/* space is reserved above */
								strcat(hnbuf, "]");
								ip = hnbuf;
							}
						}
					}

					newserver = NULL;
					for (s = ret->srvrs; s != NULL; s = s->next) {
						if (strcmp(server_ip(s->server), ip) == 0 &&
								server_port(s->server) == port &&
								(*proto != 't' || server_ctype(s->server) == CON_TCP) &&
								(*proto != 'u' || server_ctype(s->server) == CON_UDP))
						{
							newserver = s->server;
							s->refcnt++;
							break;
						}
					}
					if (newserver == NULL) {
						newserver = server_new(ip, (unsigned short)port,
								*proto == 'f' ? CON_FILE :
								*proto == 'u' ? CON_UDP : CON_TCP,
								walk == (void *)1 ? NULL : walk,
								queuesize, batchsize, maxstalls,
								iotimeout, sockbufsize);
						if (newserver == NULL) {
							logerr("failed to add server %s:%d (%s) "
									"to cluster %s: %s\n", ip, port, proto,
									name, strerror(errno));
							router_free(ret);
							freeaddrinfo(saddrs);
							return NULL;
						}
						if (ret->srvrs == NULL) {
							s = ret->srvrs = ra_malloc(ret, sizeof(servers));
						} else {
							for (s = ret->srvrs; s->next != NULL; s = s->next)
								;
							s = s->next = ra_malloc(ret, sizeof(servers));
						}
						s->next = NULL;
						s->refcnt = 1;
						s->server = newserver;
					}

					if (cl->type == CARBON_CH ||
							cl->type == FNV1A_CH ||
							cl->type == JUMP_CH)
					{
						if (w == NULL) {
							cl->members.ch->servers = w =
								ra_malloc(ret, sizeof(servers));
						} else {
							w = w->next = ra_malloc(ret, sizeof(servers));
						}
						if (w == NULL) {
							logerr("malloc failed for %s_ch %s\n",
									cl->type == CARBON_CH ? "carbon" :
									cl->type == FNV1A_CH ? "fnv1a" :
									"jump_fnv1a", ip);
							router_free(ret);
							freeaddrinfo(saddrs);
							return NULL;
						}
						w->next = NULL;
						if (s->refcnt > 1) {
							char *sinst = server_instance(newserver);
							if (sinst == NULL && inst != NULL) {
								logerr("cannot set instance '%s' for "
										"server %s:%d: server was previously "
										"defined without instance\n",
										inst, server_ip(newserver),
										server_port(newserver));
								router_free(ret);
								freeaddrinfo(saddrs);
								return NULL;
							} else if (sinst != NULL && inst == NULL) {
								logerr("cannot define server %s:%d without "
										"instance: server was previously "
										"defined with instance '%s'\n",
										server_ip(newserver),
										server_port(newserver), sinst);
								router_free(ret);
								freeaddrinfo(saddrs);
								return NULL;
							} else if (sinst != NULL && inst != NULL &&
									strcmp(sinst, inst) != 0)
							{
								logerr("cannot set instance '%s' for "
										"server %s:%d: server was previously "
										"defined with instance '%s'\n",
										inst, server_ip(newserver),
										server_port(newserver), sinst);
								router_free(ret);
								freeaddrinfo(saddrs);
								return NULL;
							} /* else: sinst == inst == NULL */
						}
						if (inst != NULL)
							server_set_instance(newserver, inst);
						w->server = newserver;
						cl->members.ch->ring = ch_addnode(
								cl->members.ch->ring,
								w->server);
						if (cl->members.ch->ring == NULL) {
							logerr("failed to add server %s:%d "
									"to ring for cluster %s: out of memory\n",
									ip, port, name);
							router_free(ret);
							freeaddrinfo(saddrs);
							return NULL;
						}
					} else if (cl->type == FORWARD ||
							cl->type == ANYOF ||
							cl->type == FAILOVER ||
							cl->type == FILELOG ||
							cl->type == FILELOGIP)
					{
						if (w == NULL) {
							w = ra_malloc(ret, sizeof(servers));
						} else {
							w = w->next = ra_malloc(ret, sizeof(servers));
						}
						if (w == NULL) {
							logerr("malloc failed for %s %s\n",
									cl->type == FORWARD ? "forward" :
									cl->type == ANYOF ? "any_of" :
									cl->type == FAILOVER ? "failover" :
									"file",
									ip);
							router_free(ret);
							freeaddrinfo(saddrs);
							return NULL;
						}
						w->next = NULL;
						w->server = newserver;
						if ((cl->type == FORWARD ||
							 cl->type == FILELOG || cl->type == FILELOGIP)
									&& cl->members.forward == NULL)
							cl->members.forward = w;
						if (cl->type == ANYOF || cl->type == FAILOVER) {
							if (s->refcnt > 1) {
								logerr("cannot share server %s:%d with "
										"any_of/failover cluster '%s'\n",
										server_ip(newserver),
										server_port(newserver),
										cl->name);
								router_free(ret);
								freeaddrinfo(saddrs);
								return NULL;
							}
							if (cl->members.anyof == NULL) {
								cl->members.anyof =
									ra_malloc(ret, sizeof(serverlist));
								if (cl->members.anyof == NULL) {
									logerr("malloc failed for %s anyof list\n",
											ip);
									router_free(ret);
									freeaddrinfo(saddrs);
									return NULL;
								}
								cl->members.anyof->count = 1;
								cl->members.anyof->servers = NULL;
								cl->members.anyof->list = w;
							} else {
								cl->members.anyof->count++;
							}
						}
					}

					walk = next;
				}

				*p = termchr;
				for (; *p != '\0' && isspace(*p); p++)
					;
			} while (*p != ';');
			p++; /* skip over ';' */
			if (cl->type == ANYOF || cl->type == FAILOVER) {
				size_t i = 0;
				cl->members.anyof->servers =
					ra_malloc(ret, sizeof(server *) * cl->members.anyof->count);
				if (cl->members.anyof->servers == NULL) {
					logerr("malloc failed for anyof servers\n");
					router_free(ret);
					return NULL;
				}
				for (w = cl->members.anyof->list; w != NULL; w = w->next)
					cl->members.anyof->servers[i++] = w->server;
				for (w = cl->members.anyof->list; w != NULL; w = w->next) {
					server_add_secondaries(w->server,
							cl->members.anyof->servers,
							cl->members.anyof->count);
					if (cl->type == FAILOVER)
						server_set_failover(w->server);
				}
			} else if (cl->type == CARBON_CH ||
					cl->type == FNV1A_CH ||
					cl->type == JUMP_CH)
			{
				/* check that replication count is actually <= the
				 * number of servers */
				size_t i = 0;
				for (w = cl->members.ch->servers; w != NULL; w = w->next)
					i++;
				if (i < cl->members.ch->repl_factor) {
					logerr("invalid cluster '%s': replication count (%zu) is "
							"larger than the number of servers (%zu)\n",
							name, cl->members.ch->repl_factor, i);
					router_free(ret);
					return NULL;
				}
			}
		} else if (strncmp(p, "match", 5) == 0 && isspace(*(p + 5))) {
			/* match rule */
			char *pat;
			char *dest;
			char stop = -1;
			cluster *w;
			route *m = NULL;
			destinations *d = NULL;
			destinations *dw = NULL;

			p += 6;
			for (; *p != '\0' && isspace(*p); p++)
				;

			do {
				pat = p;
				for (; *p != '\0' && !isspace(*p); p++)
					;
				if (*p == '\0') {
					logerr("unexpected end of file after 'match'\n");
					router_free(ret);
					return NULL;
				}
				*p++ = '\0';
				for (; *p != '\0' && isspace(*p); p++)
					;

				if (r == NULL) {
					ret->routes = r = ra_malloc(ret, sizeof(route));
				} else {
					r = r->next = ra_malloc(ret, sizeof(route));
				}
				if (r == NULL) {
					logerr("malloc failed for match '%s'\n", pat);
					router_free(ret);
					return NULL;
				}
				r->next = NULL;
				if (m == NULL)
					m = r;
				if (strcmp(pat, "*") == 0) {
					r->pattern = NULL;
					r->strmatch = NULL;
					r->matchtype = MATCHALL;
				} else {
					int err = determine_if_regex(r, pat,
							REG_EXTENDED | REG_NOSUB);
					if (err != 0) {
						char ebuf[512];
						regerror(err, &r->rule, ebuf, sizeof(ebuf));
						logerr("invalid expression '%s' for match: %s\n",
								pat, ebuf);
						router_free(ret);
						return NULL;
					}
				}
			} while (
					(strncmp(p, "send", 4) != 0 || !isspace(*(p + 4))) &&
					(strncmp(p, "validate", 8) != 0 || !isspace(*(p + 8))));
			if (*p == 'v') {
				p += 9;

				if (dw == NULL) {
					dw = d = ra_malloc(ret, sizeof(destinations));
				} else {
					d = d->next = ra_malloc(ret, sizeof(destinations));
				}
				if (d == NULL) {
					logerr("out of memory allocating new validation "
							"for 'match %s'\n", pat);
					router_free(ret);
					return NULL;
				}
				d->next = NULL;
				if ((d->cl = ra_malloc(ret, sizeof(cluster))) == NULL) {
					logerr("malloc failed for validation rule in 'match %s'\n",
							pat);
					router_free(ret);
					return NULL;
				}
				d->cl->name = NULL;
				d->cl->type = VALIDATION;
				d->cl->next = NULL;
				d->cl->members.validation = ra_malloc(ret, sizeof(validate));
				if (d->cl->members.validation == NULL) {
					logerr("malloc failed for validation member in "
							"'match %s'\n", pat);
					router_free(ret);
					return NULL;
				}

				for (; *p != '\0' && isspace(*p); p++)
					;
				pat = p;
				do {
					for (; *p != '\0' && !isspace(*p); p++)
						;
				} while (*p != '\0' && *(p - 1) == '\\' && *(p++) != '\0');
				if (*p == '\0') {
					logerr("unexpected end of file after 'validate'\n");
					router_free(ret);
					return NULL;
				}
				*p++ = '\0';
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strcmp(pat, "*") == 0) {
					logerr("cannot use '*' as validate expression\n");
					router_free(ret);
					return NULL;
				} else {
					route *rule = ra_malloc(ret, sizeof(route));
					int err;

					if (rule == NULL) {
						logerr("malloc failed for validate '%s'\n", pat);
						router_free(ret);
						return NULL;
					}
					rule->next = NULL;
					rule->dests = NULL;
					d->cl->members.validation->rule = rule;

					err = determine_if_regex(rule, pat,
							REG_EXTENDED | REG_NOSUB);
					if (err != 0) {
						char ebuf[512];
						regerror(err, &rule->rule, ebuf, sizeof(ebuf));
						logerr("invalid expression '%s' for validate: %s\n",
								pat, ebuf);
						router_free(ret);
						return NULL;
					}
				}

				if (strncmp(p, "else", 4) != 0 || !isspace(*(p + 4))) {
					logerr("expected 'else' after validate %s\n", pat);
					router_free(ret);
					return NULL;
				}
				p += 5;

				for (; *p != '\0' && isspace(*p); p++)
					;

				if ((strncmp(p, "drop", 4) != 0 || !isspace(*(p + 4))) &&
						(strncmp(p, "log", 3) != 0 || !isspace(*(p + 3))))
				{
					logerr("expected 'drop' or 'log' after validate %s else\n",
							pat);
					router_free(ret);
					return NULL;
				}
				if (*p == 'd') {
					p += 5;
					d->cl->members.validation->action = VAL_DROP;
				} else {
					p += 4;
					d->cl->members.validation->action = VAL_LOG;
				}

				for (; *p != '\0' && isspace(*p); p++)
					;
				if (*p == '\0') {
					logerr("unexpected end of file after validate %s\n", pat);
					router_free(ret);
					return NULL;
				}

				if (strncmp(p, "stop", 4) == 0) {
					if (isspace(*(p + 4))) {
						p += 5;
						for (; *p != '\0' && isspace(*p); p++)
							;
					} else if (*(p + 4) != ';') {
						logerr("expected ';' after 'stop' for validate %s\n",
								pat);
						router_free(ret);
						return NULL;
					}
				}
				if (*p == ';') {
					p++;
					do {
						m->dests = dw;
						/* always ignore stop here, else the whole
						 * contruct makes no sense */
						m->stop = 0;
					} while (m != r && (m = m->next) != NULL);
					continue;
				}

				if (strncmp(p, "send", 4) != 0 || !isspace(*(p + 4))) {
					logerr("expected 'send to' after validate %s\n", pat);
					router_free(ret);
					return NULL;
				}
			}
			p += 5;
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "to", 2) != 0 || !isspace(*(p + 2))) {
				logerr("expected 'to' after 'send' for match %s\n",
						r->pattern ? r->pattern : "*");
				router_free(ret);
				return NULL;
			}
			p += 3;
			for (; *p != '\0' && isspace(*p); p++)
				;
			do {
				char save;
				dest = p;
				for (; *p != '\0' && !isspace(*p) && *p != ';'; p++)
					;
				if (*p == '\0')
					break;
				save = *p;
				*p = '\0';

				/* lookup dest */
				for (w = ret->clusters; w != NULL; w = w->next) {
					if (w->type != GROUP &&
							w->type != AGGRSTUB &&
							w->type != STATSTUB &&
							w->type != AGGREGATION &&
							w->type != REWRITE &&
							w->type != VALIDATION &&
							strcmp(w->name, dest) == 0)
						break;
				}
				if (w == NULL) {
					logerr("no such cluster '%s' for 'match %s'\n", dest,
							r->pattern ? r->pattern : "*");
					router_free(ret);
					return NULL;
				}

				if (dw == NULL) {
					dw = d = ra_malloc(ret, sizeof(destinations));
				} else {
					d = d->next = ra_malloc(ret, sizeof(destinations));
				}
				if (d == NULL) {
					logerr("out of memory allocating new destination '%s' "
							"for 'match %s'\n", dest,
							r->pattern ? r->pattern : "*");
					router_free(ret);
					return NULL;
				}
				d->cl = w;
				d->next = NULL;

				*p = save;
				for (; *p != '\0' && isspace(*p); p++)
					;
			} while (*p != ';' &&
					!(strncmp(p, "stop", 4) == 0 &&
						(isspace(*(p + 4)) || *(p + 4) == ';')));

			if (*p == '\0') {
				logerr("unexpected end of file after 'send to %s'\n",
						dest);
				router_free(ret);
				return NULL;
			} else if (*p == ';') {
				stop = 0;
			} else { /* due to strncmp above, this must be stop */
				p += 4;
				stop = 1;
			}

			/* find the closing ';' */
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (*p != ';') {
				logerr("expected ';' after match %s\n", pat);
				router_free(ret);
				return NULL;
			}
			p++;

			/* fill in the destinations for all the routes */
			do {
				m->dests = dw;
				m->stop = w->type == BLACKHOLE ? 1 : stop;
			} while (m != r && (m = m->next) != NULL);

			if (matchcatchallfound) {
				logerr("warning: match %s will never be matched "
						"due to preceding match * ... stop\n",
						r->pattern == NULL ? "*" : r->pattern);
			}
			if (r->matchtype == MATCHALL && r->stop)
				matchcatchallfound = 1;
		} else if (strncmp(p, "aggregate", 9) == 0 && isspace(*(p + 9))) {
			/* aggregation rule */
			char *type;
			char *pat;
			char *num;
			enum _aggr_timestamp tswhen = TS_END;
			cluster *w;
			int err;
			int intv;
			int exp;
			char stop = 0;
			route *m = NULL;
			destinations *d = NULL;
			destinations *dw = NULL;

			p += 10;
			for (; *p != '\0' && isspace(*p); p++)
				;

			w = ra_malloc(ret, sizeof(cluster));
			if (w == NULL) {
				logerr("malloc failed for aggregate\n");
				router_free(ret);
				return NULL;
			}
			w->name = NULL;
			w->type = AGGREGATION;
			w->next = NULL;

			do {
				pat = p;
				for (; *p != '\0' && !isspace(*p); p++)
					;
				if (*p == '\0') {
					logerr("unexpected end of file after 'aggregate'\n");
					router_free(ret);
					return NULL;
				}
				*p++ = '\0';
				for (; *p != '\0' && isspace(*p); p++)
					;

				if (r == NULL) {
					ret->routes = r = ra_malloc(ret, sizeof(route));
				} else {
					r = r->next = ra_malloc(ret, sizeof(route));
				}
				if (r == NULL) {
					logerr("malloc failed for aggregate route\n");
					router_free(ret);
					return NULL;
				}
				r->next = NULL;
				if (m == NULL)
					m = r;
				err = determine_if_regex(r, pat, REG_EXTENDED);
				if (err != 0) {
					char ebuf[512];
					regerror(err, &r->rule, ebuf, sizeof(ebuf));
					logerr("invalid expression '%s' "
							"for aggregation: %s\n", pat, ebuf);
					router_free(ret);
					return NULL;
				}
			} while (strncmp(p, "every", 5) != 0 || !isspace(*(p + 5)));
			p += 6;
			for (; *p != '\0' && isspace(*p); p++)
				;
			num = p;
			for (; *p != '\0' && isdigit(*p); p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of file after 'every %s'\n", num);
				router_free(ret);
				return NULL;
			}
			if (!isspace(*p)) {
				logerr("unexpected character '%c', "
						"expected number after 'every'\n", *p);
				router_free(ret);
				return NULL;
			}
			*p++ = '\0';
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "seconds", 7) != 0 || !isspace(*(p + 7))) {
				logerr("expected 'seconds' after 'every %s'\n", num);
				router_free(ret);
				return NULL;
			}
			p += 8;
			intv = atoi(num);
			if (intv == 0) {
				logerr("interval must be non-zero\n");
				router_free(ret);
				return NULL;
			}
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "expire", 6) != 0 || !isspace(*(p + 6))) {
				logerr("expected 'expire after' after 'every %s seconds\n",
						num);
				router_free(ret);
				return NULL;
			}
			p += 7;
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "after", 5) != 0 || !isspace(*(p + 5))) {
				logerr("expected 'after' after 'expire'\n");
				router_free(ret);
				return NULL;
			}
			p += 6;
			for (; *p != '\0' && isspace(*p); p++)
				;
			num = p;
			for (; *p != '\0' && isdigit(*p); p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of file after 'expire after %s'\n",
						num);
				router_free(ret);
				return NULL;
			}
			if (!isspace(*p)) {
				logerr("unexpected character '%c', "
						"expected number after 'expire after'\n", *p);
				router_free(ret);
				return NULL;
			}
			*p++ = '\0';
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "seconds", 7) != 0 || !isspace(*(p + 7))) {
				logerr("expected 'seconds' after 'expire after %s'\n", num);
				router_free(ret);
				return NULL;
			}
			p += 8;
			exp = atoi(num);
			if (exp == 0) {
				logerr("expire must be non-zero\n");
				router_free(ret);
				return NULL;
			}
			if (exp < intv) {
				logerr("expire (%d) must be greater than interval (%d)\n",
						exp, intv);
				router_free(ret);
				return NULL;
			}
			for (; *p != '\0' && isspace(*p); p++)
				;

			/* optional timestamp bit */
			if (strncmp(p, "timestamp", 9) == 0 && isspace(*(p + 9))) {
				p += 10;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "at", 2) != 0 || !isspace(*(p + 2))) {
					logerr("expected 'at' after 'timestamp'\n");
					router_free(ret);
					return NULL;
				}
				p += 3;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "start", 5) == 0 && isspace(*(p + 5))) {
					p += 6;
					tswhen = TS_START;
				} else if (strncmp(p, "middle", 6) == 0 && isspace(*(p + 6))) {
					p += 7;
					tswhen = TS_MIDDLE;
				} else if (strncmp(p, "end", 3) == 0 && isspace(*(p + 3))) {
					p += 4;
					tswhen = TS_END;
				} else {
					logerr("expected 'start', 'middle' or 'end' "
							"after 'timestamp at'\n");
					router_free(ret);
					return NULL;
				}
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "of", 2) != 0 || !isspace(*(p + 2))) {
					logerr("expected 'of' after 'timestamp at ...'\n");
					router_free(ret);
					return NULL;
				}
				p += 3;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "bucket", 6) != 0 || !isspace(*(p + 6))) {
					logerr("expected 'bucket' after 'timestamp at ... of'\n");
					router_free(ret);
					return NULL;
				}
				p += 7;
				for (; *p != '\0' && isspace(*p); p++)
					;
			}

			w->members.aggregation = aggregator_new(intv, exp, tswhen);
			if (w->members.aggregation == NULL) {
				logerr("out of memory while allocating new aggregator\n");
				router_free(ret);
				return NULL;
			}
			if (a == NULL) {
				ret->aggregators = a = w->members.aggregation;
			} else {
				a = a->next = w->members.aggregation;
			}
			do {
				if (strncmp(p, "compute", 7) != 0 || !isspace(*(p + 7))) {
					logerr("expected 'compute' at: %.20s\n", p);
					router_free(ret);
					return NULL;
				}
				p += 8;
				pat = p;
				for (; *p != '\0' && !isspace(*p); p++)
					;
				if (*p == '\0') {
					logerr("unexpected end of file after 'compute'\n");
					router_free(ret);
					return NULL;
				}
				*p++ = '\0';
				type = pat;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "write", 5) != 0 || !isspace(*(p + 5))) {
					logerr("expected 'write to' after 'compute %s'\n", pat);
					router_free(ret);
					return NULL;
				}
				p += 6;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "to", 2) != 0 || !isspace(*(p + 2))) {
					logerr("expected 'write to' after 'compute %s'\n", pat);
					router_free(ret);
					return NULL;
				}
				p += 3;
				for (; *p != '\0' && isspace(*p); p++)
					;
				pat = p;
				for (; *p != '\0' && !isspace(*p); p++)
					;
				if (*p == '\0') {
					logerr("unexpected end of file after 'write to'\n");
					router_free(ret);
					return NULL;
				}
			 	*p++ = '\0';

				if (aggregator_add_compute(w->members.aggregation, pat, type) != 0) {
					logerr("expected sum, count, max, min, average, "
							"median, percentile<%>, variance or stddev "
							"after 'compute', got '%s'\n", type);
					router_free(ret);
					return NULL;
				}

				for (; *p != '\0' && isspace(*p); p++)
					;
			} while (*p != ';' &&
					!(strncmp(p, "send", 4) == 0 && isspace(*(p + 4))) &&
					!(strncmp(p, "stop", 4) == 0 &&
						(isspace(*(p + 4)) || *(p + 4) == ';')));

			if (strncmp(p, "send", 4) == 0 && isspace(*(p + 4))) {
				cluster *cw;
				p += 5;
				for (; *p != '\0' && isspace(*p); p++)
					;
				if (strncmp(p, "to", 2) != 0 || !isspace(*(p + 2))) {
					logerr("expected 'to' after 'send' for aggregate\n");
					router_free(ret);
					return NULL;
				}
				p += 3;
				for (; *p != '\0' && isspace(*p); p++)
					;
				do {
					char save;
					char *dest = p;
					for (; *p != '\0' && !isspace(*p) && *p != ';'; p++)
						;
					if (*p == '\0')
						break;
					save = *p;
					*p = '\0';

					/* lookup dest */
					for (cw = ret->clusters; cw != NULL; cw = cw->next) {
						if (cw->type != GROUP &&
								cw->type != AGGRSTUB &&
								cw->type != STATSTUB &&
								cw->type != AGGREGATION &&
								cw->type != REWRITE &&
								cw->type != VALIDATION &&
								strcmp(cw->name, dest) == 0)
							break;
					}
					if (cw == NULL) {
						logerr("no such cluster '%s' for aggregate\n", dest);
						router_free(ret);
						return NULL;
					}

					if (dw == NULL) {
						dw = d = ra_malloc(ret, sizeof(destinations));
					} else {
						d = d->next = ra_malloc(ret, sizeof(destinations));
					}
					if (d == NULL) {
						logerr("out of memory allocating new destination '%s' "
								"for aggregation\n", dest);
						router_free(ret);
						return NULL;
					}
					d->cl = cw;
					d->next = NULL;

					*p = save;
					for (; *p != '\0' && isspace(*p); p++)
						;
				} while (*p != ';' &&
						!(strncmp(p, "stop", 4) == 0 &&
							(isspace(*(p + 4)) || *(p + 4) == ';')));
			}

			if (strncmp(p, "stop", 4) == 0 &&
					(isspace(*(p + 4)) || *(p + 4) == ';'))
			{
				p += 4;
				stop = 1;
				for (; *p != '\0' && isspace(*p); p++)
					;
			}

			if (*p == '\0') {
				logerr("unexpected end of file after 'aggregate %s'\n",
						r->pattern);
				router_free(ret);
				return NULL;
			}
			if (*p != ';') {
				logerr("expected ';' after aggregate %s\n", r->pattern);
				router_free(ret);
				return NULL;
			}
			p++;

			/* add cluster to the list of clusters */
			cl = cl->next = w;

			/* fill in the destinations for all the routes, this is for
			 * printing and free-ing */
			do {
				m->dests = ra_malloc(ret, sizeof(destinations));
				if (m->dests == NULL) {
					logerr("malloc failed for aggregation destinations\n");
					router_free(ret);
					return NULL;
				}
				m->dests->cl = w;
				m->dests->next = dw;
				m->stop = stop;
			} while (m != r && (m = m->next) != NULL);

			if (dw != NULL) {
				char stubname[48];

				m = ra_malloc(ret, sizeof(route));
				if (m == NULL) {
					logerr("malloc failed for aggregation destination stub\n");
					router_free(ret);
					return NULL;
				}
				m->pattern = NULL;
				m->strmatch = NULL;
				m->dests = dw;
				m->stop = 1;
				m->matchtype = MATCHALL;
				m->next = NULL;

				/* inject stub route for dests */
				d = ra_malloc(ret, sizeof(destinations));
				if (d == NULL) {
					logerr("malloc failed for aggr stub route\n");
					router_free(ret);
					return NULL;
				}
				d->next = NULL;
				cl = cl->next = d->cl = ra_malloc(ret, sizeof(cluster));
				if (cl == NULL) {
					logerr("malloc failed for aggr stub destinations\n");
					router_free(ret);
					return NULL;
				}
				cl->name = NULL;
				cl->type = AGGRSTUB;
				cl->members.routes = m;
				cl->next = NULL;

				snprintf(stubname, sizeof(stubname),
						STUB_AGGR "%p__", w->members.aggregation);
				m = ra_malloc(ret, sizeof(route));
				if (m == NULL) {
					logerr("malloc failed for catch stub aggr route\n");
					router_free(ret);
					return NULL;
				}
				m->pattern = ra_strdup(ret, stubname);
				m->strmatch = ra_strdup(ret, stubname);
				if (m->pattern == NULL || m->strmatch == NULL) {
					logerr("malloc failed for catch stub aggr route pattern\n");
					router_free(ret);
					return NULL;
				}
				m->dests = d;
				m->stop = 1;
				m->matchtype = STARTS_WITH;
				/* enforce first match to avoid interference */
				m->next = ret->routes;
				ret->routes = m;

				aggregator_set_stub(w->members.aggregation, stubname);
			}

			if (matchcatchallfound) {
				logerr("warning: aggregate %s will never be matched "
						"due to preceding match * ... stop\n",
						r->pattern);
			}
		} else if (strncmp(p, "rewrite", 7) == 0 && isspace(*(p + 7))) {
			/* rewrite rule */
			char *pat;
			char *replacement;
			int err;

			p += 8;
			for (; *p != '\0' && isspace(*p); p++)
				;
			pat = p;
			for (; *p != '\0' && !isspace(*p); p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of file after 'rewrite'\n");
				router_free(ret);
				return NULL;
			}
			*p++ = '\0';
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "into", 4) != 0 || !isspace(*(p + 4))) {
				logerr("expected 'into' after rewrite %s\n", pat);
				router_free(ret);
				return NULL;
			}
			p += 5;
			for (; *p != '\0' && isspace(*p); p++)
				;
			replacement = p;
			for (; *p != '\0' && !isspace(*p) && *p != ';'; p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of file after 'into %s'\n",
						replacement);
				router_free(ret);
				return NULL;
			} else if (*p == ';') {
				*p++ = '\0';
			} else {
				*p++ = '\0';
				for (; *p != '\0' && isspace(*p) && *p != ';'; p++)
					;
				if (*p != ';') {
					logerr("expected ';' after %s\n", replacement);
					router_free(ret);
					return NULL;
				}
				p++;
			}

			if (r == NULL) {
				ret->routes = r = ra_malloc(ret, sizeof(route));
			} else {
				r = r->next = ra_malloc(ret, sizeof(route));
			}
			if (r == NULL) {
				logerr("malloc failed for rewrite route\n");
				router_free(ret);
				return NULL;
			}
			r->next = NULL;
			err = determine_if_regex(r, pat, REG_EXTENDED | REG_FORCE);
			if (err != 0) {
				char ebuf[512];
				regerror(err, &r->rule, ebuf, sizeof(ebuf));
				logerr("invalid expression '%s' for rewrite: %s\n", pat, ebuf);
				router_free(ret);
				return NULL;
			}

			if ((cl = cl->next = ra_malloc(ret, sizeof(cluster))) == NULL) {
				logerr("malloc failed for rewrite destination\n");
				router_free(ret);
				return NULL;
			}
			cl->type = REWRITE;
			cl->name = NULL;
			cl->members.replacement = ra_strdup(ret, replacement);
			cl->next = NULL;
			if (cl->members.replacement == NULL) {
				logerr("malloc failed for rewrite cluster replacement\n");
				router_free(ret);
				return NULL;
			}

			r->dests = ra_malloc(ret, sizeof(destinations));
			if (r->dests == NULL) {
				logerr("malloc failed for rewrite destinations\n");
				router_free(ret);
				return NULL;
			}
			r->dests->cl = cl;
			r->dests->next = NULL;
			r->stop = 0;
			r->next = NULL;

			if (matchcatchallfound) {
				logerr("warning: rewrite %s will never be matched "
						"due to preceding match * ... stop\n",
						r->pattern == NULL ? "*" : r->pattern);
			}
		} else if (strncmp(p, "send", 4) == 0 && isspace(*(p + 4))) {
			cluster *cw;
			destinations *d = NULL;
			destinations *dw = NULL;
			route *m;
			char stubname[48];

			p += 4;
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "statistics", 10) != 0 || !isspace(*(p + 10))) {
				logerr("expected 'statistics' after 'send'\n");
				router_free(ret);
				return NULL;
			}
			p += 10;
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (strncmp(p, "to", 2) != 0 || !isspace(*(p + 2))) {
				logerr("expected 'to' after 'send statistics'\n");
				router_free(ret);
				return NULL;
			}
			p += 2;
			if (ret->collector_stub != NULL) {
				logerr("duplicate 'send statistics to' not allowed, "
						"use multiple destinations instead\n");
				router_free(ret);
				return NULL;
			}
			for (; *p != '\0' && isspace(*p); p++)
				;
			do {
				char save;
				char *dest = p;
				for (; *p != '\0' && !isspace(*p) && *p != ';'; p++)
					;
				if (*p == '\0' || *p == ';')
					break;
				save = *p;
				*p = '\0';

				/* lookup dest */
				for (cw = ret->clusters; cw != NULL; cw = cw->next) {
					if (cw->type != GROUP &&
							cw->type != AGGRSTUB &&
							cw->type != STATSTUB &&
							cw->type != AGGREGATION &&
							cw->type != REWRITE &&
							cw->type != VALIDATION &&
							strcmp(cw->name, dest) == 0)
						break;
				}
				if (cw == NULL) {
					logerr("no such cluster '%s' for send statistics to\n",
							dest);
					router_free(ret);
					return NULL;
				}

				if (dw == NULL) {
					dw = d = ra_malloc(ret, sizeof(destinations));
				} else {
					d = d->next = ra_malloc(ret, sizeof(destinations));
				}
				if (d == NULL) {
					logerr("out of memory allocating new destination '%s' "
							"for 'send statistics to'\n", dest);
					router_free(ret);
					return NULL;
				}
				d->cl = cw;
				d->next = NULL;

				*p = save;
				for (; *p != '\0' && isspace(*p); p++)
					;
			} while (*p != ';' &&
					!(strncmp(p, "stop", 4) == 0 &&
						(isspace(*(p + 4)) || *(p + 4) == ';')));
			if (strncmp(p, "stop", 4) == 0 &&
					(isspace(*(p + 4)) || *(p + 4) == ';'))
			{
				p += 4;
				for (; *p != '\0' && isspace(*p); p++)
					;
			}
			if (*p == ';')
				p++;

			if (dw == NULL) {
				logerr("expected cluster name after 'send statistics to'\n");
				router_free(ret);
				return NULL;
			}

			/* create a top-rule with match for unique prefix and send
			 * that to the defined targets */
			m = ra_malloc(ret, sizeof(route));
			if (m == NULL) {
				logerr("malloc failed for send statistics route\n");
				router_free(ret);
				return NULL;
			}
			m->pattern = NULL;
			m->strmatch = NULL;
			m->dests = dw;
			m->stop = 1;
			m->matchtype = MATCHALL;
			m->next = NULL;

			/* inject stub route for dests */
			d = ra_malloc(ret, sizeof(destinations));
			if (d == NULL) {
				logerr("malloc failed for send statistics stub\n");
				router_free(ret);
				return NULL;
			}
			d->next = NULL;
			cl = cl->next = d->cl = ra_malloc(ret, sizeof(cluster));
			if (cl == NULL) {
				logerr("malloc failed for send statistics cluster\n");
				router_free(ret);
				return NULL;
			}
			cl->name = NULL;
			cl->type = STATSTUB;
			cl->members.routes = m;
			cl->next = NULL;

			snprintf(stubname, sizeof(stubname),
					STUB_STATS "%p__", ret);
			m = ra_malloc(ret, sizeof(route));
			if (m == NULL) {
				logerr("malloc failed for send statistics route stub\n");
				router_free(ret);
				return NULL;
			}
			m->pattern = ra_strdup(ret, stubname);
			m->strmatch = ra_strdup(ret, stubname);
			if (m->pattern == NULL || m->strmatch == NULL) {
				logerr("malloc failed for send statistics stub pattern\n");
				router_free(ret);
				return NULL;
			}
			m->dests = d;
			m->stop = 1;
			m->matchtype = STARTS_WITH;
			/* enforce first match to avoid interference */
			m->next = ret->routes;
			ret->routes = m;
			if (r == NULL)
				r = ret->routes;

			ret->collector_stub = m->pattern;
		} else if (strncmp(p, "include", 7) == 0 && isspace(*(p + 7))) {
			char *name = NULL;
			char endchar;

			p += 7;
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of input after 'include'\n");
				router_free(ret);
				return NULL;
			}
			name = p;
			for (; *p != '\0' && !isspace(*p) && *p != ';'; p++)
				;
			if (*p == '\0') {
				logerr("unexpected end of input after 'include %s' "
						"(did you forget ';'?)\n", name);
				router_free(ret);
				return NULL;
			}
			endchar = *p;
			*p = '\0';
			if (strchr(name, '*') || strchr(name, '?') ||
					(strchr(name, '[') && strchr(name, ']')))
			{
				/* include path is a glob pattern */
				glob_t globbuf;
				char *globpath;
				size_t i;

				if ((i = glob(name, 0, NULL, &globbuf)) != 0) {
					switch (i) {
						case GLOB_NOSPACE:
							/* since we don't set a limit, we won't
							 * reach it either */
							logerr("out of memory while globbing files for "
									"pattern '%s'\n", name);
							router_free(ret);
							ret = NULL;
							break;
						case GLOB_ABORTED:
							/* we don't use a call-back, so this is
							 * a real error of some sort */
							logerr("unable to match any files from "
									"pattern '%s': %s\n",
									name, strerror(errno));
							router_free(ret);
							ret = NULL;
							break;
						case GLOB_NOMATCH:
							/* we don't want to abort on failing globs */
							logout("warning: pattern '%s' did not match "
									"any files\n", name);
							break;
					}
				}

				/* read all files matched by glob */
				for (i = 0; i < globbuf.gl_pathc; i++) {
					globpath = globbuf.gl_pathv[i];
					ret = router_readconfig(ret, globpath, queuesize,
							batchsize, maxstalls, iotimeout, sockbufsize);
					if (ret == NULL)
						break;
				}

				/* also after errors the globbuf structure is
				 * initialised and might need free-ing */
				globfree(&globbuf);
			} else {
				/* include path is a regular file path */
				ret = router_readconfig(ret, name, queuesize,
						batchsize, maxstalls, iotimeout, sockbufsize);
			}
			if (ret == NULL)
				/* router_readconfig already barked and freed ret */
				return NULL;
			/* the included file could have added new aggregates,
			 * matches, or clusters, so adjust these chains to point to
			 * the new end. */
			for (a = ret->aggregators; a != NULL && a->next != NULL; a = a->next)
				;
			for (cl = ret->clusters; cl->next != NULL; cl = cl->next)
				;
			for (r = ret->routes; r != NULL && r->next != NULL; r = r->next)
				;
			*p = endchar;
			for (; *p != '\0' && isspace(*p); p++)
				;
			if (*p != ';') {
				logerr("expected ';' after 'include %s'\n", name);
				router_free(ret);
				return NULL;
			}
			p++;
		} else {
			/* garbage? */
			logerr("unexpected input in config: %s\n", p);
			router_free(ret);
			return NULL;
		}
	} while (*p != '\0');

	return ret;
}

typedef struct _block {
	char *pattern;
	size_t hash;
	size_t refcnt;
	size_t seqnr;
	route *firstroute;
	route *lastroute;
	struct _block *prev;
	struct _block *next;
} block;

/**
 * Tries to optimise the match and aggregation rules in such a way that
 * the number of matches for non-matching metrics are reduced.  The
 * problem is that with many metrics flowing in, the time to perform
 * lots of regex matches is high.  This is not too bad if that time
 * spent actually results in a metric being counted (aggregation) or
 * sent further (match), but it is when the metric would be discarded
 * for it did not match anything.
 * Hence, we employ a simple strategy to try and reduce the incoming
 * stream of metrics as soon as possible before performing the more
 * specific and expensive matches to confirm fit.
 */
void
router_optimise(router *r)
{
	char *p;
	char pblock[64];
	char *b;
	route *rwalk;
	route *rnext;
	route *rlast;
	block *blocks;
	block *bwalk;
	block *bstart;
	block *blast;
	size_t bsum;
	size_t seq;

	/* avoid optimising anything if it won't pay off */
	seq = 0;
	for (rwalk = r->routes; rwalk != NULL && seq < 50; rwalk = rwalk->next)
		seq++;
	if (seq < 50)
		return;

	/* Heuristic: the last part of the matching regex is the most
	 * discriminating part of the metric.  The last part is defined as a
	 * block of characters matching [a-zA-Z_]+ at the end disregarding
	 * any characters not matched by the previous expression.  Then from
	 * these last parts we create groups, that -- if having enough
	 * members -- is used to reduce the amount of comparisons done
	 * before determining that an input metric cannot match any
	 * expression we have defined. */
	seq = 0;
	blast = bstart = blocks = malloc(sizeof(block));
	blocks->refcnt = 0;
	blocks->pattern = NULL;
	blocks->hash = 0;
	blocks->seqnr = seq++;
	blocks->prev = NULL;
	blocks->next = NULL;
	rlast = NULL;
	for (rwalk = r->routes; rwalk != NULL; rwalk = rnext) {
		/* matchall and rewrite rules cannot be in a group (issue #218) */
		if (rwalk->matchtype == MATCHALL || rwalk->dests->cl->type == REWRITE) {
			blast->next = malloc(sizeof(block));
			blast->next->prev = blast;
			blast = blast->next;
			blast->pattern = NULL;
			blast->hash = 0;
			blast->refcnt = 1;
			blast->seqnr = seq++;
			blast->firstroute = rwalk;
			blast->lastroute = rwalk;
			blast->next = NULL;
			rnext = rwalk->next;
			rwalk->next = NULL;
			bstart = blast;
			rlast = NULL;
			continue;
		}

		p = rwalk->pattern + strlen(rwalk->pattern);
		/* strip off chars that won't belong to a block */
		bsum = 0;
		while (
				p > rwalk->pattern && (
					bsum > 0 || (
						(*p < 'a' || *p > 'z') &&
						(*p < 'A' || *p > 'Z') &&
						*p != '_'
					)
				)
			  )
		{
			if (*p == ')' || *p == '(') {
				char esc = 0;
				char oe = *p;
				/* trim escapes, need to figure out if this is escaped
				 * or not, in order to find the a matching open or not */
				for (--p; p > rwalk->pattern && *p == '\\'; p--)
					esc = !esc;
				if (!esc) /* not escaped */
					bsum += oe == ')' ? 1 : -1;
				continue;  /* skip p-- */
			}
			p--;
		}
		if (p == rwalk->pattern) {
			/* nothing we can do with a pattern like this */
			blast->next = malloc(sizeof(block));
			blast->next->prev = blast;
			blast = blast->next;
			blast->pattern = NULL;
			blast->hash = 0;
			blast->refcnt = 1;
			blast->seqnr = seq++;
			blast->firstroute = rwalk;
			blast->lastroute = rwalk;
			blast->next = NULL;
			rnext = rwalk->next;
			rwalk->next = NULL;
			bstart = blast;
			rlast = NULL;
			continue;
		}
		/* find the block */
		bsum = 0;
		b = pblock;
		while (
				p >= rwalk->pattern && b - pblock < sizeof(pblock) &&
				(
					(*p >= 'a' && *p <= 'z') ||
					(*p >= 'A' && *p <= 'Z') ||
					*p == '_'
				)
			  )
		{
			bsum += *p;
			*b++ = *p--;
		}
		*b = '\0';
		b = pblock;
		if (strlen(b) < 3) {
			/* this probably isn't selective enough, don't put in a group */
			blast->next = malloc(sizeof(block));
			blast->next->prev = blast;
			blast = blast->next;
			blast->pattern = NULL;
			blast->hash = 0;
			blast->refcnt = 1;
			blast->seqnr = seq++;
			blast->firstroute = rwalk;
			blast->lastroute = rwalk;
			blast->next = NULL;
			rnext = rwalk->next;
			rwalk->next = NULL;
			bstart = blast;
			rlast = NULL;
			continue;
		}

		/* at this point, b points to the tail block in reverse, see if
		 * we already had such tail in place */
		for (bwalk = bstart; bwalk != NULL; bwalk = bwalk->next) {
			if (bwalk->hash != bsum || strcmp(bwalk->pattern, b) != 0)
				continue;
			break;
		}
		if (bwalk == NULL) {
			blast->next = malloc(sizeof(block));
			blast->next->prev = blast;
			blast = blast->next;
			blast->pattern = strdup(b);
			blast->hash = bsum;
			blast->refcnt = 1;
			blast->seqnr = seq;
			blast->firstroute = rwalk;
			blast->lastroute = rwalk;
			blast->next = NULL;
			rnext = rwalk->next;
			rwalk->next = NULL;
			if (rwalk->stop) {
				bstart = blast;
				rlast = NULL;
				seq++;
			}
			continue;
		}

		bwalk->refcnt++;
		bwalk->lastroute = bwalk->lastroute->next = rwalk;
		rnext = rwalk->next;
		rwalk->next = NULL;
		if (rlast == NULL || strcmp(rlast->pattern, b) == 0) {
			if (rwalk->stop) {
				/* move this one to the end */
				if (bwalk->next != NULL) {
					bwalk->prev->next = bwalk->next;
					blast = blast->next = bwalk;
					bwalk->next = NULL;
				}
				rlast = rwalk;
			}
		} else {
			bstart = blast;
			rlast = NULL;
			seq++;
		}
	}
	/* make loop below easier by appending a dummy (reuse the one from
	 * start) */
	blast = blast->next = blocks;
	blocks = blocks->next;
	blast->next = NULL;
	blast->seqnr = seq;

	rwalk = r->routes = NULL;
	seq = 1;
	/* create groups, if feasible */
	for (bwalk = blocks; bwalk != NULL; bwalk = blast) {
		if (bwalk->seqnr != seq) {
			seq++;
			blast = bwalk;
			continue;
		}

		if (bwalk->refcnt == 0) {
			blast = bwalk->next;
			free(bwalk);
			continue;
		} else if (bwalk->refcnt < 3) {
			if (r->routes == NULL) {
				r->routes = bwalk->firstroute;
			} else {
				rwalk->next = bwalk->firstroute;
			}
			rwalk = bwalk->lastroute;
			blast = bwalk->next;
			free(bwalk->pattern);
			free(bwalk);
		} else {
			if (r->routes == NULL) {
				rwalk = r->routes = malloc(sizeof(route));
			} else {
				rwalk = rwalk->next = malloc(sizeof(route));
			}
			rwalk->pattern = NULL;
			rwalk->stop = 0;
			rwalk->matchtype = CONTAINS;
			rwalk->dests = malloc(sizeof(destinations));
			rwalk->dests->cl = malloc(sizeof(cluster));
			rwalk->dests->cl->name = bwalk->pattern;
			rwalk->dests->cl->type = GROUP;
			rwalk->dests->cl->members.routes = bwalk->firstroute;
			rwalk->dests->cl->next = NULL;
			rwalk->dests->next = NULL;
			rwalk->next = NULL;
			blast = bwalk->next;
			free(bwalk);
		}
	}
}

/**
 * Returns all (unique) servers from the cluster-configuration.
 */
server **
router_getservers(router *r)
{
	server **ret;
	servers *s;
	int i;

	i = 0;
	for (s = r->srvrs; s != NULL; s = s->next)
		i++;

	ret = malloc(sizeof(server *) * (i + 1));
	if (ret == NULL)
		return NULL;

	i = 0;
	for (s = r->srvrs; s != NULL; s = s->next)
		ret[i++] = s->server;

	/* ensure NULL-termination */
	ret[i] = NULL;

	return ret;
}

/**
 * Returns all aggregators from this router configuration
 */
inline aggregator *
router_getaggregators(router *rtr)
{
	return rtr->aggregators;
}

/**
 * Returns the stub prefix to use for collector stats, or NULL if no
 * such thing should be employed.
 */
inline char *
router_getcollectorstub(router *rtr)
{
	return rtr->collector_stub;
}

/**
 * Mere debugging function to check if the configuration is picked up
 * alright.  If all is set to false, aggregation rules won't be printed.
 * This comes in handy because aggregations usually come in the order of
 * thousands.
 */
void
router_printconfig(router *rtr, FILE *f, char pmode)
{
	cluster *c;
	route *r;
	servers *s;

#define PPROTO \
	server_ctype(s->server) == CON_UDP ? " proto udp" : ""

	for (c = rtr->clusters; c != NULL; c = c->next) {
		if (c->type == BLACKHOLE || c->type == REWRITE ||
				c->type == GROUP || c->type == AGGREGATION ||
				c->type == AGGRSTUB || c->type == STATSTUB)
			continue;
		fprintf(f, "cluster %s\n", c->name);
		if (c->type == FORWARD) {
			fprintf(f, "    forward\n");
			for (s = c->members.forward; s != NULL; s = s->next)
				fprintf(f, "        %s:%d%s\n",
						server_ip(s->server), server_port(s->server), PPROTO);
		} else if (c->type == FILELOG || c->type == FILELOGIP) {
			fprintf(f, "    file%s\n", c->type == FILELOGIP ? " ip" : "");
			for (s = c->members.forward; s != NULL; s = s->next)
				fprintf(f, "        %s\n",
						server_ip(s->server));
		} else if (c->type == ANYOF || c->type == FAILOVER) {
			fprintf(f, "    %s\n", c->type == ANYOF ? "any_of" : "failover");
			for (s = c->members.anyof->list; s != NULL; s = s->next)
				fprintf(f, "        %s:%d%s\n",
						server_ip(s->server), server_port(s->server), PPROTO);
		} else if (c->type == CARBON_CH ||
				c->type == FNV1A_CH ||
				c->type == JUMP_CH)
		{
			fprintf(f, "    %s_ch replication %d\n",
					c->type == CARBON_CH ? "carbon" : 
					c->type == FNV1A_CH ? "fnv1a" : "jump_fnv1a",
					c->members.ch->repl_factor);
			for (s = c->members.ch->servers; s != NULL; s = s->next)
				fprintf(f, "        %s:%d%s%s%s\n",
						server_ip(s->server), server_port(s->server),
						server_instance(s->server) ? "=" : "",
						server_instance(s->server) ? server_instance(s->server) : "",
						PPROTO);
		}
		fprintf(f, "    ;\n");
		if (pmode & PMODE_HASH) {
			if (c->type == CARBON_CH ||
					c->type == FNV1A_CH ||
					c->type == JUMP_CH)
			{
				fprintf(f, "# hash ring for %s follows\n", c->name);
				ch_printhashring(c->members.ch->ring, f);
			}
		}
	}
	fprintf(f, "\n");
	for (r = rtr->routes; r != NULL; r = r->next) {
		if (r->dests->cl->type == AGGREGATION) {
			cluster *aggr = r->dests->cl;
			struct _aggr_computes *ac;
			char stubname[48];
			char percentile[16];

			if (!(pmode & PMODE_AGGR))
				continue;

			if (pmode & PMODE_STUB || r->dests->next == NULL) {
				stubname[0] = '\0';
			} else {
				snprintf(stubname, sizeof(stubname),
						STUB_AGGR "%p__", aggr->members.aggregation);
			}

			fprintf(f, "aggregate");
			if (r->next == NULL || r->next->dests->cl != aggr) {
				fprintf(f, " %s\n", r->pattern);
			} else {
				fprintf(f, "\n");
				do {
					fprintf(f, "        %s\n", r->pattern);
				} while (r->next != NULL && r->next->dests->cl == aggr
						&& (r = r->next) != NULL);
			}
			fprintf(f, "    every %u seconds\n"
					"    expire after %u seconds\n"
					"    timestamp at %s of bucket\n",
					aggr->members.aggregation->interval,
					aggr->members.aggregation->expire,
					aggr->members.aggregation->tswhen == TS_START ? "start" :
					aggr->members.aggregation->tswhen == TS_MIDDLE ? "middle" :
					aggr->members.aggregation->tswhen == TS_END ? "end" :
					"<unknown>");
			for (ac = aggr->members.aggregation->computes; ac != NULL; ac = ac->next) {
				snprintf(percentile, sizeof(percentile),
						"percentile%d", ac->percentile);
				fprintf(f, "    compute %s write to\n"
						"        %s\n",
						ac->type == SUM ? "sum" : ac->type == CNT ? "count" :
						ac->type == MAX ? "max" : ac->type == MIN ? "min" :
						ac->type == AVG ? "average" : 
						ac->type == MEDN ? "median" :
						ac->type == PCTL ? percentile :
						ac->type == VAR ? "variance" :
						ac->type == SDEV ? "stddev" :
						"<unknown>",
						ac->metric + strlen(stubname));
			}
			if (!(pmode & PMODE_STUB) && r->dests->next != NULL) {
				destinations *dn = r->dests->next;
				fprintf(f, "    send to");
				if (dn->next == NULL) {
					fprintf(f, " %s\n", dn->cl->name);
				} else {
					for (; dn != NULL; dn = dn->next)
						fprintf(f, "\n        %s", dn->cl->name);
					fprintf(f, "\n");
				}
			}
			fprintf(f, "%s    ;\n", r->stop ? "    stop\n" : "");
		} else if (r->dests->cl->type == REWRITE) {
			fprintf(f, "rewrite %s\n    into %s\n    ;\n",
					r->pattern, r->dests->cl->members.replacement);
		} else if (r->dests->cl->type == GROUP) {
			size_t cnt = 0;
			route *rwalk;
			char blockname[64];
			char *b = &blockname[sizeof(blockname) - 1];
			char *p;

			for (rwalk = r->dests->cl->members.routes; rwalk != NULL; rwalk = rwalk->next)
				cnt++;
			/* reverse the name, to make it human consumable */
			*b-- ='\0';
			for (p = r->dests->cl->name; *p != '\0' && b > blockname; p++)
				*b-- = *p;
			fprintf(f, "# common pattern group '%s' "
					"contains %zu aggregations/matches\n",
					++b, cnt);
			if (mode & PMODE_AGGR) {
				router srtr;
				memset(&srtr, 0, sizeof(srtr));
				srtr.routes = r->dests->cl->members.routes;
				/* recurse */
				router_printconfig(&srtr, f, pmode);
			}
		} else if (r->dests->cl->type == AGGRSTUB ||
				r->dests->cl->type == STATSTUB)
		{
			if (pmode & PMODE_STUB) {
				fprintf(f, "# stub match for aggregate/statistics rule "
						"with send to\n");
				fprintf(f, "match ^%s\n    send to", r->pattern);
				if (r->dests->cl->members.routes->dests->next == NULL) {
					fprintf(f, " %s",
							r->dests->cl->members.routes->dests->cl->name);
				} else {
					destinations *d = r->dests->cl->members.routes->dests;
					for (; d != NULL; d = d->next)
						fprintf(f, "\n        %s", d->cl->name);
				}
				fprintf(f, "%s\n    ;\n", r->stop ? "\n    stop" : "");
			} else if (r->dests->cl->type == STATSTUB) {
				destinations *d = r->dests->cl->members.routes->dests;
				fprintf(f, "send statistics to");
				if (d->next == NULL) {
					fprintf(f, " %s", d->cl->name);
				} else {
					for ( ; d != NULL; d = d->next)
						fprintf(f, "\n        %s", d->cl->name);
				}
				fprintf(f, "\n    stop\n    ;\n");
			}
		} else {
			route *or = r;
			destinations *d;
			fprintf(f, "match");
			if (r->next == NULL || r->next->dests != or->dests) {
				fprintf(f, " %s\n",
						r->matchtype == MATCHALL ? "*" : r->pattern);
			} else {
				fprintf(f, "\n");
				do {
					fprintf(f, "        %s\n",
							r->matchtype == MATCHALL ? "*" : r->pattern);
				} while (r->next != NULL && r->next->dests == or->dests
						&& (r = r->next) != NULL);
			}
			d = or->dests;
			if (d->cl->type == VALIDATION) {
				validate *v = d->cl->members.validation;
				fprintf(f, "    validate %s else %s\n",
						v->rule->pattern,
						v->action == VAL_LOG ? "log" : "drop");
				/* hide this pseudo target */
				d = d->next;
			}
			if (d != NULL) {
				fprintf(f, "    send to");
				if (d->next == NULL) {
					fprintf(f, " %s", d->cl->name);
				} else {
					for ( ; d != NULL; d = d->next)
						fprintf(f, "\n        %s", d->cl->name);
				}
				fprintf(f, "\n%s    ;\n", or->stop ? "    stop\n" : "");
			} else {
				fprintf(f, "    ;\n");
			}
		}
	}
	fflush(f);
}

/**
 * Prints the differences between old and new at the highest level of
 * the change.  (When a cluster is gone, its servers are not
 * considered.)  Returns whether there were changes found.
 */
char
router_printdiffs(router *old, router *new, FILE *out)
{
	FILE *f;
	int fd;
	char *tmp = getenv("TMPDIR");
	char patho[512];
	char pathn[512];
	char buf[1024];
	size_t len;
	int ret;
	mode_t mask;

	/* First idea was to printconfig both old and new, and run diff -u
	 * on them.  Simple and effective.  Does require diff and two files
	 * though.  For now, I guess we can live with that. */

	if (tmp == NULL)
		tmp = "/tmp";

	snprintf(patho, sizeof(patho), "%s/carbon-c-relay_route.XXXXXX", tmp);
	mask = umask(S_IWGRP | S_IWOTH);
	fd = mkstemp(patho);
	umask(mask);
	if (fd < 0) {
		logerr("failed to create temporary file: %s\n", strerror(errno));
		return 1;
	}
	f = fdopen(fd, "r+");
	if (f == NULL) {
		logerr("failed to open stream for file '%s': %s\n",
				patho, strerror(errno));
		return 1;
	}
	router_printconfig(old, f, PMODE_AGGR);
	fclose(f);

	snprintf(pathn, sizeof(pathn), "%s/carbon-c-relay_route.XXXXXX", tmp);
	mask = umask(S_IWGRP | S_IWOTH);
	fd = mkstemp(pathn);
	umask(mask);
	if (fd < 0) {
		logerr("failed to create temporary file: %s\n", strerror(errno));
		return 1;
	}
	f = fdopen(fd, "r+");
	if (f == NULL) {
		logerr("failed to open stream for file '%s': %s\n",
				pathn, strerror(errno));
		return 1;
	}
	router_printconfig(new, f, PMODE_AGGR);
	fclose(f);

	/* diff and print its output */
	snprintf(buf, sizeof(buf), "diff -u %s %s", patho, pathn);
	f = popen(buf, "r");
	while ((len = fread(buf, 1, sizeof(buf) - 1, f)) > 0) {
		if (fwrite(buf, len, 1, out) == 0)
			/* ignore */
			;
	}
	ret = pclose(f);

	unlink(patho);
	unlink(pathn);

	/* ret:
	 * 0 - no diffs
	 * 1 - diffs
	 * 2 - some error (file not found?)
	 * let's say that an error means potential difference
	 */
	return ret != 0;
}

/**
 * Evaluates all servers in new and if an identical server exists in
 * old, swaps their queues.
 */
void
router_transplant_queues(router *new, router *old)
{
	servers *os;
	servers *ns;

	for (ns = new->srvrs; ns != NULL; ns = ns->next) {
		for (os = old->srvrs; os != NULL; os = os->next) {
			if (strcmp(server_ip(ns->server), server_ip(os->server)) == 0 &&
					server_port(ns->server) == server_port(os->server) &&
					server_ctype(ns->server) == server_ctype(os->server))
			{
				server_swap_queue(ns->server, os->server);
				continue;
			}
		}
	}
}

/**
 * Starts all resources (servers) associated to this router.
 */
char
router_start(router *rtr)
{
	servers *s;
	char ret = 0;
	int err;

	for (s = rtr->srvrs; s != NULL; s = s->next) {
		if ((err = server_start(s->server)) != 0) {
			logerr("failed to start server %s:%u: %s\n",
					server_ip(s->server),
					server_port(s->server),
					strerror(err));
			ret = 1;
		}
	}

	return ret;
}

/**
 * Shuts down all resources (servers) associated to this router.
 */
void
router_shutdown(router *rtr)
{
	servers *s;

	for (s = rtr->srvrs; s != NULL; s = s->next)
		server_shutdown(s->server);
}

/**
 * Free the routes and all associated resources.
 */
void
router_free(router *rtr)
{
	servers *s;

	router_free_intern(rtr->clusters, rtr->routes);

	/* free all servers from the pool, in case of secondaries, the
	 * previous call to router_shutdown made sure nothing references the
	 * servers anymore */
	for (s = rtr->srvrs; s != NULL; s = s->next)
		server_free(s->server);

	ra_free(rtr);
	free(rtr);
}

inline static char
router_metric_matches(
		const route *r,
		char *metric,
		char *firstspace,
		regmatch_t *pmatch)
{
	char ret = 0;
	char firstspc = *firstspace;

	switch (r->matchtype) {
		case MATCHALL:
			ret = 1;
			break;
		case REGEX:
			*firstspace = '\0';
			ret = regexec(&r->rule, metric, r->nmatch, pmatch, 0) == 0;
			*firstspace = firstspc;
			break;
		case CONTAINS:
			*firstspace = '\0';
			ret = strstr(metric, r->strmatch) != NULL;
			*firstspace = firstspc;
			break;
		case STARTS_WITH:
			ret = strncmp(metric, r->strmatch, strlen(r->strmatch)) == 0;
			break;
		case ENDS_WITH:
			*firstspace = '\0';
			ret = strcmp(
					firstspace - strlen(r->strmatch),
					r->strmatch) == 0;
			*firstspace = firstspc;
			break;
		case MATCHES:
			*firstspace = '\0';
			ret = strcmp(metric, r->strmatch) == 0;
			*firstspace = firstspc;
			break;
		default:
			ret = 0;
			break;
	}

	return ret;
}

inline size_t
router_rewrite_metric(
		char (*newmetric)[METRIC_BUFSIZ],
		char **newfirstspace,
		const char *metric,
		const char *firstspace,
		const char *replacement,
		const size_t nmatch,
		const regmatch_t *pmatch)
{
	char escape = 0;
	int ref = 0;
	char *s = *newmetric;
	const char *p;
	const char *q;
	const char *t;
	enum rewrite_case { RETAIN, LOWER, UPPER } rcase = RETAIN;

	assert(pmatch != NULL);

	/* insert leading part */
	q = metric;
	t = metric + pmatch[0].rm_so;
	if (s - *newmetric + t - q < sizeof(*newmetric)) {
		while (q < t)
			*s++ = *q++;
	} else {
		return 0;  /* won't fit, don't try further */
	}

	for (p = replacement; ; p++) {
		switch (*p) {
			case '\\':
				if (!escape) {
					escape = 1;
					rcase = RETAIN;
					break;
				}
				/* fall through so we handle \1\2 */
			default:
				if (escape == 1 && rcase == RETAIN && *p == '_') {
					rcase = LOWER;
				} else if (escape == 1 && rcase == RETAIN && *p == '^') {
					rcase = UPPER;
				} else if (escape && *p >= '0' && *p <= '9') {
					escape = 2;
					ref *= 10;
					ref += *p - '0';
				} else {
					if (escape) {
						if (ref > 0 && ref <= nmatch
								&& pmatch[ref].rm_so >= 0)
						{
							/* insert match part */
							q = metric + pmatch[ref].rm_so;
							t = metric + pmatch[ref].rm_eo;
							if (s - *newmetric + t - q < sizeof(*newmetric)) {
								switch (rcase) {
									case RETAIN:
										while (q < t)
											*s++ = *q++;
										break;
									case LOWER:
										while (q < t)
											*s++ = (char)tolower(*q++);
										break;
									case UPPER:
										while (q < t)
											*s++ = (char)toupper(*q++);
										break;
								}
							}
						}
						ref = 0;
					}
					if (*p != '\\') { /* \1\2 case */
						escape = 0;
						rcase = RETAIN;
						if (s - *newmetric + 1 < sizeof(*newmetric))
							*s++ = *p;
					}
				}
				break;
		}
		if (*p == '\0')
			break;
	}
	/* undo trailing \0 */
	s--;

	/* insert remaining part */
	q = metric + pmatch[0].rm_eo;
	t = firstspace;
	if (s - *newmetric + t - q < sizeof(*newmetric)) {
		while (q < t)
			*s++ = *q++;
	} else {
		return 0;  /* won't fit, don't try further */
	}

	/* record new position of firstspace */
	*newfirstspace = s;

	/* copy data part */
	if (s - *newmetric + strlen(firstspace) < sizeof(*newmetric))
	{
		for (p = firstspace; *p != '\0'; p++)
			*s++ = *p;
		*s++ = '\0';

		return s - *newmetric;
	}

	return 0;  /* we couldn't copy everything */
}

static char
router_route_intern(
		char *blackholed,
		destination ret[],
		size_t *curlen,
		size_t retsize,
		char *srcaddr,
		char *metric,
		char *firstspace,
		const route *r)
{
	const route *w;
	destinations *d;
	char stop = 0;
	char wassent = 0;
	const char *p;
	const char *q = NULL;  /* pacify compiler, won't happen in reality */
	const char *t;
	char newmetric[METRIC_BUFSIZ];
	char *newfirstspace = NULL;
	size_t len;
	regmatch_t pmatch[RE_MAX_MATCHES];

#define failif(RETLEN, WANTLEN) \
	if (WANTLEN > RETLEN) { \
		logerr("router_route: out of destination slots, " \
				"increase CONN_DESTS_SIZE in router.h\n"); \
		return 1; \
	}


	for (w = r; w != NULL; w = w->next) {
		if (w->dests->cl->type == GROUP) {
			/* strrstr doesn't exist, grrr
			 * therefore the pattern in the group is stored in reverse,
			 * such that we can start matching the tail easily without
			 * having to calculate the end of the pattern string all the
			 * time */
			for (p = firstspace - 1; p >= metric; p--) {
				for (q = w->dests->cl->name, t = p;
						*q != '\0' && t >= metric;
						q++, t--)
				{
					if (*q != *t)
						break;
				}
				if (*q == '\0')
					break;
			}
			/* indirection */
			assert(q != NULL);
			if (*q == '\0')
				stop = router_route_intern(
						blackholed,
						ret,
						curlen,
						retsize,
						srcaddr,
						metric,
						firstspace,
						w->dests->cl->members.routes);
		} else if (router_metric_matches(w, metric, firstspace, pmatch)) {
			stop = w->stop;
			/* rule matches, send to destination(s) */
			for (d = w->dests; d != NULL; d = d->next) {
				switch (d->cl->type) {
					case BLACKHOLE:
						*blackholed = 1;
						break;
					case FILELOGIP: {
						servers *s;
						snprintf(newmetric, sizeof(newmetric), "%s %s",
								srcaddr, metric);
						for (s = d->cl->members.forward; s != NULL; s = s->next)
						{
							failif(retsize, *curlen + 1);
							ret[*curlen].dest = s->server;
							ret[(*curlen)++].metric = strdup(newmetric);
						}
						wassent = 1;
					}	break;
					case FILELOG:
					case FORWARD: {
						/* simple case, no logic necessary */
						servers *s;
						for (s = d->cl->members.forward; s != NULL; s = s->next)
						{
							failif(retsize, *curlen + 1);
							ret[*curlen].dest = s->server;
							ret[(*curlen)++].metric = strdup(metric);
						}
						wassent = 1;
					}	break;
					case ANYOF: {
						/* we queue the same metrics at the same server */
						unsigned int hash;
						
						fnv1a_32(hash, p, metric, firstspace);

						/* We could use the retry approach here, but since
						 * our c is very small compared to MAX_INT, the bias
						 * we introduce for the last few of the range
						 * (MAX_INT % c) can be considered neglicible given
						 * the number of occurances of c in the range of
						 * MAX_INT, therefore we stick with a simple mod. */
						hash %= d->cl->members.anyof->count;
						failif(retsize, *curlen + 1);
						ret[*curlen].dest =
							d->cl->members.anyof->servers[hash];
						ret[(*curlen)++].metric = strdup(metric);
						wassent = 1;
					}	break;
					case FAILOVER: {
						/* queue at the first non-failing server */
						unsigned short i;

						failif(retsize, *curlen + 1);
						ret[*curlen].dest = NULL;
						for (i = 0; i < d->cl->members.anyof->count; i++) {
							server *s = d->cl->members.anyof->servers[i];
							if (server_failed(s))
								continue;
							ret[*curlen].dest = s;
							break;
						}
						if (ret[*curlen].dest == NULL)
							/* all failed, take first server */
							ret[*curlen].dest =
								d->cl->members.anyof->servers[0];
						ret[(*curlen)++].metric = strdup(metric);
						wassent = 1;
					}	break;
					case CARBON_CH:
					case FNV1A_CH:
					case JUMP_CH: {
						/* let the ring(bearer) decide */
						failif(retsize,
								*curlen + d->cl->members.ch->repl_factor);
						ch_get_nodes(
								&ret[*curlen],
								d->cl->members.ch->ring,
								d->cl->members.ch->repl_factor,
								metric,
								firstspace);
						*curlen += d->cl->members.ch->repl_factor;
						wassent = 1;
					}	break;
					case AGGREGATION: {
						/* aggregation rule */
						aggregator_putmetric(
								d->cl->members.aggregation,
								metric,
								firstspace,
								w->nmatch, pmatch);
						wassent = 1;
						/* we need to break out of the inner loop. since
						 * the rest of dests are meant for the stub, and
						 * we should certainly not process it now */
						while (d->next != NULL)
							d = d->next;
					}	break;
					case REWRITE: {
						/* rewrite metric name */
						if ((len = router_rewrite_metric(
									&newmetric, &newfirstspace,
									metric, firstspace,
									d->cl->members.replacement,
									w->nmatch, pmatch)) == 0)
						{
							logerr("router_route: failed to rewrite "
									"metric: newmetric size too small to hold "
									"replacement (%s -> %s)\n",
									metric, d->cl->members.replacement);
							break;
						};

						/* scary! write back the rewritten metric */
						memcpy(metric, newmetric, len);
						firstspace = metric + (newfirstspace - newmetric);
					}	break;
					case AGGRSTUB:
					case STATSTUB: {
						/* strip off the stub pattern, and reroute this
						 * thing */
						router_route_intern(
								blackholed,
								ret,
								curlen,
								retsize,
								srcaddr,
								metric + strlen(w->pattern),
								firstspace + strlen(w->pattern),
								w->dests->cl->members.routes);
					}	break;
					case VALIDATION: {
						/* test whether data matches, if not, either log
						 * or drop and stop */
						char *lastchr = firstspace + strlen(firstspace) - 1;
						if (router_metric_matches(
									w->dests->cl->members.validation->rule,
									firstspace + 1,
									lastchr,
									pmatch))
							break;

						if (w->dests->cl->members.validation->action == VAL_LOG)
						{
							logerr("dropping metric due to validation error: "
									"%s", metric);
							wassent = 1;
						}

						/* only stop if this is a validate without
						 * destinations */
						stop |= d->next == NULL;
						/* break out of the dests loop */
						while (d->next != NULL)
							d = d->next;
						break;
					}
					case GROUP: {
						/* this should not happen */
					}	break;
				}
			}

			/* stop processing further rules if requested */
			if (stop)
				break;
		}
	}
	if (!wassent)
		*blackholed = 1;

	return stop;
}

/**
 * Looks up the locations the given metric_path should be sent to, and
 * returns the list of servers in ret, the number of servers is
 * returned in retcnt.
 * Returns whether the metric was blackholed (e.g. not routed anywhere).
 */
inline char
router_route(
		router *rtr,
		destination ret[],
		size_t *retcnt,
		size_t retsize,
		char *srcaddr,
		char *metric,
		char *firstspace)
{
	size_t curlen = 0;
	char blackholed = 0;

	(void)router_route_intern(&blackholed, ret, &curlen, retsize, srcaddr,
			metric, firstspace, rtr->routes);

	*retcnt = curlen;
	return blackholed;
}

/**
 * Prints for metric_path which rules and/or aggregations would be
 * triggered.  Useful for testing regular expressions.
 */
static char
router_test_intern(char *metric, char *firstspace, route *routes)
{
	route *w;
	destinations *d;
	char stop = 0;
	char gotmatch = 0;
	char newmetric[METRIC_BUFSIZ];
	char *newfirstspace = NULL;
	size_t len;
	regmatch_t pmatch[RE_MAX_MATCHES];

	for (w = routes; w != NULL; w = w->next) {
		if (w->dests->cl->type == GROUP) {
			/* just recurse, in test mode performance shouldn't be an
			 * issue at all */
			gotmatch |= router_test_intern(
					metric,
					firstspace,
					w->dests->cl->members.routes);
			if (gotmatch & 2)
				break;
		} else if (router_metric_matches(w, metric, firstspace, pmatch)) {
			gotmatch = 1;
			switch (w->dests->cl->type) {
				case AGGREGATION:
					fprintf(stdout, "aggregation\n");
					break;
				case REWRITE:
					fprintf(stdout, "rewrite\n");
					break;
				case AGGRSTUB:
				case STATSTUB: {
					gotmatch |= router_test_intern(
							metric + strlen(w->pattern),
							firstspace + strlen(w->pattern),
							w->dests->cl->members.routes);
					return gotmatch;
				}	break;
				default:
					fprintf(stdout, "match\n");
					break;
			}
			*firstspace = '\0';
			switch (w->matchtype) {
				case MATCHALL:
					fprintf(stdout, "    * -> %s\n", metric);
					break;
				case REGEX:
					fprintf(stdout, "    %s (regex) -> %s\n",
							w->pattern, metric);
					break;
				default: {
					char *x;
					switch (w->matchtype) {
						case CONTAINS:
							x = "strstr";
							break;
						case STARTS_WITH:
							x = "strncmp";
							break;
						case ENDS_WITH:
							x = "tailcmp";
							break;
						case MATCHES:
							x = "strcmp";
							break;
						default:
							x = "!impossible?";
							break;
					}
					fprintf(stdout, "    %s [%s: %s]\n    -> %s\n",
							w->pattern, x, w->strmatch, metric);
				}	break;
			}
			*firstspace = ' ';
			stop = w->stop;
			for (d = w->dests; d != NULL; d = d->next) {
				switch (d->cl->type) {
					case AGGREGATION: {
						struct _aggr_computes *ac;
						int stublen = 0;
						char percentile[16];

						if (mode & MODE_DEBUG || d->next == NULL) {
							stublen = 0;
						} else {
							char x;
							stublen = snprintf(&x, 1,
									STUB_AGGR "%p__",
									d->cl->members.aggregation);
						}

						for (ac = d->cl->members.aggregation->computes; ac != NULL; ac = ac->next)
						{
							if (w->nmatch == 0 || (len = router_rewrite_metric(
											&newmetric, &newfirstspace,
											metric, firstspace,
											ac->metric,
											w->nmatch, pmatch)) == 0)
							{
								if (w->nmatch > 0) {
									fprintf(stderr, "router_test: failed to "
											"rewrite metric: newmetric size too "
											"small to hold replacement "
											"(%s -> %s)\n",
											metric, ac->metric);
									break;
								}
								len = snprintf(newmetric, sizeof(newmetric),
										"%s", ac->metric);
								if (len >= sizeof(newmetric))
										len = sizeof(newmetric) - 1;
								newfirstspace = newmetric + len;
							}

							snprintf(percentile, sizeof(percentile),
									"percentile%d", ac->percentile);
							fprintf(stdout, "    %s%s%s%s -> %s\n",
									ac->type == SUM ? "sum" : ac->type == CNT ? "count" :
									ac->type == MAX ? "max" : ac->type == MIN ? "min" :
									ac->type == AVG ? "average" :
									ac->type == MEDN ? "median" :
									ac->type == PCTL ? percentile :
									ac->type == VAR ? "variance" :
									ac->type == SDEV ? "stddev" : "<unknown>",
									w->nmatch > 0 ? "(" : "",
									w->nmatch > 0 ? ac->metric + stublen : "",
									w->nmatch > 0 ? ")" : "",
									newmetric + stublen);

							if (mode & MODE_DEBUG && d->next != NULL) {
								gotmatch |= router_test_intern(
										newmetric,
										newfirstspace,
										routes);
							}
						}
						if (mode & MODE_DEBUG) {
							return gotmatch;
						} else {
							gotmatch |= 4;
						}
					}	break;
					case BLACKHOLE: {
						fprintf(stdout, "    blackholed\n");
					}	break;
					case REWRITE: {
						/* rewrite metric name */
						if ((len = router_rewrite_metric(
									&newmetric, &newfirstspace,
									metric, firstspace,
									d->cl->members.replacement,
									w->nmatch, pmatch)) == 0)
						{
							fprintf(stderr, "router_test: failed to rewrite "
									"metric: newmetric size too small to hold "
									"replacement (%s -> %s)\n",
									metric, d->cl->members.replacement);
							break;
						};

						/* scary! write back the rewritten metric */
						memcpy(metric, newmetric, len);
						firstspace = metric + (newfirstspace - newmetric);
						*firstspace = '\0';
						fprintf(stdout, "    into(%s) -> %s\n",
								d->cl->members.replacement, metric);
						*firstspace = ' ';
					}	break;
					case FORWARD: {
						servers *s;

						fprintf(stdout, "    forward(%s)\n", d->cl->name);
						for (s = d->cl->members.forward; s != NULL; s = s->next)
							fprintf(stdout, "        %s:%d\n",
									server_ip(s->server), server_port(s->server));
					}	break;
					case CARBON_CH:
					case FNV1A_CH:
					case JUMP_CH: {
						destination dst[CONN_DESTS_SIZE];
						int i;

						fprintf(stdout, "    %s_ch(%s)\n",
								d->cl->type == CARBON_CH ? "carbon" :
								d->cl->type == FNV1A_CH ? "fnv1a" :
								"jump_fnv1a", d->cl->name);
						if (gotmatch & 4)
							break;
						if (mode & MODE_DEBUG) {
							fprintf(stdout, "        hash_pos(%d)\n",
									ch_gethashpos(d->cl->members.ch->ring,
										metric, firstspace));
						}
						ch_get_nodes(dst,
								d->cl->members.ch->ring,
								d->cl->members.ch->repl_factor,
								metric,
								firstspace);
						for (i = 0; i < d->cl->members.ch->repl_factor; i++) {
							fprintf(stdout, "        %s:%d\n",
									server_ip(dst[i].dest),
									server_port(dst[i].dest));
							free((char *)dst[i].metric);
						}
					}	break;
					case FAILOVER:
					case ANYOF: {
						unsigned int hash;

						fprintf(stdout, "    %s(%s)\n",
								d->cl->type == ANYOF ? "any_of" : "failover",
								d->cl->name);
						if (gotmatch & 4)
							break;
						if (d->cl->type == ANYOF) {
							const char *p;
							fnv1a_32(hash, p, metric, firstspace);
							hash %= d->cl->members.anyof->count;
						} else {
							hash = 0;
						}
						fprintf(stdout, "        %s:%d\n",
								server_ip(d->cl->members.anyof->servers[hash]),
								server_port(d->cl->members.anyof->servers[hash]));
					}	break;
					case VALIDATION: {
						char *lastspc = firstspace + strlen(firstspace);
						fprintf(stdout, "    validate\n        %s -> %s\n",
								d->cl->members.validation->rule->pattern,
								firstspace + 1);
						
						if (router_metric_matches(
									d->cl->members.validation->rule,
									firstspace + 1,
									lastspc,
									pmatch))
						{
							fprintf(stdout, "        match\n");
						} else {
							fprintf(stdout, "        fail -> %s\n",
									d->cl->members.validation->action == VAL_LOG ? "log" : "drop");
							stop |= d->next == NULL;
							while (d->next != NULL)
								d = d->next;
						}
					}	break;
					default: {
						fprintf(stdout, "    cluster(%s)\n", d->cl->name);
					}	break;
				}
			}
			if (stop) {
				gotmatch = 3;
				fprintf(stdout, "    stop\n");
				break;
			}
		}
	}

	return gotmatch;
}

void
router_test(router *rtr, char *metric)
{
	char *firstspace;

	for (firstspace = metric; *firstspace != '\0'; firstspace++)
		if (*firstspace == ' ')
			break;
	if (!router_test_intern(metric, firstspace, rtr->routes)) {
		*firstspace = '\0';
		fprintf(stdout, "nothing matched %s\n", metric);
	}
	fflush(stdout);
}