File: dns_dntop.c

package info (click to toggle)
rbldnsd 0.998b~pre1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 744 kB
  • ctags: 1,132
  • sloc: ansic: 8,212; python: 549; sh: 502; makefile: 249; awk: 33
file content (57 lines) | stat: -rw-r--r-- 1,143 bytes parent folder | download | duplicates (3)
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
/* dns_dntop() produces printable ascii representation (external form)
 * of a domain name
 */

#include "dns.h"

unsigned dns_dntop(const unsigned char *dn, char *dst, unsigned dstsiz) {
  char *d = dst;
  char *m = dst + dstsiz - 1;
  unsigned n;
  unsigned char c;

  if (!(n = *dn++)) {
    if (dstsiz < 2)
      return 0;
    *d++ = '.';
    *d++ = '\0';
    return 1;
  }
  do {
    if (d >= m)
      return 0;
    if (dst != d) *d++ = '.';
    do {
      switch((c = *dn++)) {
      case '"':
      case '.':
      case ';':
      case '\\':
      /* Special modifiers in zone files. */
      case '@':
      case '$':
        if (d + 1 >= m)
          return 0;
        *d++ = '\\';
        *d++ = c;
        break;
      default:
        if (c <= 0x20 || c >= 0x7f) {
          if (d + 3 >= m)
            return 0;
          *d++ = '\\';
          *d++ = '0' + (c / 100);
          *d++ = '0' + ((c % 100) / 10);
          *d++ = '0' + (c % 10);
        }
        else {
          if (d >= m)
            return 0;
          *d++ = c;
        }
      }
    } while(--n);
  } while((n = *dn++) != 0);
  *d = '\0';
  return d - dst;
}