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
|
/*
* rdata.c -- RDATA conversion functions.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "rdata.h"
#include "zonec.h"
/* Taken from RFC 4398, section 2.1. */
lookup_table_type dns_certificate_types[] = {
/* 0 Reserved */
{ 1, "PKIX" }, /* X.509 as per PKIX */
{ 2, "SPKI" }, /* SPKI cert */
{ 3, "PGP" }, /* OpenPGP packet */
{ 4, "IPKIX" }, /* The URL of an X.509 data object */
{ 5, "ISPKI" }, /* The URL of an SPKI certificate */
{ 6, "IPGP" }, /* The fingerprint and URL of an OpenPGP packet */
{ 7, "ACPKIX" }, /* Attribute Certificate */
{ 8, "IACPKIX" }, /* The URL of an Attribute Certificate */
{ 253, "URI" }, /* URI private */
{ 254, "OID" }, /* OID private */
/* 255 Reserved */
/* 256-65279 Available for IANA assignment */
/* 65280-65534 Experimental */
/* 65535 Reserved */
{ 0, NULL }
};
/* Taken from RFC 2535, section 7. */
lookup_table_type dns_algorithms[] = {
{ 1, "RSAMD5" }, /* RFC 2537 */
{ 2, "DH" }, /* RFC 2539 */
{ 3, "DSA" }, /* RFC 2536 */
{ 4, "ECC" },
{ 5, "RSASHA1" }, /* RFC 3110 */
{ 6, "DSA-NSEC3-SHA1" }, /* RFC 5155 */
{ 7, "RSASHA1-NSEC3-SHA1" }, /* RFC 5155 */
{ 8, "RSASHA256" }, /* RFC 5702 */
{ 10, "RSASHA512" }, /* RFC 5702 */
{ 12, "ECC-GOST" }, /* RFC 5933 */
{ 13, "ECDSAP256SHA256" }, /* RFC 6605 */
{ 14, "ECDSAP384SHA384" }, /* RFC 6605 */
{ 15, "ED25519" }, /* RFC 8080 */
{ 16, "ED448" }, /* RFC 8080 */
{ 252, "INDIRECT" },
{ 253, "PRIVATEDNS" },
{ 254, "PRIVATEOID" },
{ 0, NULL }
};
const char *svcparamkey_strs[] = {
"mandatory", "alpn", "no-default-alpn", "port",
"ipv4hint", "ech", "ipv6hint"
};
typedef int (*rdata_to_string_type)(buffer_type *output,
rdata_atom_type rdata,
rr_type *rr);
static int
rdata_dname_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
buffer_printf(output,
"%s",
dname_to_string(domain_dname(rdata_atom_domain(rdata)),
NULL));
return 1;
}
static int
rdata_dns_name_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
const uint8_t *data = rdata_atom_data(rdata);
size_t offset = 0;
uint8_t length = data[offset];
size_t i;
while (length > 0)
{
if (offset) /* concat label */
buffer_printf(output, ".");
for (i = 1; i <= length; ++i) {
uint8_t ch = data[i+offset];
if (ch=='.' || ch==';' || ch=='(' || ch==')' || ch=='\\') {
buffer_printf(output, "\\%c", (char) ch);
} else if (!isgraph((unsigned char) ch)) {
buffer_printf(output, "\\%03u", (unsigned int) ch);
} else if (isprint((unsigned char) ch)) {
buffer_printf(output, "%c", (char) ch);
} else {
buffer_printf(output, "\\%03u", (unsigned int) ch);
}
}
/* next label */
offset = offset+length+1;
length = data[offset];
}
/* root label */
buffer_printf(output, ".");
return 1;
}
static int
rdata_text_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
const uint8_t *data = rdata_atom_data(rdata);
uint8_t length = data[0];
size_t i;
buffer_printf(output, "\"");
for (i = 1; i <= length; ++i) {
char ch = (char) data[i];
if (isprint((unsigned char)ch)) {
if (ch == '"' || ch == '\\') {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u", (unsigned) data[i]);
}
}
buffer_printf(output, "\"");
return 1;
}
static int
rdata_texts_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint16_t pos = 0;
const uint8_t *data = rdata_atom_data(rdata);
uint16_t length = rdata_atom_size(rdata);
size_t i;
while (pos < length && pos + data[pos] < length) {
buffer_printf(output, "\"");
for (i = 1; i <= data[pos]; ++i) {
char ch = (char) data[pos + i];
if (isprint((unsigned char)ch)) {
if (ch == '"' || ch == '\\') {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u", (unsigned) data[pos+i]);
}
}
pos += data[pos]+1;
buffer_printf(output, pos < length?"\" ":"\"");
}
return 1;
}
static int
rdata_long_text_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
const uint8_t *data = rdata_atom_data(rdata);
uint16_t length = rdata_atom_size(rdata);
size_t i;
buffer_printf(output, "\"");
for (i = 0; i < length; ++i) {
char ch = (char) data[i];
if (isprint((unsigned char)ch)) {
if (ch == '"' || ch == '\\') {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u", (unsigned) data[i]);
}
}
buffer_printf(output, "\"");
return 1;
}
static int
rdata_tag_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
const uint8_t *data = rdata_atom_data(rdata);
uint8_t length = data[0];
size_t i;
for (i = 1; i <= length; ++i) {
char ch = (char) data[i];
if (isdigit((unsigned char)ch) || islower((unsigned char)ch))
buffer_printf(output, "%c", ch);
else return 0;
}
return 1;
}
static int
rdata_byte_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint8_t data = *rdata_atom_data(rdata);
buffer_printf(output, "%lu", (unsigned long) data);
return 1;
}
static int
rdata_short_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint16_t data = read_uint16(rdata_atom_data(rdata));
buffer_printf(output, "%lu", (unsigned long) data);
return 1;
}
static int
rdata_long_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint32_t data = read_uint32(rdata_atom_data(rdata));
buffer_printf(output, "%lu", (unsigned long) data);
return 1;
}
static int
rdata_a_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int result = 0;
char str[200];
if (inet_ntop(AF_INET, rdata_atom_data(rdata), str, sizeof(str))) {
buffer_printf(output, "%s", str);
result = 1;
}
return result;
}
static int
rdata_aaaa_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int result = 0;
char str[200];
if (inet_ntop(AF_INET6, rdata_atom_data(rdata), str, sizeof(str))) {
buffer_printf(output, "%s", str);
result = 1;
}
return result;
}
static int
rdata_ilnp64_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint8_t* data = rdata_atom_data(rdata);
uint16_t a1 = read_uint16(data);
uint16_t a2 = read_uint16(data+2);
uint16_t a3 = read_uint16(data+4);
uint16_t a4 = read_uint16(data+6);
buffer_printf(output, "%.4x:%.4x:%.4x:%.4x", a1, a2, a3, a4);
return 1;
}
static int
rdata_eui48_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint8_t* data = rdata_atom_data(rdata);
uint8_t a1 = data[0];
uint8_t a2 = data[1];
uint8_t a3 = data[2];
uint8_t a4 = data[3];
uint8_t a5 = data[4];
uint8_t a6 = data[5];
buffer_printf(output, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
a1, a2, a3, a4, a5, a6);
return 1;
}
static int
rdata_eui64_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint8_t* data = rdata_atom_data(rdata);
uint8_t a1 = data[0];
uint8_t a2 = data[1];
uint8_t a3 = data[2];
uint8_t a4 = data[3];
uint8_t a5 = data[4];
uint8_t a6 = data[5];
uint8_t a7 = data[6];
uint8_t a8 = data[7];
buffer_printf(output, "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
a1, a2, a3, a4, a5, a6, a7, a8);
return 1;
}
static int
rdata_rrtype_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint16_t type = read_uint16(rdata_atom_data(rdata));
buffer_printf(output, "%s", rrtype_to_string(type));
return 1;
}
static int
rdata_algorithm_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint8_t id = *rdata_atom_data(rdata);
buffer_printf(output, "%u", (unsigned) id);
return 1;
}
static int
rdata_certificate_type_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint16_t id = read_uint16(rdata_atom_data(rdata));
lookup_table_type *type
= lookup_by_id(dns_certificate_types, id);
if (type) {
buffer_printf(output, "%s", type->name);
} else {
buffer_printf(output, "%u", (unsigned) id);
}
return 1;
}
static int
rdata_period_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint32_t period = read_uint32(rdata_atom_data(rdata));
buffer_printf(output, "%lu", (unsigned long) period);
return 1;
}
static int
rdata_time_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int result = 0;
time_t time = (time_t) read_uint32(rdata_atom_data(rdata));
struct tm *tm = gmtime(&time);
char buf[15];
if (strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm)) {
buffer_printf(output, "%s", buf);
result = 1;
}
return result;
}
static int
rdata_base32_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int length;
size_t size = rdata_atom_size(rdata);
if(size == 0) {
buffer_write(output, "-", 1);
return 1;
}
size -= 1; /* remove length byte from count */
buffer_reserve(output, size * 2 + 1);
length = b32_ntop(rdata_atom_data(rdata)+1, size,
(char *) buffer_current(output), size * 2);
if (length > 0) {
buffer_skip(output, length);
}
return length != -1;
}
static int
rdata_base64_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int length;
size_t size = rdata_atom_size(rdata);
if(size == 0) {
/* single zero represents empty buffer */
buffer_write(output, "0", 1);
return 1;
}
buffer_reserve(output, size * 2 + 1);
length = __b64_ntop(rdata_atom_data(rdata), size,
(char *) buffer_current(output), size * 2);
if (length > 0) {
buffer_skip(output, length);
}
return length != -1;
}
static void
hex_to_string(buffer_type *output, const uint8_t *data, size_t size)
{
static const char hexdigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
size_t i;
buffer_reserve(output, size * 2);
for (i = 0; i < size; ++i) {
uint8_t octet = *data++;
buffer_write_u8(output, hexdigits[octet >> 4]);
buffer_write_u8(output, hexdigits[octet & 0x0f]);
}
}
static int
rdata_hex_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
if(rdata_atom_size(rdata) == 0) {
/* single zero represents empty buffer, such as CDS deletes */
buffer_printf(output, "0");
} else {
hex_to_string(output, rdata_atom_data(rdata), rdata_atom_size(rdata));
}
return 1;
}
static int
rdata_hexlen_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
if(rdata_atom_size(rdata) <= 1) {
/* NSEC3 salt hex can be empty */
buffer_printf(output, "-");
return 1;
}
hex_to_string(output, rdata_atom_data(rdata)+1, rdata_atom_size(rdata)-1);
return 1;
}
static int
rdata_nsap_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
buffer_printf(output, "0x");
hex_to_string(output, rdata_atom_data(rdata), rdata_atom_size(rdata));
return 1;
}
static int
rdata_apl_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int result = 0;
buffer_type packet;
buffer_create_from(
&packet, rdata_atom_data(rdata), rdata_atom_size(rdata));
if (buffer_available(&packet, 4)) {
uint16_t address_family = buffer_read_u16(&packet);
uint8_t prefix = buffer_read_u8(&packet);
uint8_t length = buffer_read_u8(&packet);
int negated = length & APL_NEGATION_MASK;
int af = -1;
length &= APL_LENGTH_MASK;
switch (address_family) {
case 1: af = AF_INET; break;
case 2: af = AF_INET6; break;
}
if (af != -1 && buffer_available(&packet, length)) {
char text_address[1000];
uint8_t address[128];
memset(address, 0, sizeof(address));
buffer_read(&packet, address, length);
if (inet_ntop(af, address, text_address, sizeof(text_address))) {
buffer_printf(output, "%s%d:%s/%d",
negated ? "!" : "",
(int) address_family,
text_address,
(int) prefix);
result = 1;
}
}
}
return result;
}
static int
rdata_services_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
int result = 0;
buffer_type packet;
buffer_create_from(
&packet, rdata_atom_data(rdata), rdata_atom_size(rdata));
if (buffer_available(&packet, 1)) {
uint8_t protocol_number = buffer_read_u8(&packet);
ssize_t bitmap_size = buffer_remaining(&packet);
uint8_t *bitmap = buffer_current(&packet);
struct protoent *proto = getprotobynumber(protocol_number);
if (proto) {
int i;
buffer_printf(output, "%s", proto->p_name);
for (i = 0; i < bitmap_size * 8; ++i) {
if (get_bit(bitmap, i)) {
struct servent *service = getservbyport((int)htons(i), proto->p_name);
if (service) {
buffer_printf(output, " %s", service->s_name);
} else {
buffer_printf(output, " %d", i);
}
}
}
buffer_skip(&packet, bitmap_size);
result = 1;
}
}
return result;
}
static int
rdata_ipsecgateway_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* rr)
{
int gateway_type = rdata_atom_data(rr->rdatas[1])[0];
switch(gateway_type) {
case IPSECKEY_NOGATEWAY:
buffer_printf(output, ".");
break;
case IPSECKEY_IP4:
rdata_a_to_string(output, rdata, rr);
break;
case IPSECKEY_IP6:
rdata_aaaa_to_string(output, rdata, rr);
break;
case IPSECKEY_DNAME:
{
region_type* temp = region_create(xalloc, free);
const dname_type* d = dname_make(temp,
rdata_atom_data(rdata), 0);
if(!d) {
region_destroy(temp);
return 0;
}
buffer_printf(output, "%s", dname_to_string(d, NULL));
region_destroy(temp);
}
break;
default:
return 0;
}
return 1;
}
static int
rdata_nxt_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
size_t i;
uint8_t *bitmap = rdata_atom_data(rdata);
size_t bitmap_size = rdata_atom_size(rdata);
for (i = 0; i < bitmap_size * 8; ++i) {
if (get_bit(bitmap, i)) {
buffer_printf(output, "%s ", rrtype_to_string(i));
}
}
buffer_skip(output, -1);
return 1;
}
static int
rdata_nsec_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
size_t saved_position = buffer_position(output);
buffer_type packet;
int insert_space = 0;
buffer_create_from(
&packet, rdata_atom_data(rdata), rdata_atom_size(rdata));
while (buffer_available(&packet, 2)) {
uint8_t window = buffer_read_u8(&packet);
uint8_t bitmap_size = buffer_read_u8(&packet);
uint8_t *bitmap = buffer_current(&packet);
int i;
if (!buffer_available(&packet, bitmap_size)) {
buffer_set_position(output, saved_position);
return 0;
}
for (i = 0; i < bitmap_size * 8; ++i) {
if (get_bit(bitmap, i)) {
buffer_printf(output,
"%s%s",
insert_space ? " " : "",
rrtype_to_string(
window * 256 + i));
insert_space = 1;
}
}
buffer_skip(&packet, bitmap_size);
}
return 1;
}
static int
rdata_loc_to_string(buffer_type *ATTR_UNUSED(output),
rdata_atom_type ATTR_UNUSED(rdata),
rr_type* ATTR_UNUSED(rr))
{
/*
* Returning 0 forces the record to be printed in unknown
* format.
*/
return 0;
}
static void
buffer_print_svcparamkey(buffer_type *output, uint16_t svcparamkey)
{
if (svcparamkey < SVCPARAMKEY_COUNT)
buffer_printf(output, "%s", svcparamkey_strs[svcparamkey]);
else
buffer_printf(output, "key%d", (int)svcparamkey);
}
static int
rdata_svcparam_port_to_string(buffer_type *output, uint16_t val_len,
uint16_t *data)
{
if (val_len != 2)
return 0; /* wireformat error, a short is 2 bytes */
buffer_printf(output, "=%d", (int)ntohs(data[0]));
return 1;
}
static int
rdata_svcparam_ipv4hint_to_string(buffer_type *output, uint16_t val_len,
uint16_t *data)
{
char ip_str[INET_ADDRSTRLEN + 1];
assert(val_len > 0); /* Guaranteed by rdata_svcparam_to_string */
if ((val_len % IP4ADDRLEN) == 0) {
if (inet_ntop(AF_INET, data, ip_str, sizeof(ip_str)) == NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, "=%s", ip_str);
data += IP4ADDRLEN / sizeof(uint16_t);
while ((val_len -= IP4ADDRLEN) > 0) {
if (inet_ntop(AF_INET, data, ip_str, sizeof(ip_str)) == NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, ",%s", ip_str);
data += IP4ADDRLEN / sizeof(uint16_t);
}
return 1;
} else
return 0;
}
static int
rdata_svcparam_ipv6hint_to_string(buffer_type *output, uint16_t val_len,
uint16_t *data)
{
char ip_str[INET6_ADDRSTRLEN + 1];
assert(val_len > 0); /* Guaranteed by rdata_svcparam_to_string */
if ((val_len % IP6ADDRLEN) == 0) {
if (inet_ntop(AF_INET6, data, ip_str, sizeof(ip_str)) == NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, "=%s", ip_str);
data += IP6ADDRLEN / sizeof(uint16_t);
while ((val_len -= IP6ADDRLEN) > 0) {
if (inet_ntop(AF_INET6, data, ip_str, sizeof(ip_str)) == NULL)
return 0; /* wireformat error, incorrect size or inet family */
buffer_printf(output, ",%s", ip_str);
data += IP6ADDRLEN / sizeof(uint16_t);
}
return 1;
} else
return 0;
}
static int
rdata_svcparam_mandatory_to_string(buffer_type *output, uint16_t val_len,
uint16_t *data)
{
assert(val_len > 0); /* Guaranteed by rdata_svcparam_to_string */
if (val_len % sizeof(uint16_t))
return 0; /* wireformat error, val_len must be multiple of shorts */
buffer_write_u8(output, '=');
buffer_print_svcparamkey(output, ntohs(*data));
data += 1;
while ((val_len -= sizeof(uint16_t))) {
buffer_write_u8(output, ',');
buffer_print_svcparamkey(output, ntohs(*data));
data += 1;
}
return 1;
}
static int
rdata_svcparam_ech_to_string(buffer_type *output, uint16_t val_len,
uint16_t *data)
{
int length;
assert(val_len > 0); /* Guaranteed by rdata_svcparam_to_string */
buffer_write_u8(output, '=');
buffer_reserve(output, val_len * 2 + 1);
length = __b64_ntop((uint8_t*) data, val_len,
(char *) buffer_current(output), val_len * 2);
if (length > 0) {
buffer_skip(output, length);
}
return length != -1;
}
static int
rdata_svcparam_alpn_to_string(buffer_type *output, uint16_t val_len,
uint16_t *data)
{
uint8_t *dp = (void *)data;
assert(val_len > 0); /* Guaranteed by rdata_svcparam_to_string */
buffer_write_u8(output, '=');
buffer_write_u8(output, '"');
while (val_len) {
uint8_t i, str_len = *dp++;
if (str_len > --val_len)
return 0;
for (i = 0; i < str_len; i++) {
if (dp[i] == '"' || dp[i] == '\\')
buffer_printf(output, "\\\\\\%c", dp[i]);
else if (dp[i] == ',')
buffer_printf(output, "\\\\%c", dp[i]);
else if (!isprint(dp[i]))
buffer_printf(output, "\\%03u", (unsigned) dp[i]);
else
buffer_write_u8(output, dp[i]);
}
dp += str_len;
if ((val_len -= str_len))
buffer_write_u8(output, ',');
}
buffer_write_u8(output, '"');
return 1;
}
static int
rdata_svcparam_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint16_t size = rdata_atom_size(rdata);
uint16_t* data = (uint16_t *)rdata_atom_data(rdata);
uint16_t svcparamkey, val_len;
uint8_t* dp;
size_t i;
if (size < 4)
return 0;
svcparamkey = ntohs(data[0]);
buffer_print_svcparamkey(output, svcparamkey);
val_len = ntohs(data[1]);
if (size != val_len + 4)
return 0; /* wireformat error */
if (!val_len) {
/* Some SvcParams MUST have values */
switch (svcparamkey) {
case SVCB_KEY_ALPN:
case SVCB_KEY_PORT:
case SVCB_KEY_IPV4HINT:
case SVCB_KEY_IPV6HINT:
case SVCB_KEY_MANDATORY:
return 0;
default:
return 1;
}
}
switch (svcparamkey) {
case SVCB_KEY_PORT:
return rdata_svcparam_port_to_string(output, val_len, data+2);
case SVCB_KEY_IPV4HINT:
return rdata_svcparam_ipv4hint_to_string(output, val_len, data+2);
case SVCB_KEY_IPV6HINT:
return rdata_svcparam_ipv6hint_to_string(output, val_len, data+2);
case SVCB_KEY_MANDATORY:
return rdata_svcparam_mandatory_to_string(output, val_len, data+2);
case SVCB_KEY_NO_DEFAULT_ALPN:
return 0; /* wireformat error, should not have a value */
case SVCB_KEY_ALPN:
return rdata_svcparam_alpn_to_string(output, val_len, data+2);
case SVCB_KEY_ECH:
return rdata_svcparam_ech_to_string(output, val_len, data+2);
default:
buffer_write(output, "=\"", 2);
dp = (void*) (data + 2);
for (i = 0; i < val_len; i++) {
if (dp[i] == '"' || dp[i] == '\\')
buffer_printf(output, "\\%c", dp[i]);
else if (!isprint(dp[i]))
buffer_printf(output, "\\%03u", (unsigned) dp[i]);
else
buffer_write_u8(output, dp[i]);
}
buffer_write_u8(output, '"');
break;
}
return 1;
}
static int
rdata_unknown_to_string(buffer_type *output, rdata_atom_type rdata,
rr_type* ATTR_UNUSED(rr))
{
uint16_t size = rdata_atom_size(rdata);
buffer_printf(output, "\\# %lu ", (unsigned long) size);
hex_to_string(output, rdata_atom_data(rdata), size);
return 1;
}
static rdata_to_string_type rdata_to_string_table[RDATA_ZF_UNKNOWN + 1] = {
rdata_dname_to_string,
rdata_dns_name_to_string,
rdata_text_to_string,
rdata_texts_to_string,
rdata_byte_to_string,
rdata_short_to_string,
rdata_long_to_string,
rdata_a_to_string,
rdata_aaaa_to_string,
rdata_rrtype_to_string,
rdata_algorithm_to_string,
rdata_certificate_type_to_string,
rdata_period_to_string,
rdata_time_to_string,
rdata_base64_to_string,
rdata_base32_to_string,
rdata_hex_to_string,
rdata_hexlen_to_string,
rdata_nsap_to_string,
rdata_apl_to_string,
rdata_ipsecgateway_to_string,
rdata_services_to_string,
rdata_nxt_to_string,
rdata_nsec_to_string,
rdata_loc_to_string,
rdata_ilnp64_to_string,
rdata_eui48_to_string,
rdata_eui64_to_string,
rdata_long_text_to_string,
rdata_tag_to_string,
rdata_svcparam_to_string,
rdata_unknown_to_string
};
int
rdata_atom_to_string(buffer_type *output, rdata_zoneformat_type type,
rdata_atom_type rdata, rr_type* record)
{
return rdata_to_string_table[type](output, rdata, record);
}
ssize_t
rdata_wireformat_to_rdata_atoms(region_type *region,
domain_table_type *owners,
uint16_t rrtype,
uint16_t data_size,
buffer_type *packet,
rdata_atom_type **rdatas)
{
size_t end = buffer_position(packet) + data_size;
size_t i;
rdata_atom_type temp_rdatas[MAXRDATALEN];
rrtype_descriptor_type *descriptor = rrtype_descriptor_by_type(rrtype);
region_type *temp_region;
assert(descriptor->maximum <= MAXRDATALEN);
if (!buffer_available(packet, data_size)) {
return -1;
}
temp_region = region_create(xalloc, free);
for (i = 0; i < descriptor->maximum; ++i) {
int is_domain = 0;
int is_normalized = 0;
int is_wirestore = 0;
size_t length = 0;
int required = i < descriptor->minimum;
switch (rdata_atom_wireformat_type(rrtype, i)) {
case RDATA_WF_COMPRESSED_DNAME:
case RDATA_WF_UNCOMPRESSED_DNAME:
is_domain = 1;
is_normalized = 1;
break;
case RDATA_WF_LITERAL_DNAME:
is_domain = 1;
is_wirestore = 1;
break;
case RDATA_WF_BYTE:
length = sizeof(uint8_t);
break;
case RDATA_WF_SHORT:
length = sizeof(uint16_t);
break;
case RDATA_WF_LONG:
length = sizeof(uint32_t);
break;
case RDATA_WF_TEXTS:
case RDATA_WF_LONG_TEXT:
length = end - buffer_position(packet);
break;
case RDATA_WF_TEXT:
case RDATA_WF_BINARYWITHLENGTH:
/* Length is stored in the first byte. */
length = 1;
if (buffer_position(packet) + length <= end) {
length += buffer_current(packet)[length - 1];
}
break;
case RDATA_WF_A:
length = sizeof(in_addr_t);
break;
case RDATA_WF_AAAA:
length = IP6ADDRLEN;
break;
case RDATA_WF_ILNP64:
length = IP6ADDRLEN/2;
break;
case RDATA_WF_EUI48:
length = EUI48ADDRLEN;
break;
case RDATA_WF_EUI64:
length = EUI64ADDRLEN;
break;
case RDATA_WF_BINARY:
/* Remaining RDATA is binary. */
length = end - buffer_position(packet);
break;
case RDATA_WF_APL:
length = (sizeof(uint16_t) /* address family */
+ sizeof(uint8_t) /* prefix */
+ sizeof(uint8_t)); /* length */
if (buffer_position(packet) + length <= end) {
/* Mask out negation bit. */
length += (buffer_current(packet)[length - 1]
& APL_LENGTH_MASK);
}
break;
case RDATA_WF_IPSECGATEWAY:
assert(i>1); /* we are past the gateway type */
switch(rdata_atom_data(temp_rdatas[1])[0]) /* gateway type */ {
default:
case IPSECKEY_NOGATEWAY:
length = 0;
break;
case IPSECKEY_IP4:
length = IP4ADDRLEN;
break;
case IPSECKEY_IP6:
length = IP6ADDRLEN;
break;
case IPSECKEY_DNAME:
is_domain = 1;
is_normalized = 1;
is_wirestore = 1;
break;
}
break;
case RDATA_WF_SVCPARAM:
length = 4;
if (buffer_position(packet) + 4 <= end) {
length +=
read_uint16(buffer_current(packet) + 2);
}
break;
}
if (is_domain) {
const dname_type *dname;
if (!required && buffer_position(packet) == end) {
break;
}
dname = dname_make_from_packet(
temp_region, packet, 1, is_normalized);
if (!dname || buffer_position(packet) > end) {
/* Error in domain name. */
region_destroy(temp_region);
return -1;
}
if(is_wirestore) {
temp_rdatas[i].data = (uint16_t *) region_alloc(
region, sizeof(uint16_t) + ((size_t)dname->name_size));
temp_rdatas[i].data[0] = dname->name_size;
memcpy(temp_rdatas[i].data+1, dname_name(dname),
dname->name_size);
} else {
temp_rdatas[i].domain
= domain_table_insert(owners, dname);
temp_rdatas[i].domain->usage ++;
}
} else {
if (buffer_position(packet) + length > end) {
if (required) {
/* Truncated RDATA. */
region_destroy(temp_region);
return -1;
} else {
break;
}
}
if (!required && buffer_position(packet) == end) {
break;
}
temp_rdatas[i].data = (uint16_t *) region_alloc(
region, sizeof(uint16_t) + length);
temp_rdatas[i].data[0] = length;
buffer_read(packet, temp_rdatas[i].data + 1, length);
}
}
if (buffer_position(packet) < end) {
/* Trailing garbage. */
region_destroy(temp_region);
return -1;
}
*rdatas = (rdata_atom_type *) region_alloc_array_init(
region, temp_rdatas, i, sizeof(rdata_atom_type));
region_destroy(temp_region);
return (ssize_t)i;
}
size_t
rdata_maximum_wireformat_size(rrtype_descriptor_type *descriptor,
size_t rdata_count,
rdata_atom_type *rdatas)
{
size_t result = 0;
size_t i;
for (i = 0; i < rdata_count; ++i) {
if (rdata_atom_is_domain(descriptor->type, i)) {
result += domain_dname(rdata_atom_domain(rdatas[i]))->name_size;
} else {
result += rdata_atom_size(rdatas[i]);
}
}
return result;
}
int
rdata_atoms_to_unknown_string(buffer_type *output,
rrtype_descriptor_type *descriptor,
size_t rdata_count,
rdata_atom_type *rdatas)
{
size_t i;
size_t size =
rdata_maximum_wireformat_size(descriptor, rdata_count, rdatas);
buffer_printf(output, " \\# %lu ", (unsigned long) size);
for (i = 0; i < rdata_count; ++i) {
if (rdata_atom_is_domain(descriptor->type, i)) {
const dname_type *dname =
domain_dname(rdata_atom_domain(rdatas[i]));
hex_to_string(
output, dname_name(dname), dname->name_size);
} else {
hex_to_string(output, rdata_atom_data(rdatas[i]),
rdata_atom_size(rdatas[i]));
}
}
return 1;
}
int
print_rdata(buffer_type *output, rrtype_descriptor_type *descriptor,
rr_type *record)
{
size_t i;
size_t saved_position = buffer_position(output);
for (i = 0; i < record->rdata_count; ++i) {
if (i == 0) {
buffer_printf(output, "\t");
} else if (descriptor->type == TYPE_SOA && i == 2) {
buffer_printf(output, " (\n\t\t");
} else {
buffer_printf(output, " ");
}
if (!rdata_atom_to_string(
output,
(rdata_zoneformat_type) descriptor->zoneformat[i],
record->rdatas[i], record))
{
buffer_set_position(output, saved_position);
return 0;
}
}
if (descriptor->type == TYPE_SOA) {
buffer_printf(output, " )");
}
return 1;
}
|