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
|
/* 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 <config.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#include "rdata.h"
#include "util/descriptor.h"
#include "dname.h"
#include "util/error.h"
#include "zone/node.h"
#include "util/utils.h"
#include "util/debug.h"
/*----------------------------------------------------------------------------*/
/* Non-API functions */
/*----------------------------------------------------------------------------*/
/*!
* \brief Compares two RDATA items as binary data.
*
* \param d1 First item.
* \param d2 Second item.
* \param count1 Size of the first item in bytes. If set to < 0, the size will
* be taken from the first two bytes of \a d1.
* \param count2 Size of the second item in bytes. If set to < 0, the size will
* be taken from the first two bytes of \a d2.
*
* \retval 0 if the items are identical.
* \retval < 0 if \a d1 goes before \a d2 in canonical order.
* \retval > 0 if \a d1 goes after \a d2 in canonical order.
*/
static int knot_rdata_compare_binary(const uint8_t *d1, const uint8_t *d2,
int count1, int count2)
{
int i1 = 0, i2 = 0;
// length stored in the first octet
if (count1 < 0) {
// take count from the first two bytes
count1 = (int)(*(uint16_t *)d1);
// and start from the third byte
i1 = 2;
}
if (count2 < 0) { // dtto
count2 = (int)(*(uint16_t *)d2);
i2 = 2;
}
while (i1 < count1 && i2 < count2 && d1[i1] == d2[i2]) {
++i1;
++i2;
}
if (i1 == count1 && i2 == count2) {
return 0;
}
if (i1 == count1 && i2 < count2) {
return -1;
} else if (i2 == count2 && i1 < count1) {
return 1;
} else {
assert(i1 < count1 && i2 < count2);
return (d1[i1] < d2[i2]) ? -1 : 1;
}
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Retrieves the domain name from MX RDATA.
*
* \note This is only convenience function. It does not (and cannot) check if
* the given RDATA is of the right type, so it always returns the second
* RDATA item, even if it is not a domain name.
*
* \param rdata RDATA to get the MX domain name from.
*
* \return MX domain name stored in \a rdata or NULL if \a rdata has less than 2
* items.
*/
static const knot_dname_t *knot_rdata_mx_name(const knot_rdata_t *rdata)
{
if (rdata->count < 2) {
return NULL;
}
return rdata->items[1].dname;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Retrieves the domain name from NS RDATA.
*
* \note This is only convenience function. It does not (and cannot) check if
* the given RDATA is of the right type, so it always returns the first
* RDATA item, even if it is not a domain name.
*
* \param rdata RDATA to get the NS domain name from.
*
* \return NS domain name stored in \a rdata or NULL if \a rdata has no items.
*/
static const knot_dname_t *knot_rdata_ns_name(const knot_rdata_t *rdata)
{
if (rdata->count < 1) {
return NULL;
}
return rdata->items[0].dname;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief Retrieves the domain name from SRV RDATA.
*
* \note This is only convenience function. It does not (and cannot) check if
* the given RDATA is of the right type, so it always returns the fourth
* RDATA item, even if it is not a domain name.
*
* \param rdata RDATA to get the SRV domain name from.
*
* \return SRV domain name stored in \a rdata or NULL if \a rdata has less than
* 4 items.
*/
static const knot_dname_t *knot_rdata_srv_name(const knot_rdata_t *rdata)
{
if (rdata->count < 4) {
return NULL;
}
return rdata->items[3].dname;
}
/*----------------------------------------------------------------------------*/
static void knot_rdata_free_items(knot_rdata_item_t *items, unsigned int count,
uint type, int free_all_dnames)
{
if (items == NULL) {
return;
}
knot_rrtype_descriptor_t *desc = knot_rrtype_descriptor_by_type(type);
assert(desc != NULL);
assert(count <= desc->length);
for (int i = 0; i < count; i++) {
if (&(items[i]) == NULL) {
continue;
}
if (desc->wireformat[i] == KNOT_RDATA_WF_COMPRESSED_DNAME
|| desc->wireformat[i] == KNOT_RDATA_WF_UNCOMPRESSED_DNAME
|| desc->wireformat[i] == KNOT_RDATA_WF_LITERAL_DNAME) {
if ((items[i].dname != NULL)) {
/*! \todo This is hack to prevent memory errors,
* as the rdata_set_items() cannot determine
* items type and so cannot increment
* reference count in case of dname type.
* Free would then release dnames that
* aren't referenced by the rdata.
*/
if (free_all_dnames) {
knot_dname_release(items[i].dname);
}
}
} else {
free(items[i].raw_data);
}
}
free(items);
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
knot_rdata_t *knot_rdata_new()
{
knot_rdata_t *rdata =
(knot_rdata_t *)malloc(sizeof(knot_rdata_t));
if (rdata == NULL) {
ERR_ALLOC_FAILED;
return NULL;
}
rdata->items = NULL;
rdata->count = 0;
rdata->next = NULL;
return rdata;
}
/*----------------------------------------------------------------------------*/
int knot_rdata_from_wire(knot_rdata_t *rdata, const uint8_t *wire,
size_t *pos, size_t total_size, size_t rdlength,
const knot_rrtype_descriptor_t *desc)
{
int i = 0;
uint8_t item_type;
size_t parsed = 0;
if (rdlength == 0) {
rdata->items = NULL;
return KNOT_EOK;
}
knot_rdata_item_t *items = (knot_rdata_item_t *)malloc(
desc->length * sizeof(knot_rdata_item_t));
CHECK_ALLOC_LOG(items, KNOT_ENOMEM);
size_t item_size = 0;
uint8_t gateway_type = 0; // only to handle IPSECKEY record
knot_dname_t *dname = NULL;
while (i < desc->length && (desc->fixed_items || parsed < rdlength)) {
item_type = desc->wireformat[i];
item_size = 0;
size_t pos2;
switch (item_type) {
case KNOT_RDATA_WF_COMPRESSED_DNAME:
case KNOT_RDATA_WF_UNCOMPRESSED_DNAME:
case KNOT_RDATA_WF_LITERAL_DNAME:
pos2 = *pos;
dname = knot_dname_parse_from_wire(
wire, &pos2, total_size, NULL);
if (dname == NULL) {
free(items);
return KNOT_ERROR;
}
items[i].dname = dname;
//*pos += dname->size;
parsed += pos2 - *pos;
*pos = pos2;
dname = 0;
break;
case KNOT_RDATA_WF_BYTE:
if (desc->type == KNOT_RRTYPE_IPSECKEY && i == 1) {
gateway_type = *(wire + *pos);
}
item_size = 1;
break;
case KNOT_RDATA_WF_SHORT:
item_size = 2;
break;
case KNOT_RDATA_WF_LONG:
item_size = 4;
break;
case KNOT_RDATA_WF_UINT48:
item_size = 6;
break;
case KNOT_RDATA_WF_TEXT:
item_size = rdlength - parsed;
break;
case KNOT_RDATA_WF_TEXT_SINGLE:
item_size = *(wire + *pos) + 1;
break;
case KNOT_RDATA_WF_A:
item_size = 4;
break;
case KNOT_RDATA_WF_AAAA:
item_size = 16;
break;
case KNOT_RDATA_WF_BINARY:
item_size = rdlength - parsed;
break;
case KNOT_RDATA_WF_BINARYWITHLENGTH:
item_size = *(wire + *pos) + 1;
break;
case KNOT_RDATA_WF_BINARYWITHSHORT:
item_size = knot_wire_read_u16(wire + *pos) + 2;
break;
case KNOT_RDATA_WF_APL:
// WTF? what to do with this??
// Same as TXT, I guess.
item_size = rdlength - parsed;
break;
case KNOT_RDATA_WF_IPSECGATEWAY:
// determine size based on the 'gateway type' field
switch (gateway_type) {
case 0:
item_size = 0;
break;
case 1:
item_size = 4;
break;
case 2:
item_size = 16;
break;
case 3:
pos2 = *pos;
//fprintf(stderr, "reading dname from pos: %zu\n", pos2);
dname = knot_dname_parse_from_wire(
wire, &pos2, total_size, NULL);
if (dname == NULL) {
knot_rdata_free_items(items, i,
desc->type, 1);
return KNOT_ERROR;
}
items[i].raw_data = (uint16_t *)malloc(
knot_dname_size(dname) + 2);
if (items[i].raw_data == NULL) {
knot_dname_free(&dname);
knot_rdata_free_items(items, i,
desc->type, 1);
return KNOT_ENOMEM;
}
items[i].raw_data[0] = knot_dname_size(dname);
memcpy((uint8_t *)(items[i].raw_data + 1),
knot_dname_name(dname),
knot_dname_size(dname));
// items[i].dname = dname;
//*pos += dname->size;
parsed += pos2 - *pos;
//fprintf(stderr, "read %zu bytes.\n", parsed);
*pos = pos2;
knot_dname_free(&dname);
// continue with next item
++i;
continue;
break;
default:
assert(0);
}
break;
default:
knot_rdata_free_items(items, i,
desc->type, 1);
return KNOT_EMALF;
}
if (item_size != 0) {
if (parsed + item_size > rdlength) {
knot_rdata_free_items(items, i,
desc->type, 1);
return KNOT_EFEWDATA;
}
items[i].raw_data = (uint16_t *)malloc(item_size + 2);
if (items[i].raw_data == NULL) {
knot_rdata_free_items(items, i,
desc->type, 1);
return KNOT_ENOMEM;
}
memcpy(items[i].raw_data, &item_size, 2);
memcpy(items[i].raw_data + 1, wire + *pos, item_size);
*pos += item_size;
parsed += item_size;
} else if (item_type == KNOT_RDATA_WF_BINARY/*
|| item_type == KNOT_RDATA_WF_IPSECGATEWAY*/) {
// fprintf(stderr, "item_size was 0, creating empty rdata item.\n");
// in this case we are at the end of the RDATA
// and should create an empty RDATA item
items[i].raw_data = (uint16_t *)malloc(2);
if (items[i].raw_data == NULL) {
knot_rdata_free_items(items, i,
desc->type, 1);
return KNOT_ENOMEM;
}
memcpy(items[i].raw_data, &item_size, 2);
} else if (item_type != KNOT_RDATA_WF_COMPRESSED_DNAME
&& item_type != KNOT_RDATA_WF_UNCOMPRESSED_DNAME
&& item_type != KNOT_RDATA_WF_LITERAL_DNAME) {
// fprintf(stderr, "RDATA item not set (i: %d), type: %u"
// " RDATA item type: %d\n", i, desc->type ,item_type);
assert(0);
}
++i;
}
assert(!desc->fixed_items || i == desc->length);
// all items are parsed, insert into the RDATA
int rc;
rc = knot_rdata_set_items(rdata, items, i);
for (int j = 0; j < i; ++j) {
assert(rdata->items[j].raw_data != NULL);
}
free(items);
return rc;
}
/*----------------------------------------------------------------------------*/
int knot_rdata_set_item(knot_rdata_t *rdata, uint pos,
knot_rdata_item_t item)
{
if (pos >= rdata->count) {
return KNOT_EBADARG;
}
/*! \todo As in set_items() we should increment refcounter for dnames,
* but we don't know the item type.
*/
rdata->items[pos] = item; // this should copy the union; or use memcpy?
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
unsigned int knot_rdata_item_count(const knot_rdata_t *rdata)
{
return rdata->count;
}
/*----------------------------------------------------------------------------*/
int knot_rdata_set_items(knot_rdata_t *rdata,
const knot_rdata_item_t *items, uint count)
{
if (rdata == NULL || items == NULL || count == 0 ||
rdata->items != NULL) {
return KNOT_EBADARG;
}
assert(rdata->count == 0);
if ((rdata->items = (knot_rdata_item_t *)malloc(
count * sizeof(knot_rdata_item_t))) == NULL) {
ERR_ALLOC_FAILED;
return KNOT_ENOMEM;
}
memcpy(rdata->items, items, count * sizeof(knot_rdata_item_t));
rdata->count = count;
/*! \todo Cannot determine items type, so the dname
* refcounters should be increased in caller.
*/
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
const knot_rdata_item_t *knot_rdata_item(const knot_rdata_t *rdata,
uint pos)
{
if (pos >= rdata->count) {
return NULL;
} else {
return &rdata->items[pos];
}
}
/*----------------------------------------------------------------------------*/
knot_rdata_item_t *knot_rdata_get_item(const knot_rdata_t *rdata,
uint pos)
{
if (pos >= rdata->count) {
return NULL;
} else {
return &rdata->items[pos];
}
}
/*----------------------------------------------------------------------------*/
int knot_rdata_item_set_dname(knot_rdata_t *rdata, uint pos,
knot_dname_t *dname)
{
if (pos >= rdata->count) {
return KNOT_EBADARG;
}
/* Retain dname. */
knot_dname_retain(dname);
rdata->items[pos].dname = dname;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int knot_rdata_item_set_raw_data(knot_rdata_t *rdata, uint pos,
uint16_t *raw_data)
{
if (pos >= rdata->count) {
return KNOT_EBADARG;
}
rdata->items[pos].raw_data = raw_data;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
void knot_rdata_free(knot_rdata_t **rdata)
{
if (rdata == NULL || *rdata == NULL) {
return;
}
if ((*rdata)->items) {
free((*rdata)->items);
}
free(*rdata);
*rdata = NULL;
}
/*----------------------------------------------------------------------------*/
void knot_rdata_deep_free(knot_rdata_t **rdata, uint type,
int free_all_dnames)
{
if (rdata == NULL || *rdata == NULL) {
return;
}
knot_rdata_free_items((*rdata)->items, (*rdata)->count, type,
free_all_dnames);
free(*rdata);
*rdata = NULL;
}
/*----------------------------------------------------------------------------*/
/* CLEANUP */
//uint knot_rdata_wire_size(const knot_rdata_t *rdata,
// const uint8_t *format)
//{
// uint size = 0;
// for (int i = 0; i < rdata->count; ++i) {
// switch (format[i]) {
// case KNOT_RDATA_WF_COMPRESSED_DNAME:
// case KNOT_RDATA_WF_UNCOMPRESSED_DNAME:
// case KNOT_RDATA_WF_LITERAL_DNAME:
// size += knot_dname_size(rdata->items[i].dname);
// break;
// case KNOT_RDATA_WF_BYTE:
// size += 1;
// break;
// case KNOT_RDATA_WF_SHORT:
// size += 2;
// break;
// case KNOT_RDATA_WF_LONG:
// size += 4;
// break;
// case KNOT_RDATA_WF_A:
// size += 4;
// break;
// case KNOT_RDATA_WF_AAAA:
// size += 16;
// break;
// case KNOT_RDATA_WF_BINARY:
// case KNOT_RDATA_WF_APL: // saved as binary
// case KNOT_RDATA_WF_IPSECGATEWAY: // saved as binary
// size += rdata->items[i].raw_data[0];
// break;
// case KNOT_RDATA_WF_TEXT:
// case KNOT_RDATA_WF_BINARYWITHLENGTH:
// size += rdata->items[i].raw_data[0] + 1;
// break;
// default:
// assert(0);
// }
// }
// return size;
//}
/*----------------------------------------------------------------------------*/
//int knot_rdata_to_wire(const knot_rdata_t *rdata, const uint8_t *format,
// uint8_t *buffer, uint buf_size)
//{
// uint copied = 0;
// uint8_t tmp[KNOT_MAX_RDATA_WIRE_SIZE];
// uint8_t *to = tmp;
// for (int i = 0; i < rdata->count; ++i) {
// assert(copied < KNOT_MAX_RDATA_WIRE_SIZE);
// const uint8_t *from = (uint8_t *)rdata->items[i].raw_data;
// uint size = 0;
// switch (format[i]) {
// case KNOT_RDATA_WF_COMPRESSED_DNAME:
// case KNOT_RDATA_WF_UNCOMPRESSED_DNAME:
// case KNOT_RDATA_WF_LITERAL_DNAME:
// size = knot_dname_size(rdata->items[i].dname);
// from = knot_dname_name(rdata->items[i].dname);
// break;
// case KNOT_RDATA_WF_BYTE:
// size = 1;
// break;
// case KNOT_RDATA_WF_SHORT:
// size = 2;
// break;
// case KNOT_RDATA_WF_LONG:
// size = 4;
// break;
// case KNOT_RDATA_WF_A:
// size = 4;
// break;
// case KNOT_RDATA_WF_AAAA:
// size = 16;
// break;
// case KNOT_RDATA_WF_TEXT:
// case KNOT_RDATA_WF_BINARYWITHLENGTH:
// // size stored in the first two bytes, but in little
// // endian and we need only the lower byte from it
// *to = *from; // lower byte is the first in little endian
// to += 1;
// case KNOT_RDATA_WF_BINARY:
// case KNOT_RDATA_WF_APL: // saved as binary
// case KNOT_RDATA_WF_IPSECGATEWAY: // saved as binary
// // size stored in the first two bytes, those bytes
// // must not be copied
// size = rdata->items[i].raw_data[0];
// from += 2; // skip the first two bytes
// break;
// default:
// assert(0);
// }
// assert(size != 0);
// assert(copied + size < KNOT_MAX_RDATA_WIRE_SIZE);
// memcpy(to, from, size);
// to += size;
// copied += size;
// }
// if (copied > buf_size) {
// dbg_rdata("Not enough place allocated for function "
// "knot_rdata_to_wire(). Allocated %u, need %u\n",
// buf_size, copied);
// return -1;
// }
// memcpy(buffer, tmp, copied);
// return 0;
//}
/*----------------------------------------------------------------------------*/
knot_rdata_t *knot_rdata_deep_copy(const knot_rdata_t *rdata,
uint16_t type, int copy_dnames)
{
knot_rdata_t *copy = knot_rdata_new();
CHECK_ALLOC_LOG(copy, NULL);
if ((copy->items = (knot_rdata_item_t *)malloc(
rdata->count * sizeof(knot_rdata_item_t))) == NULL) {
knot_rdata_free(©);
ERR_ALLOC_FAILED;
return NULL;
}
copy->count = rdata->count;
knot_rrtype_descriptor_t *d = knot_rrtype_descriptor_by_type(type);
assert(copy->count <= d->length);
// copy all items one by one
for (int i = 0; i < copy->count; ++i) {
if (d->wireformat[i] == KNOT_RDATA_WF_COMPRESSED_DNAME
|| d->wireformat[i] == KNOT_RDATA_WF_UNCOMPRESSED_DNAME
|| d->wireformat[i] == KNOT_RDATA_WF_LITERAL_DNAME) {
if (copy_dnames) {
copy->items[i].dname =
knot_dname_deep_copy(rdata->items[i].dname);
} else {
copy->items[i].dname = rdata->items[i].dname;
knot_dname_retain(rdata->items[i].dname);
}
} else {
copy->items[i].raw_data = (uint16_t *)malloc(
rdata->items[i].raw_data[0] + 2);
if (copy->items[i].raw_data == NULL) {
knot_rdata_deep_free(©, type, 1);
return NULL;
}
memcpy(copy->items[i].raw_data,
rdata->items[i].raw_data,
rdata->items[i].raw_data[0] + 2);
}
}
return copy;
}
/*----------------------------------------------------------------------------*/
int knot_rdata_compare(const knot_rdata_t *r1, const knot_rdata_t *r2,
const uint8_t *format)
{
uint count = (r1->count < r2->count) ? r1->count : r2->count;
int cmp = 0;
for (int i = 0; i < count; ++i) {
/* CLEANUP */
// const uint8_t *data1, *data2;
// int size1, size2;
if (format[i] == KNOT_RDATA_WF_COMPRESSED_DNAME ||
format[i] == KNOT_RDATA_WF_UNCOMPRESSED_DNAME ||
format[i] == KNOT_RDATA_WF_LITERAL_DNAME) {
cmp = knot_dname_compare(r1->items[i].dname,
r2->items[i].dname);
// data1 = knot_dname_name(r1->items[i].dname);
// data2 = knot_dname_name(r2->items[i].dname);
// size1 = knot_dname_size(r2->items[i].dname);
// size2 = knot_dname_size(r2->items[i].dname);
} else {
cmp = knot_rdata_compare_binary(
(uint8_t *)(r1->items[i].raw_data + 1),
(uint8_t *)(r2->items[i].raw_data + 1),
r1->items[i].raw_data[0],
r1->items[i].raw_data[0]);
// data1 = (uint8_t *)(r1->items[i].raw_data + 1);
// data2 = (uint8_t *)(r2->items[i].raw_data + 1);
// size1 = r1->items[i].raw_data[0];
// size2 = r1->items[i].raw_data[0];
}
// cmp =
if (cmp != 0) {
return cmp;
}
}
assert(cmp == 0);
return 0;
}
/*----------------------------------------------------------------------------*/
const knot_dname_t *knot_rdata_cname_name(const knot_rdata_t *rdata)
{
if (rdata->count < 1) {
return NULL;
}
return rdata->items[0].dname;
}
/*----------------------------------------------------------------------------*/
const knot_dname_t *knot_rdata_dname_target(const knot_rdata_t *rdata)
{
if (rdata->count < 1) {
return NULL;
}
return rdata->items[0].dname;
}
/*---------------------------------------------------------------------------*/
const knot_dname_t *knot_rdata_get_name(const knot_rdata_t *rdata,
uint16_t type)
{
// iterate over the rdata items or act as if we knew where the name is?
switch (type) {
case KNOT_RRTYPE_NS:
return knot_rdata_ns_name(rdata);
case KNOT_RRTYPE_MX:
return knot_rdata_mx_name(rdata);
case KNOT_RRTYPE_SRV:
return knot_rdata_srv_name(rdata);
case KNOT_RRTYPE_CNAME:
return knot_rdata_cname_name(rdata);
}
return NULL;
}
/*---------------------------------------------------------------------------*/
int64_t knot_rdata_soa_serial(const knot_rdata_t *rdata)
{
if (!rdata) {
return -1;
}
if (rdata->count < 3) {
return -1;
}
// the number is in network byte order, transform it
return knot_wire_read_u32((uint8_t *)(rdata->items[2].raw_data + 1));
}
/*---------------------------------------------------------------------------*/
uint32_t knot_rdata_soa_refresh(const knot_rdata_t *rdata)
{
if (!rdata) {
return 0;
}
if (rdata->count < 4) {
return 0; /*! \todo Some other error value. */
}
// the number is in network byte order, transform it
return knot_wire_read_u32((uint8_t *)(rdata->items[3].raw_data + 1));
}
/*---------------------------------------------------------------------------*/
uint32_t knot_rdata_soa_retry(const knot_rdata_t *rdata)
{
if (!rdata) {
return 0;
}
if (rdata->count < 5) {
return 0; /*! \todo Some other error value. */
}
// the number is in network byte order, transform it
return knot_wire_read_u32((uint8_t *)(rdata->items[4].raw_data + 1));
}
/*---------------------------------------------------------------------------*/
uint32_t knot_rdata_soa_expire(const knot_rdata_t *rdata)
{
if (!rdata) {
return -1;
}
if (rdata->count < 6) {
return 0; /*! \todo Some other error value. */
}
// the number is in network byte order, transform it
return knot_wire_read_u32((uint8_t *)(rdata->items[5].raw_data + 1));
}
/*---------------------------------------------------------------------------*/
uint32_t knot_rdata_soa_minimum(const knot_rdata_t *rdata)
{
if (!rdata) {
return -1;
}
if (rdata->count < 7) {
return 0; /*! \todo Some other error value. */
}
// the number is in network byte order, transform it
return knot_wire_read_u32((uint8_t *)(rdata->items[6].raw_data + 1));
}
/*---------------------------------------------------------------------------*/
uint16_t knot_rdata_rrsig_type_covered(const knot_rdata_t *rdata)
{
if (rdata->count < 1) {
return 0;
}
return knot_wire_read_u16((uint8_t *)(rdata->items[0].raw_data + 1));
}
|