ldns  1.8.3
/build/ldns-FYPZzC/ldns-1.8.3/host2str.c
Go to the documentation of this file.
1/*
2 * host2str.c
3 *
4 * conversion routines from the host format
5 * to the presentation format (strings)
6 *
7 * a Net::DNS like library for C
8 *
9 * (c) NLnet Labs, 2004-2006
10 *
11 * See the file LICENSE for the license
12 */
13#include <ldns/config.h>
14
15#include <ldns/ldns.h>
16
17#include <limits.h>
18
19#ifdef HAVE_SYS_SOCKET_H
20#include <sys/socket.h>
21#endif
22#ifdef HAVE_ARPA_INET_H
23#include <arpa/inet.h>
24#endif
25#ifdef HAVE_NETDB_H
26#include <netdb.h>
27#endif
28#include <time.h>
29#include <sys/time.h>
30
31#ifdef HAVE_SSL
32#include <openssl/bn.h>
33#include <openssl/rsa.h>
34#ifdef USE_DSA
35#include <openssl/dsa.h>
36#endif
37#endif
38
39#ifndef INET_ADDRSTRLEN
40#define INET_ADDRSTRLEN 16
41#endif
42#ifndef INET6_ADDRSTRLEN
43#define INET6_ADDRSTRLEN 46
44#endif
45
46/* Internal helper function */
49
50/* lookup tables for standard DNS stuff */
51
52/* Taken from RFC 2535, section 7. */
54 { LDNS_RSAMD5, "RSAMD5" },
55 { LDNS_DH, "DH" },
56 { LDNS_DSA, "DSA" },
57 { LDNS_ECC, "ECC" },
58 { LDNS_RSASHA1, "RSASHA1" },
59 { LDNS_DSA_NSEC3, "DSA-NSEC3-SHA1" },
60 { LDNS_RSASHA1_NSEC3, "RSASHA1-NSEC3-SHA1" },
61 { LDNS_RSASHA256, "RSASHA256"},
62 { LDNS_RSASHA512, "RSASHA512"},
63 { LDNS_ECC_GOST, "ECC-GOST"},
64 { LDNS_ECDSAP256SHA256, "ECDSAP256SHA256"},
65 { LDNS_ECDSAP384SHA384, "ECDSAP384SHA384"},
66 { LDNS_ED25519, "ED25519"},
67 { LDNS_ED448, "ED448"},
68 { LDNS_INDIRECT, "INDIRECT" },
69 { LDNS_PRIVATEDNS, "PRIVATEDNS" },
70 { LDNS_PRIVATEOID, "PRIVATEOID" },
71 { 0, NULL }
72};
73
74/* Hashing algorithms used in the DS record */
76 {LDNS_SHA1 , "SHA1" }, /* RFC 4034 */
77 {LDNS_SHA256 , "SHA256" }, /* RFC 4509 */
78 {LDNS_HASH_GOST, "HASH-GOST" }, /* RFC 5933 */
79 {LDNS_SHA384 , "SHA384" }, /* RFC 6605 */
80 { 0, NULL }
81};
82
83/* Taken from RFC 4398 */
85 { LDNS_CERT_PKIX, "PKIX" },
86 { LDNS_CERT_SPKI, "SPKI" },
87 { LDNS_CERT_PGP, "PGP" },
88 { LDNS_CERT_IPKIX, "IPKIX" },
89 { LDNS_CERT_ISPKI, "ISPKI" },
90 { LDNS_CERT_IPGP, "IPGP" },
91 { LDNS_CERT_ACPKIX, "ACPKIX" },
92 { LDNS_CERT_IACPKIX, "IACPKIX" },
93 { LDNS_CERT_URI, "URI" },
94 { LDNS_CERT_OID, "OID" },
95 { 0, NULL }
96};
97
98/* classes */
100 { LDNS_RR_CLASS_IN, "IN" },
101 { LDNS_RR_CLASS_CH, "CH" },
102 { LDNS_RR_CLASS_HS, "HS" },
103 { LDNS_RR_CLASS_NONE, "NONE" },
104 { LDNS_RR_CLASS_ANY, "ANY" },
105 { 0, NULL }
106};
107
108/* if these are used elsewhere */
110 { LDNS_RCODE_NOERROR, "NOERROR" },
111 { LDNS_RCODE_FORMERR, "FORMERR" },
112 { LDNS_RCODE_SERVFAIL, "SERVFAIL" },
113 { LDNS_RCODE_NXDOMAIN, "NXDOMAIN" },
114 { LDNS_RCODE_NOTIMPL, "NOTIMPL" },
115 { LDNS_RCODE_REFUSED, "REFUSED" },
116 { LDNS_RCODE_YXDOMAIN, "YXDOMAIN" },
117 { LDNS_RCODE_YXRRSET, "YXRRSET" },
118 { LDNS_RCODE_NXRRSET, "NXRRSET" },
119 { LDNS_RCODE_NOTAUTH, "NOTAUTH" },
120 { LDNS_RCODE_NOTZONE, "NOTZONE" },
121 { 0, NULL }
122};
123
125 { LDNS_PACKET_QUERY, "QUERY" },
126 { LDNS_PACKET_IQUERY, "IQUERY" },
127 { LDNS_PACKET_STATUS, "STATUS" },
128 { LDNS_PACKET_NOTIFY, "NOTIFY" },
129 { LDNS_PACKET_UPDATE, "UPDATE" },
130 { 0, NULL }
131};
132
137 LDNS_COMMENT_KEY, NULL
138};
143
146};
149
150static bool
151ldns_output_format_covers_type(const ldns_output_format* fmt, ldns_rr_type t)
152{
153 return fmt && (fmt->flags & LDNS_FMT_RFC3597) &&
154 ((ldns_output_format_storage*)fmt)->bitmap &&
156 ((ldns_output_format_storage*)fmt)->bitmap, t);
157}
158
161{
163 ldns_status s;
164
165 assert(fmt != NULL);
166
167 if (!(fmt_st->flags & LDNS_FMT_RFC3597)) {
168 ldns_output_format_set(fmt, LDNS_FMT_RFC3597);
169 }
170 if (! fmt_st->bitmap) {
172 if (s != LDNS_STATUS_OK) {
173 return s;
174 }
175 }
176 return ldns_nsec_bitmap_set_type(fmt_st->bitmap, t);
177}
178
181{
183 ldns_status s;
184
185 assert(fmt != NULL);
186
187 if (!(fmt_st->flags & LDNS_FMT_RFC3597)) {
188 ldns_output_format_set(fmt, LDNS_FMT_RFC3597);
189 }
190 if (! fmt_st->bitmap) {
192 if (s != LDNS_STATUS_OK) {
193 return s;
194 }
195 }
196 return ldns_nsec_bitmap_clear_type(fmt_st->bitmap, t);
197}
198
201{
203 if (lt && lt->name) {
204 ldns_buffer_printf(output, "%s", lt->name);
205 } else {
206 ldns_buffer_printf(output, "OPCODE%u", opcode);
207 }
208 return ldns_buffer_status(output);
209}
210
213{
215 if (lt && lt->name) {
216 ldns_buffer_printf(output, "%s", lt->name);
217 } else {
218 ldns_buffer_printf(output, "RCODE%u", rcode);
219 }
220 return ldns_buffer_status(output);
221}
222
225 ldns_algorithm algorithm)
226{
228 algorithm);
229 if (lt && lt->name) {
230 ldns_buffer_printf(output, "%s", lt->name);
231 } else {
232 ldns_buffer_printf(output, "ALG%u", algorithm);
233 }
234 return ldns_buffer_status(output);
235}
236
239 ldns_cert_algorithm cert_algorithm)
240{
242 cert_algorithm);
243 if (lt && lt->name) {
244 ldns_buffer_printf(output, "%s", lt->name);
245 } else {
246 ldns_buffer_printf(output, "CERT_ALG%u",
247 cert_algorithm);
248 }
249 return ldns_buffer_status(output);
250}
251
252char *
254{
255 char *str;
256 ldns_buffer *buf;
257
258 buf = ldns_buffer_new(12);
259 if (!buf) {
260 return NULL;
261 }
262
263 str = NULL;
264 if (ldns_pkt_opcode2buffer_str(buf, opcode) == LDNS_STATUS_OK) {
265 str = ldns_buffer_export2str(buf);
266 }
267
268 ldns_buffer_free(buf);
269 return str;
270}
271
272char *
274{
275 char *str;
276 ldns_buffer *buf;
277
278 buf = ldns_buffer_new(10);
279 if (!buf) {
280 return NULL;
281 }
282
283 str = NULL;
284 if (ldns_pkt_rcode2buffer_str(buf, rcode) == LDNS_STATUS_OK) {
285 str = ldns_buffer_export2str(buf);
286 }
287
288 ldns_buffer_free(buf);
289 return str;
290}
291
292char *
294{
295 char *str;
296 ldns_buffer *buf;
297
298 buf = ldns_buffer_new(10);
299 if (!buf) {
300 return NULL;
301 }
302
303 str = NULL;
304 if (ldns_algorithm2buffer_str(buf, algorithm)
305 == LDNS_STATUS_OK) {
306 str = ldns_buffer_export2str(buf);
307 }
308
309 ldns_buffer_free(buf);
310 return str;
311}
312
313char *
315{
316 char *str;
317 ldns_buffer *buf;
318
319 buf = ldns_buffer_new(10);
320 if (!buf) {
321 return NULL;
322 }
323
324 str = NULL;
325 if (ldns_cert_algorithm2buffer_str(buf, cert_algorithm)
326 == LDNS_STATUS_OK) {
327 str = ldns_buffer_export2str(buf);
328 }
329
330 ldns_buffer_free(buf);
331 return str;
332}
333
334
335/* do NOT pass compressed data here :p */
338{
339 /* can we do with 1 pos var? or without at all? */
340 uint8_t src_pos = 0;
341 uint8_t len;
342 uint8_t *data;
343 uint8_t i;
344 unsigned char c;
345
346 data = (uint8_t*)ldns_rdf_data(dname);
347 len = data[src_pos];
348
349 if (ldns_rdf_size(dname) > LDNS_MAX_DOMAINLEN) {
350 /* too large, return */
352 }
353
354 /* special case: root label */
355 if (1 == ldns_rdf_size(dname)) {
356 ldns_buffer_printf(output, ".");
357 } else {
358 while ((len > 0) && src_pos < ldns_rdf_size(dname)) {
359 src_pos++;
360 for(i = 0; i < len; i++) {
361 /* paranoia check for various 'strange'
362 characters in dnames
363 */
364 c = (unsigned char) data[src_pos];
365 if(c == '.' || c == ';' ||
366 c == '(' || c == ')' ||
367 c == '\\') {
368 ldns_buffer_printf(output, "\\%c",
369 data[src_pos]);
370 } else if (!(isascii(c) && isgraph(c))) {
371 ldns_buffer_printf(output, "\\%03u",
372 data[src_pos]);
373 } else {
374 ldns_buffer_printf(output, "%c", data[src_pos]);
375 }
376 src_pos++;
377 }
378
379 if (src_pos < ldns_rdf_size(dname)) {
380 ldns_buffer_printf(output, ".");
381 }
382 len = data[src_pos];
383 }
384 }
385 return ldns_buffer_status(output);
386}
387
390{
391 uint8_t data = ldns_rdf_data(rdf)[0];
392 ldns_buffer_printf(output, "%lu", (unsigned long) data);
393 return ldns_buffer_status(output);
394}
395
398{
399 uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
400 ldns_buffer_printf(output, "%lu", (unsigned long) data);
401 return ldns_buffer_status(output);
402}
403
406{
407 uint32_t data = ldns_read_uint32(ldns_rdf_data(rdf));
408 ldns_buffer_printf(output, "%lu", (unsigned long) data);
409 return ldns_buffer_status(output);
410}
411
414{
415 /* create a YYYYMMDDHHMMSS string if possible */
416 struct tm tm;
417 char date_buf[16];
418
419 memset(&tm, 0, sizeof(tm));
421 && strftime(date_buf, 15, "%Y%m%d%H%M%S", &tm)) {
422 ldns_buffer_printf(output, "%s", date_buf);
423 }
424 return ldns_buffer_status(output);
425}
426
429{
430 char str[INET_ADDRSTRLEN];
431
432 if (inet_ntop(AF_INET, ldns_rdf_data(rdf), str, INET_ADDRSTRLEN)) {
433 ldns_buffer_printf(output, "%s", str);
434 }
435 return ldns_buffer_status(output);
436}
437
440{
441 char str[INET6_ADDRSTRLEN];
442
443 if (inet_ntop(AF_INET6, ldns_rdf_data(rdf), str, INET6_ADDRSTRLEN)) {
444 ldns_buffer_printf(output, "%s", str);
445 }
446
447 return ldns_buffer_status(output);
448}
449
450static void
451ldns_characters2buffer_str(ldns_buffer* output,
452 size_t amount, const uint8_t* characters)
453{
454 uint8_t ch;
455 while (amount > 0) {
456 ch = *characters++;
457 if (isprint((int)ch) || ch == '\t') {
458 if (ch == '\"' || ch == '\\')
459 ldns_buffer_printf(output, "\\%c", ch);
460 else
461 ldns_buffer_printf(output, "%c", ch);
462 } else {
463 ldns_buffer_printf(output, "\\%03u",
464 (unsigned)(uint8_t) ch);
465 }
466 amount--;
467 }
468}
469
472{
473 if(ldns_rdf_size(rdf) < 1) {
475 }
476 if((int)ldns_rdf_size(rdf) < (int)ldns_rdf_data(rdf)[0] + 1) {
478 }
479 ldns_buffer_printf(output, "\"");
480 ldns_characters2buffer_str(output,
481 ldns_rdf_data(rdf)[0], ldns_rdf_data(rdf) + 1);
482 ldns_buffer_printf(output, "\"");
483 return ldns_buffer_status(output);
484}
485