summaryrefslogtreecommitdiff
path: root/dnsstub/dns_dtda.c
blob: 38358a2731db081493b82247f0b4495b222996f1 (plain)
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
#include "stralloc.h"
#include "dnsresolv.h"

/**
	@file dns_dtda.c
	@author djb
	@source ucspi-tcp
	@brief domain to dot append
*/

int dns_domain_todot_cat(stralloc *out,const char *d)
{
  char ch;
  char ch2;
  unsigned char ch3;
  char buf[4];

  if (!*d)
    return stralloc_append(out,".");

  for (;;) {
    ch = *d++;
    while (ch--) {
      ch2 = *d++;
      if ((ch2 >= 'A') && (ch2 <= 'Z')) ch2 += 32;   // FQDN -> lowercase
      if (((ch2 >= 'a') && (ch2 <= 'z')) || 
          ((ch2 >= '0') && (ch2 <= '9')) || 
           (ch2 == '-') || (ch2 == '_')) {
        if (!stralloc_append(out,&ch2)) return DNS_MEM;
      }
      else {                                         // decimal -> octal
        ch3 = ch2;
        buf[3] = '0' + (ch3 & 7); ch3 >>= 3;
        buf[2] = '0' + (ch3 & 7); ch3 >>= 3;
        buf[1] = '0' + (ch3 & 7);
        buf[0] = '\\';
        if (!stralloc_catb(out,buf,4)) return DNS_MEM;
      }
    }
    if (!*d) return 1;
    if (!stralloc_append(out,".")) return DNS_MEM;
  }
}