bglibs: dns.h Source File

bglibs

dns.h
1 #ifndef DNS_H
2 #define DNS_H
3
4 #include "sysdeps.h"
5 #include "systime.h"
6 #include "ipv4.h"
7 #include "ipv6.h"
8 #include "str.h"
9 #include "uint16.h"
10
12 #define TV_LESS(A,B) (((A)->tv_sec < (B)->tv_sec) || ((A)->tv_sec == (B)->tv_sec && (A)->tv_usec < (B)->tv_usec))
13
36 #define DNS_C_IN 1
37
38 #define DNS_C_ANY 255
39
41 #define DNS_T_A 1
42
43 #define DNS_T_NS 2
44
45 #define DNS_T_CNAME 5
46
47 #define DNS_T_SOA 6
48
49 #define DNS_T_PTR 12
50
51 #define DNS_T_HINFO 13
52
53 #define DNS_T_MX 15
54
55 #define DNS_T_TXT 16
56
57 #define DNS_T_RP 17
58
59 #define DNS_T_SIG 24
60
61 #define DNS_T_KEY 25
62
63 #define DNS_T_AAAA 28
64
65 #define DNS_T_AXFR 252
66
67 #define DNS_T_ANY 255
68
70 #define DNS_RANDOM_SEED (32*4)
71
72 #define DNS_MAX_IPS 16
73
74 #define DNS_NAME4_DOMAIN (4*4+14)
75
76 #define DNS_NAME6_DOMAIN (32*2+10)
77
79 struct dns_transmit {
81 char *query;
83 unsigned int querylen;
85 char *packet;
87 unsigned int packetlen;
91 int s1;
101 int tcpstate;
103 unsigned int udploop;
105 unsigned int curserver;
107 struct timeval deadline;
109 unsigned int pos;
111 const ipv4addr *servers;
113 ipv4addr localip;
115 uint16 qtype;
116 };
117
119 struct dns_mx
120 {
122 uint16 distance;
124 char* name;
125 };
126
127 union dns_result_rrs
128 {
129 void* __ptr;
130 ipv4addr* ip4;
131 ipv6addr* ip6;
132 struct dns_mx* mx;
133 char** name;
134 };
135
137 struct dns_result
138 {
140 int count;
142 int type;
144 union dns_result_rrs rr;
146 void* __buffer;
147 };
148
149 extern int dns_result_alloc(struct dns_result* d, int type, int count, int size);
150 extern void dns_result_free(struct dns_result* d);
151
152 extern void dns_random_init(const char [DNS_RANDOM_SEED]);
153 extern unsigned int dns_random(unsigned int);
154
155 extern void dns_rotate(unsigned char*, unsigned int n, unsigned int shift);
156 extern void dns_rotateipv4(ipv4addr*, unsigned int);
157 extern void dns_rotateipv6(ipv6addr*, unsigned int);
158
159 extern void dns_sort_mx(struct dns_mx* mxs, int count);
160
161 extern void dns_domain_free(char **);
162 extern int dns_domain_copy(char **,const char *);
163 extern unsigned int dns_domain_length(const char *);
164 extern int dns_domain_equal(const char *,const char *);
165 extern int dns_domain_suffix(const char *,const char *);
166 extern unsigned int dns_domain_suffixpos(const char *,const char *);
167 extern int dns_domain_fromdot(char **,const char *,unsigned int);
168 extern int dns_domain_todot_cat(str *,const char *);
169
170 extern unsigned int dns_packet_copy(const char *,unsigned int,unsigned int,unsigned char *,unsigned int);
171 extern unsigned int dns_packet_getname(const char *,unsigned int,unsigned int,char **);
172 extern unsigned int dns_packet_skipname(const char *,unsigned int,unsigned int);
173 extern int dns_packet_extract(struct dns_result* out, const char* buf, unsigned int len, uint16 rrtype, uint16 rrclass,
174 int (*sizefn)(const char* buf, unsigned int len, unsigned int pos, uint16 datalen),
175 int (*copy)(struct dns_result* out, unsigned int index, unsigned int offset,
176 const char* buf, unsigned int len, unsigned int pos, uint16 datalen));
177
178 extern int dns_transmit_start(struct dns_transmit *,const ipv4addr [DNS_MAX_IPS],int,const char *,uint16,const ipv4addr *);
179 extern void dns_transmit_free(struct dns_transmit *);
180 extern void dns_transmit_io(const struct dns_transmit *,iopoll_fd *,struct timeval *);
181 extern int dns_transmit_get(struct dns_transmit *,const iopoll_fd *,const struct timeval *);
182
183 extern int dns_read_resolvconf(str *out);
184 extern int dns_resolvconfip(ipv4addr [DNS_MAX_IPS]);
185 extern int dns_resolve(struct dns_transmit *,const char *,uint16);
186
187 extern int dns_ip4_packet(struct dns_result*, const char*, unsigned int);
188 extern int dns_ip4_r(struct dns_transmit*, struct dns_result*, const char *);
189 extern int dns_ip4(struct dns_result*, const char *);
190 extern int dns_ip6_packet(struct dns_result*, const char*, unsigned int);
191 extern int dns_ip6_r(struct dns_transmit*, struct dns_result*, const char *);
192 extern int dns_ip6(struct dns_result*, const char*);
193 extern int dns_name_packet(struct dns_result*, const char *,unsigned int);
194 extern void dns_name4_domain(char [DNS_NAME4_DOMAIN],const ipv4addr *);
195 extern void dns_name6_domain(char [DNS_NAME6_DOMAIN],const ipv6addr *);
196 extern int dns_name4_r(struct dns_transmit*, struct dns_result*, const ipv4addr*);
197 extern int dns_name4(struct dns_result*, const ipv4addr*);
198 extern int dns_name6_r(struct dns_transmit*, struct dns_result*, const ipv6addr*);
199 extern int dns_name6(struct dns_result*, const ipv6addr*);
200 extern int dns_txt_packet(struct dns_result*, const char *,unsigned int);
201 extern int dns_txt_r(struct dns_transmit *,struct dns_result*, const char *);
202 extern int dns_txt(struct dns_result*, const char *);
203 extern int dns_mx_packet(struct dns_result*, const char *,unsigned int);
204 extern int dns_mx_r(struct dns_transmit *,struct dns_result*, const char *);
205 extern int dns_mx(struct dns_result*, const char *);
206
207 extern int dns_resolvconfrewrite(str *);
208 extern int dns_qualify_rules(struct dns_result*,str *,const char *,const str *,
209 int (*)(struct dns_transmit*, struct dns_result*, const char*));
210 extern int dns_qualify(struct dns_result*,str *,const char *,
211 int (*)(struct dns_transmit*, struct dns_result*, const char*));
212
213 extern unsigned fmt_dns_domain(char*, const char*);
214
216 #define DNS_R_FN_WRAP(NAME,TYPE) \
217 int dns_##NAME(struct dns_result* out,TYPE in) \
218 { \
219 struct dns_transmit tx = {0}; \
220 int r = dns_##NAME##_r(&tx,out,in); \
221 dns_transmit_free(&tx); \
222 return r; \
223 }
224
227 #endif
DNS_RANDOM_SEED
#define DNS_RANDOM_SEED
Definition: dns.h:70
dns_transmit::s1
int s1
Definition: dns.h:91
fmt_dns_domain
unsigned fmt_dns_domain(char *, const char *)
Definition: dns_fmt.c:4
dns_mx_packet
int dns_mx_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_mx.c:34
dns_transmit
Definition: dns.h:79
dns_mx::distance
uint16 distance
Definition: dns.h:122
dns_transmit_get
int dns_transmit_get(struct dns_transmit *, const iopoll_fd *, const struct timeval *)
Definition: dns_transmit.c:257
dns_result_rrs
Definition: dns.h:127
dns_transmit::udploop
unsigned int udploop
Definition: dns.h:103
dns_resolvconfip
int dns_resolvconfip(ipv4addr [DNS_MAX_IPS])
Definition: dns_resolvconfip.c:84
dns_random
unsigned int dns_random(unsigned int)
Definition: dns_random.c:28
dns_txt_r
int dns_txt_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_txt.c:50
dns_qualify
int dns_qualify(struct dns_result *, str *, const char *, int(*)(struct dns_transmit *, struct dns_result *, const char *))
Definition: dns_qualify.c:99
dns_name4
int dns_name4(struct dns_result *, const ipv4addr *)
dns_txt_packet
int dns_txt_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_txt.c:44
DNS_NAME4_DOMAIN
#define DNS_NAME4_DOMAIN
Definition: dns.h:74
ipv6addr
Definition: ipv6.h:11
dns_transmit::querylen
unsigned int querylen
Definition: dns.h:83
dns_ip4_packet
int dns_ip4_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_ip4.c:18
dns_ip6
int dns_ip6(struct dns_result *, const char *)
dns_txt
int dns_txt(struct dns_result *, const char *)
dns_transmit_start
int dns_transmit_start(struct dns_transmit *, const ipv4addr [DNS_MAX_IPS], int, const char *, uint16, const ipv4addr *)
Definition: dns_transmit.c:204
dns_mx
int dns_mx(struct dns_result *, const char *)
dns_rotate
void dns_rotate(unsigned char *, unsigned int n, unsigned int shift)
Definition: dns_rotate.c:11
dns_result::count
int count
Definition: dns.h:140
dns_domain_fromdot
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_domain_fromdot.c:8
dns_transmit::localip
ipv4addr localip
Definition: dns.h:113
dns_transmit::tcpstate
int tcpstate
Definition: dns.h:101
dns_transmit::deadline
struct timeval deadline
Definition: dns.h:107
dns_transmit::curserver
unsigned int curserver
Definition: dns.h:105
dns_transmit::packet
char * packet
Definition: dns.h:85
dns_mx
Definition: dns.h:119
dns_result::__buffer
void * __buffer
Definition: dns.h:146
dns_name6_r
int dns_name6_r(struct dns_transmit *, struct dns_result *, const ipv6addr *)
Definition: dns_name6.c:4
dns_result_alloc
int dns_result_alloc(struct dns_result *d, int type, int count, int size)
Definition: dns_result.c:21
dns_ip4_r
int dns_ip4_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_ip4.c:27
dns_domain_todot_cat
int dns_domain_todot_cat(str *, const char *)
Definition: dns_domain_todot.c:4
dns_qualify_rules
int dns_qualify_rules(struct dns_result *, str *, const char *, const str *, int(*)(struct dns_transmit *, struct dns_result *, const char *))
Definition: dns_qualify.c:50
dns_transmit::packetlen
unsigned int packetlen
Definition: dns.h:87
dns_transmit::servers
const ipv4addr * servers
Definition: dns.h:111
dns_name4_domain
void dns_name4_domain(char [DNS_NAME4_DOMAIN], const ipv4addr *)
Definition: dns_name4_domain.c:7
dns_packet_copy
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, unsigned char *, unsigned int)
Definition: dns_packet.c:12
DNS_NAME6_DOMAIN
#define DNS_NAME6_DOMAIN
Definition: dns.h:76
dns_packet_getname
unsigned int dns_packet_getname(const char *, unsigned int, unsigned int, char **)
Definition: dns_packet.c:41
dns_packet_extract
int dns_packet_extract(struct dns_result *out, const char *buf, unsigned int len, uint16 rrtype, uint16 rrclass, int(*sizefn)(const char *buf, unsigned int len, unsigned int pos, uint16 datalen), int(*copy)(struct dns_result *out, unsigned int index, unsigned int offset, const char *buf, unsigned int len, unsigned int pos, uint16 datalen))
Definition: dns_packet.c:96
dns_result
Definition: dns.h:137
DNS_MAX_IPS
#define DNS_MAX_IPS
Definition: dns.h:72
dns_result_free
void dns_result_free(struct dns_result *d)
Definition: dns_result.c:8
dns_resolvconfrewrite
int dns_resolvconfrewrite(str *)
Definition: dns_resolvconfrewrite.c:128
dns_transmit::query
char * query
Definition: dns.h:81
dns_domain_length
unsigned int dns_domain_length(const char *)
Definition: dns_domain.c:8
dns_domain_suffix
int dns_domain_suffix(const char *, const char *)
Definition: dns_domain.c:56
ipv4addr
Definition: ipv4.h:11
dns_domain_free
void dns_domain_free(char **)
Definition: dns_domain.c:20
dns_read_resolvconf
int dns_read_resolvconf(str *out)
Definition: dns_resolvconfip.c:15
str
Definition: str.h:30
dns_domain_suffixpos
unsigned int dns_domain_suffixpos(const char *, const char *)
Definition: dns_domain.c:69
dns_packet_skipname
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:23
dns_name_packet
int dns_name_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_name_packet.c:31
dns_name6
int dns_name6(struct dns_result *, const ipv6addr *)
dns_transmit_free
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:79
dns_name4_r
int dns_name4_r(struct dns_transmit *, struct dns_result *, const ipv4addr *)
Definition: dns_name4.c:6
dns_rotateipv4
void dns_rotateipv4(ipv4addr *, unsigned int)
Definition: dns_rotate.c:27
dns_domain_equal
int dns_domain_equal(const char *, const char *)
Definition: dns_domain.c:44
dns_rotateipv6
void dns_rotateipv6(ipv6addr *, unsigned int)
Definition: dns_rotate.c:33
dns_ip6_r
int dns_ip6_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_ip6.c:27
dns_transmit::qtype
uint16 qtype
Definition: dns.h:115
dns_mx_r
int dns_mx_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_mx.c:40
dns_resolve
int dns_resolve(struct dns_transmit *, const char *, uint16)
Definition: dns_resolve.c:33
dns_mx::name
char * name
Definition: dns.h:124
dns_transmit::pos
unsigned int pos
Definition: dns.h:109
dns_ip6_packet
int dns_ip6_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_ip6.c:18
dns_name6_domain
void dns_name6_domain(char [DNS_NAME6_DOMAIN], const ipv6addr *)
Definition: dns_name6_domain.c:7
dns_ip4
int dns_ip4(struct dns_result *, const char *)
dns_transmit_io
void dns_transmit_io(const struct dns_transmit *, iopoll_fd *, struct timeval *)
Definition: dns_transmit.c:236
dns_domain_copy
int dns_domain_copy(char **, const char *)
Definition: dns_domain.c:29
dns_result::type
int type
Definition: dns.h:142