ldns  1.7.0
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 #ifndef INET_ADDRSTRLEN
32 #define INET_ADDRSTRLEN 16
33 #endif
34 #ifndef INET6_ADDRSTRLEN
35 #define INET6_ADDRSTRLEN 46
36 #endif
37 
38 /* lookup tables for standard DNS stuff */
39 
40 /* Taken from RFC 2535, section 7. */
42  { LDNS_RSAMD5, "RSAMD5" },
43  { LDNS_DH, "DH" },
44  { LDNS_DSA, "DSA" },
45  { LDNS_ECC, "ECC" },
46  { LDNS_RSASHA1, "RSASHA1" },
47  { LDNS_DSA_NSEC3, "DSA-NSEC3-SHA1" },
48  { LDNS_RSASHA1_NSEC3, "RSASHA1-NSEC3-SHA1" },
49 #ifdef USE_SHA2
50  { LDNS_RSASHA256, "RSASHA256"},
51  { LDNS_RSASHA512, "RSASHA512"},
52 #endif
53 #ifdef USE_GOST
54  { LDNS_ECC_GOST, "ECC-GOST"},
55 #endif
56 #ifdef USE_ECDSA
57  { LDNS_ECDSAP256SHA256, "ECDSAP256SHA256"},
58  { LDNS_ECDSAP384SHA384, "ECDSAP384SHA384"},
59 #endif
60 #ifdef USE_ED25519
61  { LDNS_ED25519, "ED25519"},
62 #endif
63 #ifdef USE_ED448
64  { LDNS_ED448, "ED448"},
65 #endif
66  { LDNS_INDIRECT, "INDIRECT" },
67  { LDNS_PRIVATEDNS, "PRIVATEDNS" },
68  { LDNS_PRIVATEOID, "PRIVATEOID" },
69  { 0, NULL }
70 };
71 
72 /* Taken from RFC 4398 */
74  { LDNS_CERT_PKIX, "PKIX" },
75  { LDNS_CERT_SPKI, "SPKI" },
76  { LDNS_CERT_PGP, "PGP" },
77  { LDNS_CERT_IPKIX, "IPKIX" },
78  { LDNS_CERT_ISPKI, "ISPKI" },
79  { LDNS_CERT_IPGP, "IPGP" },
80  { LDNS_CERT_ACPKIX, "ACPKIX" },
81  { LDNS_CERT_IACPKIX, "IACPKIX" },
82  { LDNS_CERT_URI, "URI" },
83  { LDNS_CERT_OID, "OID" },
84  { 0, NULL }
85 };
86 
87 /* classes */
89  { LDNS_RR_CLASS_IN, "IN" },
90  { LDNS_RR_CLASS_CH, "CH" },
91  { LDNS_RR_CLASS_HS, "HS" },
92  { LDNS_RR_CLASS_NONE, "NONE" },
93  { LDNS_RR_CLASS_ANY, "ANY" },
94  { 0, NULL }
95 };
96 
97 /* if these are used elsewhere */
99  { LDNS_RCODE_NOERROR, "NOERROR" },
100  { LDNS_RCODE_FORMERR, "FORMERR" },
101  { LDNS_RCODE_SERVFAIL, "SERVFAIL" },
102  { LDNS_RCODE_NXDOMAIN, "NXDOMAIN" },
103  { LDNS_RCODE_NOTIMPL, "NOTIMPL" },
104  { LDNS_RCODE_REFUSED, "REFUSED" },
105  { LDNS_RCODE_YXDOMAIN, "YXDOMAIN" },
106  { LDNS_RCODE_YXRRSET, "YXRRSET" },
107  { LDNS_RCODE_NXRRSET, "NXRRSET" },
108  { LDNS_RCODE_NOTAUTH, "NOTAUTH" },
109  { LDNS_RCODE_NOTZONE, "NOTZONE" },
110  { 0, NULL }
111 };
112 
114  { LDNS_PACKET_QUERY, "QUERY" },
115  { LDNS_PACKET_IQUERY, "IQUERY" },
116  { LDNS_PACKET_STATUS, "STATUS" },
117  { LDNS_PACKET_NOTIFY, "NOTIFY" },
118  { LDNS_PACKET_UPDATE, "UPDATE" },
119  { 0, NULL }
120 };
121 
126  LDNS_COMMENT_KEY, NULL
127 };
132 
135 };
138 
139 static bool
140 ldns_output_format_covers_type(const ldns_output_format* fmt, ldns_rr_type t)
141 {
142  return fmt && (fmt->flags & LDNS_FMT_RFC3597) &&
143  ((ldns_output_format_storage*)fmt)->bitmap &&
145  ((ldns_output_format_storage*)fmt)->bitmap, t);
146 }
147 
150 {
152  ldns_status s;
153 
154  assert(fmt != NULL);
155 
156  if (!(fmt_st->flags & LDNS_FMT_RFC3597)) {
157  ldns_output_format_set(fmt, LDNS_FMT_RFC3597);
158  }
159  if (! fmt_st->bitmap) {
161  if (s != LDNS_STATUS_OK) {
162  return s;
163  }
164  }
165  return ldns_nsec_bitmap_set_type(fmt_st->bitmap, t);
166 }
167 
170 {
172  ldns_status s;
173 
174  assert(fmt != NULL);
175 
176  if (!(fmt_st->flags & LDNS_FMT_RFC3597)) {
177  ldns_output_format_set(fmt, LDNS_FMT_RFC3597);
178  }
179  if (! fmt_st->bitmap) {
181  if (s != LDNS_STATUS_OK) {
182  return s;
183  }
184  }
185  return ldns_nsec_bitmap_clear_type(fmt_st->bitmap, t);
186 }
187 
190 {
191  ldns_lookup_table *lt = ldns_lookup_by_id(ldns_opcodes, opcode);
192  if (lt && lt->name) {
193  ldns_buffer_printf(output, "%s", lt->name);
194  } else {
195  ldns_buffer_printf(output, "OPCODE%u", opcode);
196  }
197  return ldns_buffer_status(output);
198 }
199 
202 {
203  ldns_lookup_table *lt = ldns_lookup_by_id(ldns_rcodes, rcode);
204  if (lt && lt->name) {
205  ldns_buffer_printf(output, "%s", lt->name);
206  } else {
207  ldns_buffer_printf(output, "RCODE%u", rcode);
208  }
209  return ldns_buffer_status(output);
210 }
211 
214  ldns_algorithm algorithm)
215 {
216  ldns_lookup_table *lt = ldns_lookup_by_id(ldns_algorithms,
217  algorithm);
218  if (lt && lt->name) {
219  ldns_buffer_printf(output, "%s", lt->name);
220  } else {
221  ldns_buffer_printf(output, "ALG%u", algorithm);
222  }
223  return ldns_buffer_status(output);
224 }
225 
228  ldns_cert_algorithm cert_algorithm)
229 {
230  ldns_lookup_table *lt = ldns_lookup_by_id(ldns_cert_algorithms,
231  cert_algorithm);
232  if (lt && lt->name) {
233  ldns_buffer_printf(output, "%s", lt->name);
234  } else {
235  ldns_buffer_printf(output, "CERT_ALG%u",
236  cert_algorithm);
237  }
238  return ldns_buffer_status(output);
239 }
240 
241 char *
243 {
244  char *str;
245  ldns_buffer *buf;
246 
247  buf = ldns_buffer_new(12);
248  if (!buf) {
249  return NULL;
250  }
251 
252  str = NULL;
253  if (ldns_pkt_opcode2buffer_str(buf, opcode) == LDNS_STATUS_OK) {
254  str = ldns_buffer_export2str(buf);
255  }
256 
257  ldns_buffer_free(buf);
258  return str;
259 }
260 
261 char *
263 {
264  char *str;
265  ldns_buffer *buf;
266 
267  buf = ldns_buffer_new(10);
268  if (!buf) {
269  return NULL;
270  }
271 
272  str = NULL;
273  if (ldns_pkt_rcode2buffer_str(buf, rcode) == LDNS_STATUS_OK) {
274  str = ldns_buffer_export2str(buf);
275  }
276 
277  ldns_buffer_free(buf);
278  return str;
279 }
280 
281 char *
283 {
284  char *str;
285  ldns_buffer *buf;
286 
287  buf = ldns_buffer_new(10);
288  if (!buf) {
289  return NULL;
290  }
291 
292  str = NULL;
293  if (ldns_algorithm2buffer_str(buf, algorithm)
294  == LDNS_STATUS_OK) {
295  str = ldns_buffer_export2str(buf);
296  }
297 
298  ldns_buffer_free(buf);
299  return str;
300 }
301 
302 char *
304 {
305  char *str;
306  ldns_buffer *buf;
307 
308  buf = ldns_buffer_new(10);
309  if (!buf) {
310  return NULL;
311  }
312 
313  str = NULL;
314  if (ldns_cert_algorithm2buffer_str(buf, cert_algorithm)
315  == LDNS_STATUS_OK) {
316  str = ldns_buffer_export2str(buf);
317  }
318 
319  ldns_buffer_free(buf);
320  return str;
321 }
322 
323 
324 /* do NOT pass compressed data here :p */
327 {
328  /* can we do with 1 pos var? or without at all? */
329  uint8_t src_pos = 0;
330  uint8_t len;
331  uint8_t *data;
332  uint8_t i;
333  unsigned char c;
334 
335  data = (uint8_t*)ldns_rdf_data(dname);
336  len = data[src_pos];
337 
338  if (ldns_rdf_size(dname) > LDNS_MAX_DOMAINLEN) {
339  /* too large, return */
341  }
342 
343  /* special case: root label */
344  if (1 == ldns_rdf_size(dname)) {
345  ldns_buffer_printf(output, ".");
346  } else {
347  while ((len > 0) && src_pos < ldns_rdf_size(dname)) {
348  src_pos++;
349  for(i = 0; i < len; i++) {
350  /* paranoia check for various 'strange'
351  characters in dnames
352  */
353  c = (unsigned char) data[src_pos];
354  if(c == '.' || c == ';' ||
355  c == '(' || c == ')' ||
356  c == '\\') {
357  ldns_buffer_printf(output, "\\%c",
358  data[src_pos]);
359  } else if (!(isascii(c) && isgraph(c))) {
360  ldns_buffer_printf(output, "\\%03u",
361  data[src_pos]);
362  } else {
363  ldns_buffer_printf(output, "%c", data[src_pos]);
364  }
365  src_pos++;
366  }
367 
368  if (src_pos < ldns_rdf_size(dname)) {
369  ldns_buffer_printf(output, ".");
370  }
371  len = data[src_pos];
372  }
373  }
374  return ldns_buffer_status(output);
375 }
376 
379 {
380  uint8_t data = ldns_rdf_data(rdf)[0];
381  ldns_buffer_printf(output, "%lu", (unsigned long) data);
382  return ldns_buffer_status(output);
383 }
384 
387 {
388  uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
389  ldns_buffer_printf(output, "%lu", (unsigned long) data);
390  return ldns_buffer_status(output);
391 }
392 
395 {
396  uint32_t data = ldns_read_uint32(ldns_rdf_data(rdf));
397  ldns_buffer_printf(output, "%lu", (unsigned long) data);
398  return ldns_buffer_status(output);
399 }
400 
403 {
404  /* create a YYYYMMDDHHMMSS string if possible */
405  struct tm tm;
406  char date_buf[16];
407 
408  memset(&tm, 0, sizeof(tm));
410  && strftime(date_buf, 15, "%Y%m%d%H%M%S", &tm)) {
411  ldns_buffer_printf(output, "%s", date_buf);
412  }
413  return ldns_buffer_status(output);
414 }
415 
418 {
419  char str[INET_ADDRSTRLEN];
420 
421  if (inet_ntop(AF_INET, ldns_rdf_data(rdf), str, INET_ADDRSTRLEN)) {
422  ldns_buffer_printf(output, "%s", str);
423  }
424  return ldns_buffer_status(output);
425 }
426 
429 {
430  char str[INET6_ADDRSTRLEN];
431 
432  if (inet_ntop(AF_INET6, ldns_rdf_data(rdf), str, INET6_ADDRSTRLEN)) {
433  ldns_buffer_printf(output, "%s", str);
434  }
435 
436  return ldns_buffer_status(output);
437 }
438 
439 static void
440 ldns_characters2buffer_str(ldns_buffer* output,
441  size_t amount, const uint8_t* characters)
442 {
443  uint8_t ch;
444  while (amount > 0) {
445  ch = *characters++;
446  if (isprint((int)ch) || ch == '\t') {
447  if (ch == '\"' || ch == '\\')
448  ldns_buffer_printf(output, "\\%c", ch);
449  else
450  ldns_buffer_printf(output, "%c", ch);
451  } else {
452  ldns_buffer_printf(output, "\\%03u",
453  (unsigned)(uint8_t) ch);
454  }
455  amount--;
456  }
457 }
458 
461 {
462  if(ldns_rdf_size(rdf) < 1) {
464  }
465  if((int)ldns_rdf_size(rdf) < (int)ldns_rdf_data(rdf)[0] + 1) {
467  }
468  ldns_buffer_printf(output, "\"");
469  ldns_characters2buffer_str(output,
470  ldns_rdf_data(rdf)[0], ldns_rdf_data(rdf) + 1);
471  ldns_buffer_printf(output, "\"");
472  return ldns_buffer_status(output);
473 }
474 
477 {
478  size_t size = ldns_b64_ntop_calculate_size(ldns_rdf_size(rdf));
479  char *b64 = LDNS_XMALLOC(char, size);
480  if(!b64) return LDNS_STATUS_MEM_ERR;
481  if (ldns_b64_ntop(ldns_rdf_data(rdf), ldns_rdf_size(rdf), b64, size)) {
482  ldns_buffer_printf(output, "%s", b64);
483  }
484  LDNS_FREE(b64);
485  return ldns_buffer_status(output);
486 }
487 
490 {
491  size_t size;
492  char *b32;
493  if(ldns_rdf_size(rdf) == 0)
494  return LDNS_STATUS_OK;
495  /* remove -1 for the b32-hash-len octet */
496  size = ldns_b32_ntop_calculate_size(ldns_rdf_size(rdf) - 1);
497  /* add one for the end nul for the string */
498  b32 = LDNS_XMALLOC(char, size + 1);
499  if(!b32) return LDNS_STATUS_MEM_ERR;
500  size = (size_t) ldns_b32_ntop_extended_hex(ldns_rdf_data(rdf) + 1,
501  ldns_rdf_size(rdf) - 1, b32, size+1);
502  if (size > 0) {
503  ldns_buffer_printf(output, "%s", b32);
504  }
505  LDNS_FREE(b32);
506  return ldns_buffer_status(output);
507 }
508 
511 {
512  size_t i;
513  for (i = 0; i < ldns_rdf_size(rdf); i++) {
514  ldns_buffer_printf(output, "%02x", ldns_rdf_data(rdf)[i]);
515  }
516 
517  return ldns_buffer_status(output);
518 }
519 
520 static ldns_status
521 ldns_rdf2buffer_str_type_fmt(ldns_buffer *output,
522  const ldns_output_format* fmt, const ldns_rdf *rdf)
523 {
524  uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
525 
526  if (! ldns_output_format_covers_type(fmt, data) &&
527  ldns_rr_descript(data) &&
528  ldns_rr_descript(data)->_name) {
529 
530  ldns_buffer_printf(output, "%s",ldns_rr_descript(data)->_name);
531  } else {
532  ldns_buffer_printf(output, "TYPE%u", data);
533  }
534  return ldns_buffer_status(output);
535 }
536 
539 {
540  return ldns_rdf2buffer_str_type_fmt(output,
542 }
543 
546 {
547  uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
548  ldns_lookup_table *lt;
549 
550  lt = ldns_lookup_by_id(ldns_rr_classes, (int) data);
551  if (lt) {
552  ldns_buffer_printf(output, "\t%s", lt->name);
553  } else {
554  ldns_buffer_printf(output, "\tCLASS%d", data);
555  }
556  return ldns_buffer_status(output);
557 }
558 
561 {
562  uint16_t data = ldns_read_uint16(ldns_rdf_data(rdf));
563  ldns_lookup_table *lt;
564  lt = ldns_lookup_by_id(ldns_cert_algorithms, (int) data);
565  if (lt) {
566  ldns_buffer_printf(output, "%s", lt->name);
567  } else {
568  ldns_buffer_printf(output, "%d", data);
569  }
570  return ldns_buffer_status(output);
571 }
572 
575 {
576  return ldns_rdf2buffer_str_int8(output, rdf);
577 }
578 
579 static void
580 loc_cm_print(ldns_buffer *output, uint8_t mantissa, uint8_t exponent)
581 {
582  uint8_t i;
583  /* is it 0.<two digits> ? */
584  if(exponent < 2) {
585  if(exponent == 1)
586  mantissa *= 10;
587  ldns_buffer_printf(output, "0.%02ld", (long)mantissa);
588  return;
589  }
590  /* always <digit><string of zeros> */
591  ldns_buffer_printf(output, "%d", (int)mantissa);
592  for(i=0; i<exponent-2; i++)
593  ldns_buffer_printf(output, "0");
594 }
595 
598 {
599  const ldns_rr_descriptor *descriptor;
600 
601  descriptor = ldns_rr_descript(type);
602 
603  switch (type) {
604  case LDNS_RR_TYPE_IXFR:
605  ldns_buffer_printf(output, "IXFR");
606  break;
607  case LDNS_RR_TYPE_AXFR:
608  ldns_buffer_printf(output, "AXFR");
609  break;
610  case LDNS_RR_TYPE_MAILA:
611  ldns_buffer_printf(output, "MAILA");
612  break;
613  case LDNS_RR_TYPE_MAILB:
614  ldns_buffer_printf(output, "MAILB");
615  break;
616  case LDNS_RR_TYPE_ANY:
617  ldns_buffer_printf(output, "ANY");
618  break;
619  default:
620  if (descriptor && descriptor->_name) {
621  ldns_buffer_printf(output, "%s", descriptor->_name);
622  } else {
623  ldns_buffer_printf(output, "TYPE%u", type);
624  }
625  }
626  return ldns_buffer_status(output);
627 }
628 
629 char *
631 {
632  char *str;
633  ldns_buffer *buf;
634 
635  buf = ldns_buffer_new(10);
636  if (!buf) {
637  return NULL;
638  }
639 
640  str = NULL;
641  if (ldns_rr_type2buffer_str(buf, type) == LDNS_STATUS_OK) {
642  str = ldns_buffer_export2str(buf);
643  }
644 
645  ldns_buffer_free(buf);
646  return str;
647 }
648 
649 
652  const ldns_rr_class klass)
653 {
654  ldns_lookup_table *lt;
655 
656  lt = ldns_lookup_by_id(ldns_rr_classes, klass);
657  if (lt) {
658  ldns_buffer_printf(output, "%s", lt->name);
659  } else {
660  ldns_buffer_printf(output, "CLASS%d", klass);
661  }
662  return ldns_buffer_status(output);
663 }
664 
665 char *
667 {
668  ldns_buffer *buf;
669  char *str;
670 
671  buf = ldns_buffer_new(10);
672  if (!buf) {
673  return NULL;
674  }
675 
676  str = NULL;
677  if (ldns_rr_class2buffer_str(buf, klass) == LDNS_STATUS_OK) {
678  str = ldns_buffer_export2str(buf);
679  }
680  ldns_buffer_free(buf);
681  return str;
682 }
683 
686 {
687  /* we could do checking (ie degrees < 90 etc)? */
688  uint8_t version;
689  uint8_t size;
690  uint8_t horizontal_precision;
691  uint8_t vertical_precision;
692  uint32_t longitude;
693  uint32_t latitude;
694  uint32_t altitude;
695  char northerness;
696  char easterness;
697  uint32_t h;
698  uint32_t m;
699  double s;
700 
701  uint32_t equator = (uint32_t) ldns_power(2, 31);
702 
703  if(ldns_rdf_size(rdf) < 1) {
705  }
706  version = ldns_rdf_data(rdf)[0];
707  if (version == 0) {
708  if(ldns_rdf_size(rdf) < 16) {
710  }
711  size = ldns_rdf_data(rdf)[1];
712  horizontal_precision = ldns_rdf_data(rdf)[2];
713  vertical_precision = ldns_rdf_data(rdf)[3];
714 
715  latitude = ldns_read_uint32(&ldns_rdf_data(rdf)[4]);
716  longitude = ldns_read_uint32(&ldns_rdf_data(rdf)[8]);
717  altitude = ldns_read_uint32(&ldns_rdf_data(rdf)[12]);
718 
719  if (latitude > equator) {
720  northerness = 'N';
721  latitude = latitude - equator;
722  } else {
723  northerness = 'S';
724  latitude = equator - latitude;
725  }
726  h = latitude / (1000 * 60 * 60);
727  latitude = latitude % (1000 * 60 * 60);
728  m = latitude / (1000 * 60);
729  latitude = latitude % (1000 * 60);
730  s = (double) latitude / 1000.0;
731  ldns_buffer_printf(output, "%02u %02u %0.3f %c ",
732  h, m, s, northerness);
733 
734  if (longitude > equator) {
735  easterness = 'E';
736  longitude = longitude - equator;
737  } else {
738  easterness = 'W';
739  longitude = equator - longitude;
740  }
741  h = longitude / (1000 * 60 * 60);
742  longitude = longitude % (1000 * 60 * 60);
743  m = longitude / (1000 * 60);
744  longitude = longitude % (1000 * 60);
745  s = (double) longitude / (1000.0);
746  ldns_buffer_printf(output, "%02u %02u %0.3f %c ",
747  h, m, s, easterness);
748 
749 
750  s = ((double) altitude) / 100;
751  s -= 100000;
752 
753  if(altitude%100 != 0)
754  ldns_buffer_printf(output, "%.2f", s);
755  else
756  ldns_buffer_printf(output, "%.0f", s);
757 
758  ldns_buffer_printf(output, "m ");
759 
760  loc_cm_print(output, (size & 0xf0) >> 4, size & 0x0f);
761  ldns_buffer_printf(output, "m ");
762 
763  loc_cm_print(output, (horizontal_precision & 0xf0) >> 4,
764  horizontal_precision & 0x0f);
765  ldns_buffer_printf(output, "m ");
766 
767  loc_cm_print(output, (vertical_precision & 0xf0) >> 4,
768  vertical_precision & 0x0f);
769  ldns_buffer_printf(output, "m");
770 
771  return ldns_buffer_status(output);
772  } else {
773  return ldns_rdf2buffer_str_hex(output, rdf);
774  }
775 }
776 
779 {
780  ldns_buffer_printf(output, "\\# %u ", ldns_rdf_size(rdf));
781  return ldns_rdf2buffer_str_hex(output, rdf);
782 }
783 
786 {
787  ldns_buffer_printf(output, "0x");
788  return ldns_rdf2buffer_str_hex(output, rdf);
789 }
790 
793 {
794  return ldns_rdf2buffer_str_hex(output, rdf);
795 }
796 
799 {
800  /* protocol, followed by bitmap of services */
801  struct protoent *protocol;
802  char *proto_name = NULL;
803  uint8_t protocol_nr;
804  struct servent *service;
805  uint16_t current_service;
806 
807  if(ldns_rdf_size(rdf) < 1) {
809  }
810  protocol_nr = ldns_rdf_data(rdf)[0];
811  protocol = getprotobynumber((int) protocol_nr);
812  if (protocol && (protocol->p_name != NULL)) {
813  proto_name = protocol->p_name;
814  ldns_buffer_printf(output, "%s ", protocol->p_name);
815  } else {
816  ldns_buffer_printf(output, "%u ", protocol_nr);
817  }
818 
819 #ifdef HAVE_ENDPROTOENT
820  endprotoent();
821 #endif
822 
823  for (current_service = 0;
824  current_service < (ldns_rdf_size(rdf)-1)*8; current_service++) {
825  if (ldns_get_bit(&(ldns_rdf_data(rdf)[1]), current_service)) {
826  service = getservbyport((int) htons(current_service),
827  proto_name);
828  if (service && service->s_name) {
829  ldns_buffer_printf(output, "%s ", service->s_name);
830  } else {
831  ldns_buffer_printf(output, "%u ", current_service);
832  }
833 #ifdef HAVE_ENDSERVENT
834  endservent();
835 #endif
836  }
837  }
838  return ldns_buffer_status(output);
839 }
840 
841 static ldns_status
842 ldns_rdf2buffer_str_nsec_fmt(ldns_buffer *output,
843  const ldns_output_format* fmt, const ldns_rdf *rdf)
844 {
845  /* Note: this code is duplicated in higher.c in
846  * ldns_nsec_type_check() function
847  */
848  uint8_t window_block_nr;
849  uint8_t bitmap_length;
850  uint16_t type;
851  uint16_t pos = 0;
852  uint16_t bit_pos;
853  uint8_t *data = ldns_rdf_data(rdf);
854 
855  while((size_t)(pos + 2) < ldns_rdf_size(rdf)) {
856  window_block_nr = data[pos];
857  bitmap_length = data[pos + 1];
858  pos += 2;
859  if (ldns_rdf_size(rdf) < pos + bitmap_length) {
861  }
862  for (bit_pos = 0; bit_pos < (bitmap_length) * 8; bit_pos++) {
863  if (! ldns_get_bit(&data[pos], bit_pos)) {
864  continue;
865  }
866  type = 256 * (uint16_t) window_block_nr + bit_pos;
867 
868  if (! ldns_output_format_covers_type(fmt, type) &&
869  ldns_rr_descript(type) &&
870  ldns_rr_descript(type)->_name){
871 
872  ldns_buffer_printf(output, "%s ",
873  ldns_rr_descript(type)->_name);
874  } else {
875  ldns_buffer_printf(output, "TYPE%u ", type);
876  }
877  }
878  pos += (uint16_t) bitmap_length;
879  }
880  return ldns_buffer_status(output);
881 }
882 
885 {
886  return ldns_rdf2buffer_str_nsec_fmt(output,
888 }
889 
892 {
893  uint8_t salt_length;
894  uint8_t salt_pos;
895 
896  uint8_t *data = ldns_rdf_data(rdf);
897 
898  if(ldns_rdf_size(rdf) < 1) {
900  }
901  salt_length = data[0];
902  /* from now there are variable length entries so remember pos */
903  if (salt_length == 0 || ((size_t)salt_length)+1 > ldns_rdf_size(rdf)) {
904  ldns_buffer_printf(output, "- ");
905  } else {
906  for (salt_pos = 0; salt_pos < salt_length; salt_pos++) {
907  ldns_buffer_printf(output, "%02x", data[1 + salt_pos]);
908  }
909  ldns_buffer_printf(output, " ");
910  }
911 
912  return ldns_buffer_status(output);
913 }
914 
917 {
918  /* period is the number of seconds */
919  if (ldns_rdf_size(rdf) != 4) {
921  }
922  ldns_buffer_printf(output, "%u", ldns_read_uint32(ldns_rdf_data(rdf)));
923  return ldns_buffer_status(output);
924 }
925 
928 {
929  /* tsigtime is 48 bits network order unsigned integer */
930  uint64_t tsigtime = 0;
931  uint8_t *data = ldns_rdf_data(rdf);
932  uint64_t d0, d1, d2, d3, d4, d5;
933 
934  if (ldns_rdf_size(rdf) < 6) {
936  }
937  d0 = data[0]; /* cast to uint64 for shift operations */
938  d1 = data[1];
939  d2 = data[2];
940  d3 = data[3];
941  d4 = data[4];
942  d5 = data[5];
943  tsigtime = (d0<<40) | (d1<<32) | (d2<<24) | (d3<<16) | (d4<<8) | d5;
944 
945  ldns_buffer_printf(output, "%llu ", (long long)tsigtime);
946 
947  return ldns_buffer_status(output);
948 }
949 
952 {
953  uint8_t *data = ldns_rdf_data(rdf);
954  uint16_t address_family;
955  uint8_t prefix;
956  bool negation;
957  uint8_t adf_length;
958  size_t i;
959  size_t pos = 0;
960 
961  while (pos < (unsigned int) ldns_rdf_size(rdf)) {
962  if(pos + 3 >= (unsigned)ldns_rdf_size(rdf))
964  address_family = ldns_read_uint16(&data[pos]);
965  prefix = data[pos + 2];
966  negation = data[pos + 3] & LDNS_APL_NEGATION;
967  adf_length = data[pos + 3] & LDNS_APL_MASK;
968  if (address_family == LDNS_APL_IP4) {
969  /* check if prefix < 32? */
970  if (negation) {
971  ldns_buffer_printf(output, "!");
972  }
973  ldns_buffer_printf(output, "%u:", address_family);
974  /* address is variable length 0 - 4 */
975  for (i = 0; i < 4; i++) {
976  if (i > 0) {
977  ldns_buffer_printf(output, ".");
978  }
979  if (i < (unsigned short) adf_length) {
980  if(pos+i+4 >= ldns_rdf_size(rdf))
982  ldns_buffer_printf(output, "%d",
983  data[pos + i + 4]);
984  } else {
985  ldns_buffer_printf(output, "0");
986  }
987  }
988  ldns_buffer_printf(output, "/%u ", prefix);
989  } else if (address_family == LDNS_APL_IP6) {
990  /* check if prefix < 128? */
991  if (negation) {
992  ldns_buffer_printf(output, "!");
993  }
994  ldns_buffer_printf(output, "%u:", address_family);
995  /* address is variable length 0 - 16 */
996  for (i = 0; i < 16; i++) {
997  if (i % 2 == 0 && i > 0) {
998  ldns_buffer_printf(output, ":");
999  }
1000  if (i < (unsigned short) adf_length) {
1001  if(pos+i+4 >= ldns_rdf_size(rdf))
1003  ldns_buffer_printf(output, "%02x",
1004  data[pos + i + 4]);
1005  } else {
1006  ldns_buffer_printf(output, "00");
1007  }
1008  }
1009  ldns_buffer_printf(output, "/%u ", prefix);
1010 
1011  } else {
1012  /* unknown address family */
1013  ldns_buffer_printf(output,
1014  "Unknown address family: %u data: ",
1015  address_family);
1016  for (i = 1; i < (unsigned short) (4 + adf_length); i++) {
1017  if(pos+i >= ldns_rdf_size(rdf))
1019  ldns_buffer_printf(output, "%02x", data[i]);
1020  }
1021  }
1022  pos += 4 + adf_length;
1023  }
1024  return ldns_buffer_status(output);
1025 }
1026 
1029 {
1030  size_t size;
1031  char *b64;
1032  if (ldns_rdf_size(rdf) < 2) {
1034  }
1035  /* Subtract the size (2) of the number that specifies the length */
1036  size = ldns_b64_ntop_calculate_size(ldns_rdf_size(rdf) - 2);
1037  ldns_buffer_printf(output, "%u ", ldns_rdf_size(rdf) - 2);
1038  if (ldns_rdf_size(rdf) > 2) {
1039  b64 = LDNS_XMALLOC(char, size);
1040  if(!b64)
1041  return LDNS_STATUS_MEM_ERR;
1042 
1043  if (ldns_rdf_size(rdf) > 2 &&
1044  ldns_b64_ntop(ldns_rdf_data(rdf) + 2,
1045  ldns_rdf_size(rdf) - 2,
1046  b64, size)) {
1047  ldns_buffer_printf(output, "%s", b64);
1048  }
1049  LDNS_FREE(b64);
1050  }
1051  return ldns_buffer_status(output);
1052 }
1053 
1056 {
1057  /* wire format from
1058  http://www.ietf.org/internet-drafts/draft-ietf-ipseckey-rr-12.txt
1059  */
1060  uint8_t *data = ldns_rdf_data(rdf);
1061  uint8_t precedence;
1062  uint8_t gateway_type;
1063  uint8_t algorithm;
1064 
1065  ldns_rdf *gateway = NULL;
1066  uint8_t *gateway_data;
1067 
1068  size_t public_key_size;
1069  uint8_t *public_key_data;
1070  ldns_rdf *public_key;
1071 
1072  size_t offset = 0;
1073  ldns_status status;
1074 
1075  if (ldns_rdf_size(rdf) < 3) {
1077  }
1078  precedence = data[0];
1079  gateway_type = data[1];
1080  algorithm = data[2];
1081  offset = 3;
1082 
1083  switch (gateway_type) {
1084  case 0:
1085  /* no gateway */
1086  break;
1087  case 1:
1088  gateway_data = LDNS_XMALLOC(uint8_t, LDNS_IP4ADDRLEN);
1089  if(!gateway_data)
1090  return LDNS_STATUS_MEM_ERR;
1091  if (ldns_rdf_size(rdf) < offset + LDNS_IP4ADDRLEN) {
1092  return LDNS_STATUS_ERR;
1093  }
1094  memcpy(gateway_data, &data[offset], LDNS_IP4ADDRLEN);
1095  gateway = ldns_rdf_new(LDNS_RDF_TYPE_A,
1096  LDNS_IP4ADDRLEN , gateway_data);
1097  offset += LDNS_IP4ADDRLEN;
1098  if(!gateway) {
1099  LDNS_FREE(gateway_data);
1100  return LDNS_STATUS_MEM_ERR;
1101  }
1102  break;
1103  case 2:
1104  gateway_data = LDNS_XMALLOC(uint8_t, LDNS_IP6ADDRLEN);
1105  if(!gateway_data)
1106  return LDNS_STATUS_MEM_ERR;
1107  if (ldns_rdf_size(rdf) < offset + LDNS_IP6ADDRLEN) {
1108  return LDNS_STATUS_ERR;
1109  }
1110  memcpy(gateway_data, &data[offset], LDNS_IP6ADDRLEN);
1111  offset += LDNS_IP6ADDRLEN;
1112  gateway =
1114  LDNS_IP6ADDRLEN, gateway_data);
1115  if(!gateway) {
1116  LDNS_FREE(gateway_data);
1117  return LDNS_STATUS_MEM_ERR;
1118  }
1119  break;
1120  case 3:
1121  status = ldns_wire2dname(&gateway, data,
1122  ldns_rdf_size(rdf), &offset);
1123  if(status != LDNS_STATUS_OK)
1124  return status;
1125  break;
1126  default:
1127  /* error? */
1128  break;
1129  }
1130 
1131  if (ldns_rdf_size(rdf) <= offset) {
1132  return LDNS_STATUS_ERR;
1133  }
1134  public_key_size = ldns_rdf_size(rdf) - offset;
1135  public_key_data = LDNS_XMALLOC(uint8_t, public_key_size);
1136  if(!public_key_data) {
1137  ldns_rdf_deep_free(gateway);
1138  return LDNS_STATUS_MEM_ERR;
1139  }
1140  memcpy(public_key_data, &data[offset], public_key_size);
1141  public_key = ldns_rdf_new(LDNS_RDF_TYPE_B64,
1142  public_key_size, public_key_data);
1143  if(!public_key) {
1144  LDNS_FREE(public_key_data);
1145  ldns_rdf_deep_free(gateway);
1146  return LDNS_STATUS_MEM_ERR;
1147  }
1148 
1149  ldns_buffer_printf(output, "%u %u %u ", precedence, gateway_type, algorithm);
1150  if (gateway)
1151  (void) ldns_rdf2buffer_str(output, gateway);
1152  else
1153  ldns_buffer_printf(output, ".");
1154  ldns_buffer_printf(output, " ");
1155  (void) ldns_rdf2buffer_str(output, public_key);
1156 
1157  ldns_rdf_deep_free(gateway);
1158  ldns_rdf_deep_free(public_key);
1159 
1160  return ldns_buffer_status(output);
1161 }
1162 
1165 {
1166  if (ldns_rdf_size(rdf) != 8) {
1168  }
1169  ldns_buffer_printf(output,"%.4x:%.4x:%.4x:%.4x",
1170  ldns_read_uint16(ldns_rdf_data(rdf)),
1171  ldns_read_uint16(ldns_rdf_data(rdf)+2),
1172  ldns_read_uint16(ldns_rdf_data(rdf)+4),
1173  ldns_read_uint16(ldns_rdf_data(rdf)+6));
1174  return ldns_buffer_status(output);
1175 }
1176 
1179 {
1180  if (ldns_rdf_size(rdf) != 6) {
1182  }
1183  ldns_buffer_printf(output,"%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
1184  ldns_rdf_data(rdf)[0], ldns_rdf_data(rdf)[1],
1185  ldns_rdf_data(rdf)[2], ldns_rdf_data(rdf)[3],
1186  ldns_rdf_data(rdf)[4], ldns_rdf_data(rdf)[5]);
1187  return ldns_buffer_status(output);
1188 }
1189 
1192 {
1193  if (ldns_rdf_size(rdf) != 8) {
1195  }
1196  ldns_buffer_printf(output,"%.2x-%.2x-%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
1197  ldns_rdf_data(rdf)[0], ldns_rdf_data(rdf)[1],
1198  ldns_rdf_data(rdf)[2], ldns_rdf_data(rdf)[3],
1199  ldns_rdf_data(rdf)[4], ldns_rdf_data(rdf)[5],
1200  ldns_rdf_data(rdf)[6], ldns_rdf_data(rdf)[7]);
1201  return ldns_buffer_status(output);
1202 }
1203 
1206 {
1207  size_t nchars;
1208  const uint8_t* chars;
1209  char ch;
1210  if (ldns_rdf_size(rdf) < 2) {