fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_name.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "uint_t.h"
3#include "byte.h"
4#include "ip.h"
5#include "dnsresolv.h"
6
14static char *q = 0;
15
16int dns_name_packet(stralloc *out,const char *buf,unsigned int len)
17{
18 unsigned int pos;
19 char header[12];
20 uint16 numanswers;
21 uint16 datalen;
22
23 if (!stralloc_copys(out,"")) return DNS_MEM;
24
25 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
26 uint16_unpack_big(header + 6,&numanswers);
27 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
28 pos += 4;
29
30 while (numanswers--) {
31 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
32 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
33 uint16_unpack_big(header + 8,&datalen);
34 if (byte_equal(header,2,DNS_T_PTR))
35 if (byte_equal(header + 2,2,DNS_C_IN)) {
36 if (!dns_packet_getname(buf,len,pos,&q)) return DNS_ERR;
37 if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR;
38 return 1;
39 }
40 pos += datalen;
41 }
42
43 return 0;
44}
45
46int dns_name4(stralloc *out,const char ip[4])
47{
48 int rc;
49 char name[DNS_NAME4_DOMAIN];
50
51 dns_name4_domain(name,ip);
52 if (dns_resolve(name,DNS_T_PTR) < 0) return DNS_ERR;
56
57 return rc;
58}
59
60int dns_name6(stralloc *out,const char ip[16])
61{
62 int rc;
63 char name[DNS_NAME6_DOMAIN];
64
65 dns_name6_domain(name,ip);
66 if (dns_resolve(name,DNS_T_PTR) < 0) return DNS_ERR;
70
71 return rc;
72}
73
74int dns_name(stralloc *out,const char ip[16])
75{
76 if (ip6_isv4mapped(ip))
77 return dns_name4(out,ip+12);
78 else
79 return dns_name6(out,ip);
80}
int dns_name6(stralloc *out, const char ip[16])
Definition: dns_name.c:60
int dns_name_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_name.c:16
int dns_name4(stralloc *out, const char ip[4])
Definition: dns_name.c:46
int dns_name(stralloc *out, const char ip[16])
Definition: dns_name.c:74
additional types and pack routines
uint16_t uint16
Definition: uint_t.h:29
void uint16_unpack_big(char[16], uint16 *)
#define ip6_isv4mapped(ip)
Definition: ip.h:105
#define byte_equal(s, n, t)
Definition: byte.h:18
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:105
#define DNS_NAME4_DOMAIN
Definition: dnsresolv.h:165
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_todot_cat(stralloc *, const char *)
Definition: dns_dtda.c:11
#define DNS_C_IN
Definition: dnsresolv.h:60
int dns_resolve(const char *, const char *)
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:22
int dns_name4_domain(char *, const char *)
void dns_domain_free(char **)
Definition: dns_domain.c:24
#define DNS_T_PTR
Definition: dnsresolv.h:67
int dns_name6_domain(char *, const char *)
#define DNS_NAME6_DOMAIN
Definition: dnsresolv.h:179
unsigned int dns_packet_getname(const char *, unsigned int, unsigned int, char **)
Definition: dns_packet.c:39
#define DNS_MEM
Definition: dnsresolv.h:43
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:14
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
unsigned int packetlen
Definition: dnsresolv.h:103
char * packet
Definition: dnsresolv.h:102