forked from NLnetLabs/ldns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host2str.c
2955 lines (2652 loc) · 77.2 KB
/
host2str.c
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
/*
* host2str.c
*
* conversion routines from the host format
* to the presentation format (strings)
*
* a Net::DNS like library for C
*
* (c) NLnet Labs, 2004-2006
*
* See the file LICENSE for the license
*/
#include <ldns/config.h>
#include <ldns/ldns.h>
#include <limits.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#include <time.h>
#include <sys/time.h>
#ifdef HAVE_SSL
#include <openssl/bn.h>
#include <openssl/rsa.h>
#ifdef USE_DSA
#include <openssl/dsa.h>
#endif
#endif
#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif
/* lookup tables for standard DNS stuff */
/* Taken from RFC 2535, section 7. */
ldns_lookup_table ldns_algorithms[] = {
{ LDNS_RSAMD5, "RSAMD5" },
{ LDNS_DH, "DH" },
#ifdef USE_DSA
{ LDNS_DSA, "DSA" },
#endif /* USE_DSA */
{ LDNS_ECC, "ECC" },
{ LDNS_RSASHA1, "RSASHA1" },
#ifdef USE_DSA
{ LDNS_DSA_NSEC3, "DSA-NSEC3-SHA1" },
#endif /* USE_DSA */
{ LDNS_RSASHA1_NSEC3, "RSASHA1-NSEC3-SHA1" },
#ifdef USE_SHA2
{ LDNS_RSASHA256, "RSASHA256"},
{ LDNS_RSASHA512, "RSASHA512"},
#endif
#ifdef USE_GOST
{ LDNS_ECC_GOST, "ECC-GOST"},
#endif
#ifdef USE_ECDSA
{ LDNS_ECDSAP256SHA256, "ECDSAP256SHA256"},
{ LDNS_ECDSAP384SHA384, "ECDSAP384SHA384"},
#endif
#ifdef USE_ED25519
{ LDNS_ED25519, "ED25519"},
#endif
#ifdef USE_ED448
{ LDNS_ED448, "ED448"},
#endif
{ LDNS_INDIRECT, "INDIRECT" },
{ LDNS_PRIVATEDNS, "PRIVATEDNS" },
{ LDNS_PRIVATEOID, "PRIVATEOID" },
{ 0, NULL }
};
/* Taken from RFC 4398 */
ldns_lookup_table ldns_cert_algorithms[] = {
{ LDNS_CERT_PKIX, "PKIX" },
{ LDNS_CERT_SPKI, "SPKI" },
{ LDNS_CERT_PGP, "PGP" },
{ LDNS_CERT_IPKIX, "IPKIX" },
{ LDNS_CERT_ISPKI, "ISPKI" },
{ LDNS_CERT_IPGP, "IPGP" },
{ LDNS_CERT_ACPKIX, "ACPKIX" },
{ LDNS_CERT_IACPKIX, "IACPKIX" },
{ LDNS_CERT_URI, "URI" },
{ LDNS_CERT_OID, "OID" },
{ 0, NULL }
};
/* classes */
ldns_lookup_table ldns_rr_classes[] = {
{ LDNS_RR_CLASS_IN, "IN" },
{ LDNS_RR_CLASS_CH, "CH" },
{ LDNS_RR_CLASS_HS, "HS" },
{ LDNS_RR_CLASS_NONE, "NONE" },
{ LDNS_RR_CLASS_ANY, "ANY" },
{ 0, NULL }
};
/* if these are used elsewhere */
ldns_lookup_table ldns_rcodes[] = {
{ LDNS_RCODE_NOERROR, "NOERROR" },
{ LDNS_RCODE_FORMERR, "FORMERR" },
{ LDNS_RCODE_SERVFAIL, "SERVFAIL" },
{ LDNS_RCODE_NXDOMAIN, "NXDOMAIN" },
{ LDNS_RCODE_NOTIMPL, "NOTIMPL" },
{ LDNS_RCODE_REFUSED, "REFUSED" },
{ LDNS_RCODE_YXDOMAIN, "YXDOMAIN" },
{ LDNS_RCODE_YXRRSET, "YXRRSET" },
{ LDNS_RCODE_NXRRSET, "NXRRSET" },
{ LDNS_RCODE_NOTAUTH, "NOTAUTH" },
{ LDNS_RCODE_NOTZONE, "NOTZONE" },
{ 0, NULL }
};
ldns_lookup_table ldns_opcodes[] = {
{ LDNS_PACKET_QUERY, "QUERY" },
{ LDNS_PACKET_IQUERY, "IQUERY" },
{ LDNS_PACKET_STATUS, "STATUS" },
{ LDNS_PACKET_NOTIFY, "NOTIFY" },
{ LDNS_PACKET_UPDATE, "UPDATE" },
{ 0, NULL }
};
const ldns_output_format ldns_output_format_nocomments_record = { 0, NULL };
const ldns_output_format *ldns_output_format_nocomments
= &ldns_output_format_nocomments_record;
const ldns_output_format ldns_output_format_onlykeyids_record = {
LDNS_COMMENT_KEY, NULL
};
const ldns_output_format *ldns_output_format_onlykeyids
= &ldns_output_format_onlykeyids_record;
const ldns_output_format *ldns_output_format_default
= &ldns_output_format_onlykeyids_record;
const ldns_output_format ldns_output_format_bubblebabble_record = {
LDNS_COMMENT_KEY | LDNS_COMMENT_BUBBLEBABBLE | LDNS_COMMENT_FLAGS, NULL
};
const ldns_output_format *ldns_output_format_bubblebabble
= &ldns_output_format_bubblebabble_record;
static bool
ldns_output_format_covers_type(const ldns_output_format* fmt, ldns_rr_type t)
{
return fmt && (fmt->flags & LDNS_FMT_RFC3597) &&
((ldns_output_format_storage*)fmt)->bitmap &&
ldns_nsec_bitmap_covers_type(
((ldns_output_format_storage*)fmt)->bitmap, t);
}
ldns_status
ldns_output_format_set_type(ldns_output_format* fmt, ldns_rr_type t)
{
ldns_output_format_storage* fmt_st = (ldns_output_format_storage*)fmt;
ldns_status s;
assert(fmt != NULL);
if (!(fmt_st->flags & LDNS_FMT_RFC3597)) {
ldns_output_format_set(fmt, LDNS_FMT_RFC3597);
}
if (! fmt_st->bitmap) {
s = ldns_rdf_bitmap_known_rr_types_space(&fmt_st->bitmap);
if (s != LDNS_STATUS_OK) {
return s;
}
}
return ldns_nsec_bitmap_set_type(fmt_st->bitmap, t);
}
ldns_status
ldns_output_format_clear_type(ldns_output_format* fmt, ldns_rr_type t)
{
ldns_output_format_storage* fmt_st = (ldns_output_format_storage*)fmt;
ldns_status s;
assert(fmt != NULL);
if (!(fmt_st->flags & LDNS_FMT_RFC3597)) {
ldns_output_format_set(fmt, LDNS_FMT_RFC3597);
}
if (! fmt_st->bitmap) {
s = ldns_rdf_bitmap_known_rr_types(&fmt_st->bitmap);
if (s != LDNS_STATUS_OK) {
return s;
}
}
return ldns_nsec_bitmap_clear_type(fmt_st->bitmap, t);
}
ldns_status
ldns_pkt_opcode2buffer_str(ldns_buffer *output, ldns_pkt_opcode opcode)
{
ldns_lookup_table *lt = ldns_lookup_by_id(ldns_opcodes, opcode);
if (lt && lt->name) {
ldns_buffer_printf(output, "%s", lt->name);
} else {
ldns_buffer_printf(output, "OPCODE%u", opcode);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_pkt_rcode2buffer_str(ldns_buffer *output, ldns_pkt_rcode rcode)
{
ldns_lookup_table *lt = ldns_lookup_by_id(ldns_rcodes, rcode);
if (lt && lt->name) {
ldns_buffer_printf(output, "%s", lt->name);
} else {
ldns_buffer_printf(output, "RCODE%u", rcode);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_algorithm2buffer_str(ldns_buffer *output,
ldns_algorithm algorithm)
{
ldns_lookup_table *lt = ldns_lookup_by_id(ldns_algorithms,
algorithm);
if (lt && lt->name) {
ldns_buffer_printf(output, "%s", lt->name);
} else {
ldns_buffer_printf(output, "ALG%u", algorithm);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_cert_algorithm2buffer_str(ldns_buffer *output,
ldns_cert_algorithm cert_algorithm)
{
ldns_lookup_table *lt = ldns_lookup_by_id(ldns_cert_algorithms,
cert_algorithm);
if (lt && lt->name) {
ldns_buffer_printf(output, "%s", lt->name);
} else {
ldns_buffer_printf(output, "CERT_ALG%u",
cert_algorithm);
}
return ldns_buffer_status(output);
}
char *
ldns_pkt_opcode2str(ldns_pkt_opcode opcode)
{
char *str;
ldns_buffer *buf;
buf = ldns_buffer_new(12);
if (!buf) {
return NULL;
}
str = NULL;
if (ldns_pkt_opcode2buffer_str(buf, opcode) == LDNS_STATUS_OK) {
str = ldns_buffer_export2str(buf);
}
ldns_buffer_free(buf);
return str;
}
char *
ldns_pkt_rcode2str(ldns_pkt_rcode rcode)
{
char *str;
ldns_buffer *buf;
buf = ldns_buffer_new(10);
if (!buf) {
return NULL;
}
str = NULL;
if (ldns_pkt_rcode2buffer_str(buf, rcode) == LDNS_STATUS_OK) {
str = ldns_buffer_export2str(buf);
}
ldns_buffer_free(buf);
return str;
}
char *
ldns_pkt_algorithm2str(ldns_algorithm algorithm)
{
char *str;
ldns_buffer *buf;
buf = ldns_buffer_new(10);
if (!buf) {
return NULL;
}
str = NULL;
if (ldns_algorithm2buffer_str(buf, algorithm)
== LDNS_STATUS_OK) {
str = ldns_buffer_export2str(buf);
}
ldns_buffer_free(buf);
return str;
}
char *
ldns_pkt_cert_algorithm2str(ldns_cert_algorithm cert_algorithm)
{
char *str;
ldns_buffer *buf;
buf = ldns_buffer_new(10);
if (!buf) {
return NULL;
}
str = NULL;
if (ldns_cert_algorithm2buffer_str(buf, cert_algorithm)
== LDNS_STATUS_OK) {
str = ldns_buffer_export2str(buf);
}
ldns_buffer_free(buf);
return str;
}
/* do NOT pass compressed data here :p */
ldns_status
ldns_rdf2buffer_str_dname(ldns_buffer *output, const ldns_rdf *dname)
{
/* can we do with 1 pos var? or without at all? */
uint8_t src_pos = 0;
uint8_t len;
uint8_t *data;
uint8_t i;
unsigned char c;
data = (uint8_t*)ldns_rdf_data(dname);
len = data[src_pos];
if (ldns_rdf_size(dname) > LDNS_MAX_DOMAINLEN) {
/* too large, return */
return LDNS_STATUS_DOMAINNAME_OVERFLOW;
}
/* special case: root label */
if (1 == ldns_rdf_size(dname)) {
ldns_buffer_printf(output, ".");
} else {
while ((len > 0) && src_pos < ldns_rdf_size(dname)) {
src_pos++;
for(i = 0; i < len; i++) {
/* paranoia check for various 'strange'
characters in dnames
*/
c = (unsigned char) data[src_pos];
if(c == '.' || c == ';' ||
c == '(' || c == ')' ||
c == '\\') {
ldns_buffer_printf(output, "\\%c",
data[src_pos]);
} else if (!(isascii(c) && isgraph(c))) {
ldns_buffer_printf(output, "\\%03u",
data[src_pos]);
} else {
ldns_buffer_printf(output, "%c", data[src_pos]);
}
src_pos++;
}
if (src_pos < ldns_rdf_size(dname)) {
ldns_buffer_printf(output, ".");
}
len = data[src_pos];
}
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_int8(ldns_buffer *output, const ldns_rdf *rdf)
{
uint8_t data = ldns_rdf_data(rdf)[0];
ldns_buffer_printf(output, "%lu", (unsigned long) data);
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_int16(ldns_buffer *output, const ldns_rdf *rdf)
{
uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
ldns_buffer_printf(output, "%lu", (unsigned long) data);
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_int32(ldns_buffer *output, const ldns_rdf *rdf)
{
uint32_t data = ldns_read_uint32(ldns_rdf_data(rdf));
ldns_buffer_printf(output, "%lu", (unsigned long) data);
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_time(ldns_buffer *output, const ldns_rdf *rdf)
{
/* create a YYYYMMDDHHMMSS string if possible */
struct tm tm;
char date_buf[16];
memset(&tm, 0, sizeof(tm));
if (ldns_serial_arithmetics_gmtime_r(ldns_rdf2native_int32(rdf), time(NULL), &tm)
&& strftime(date_buf, 15, "%Y%m%d%H%M%S", &tm)) {
ldns_buffer_printf(output, "%s", date_buf);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_a(ldns_buffer *output, const ldns_rdf *rdf)
{
char str[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, ldns_rdf_data(rdf), str, INET_ADDRSTRLEN)) {
ldns_buffer_printf(output, "%s", str);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_aaaa(ldns_buffer *output, const ldns_rdf *rdf)
{
char str[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, ldns_rdf_data(rdf), str, INET6_ADDRSTRLEN)) {
ldns_buffer_printf(output, "%s", str);
}
return ldns_buffer_status(output);
}
static void
ldns_characters2buffer_str(ldns_buffer* output,
size_t amount, const uint8_t* characters)
{
uint8_t ch;
while (amount > 0) {
ch = *characters++;
if (isprint((int)ch) || ch == '\t') {
if (ch == '\"' || ch == '\\')
ldns_buffer_printf(output, "\\%c", ch);
else
ldns_buffer_printf(output, "%c", ch);
} else {
ldns_buffer_printf(output, "\\%03u",
(unsigned)(uint8_t) ch);
}
amount--;
}
}
ldns_status
ldns_rdf2buffer_str_str(ldns_buffer *output, const ldns_rdf *rdf)
{
if(ldns_rdf_size(rdf) < 1) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
if((int)ldns_rdf_size(rdf) < (int)ldns_rdf_data(rdf)[0] + 1) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
ldns_buffer_printf(output, "\"");
ldns_characters2buffer_str(output,
ldns_rdf_data(rdf)[0], ldns_rdf_data(rdf) + 1);
ldns_buffer_printf(output, "\"");
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_b64(ldns_buffer *output, const ldns_rdf *rdf)
{
size_t size;
char *b64;
if (ldns_rdf_size(rdf) == 0) {
ldns_buffer_printf(output, "0");
return ldns_buffer_status(output);
} else
size = ldns_b64_ntop_calculate_size(ldns_rdf_size(rdf));
if (!(b64 = LDNS_XMALLOC(char, size)))
return LDNS_STATUS_MEM_ERR;
if (ldns_b64_ntop(ldns_rdf_data(rdf), ldns_rdf_size(rdf), b64, size)) {
ldns_buffer_printf(output, "%s", b64);
}
LDNS_FREE(b64);
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_b32_ext(ldns_buffer *output, const ldns_rdf *rdf)
{
size_t size;
char *b32;
if(ldns_rdf_size(rdf) == 0)
return LDNS_STATUS_OK;
/* remove -1 for the b32-hash-len octet */
size = ldns_b32_ntop_calculate_size(ldns_rdf_size(rdf) - 1);
/* add one for the end nul for the string */
b32 = LDNS_XMALLOC(char, size + 1);
if(!b32) return LDNS_STATUS_MEM_ERR;
size = (size_t) ldns_b32_ntop_extended_hex(ldns_rdf_data(rdf) + 1,
ldns_rdf_size(rdf) - 1, b32, size+1);
if (size > 0) {
ldns_buffer_printf(output, "%s", b32);
}
LDNS_FREE(b32);
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_hex(ldns_buffer *output, const ldns_rdf *rdf)
{
size_t i;
for (i = 0; i < ldns_rdf_size(rdf); i++) {
ldns_buffer_printf(output, "%02x", ldns_rdf_data(rdf)[i]);
}
return ldns_buffer_status(output);
}
static ldns_status
ldns_rdf2buffer_str_type_fmt(ldns_buffer *output,
const ldns_output_format* fmt, const ldns_rdf *rdf)
{
uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
if (! ldns_output_format_covers_type(fmt, data) &&
ldns_rr_descript(data) &&
ldns_rr_descript(data)->_name) {
ldns_buffer_printf(output, "%s",ldns_rr_descript(data)->_name);
} else {
ldns_buffer_printf(output, "TYPE%u", data);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_type(ldns_buffer *output, const ldns_rdf *rdf)
{
return ldns_rdf2buffer_str_type_fmt(output,
ldns_output_format_default, rdf);
}
ldns_status
ldns_rdf2buffer_str_class(ldns_buffer *output, const ldns_rdf *rdf)
{
uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
ldns_lookup_table *lt;
lt = ldns_lookup_by_id(ldns_rr_classes, (int) data);
if (lt) {
ldns_buffer_printf(output, "\t%s", lt->name);
} else {
ldns_buffer_printf(output, "\tCLASS%d", data);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_cert_alg(ldns_buffer *output, const ldns_rdf *rdf)
{
uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
ldns_lookup_table *lt;
lt = ldns_lookup_by_id(ldns_cert_algorithms, (int) data);
if (lt) {
ldns_buffer_printf(output, "%s", lt->name);
} else {
ldns_buffer_printf(output, "%d", data);
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_alg(ldns_buffer *output, const ldns_rdf *rdf)
{
return ldns_rdf2buffer_str_int8(output, rdf);
}
static void
loc_cm_print(ldns_buffer *output, uint8_t mantissa, uint8_t exponent)
{
uint8_t i;
/* is it 0.<two digits> ? */
if(exponent < 2) {
if(exponent == 1)
mantissa *= 10;
ldns_buffer_printf(output, "0.%02ld", (long)mantissa);
return;
}
/* always <digit><string of zeros> */
ldns_buffer_printf(output, "%d", (int)mantissa);
for(i=0; i<exponent-2; i++)
ldns_buffer_printf(output, "0");
}
ldns_status
ldns_rr_type2buffer_str(ldns_buffer *output, const ldns_rr_type type)
{
const ldns_rr_descriptor *descriptor;
descriptor = ldns_rr_descript(type);
switch (type) {
case LDNS_RR_TYPE_IXFR:
ldns_buffer_printf(output, "IXFR");
break;
case LDNS_RR_TYPE_AXFR:
ldns_buffer_printf(output, "AXFR");
break;
case LDNS_RR_TYPE_MAILA:
ldns_buffer_printf(output, "MAILA");
break;
case LDNS_RR_TYPE_MAILB:
ldns_buffer_printf(output, "MAILB");
break;
case LDNS_RR_TYPE_ANY:
ldns_buffer_printf(output, "ANY");
break;
default:
if (descriptor && descriptor->_name) {
ldns_buffer_printf(output, "%s", descriptor->_name);
} else {
ldns_buffer_printf(output, "TYPE%u", type);
}
}
return ldns_buffer_status(output);
}
char *
ldns_rr_type2str(const ldns_rr_type type)
{
char *str;
ldns_buffer *buf;
buf = ldns_buffer_new(10);
if (!buf) {
return NULL;
}
str = NULL;
if (ldns_rr_type2buffer_str(buf, type) == LDNS_STATUS_OK) {
str = ldns_buffer_export2str(buf);
}
ldns_buffer_free(buf);
return str;
}
ldns_status
ldns_rr_class2buffer_str(ldns_buffer *output,
const ldns_rr_class klass)
{
ldns_lookup_table *lt;
lt = ldns_lookup_by_id(ldns_rr_classes, klass);
if (lt) {
ldns_buffer_printf(output, "%s", lt->name);
} else {
ldns_buffer_printf(output, "CLASS%d", klass);
}
return ldns_buffer_status(output);
}
char *
ldns_rr_class2str(const ldns_rr_class klass)
{
ldns_buffer *buf;
char *str;
buf = ldns_buffer_new(10);
if (!buf) {
return NULL;
}
str = NULL;
if (ldns_rr_class2buffer_str(buf, klass) == LDNS_STATUS_OK) {
str = ldns_buffer_export2str(buf);
}
ldns_buffer_free(buf);
return str;
}
ldns_status
ldns_rdf2buffer_str_loc(ldns_buffer *output, const ldns_rdf *rdf)
{
/* we could do checking (ie degrees < 90 etc)? */
uint8_t version;
uint8_t size;
uint8_t horizontal_precision;
uint8_t vertical_precision;
uint32_t longitude;
uint32_t latitude;
uint32_t altitude;
char latitude_hemisphere;
char longitude_hemisphere;
uint32_t h;
uint32_t m;
double s;
uint32_t equator = (uint32_t) ldns_power(2, 31);
if(ldns_rdf_size(rdf) < 1) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
version = ldns_rdf_data(rdf)[0];
if (version == 0) {
if(ldns_rdf_size(rdf) < 16) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
size = ldns_rdf_data(rdf)[1];
horizontal_precision = ldns_rdf_data(rdf)[2];
vertical_precision = ldns_rdf_data(rdf)[3];
latitude = ldns_read_uint32(&ldns_rdf_data(rdf)[4]);
longitude = ldns_read_uint32(&ldns_rdf_data(rdf)[8]);
altitude = ldns_read_uint32(&ldns_rdf_data(rdf)[12]);
if (latitude > equator) {
latitude_hemisphere = 'N';
latitude = latitude - equator;
} else {
latitude_hemisphere = 'S';
latitude = equator - latitude;
}
h = latitude / (1000 * 60 * 60);
latitude = latitude % (1000 * 60 * 60);
m = latitude / (1000 * 60);
latitude = latitude % (1000 * 60);
s = (double) latitude / 1000.0;
ldns_buffer_printf(output, "%02u %02u %0.3f %c ",
h, m, s, latitude_hemisphere);
if (longitude > equator) {
longitude_hemisphere = 'E';
longitude = longitude - equator;
} else {
longitude_hemisphere = 'W';
longitude = equator - longitude;
}
h = longitude / (1000 * 60 * 60);
longitude = longitude % (1000 * 60 * 60);
m = longitude / (1000 * 60);
longitude = longitude % (1000 * 60);
s = (double) longitude / (1000.0);
ldns_buffer_printf(output, "%02u %02u %0.3f %c ",
h, m, s, longitude_hemisphere);
s = ((double) altitude) / 100;
s -= 100000;
if(altitude%100 != 0)
ldns_buffer_printf(output, "%.2f", s);
else
ldns_buffer_printf(output, "%.0f", s);
ldns_buffer_printf(output, "m ");
loc_cm_print(output, (size & 0xf0) >> 4, size & 0x0f);
ldns_buffer_printf(output, "m ");
loc_cm_print(output, (horizontal_precision & 0xf0) >> 4,
horizontal_precision & 0x0f);
ldns_buffer_printf(output, "m ");
loc_cm_print(output, (vertical_precision & 0xf0) >> 4,
vertical_precision & 0x0f);
ldns_buffer_printf(output, "m");
return ldns_buffer_status(output);
} else {
return ldns_rdf2buffer_str_hex(output, rdf);
}
}
ldns_status
ldns_rdf2buffer_str_unknown(ldns_buffer *output, const ldns_rdf *rdf)
{
ldns_buffer_printf(output, "\\# %u ", ldns_rdf_size(rdf));
return ldns_rdf2buffer_str_hex(output, rdf);
}
ldns_status
ldns_rdf2buffer_str_nsap(ldns_buffer *output, const ldns_rdf *rdf)
{
ldns_buffer_printf(output, "0x");
return ldns_rdf2buffer_str_hex(output, rdf);
}
ldns_status
ldns_rdf2buffer_str_atma(ldns_buffer *output, const ldns_rdf *rdf)
{
return ldns_rdf2buffer_str_hex(output, rdf);
}
ldns_status
ldns_rdf2buffer_str_wks(ldns_buffer *output, const ldns_rdf *rdf)
{
/* protocol, followed by bitmap of services */
struct protoent *protocol;
char *proto_name = NULL;
uint8_t protocol_nr;
struct servent *service;
uint16_t current_service;
if(ldns_rdf_size(rdf) < 1) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
protocol_nr = ldns_rdf_data(rdf)[0];
protocol = getprotobynumber((int) protocol_nr);
if (protocol && (protocol->p_name != NULL)) {
proto_name = protocol->p_name;
ldns_buffer_printf(output, "%s ", protocol->p_name);
} else {
ldns_buffer_printf(output, "%u ", protocol_nr);
}
#ifdef HAVE_ENDPROTOENT
endprotoent();
#endif
for (current_service = 0;
current_service < (ldns_rdf_size(rdf)-1)*8; current_service++) {
if (ldns_get_bit(&(ldns_rdf_data(rdf)[1]), current_service)) {
service = getservbyport((int) htons(current_service),
proto_name);
if (service && service->s_name) {
ldns_buffer_printf(output, "%s ", service->s_name);
} else {
ldns_buffer_printf(output, "%u ", current_service);
}
#ifdef HAVE_ENDSERVENT
endservent();
#endif
}
/* exit from loop before integer overflow */
if(current_service == 65535) { break; }
}
return ldns_buffer_status(output);
}
static ldns_status
ldns_rdf2buffer_str_nsec_fmt(ldns_buffer *output,
const ldns_output_format* fmt, const ldns_rdf *rdf)
{
/* Note: this code is duplicated in higher.c in
* ldns_nsec_type_check() function
*/
uint8_t window_block_nr;
uint8_t bitmap_length;
uint16_t type;
uint16_t pos = 0;
uint16_t bit_pos;
uint8_t *data = ldns_rdf_data(rdf);
while((size_t)(pos + 2) < ldns_rdf_size(rdf)) {
window_block_nr = data[pos];
bitmap_length = data[pos + 1];
pos += 2;
if (ldns_rdf_size(rdf) < pos + bitmap_length) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
for (bit_pos = 0; bit_pos < (bitmap_length) * 8; bit_pos++) {
if (! ldns_get_bit(&data[pos], bit_pos)) {
continue;
}
type = 256 * (uint16_t) window_block_nr + bit_pos;
if (! ldns_output_format_covers_type(fmt, type) &&
ldns_rr_descript(type) &&
ldns_rr_descript(type)->_name){
ldns_buffer_printf(output, "%s ",
ldns_rr_descript(type)->_name);
} else {
ldns_buffer_printf(output, "TYPE%u ", type);
}
}
pos += (uint16_t) bitmap_length;
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_nsec(ldns_buffer *output, const ldns_rdf *rdf)
{
return ldns_rdf2buffer_str_nsec_fmt(output,
ldns_output_format_default, rdf);
}
ldns_status
ldns_rdf2buffer_str_nsec3_salt(ldns_buffer *output, const ldns_rdf *rdf)
{
uint8_t salt_length;
uint8_t salt_pos;
uint8_t *data = ldns_rdf_data(rdf);
if(ldns_rdf_size(rdf) < 1) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
salt_length = data[0];
/* from now there are variable length entries so remember pos */
if (salt_length == 0 || ((size_t)salt_length)+1 > ldns_rdf_size(rdf)) {
ldns_buffer_printf(output, "- ");
} else {
for (salt_pos = 0; salt_pos < salt_length; salt_pos++) {
ldns_buffer_printf(output, "%02x", data[1 + salt_pos]);
}
ldns_buffer_printf(output, " ");
}
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_period(ldns_buffer *output, const ldns_rdf *rdf)
{
/* period is the number of seconds */
if (ldns_rdf_size(rdf) != 4) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
ldns_buffer_printf(output, "%u", ldns_read_uint32(ldns_rdf_data(rdf)));
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_tsigtime(ldns_buffer *output,const ldns_rdf *rdf)
{
/* tsigtime is 48 bits network order unsigned integer */
uint64_t tsigtime = 0;
uint8_t *data = ldns_rdf_data(rdf);
uint64_t d0, d1, d2, d3, d4, d5;
if (ldns_rdf_size(rdf) < 6) {
return LDNS_STATUS_WIRE_RDATA_ERR;
}
d0 = data[0]; /* cast to uint64 for shift operations */
d1 = data[1];
d2 = data[2];
d3 = data[3];
d4 = data[4];
d5 = data[5];
tsigtime = (d0<<40) | (d1<<32) | (d2<<24) | (d3<<16) | (d4<<8) | d5;
ldns_buffer_printf(output, "%llu ", (long long)tsigtime);
return ldns_buffer_status(output);
}
ldns_status
ldns_rdf2buffer_str_apl(ldns_buffer *output, const ldns_rdf *rdf)
{
uint8_t *data = ldns_rdf_data(rdf);
uint16_t address_family;
uint8_t prefix;
bool negation;
uint8_t adf_length;
size_t i;
size_t pos = 0;
while (pos < (unsigned int) ldns_rdf_size(rdf)) {
if(pos + 3 >= (unsigned)ldns_rdf_size(rdf))
return LDNS_STATUS_WIRE_RDATA_ERR;
address_family = ldns_read_uint16(&data[pos]);
prefix = data[pos + 2];
negation = data[pos + 3] & LDNS_APL_NEGATION;
adf_length = data[pos + 3] & LDNS_APL_MASK;
if (address_family == LDNS_APL_IP4) {
/* check if prefix < 32? */
if (negation) {
ldns_buffer_printf(output, "!");
}
ldns_buffer_printf(output, "%u:", address_family);
/* address is variable length 0 - 4 */
for (i = 0; i < 4; i++) {
if (i > 0) {
ldns_buffer_printf(output, ".");
}