fehQlibs 26
Qlibs
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 "dnsresolv.h"
5
13int dns_txt_packet(stralloc *out,const char *buf,unsigned int len)
14{
15 unsigned int pos;
16 char header[12];
17 uint16 numanswers;
18 uint16 datalen;
19 char ch;
20 unsigned int txtlen;
21 int i;
22 int ranswers = 0;
23
24 if (!stralloc_copys(out,"")) return DNS_MEM;
25
26 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
27 uint16_unpack_big(header + 6,&numanswers);
28 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
29 pos += 4;
30
31 while (numanswers--) {
32 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
33 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
34 uint16_unpack_big(header + 8,&datalen);
35 if (byte_equal(header,2,DNS_T_TXT))
36 if (byte_equal(header + 2,2,DNS_C_IN)) {
37 if (pos + datalen > len) return DNS_ERR;
38 txtlen = (unsigned char) buf[pos];
39 for (i = 1; i < datalen; ++i) {
40 ch = buf[pos + i];
41 if (i == txtlen + 1) // next label
42 txtlen += (unsigned char) ch + 1;
43 else {
44 if (ch < 32) ch = '?';
45 if (ch > 126) ch = '?';
46 if (!stralloc_append(out,&ch)) return DNS_MEM;
47 }
48 }
49 if (txtlen) if (!stralloc_append(out,"\n")) return DNS_MEM;
50 }
51 pos += datalen;
52 ++ranswers;
53 }
54
55 return ranswers;
56}
57
58static char *q = 0;
59
60int dns_txt(stralloc *out,const stralloc *fqdn)
61{
62 int rc;
63
64 if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
65 if (dns_resolve(q,DNS_T_TXT) < 0) return DNS_ERR;
69
70 return rc;
71}
int dns_txt(stralloc *out, const stralloc *fqdn)
Definition: dns_txt.c:60
int dns_txt_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_txt.c:13
additional types and pack routines
uint16_t uint16
Definition: uint_t.h:29
void uint16_unpack_big(char[16], uint16 *)
#define byte_equal(s, n, t)
Definition: byte.h:18
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:105
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, char *, unsigned int)
Definition: dns_packet.c:12
#define DNS_ERR
Definition: dnsresolv.h:44
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_dfd.c:13
#define DNS_C_IN
Definition: dnsresolv.h:60
int dns_resolve(const char *, const char *)
#define DNS_T_TXT
Definition: dnsresolv.h:70
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:22
void dns_domain_free(char **)
Definition: dns_domain.c:24
#define DNS_MEM
Definition: dnsresolv.h:43
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:14
int stralloc_append(stralloc *sa, const char *in)
Definition: stralloc.c:112
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
unsigned int packetlen
Definition: dnsresolv.h:103
char * packet
Definition: dnsresolv.h:102
size_t len
Definition: stralloc.h:19
char * s
Definition: stralloc.h:18