djbdnscurve6 45
djbdnscurve6
Loading...
Searching...
No Matches
dns_txt.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "uint_t.h"
3#include "byte.h"
4#include "dns.h"
5
6int dns_txt_packet(stralloc *out,const char *buf,unsigned int len)
7{
8 unsigned int pos;
9 char header[12];
10 uint16 numanswers;
11 uint16 datalen;
12 char ch;
13 unsigned int txtlen;
14 int i;
15 int ranswers = 0;
16
17 if (!stralloc_copys(out,"")) return DNS_MEM;
18
19 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
20 uint16_unpack_big(header + 6,&numanswers);
21 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
22 pos += 4;
23
24 while (numanswers--) {
25 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
26 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
27 uint16_unpack_big(header + 8,&datalen);
28 if (byte_equal(header,2,DNS_T_TXT))
29 if (byte_equal(header + 2,2,DNS_C_IN)) {
30 if (pos + datalen > len) return DNS_ERR;
31 txtlen = (unsigned char) buf[pos];
32 for (i = 1; i < datalen; ++i) {
33 ch = buf[pos + i];
34 if (i == txtlen + 1) // next label
35 txtlen += (unsigned char) ch + 1;
36 else {
37 if (ch < 32) ch = '?';
38 if (ch > 126) ch = '?';
39 if (!stralloc_append(out,&ch)) return DNS_MEM;
40 }
41 }
42 }
43 pos += datalen;
44 ++ranswers;
45 if (numanswers) if (!stralloc_append(out,"\n")) return DNS_MEM;
46 }
47
48 return ranswers;
49}
50
51static char *q = 0;
52
53int dns_txt(stralloc *out,const stralloc *fqdn)
54{
55 int rc;
56
57 if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
58 if (dns_resolve(q,DNS_T_TXT) < 0) return DNS_ERR;
62
63 return rc;
64}
uint16 len
Definition: axfrdns.c:319
char buf[MSGSIZE]
Definition: axfrdns.c:318
#define DNS_ERR
Definition: dns.h:32
#define DNS_C_IN
Definition: dns.h:53
#define DNS_T_TXT
Definition: dns.h:63
#define DNS_MEM
Definition: dns.h:31
int dns_domain_fromdot(char **out, const char *buf, unsigned int n)
Definition: dns_dfd.c:6
void dns_domain_free(char **out)
Definition: dns_domain.c:17
unsigned int dns_packet_copy(const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
Definition: dns_packet.c:8
unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos)
Definition: dns_packet.c:18
int dns_resolve(const char *q, const char qtype[2])
Definition: dns_resolve.c:9
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:7
void dns_transmit_free(struct dns_transmit *d)
Definition: dns_transmit.c:95
int dns_txt(stralloc *out, const stralloc *fqdn)
Definition: dns_txt.c:53
int dns_txt_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_txt.c:6
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
unsigned int packetlen
Definition: dns.h:96
char * packet
Definition: dns.h:95