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
|
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <urcu.h>
#include "updates/xfr-in.h"
#include "nameserver/name-server.h"
#include "util/wire.h"
#include "util/debug.h"
// #include "knot/zone/zone-dump.h"
// #include "knot/zone/zone-load.h"
#include "packet/packet.h"
#include "dname.h"
#include "zone/zone.h"
#include "packet/query.h"
#include "packet/response.h"
#include "util/error.h"
#include "updates/changesets.h"
#include "tsig.h"
#include "tsig-op.h"
/*----------------------------------------------------------------------------*/
/* Non-API functions */
/*----------------------------------------------------------------------------*/
static int xfrin_create_query(knot_dname_t *qname, uint16_t qtype,
uint16_t qclass, knot_ns_xfr_t *xfr, size_t *size,
const knot_rrset_t *soa, int use_tsig)
{
knot_packet_t *pkt = knot_packet_new(KNOT_PACKET_PREALLOC_QUERY);
CHECK_ALLOC_LOG(pkt, KNOT_ENOMEM);
/*! \todo Get rid of the numeric constant. */
int rc = knot_packet_set_max_size(pkt, 512);
if (rc != KNOT_EOK) {
knot_packet_free(&pkt);
return KNOT_ERROR;
}
rc = knot_query_init(pkt);
if (rc != KNOT_EOK) {
knot_packet_free(&pkt);
return KNOT_ERROR;
}
knot_question_t question;
/* Retain qname until the question is freed. */
knot_dname_retain(qname);
/* Set random query ID. */
knot_packet_set_random_id(pkt);
knot_wire_set_id(pkt->wireformat, pkt->header.id);
// this is ugly!!
question.qname = (knot_dname_t *)qname;
question.qtype = qtype;
question.qclass = qclass;
rc = knot_query_set_question(pkt, &question);
if (rc != KNOT_EOK) {
knot_dname_release(question.qname);
knot_packet_free(&pkt);
return KNOT_ERROR;
}
/* Reserve space for TSIG. */
if (use_tsig && xfr->tsig_key) {
dbg_xfrin_detail("xfrin: setting packet TSIG size to %zu\n",
xfr->tsig_size);
knot_packet_set_tsig_size(pkt, xfr->tsig_size);
}
/* Add SOA RR to authority section for IXFR. */
if (qtype == KNOT_RRTYPE_IXFR && soa) {
knot_query_add_rrset_authority(pkt, soa);
}
/*! \todo OPT RR ?? */
uint8_t *wire = NULL;
size_t wire_size = 0;
rc = knot_packet_to_wire(pkt, &wire, &wire_size);
if (rc != KNOT_EOK) {
dbg_xfrin("Failed to write packet to wire.\n");
knot_dname_release(question.qname);
knot_packet_free(&pkt);
return KNOT_ERROR;
}
if (wire_size > *size) {
dbg_xfrin("Not enough space provided for the wire "
"format of the query.\n");
knot_packet_free(&pkt);
return KNOT_ESPACE;
}
// wire format created, sign it with TSIG if required
if (use_tsig && xfr->tsig_key) {
char *name = knot_dname_to_str(xfr->tsig_key->name);
dbg_xfrin_detail("Signing XFR query with key (name %s): \n",
name);
free(name);
dbg_xfrin_hex_detail(xfr->tsig_key->secret,
xfr->tsig_key->secret_size);
xfr->digest_size = xfr->digest_max_size;
rc = knot_tsig_sign(wire, &wire_size, *size, NULL, 0,
xfr->digest, &xfr->digest_size, xfr->tsig_key);
if (rc != KNOT_EOK) {
/*! \todo [TSIG] Handle TSIG errors. */
knot_packet_free(&pkt);
return rc;
}
dbg_xfrin_detail("Signed XFR query, new wire size: %zu, digest:"
"\n", wire_size);
dbg_xfrin_hex_detail((const char*)xfr->digest, xfr->digest_size);
}
memcpy(xfr->wire, wire, wire_size);
*size = wire_size;
dbg_xfrin("Created query of size %zu.\n", *size);
knot_packet_dump(pkt);
knot_packet_free(&pkt);
/* Release qname. */
knot_dname_release(question.qname);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
int xfrin_create_soa_query(knot_dname_t *owner, knot_ns_xfr_t *xfr,
size_t *size)
{
/*! \todo [TSIG] Should TSIG apply for SOA query too? */
return xfrin_create_query(owner, KNOT_RRTYPE_SOA,
KNOT_CLASS_IN, xfr, size, 0, 0);
}
/*----------------------------------------------------------------------------*/
int xfrin_transfer_needed(const knot_zone_contents_t *zone,
knot_packet_t *soa_response)
{
// first, parse the rest of the packet
assert(!knot_packet_is_query(soa_response));
dbg_xfrin("Response - parsed: %zu, total wire size: %zu\n",
soa_response->parsed, soa_response->size);
int ret;
if (soa_response->parsed < soa_response->size) {
ret = knot_packet_parse_rest(soa_response);
if (ret != KNOT_EOK) {
return KNOT_EMALF;
}
}
/*
* Retrieve the local Serial
*/
const knot_rrset_t *soa_rrset =
knot_node_rrset(knot_zone_contents_apex(zone),
KNOT_RRTYPE_SOA);
if (soa_rrset == NULL) {
char *name = knot_dname_to_str(knot_node_owner(
knot_zone_contents_apex(zone)));
dbg_xfrin("SOA RRSet missing in the zone %s!\n", name);
free(name);
return KNOT_ERROR;
}
int64_t local_serial = knot_rdata_soa_serial(
knot_rrset_rdata(soa_rrset));
if (local_serial < 0) {
dbg_xfrin_exec(
char *name = knot_dname_to_str(knot_rrset_owner(soa_rrset));
dbg_xfrin("Malformed data in SOA of zone %s\n", name);
free(name);
);
return KNOT_EMALF; // maybe some other error
}
/*
* Retrieve the remote Serial
*/
// the SOA should be the first (and only) RRSet in the response
soa_rrset = knot_packet_answer_rrset(soa_response, 0);
if (soa_rrset == NULL
|| knot_rrset_type(soa_rrset) != KNOT_RRTYPE_SOA) {
return KNOT_EMALF;
}
int64_t remote_serial = knot_rdata_soa_serial(
knot_rrset_rdata(soa_rrset));
if (remote_serial < 0) {
return KNOT_EMALF; // maybe some other error
}
return (ns_serial_compare(local_serial, remote_serial) < 0);
}
/*----------------------------------------------------------------------------*/
int xfrin_create_axfr_query(knot_dname_t *owner, knot_ns_xfr_t *xfr,
size_t *size, int use_tsig)
{
return xfrin_create_query(owner, KNOT_RRTYPE_AXFR,
KNOT_CLASS_IN, xfr, size, 0, use_tsig);
}
/*----------------------------------------------------------------------------*/
int xfrin_create_ixfr_query(const knot_zone_contents_t *zone,
knot_ns_xfr_t *xfr, size_t *size, int use_tsig)
{
/*!
* \todo Implement properly.
*/
knot_node_t *apex = knot_zone_contents_get_apex(zone);
const knot_rrset_t *soa = knot_node_rrset(apex, KNOT_RRTYPE_SOA);
return xfrin_create_query(knot_node_get_owner(apex), KNOT_RRTYPE_IXFR,
KNOT_CLASS_IN, xfr, size, soa, use_tsig);
}
/*----------------------------------------------------------------------------*/
static int xfrin_add_orphan_rrsig(xfrin_orphan_rrsig_t *rrsigs,
knot_rrset_t *rr)
{
// try to find similar RRSIGs (check owner and type covered) in the list
assert(knot_rrset_type(rr) == KNOT_RRTYPE_RRSIG);
int ret = 0;
xfrin_orphan_rrsig_t **last = &rrsigs;
while (*last != NULL) {
// check if the RRSIG is not similar to the one we want to add
assert((*last)->rrsig != NULL);
if (knot_rrset_compare((*last)->rrsig, rr,
KNOT_RRSET_COMPARE_HEADER) == 1
&& knot_rdata_rrsig_type_covered(knot_rrset_rdata(
(*last)->rrsig))
== knot_rdata_rrsig_type_covered(knot_rrset_rdata(rr))) {
ret = knot_rrset_merge((void **)&(*last)->rrsig,
(void **)&rr);
if (ret != KNOT_EOK) {
return ret;
} else {
return 1;
}
}
last = &((*last)->next);
}
assert(*last == NULL);
// we did not find the right RRSIGs, add to the end
*last = (xfrin_orphan_rrsig_t *)malloc(sizeof(xfrin_orphan_rrsig_t));
CHECK_ALLOC_LOG(*last, KNOT_ENOMEM);
(*last)->rrsig = rr;
(*last)->next = NULL;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_process_orphan_rrsigs(knot_zone_contents_t *zone,
xfrin_orphan_rrsig_t *rrsigs)
{
xfrin_orphan_rrsig_t **last = &rrsigs;
int ret = 0;
while (*last != NULL) {
knot_rrset_t *rrset = NULL;
knot_node_t *node = NULL;
ret = knot_zone_contents_add_rrsigs(zone, (*last)->rrsig,
&rrset, &node,
KNOT_RRSET_DUPL_MERGE, 1);
if (ret > 0) {
knot_rrset_free(&(*last)->rrsig);
} else if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add orphan RRSIG to zone.\n");
return ret;
} else {
(*last)->rrsig = NULL;
}
last = &((*last)->next);
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static void xfrin_free_orphan_rrsigs(xfrin_orphan_rrsig_t **rrsigs)
{
xfrin_orphan_rrsig_t *r = *rrsigs;
while (r != NULL) {
xfrin_orphan_rrsig_t *prev = r;
r = r->next;
free(prev);
}
*rrsigs = NULL;
}
/*----------------------------------------------------------------------------*/
/*! \note [TSIG] */
static int xfrin_check_tsig(knot_packet_t *packet, knot_ns_xfr_t *xfr,
int tsig_req)
{
assert(packet != NULL);
assert(xfr != NULL);
dbg_xfrin_verb("xfrin_check_tsig(): packet nr: %d, required: %d\n",
xfr->packet_nr, tsig_req);
/*
* If we are expecting it (i.e. xfr->prev_digest_size > 0)
* a) it should be there (first, last or each 100th packet) and it
* is not
* Then we should discard the changes and close the connection.
* b) it should be there and it is or it may not be there (other
* packets) and it is
* We validate the TSIG and reset packet number counting and
* data aggregation.
*
* If we are not expecting it (i.e. xfr->prev_digest_size <= 0) and
* it is there => it should probably be considered an error
*/
knot_rrset_t *tsig = NULL;
int ret = knot_packet_parse_next_rr_additional(packet, &tsig);
if (ret != KNOT_EOK) {
return ret;
}
if (xfr->tsig_key) {
if (tsig_req && tsig == NULL) {
// TSIG missing!!
return KNOT_EMALF;
} else if (tsig != NULL) {
// TSIG there, either required or not, process
if (xfr->packet_nr == 0) {
ret = knot_tsig_client_check(tsig,
xfr->wire, xfr->wire_size,
xfr->digest, xfr->digest_size,
xfr->tsig_key);
} else {
ret = knot_tsig_client_check_next(tsig,
xfr->wire, xfr->wire_size,
xfr->digest, xfr->digest_size,
xfr->tsig_key);
}
if (ret != KNOT_EOK) {
/*! \note [TSIG] No need to check TSIG error
* here, propagate and check elsewhere.*/
return ret;
}
// and reset the data storage
//xfr->packet_nr = 1;
xfr->tsig_data_size = 0;
// Extract the digest from the TSIG RDATA and store it.
if (xfr->digest_max_size < tsig_rdata_mac_length(tsig)) {
return KNOT_ESPACE;
}
memcpy(xfr->digest, tsig_rdata_mac(tsig),
tsig_rdata_mac_length(tsig));
xfr->digest_size = tsig_rdata_mac_length(tsig);
} else { // TSIG not required and not there
// just append the wireformat to the TSIG data
assert(KNOT_NS_TSIG_DATA_MAX_SIZE - xfr->tsig_data_size
>= xfr->wire_size);
memcpy(xfr->tsig_data + xfr->tsig_data_size,
xfr->wire, xfr->wire_size);
xfr->tsig_data_size += xfr->wire_size;
}
} else if (tsig != NULL) {
// TSIG where it should not be
return KNOT_EMALF;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int xfrin_process_axfr_packet(/*const uint8_t *pkt, size_t size,
xfrin_constructed_zone_t **constr*/
knot_ns_xfr_t *xfr)
{
const uint8_t *pkt = xfr->wire;
size_t size = xfr->wire_size;
xfrin_constructed_zone_t **constr =
(xfrin_constructed_zone_t **)(&xfr->data);
if (pkt == NULL || constr == NULL) {
dbg_xfrin("Wrong parameters supported.\n");
return KNOT_EBADARG;
}
dbg_xfrin("Processing AXFR packet of size %zu.\n", size);
// check if the response is OK
if (knot_wire_get_rcode(pkt) != KNOT_RCODE_NOERROR) {
return KNOT_EXFRREFUSED;
}
/*! \todo Should TC bit be checked? */
knot_packet_t *packet =
knot_packet_new(KNOT_PACKET_PREALLOC_NONE);
if (packet == NULL) {
dbg_xfrin("Could not create packet structure.\n");
return KNOT_ENOMEM;
}
int ret = knot_packet_parse_from_wire(packet, pkt, size, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Could not parse packet: %s.\n",
knot_strerror(ret));
knot_packet_free(&packet);
/*! \todo Cleanup. */
return KNOT_EMALF;
}
/*! \todo [TSIG] If packet RCODE is NOTAUTH(9), process as TSIG error. */
knot_rrset_t *rr = NULL;
ret = knot_packet_parse_next_rr_answer(packet, &rr);
if (ret != KNOT_EOK) {
dbg_xfrin("Could not parse first Answer RR: %s.\n",
knot_strerror(ret));
knot_packet_free(&packet);
/*! \todo Cleanup. */
return KNOT_EMALF;
}
if (rr == NULL) {
dbg_xfrin("No RRs in the packet.\n");
knot_packet_free(&packet);
/*! \todo Cleanup. */
return KNOT_EMALF;
}
/*! \todo We should probably test whether the Question of the first
* message corresponds to the SOA RR.
*/
knot_node_t *node = NULL;
int in_zone = 0;
knot_zone_contents_t *zone = NULL;
if (*constr == NULL) {
// this should be the first packet
/*! \note [TSIG] Packet number for checking TSIG validation. */
xfr->packet_nr = 0;
/*! \note [TSIG] Storing total size of data for TSIG digest. */
xfr->tsig_data_size = 0;
// create new zone
/*! \todo Ensure that the packet is the first one. */
if (knot_rrset_type(rr) != KNOT_RRTYPE_SOA) {
dbg_xfrin("No zone created, but the first RR in "
"Answer is not a SOA RR.\n");
knot_packet_free(&packet);
knot_node_free(&node, 0, 0);
knot_rrset_deep_free(&rr, 1, 1, 1);
/*! \todo Cleanup. */
return KNOT_EMALF;
}
if (knot_dname_compare(knot_rrset_owner(rr),
knot_packet_qname(packet)) != 0) {
dbg_xfrin_exec(
char *rr_owner =
knot_dname_to_str(knot_rrset_owner(rr));
char *qname = knot_dname_to_str(
knot_packet_qname(packet));
dbg_xfrin("Owner of the first SOA RR (%s) does not"
" match QNAME (%s).\n", rr_owner, qname);
free(rr_owner);
free(qname);
);
/*! \todo Cleanup. */
knot_packet_free(&packet);
knot_node_free(&node, 0, 0);
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_EMALF;
}
node = knot_node_new(rr->owner, NULL, 0);
if (node == NULL) {
dbg_xfrin("Failed to create new node.\n");
knot_packet_free(&packet);
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_ENOMEM;
}
// the first RR is SOA and its owner and QNAME are the same
// create the zone
*constr = (xfrin_constructed_zone_t *)malloc(
sizeof(xfrin_constructed_zone_t));
if (*constr == NULL) {
dbg_xfrin("Failed to create new constr. zone.\n");
knot_packet_free(&packet);
knot_node_free(&node, 0, 0);
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_ENOMEM;
}
memset(*constr, 0, sizeof(xfrin_constructed_zone_t));
(*constr)->contents = knot_zone_contents_new(node, 0, 1, NULL);
// assert(0);
if ((*constr)->contents== NULL) {
dbg_xfrin("Failed to create new zone.\n");
knot_packet_free(&packet);
knot_node_free(&node, 0, 0);
knot_rrset_deep_free(&rr, 1, 1, 1);
/*! \todo Cleanup. */
return KNOT_ENOMEM;
}
in_zone = 1;
assert(node->owner == rr->owner);
zone = (*constr)->contents;
assert(zone->apex == node);
assert(zone->apex->owner == rr->owner);
// add the RRSet to the node
//ret = knot_node_add_rrset(node, rr, 0);
ret = knot_zone_contents_add_rrset(zone, rr, &node,
KNOT_RRSET_DUPL_MERGE, 1);
if (ret < 0) {
dbg_xfrin("Failed to add RRSet to zone node: %s.\n",
knot_strerror(ret));
knot_packet_free(&packet);
knot_node_free(&node, 0, 0);
knot_rrset_deep_free(&rr, 1, 1, 1);
/*! \todo Cleanup. */
return KNOT_ERROR;
} else if (ret > 0) {
dbg_xfrin("Merged SOA RRSet.\n");
// merged, free the RRSet
//knot_rrset_deep_free(&rr, 1, 0, 0);
knot_rrset_free(&rr);
}
// take next RR
ret = knot_packet_parse_next_rr_answer(packet, &rr);
} else {
zone = (*constr)->contents;
++xfr->packet_nr;
}
/*! \note [TSIG] add the packet wire size to the data to be verified by
* TSIG
*/
if (xfr->tsig_key) {
dbg_xfrin("Adding packet wire to TSIG data (size till now: %zu,"
" adding: %zu).\n", xfr->tsig_data_size,
xfr->wire_size);
assert(KNOT_NS_TSIG_DATA_MAX_SIZE - xfr->tsig_data_size
>= xfr->wire_size);
memcpy(xfr->tsig_data + xfr->tsig_data_size, xfr->wire,
xfr->wire_size);
xfr->tsig_data_size += xfr->wire_size;
}
assert(zone != NULL);
while (ret == KNOT_EOK && rr != NULL) {
// process the parsed RR
dbg_xfrin("\nNext RR:\n\n");
knot_rrset_dump(rr, 0);
if (node != NULL
&& knot_dname_compare(rr->owner, node->owner) != 0) {
dbg_xfrin_exec(
char *name = knot_dname_to_str(node->owner);
dbg_xfrin("Node owner: %s\n", name);
free(name);
);
if (!in_zone) {
// this should not happen
assert(0);
// the node is not in the zone and the RR has
// other owner, so a new node must be created
// insert the old node to the zone
}
node = NULL;
}
if (knot_rrset_type(rr) == KNOT_RRTYPE_SOA) {
// this must be the last SOA, do not do anything more
// discard the RR
assert(knot_zone_contents_apex((zone)) != NULL);
assert(knot_node_rrset(knot_zone_contents_apex((zone)),
KNOT_RRTYPE_SOA) != NULL);
dbg_xfrin("Found last SOA, transfer finished.\n");
dbg_xfrin("Verifying TSIG...\n");
/*! \note [TSIG] Now check if there is not a TSIG record
* at the end of the packet.
*/
ret = xfrin_check_tsig(packet, xfr, 1);
dbg_xfrin_detail("xfrin_check_tsig() returned %d\n",
ret);
knot_packet_free(&packet);
knot_rrset_deep_free(&rr, 1, 1, 1);
if (ret != KNOT_EOK) {
/*! \todo [TSIG] Handle TSIG errors. */
return ret;
}
// we must now find place for all orphan RRSIGs
ret = xfrin_process_orphan_rrsigs(zone,
(*constr)->rrsigs);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to process orphan "
"RRSIGs\n");
/*! \todo Cleanup?? */
return ret;
}
xfrin_free_orphan_rrsigs(&(*constr)->rrsigs);
return 1;
}
if (knot_rrset_type(rr) == KNOT_RRTYPE_RRSIG) {
// RRSIGs require special handling, as there are no
// nodes for them
knot_rrset_t *tmp_rrset = NULL;
ret = knot_zone_contents_add_rrsigs(zone, rr,
&tmp_rrset, &node, KNOT_RRSET_DUPL_MERGE, 1);
if (ret == KNOT_ENONODE || ret == KNOT_ENORRSET) {
dbg_xfrin("No node or RRSet for RRSIGs\n");
dbg_xfrin("Saving for later insertion.\n");
ret = xfrin_add_orphan_rrsig((*constr)->rrsigs,
rr);
if (ret > 0) {
dbg_xfrin("Merged RRSIGs.\n");
knot_rrset_free(&rr);
} else if (ret != KNOT_EOK) {
dbg_xfrin("Failed to save orphan"
" RRSIGs.\n");
knot_packet_free(&packet);
knot_node_free(&node, 1, 0); // ???
knot_rrset_deep_free(&rr, 1, 1, 1);
return ret;
}
} else if (ret < 0) {
dbg_xfrin("Failed to add RRSIGs (%s).\n",
knot_strerror(ret));
knot_packet_free(&packet);
knot_node_free(&node, 1, 0); // ???
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_ERROR; /*! \todo Other error code. */
} else if (ret == 1) {
assert(node != NULL);
dbg_xfrin_exec(
char *name = knot_dname_to_str(node->owner);
dbg_xfrin("Found node for the record in "
"zone: %s.\n", name);
free(name);
);
in_zone = 1;
knot_rrset_deep_free(&rr, 1, 0, 0);
} else if (ret == 2) {
// should not happen
assert(0);
// knot_rrset_deep_free(&rr, 1, 1, 1);
} else {
assert(node != NULL);
dbg_xfrin_exec(
char *name = knot_dname_to_str(node->owner);
dbg_xfrin("Found node for the record in "
"zone: %s.\n", name);
free(name);
);
in_zone = 1;
assert(tmp_rrset->rrsigs == rr);
}
// parse next RR
ret = knot_packet_parse_next_rr_answer(packet, &rr);
continue;
}
/*! \note [TSIG] TSIG where it should not be - in Answer section.*/
if (knot_rrset_type(rr) == KNOT_RRTYPE_TSIG) {
// not allowed here
dbg_xfrin(" in Answer section.\n");
knot_packet_free(&packet);
knot_node_free(&node, 1, 0); // ???
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_EMALF;
}
knot_node_t *(*get_node)(const knot_zone_contents_t *,
const knot_dname_t *) = NULL;
int (*add_node)(knot_zone_contents_t *, knot_node_t *, int,
uint8_t, int) = NULL;
if (knot_rrset_type(rr) == KNOT_RRTYPE_NSEC3) {
get_node = knot_zone_contents_get_nsec3_node;
add_node = knot_zone_contents_add_nsec3_node;
} else {
get_node = knot_zone_contents_get_node;
add_node = knot_zone_contents_add_node;
}
if (node == NULL && (node = get_node(zone,
knot_rrset_owner(rr))) != NULL) {
// the node for this RR was found in the zone
dbg_xfrin("Found node for the record in zone.\n");
in_zone = 1;
}
if (node == NULL) {
// a new node for the RR is required but it is not
// in the zone
node = knot_node_new(rr->owner, NULL, 0);
if (node == NULL) {
dbg_xfrin("Failed to create new node.\n");
knot_packet_free(&packet);
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_ENOMEM;
}
dbg_xfrin("Created new node for the record.\n");
// insert the RRSet to the node
ret = knot_node_add_rrset(node, rr, 1);
if (ret < 0) {
dbg_xfrin("Failed to add RRSet to node (%s"
")\n", knot_strerror(ret));
knot_packet_free(&packet);
knot_node_free(&node, 1, 0); // ???
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_ERROR;
} else if (ret > 0) {
// should not happen, this is new node
assert(0);
// knot_rrset_deep_free(&rr, 1, 0, 0);
}
// insert the node into the zone
ret = add_node(zone, node, 1, 0, 1);
assert(node != NULL);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add node to zone (%s)"
".\n", knot_strerror(ret));
knot_packet_free(&packet);
knot_node_free(&node, 1, 0); // ???
knot_rrset_deep_free(&rr, 1, 1, 1);
return KNOT_ERROR;
}
in_zone = 1;
} else {
assert(in_zone);
ret = knot_zone_contents_add_rrset(zone, rr, &node,
KNOT_RRSET_DUPL_MERGE, 1);
if (ret < 0) {
dbg_xfrin("Failed to add RRSet to zone:"
"%s.\n", knot_strerror(ret));
return KNOT_ERROR;
} else if (ret > 0) {
// merged, free the RRSet
// knot_rrset_deep_free(&rr, 1, 0, 0);
knot_rrset_free(&rr);
}
}
rr = NULL;
// parse next RR
ret = knot_packet_parse_next_rr_answer(packet, &rr);
}
assert(ret != KNOT_EOK || rr == NULL);
if (ret < 0) {
// some error in parsing
dbg_xfrin("Could not parse next RR: %s.\n",
knot_strerror(ret));
knot_packet_free(&packet);
knot_node_free(&node, 0, 0);
knot_rrset_deep_free(&rr, 1, 1, 1);
/*! \todo Cleanup. */
return KNOT_EMALF;
}
assert(ret == KNOT_EOK);
assert(rr == NULL);
// if the last node is not yet in the zone, insert
if (!in_zone) {
assert(node != NULL);
ret = knot_zone_contents_add_node(zone, node, 1, 0, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add last node into zone (%s)"
".\n", knot_strerror(ret));
knot_packet_free(&packet);
knot_node_free(&node, 1, 0);
return KNOT_ERROR; /*! \todo Other error */
}
}
/*! \note [TSIG] Now check if there is not a TSIG record at the end of
* the packet.
*/
ret = xfrin_check_tsig(packet, xfr,
knot_ns_tsig_required(xfr->packet_nr));
++xfr->packet_nr;
knot_packet_free(&packet);
dbg_xfrin("Processed one AXFR packet successfully.\n");
/*! \note [TSIG] TSIG errors are propagated and reported in a standard
* manner, as we're in response processing, no further error response
* should be sent.
*/
return ret;
}
/*----------------------------------------------------------------------------*/
static int xfrin_parse_first_rr(knot_packet_t **packet, const uint8_t *pkt,
size_t size, knot_rrset_t **rr)
{
*packet = knot_packet_new(KNOT_PACKET_PREALLOC_NONE);
if (packet == NULL) {
dbg_xfrin("Could not create packet structure.\n");
return KNOT_ENOMEM;
}
int ret = knot_packet_parse_from_wire(*packet, pkt, size, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Could not parse packet: %s.\n",
knot_strerror(ret));
knot_packet_free(packet);
return KNOT_EMALF;
}
// check if the TC bit is set (it must not be)
if (knot_wire_get_tc(pkt)) {
dbg_xfrin("IXFR response has TC bit set.\n");
knot_packet_free(packet);
return KNOT_EMALF;
}
ret = knot_packet_parse_next_rr_answer(*packet, rr);
if (ret != KNOT_EOK) {
dbg_xfrin("Could not parse first Answer RR: %s.\n",
knot_strerror(ret));
knot_packet_free(packet);
return KNOT_EMALF;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int xfrin_process_ixfr_packet(/*const uint8_t *pkt, size_t size,
knot_changesets_t **chs*/knot_ns_xfr_t *xfr)
{
size_t size = xfr->wire_size;
const uint8_t *pkt = xfr->wire;
knot_changesets_t **chs = (knot_changesets_t **)(&xfr->data);
if (pkt == NULL || chs == NULL) {
dbg_xfrin("Wrong parameters supported.\n");
return KNOT_EBADARG;
}
// check if the response is OK
if (knot_wire_get_rcode(pkt) != KNOT_RCODE_NOERROR) {
return KNOT_EXFRREFUSED;
}
knot_packet_t *packet = NULL;
// knot_rrset_t *soa1 = NULL;
// knot_rrset_t *soa2 = NULL;
knot_rrset_t *rr = NULL;
int ret;
if ((ret = xfrin_parse_first_rr(&packet, pkt, size, &rr)) != KNOT_EOK) {
return ret;
}
assert(packet != NULL);
// state of the transfer
// -1 .. a SOA is expected to create a new changeset
int state = 0;
if (rr == NULL) {
dbg_xfrin("No RRs in the packet.\n");
knot_packet_free(&packet);
/*! \todo Some other action??? */
return KNOT_EMALF;
}
if (*chs == NULL) {
dbg_xfrin("Changesets empty, creating new.\n");
ret = knot_changeset_allocate(chs);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&rr, 1, 1, 1);
knot_packet_free(&packet);
return ret;
}
// the first RR must be a SOA
if (knot_rrset_type(rr) != KNOT_RRTYPE_SOA) {
dbg_xfrin("First RR is not a SOA RR!\n");
knot_rrset_deep_free(&rr, 1, 1, 1);
ret = KNOT_EMALF;
goto cleanup;
}
// just store the first SOA for later use
(*chs)->first_soa = rr;
state = -1;
dbg_xfrin("First SOA of IXFR saved, state set to -1.\n");
// parse the next one
ret = knot_packet_parse_next_rr_answer(packet, &rr);
if (ret != KNOT_EOK) {
return ret;
}
/*
* If there is no other records in the response than the SOA, it
* means one of these two cases:
*
* 1) The server does not have newer zone than ours.
* This is indicated by serial equal to the one of our zone.
* 2) The server wants to send the transfer but is unable to fit
* it in the packet. This is indicated by serial different
* (newer) from the one of our zone.
*
* The serials must be compared in other parts of the server, so
* just indicate that the answer contains only one SOA.
*/
if (rr == NULL) {
dbg_xfrin("Response containing only SOA,\n");
knot_packet_free(&packet);
return XFRIN_RES_SOA_ONLY;
} else if (knot_rrset_type(rr) != KNOT_RRTYPE_SOA) {
knot_rrset_deep_free(&rr, 1, 1, 1);
dbg_xfrin("Fallback to AXFR.\n");
ret = XFRIN_RES_FALLBACK;
knot_free_changesets(chs);
xfr->data = 0;
return ret;
}
} else {
if ((*chs)->first_soa == NULL) {
dbg_xfrin("Changesets don't contain frist SOA!\n");
ret = KNOT_EBADARG;
goto cleanup;
}
dbg_xfrin("Changesets present.\n");
}
/*
* Process the next RR. Different requirements are in place in
* different cases:
*
* 1) Last changeset has both soa_from and soa_to.
* a) The next RR is a SOA.
* i) The next RR is equal to the first_soa saved in changesets.
* This denotes the end of the transfer. It may be dropped and
* the end should be signalised by returning positive value.
*
* ii) The next RR is some other SOA.
* This means a start of new changeset - create it and add it
* to the list.
*
* b) The next RR is not a SOA.
* Put the RR into the ADD part of the last changeset as this is
* not finished yet. Continue while SOA is not encountered. Then
* jump to 1-a.
*
* 2) Last changeset has only the soa_from and does not have soa_to.
* a) The next RR is a SOA.
* This means start of the ADD section. Put the SOA to the
* changeset. Continue adding RRs to the ADD section while SOA
* is not encountered. This is identical to 1-b.
*
* b) The next RR is not a SOA.
* This means the REMOVE part is not finished yet. Add the RR to
* the REMOVE part. Continue adding next RRs until a SOA is
* encountered. Then jump to 2-a.
*/
// first, find out in what state we are
/*! \todo It would be more elegant to store the state in the
* changesets structure, or in some place persistent between
* calls to this function.
*/
if (state != -1) {
dbg_xfrin("State is not -1, deciding...\n");
// there should be at least one started changeset right now
if ((*chs)->count <= 0) {
knot_rrset_deep_free(&rr, 1, 1, 1);
ret = KNOT_EMALF;
goto cleanup;
}
// a changeset should be created only when there is a SOA
assert((*chs)->sets[(*chs)->count - 1].soa_from != NULL);
if ((*chs)->sets[(*chs)->count - 1].soa_to == NULL) {
state = XFRIN_CHANGESET_REMOVE;
} else {
state = XFRIN_CHANGESET_ADD;
}
}
dbg_xfrin("State before the loop: %d\n", state);
/*! \todo This may be implemented with much less IFs! */
while (ret == KNOT_EOK && rr != NULL) {
dbg_xfrin_exec(
dbg_xfrin("Next loop, state: %d\n", state);
char *name = knot_dname_to_str(knot_rrset_owner(rr));
dbg_xfrin("Actual RR: %s, type %s.\n", name,
knot_rrtype_to_string(knot_rrset_type(rr)));
free(name);
);
switch (state) {
case -1:
// a SOA is expected
// this may be either a start of a changeset or the
// last SOA (in case the transfer was empty, but that
// is quite weird in fact
if (knot_rrset_type(rr) != KNOT_RRTYPE_SOA) {
dbg_xfrin("First RR is not a SOA RR!\n");
dbg_xfrin("RR type: %s\n",
knot_rrtype_to_string(knot_rrset_type(rr)));
ret = KNOT_EMALF;
knot_rrset_deep_free(&rr, 1, 1, 1);
goto cleanup;
}
if (knot_rdata_soa_serial(knot_rrset_rdata(rr))
== knot_rdata_soa_serial(
knot_rrset_rdata((*chs)->first_soa))) {
/*! \note [TSIG] Check TSIG, we're at the end of
* transfer.
*/
ret = xfrin_check_tsig(packet, xfr, 1);
// last SOA, discard and end
knot_rrset_deep_free(&rr, 1, 1, 1);
knot_packet_free(&packet);
/*! \note [TSIG] If TSIG validates, consider
* transfer complete. */
if (ret == KNOT_EOK) {
ret = XFRIN_RES_COMPLETE;
}
return ret;
} else {
// normal SOA, start new changeset
(*chs)->count++;
if ((ret = knot_changesets_check_size(*chs))
!= KNOT_EOK) {
(*chs)->count--;
knot_rrset_deep_free(&rr, 1, 1, 1);
goto cleanup;
}
ret = knot_changeset_add_soa(
&(*chs)->sets[(*chs)->count - 1], rr,
XFRIN_CHANGESET_REMOVE);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&rr, 1, 1, 1);
goto cleanup;
}
// change state to REMOVE
state = XFRIN_CHANGESET_REMOVE;
}
break;
case XFRIN_CHANGESET_REMOVE:
// if the next RR is SOA, store it and change state to
// ADD
if (knot_rrset_type(rr) == KNOT_RRTYPE_SOA) {
// we should not be here if soa_from is not set
assert((*chs)->sets[(*chs)->count - 1].soa_from
!= NULL);
ret = knot_changeset_add_soa(
&(*chs)->sets[(*chs)->count - 1], rr,
XFRIN_CHANGESET_ADD);
if (ret != KNOT_EOK) {
knot_rrset_deep_free(&rr, 1, 1, 1);
goto cleanup;
}
state = XFRIN_CHANGESET_ADD;
} else {
// just add the RR to the REMOVE part and
// continue
if ((ret = knot_changeset_add_new_rr(
&(*chs)->sets[(*chs)->count - 1], rr,
XFRIN_CHANGESET_REMOVE)) != KNOT_EOK) {
knot_rrset_deep_free(&rr, 1, 1, 1);
goto cleanup;
}
}
break;
case XFRIN_CHANGESET_ADD:
// if the next RR is SOA change to state -1 and do not
// parse next RR
if (knot_rrset_type(rr) == KNOT_RRTYPE_SOA) {
state = -1;
continue;
} else {
// just add the RR to the ADD part and continue
if ((ret = knot_changeset_add_new_rr(
&(*chs)->sets[(*chs)->count - 1], rr,
XFRIN_CHANGESET_ADD)) != KNOT_EOK) {
knot_rrset_deep_free(&rr, 1, 1, 1);
goto cleanup;
}
}
break;
default:
assert(0);
}
// parse the next RR
dbg_xfrin("Parsing next RR..\n");
ret = knot_packet_parse_next_rr_answer(packet, &rr);
dbg_xfrin("Returned %d, %p.\n", ret, rr);
}
/*! \note Check TSIG, we're at the end of packet. It may not be
* required.
*/
ret = xfrin_check_tsig(packet, xfr,
knot_ns_tsig_required(xfr->packet_nr));
dbg_xfrin_detail("xfrin_check_tsig() returned %d\n", ret);
++xfr->packet_nr;
/*! \note [TSIG] Cleanup and propagate error if TSIG validation fails.*/
if (ret != KNOT_EOK) {
goto cleanup;
}
// here no RRs remain in the packet but the transfer is not finished
// yet, return EOK
knot_packet_free(&packet);
return KNOT_EOK;
cleanup:
/* We should go here only if some error occured. */
assert(ret < 0);
dbg_xfrin("Cleanup after processing IXFR/IN packet.\n");
knot_free_changesets(chs);
knot_packet_free(&packet);
xfr->data = 0;
return ret;
}
/*----------------------------------------------------------------------------*/
/* Applying changesets to zone */
/*----------------------------------------------------------------------------*/
typedef struct {
/*!
* Deleted (without owners and RDATA) after successful update.
*/
knot_rrset_t **old_rrsets;
int old_rrsets_count;
int old_rrsets_allocated;
/*!
* Deleted after successful update.
*/
knot_rdata_t **old_rdata;
uint *old_rdata_types;
int old_rdata_count;
int old_rdata_allocated;
/*!
* \brief Copied RRSets (i.e. modified by the update).
*
* Deleted (without owners and RDATA) after failed update.
*/
knot_rrset_t **new_rrsets;
int new_rrsets_count;
int new_rrsets_allocated;
/*!
* Deleted (without contents) after successful update.
*/
knot_node_t **old_nodes;
int old_nodes_count;
int old_nodes_allocated;
/*!
* Deleted (without contents) after failed update.
*/
knot_node_t **new_nodes;
int new_nodes_count;
int new_nodes_allocated;
ck_hash_table_item_t **old_hash_items;
int old_hash_items_count;
int old_hash_items_allocated;
} xfrin_changes_t;
/*----------------------------------------------------------------------------*/
static void xfrin_changes_free(xfrin_changes_t **changes)
{
free((*changes)->old_nodes);
free((*changes)->old_rrsets);
free((*changes)->old_rdata);
free((*changes)->old_rdata_types);
free((*changes)->new_rrsets);
free((*changes)->new_nodes);
free((*changes)->old_hash_items);
}
/*----------------------------------------------------------------------------*/
static int xfrin_changes_check_rrsets(knot_rrset_t ***rrsets,
int *count, int *allocated, int to_add)
{
/* Ensure at least requested size is allocated. */
int new_count = (*count + to_add);
assert(new_count >= 0);
if (new_count <= *allocated) {
return KNOT_EOK;
}
/* Allocate new memory block. */
knot_rrset_t **rrsets_new = malloc(new_count * sizeof(knot_rrset_t *));
if (rrsets_new == NULL) {
return KNOT_ENOMEM;
}
/* Initialize new memory and copy old data. */
memset(rrsets_new, 0, new_count * sizeof(knot_rrset_t *));
memcpy(rrsets_new, *rrsets, (*allocated) * sizeof(knot_rrset_t *));
/* Free old nodes and switch pointers. */
free(*rrsets);
*rrsets = rrsets_new;
*allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_changes_check_nodes(knot_node_t ***nodes,
int *count, int *allocated)
{
assert(nodes != NULL);
assert(count != NULL);
assert(allocated != 0);
/* Ensure at least count and some reserve is allocated. */
int new_count = *count + 2;
if (new_count <= *allocated) {
return KNOT_EOK;
}
/* Allocate new memory block. */
const size_t node_len = sizeof(knot_node_t *);
knot_node_t **nodes_new = malloc(new_count * node_len);
if (nodes_new == NULL) {
return KNOT_ENOMEM;
}
/* Clear memory block and copy old data. */
memset(nodes_new, 0, new_count * node_len);
memcpy(nodes_new, *nodes, (*allocated) * node_len);
/* Free old nodes and switch pointers. */
free(*nodes);
*nodes = nodes_new;
*allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_changes_check_rdata(knot_rdata_t ***rdatas, uint **types,
int count, int *allocated, int to_add)
{
/* Ensure at least requested size is allocated. */
int new_count = (count + to_add);
assert(new_count >= 0);
if (new_count <= *allocated) {
return KNOT_EOK;
}
/* Allocate new memory block. */
knot_rdata_t **rdatas_new = malloc(new_count * sizeof(knot_rdata_t *));
if (rdatas_new == NULL) {
return KNOT_ENOMEM;
}
uint *types_new = malloc(new_count * sizeof(uint));
if (types_new == NULL) {
return KNOT_ENOMEM;
}
/* Initialize new memory and copy old data. */
memset(rdatas_new, 0, new_count * sizeof(knot_rdata_t *));
memcpy(rdatas_new, *rdatas, (*allocated) * sizeof(knot_rdata_t *));
memset(types_new, 0, new_count * sizeof(uint));
memcpy(types_new, *types, (*allocated) * sizeof(uint));
/* Free old rdatas and switch pointers. */
free(*rdatas);
free(*types);
*rdatas = rdatas_new;
*types = types_new;
*allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_changes_check_hash_items(ck_hash_table_item_t ***items,
int *count, int *allocated)
{
/* Prevent infinite loop in case of allocated = 0. */
int new_count = 0;
if (*allocated == 0) {
new_count = *count + 1;
} else {
if (*count == *allocated) {
new_count = *allocated * 2;
}
}
const size_t item_len = sizeof(ck_hash_table_item_t *);
ck_hash_table_item_t **items_new = malloc(new_count * item_len);
if (items_new == NULL) {
return KNOT_ENOMEM;
}
memset(items_new, 0, new_count * item_len);
memcpy(items_new, *items, (*count) * item_len);
free(*items);
*items = items_new;
*allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static void xfrin_zone_contents_free(knot_zone_contents_t **contents)
{
/*! \todo This should be all in some API!! */
if ((*contents)->table != NULL) {
// ck_destroy_table(&(*contents)->table, NULL, 0);
ck_table_free(&(*contents)->table);
}
// free the zone tree, but only the structure
// (nodes are already destroyed)
dbg_zone("Destroying zone tree.\n");
knot_zone_tree_free(&(*contents)->nodes);
dbg_zone("Destroying NSEC3 zone tree.\n");
knot_zone_tree_free(&(*contents)->nsec3_nodes);
knot_nsec3_params_free(&(*contents)->nsec3_params);
knot_dname_table_deep_free(&(*contents)->dname_table);
free(*contents);
*contents = NULL;
}
/*----------------------------------------------------------------------------*/
static void xfrin_rollback_update(knot_zone_contents_t *contents,
xfrin_changes_t *changes)
{
/*
* This function is called only when no references were actually set to
* the new nodes, just the new nodes reference other.
* We thus do not need to fix any references, just from the old nodes
* to the new ones.
*/
// discard new nodes, but do not remove RRSets from them
for (int i = 0; i < changes->new_nodes_count; ++i) {
knot_node_free(&changes->new_nodes[i], 0, 0);
}
// set references from old nodes to new nodes to NULL and remove the
// old flag
for (int i = 0; i < changes->old_nodes_count; ++i) {
knot_node_set_new_node(changes->old_nodes[i], NULL);
knot_node_clear_old(changes->old_nodes[i]);
}
// discard new RRSets
for (int i = 0; i < changes->old_rrsets_count; ++i) {
knot_rrset_deep_free(&changes->new_rrsets[i], 0, 1, 0);
}
// destroy the shallow copy of zone
xfrin_zone_contents_free(&contents);
}
/*----------------------------------------------------------------------------*/
static knot_rdata_t *xfrin_remove_rdata(knot_rrset_t *from,
const knot_rrset_t *what)
{
knot_rdata_t *old = NULL;
knot_rdata_t *old_actual = NULL;
const knot_rdata_t *rdata = knot_rrset_rdata(what);
while (rdata != NULL) {
old_actual = knot_rrset_remove_rdata(from, rdata);
if (old_actual != NULL) {
old_actual->next = old;
old = old_actual;
}
rdata = knot_rrset_rdata_next(what, rdata);
}
return old;
}
/*----------------------------------------------------------------------------*/
static int xfrin_get_node_copy(knot_node_t **node, xfrin_changes_t *changes)
{
knot_node_t *new_node =
knot_node_get_new_node(*node);
if (new_node == NULL) {
dbg_xfrin("Creating copy of node.\n");
int ret = knot_node_shallow_copy(*node, &new_node);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to create node copy.\n");
return KNOT_ENOMEM;
}
dbg_xfrin_detail("Created copy of old node %p to new node %p\n",
*node, new_node);
assert(changes);
// changes->new_nodes_allocated = 0;
// save the copy of the node
ret = xfrin_changes_check_nodes(
&changes->new_nodes,
&changes->new_nodes_count,
&changes->new_nodes_allocated);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add new node to list.\n");
knot_node_free(&new_node, 0, 0);
return ret;
}
// changes->old_nodes_allocated = 0;
// save the old node to list of old nodes
ret = xfrin_changes_check_nodes(
&changes->old_nodes,
&changes->old_nodes_count,
&changes->old_nodes_allocated);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add old node to list.\n");
knot_node_free(&new_node, 0, 0);
return ret;
}
assert(changes->new_nodes);
assert(changes->old_nodes);
changes->new_nodes[changes->new_nodes_count++] = new_node;
changes->old_nodes[changes->old_nodes_count++] = *node;
// mark the old node as old
knot_node_set_old(*node);
knot_node_set_new(new_node);
knot_node_set_new_node(*node, new_node);
}
*node = new_node;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_copy_old_rrset(knot_rrset_t *old, knot_rrset_t **copy,
xfrin_changes_t *changes)
{
// create new RRSet by copying the old one
int ret = knot_rrset_shallow_copy(old, copy);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to create RRSet copy.\n");
return KNOT_ENOMEM;
}
// add the RRSet to the list of new RRSets
ret = xfrin_changes_check_rrsets(&changes->new_rrsets,
&changes->new_rrsets_count,
&changes->new_rrsets_allocated, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add new RRSet to list.\n");
knot_rrset_free(copy);
return ret;
}
changes->new_rrsets[changes->new_rrsets_count++] = *copy;
// add the old RRSet to the list of old RRSets
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add old RRSet to list.\n");
return ret;
}
changes->old_rrsets[changes->old_rrsets_count++] = old;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_copy_rrset(knot_node_t *node, knot_rr_type_t type,
knot_rrset_t **rrset, xfrin_changes_t *changes)
{
knot_rrset_t *old = knot_node_remove_rrset(node, type);
if (old == NULL) {
dbg_xfrin("RRSet not found for RR to be removed.\n");
return 1;
}
int ret = xfrin_copy_old_rrset(old, rrset, changes);
if (ret != KNOT_EOK) {
return ret;
}
dbg_xfrin_detail("Copied old rrset %p to new %p.\n",
old, *rrset);
// replace the RRSet in the node copy by the new one
ret = knot_node_add_rrset(node, *rrset, 0);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add RRSet copy to node\n");
return KNOT_ERROR;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_remove_rrsigs(xfrin_changes_t *changes,
const knot_rrset_t *remove,
knot_node_t *node,
knot_rrset_t **rrset)
{
assert(changes != NULL);
assert(remove != NULL);
assert(node != NULL);
assert(rrset != NULL);
assert(knot_rrset_type(remove) == KNOT_RRTYPE_RRSIG);
/*! \todo These optimalizations may be useless as there may be only
* one RRSet of each type and owner in the changeset.
*/
int ret;
if (!*rrset
|| knot_dname_compare(knot_rrset_owner(*rrset),
knot_node_owner(node)) != 0
|| knot_rrset_type(*rrset) != knot_rdata_rrsig_type_covered(
knot_rrset_rdata(remove))) {
// find RRSet based on the Type Covered
knot_rr_type_t type = knot_rdata_rrsig_type_covered(
knot_rrset_rdata(remove));
// copy the rrset
ret = xfrin_copy_rrset(node, type, rrset, changes);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to copy rrset from changeset.\n");
return ret;
}
} else {
// we should have the right RRSIG RRSet in *rrset
assert(knot_rrset_type(*rrset)
== knot_rdata_rrsig_type_covered(
knot_rrset_rdata(remove)));
// this RRSet should be the already copied RRSet so we may
// update it right away
}
// get the old rrsigs
knot_rrset_t *old = knot_rrset_get_rrsigs(*rrset);
if (old == NULL) {
return 1;
}
// copy the RRSIGs
/*! \todo This may be done unnecessarily more times. */
knot_rrset_t *rrsigs;
ret = xfrin_copy_old_rrset(old, &rrsigs, changes);
if (ret != KNOT_EOK) {
return ret;
}
// set the RRSIGs to the new RRSet copy
if (knot_rrset_set_rrsigs(*rrset, rrsigs) != KNOT_EOK) {
dbg_xfrin("Failed to set rrsigs.\n");
return KNOT_ERROR;
}
// now in '*rrset' we have a copy of the RRSet which holds the RRSIGs
// and in 'rrsigs' we have the copy of the RRSIGs
knot_rdata_t *rdata = xfrin_remove_rdata(rrsigs, remove);
if (rdata == NULL) {
dbg_xfrin("Failed to remove RDATA from RRSet: %s.\n",
knot_strerror(ret));
return 1;
}
// if the RRSet is empty, remove from node and add to old RRSets
// check if there is no RRSIGs; if there are, leave the RRSet
// there; it may be eventually removed when the RRSIGs are removed
if (knot_rrset_rdata(rrsigs) == NULL) {
// remove the RRSIGs from the RRSet
knot_rrset_set_rrsigs(*rrset, NULL);
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add empty RRSet to the "
"list of old RRSets.");
// delete the RRSet right away
knot_rrset_free(&rrsigs);
return ret;
}
changes->old_rrsets[changes->old_rrsets_count++] = rrsigs;
// now check if the RRSet is not totally empty
if (knot_rrset_rdata(*rrset) == NULL) {
assert(knot_rrset_rrsigs(*rrset) == NULL);
// remove the whole RRSet from the node
knot_rrset_t *tmp = knot_node_remove_rrset(node,
knot_rrset_type(*rrset));
assert(tmp == *rrset);
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add empty RRSet to "
"the list of old RRSets.");
// delete the RRSet right away
knot_rrset_free(rrset);
return ret;
}
changes->old_rrsets[changes->old_rrsets_count++] =
*rrset;
}
}
// connect the RDATA to the list of old RDATA
ret = xfrin_changes_check_rdata(&changes->old_rdata,
&changes->old_rdata_types,
changes->old_rdata_count,
&changes->old_rdata_allocated, 1);
if (ret != KNOT_EOK) {
return ret;
}
changes->old_rdata[changes->old_rdata_count] = rdata;
changes->old_rdata_types[changes->old_rdata_count] =
knot_rrset_type(remove);
++changes->old_rdata_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_remove_normal(xfrin_changes_t *changes,
const knot_rrset_t *remove,
knot_node_t *node,
knot_rrset_t **rrset)
{
assert(changes != NULL);
assert(remove != NULL);
assert(node != NULL);
assert(rrset != NULL);
int ret;
dbg_xfrin_detail("Removing RRSet: \n");
knot_rrset_dump(remove, 0);
// now we have the copy of the node, so lets get the right RRSet
// check if we do not already have it
if (!*rrset
|| knot_dname_compare(knot_rrset_owner(*rrset),
knot_node_owner(node)) != 0
|| knot_rrset_type(*rrset)
!= knot_rrset_type(remove)) {
/*!
* \todo This may happen also with already
* copied RRSet. In that case it would be
* an unnecesary overhead but will
* probably not cause problems. TEST!!
*/
ret = xfrin_copy_rrset(node,
knot_rrset_type(remove), rrset, changes);
dbg_xfrin_detail("Copied RRSet:\n");
knot_rrset_dump(*rrset, 0);
if (ret != KNOT_EOK) {
return ret;
}
}
if (*rrset == NULL) {
dbg_xfrin("RRSet not found for RR to be removed.\n");
return 1;
}
dbg_xfrin_exec(
char *name = knot_dname_to_str(knot_rrset_owner(*rrset));
dbg_xfrin("Updating RRSet with owner %s, type %s\n", name,
knot_rrtype_to_string(knot_rrset_type(*rrset)));
free(name);
);
// remove the specified RRs from the RRSet (de facto difference of
// sets)
knot_rdata_t *rdata = xfrin_remove_rdata(*rrset, remove);
if (rdata == NULL) {
dbg_xfrin("Failed to remove RDATA from RRSet: %s.\n",
knot_strerror(ret));
return 1;
}
dbg_xfrin_exec_detail(
dbg_xfrin_detail("Removed rdata: \n");
knot_rdata_t *r = rdata;
if (r != NULL) {
do {
dbg_xfrin_detail("pointer: %p\n", r);
knot_rdata_dump(r, knot_rrset_type(remove), 0);
r = r->next;
} while (r != NULL && r != rdata);
}
);
// if the RRSet is empty, remove from node and add to old RRSets
// check if there is no RRSIGs; if there are, leave the RRSet
// there; it may be eventually removed when the RRSIGs are removed
if (knot_rrset_rdata(*rrset) == NULL
&& knot_rrset_rrsigs(*rrset) == NULL) {
knot_rrset_t *tmp = knot_node_remove_rrset(node,
knot_rrset_type(*rrset));
dbg_xfrin_detail("Removed whole RRSet (%p).\n", tmp);
// add the removed RRSet to list of old RRSets
assert(tmp == *rrset);
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add empty RRSet to the "
"list of old RRSets.");
// delete the RRSet right away
knot_rrset_free(rrset);
return ret;
}
changes->old_rrsets[changes->old_rrsets_count++] = *rrset;
}
// connect the RDATA to the list of old RDATA
ret = xfrin_changes_check_rdata(&changes->old_rdata,
&changes->old_rdata_types,
changes->old_rdata_count,
&changes->old_rdata_allocated, 1);
if (ret != KNOT_EOK) {
return ret;
}
changes->old_rdata[changes->old_rdata_count] = rdata;
changes->old_rdata_types[changes->old_rdata_count] =
knot_rrset_type(remove);
++changes->old_rdata_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_remove_all_rrsets(xfrin_changes_t *changes,
knot_node_t *node, uint16_t type)
{
/*! \todo Implement. */
int ret;
if (type == KNOT_RRTYPE_ANY) {
// put all the RRSets to the changes structure
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
knot_node_rrset_count(node));
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to check changeset rrsets.\n");
return ret;
}
knot_rrset_t **rrsets = knot_node_get_rrsets(node);
knot_rrset_t **place = changes->old_rrsets
+ changes->old_rrsets_count;
/*! \todo Test this!!! */
memcpy(place, rrsets, knot_node_rrset_count(node) * sizeof(knot_rrset_t *));
// remove all RRSets from the node
knot_node_remove_all_rrsets(node);
} else {
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated,
1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to check changeset rrsets.\n");
return ret;
}
// remove only RRSet with the given type
knot_rrset_t *rrset = knot_node_remove_rrset(node, type);
changes->old_rrsets[changes->old_rrsets_count++] = rrset;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_remove(knot_zone_contents_t *contents,
knot_changeset_t *chset,
xfrin_changes_t *changes)
{
/*
* Iterate over removed RRSets, copy appropriate nodes and remove
* the rrsets from them. By default, the RRSet should be copied so that
* RDATA may be removed from it.
*/
int ret = 0;
knot_node_t *node = NULL;
knot_rrset_t *rrset = NULL;
for (int i = 0; i < chset->remove_count; ++i) {
// check if the old node is not the one we should use
if (!node || knot_rrset_owner(chset->remove[i])
!= knot_node_owner(node)) {
node = knot_zone_contents_get_node(contents,
knot_rrset_owner(chset->remove[i]));
if (node == NULL) {
dbg_xfrin("Node not found for RR to be removed"
"!\n");
continue;
}
}
// create a copy of the node if not already created
if (!knot_node_is_new(node)) {
ret = xfrin_get_node_copy(&node, changes);
if (ret != KNOT_EOK) {
return ret;
}
}
assert(node != NULL);
assert(knot_node_is_new(node));
// first check if all RRSets should be removed
if (knot_rrset_class(chset->remove[i]) == KNOT_CLASS_ANY) {
ret = xfrin_apply_remove_all_rrsets(
changes, node,
knot_rrset_type(chset->remove[i]));
} else if (knot_rrset_type(chset->remove[i])
== KNOT_RRTYPE_RRSIG) {
// this should work also for UPDATE
ret = xfrin_apply_remove_rrsigs(changes,
chset->remove[i],
node, &rrset);
} else {
// this should work also for UPDATE
ret = xfrin_apply_remove_normal(changes,
chset->remove[i],
node, &rrset);
}
dbg_xfrin("xfrin_apply_remove() ret = %d\n", ret);
if (ret > 0) {
continue;
} else if (ret != KNOT_EOK) {
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static knot_node_t *xfrin_add_new_node(knot_zone_contents_t *contents,
knot_rrset_t *rrset)
{
/*! \todo Why is the function disabled? */
//return NULL;
knot_node_t *node = knot_node_new(knot_rrset_get_owner(rrset),
NULL, KNOT_NODE_FLAGS_NEW);
if (node == NULL) {
dbg_xfrin("Failed to create a new node.\n");
return NULL;
}
int ret = 0;
// insert the node into zone structures and create parents if
// necessary
// dbg_xfrin("Adding new node to zone. From owner: %s type %s\n",
// knot_dname_to_str(node->owner),
// knot_rrtype_to_string(rrset->type));
// getchar();
if (knot_rrset_type(rrset) == KNOT_RRTYPE_NSEC3) {
ret = knot_zone_contents_add_nsec3_node(contents, node, 1, 0,
1);
} else {
ret = knot_zone_contents_add_node(contents, node, 1,
KNOT_NODE_FLAGS_NEW, 1);
}
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add new node to zone contents.\n");
return NULL;
}
// find previous node and connect the new one to it
knot_node_t *prev = NULL;
if (knot_rrset_type(rrset) == KNOT_RRTYPE_NSEC3) {
prev = knot_zone_contents_get_previous_nsec3(contents,
knot_rrset_owner(rrset));
} else {
prev = knot_zone_contents_get_previous(contents,
knot_rrset_owner(rrset));
}
// fix prev and next pointers
if (prev != NULL) {
knot_node_set_previous(node, prev);
}
// printf("contents owned by: %s (%p)\n",
// knot_dname_to_str(contents->apex->owner),
// contents);
assert(contents->zone != NULL);
knot_node_set_zone(node, contents->zone);
return node;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_add_normal(xfrin_changes_t *changes,
knot_rrset_t *add,
knot_node_t *node,
knot_rrset_t **rrset)
{
assert(changes != NULL);
assert(add != NULL);
assert(node != NULL);
assert(rrset != NULL);
int ret;
dbg_xfrin("applying rrset:\n");
knot_rrset_dump(add, 0);
// getchar();
if (!*rrset
|| knot_dname_compare(knot_rrset_owner(*rrset),
knot_node_owner(node)) != 0
|| knot_rrset_type(*rrset)
!= knot_rrset_type(add)) {
dbg_xfrin("Removing rrset!\n");
*rrset = knot_node_remove_rrset(node, knot_rrset_type(add));
}
dbg_xfrin("Removed RRSet: \n");
knot_rrset_dump(*rrset, 1);
if (*rrset == NULL) {
dbg_xfrin_exec_verb(
char *name = knot_dname_to_str(add->owner);
dbg_xfrin_verb("RRSet to be added not found in zone.\n");
dbg_xfrin_verb("owner: %s type: %s\n", name,
knot_rrtype_to_string(add->type));
free(name);
);
// getchar();
// add the RRSet from the changeset to the node
/*! \todo What about domain names?? Shouldn't we use the
* zone-contents' version of this function??
*/
ret = knot_node_add_rrset(node, add, 0);
// ret = knot_zone_contents_add_rrset(node->zone->contents,
// rrset, node,
// KNOT_RRSET_DUPL_MERGE,
// 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add RRSet to node.\n");
return KNOT_ERROR;
}
return 1; // return 1 to indicate the add RRSet was used
}
knot_rrset_t *old = *rrset;
dbg_xfrin_exec(
char *name = knot_dname_to_str(knot_rrset_owner(*rrset));
dbg_xfrin("Found RRSet with owner %s, type %s\n", name,
knot_rrtype_to_string(knot_rrset_type(*rrset)));
free(name);
);
knot_rrset_dump(*rrset, 1);
ret = xfrin_copy_old_rrset(old, rrset, changes);
if (ret != KNOT_EOK) {
assert(0);
return ret;
}
// dbg_xfrin("After copy: Found RRSet with owner %s, type %s\n",
// knot_dname_to_str((*rrset)->owner),
// knot_rrtype_to_string(knot_rrset_type(*rrset)));
// merge the changeset RRSet to the copy
/* What if the update fails?
* The changesets will be destroyed - that will destroy 'add',
* and the copied RRSet will be destroyed because it is in the new
* rrsets list.
*
* If the update is successfull, the old RRSet will be destroyed,
* but the one from the changeset will be not!!
*
* TODO: add the 'add' rrset to list of old RRSets?
*/
dbg_xfrin("Merging RRSets with owners: %s %s types: %d %d\n",
(*rrset)->owner->name, add->owner->name, (*rrset)->type,
add->type);
dbg_xfrin_detail("RDATA in RRSet1: %p, RDATA in RRSet2: %p\n",
(*rrset)->rdata, add->rdata);
ret = knot_rrset_merge((void **)rrset, (void **)&add);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to merge changeset RRSet to copy.\n");
return KNOT_ERROR;
}
dbg_xfrin("Merge returned: %d\n", ret);
knot_rrset_dump(*rrset, 1);
ret = knot_node_add_rrset(node, *rrset, 0);
// return 2 so that the add RRSet is removed from
// the changeset (and thus not deleted)
// and put to list of new RRSets (is this ok?)
// and deleted
return 2;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_add_rrsig(xfrin_changes_t *changes,
knot_rrset_t *add,
knot_node_t *node,
knot_rrset_t **rrset)
{
assert(changes != NULL);
assert(add != NULL);
assert(node != NULL);
assert(rrset != NULL);
assert(knot_rrset_type(add) == KNOT_RRTYPE_RRSIG);
int ret;
knot_rr_type_t type = knot_rdata_rrsig_type_covered(
knot_rrset_rdata(add));
if (!*rrset
|| knot_dname_compare(knot_rrset_owner(*rrset),
knot_node_owner(node)) != 0
|| knot_rrset_type(*rrset) != knot_rdata_rrsig_type_covered(
knot_rrset_rdata(add))) {
// copy the rrset
ret = xfrin_copy_rrset(node, type, rrset, changes);
if (ret < 0) {
return ret;
}
} else {
// we should have the right RRSIG RRSet in *rrset
assert(knot_rrset_type(*rrset) == type);
// this RRSet should be the already copied RRSet so we may
// update it right away
}
if (*rrset == NULL) {
dbg_xfrin("RRSet to be added not found in zone.\n");
// create a new RRSet to add the RRSIGs into
*rrset = knot_rrset_new(knot_node_get_owner(node), type,
knot_rrset_class(add),
knot_rrset_ttl(add));
if (*rrset == NULL) {
dbg_xfrin("Failed to create new RRSet for RRSIGs.\n");
return KNOT_ENOMEM;
}
// add the RRSet from the changeset to the node
ret = knot_node_add_rrset(node, *rrset, 0);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add RRSet to node.\n");
return KNOT_ERROR;
}
}
dbg_xfrin_exec(
char *name = knot_dname_to_str(knot_rrset_owner(*rrset));
dbg_xfrin("Found RRSet with owner %s, type %s\n", name,
knot_rrtype_to_string(knot_rrset_type(*rrset)));
free(name);
);
if (knot_rrset_rrsigs(*rrset) == NULL) {
ret = knot_rrset_set_rrsigs(*rrset, add);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add RRSIGs to the RRSet.\n");
return KNOT_ERROR;
}
return 1;
} else {
knot_rrset_t *old = knot_rrset_get_rrsigs(*rrset);
assert(old != NULL);
knot_rrset_t *rrsig;
ret = xfrin_copy_old_rrset(old, &rrsig, changes);
if (ret != KNOT_EOK) {
return ret;
}
// replace the old RRSIGs with the new ones
knot_rrset_set_rrsigs(*rrset, rrsig);
// merge the changeset RRSet to the copy
/*! \todo What if the update fails?
*
*/
ret = knot_rrset_merge((void **)&rrsig, (void **)&add);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to merge changeset RRSet to copy.\n");
return KNOT_ERROR;
}
return 2;
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_add(knot_zone_contents_t *contents,
knot_changeset_t *chset,
xfrin_changes_t *changes)
{
// iterate over removed RRSets, copy appropriate nodes and remove
// the rrsets from them
int ret = 0;
knot_node_t *node = NULL;
knot_rrset_t *rrset = NULL;
for (int i = 0; i < chset->add_count; ++i) {
dbg_xfrin_detail("Adding RRSet:\n");
knot_rrset_dump(chset->add[i], 0);
// check if the old node is not the one we should use
if (!node || knot_rrset_owner(chset->add[i])
!= knot_node_owner(node)) {
node = knot_zone_contents_get_node(contents,
knot_rrset_owner(chset->add[i]));
if (node == NULL) {
// create new node, connect it properly to the
// zone nodes
dbg_xfrin("Creating new node from.\n");
node = xfrin_add_new_node(contents,
chset->add[i]);
if (node == NULL) {
dbg_xfrin("Failed to create new node "
"in zone.\n");
return KNOT_ERROR;
}
// continue; // continue with another RRSet
}
}
// create a copy of the node if not already created
if (!knot_node_is_new(node)) {
xfrin_get_node_copy(&node, changes);
}
assert(node != NULL);
assert(knot_node_is_new(node));
if (knot_rrset_type(chset->add[i]) == KNOT_RRTYPE_RRSIG) {
ret = xfrin_apply_add_rrsig(changes, chset->add[i],
node, &rrset);
} else {
ret = xfrin_apply_add_normal(changes, chset->add[i],
node, &rrset);
}
dbg_xfrin("xfrin_apply_..() returned %d, rrset: %p\n", ret,
rrset);
if (ret == 1) {
// the ADD RRSet was used, i.e. it should be removed
// from the changeset and saved in the list of new
// RRSets
ret = xfrin_changes_check_rrsets(
&changes->new_rrsets,
&changes->new_rrsets_count,
&changes->new_rrsets_allocated, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add old RRSet to list.\n");
return ret;
}
changes->new_rrsets[changes->new_rrsets_count++] =
chset->add[i];
chset->add[i] = NULL;
} else if (ret == 2) {
// the copy of the RRSet was used, but it was already
// stored in the new RRSets list
// just delete the add RRSet, but without RDATA
// as these were merged to the copied RRSet
knot_rrset_free(&chset->add[i]);
} else if (ret != KNOT_EOK) {
return ret;
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
/*!
* \todo This must be tested!! Simulate failure somehow.
*/
static void xfrin_clean_changes_after_fail(xfrin_changes_t *changes)
{
/* 1) Delete copies of RRSets created because they were updated.
* Do not delete their RDATA or owners.
*/
for (int i = 0; i < changes->new_rrsets_count; ++i) {
knot_rrset_free(&changes->new_rrsets[i]);
}
/* 2) Delete copies of nodes created because they were updated.
* Do not delete their RRSets.
*/
for (int i = 0; i < changes->new_nodes_count; ++i) {
knot_node_free(&changes->new_nodes[i], 0, 1);
}
// changesets will be deleted elsewhere
// so just delete the changes structure
xfrin_changes_free(&changes);
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_replace_soa(knot_zone_contents_t *contents,
xfrin_changes_t *changes,
knot_changeset_t *chset)
{
knot_node_t *node = knot_zone_contents_get_apex(contents);
assert(node != NULL);
int ret = 0;
// create a copy of the node if not already created
if (!knot_node_is_new(node)) {
ret = xfrin_get_node_copy(&node, changes);
if (ret != KNOT_EOK) {
return ret;
}
}
assert(knot_node_is_new(node));
// set the node copy as the apex of the contents
contents->apex = node;
// remove the SOA RRSet from the apex
knot_rrset_t *rrset = knot_node_remove_rrset(node, KNOT_RRTYPE_SOA);
assert(rrset != NULL);
// add the old RRSet to the list of old RRSets
ret = xfrin_changes_check_rrsets(&changes->old_rrsets,
&changes->old_rrsets_count,
&changes->old_rrsets_allocated, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add old RRSet to list.\n");
return ret;
}
// save also the SOA RDATA, because RDATA are not deleted with the
// RRSet
ret = xfrin_changes_check_rdata(&changes->old_rdata,
&changes->old_rdata_types,
changes->old_rdata_count,
&changes->old_rdata_allocated, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add old RDATA to list.\n");
return ret;
}
// save the SOA to the new RRSet, so that it is deleted if the
// apply fails
ret = xfrin_changes_check_rrsets(&changes->new_rrsets,
&changes->new_rrsets_count,
&changes->new_rrsets_allocated, 1);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add old RRSet to list.\n");
return ret;
}
changes->old_rrsets[changes->old_rrsets_count++] = rrset;
/*! \todo Maybe check if the SOA does not have more RDATA? */
changes->old_rdata[changes->old_rdata_count] = rrset->rdata;
changes->old_rdata_types[changes->old_rdata_count] = KNOT_RRTYPE_SOA;
++changes->old_rdata_count;
// insert the new SOA RRSet to the node
dbg_xfrin_verb("Adding SOA.\n");
ret = knot_node_add_rrset(node, chset->soa_to, 0);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to add RRSet to node.\n");
return KNOT_ERROR;
}
changes->new_rrsets[changes->new_rrsets_count++] = chset->soa_to;
// remove the SOA from the changeset, so it will not be deleted after
// successful apply
chset->soa_to = NULL;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_apply_changeset(knot_zone_contents_t *contents,
xfrin_changes_t *changes,
knot_changeset_t *chset)
{
/*
* Applies one changeset to the zone. Checks if the changeset may be
* applied (i.e. the origin SOA (soa_from) has the same serial as
* SOA in the zone apex.
*/
// check if serial matches
/*! \todo Only if SOA is present? */
const knot_rrset_t *soa = knot_node_rrset(contents->apex,
KNOT_RRTYPE_SOA);
if (soa == NULL || knot_rdata_soa_serial(knot_rrset_rdata(soa))
!= chset->serial_from) {
dbg_xfrin("SOA serials do not match!!\n");
return KNOT_ERROR;
}
int ret = xfrin_apply_remove(contents, chset, changes);
if (ret != KNOT_EOK) {
xfrin_clean_changes_after_fail(changes);
return ret;
}
ret = xfrin_apply_add(contents, chset, changes);
if (ret != KNOT_EOK) {
xfrin_clean_changes_after_fail(changes);
return ret;
}
/*! \todo Only if SOA is present? */
return xfrin_apply_replace_soa(contents, changes, chset);
}
/*----------------------------------------------------------------------------*/
static void xfrin_check_node_in_tree(knot_zone_tree_node_t *tnode, void *data)
{
assert(tnode != NULL);
assert(data != NULL);
assert(tnode->node != NULL);
xfrin_changes_t *changes = (xfrin_changes_t *)data;
knot_node_t *node = knot_node_get_new_node(tnode->node);
if (node == NULL) {
// no RRSets were removed from this node, thus it cannot be
// empty
return;
}
dbg_xfrin("xfrin_check_node_in_tree: children of old node: %u, "
"children of new node: %u.\n",
knot_node_children(node),
knot_node_children(tnode->node));
// check if the node is empty and has no children
// to be sure, check also the count of children of the old node
if (knot_node_rrset_count(node) == 0
&& knot_node_children(node) == 0
&& knot_node_children(tnode->node) == 0) {
// in this case the new node copy should be removed
// but it cannot be deleted because if a rollback happens,
// the node must be in the new nodes list
// just add it to the old nodes list so that it is deleted
// after successful update
// set the new node of the old node to NULL
knot_node_set_new_node(tnode->node, NULL);
// if the parent has a new copy, decrease the number of
// children of that copy
if (knot_node_new_node(knot_node_parent(node, 0))) {
/*! \todo Replace by some API. */
--node->parent->new_node->children;
}
// put the new node to te list of old nodes
if (xfrin_changes_check_nodes(&changes->old_nodes,
&changes->old_nodes_count,
&changes->old_nodes_allocated)
!= KNOT_EOK) {
/*! \todo Notify about the error!!! */
return;
}
changes->old_nodes[changes->old_nodes_count++] = node;
// leave the old node in the old node list, we will delete
// it later
}
}
/*----------------------------------------------------------------------------*/
static int xfrin_finalize_remove_nodes(knot_zone_contents_t *contents,
xfrin_changes_t *changes)
{
assert(contents != NULL);
assert(changes != NULL);
knot_node_t *node;
knot_zone_tree_node_t *removed;
ck_hash_table_item_t *rem_hash;
int ret;
for (int i = 0; i < changes->old_nodes_count; ++i) {
node = changes->old_nodes[i];
// if the node is marked as old and has no new node copy
// remove it from the zone structure but do not delete it
// that may be done only after the grace period
if (knot_node_is_old(node)
&& knot_node_new_node(node) == NULL) {
if (knot_node_rrset(node, KNOT_RRTYPE_NSEC3)
!= NULL) {
ret = knot_zone_contents_remove_nsec3_node(
contents, node, &removed);
} else {
ret = knot_zone_contents_remove_node(
contents, node, &removed, &rem_hash);
}
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to remove node from zone"
"!\n");
return KNOT_ENONODE;
}
assert(removed != NULL);
assert(removed->node == node);
// delete the tree node (not needed)
free(removed);
if (rem_hash != NULL) {
// save the removed hash table item
ret = xfrin_changes_check_hash_items(
&changes->old_hash_items,
&changes->old_hash_items_count,
&changes->old_hash_items_allocated);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to save the hash"
" table item to list of "
"old items.\n");
return ret;
}
changes->old_hash_items[
changes->old_hash_items_count++]
= rem_hash;
}
}
}
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_finalize_contents(knot_zone_contents_t *contents,
xfrin_changes_t *changes)
{
// don't know what should have been done here, except for one thing:
// walk through the zone and remove empty nodes (save them in the
// old nodes list). But only those having no children!!!
/*
* Walk through the zone and remove empty nodes.
* We must walk backwards, so that children are processed before
* their parents. This will allow to remove chain of parent-children
* nodes.
* We cannot remove the nodes right away as it would modify the very
* structure used for walking through the zone. Just put the nodes
* to the list of old nodes to be removed.
* We must also decrease the node's parent's children count now
* and not when deleting the node, so that the chain of parent-child
* nodes may be removed.
*/
knot_zone_tree_t *t = knot_zone_contents_get_nodes(contents);
assert(t != NULL);
// walk through the zone and select nodes to be removed
knot_zone_tree_reverse_apply_postorder(t, xfrin_check_node_in_tree,
(void *)changes);
// Do the same with NSEC3 nodes.
t = knot_zone_contents_get_nsec3_nodes(contents);
assert(t != NULL);
knot_zone_tree_reverse_apply_postorder(t, xfrin_check_node_in_tree,
(void *)changes);
// remove the nodes one by one
return xfrin_finalize_remove_nodes(contents, changes);
}
/*----------------------------------------------------------------------------*/
static void xfrin_fix_refs_in_node(knot_zone_tree_node_t *tnode, void *data)
{
/*! \todo Passed data is always seto to NULL. */
assert(tnode != NULL);
//assert(data != NULL);
//xfrin_changes_t *changes = (xfrin_changes_t *)data;
// 1) Fix the reference to the node to the new one if there is some
knot_node_t *node = tnode->node;
knot_node_t *new_node = knot_node_get_new_node(node);
if (new_node != NULL) {
//assert(knot_node_rrset_count(new_node) > 0);
node = new_node;
tnode->node = new_node;
}
// 2) fix references from the node remaining in the zone
knot_node_update_refs(node);
}
/*----------------------------------------------------------------------------*/
static void xfrin_fix_gen_in_node(knot_zone_tree_node_t *tnode, void *data)
{
/*! \todo Passed data is always seto to NULL. */
assert(tnode != NULL);
knot_node_t *node = tnode->node;
knot_node_set_old(node);
}
/*----------------------------------------------------------------------------*/
static void xfrin_fix_hash_refs(ck_hash_table_item_t *item, void *data)
{
if (item == NULL) {
return;
}
knot_node_t *new_node = knot_node_get_new_node(
(knot_node_t *)item->value);
if (new_node != NULL) {
assert(item->key_length
== knot_dname_size(knot_node_owner(new_node)));
assert(strncmp(item->key, (const char *)knot_dname_name(
knot_node_owner(new_node)), item->key_length) == 0);
item->value = (void *)new_node;
item->key = (const char *)knot_dname_name(
knot_node_owner(new_node));
}
}
/*----------------------------------------------------------------------------*/
static void xfrin_fix_dname_refs(knot_dname_t *dname, void *data)
{
UNUSED(data);
knot_dname_update_node(dname);
}
/*----------------------------------------------------------------------------*/
static int xfrin_fix_references(knot_zone_contents_t *contents)
{
/*! \todo This function must not fail!! */
/*
* Now the contents are already switched, and we should update all
* references not updated yet, so that the old contents may be removed.
*
* Walk through the zone tree, so that each node will be checked
* and updated.
*/
// fix references in normal nodes
knot_zone_tree_t *tree = knot_zone_contents_get_nodes(contents);
knot_zone_tree_forward_apply_inorder(tree, xfrin_fix_refs_in_node,
NULL);
// fix refereces in NSEC3 nodes
tree = knot_zone_contents_get_nsec3_nodes(contents);
knot_zone_tree_forward_apply_inorder(tree, xfrin_fix_refs_in_node,
NULL);
// fix references in hash table
ck_hash_table_t *table = knot_zone_contents_get_hash_table(contents);
ck_apply(table, xfrin_fix_hash_refs, NULL);
// fix references dname table
int ret = knot_zone_contents_dname_table_apply(contents,
xfrin_fix_dname_refs, NULL);
assert(ret == KNOT_EOK);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static int xfrin_fix_generation(knot_zone_contents_t *contents)
{
assert(contents != NULL);
knot_zone_tree_t *tree = knot_zone_contents_get_nodes(contents);
knot_zone_tree_forward_apply_inorder(tree, xfrin_fix_gen_in_node,
NULL);
tree = knot_zone_contents_get_nsec3_nodes(contents);
knot_zone_tree_forward_apply_inorder(tree, xfrin_fix_gen_in_node,
NULL);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
static void xfrin_cleanup_update(xfrin_changes_t *changes)
{
// free old nodes but do not destroy their RRSets
// remove owners also, because of reference counting
for (int i = 0; i < changes->old_nodes_count; ++i) {
dbg_xfrin_detail("Deleting old node: %p\n", changes->old_nodes[i]);
knot_node_dump(changes->old_nodes[i], 0);
knot_node_free(&changes->old_nodes[i], 1, 0);
}
// free old RRSets, and destroy also domain names in them
// because of reference counting
// check if there are not some duplicate RRSets
// for (int i = 0; i < changes->old_rrsets_count; ++i) {
// for (int j = i + 1; j < changes->old_rrsets_count; ++j) {
// if (changes->old_rrsets[i] == changes->old_rrsets[j]) {
// assert(0);
// }
// if (changes->old_rrsets[i]->rdata != NULL
// && changes->old_rrsets[i]->rdata
// == changes->old_rrsets[j]->rdata) {
// assert(0);
// }
// }
// }
for (int i = 0; i < changes->old_rrsets_count; ++i) {
// knot_rrset_deep_free(&changes->old_rrsets[i], 1, 1, 1);
dbg_xfrin_detail("Deleting old RRSet: %p\n", changes->old_rrsets[i]);
knot_rrset_dump(changes->old_rrsets[i], 0);
knot_rrset_free(&changes->old_rrsets[i]);
}
// delete old RDATA
for (int i = 0; i < changes->old_rdata_count; ++i) {
dbg_xfrin_detail("Deleting old RDATA: %p, type: %s\n",
changes->old_rdata[i],
knot_rrtype_to_string(changes->old_rdata_types[i]));
knot_rdata_dump(changes->old_rdata[i], changes->old_rdata_types[i], 0);
knot_rdata_t *rdata = changes->old_rdata[i];
assert(rdata != NULL);
do {
knot_rdata_t *tmp = rdata->next;
knot_rdata_deep_free(&rdata,
changes->old_rdata_types[i], 1);
rdata = tmp;
} while (rdata != NULL && rdata != changes->old_rdata[i]);
changes->old_rdata[i] = NULL;
}
// free old hash table items, but do not touch their contents
for (int i = 0; i < changes->old_hash_items_count; ++i) {
free(changes->old_hash_items[i]);
}
free(changes->old_hash_items);
// free allocated arrays of nodes and rrsets
free(changes->new_nodes);
free(changes->old_nodes);
free(changes->new_rrsets);
free(changes->old_rrsets);
free(changes->old_rdata);
free(changes->old_rdata_types);
}
/*----------------------------------------------------------------------------*/
int xfrin_apply_changesets_to_zone(knot_zone_t *zone,
knot_changesets_t *chsets)
{
if (zone == NULL || chsets == NULL || chsets->count == 0) {
return KNOT_EBADARG;
}
knot_zone_contents_t *old_contents = knot_zone_get_contents(zone);
if (!old_contents) {
return KNOT_EBADARG;
}
// dbg_xfrin("\nOLD ZONE CONTENTS:\n\n");
// knot_zone_contents_dump(old_contents, 1);
/*
* Ensure that the zone generation is set to 0.
*/
if (!knot_zone_contents_gen_is_old(old_contents)) {
// this would mean that a previous update was not completed
// abort
dbg_zone("Trying to apply changesets to zone that is "
"being updated. Aborting.\n");
return KNOT_EAGAIN;
}
/*
* Create a shallow copy of the zone, so that the structures may be
* updated.
*
* This will create new zone contents structures (normal nodes' tree,
* NSEC3 tree, hash table, domain name table), but fill them with the
* data from the old contents.
*/
knot_zone_contents_t *contents_copy = NULL;
int ret = knot_zone_contents_shallow_copy(old_contents,
&contents_copy);
if (ret != KNOT_EOK) {
dbg_xfrin("Failed to create shallow copy of zone: %s\n",
knot_strerror(ret));
return ret;
}
/*
* Now, apply one changeset after another until all are applied.
* Changesets may be either from IXFR or from a dynamic update.
* Dynamic updates use special TYPE and CLASS values to distinguish
* requests, such as "remove all RRSets from a node", "remove all RRs
* with the specified type from a node", etc.
*
* When updating anything within some node (removing RR, adding RR),
* the node structure is copied, but the RRSets within are not.
*
* 1) When removing RRs from node, The affected RRSet is copied. This
* it also a 'shallow copy', i.e. the RDATA remain the exact same.
* The specified RRs (i.e. RDATA) are then removed from the copied
* RRSet.
* 2) When adding RRs to node, there are two cases:
* a) If there is a RRSet that should contain these RRs
* this RRSet is copied (shallow copy) and the RRs are added to
* it (rrset_merge()).
* b) If there is not such a RRSet, the whole RRSet from the
* changeset is added to the new node (thus this RRSet must not
* be deleted afterwards).
*
* A special case are RRSIG RRs. These functions assume that they
* are grouped together in knot_rrset_t structures according to
* their header (owner, type, class) AND their 'type covered', i.e.
* there may be more RRSIG RRSets in one changeset (while there
* should not be more RRSets of any other type).
* 3) When removing RRSIG RRs from node, the appropriate RRSet holding
* them must be found (according to the 'type covered' field). This
* RRSet is then copied (shallow copy), Its RRSIGs are also copied
* and the RRSIG RRs are added to the RRSIG copy.
* 4) When adding RRSIG RRs to node, the same process is done - the
* proper RRSet holding them is found, copied, its RRSIGs are
* copied (if there are some) and the RRs are added to the copy.
*
* When a node is copied, reference to the copy is stored within the
* old node (node_t.old_node). This is important, because when the
* zone contents are switched to the new ones, references from old nodes
* that should point to new nodes are not yet set (it would influence
* replying from the old zone contents). While all these references
* (such as node_t.prev, node_t.next, node_t.parent, etc.) are properly
* modified, the search functions use old or new nodes accordingly
* (old nodes while old contents are used, new nodes when new contents
* are used). The 'check_version' parameter turns on this behaviour in
* search functions.
*
* In case of error, we must remove all data created by the update, i.e.
* - new nodes,
* - new RRSets,
* and remove the references to the new nodes from old nodes.
*
* In case of success, the RRSet structures from the changeset
* structures must not be deleted, as they are either already used by
* the server (stored within the new zone contents) or deleted when
* cleaning up the temporary 'changes' structure.
*/
xfrin_changes_t changes;
memset(&changes, 0, sizeof(xfrin_changes_t));
for (int i = 0; i < chsets->count; ++i) {
if ((ret = xfrin_apply_changeset(contents_copy, &changes,
&chsets->sets[i])) != KNOT_EOK) {
xfrin_rollback_update(contents_copy, &changes);
dbg_xfrin("Failed to apply changesets to zone: "
"%s\n", knot_strerror(ret));
return ret;
}
}
/*
* When all changesets are applied, set generation 1 to the copy of
* the zone so that new nodes are used instead of old ones.
*/
// knot_zone_contents_switch_generation(contents_copy);
//contents_copy->generation = 1;
knot_zone_contents_set_gen_new(contents_copy);
/*
* Finalize the zone contents.
*/
ret = xfrin_finalize_contents(contents_copy, &changes);
if (ret != KNOT_EOK) {
xfrin_rollback_update(contents_copy, &changes);
dbg_xfrin("Failed to finalize new zone contents: %s\n",
knot_strerror(ret));
return ret;
}
/*
* Switch the zone contents
*/
knot_zone_contents_t *old =
knot_zone_switch_contents(zone, contents_copy);
assert(old == old_contents);
/*
* From now on, the new contents of the zone are being used.
* References to nodes may be updated in the meantime. However, we must
* traverse the zone and fix all references that were not.
*/
/*! \todo This operation must not fail!!! .*/
ret = xfrin_fix_references(contents_copy);
assert(ret == KNOT_EOK);
// set generation to finished
knot_zone_contents_set_gen_new_finished(contents_copy);
// set generation of all nodes to the old one
// now it is safe (no old nodes should be referenced)
ret = xfrin_fix_generation(contents_copy);
assert(ret == KNOT_EOK);
/*
* Now we may also set the generation back to 0 so that another
* update is possible.
*/
knot_zone_contents_set_gen_old(contents_copy);
/*
* Wait until all readers finish reading
*/
synchronize_rcu();
/*
* Delete all old and unused data.
*/
xfrin_zone_contents_free(&old_contents);
xfrin_cleanup_update(&changes);
return KNOT_EOK;
}
|