summaryrefslogtreecommitdiff
path: root/src/libknot/zone/zone-diff.c
blob: a4efff978513250b425b08921556015cdf8de2b4 (plain)
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
/*  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 <config.h>

#include "libknot/util/debug.h"
#include "libknot/rdata.h"
#include "zone-diff.h"
#include "libknot/nameserver/name-server.h"

struct zone_diff_param {
	const knot_zone_contents_t *contents;
	char nsec3;
	knot_changeset_t *changeset;
	int ret;
};

// forward declaration
static int knot_zone_diff_rdata(const knot_rrset_t *rrset1,
                                const knot_rrset_t *rrset2,
                                knot_changeset_t *changeset);

static int knot_zone_diff_load_soas(const knot_zone_contents_t *zone1,
                                    const knot_zone_contents_t *zone2,
                                    knot_changeset_t *changeset)
{
	if (zone1 == NULL || zone2 == NULL || changeset == NULL) {
		return KNOT_EINVAL;
	}

	const knot_node_t *apex1 = knot_zone_contents_apex(zone1);
	const knot_node_t *apex2 = knot_zone_contents_apex(zone2);
	if (apex1 == NULL || apex2 == NULL) {
		dbg_zonediff("zone_diff: "
		             "both zones must have apex nodes.\n");
		return KNOT_EINVAL;
	}

	knot_rrset_t *soa_rrset1 = knot_node_get_rrset(apex1, KNOT_RRTYPE_SOA);
	knot_rrset_t *soa_rrset2 = knot_node_get_rrset(apex2, KNOT_RRTYPE_SOA);
	if (soa_rrset1 == NULL || soa_rrset2 == NULL) {
		dbg_zonediff("zone_diff: "
		             "both zones must have apex nodes.\n");
		return KNOT_EINVAL;
	}

	if (knot_rrset_rdata(soa_rrset1) == NULL ||
	    knot_rrset_rdata(soa_rrset2) == NULL) {
		dbg_zonediff("zone_diff: "
		             "both zones must have apex nodes with SOA "
		             "RRs.\n");
		return KNOT_EINVAL;
	}

	int64_t soa_serial1 =
		knot_rdata_soa_serial(knot_rrset_rdata(soa_rrset1));
	if (soa_serial1 == -1) {
		dbg_zonediff("zone_diff: load_soas: Got bad SOA.\n");
	}

	int64_t soa_serial2 =
		knot_rdata_soa_serial(knot_rrset_rdata(soa_rrset2));
	
	if (soa_serial2 == -1) {
		dbg_zonediff("zone_diff: load_soas: Got bad SOA.\n");
	}	

	if (ns_serial_compare(soa_serial1, soa_serial2) == 0) {
		dbg_zonediff("zone_diff: "
		             "second zone must have higher serial than the "
		             "first one. (%lld vs. %lld)\n",
		             soa_serial1, soa_serial2);
		return KNOT_ENODIFF;
	}
	
	if (ns_serial_compare(soa_serial1, soa_serial2) > 0) {
		dbg_zonediff("zone_diff: "
		             "second zone must have higher serial than the "
		             "first one. (%lld vs. %lld)\n",
		             soa_serial1, soa_serial2);
		return KNOT_ERANGE;
	}
	
	/* We will not touch SOA later, now is the time to handle RRSIGs. */
	int ret = knot_zone_diff_rdata(knot_rrset_rrsigs(soa_rrset1),
	                               knot_rrset_rrsigs(soa_rrset2),
	                               changeset);
	if (ret != KNOT_EOK) {
		dbg_zonediff_verb("zone_diff: load_soas: Failed to diff SOAs' RRSIGs."
		       " Reason: %s.\n", knot_strerror(ret));
		/* This might not necasarilly be an error. */
	}

	assert(changeset);

	ret = knot_rrset_deep_copy(soa_rrset1, &changeset->soa_from, 1);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: load_soas: Cannot copy RRSet.\n");
		return ret;
	}
	
	/* We MUST NOT save this RRSIG. */
	knot_rrset_deep_free(&changeset->soa_from->rrsigs, 1, 1, 1);
	assert(changeset->soa_from->rrsigs == NULL);

	ret = knot_rrset_deep_copy(soa_rrset2, &changeset->soa_to, 1);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: load_soas: Cannot copy RRSet.\n");
		return ret;
	}
	
	knot_rrset_deep_free(&changeset->soa_to->rrsigs, 1, 1, 1);
	assert(changeset->soa_to->rrsigs == NULL);
	
	changeset->serial_from = soa_serial1;
	changeset->serial_to = soa_serial2;
	
	dbg_zonediff_verb("zone_diff: load_soas: SOAs diffed. (%lld -> %lld)\n",
	            soa_serial1, soa_serial2);

	return KNOT_EOK;
}

/*!< \todo Only use add or remove function, not both as they are the same. */
/*!< \todo Also, this might be all handled by function in changesets.h!!! */
static int knot_zone_diff_changeset_add_rrset(knot_changeset_t *changeset,
                                              const knot_rrset_t *rrset)
{
	/* Remove all RRs of the RRSet. */
	if (changeset == NULL || rrset == NULL) {
		dbg_zonediff("zone_diff: add_rrset: NULL parameters.\n");
		return KNOT_EINVAL;
	}
	
	if (knot_rrset_rdata_rr_count(rrset) == 0) {
		dbg_zonediff_detail("zone_diff: Nothing to add.\n");
		return KNOT_EOK;
	}
	
	dbg_zonediff_detail("zone_diff: add_rrset: Adding RRSet (%d RRs):\n",
	              knot_rrset_rdata_rr_count(rrset));
	knot_rrset_dump(rrset, 1);
	
	knot_rrset_t *rrset_copy = NULL;
	int ret = knot_rrset_deep_copy(rrset, &rrset_copy, 1);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: add_rrset: Cannot copy RRSet.\n");
		return ret;
	}
	if (rrset_copy->rrsigs != NULL) {
		knot_rrset_deep_free(&rrset_copy->rrsigs, 1, 1, 1);
	}
	assert(knot_rrset_rrsigs(rrset_copy) == NULL);
	
	ret = knot_changeset_add_new_rr(changeset, rrset_copy,
	                                    KNOT_CHANGESET_ADD);
	if (ret != KNOT_EOK) {
		/* We have to free the copy now! */
		knot_rrset_deep_free(&rrset_copy, 1, 1, 1);
		dbg_zonediff("zone_diff: add_rrset: Could not add RRSet. "
		             "Reason: %s.\n", knot_strerror(ret));
		return ret;
	}

	return KNOT_EOK;
}

static int knot_zone_diff_changeset_remove_rrset(knot_changeset_t *changeset,
                                                 const knot_rrset_t *rrset)
{
	/* Remove all RRs of the RRSet. */
	if (changeset == NULL) {
		dbg_zonediff("zone_diff: remove_rrset: NULL parameters.\n");
		return KNOT_EINVAL;
	}
	
	if (rrset == NULL) {
		return KNOT_EOK;
	}
	
	if (knot_rrset_rdata_rr_count(rrset) == 0) {
		/* RDATA are the same, however*/
		dbg_zonediff_detail("zone_diff: Nothing to remove.\n");
		return KNOT_EOK;
	}
	
	dbg_zonediff_detail("zone_diff: remove_rrset: Removing RRSet (%d RRs):\n",
	              knot_rrset_rdata_rr_count(rrset));
	knot_rrset_dump(rrset, 1);
	
	knot_rrset_t *rrset_copy = NULL;
	int ret = knot_rrset_deep_copy(rrset, &rrset_copy, 1);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: remove_rrset: Cannot copy RRSet.\n");
		return ret;
	}
	if (rrset_copy->rrsigs != NULL) {
		knot_rrset_deep_free(&rrset_copy->rrsigs, 1, 1, 1);
	}
	
	assert(knot_rrset_rrsigs(rrset_copy) == NULL);
	
	ret = knot_changeset_add_new_rr(changeset, rrset_copy,
	                                    KNOT_CHANGESET_REMOVE);
	if (ret != KNOT_EOK) {
		/* We have to free the copy now. */
		knot_rrset_deep_free(&rrset_copy, 1, 1, 1);
		dbg_zonediff("zone_diff: remove_rrset: Could not remove RRSet. "
		             "Reason: %s.\n", knot_strerror(ret));
		return ret;
	}

	return KNOT_EOK;
}

static int knot_zone_diff_add_node(const knot_node_t *node,
                                   knot_changeset_t *changeset)
{
	if (node == NULL || changeset == NULL) {
		dbg_zonediff("zone_diff: add_node: NULL arguments.\n");
		return KNOT_EINVAL;
	}
	
	/* Add all rrsets from node. */
	const knot_rrset_t **rrsets = knot_node_rrsets(node);
	if (rrsets == NULL) {
		/* Empty non-terminals - legal case. */
		dbg_zonediff_detail("zone_diff: Node has no RRSets.\n");
		return KNOT_EOK;
	}

	for (uint i = 0; i < knot_node_rrset_count(node); i++) {
		assert(rrsets[i]);
		int ret = knot_zone_diff_changeset_add_rrset(changeset,
		                                         rrsets[i]);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: add_node: Cannot add RRSet (%s).\n",
			       knot_strerror(ret));
			free(rrsets);
			return ret;
		}
		
		if (knot_rrset_rrsigs(rrsets[i])) {
			/* Add RRSIGs of the new node. */
			ret = knot_zone_diff_changeset_add_rrset(changeset,
						knot_rrset_rrsigs(rrsets[i]));
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone_diff: add_node: Cannot "
				             "add RRSIG (%s).\n",
				       knot_strerror(ret));
				free(rrsets);
				return ret;
			}
		}
	}
	
	free(rrsets);
	
	return KNOT_EOK;
}

static int knot_zone_diff_remove_node(knot_changeset_t *changeset,
                                                const knot_node_t *node)
{
	if (changeset == NULL || node == NULL) {
		dbg_zonediff("zone_diff: remove_node: NULL parameters.\n");
		return KNOT_EINVAL;
	}
	
	dbg_zonediff("zone_diff: remove_node: Removing node:\n");
dbg_zonediff_exec_detail(
	knot_node_dump((knot_node_t *)node, 1);
);

	const knot_rrset_t **rrsets = knot_node_rrsets(node);
	if (rrsets == NULL) {
		dbg_zonediff_verb("zone_diff: remove_node: "
		                  "Nothing to remove.\n");
		return KNOT_EOK;
	}
	
	dbg_zonediff_detail("zone_diff: remove_node: Will be removing %d RRSets.\n",
	              knot_node_rrset_count(node));

	/* Remove all the RRSets of the node. */
	for (uint i = 0; i < knot_node_rrset_count(node); i++) {
		int ret = knot_zone_diff_changeset_remove_rrset(changeset,
		                                            rrsets[i]);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: remove_node: Failed to "
			             "remove rrset. Error: %s\n",
			             knot_strerror(ret));
			free(rrsets);
			return ret;
		}
		if (knot_rrset_rrsigs(rrsets[i])) {
			/* Remove RRSIGs of the old node. */
			ret = knot_zone_diff_changeset_remove_rrset(changeset,
						knot_rrset_rrsigs(rrsets[i]));
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone_diff: remove_node: Cannot "
				             "remove RRSIG (%s).\n",
				       knot_strerror(ret));
				free(rrsets);
				return ret;
			}
		}
	}
	
	free(rrsets);

	return KNOT_EOK;
}

static int knot_zone_diff_rdata_return_changes(const knot_rrset_t *rrset1,
                                               const knot_rrset_t *rrset2,
                                               knot_rrset_t **changes)
{
	if (rrset1 == NULL || rrset2 == NULL) {
		dbg_zonediff("zone_diff: diff_rdata: NULL arguments. (%p) (%p).\n",
		       rrset1, rrset2);
		return KNOT_EINVAL;
	}
	
	/*
	* Take one rdata from first list and search through the second list
	* looking for an exact match. If no match occurs, it means that this
	* particular RR has changed.
	* After the list has been traversed, we have a list of
	* changed/removed rdatas. This has awful computation time.
	*/
	dbg_zonediff_detail("zone_diff: diff_rdata: Diff of %s, type=%s. "
	              "RR count 1=%d RR count 2=%d.\n",
	              knot_dname_to_str(rrset1->owner),
	              knot_rrtype_to_string(rrset1->type),
	              knot_rrset_rdata_rr_count(rrset1),
	              knot_rrset_rdata_rr_count(rrset2));

	/* Create fake RRSet, it will be easier to handle. */
	*changes = knot_rrset_new(knot_rrset_get_owner(rrset1),
	                          knot_rrset_type(rrset1),
	                          knot_rrset_class(rrset1),
	                          knot_rrset_ttl(rrset1));
	if (*changes == NULL) {
		dbg_zonediff("zone_diff: diff_rdata: "
		             "Could not create RRSet with changes.\n");
		return KNOT_ENOMEM;
	}

	knot_rrtype_descriptor_t *desc =
		knot_rrtype_descriptor_by_type(knot_rrset_type(rrset1));
	assert(desc);

	const knot_rdata_t *tmp_rdata = knot_rrset_rdata(rrset1);
	while(tmp_rdata != NULL) {
		const knot_rdata_t *tmp_rdata_second_rrset =
			knot_rrset_rdata(rrset2);
		while ((tmp_rdata_second_rrset != NULL) &&
		       (knot_rdata_compare(tmp_rdata,
		                           tmp_rdata_second_rrset,
		                           desc->wireformat) != 0)) {
			tmp_rdata_second_rrset =
				knot_rrset_rdata_next(rrset2,
				                      tmp_rdata_second_rrset);
		}
		if (tmp_rdata_second_rrset == NULL) {
			/*
			 * This means that the while cycle above has finished
			 * because the list was traversed - there's no match.
			 */
			dbg_zonediff("zone_diff: diff_rdata: "
			       "No match for RR (type=%u owner=%s).\n",
			       knot_rrset_type(rrset1),
			       knot_dname_to_str(rrset1->owner));
			/* Make a copy of tmp_rdata. */
			knot_rdata_t *tmp_rdata_copy =
				knot_rdata_deep_copy(tmp_rdata,
			                             knot_rrset_type(rrset1),
			                             1);
			int ret = knot_rrset_add_rdata(*changes,
			                           tmp_rdata_copy);
			/*!< \todo dispose of the copy. */
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone_diff: diff_rdata: "
				             "Could not add rdata to rrset.");
				knot_rrset_deep_free(changes, 1, 1, 0);
				return ret;
			}
		} else {
			dbg_zonediff_detail("zone_diff: diff_rdata: "
			              "Found matching RR for type %s.\n",
			              knot_rrtype_to_string(rrset1->type));
		}
		tmp_rdata = knot_rrset_rdata_next(rrset1, tmp_rdata);
	}
	return KNOT_EOK;
}

static int knot_zone_diff_rdata(const knot_rrset_t *rrset1,
                                const knot_rrset_t *rrset2,
                                knot_changeset_t *changeset)
{
	if ((changeset == NULL) || (rrset1 == NULL && rrset2 == NULL)) {
		dbg_zonediff("zone_diff: diff_rdata: NULL arguments.\n");
		return KNOT_EINVAL;
	}
	/*
	 * The easiest solution is to remove all the RRs that had no match and
	 * to add all RRs that had no match, but those from second RRSet. */

	/* Get RRs to remove from zone. */
	knot_rrset_t *to_remove = NULL;
	if (rrset1 != NULL && rrset2 == NULL) {
		assert(rrset1->type == KNOT_RRTYPE_RRSIG);
		dbg_zonediff_detail("zone_diff: diff_rdata: RRSIG will be "
		              "removed.\n");
		int ret = knot_rrset_deep_copy(rrset1, &to_remove, 1);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: diff_rdata: Could not copy rrset. "
			             "Error: %s.\n", knot_strerror(ret));
			return ret;
		}
	} else if (rrset1 != NULL && rrset2 != NULL) {
		int ret = knot_zone_diff_rdata_return_changes(rrset1, rrset2,
		                                              &to_remove);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: diff_rdata: Could not get changes. "
			             "Error: %s.\n", knot_strerror(ret));
			return ret;
		}
	} else {
		dbg_zonediff("zone_diff: diff_rdata: These are not the diffs you "
		       "are looking for.\n");
	}
	
	dbg_zonediff_detail("zone_diff: diff_rdata: To remove:\n");
	knot_rrset_dump(to_remove, 1);
	
	/*
	 * to_remove RRSet might be empty, meaning that
	 * there are no differences in RDATA, but TTLs can differ.
	 */
	if (rrset1 && rrset2 && 
	    (knot_rrset_ttl(rrset1) != knot_rrset_ttl(rrset2)) &&
	    knot_rrset_rdata_rr_count(to_remove) == 0) {
		/* We have to remove old TTL. */
		assert(knot_rrset_ttl(to_remove) == knot_rrset_ttl(rrset1));
		/*
		 * Fill the RDATA so that the change gets saved. All RRs can
		 * be copied because TTLs are the same for all of them.
		 */
		knot_rdata_t *tmp_rdata_copy =
			knot_rdata_deep_copy(knot_rrset_rdata(rrset1),
		                             knot_rrset_type(rrset1),
		                             1);
		if (tmp_rdata_copy == NULL) {
			dbg_zonediff("zone diff: diff_rdata: Cannot copy "
			             "RDATA (Different TTLs).\n");
			/* TODO cleanup. */
			return KNOT_ENOMEM;
		}
		int ret = knot_rrset_add_rdata(to_remove, tmp_rdata_copy);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone diff: diff_rdata: Cannot add "
			             "RDATA to RRSet. Reason: %s\n",
			             knot_strerror(ret));
			/* TODO cleanup. */
			return ret;
		}
	}
	
	int ret = knot_zone_diff_changeset_remove_rrset(changeset,
	                                                to_remove);
	if (ret != KNOT_EOK) {
		knot_rrset_deep_free(&to_remove, 1, 1, 1);
		dbg_zonediff("zone_diff: diff_rdata: Could not remove RRs. "
		             "Error: %s.\n", knot_strerror(ret));
		return ret;
	}
	
	/* Copy was made in add_rrset function, we can free now. */
	knot_rrset_deep_free(&to_remove, 1, 1, 1);

	/* Get RRs to add to zone. */
	knot_rrset_t *to_add = NULL;
	if (rrset2 != NULL && rrset1 == NULL) {
		assert(rrset2->type == KNOT_RRTYPE_RRSIG);
		dbg_zonediff_detail("zone_diff: diff_rdata: RRSIG will be "
		              "added.\n");
		int ret = knot_rrset_deep_copy(rrset2, &to_add, 1);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: diff_rdata: Could not copy rrset. "
			             "Error: %s.\n", knot_strerror(ret));
			return ret;
		}
	} else if (rrset1 != NULL && rrset2 != NULL) {
		ret = knot_zone_diff_rdata_return_changes(rrset2, rrset1,
		                                          &to_add);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: diff_rdata: Could not get changes. "
			             "Error: %s.\n", knot_strerror(ret));
			return ret;
		}
	} else {
		dbg_zonediff("zone_diff: diff_rdata: These are not the diffs you "
		       "are looking for.\n");
	}
	
	dbg_zonediff_detail("zone_diff: diff_rdata: To add:\n");
	knot_rrset_dump(to_add, 1);
	
	/*
	 * to_remove RRSet might be empty, meaning that
	 * there are no differences in RDATA, but TTLs can differ.
	 */
	if (rrset1 && rrset2 &&
	    knot_rrset_ttl(rrset1) != knot_rrset_ttl(rrset2)) {
		/* We have to add newer TTL. */
		knot_rrset_set_ttl(to_add, knot_rrset_ttl(rrset2));
		if (knot_rrset_rdata_rr_count(to_add) == 0) {
			/*
			 * Fill the RDATA so that the change gets saved. All RRs can
			 * be copied because TTLs are the same for all of them.
			 */
			knot_rdata_t *tmp_rdata_copy =
				knot_rdata_deep_copy(knot_rrset_rdata(rrset1),
			                             knot_rrset_type(rrset1),
			                             1);
			if (tmp_rdata_copy == NULL) {
				dbg_zonediff("zone diff: diff_rdata: Cannot copy "
				             "RDATA (Different TTLs).\n");
				/* TODO cleanup. */
				return KNOT_ENOMEM;
			}
			int ret = knot_rrset_add_rdata(to_add, tmp_rdata_copy);
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone diff: diff_rdata: Cannot add "
				             "RDATA to RRSet. Reason: %s\n",
				             knot_strerror(ret));
				/* TODO cleanup. */
				return ret;
			}
		}
	}

	ret = knot_zone_diff_changeset_add_rrset(changeset,
	                                         to_add);
	if (ret != KNOT_EOK) {
		knot_rrset_deep_free(&to_add, 1, 1, 1);
		dbg_zonediff("zone_diff: diff_rdata: Could not remove RRs. "
		             "Error: %s.\n", knot_strerror(ret));	
		return ret;
	}
	
	/* Copy was made in add_rrset function, we can free now. */
	knot_rrset_deep_free(&to_add, 1, 1, 1);
	
	return KNOT_EOK;
}

static int knot_zone_diff_rrsets(const knot_rrset_t *rrset1,
                                 const knot_rrset_t *rrset2,
                                 knot_changeset_t *changeset)
{
//	if (rrset1 == NULL || rrset2 == NULL) {
//		/* This could happen when diffing RRSIGs. */
//		if (rrset1 == NULL && rrset2 != NULL) {
//			dbg_zonediff("zone_diff: diff_rrsets: RRSIG missing in first"
//			       " rrset1.\n");
//			int ret =
//				knot_zone_diff_changeset_add_rrset(changeset,
//			                                           rrset2);
//			if (ret != KNOT_EOK) {
//				dbg_zonediff("zone_diff: diff_rrsets: "
//				       "Cannot add RRSIG. (%s)\n",
//				       knot_strerror(ret));
//			}
//		} else if (rrset1 != NULL && rrset2 == NULL) {
//			dbg_zonediff("zone_diff: diff_rrsets: RRSIG missing in second"
//			       " rrset1.\n");
//			int ret =
//				knot_zone_diff_changeset_remove_rrset(changeset,
//			                                              rrset1);
//			if (ret != KNOT_EOK) {
//				dbg_zonediff("zone_diff: diff_rrsets: "
//				       "Cannot remove RRSIG. (%s)\n",
//				       knot_strerror(ret));
//			}
//		}
//		dbg_zonediff_detail("zone_diff: diff_rrsets: "
//		              "NULL arguments (RRSIGs?). (%p) (%p)\n",
//		              rrset1, rrset2);
//		return KNOT_EOK;
//	}

	assert(knot_dname_compare(knot_rrset_owner(rrset1),
	                          knot_rrset_owner(rrset2)) == 0);
	assert(knot_rrset_type(rrset1) == knot_rrset_type(rrset2));
	
	int ret = knot_zone_diff_rdata(knot_rrset_rrsigs(rrset1),
	                               knot_rrset_rrsigs(rrset2), changeset);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: diff_rrsets (%s:%s): "
		             "Failed to diff RRSIGs. "
		             "They were: %p %p. (%s).\n",
		             knot_dname_to_str(rrset1->owner),
		             knot_rrtype_to_string(rrset1->type),
		             rrset1->rrsigs,
		             rrset2->rrsigs, knot_strerror(ret));
	}

	/* RRs (=rdata) have to be cross-compared, unfortunalely. */
	return knot_zone_diff_rdata(rrset1, rrset2, changeset);
}

/*!< \todo this could be generic function for adding / removing. */
static void knot_zone_diff_node(knot_node_t *node, void *data)
{
	if (node == NULL || data == NULL) {
		dbg_zonediff("zone_diff: diff_node: NULL arguments.\n");
		return;
	}

	struct zone_diff_param *param = (struct zone_diff_param *)data;
	if (param->changeset == NULL || param->contents == NULL) {
		dbg_zonediff("zone_diff: diff_node: NULL arguments.\n");
		param->ret = KNOT_EINVAL;
		return;
	}

	if (param->ret != KNOT_EOK) {
		/* Error occured before, no point in continuing. */
		dbg_zonediff_detail("zone_diff: diff_node: error: %s\n",
		                    knot_strerror(param->ret));
		return;
	}

	/*
	 * First, we have to search the second tree to see if there's according
	 * node, if not, the whole node has been removed.
	 */
	const knot_node_t *node_in_second_tree = NULL;
	const knot_dname_t *node_owner = knot_node_owner(node);
	assert(node_owner);
	if (!param->nsec3) {
		node_in_second_tree =
			knot_zone_contents_find_node(param->contents,
			                             node_owner);
	} else {
		dbg_zonediff_verb("zone_diff: diff_node: NSEC3 zone.\n");
		node_in_second_tree =
			knot_zone_contents_find_nsec3_node(param->contents,
			                                   node_owner);
	}

	if (node_in_second_tree == NULL) {
		dbg_zonediff_detail("zone_diff: diff_node: Node %s is not "
		              "in the second tree.\n",
		              knot_dname_to_str(node_owner));
		int ret = knot_zone_diff_remove_node(param->changeset,
		                                               node);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: failed to remove node.\n");
			param->ret = ret;
			return;
		}
		param->ret = KNOT_EOK;
		return;
	}
	
	assert(node_in_second_tree != node);

	dbg_zonediff_detail("zone_diff: diff_node: Node %s is present in "
	              "both trees.\n", knot_dname_to_str(node_owner));
	/* The nodes are in both trees, we have to diff each RRSet. */
	const knot_rrset_t **rrsets = knot_node_rrsets(node);
	if (rrsets == NULL) {
		dbg_zonediff("zone_diff: Node in first tree has no RRSets.\n");
		/*
		 * If there are no RRs in the first tree, all of the RRs 
		 * in the second tree will have to be inserted to ADD section.
		 */
		int ret = knot_zone_diff_add_node(node_in_second_tree,
		                                  param->changeset);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: diff_node: "
			             "Could not add node from second tree. "
			             "Reason: %s.\n", knot_strerror(ret));
		}
		param->ret = ret;
		return;
	}

	for (uint i = 0; i < knot_node_rrset_count(node); i++) {
		/* Search for the RRSet in the node from the second tree. */
		const knot_rrset_t *rrset = rrsets[i];
		assert(rrset);
		
		/* SOAs are handled explicitly. */
		if (knot_rrset_type(rrset) == KNOT_RRTYPE_SOA) {
			continue;
		}
		
		const knot_rrset_t *rrset_from_second_node =
			knot_node_rrset(node_in_second_tree,
			                knot_rrset_type(rrset));
		if (rrset_from_second_node == NULL) {
			dbg_zonediff("zone_diff: diff_node: There is no counterpart "
			       "for RRSet of type %s in second tree.\n",
			       knot_rrtype_to_string(knot_rrset_type(rrset)));
			/* RRSet has been removed. Make a copy and remove. */
			assert(rrset);
			int ret = knot_zone_diff_changeset_remove_rrset(
				param->changeset,
				rrset);
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone_diff: diff_node: "
				             "Failed to remove RRSet.\n");
				param->ret = ret;
				free(rrsets);
				return;
			}
			
			/* Remove RRSet's RRSIGs as well. */
			if (knot_rrset_rrsigs(rrset)) {
				ret = knot_zone_diff_changeset_remove_rrset(
				            param->changeset,
				            knot_rrset_rrsigs(rrset));
				if (ret != KNOT_EOK) {
				    dbg_zonediff("zone_diff: diff_node+: "
				                 "Failed to remove RRSIGs.\n");
				    param->ret = ret;
				    free(rrsets);
				    return;
				}
			}
		} else {
			dbg_zonediff("zone_diff: diff_node: There is a counterpart "
			       "for RRSet of type %s in second tree.\n",
			       knot_rrtype_to_string(knot_rrset_type(rrset)));
			/* Diff RRSets. */
			int ret = knot_zone_diff_rrsets(rrset,
			                                rrset_from_second_node,
			                                param->changeset);
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone_diff: "
				             "Failed to diff RRSets.\n");
				param->ret = ret;
				free(rrsets);
				return;
			}
			
//			dbg_zonediff_verb("zone_diff: diff_node: Changes in "
//			            "RRSIGs.\n");
//			/*! \todo There is ad-hoc solution in the function, maybe handle here. */
//			ret = knot_zone_diff_rrsets(rrset->rrsigs,
//			                                rrset_from_second_node->rrsigs,
//			                                param->changeset);
//			if (ret != KNOT_EOK) {
//				dbg_zonediff("zone_diff: "
//				             "Failed to diff RRSIGs.\n");
//				param->ret = ret;
//				return;
//			}
		}
	}
	
	free(rrsets);
	
	/*! \todo move to one function with the code above. */
	rrsets = knot_node_rrsets(node_in_second_tree);
	if (rrsets == NULL) {
		dbg_zonediff("zone_diff: Node in second tree has no RRSets.\n");
		/*
		 * This can happen when node in second 
		 * tree is empty non-terminal and as such has no RRs.
		 * Whole node from the first tree has to be removed.
		 */
		// TODO following code creates duplicated RR in diff.
		// IHMO such case should be handled here
//		int ret = knot_zone_diff_remove_node(param->changeset,
//		                                     node);
//		if (ret != KNOT_EOK) {
//			dbg_zonediff("zone_diff: diff_node: "
//			             "Cannot remove node. Reason: %s.\n",
//			             knot_strerror(ret));
//		}
		param->ret = KNOT_EOK;
		return;
	}

	for (uint i = 0; i < knot_node_rrset_count(node_in_second_tree); i++) {
		/* Search for the RRSet in the node from the second tree. */
		const knot_rrset_t *rrset = rrsets[i];
		assert(rrset);
		
		/* SOAs are handled explicitly. */
		if (knot_rrset_type(rrset) == KNOT_RRTYPE_SOA) {
			continue;
		}
		
		const knot_rrset_t *rrset_from_first_node =
			knot_node_rrset(node,
			                knot_rrset_type(rrset));
		if (rrset_from_first_node == NULL) {
			dbg_zonediff("zone_diff: diff_node: There is no counterpart "
			       "for RRSet of type %s in first tree.\n",
			       knot_rrtype_to_string(knot_rrset_type(rrset)));
			/* RRSet has been added. Make a copy and add. */
			assert(rrset);
			int ret = knot_zone_diff_changeset_add_rrset(
				param->changeset,
				rrset);
			if (ret != KNOT_EOK) {
				dbg_zonediff("zone_diff: diff_node: "
				             "Failed to add RRSet.\n");
				param->ret = ret;
				free(rrsets);
				return;
			}
			if (knot_rrset_rrsigs(rrset)) {
				int ret = knot_zone_diff_changeset_add_rrset(
			        param->changeset,
			         knot_rrset_rrsigs(rrset));
			   if (ret != KNOT_EOK) {
			     dbg_zonediff("zone_diff: diff_node: "
			            "Failed to add RRSIGs.\n");
			    param->ret = ret;
					free(rrsets);
				return;	
			 }
			}
		} else {
			/* Already handled. */
			;
		}
	}
	
	free(rrsets);

	assert(param->ret == KNOT_EOK);
}

/*!< \todo possibly not needed! */
static void knot_zone_diff_add_new_nodes(knot_node_t *node, void *data)
{
	assert(node);
	if (node == NULL || data == NULL) {
		dbg_zonediff("zone_diff: add_new_nodes: NULL arguments.\n");
		return;
	}

	struct zone_diff_param *param = (struct zone_diff_param *)data;
	if (param->changeset == NULL || param->contents == NULL) {
		dbg_zonediff("zone_diff: add_new_nodes: NULL arguments.\n");
		param->ret = KNOT_EINVAL;
		return;
	}

	if (param->ret != KNOT_EOK) {
		/* Error occured before, no point in continuing. */
		dbg_zonediff_detail("zone_diff: add_new_nodes: error: %s\n",
		                    knot_strerror(param->ret));
		return;
	}
	
	/*
	* If a node is not present in the second zone, it is a new node
	* and has to be added to changeset. Differencies on the RRSet level are
	* already handled.
	*/
	const knot_zone_contents_t *other_zone = param->contents;
	assert(other_zone);
	
	const knot_dname_t *node_owner = knot_node_owner(node);
	/*
	 * Node should definitely have an owner, otherwise it would not be in
	 * the tree.
	 */
	assert(node_owner);
	
	knot_node_t *new_node = NULL;
	if (!param->nsec3) {
		new_node = knot_zone_contents_get_node(other_zone, node_owner);
	} else {
		new_node = knot_zone_contents_get_nsec3_node(other_zone,
		                                             node_owner);
	}
	
	if (!new_node) {
		assert(node);
		int ret = knot_zone_diff_add_node(node, param->changeset);
		if (ret != KNOT_EOK) {
			dbg_zonediff("zone_diff: add_new_nodes: Cannot add "
			             "node: %s to changeset. Reason: %s.\n",
			             knot_dname_to_str(node->owner),
			             knot_strerror(ret));
		}
	}
	
	assert(param->ret == KNOT_EOK);
}

int knot_zone_contents_diff(const knot_zone_contents_t *zone1,
                            const knot_zone_contents_t *zone2,
                            knot_changeset_t *changeset)
{
	if (zone1 == NULL || zone2 == NULL) {
		dbg_zonediff("zone_diff: NULL argument(s).\n");
		return KNOT_EINVAL;
	}

//	/* Create changeset structure. */
//	*changeset = malloc(sizeof(knot_changeset_t));
//	if (*changeset == NULL) {
//		ERR_ALLOC_FAILED;
//		return KNOT_ENOMEM;
//	}
	memset(changeset, 0, sizeof(knot_changeset_t));

	/* Settle SOAs first. */
	int ret = knot_zone_diff_load_soas(zone1, zone2, changeset);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: loas_SOAs failed with error: %s\n",
		             knot_strerror(ret));
		return ret;
	}
	
	dbg_zonediff("zone_diff: SOAs loaded.\n");
	
	/* Traverse one tree, compare every node, each RRSet with its rdata. */
	struct zone_diff_param param;
	param.contents = zone2;
	param.nsec3 = 0;
	param.changeset = changeset;
	param.ret = KNOT_EOK;
	ret = knot_zone_contents_tree_apply_inorder(
	                        (knot_zone_contents_t *)zone1,
	                        knot_zone_diff_node,
	                        &param);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: Tree traversal failed "
		             "with error: %s. Error from inner function: %s\n",
		             knot_strerror(ret),
		             knot_strerror(param.ret));
		return ret;
	}

	/* Do the same for NSEC3 nodes. */
	param.nsec3 = 1;
	ret = knot_zone_contents_nsec3_apply_inorder((knot_zone_contents_t *)zone1, knot_zone_diff_node,
	                                             &param);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: Tree traversal failed "
		             "with error: %s\n",
		             knot_strerror(ret));
		return ret;
	}

	/*
	 * Some nodes may have been added. The code above will not notice,
	 * we have to go through the second tree and add missing nodes to
	 * changeset.
	 */
	param.nsec3 = 0;
	param.contents = zone1;
	ret = knot_zone_contents_tree_apply_inorder((knot_zone_contents_t *)zone2,
		knot_zone_diff_add_new_nodes,
		&param);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: Tree traversal failed "
		             "with error: %s. Error from inner function: %s\n",
		             knot_strerror(ret),
		             knot_strerror(param.ret));
		return ret;
	}

	/* NSEC3 nodes. */
	param.nsec3 = 1;
	param.contents = zone1;
	ret = knot_zone_contents_nsec3_apply_inorder((knot_zone_contents_t *)zone2,
		knot_zone_diff_add_new_nodes,
		&param);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: Tree traversal failed "
		             "with error: %s\n",
		             knot_strerror(ret));
		return ret;
	}

	return KNOT_EOK;
}

#ifdef KNOT_ZONEDIFF_DEBUG
#ifdef DEBUG_ENABLE_DETAILS
static void knot_zone_diff_dump_changeset(knot_changeset_t *ch)
{
	dbg_zonediff_detail("Changeset FROM: %d\n", ch->serial_from);
	rrset_dump_text(ch->soa_from, stderr);
	dbg_zonediff_detail("\n");
	dbg_zonediff_detail("Changeset TO: %d\n", ch->serial_to);
	rrset_dump_text(ch->soa_to, stderr);
	dbg_zonediff_detail("\n");
	dbg_zonediff_detail("Adding %d RRs.\n", ch->add_count);
	dbg_zonediff_detail("Removing %d RRs.\n", ch->remove_count);
	
	dbg_zonediff_detail("ADD section:\n");
	dbg_zonediff_detail("**********************************************\n");
	for (int i = 0; i < ch->add_count; i++) {
		rrset_dump_text(ch->add[i], stderr);
		dbg_zonediff_detail("\n");
	}
	dbg_zonediff_detail("REMOVE section:\n");
	dbg_zonediff_detail("**********************************************\n");
	for (int i = 0; i < ch->remove_count; i++) {
		rrset_dump_text(ch->remove[i], stderr);
		dbg_zonediff_detail("\n");
	}
}
#endif
#endif

int knot_zone_diff_create_changesets(const knot_zone_contents_t *z1,
                                     const knot_zone_contents_t *z2,
                                     knot_changesets_t **changesets)
{
	if (z1 == NULL || z2 == NULL) {
		dbg_zonediff("zone_diff: create_changesets: NULL arguments.\n");
		return KNOT_EINVAL;
	}
	/* Create changesets. */
	/* Setting type to IXFR - that's the default, DDNS triggers special
	 * processing when applied. See #2110 and #2111.
	 */
	int ret = knot_changeset_allocate(changesets, KNOT_CHANGESET_TYPE_IXFR);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: create_changesets: "
		             "Could not allocate changesets."
		             "Reason: %s.\n", knot_strerror(ret));
		return ret;
	}
	
	memset((*changesets)->sets, 0, sizeof(knot_changeset_t));
	
	ret = knot_zone_contents_diff(z1, z2, (*changesets)->sets);
	if (ret != KNOT_EOK) {
		dbg_zonediff("zone_diff: create_changesets: "
		             "Could not diff zones. "
		             "Reason: %s.\n", knot_strerror(ret));
		return ret;
	}
	
	(*changesets)->count = 1;
	
	dbg_zonediff("Changesets created successfully!\n");
	dbg_zonediff_detail("Changeset dump:\n");
dbg_zonediff_exec_detail(
	knot_zone_diff_dump_changeset((*changesets)->sets);
);
	return KNOT_EOK;
}

///* Mostly just for testing. We only shall diff zones in memory later. */
//int knot_zone_diff_zones(const char *zonefile1, const char *zonefile2)
//{
//	/* Compile test zones. */
//	int ret = zone_read("example.com.", "/home/jan/test/testzone1", "tmpzone1.db", 0);
//	assert(ret == KNOT_EOK);
//	ret = zone_read("example.com.", "/home/jan/test/testzone2", "tmpzone2.db", 0);
//	assert(ret == KNOT_EOK);
//	/* Load test zones. */
//	zloader_t *loader = NULL;
//	int ret = knot_zload_open(&loader, "tmpzone1.db");
//	assert(ret == KNOT_EOK);
//	knot_zone_t *z1 = knot_zload_load(loader);
//	ret = knot_zload_open(&loader, "tmpzone2.db");
//	assert(ret == KNOT_EOK);
//	knot_zone_t *z2 = knot_zload_load(loader);
//	assert(z1 && z2);
//	knot_changeset_t *changeset = malloc(sizeof(knot_changeset_t));
//	memset(changeset, 0, sizeof(knot_changeset_t));
//	assert(knot_zone_contents_diff(z1->contents, z2->contents,
//	                               changeset) == KNOT_EOK);
//	dbg_zonediff("Changeset created: From=%d to=%d.\n", changeset.serial_from,
//	             changeset.serial_to);
//	//	knot_changesets_t chngsets;
//	//	chngsets->sets = malloc(sizeof(knot_changeset_t));
//	//	chngsets->sets[0] = changeset;
//	//	chngsets->count = 1;
//	//	chngsets->allocated = 1;
//	//	knot_zone_contents_t *new_zone = NULL;
//	//	ret = xfrin_apply_changesets(z1, chngsets, &new_zone);
//	//	if (ret != KNOT_EOK) {
//	//		dbg_zonediff("Application of changesets failed. (%s)\n",
//	//		       knot_strerror(ret));
//	//	}
	
//	//	assert(new_zone);
	
//	/* Dump creted zone. */
//	//	FILE *f = fopen("testovani", "w");
//	//	zone_dump_text(new_zone, f);
	
//	knot_zone_deep_free(&z2, 0);
//	knot_zone_deep_free(&z1, 0);
//	//	knot_zone_contents_deep_free(&new_zone, 1);
//	//	knot_zone_free(&z1);
	
//	knot_free_changeset(&changeset);
//	exit(0);
//}