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;
115  uint16 qtype;
116 };
117 
119 struct dns_mx
120 {
122  uint16 distance;
124  char* name;
125 };
126 
128 {
129  void* __ptr;
130  ipv4addr* ip4;
131  ipv6addr* ip6;
132  struct dns_mx* mx;
133  char** name;
134 };
135 
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);
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
#define DNS_RANDOM_SEED
Definition: dns.h:70
int s1
Definition: dns.h:91
unsigned fmt_dns_domain(char *, const char *)
Definition: dns_fmt.c:4
int dns_mx_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_mx.c:34
Definition: dns.h:79
uint16 distance
Definition: dns.h:122
int dns_transmit_get(struct dns_transmit *, const iopoll_fd *, const struct timeval *)
Definition: dns_transmit.c:257
Definition: dns.h:127
unsigned int udploop
Definition: dns.h:103
int dns_resolvconfip(ipv4addr [DNS_MAX_IPS])
Definition: dns_resolvconfip.c:84
unsigned int dns_random(unsigned int)
Definition: dns_random.c:28
int dns_txt_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_txt.c:50
int dns_qualify(struct dns_result *, str *, const char *, int(*)(struct dns_transmit *, struct dns_result *, const char *))
Definition: dns_qualify.c:99
int dns_name4(struct dns_result *, const ipv4addr *)
int dns_txt_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_txt.c:44
#define DNS_NAME4_DOMAIN
Definition: dns.h:74
Definition: ipv6.h:11
unsigned int querylen
Definition: dns.h:83
int dns_ip4_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_ip4.c:18
int dns_ip6(struct dns_result *, const char *)
int dns_txt(struct dns_result *, const char *)
int dns_transmit_start(struct dns_transmit *, const ipv4addr [DNS_MAX_IPS], int, const char *, uint16, const ipv4addr *)
Definition: dns_transmit.c:204
int dns_mx(struct dns_result *, const char *)
void dns_rotate(unsigned char *, unsigned int n, unsigned int shift)
Definition: dns_rotate.c:11
int count
Definition: dns.h:140
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_domain_fromdot.c:8
ipv4addr localip
Definition: dns.h:113
int tcpstate
Definition: dns.h:101
struct timeval deadline
Definition: dns.h:107
unsigned int curserver
Definition: dns.h:105
char * packet
Definition: dns.h:85
Definition: dns.h:119
void * __buffer
Definition: dns.h:146
int dns_name6_r(struct dns_transmit *, struct dns_result *, const ipv6addr *)
Definition: dns_name6.c:4
int dns_result_alloc(struct dns_result *d, int type, int count, int size)
Definition: dns_result.c:21
int dns_ip4_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_ip4.c:27
int dns_domain_todot_cat(str *, const char *)
Definition: dns_domain_todot.c:4
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
unsigned int packetlen
Definition: dns.h:87
const ipv4addr * servers
Definition: dns.h:111
void dns_name4_domain(char [DNS_NAME4_DOMAIN], const ipv4addr *)
Definition: dns_name4_domain.c:7
unsigned int dns_packet_copy(const char *, unsigned int, unsigned int, unsigned char *, unsigned int)
Definition: dns_packet.c:12
#define DNS_NAME6_DOMAIN
Definition: dns.h:76
unsigned int dns_packet_getname(const char *, unsigned int, unsigned int, char **)
Definition: dns_packet.c:41
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
Definition: dns.h:137
#define DNS_MAX_IPS
Definition: dns.h:72
void dns_result_free(struct dns_result *d)
Definition: dns_result.c:8
int dns_resolvconfrewrite(str *)
Definition: dns_resolvconfrewrite.c:128
char * query
Definition: dns.h:81
unsigned int dns_domain_length(const char *)
Definition: dns_domain.c:8
int dns_domain_suffix(const char *, const char *)
Definition: dns_domain.c:56
Definition: ipv4.h:11
void dns_domain_free(char **)
Definition: dns_domain.c:20
int dns_read_resolvconf(str *out)
Definition: dns_resolvconfip.c:15
Definition: str.h:30
unsigned int dns_domain_suffixpos(const char *, const char *)
Definition: dns_domain.c:69
unsigned int dns_packet_skipname(const char *, unsigned int, unsigned int)
Definition: dns_packet.c:23
int dns_name_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_name_packet.c:31
int dns_name6(struct dns_result *, const ipv6addr *)
void dns_transmit_free(struct dns_transmit *)
Definition: dns_transmit.c:79
int dns_name4_r(struct dns_transmit *, struct dns_result *, const ipv4addr *)
Definition: dns_name4.c:6
void dns_rotateipv4(ipv4addr *, unsigned int)
Definition: dns_rotate.c:27
int dns_domain_equal(const char *, const char *)
Definition: dns_domain.c:44
void dns_rotateipv6(ipv6addr *, unsigned int)
Definition: dns_rotate.c:33
int dns_ip6_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_ip6.c:27
uint16 qtype
Definition: dns.h:115
int dns_mx_r(struct dns_transmit *, struct dns_result *, const char *)
Definition: dns_mx.c:40
int dns_resolve(struct dns_transmit *, const char *, uint16)
Definition: dns_resolve.c:33
char * name
Definition: dns.h:124
unsigned int pos
Definition: dns.h:109
int dns_ip6_packet(struct dns_result *, const char *, unsigned int)
Definition: dns_ip6.c:18
void dns_name6_domain(char [DNS_NAME6_DOMAIN], const ipv6addr *)
Definition: dns_name6_domain.c:7
int dns_ip4(struct dns_result *, const char *)
void dns_transmit_io(const struct dns_transmit *, iopoll_fd *, struct timeval *)
Definition: dns_transmit.c:236
int dns_domain_copy(char **, const char *)
Definition: dns_domain.c:29
int type
Definition: dns.h:142