djbdnscurve6  38
djbdnscurve6
printrecord.c
Go to the documentation of this file.
1 #include "uint_t.h"
2 #include "error.h"
3 #include "byte.h"
4 #include "dns.h"
5 #include "printrecord.h"
6 #include "ip.h"
7 
8 static char *d;
9 
10 unsigned int printrecord_cat(stralloc *out,const char *buf,unsigned int len,unsigned int pos,const char *q,const char qtype[2])
11 {
12  const char *x;
13  char misc[20];
14  uint16 datalen;
15  uint16 u16;
16  uint32 u32;
17  unsigned int newpos;
18  int i;
19  unsigned char ch;
20  char ip4str[IP4_FMT];
21  char ip6str[IP6_FMT];
22  int stringlen;
23 
24  pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0;
25  pos = dns_packet_copy(buf,len,pos,misc,10); if (!pos) return 0;
26  uint16_unpack_big(misc + 8,&datalen);
27  newpos = pos + datalen;
28 
29  if (q) {
30  if (!dns_domain_equal(d,q))
31  return newpos;
32  if (byte_diff(qtype,2,misc) && byte_diff(qtype,2,DNS_T_ANY))
33  return newpos;
34  }
35 
36  if (dns_domain_todot_cat(out,d) <= 0) return 0;
37  if (!stralloc_cats(out," ")) return 0;
38  uint32_unpack_big(misc + 4,&u32);
39  if (!stralloc_catulong0(out,u32,0)) return 0;
40 
41  if (byte_diff(misc + 2,2,DNS_C_IN)) {
42  if (!stralloc_cats(out," weird class\n")) return 0;
43  return newpos;
44  }
45 
46  x = 0;
47  if (byte_equal(misc,2,DNS_T_NS)) x = " NS ";
48  if (byte_equal(misc,2,DNS_T_PTR)) x = " PTR ";
49  if (byte_equal(misc,2,DNS_T_CNAME)) x = " CNAME ";
50  if (x) {
51  pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0;
52  if (!stralloc_cats(out,x)) return 0;
53  if (dns_domain_todot_cat(out,d) <= 0) return 0;
54  }
55  else if (byte_equal(misc,2,DNS_T_MX)) {
56  if (!stralloc_cats(out," MX ")) return 0;
57  pos = dns_packet_copy(buf,len,pos,misc,2); if (!pos) return 0;
58  pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0;
59  uint16_unpack_big(misc,&u16);
60  if (!stralloc_catulong0(out,u16,0)) return 0;
61  if (!stralloc_cats(out," ")) return 0;
62  if (dns_domain_todot_cat(out,d) <= 0) return 0;
63  }
64  else if (byte_equal(misc,2,DNS_T_SOA)) {
65  if (!stralloc_cats(out," SOA ")) return 0;
66  pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0;
67  if (dns_domain_todot_cat(out,d) <= 0) return 0;
68  if (!stralloc_cats(out," ")) return 0;
69  pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0;
70  if (dns_domain_todot_cat(out,d) <= 0) return 0;
71  pos = dns_packet_copy(buf,len,pos,misc,20); if (!pos) return 0;
72  for (i = 0; i < 5; ++i) {
73  if (!stralloc_cats(out," ")) return 0;
74  uint32_unpack_big(misc + 4 * i,&u32);
75  if (!stralloc_catulong0(out,u32,0)) return 0;
76  }
77  }
78  else if (byte_equal(misc,2,DNS_T_A)) {
79  if (datalen != 4) { errno = EPROTO; return 0; }
80  if (!stralloc_cats(out," A ")) return 0;
81  pos = dns_packet_copy(buf,len,pos,misc,4); if (!pos) return 0;
82  stringlen = ip4_fmt(ip4str,misc);
83  if (!stralloc_catb(out,ip4str,stringlen)) return 0;
84  }
85  else if (byte_equal(misc,2,DNS_T_AAAA)) {
86  if (datalen != 16) { errno = EPROTO; return 0; }
87  if (!stralloc_cats(out," AAAA ")) return 0;
88  pos = dns_packet_copy(buf,len,pos,misc,16); if (!pos) return 0;
89  stringlen = ip6_fmt(ip6str,misc);
90  if (!stralloc_catb(out,ip6str,stringlen)) return 0;
91  }
92  else {
93  if (!stralloc_cats(out," ")) return 0;
94  uint16_unpack_big(misc,&u16);
95  if (!stralloc_catulong0(out,u16,0)) return 0;
96  if (!stralloc_cats(out," ")) return 0;
97  while (datalen--) {
98  pos = dns_packet_copy(buf,len,pos,misc,1); if (!pos) return 0;
99  if ((misc[0] >= 32) && (misc[0] <= 126) && (misc[0] != '\\')) { // allow spaces
100  if (!stralloc_catb(out,misc,1)) return 0;
101  }
102  else {
103  ch = misc[0];
104  misc[3] = '0' + (7 & ch); ch >>= 3;
105  misc[2] = '0' + (7 & ch); ch >>= 3;
106  misc[1] = '0' + (7 & ch);
107  misc[0] = '\\';
108  if (!stralloc_catb(out,misc,4)) return 0;
109  if (!stralloc_catb(out,"?",1)) return 0;
110  }
111  }
112  }
113 
114  if (!stralloc_cats(out,"\n")) return 0;
115  if (pos != newpos) { errno = EPROTO; return 0; }
116  return newpos;
117 }
118 
119 unsigned int printrecord(stralloc *out,const char *buf,unsigned int len,unsigned int pos,const char *q,const char qtype[2])
120 {
121  if (!stralloc_copys(out,"")) return 0;
122  return printrecord_cat(out,buf,len,pos,q,qtype);
123 }
uint16 len
Definition: axfrdns.c:302
char buf[MSGSIZE]
Definition: axfrdns.c:301
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int)
Definition: dns_packet.c:8
#define DNS_T_A
Definition: dns.h:37
int dns_domain_equal(const char *, const char *)
Definition: dns_domain.c:39
int dns_domain_todot_cat(stralloc *, const char *)
Definition: dns_dtda.c:11
#define DNS_C_IN
Definition: dns.h:34
#define DNS_T_ANY
Definition: dns.h:66
#define DNS_T_PTR
Definition: dns.h:41
#define DNS_T_SOA
Definition: dns.h:40
#define DNS_T_NS
Definition: dns.h:38
#define DNS_T_CNAME
Definition: dns.h:39
#define DNS_T_AAAA
Definition: dns.h:48
unsigned int dns_packet_getname(const char *, unsigned int, unsigned int, char **)
Definition: dns_packet.c:35
#define DNS_T_MX
Definition: dns.h:43
struct line * x
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
unsigned int printrecord_cat(stralloc *out, const char *buf, unsigned int len, unsigned int pos, const char *q, const char qtype[2])
Definition: printrecord.c:10
unsigned int printrecord(stralloc *out, const char *buf, unsigned int len, unsigned int pos, const char *q, const char qtype[2])
Definition: printrecord.c:119
char ip6str[IP6_FMT]
Definition: tinydns-edit.c:67
char ip4str[IP4_FMT]
Definition: tinydns-edit.c:66