-
Notifications
You must be signed in to change notification settings - Fork 10
/
rdns.h
251 lines (221 loc) · 6.63 KB
/
rdns.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (c) 2013-2014, Vsevolod Stakhov
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef RDNS_H
#define RDNS_H
#include <sys/types.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct rdns_reply;
struct rdns_request;
struct rdns_io_channel;
typedef void (*dns_callback_type) (struct rdns_reply *reply, void *arg);
enum rdns_request_type {
DNS_REQUEST_A = 0,
DNS_REQUEST_PTR,
DNS_REQUEST_MX,
DNS_REQUEST_TXT,
DNS_REQUEST_SRV,
DNS_REQUEST_SPF,
DNS_REQUEST_AAA
};
union rdns_reply_element_un {
struct {
struct in_addr addr;
} a;
struct {
struct in6_addr addr;
} aaa;
struct {
char *name;
} ptr;
struct {
char *name;
uint16_t priority;
} mx;
struct {
char *data;
} txt;
struct {
uint16_t priority;
uint16_t weight;
uint16_t port;
char *target;
} srv;
};
struct rdns_reply_entry {
union rdns_reply_element_un content;
uint16_t type;
uint16_t ttl;
struct rdns_reply_entry *prev, *next;
};
enum dns_rcode {
DNS_RC_NOERROR = 0,
DNS_RC_FORMERR = 1,
DNS_RC_SERVFAIL = 2,
DNS_RC_NXDOMAIN = 3,
DNS_RC_NOTIMP = 4,
DNS_RC_REFUSED = 5,
DNS_RC_YXDOMAIN = 6,
DNS_RC_YXRRSET = 7,
DNS_RC_NXRRSET = 8,
DNS_RC_NOTAUTH = 9,
DNS_RC_NOTZONE = 10,
DNS_RC_TIMEOUT = 11,
DNS_RC_NETERR = 12
};
struct rdns_reply {
struct rdns_request *request;
struct rdns_resolver *resolver;
struct rdns_reply_entry *entries;
enum dns_rcode code;
};
typedef void (*rdns_periodic_callback)(void *user_data);
struct rdns_async_context {
void *data;
void* (*add_read)(void *priv_data, int fd, void *user_data);
void (*del_read)(void *priv_data, void *ev_data);
void* (*add_write)(void *priv_data, int fd, void *user_data);
void (*del_write)(void *priv_data, void *ev_data);
void* (*add_timer)(void *priv_data, double after, void *user_data);
void (*repeat_timer)(void *priv_data, void *ev_data);
void (*del_timer)(void *priv_data, void *ev_data);
void* (*add_periodic)(void *priv_data, double after,
rdns_periodic_callback cb, void *user_data);
void (*del_periodic)(void *priv_data, void *ev_data);
void (*cleanup)(void *priv_data);
};
/**
* Type of rdns plugin
*/
enum rdns_plugin_type {
RDNS_PLUGIN_NETWORK = 0//!< use the specified plugin instead of send/recv functions
};
typedef ssize_t (*rdns_network_send_callback) (struct rdns_request *req, void *plugin_data);
typedef ssize_t (*rdns_network_recv_callback) (struct rdns_io_channel *ioc, void *buf,
size_t len, void *plugin_data, struct rdns_request **req_out);
typedef void (*rdns_network_finish_callback) (struct rdns_request *req, void *plugin_data);
typedef void (*rdns_plugin_dtor_callback) (struct rdns_resolver *resolver, void *plugin_data);
struct rdns_plugin {
enum rdns_plugin_type type;
union {
struct {
rdns_network_send_callback send_cb;
rdns_network_recv_callback recv_cb;
rdns_network_finish_callback finish_cb;
} network_plugin;
} cb;
rdns_plugin_dtor_callback dtor;
void *data;
};
/* Rspamd DNS API */
/**
* Create DNS resolver structure
*/
struct rdns_resolver *rdns_resolver_new (void);
/**
* Bind resolver to specified async context
* @param ctx
*/
void rdns_resolver_async_bind (struct rdns_resolver *resolver,
struct rdns_async_context *ctx);
/**
* Add new DNS server definition to the resolver
* @param resolver resolver object
* @param name name of DNS server (should be ipv4 or ipv6 address)
* @param priority priority (can be 0 for fair round-robin)
* @param io_cnt a number of sockets that are simultaneously opened to this server
* @return true if a server has been added to resolver
*/
bool rdns_resolver_add_server (struct rdns_resolver *resolver,
const char *name, unsigned int port,
int priority, unsigned int io_cnt);
/**
* Register new plugin for rdns resolver
* @param resolver
* @param plugin
*/
void rdns_resolver_register_plugin (struct rdns_resolver *resolver,
struct rdns_plugin *plugin);
/**
* Init DNS resolver
* @param resolver
* @return
*/
bool rdns_resolver_init (struct rdns_resolver *resolver);
/**
* Decrease refcount for a resolver and free it if refcount is 0
* @param resolver
*/
void rdns_resolver_release (struct rdns_resolver *resolver);
/**
* Make a DNS request
* @param resolver resolver object
* @param cb callback to call on resolve completing
* @param ud user data for callback
* @param timeout timeout in seconds
* @param repeats how much time to retransmit query
* @param queries how much RR queries to send
* @param ... -> queries in format: <query_type>[,type_argument[,type_argument...]]
* @return opaque request object or NULL
*/
struct rdns_request* rdns_make_request_full (
struct rdns_resolver *resolver,
dns_callback_type cb,
void *cbdata,
double timeout,
unsigned int repeats,
const char *name,
unsigned int queries,
...
);
/**
* Get textual presentation of DNS error code
*/
const char *rdns_strerror (enum dns_rcode rcode);
/**
* Get textual presentation of DNS request type
*/
const char *rdns_strtype (enum rdns_request_type type);
/**
* Increase refcount for a request
* @param req
* @return
*/
struct rdns_request* rdns_request_retain (struct rdns_request *req);
/**
* Decrease refcount for a request and free it if refcount is 0
* @param req
*/
void rdns_request_release (struct rdns_request *req);
/*
* Private functions used by async libraries as callbacks
*/
void rdns_process_read (int fd, void *arg);
void rdns_process_timer (void *arg);
void rdns_process_retransmit (int fd, void *arg);
#endif