djbdnscurve6 45
djbdnscurve6
Loading...
Searching...
No Matches
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
8static char *d;
9
10unsigned 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
119unsigned 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:319
char buf[MSGSIZE]
Definition: axfrdns.c:318
#define DNS_T_A
Definition: dns.h:56
#define DNS_C_IN
Definition: dns.h:53
#define DNS_T_ANY
Definition: dns.h:85
#define DNS_T_PTR
Definition: dns.h:60
#define DNS_T_SOA
Definition: dns.h:59
#define DNS_T_NS
Definition: dns.h:57
#define DNS_T_CNAME
Definition: dns.h:58
#define DNS_T_AAAA
Definition: dns.h:67
#define DNS_T_MX
Definition: dns.h:62
int dns_domain_equal(const char *dn1, const char *dn2)
Definition: dns_domain.c:39
int dns_domain_todot_cat(stralloc *out, const char *d)
Definition: dns_dtda.c:11
unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d)
Definition: dns_packet.c:35
unsigned int dns_packet_copy(const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
Definition: dns_packet.c:8
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
uint32 u32
Definition: siphash.c:25
char ip6str[IP6_FMT]
Definition: tinydns-edit.c:69
char ip4str[IP4_FMT]
Definition: tinydns-edit.c:68