djbdnscurve6  38
djbdnscurve6
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 
6 int 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 = 0;
32  for (i = 0; i < datalen; ++i) {
33  ch = buf[pos + i];
34  if (!txtlen)
35  txtlen = (unsigned char) ch;
36  else {
37  --txtlen;
38  if (ch < 32) ch = '?';
39  if (ch > 126) ch = '?';
40  if (!stralloc_append(out,&ch)) return DNS_MEM;
41  }
42  }
43  }
44  pos += datalen;
45  ++ranswers;
46  if (numanswers) if (!stralloc_append(out,"\n")) return DNS_MEM;
47  }
48 
49  return ranswers;
50 }
51 
52 static char *q = 0;
53 
54 int dns_txt(stralloc *out,const stralloc *fqdn)
55 {
56  int rc;
57 
58  if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
59  if (dns_resolve(q,DNS_T_TXT) < 0) return DNS_ERR;
62  dns_domain_free(&q);
63 
64  return rc;
65 }
uint16 len
Definition: axfrdns.c:302
char buf[MSGSIZE]
Definition: axfrdns.c:301
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:96
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int)
Definition: dns_packet.c:8
#define DNS_ERR
Definition: dns.h:22
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_dfd.c:6
#define DNS_C_IN
Definition: dns.h:34
int dns_resolve(const char *, const char *)
#define DNS_T_TXT
Definition: dns.h:44
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:18
void dns_domain_free(char **)
Definition: dns_domain.c:17
#define DNS_MEM
Definition: dns.h:21
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:7
int dns_txt(stralloc *out, const stralloc *fqdn)
Definition: dns_txt.c:54
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:77
char * packet
Definition: dns.h:76