Deleted Added
full compact
outside_network.h (266114) outside_network.h (276605)
1/*
2 * services/outside_network.h - listen to answers from the network
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file has functions to send queries to authoritative servers,
40 * and wait for the pending answer, with timeouts.
41 */
42
43#ifndef OUTSIDE_NETWORK_H
44#define OUTSIDE_NETWORK_H
45
46#include "util/rbtree.h"
47#include "util/netevent.h"
1/*
2 * services/outside_network.h - listen to answers from the network
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file has functions to send queries to authoritative servers,
40 * and wait for the pending answer, with timeouts.
41 */
42
43#ifndef OUTSIDE_NETWORK_H
44#define OUTSIDE_NETWORK_H
45
46#include "util/rbtree.h"
47#include "util/netevent.h"
48#include "dnstap/dnstap_config.h"
48struct pending;
49struct pending_timeout;
50struct ub_randstate;
51struct pending_tcp;
52struct waiting_tcp;
53struct waiting_udp;
54struct infra_cache;
55struct port_comm;
56struct port_if;
57struct sldns_buffer;
49struct pending;
50struct pending_timeout;
51struct ub_randstate;
52struct pending_tcp;
53struct waiting_tcp;
54struct waiting_udp;
55struct infra_cache;
56struct port_comm;
57struct port_if;
58struct sldns_buffer;
59struct serviced_query;
60struct dt_env;
58
59/**
60 * Send queries to outside servers and wait for answers from servers.
61 * Contains answer-listen sockets.
62 */
63struct outside_network {
64 /** Base for select calls */
65 struct comm_base* base;
66 /** pointer to time in seconds */
67 time_t* now_secs;
68 /** pointer to time in microseconds */
69 struct timeval* now_tv;
70
71 /** buffer shared by UDP connections, since there is only one
72 datagram at any time. */
73 struct sldns_buffer* udp_buff;
74 /** serviced_callbacks malloc overhead when processing multiple
75 * identical serviced queries to the same server. */
76 size_t svcd_overhead;
77 /** use x20 bits to encode additional ID random bits */
78 int use_caps_for_id;
79 /** outside network wants to quit. Stop queued msgs from sent. */
80 int want_to_quit;
81
82 /** number of unwanted replies received (for statistics) */
83 size_t unwanted_replies;
84 /** cumulative total of unwanted replies (for defense) */
85 size_t unwanted_total;
86 /** threshold when to take defensive action. If 0 then never. */
87 size_t unwanted_threshold;
88 /** what action to take, called when defensive action is needed */
89 void (*unwanted_action)(void*);
90 /** user param for action */
91 void* unwanted_param;
92
93 /** linked list of available commpoints, unused file descriptors,
94 * for use as outgoing UDP ports. cp.fd=-1 in them. */
95 struct port_comm* unused_fds;
96 /** if udp is done */
97 int do_udp;
98 /** if udp is delay-closed (delayed answers do not meet closed port)*/
99 int delayclose;
100 /** timeout for delayclose */
101 struct timeval delay_tv;
102
103 /** array of outgoing IP4 interfaces */
104 struct port_if* ip4_ifs;
105 /** number of outgoing IP4 interfaces */
106 int num_ip4;
107
108 /** array of outgoing IP6 interfaces */
109 struct port_if* ip6_ifs;
110 /** number of outgoing IP6 interfaces */
111 int num_ip6;
112
113 /** pending udp queries waiting to be sent out, waiting for fd */
114 struct pending* udp_wait_first;
115 /** last pending udp query in list */
116 struct pending* udp_wait_last;
117
118 /** pending udp answers. sorted by id, addr */
119 rbtree_t* pending;
120 /** serviced queries, sorted by qbuf, addr, dnssec */
121 rbtree_t* serviced;
122 /** host cache, pointer but not owned by outnet. */
123 struct infra_cache* infra;
124 /** where to get random numbers */
125 struct ub_randstate* rnd;
126 /** ssl context to create ssl wrapped TCP with DNS connections */
127 void* sslctx;
61
62/**
63 * Send queries to outside servers and wait for answers from servers.
64 * Contains answer-listen sockets.
65 */
66struct outside_network {
67 /** Base for select calls */
68 struct comm_base* base;
69 /** pointer to time in seconds */
70 time_t* now_secs;
71 /** pointer to time in microseconds */
72 struct timeval* now_tv;
73
74 /** buffer shared by UDP connections, since there is only one
75 datagram at any time. */
76 struct sldns_buffer* udp_buff;
77 /** serviced_callbacks malloc overhead when processing multiple
78 * identical serviced queries to the same server. */
79 size_t svcd_overhead;
80 /** use x20 bits to encode additional ID random bits */
81 int use_caps_for_id;
82 /** outside network wants to quit. Stop queued msgs from sent. */
83 int want_to_quit;
84
85 /** number of unwanted replies received (for statistics) */
86 size_t unwanted_replies;
87 /** cumulative total of unwanted replies (for defense) */
88 size_t unwanted_total;
89 /** threshold when to take defensive action. If 0 then never. */
90 size_t unwanted_threshold;
91 /** what action to take, called when defensive action is needed */
92 void (*unwanted_action)(void*);
93 /** user param for action */
94 void* unwanted_param;
95
96 /** linked list of available commpoints, unused file descriptors,
97 * for use as outgoing UDP ports. cp.fd=-1 in them. */
98 struct port_comm* unused_fds;
99 /** if udp is done */
100 int do_udp;
101 /** if udp is delay-closed (delayed answers do not meet closed port)*/
102 int delayclose;
103 /** timeout for delayclose */
104 struct timeval delay_tv;
105
106 /** array of outgoing IP4 interfaces */
107 struct port_if* ip4_ifs;
108 /** number of outgoing IP4 interfaces */
109 int num_ip4;
110
111 /** array of outgoing IP6 interfaces */
112 struct port_if* ip6_ifs;
113 /** number of outgoing IP6 interfaces */
114 int num_ip6;
115
116 /** pending udp queries waiting to be sent out, waiting for fd */
117 struct pending* udp_wait_first;
118 /** last pending udp query in list */
119 struct pending* udp_wait_last;
120
121 /** pending udp answers. sorted by id, addr */
122 rbtree_t* pending;
123 /** serviced queries, sorted by qbuf, addr, dnssec */
124 rbtree_t* serviced;
125 /** host cache, pointer but not owned by outnet. */
126 struct infra_cache* infra;
127 /** where to get random numbers */
128 struct ub_randstate* rnd;
129 /** ssl context to create ssl wrapped TCP with DNS connections */
130 void* sslctx;
131#ifdef USE_DNSTAP
132 /** dnstap environment */
133 struct dt_env* dtenv;
134#endif
128
129 /**
130 * Array of tcp pending used for outgoing TCP connections.
131 * Each can be used to establish a TCP connection with a server.
132 * The file descriptors are -1 if they are free, and need to be
133 * opened for the tcp connection. Can be used for ip4 and ip6.
134 */
135 struct pending_tcp **tcp_conns;
136 /** number of tcp communication points. */
137 size_t num_tcp;
135
136 /**
137 * Array of tcp pending used for outgoing TCP connections.
138 * Each can be used to establish a TCP connection with a server.
139 * The file descriptors are -1 if they are free, and need to be
140 * opened for the tcp connection. Can be used for ip4 and ip6.
141 */
142 struct pending_tcp **tcp_conns;
143 /** number of tcp communication points. */
144 size_t num_tcp;
145 /** number of tcp communication points in use. */
146 size_t num_tcp_outgoing;
138 /** list of tcp comm points that are free for use */
139 struct pending_tcp* tcp_free;
140 /** list of tcp queries waiting for a buffer */
141 struct waiting_tcp* tcp_wait_first;
142 /** last of waiting query list */
143 struct waiting_tcp* tcp_wait_last;
144};
145
146/**
147 * Outgoing interface. Ports available and currently used are tracked
148 * per interface
149 */
150struct port_if {
151 /** address ready to allocate new socket (except port no). */
152 struct sockaddr_storage addr;
153 /** length of addr field */
154 socklen_t addrlen;
155
156 /** the available ports array. These are unused.
157 * Only the first total-inuse part is filled. */
158 int* avail_ports;
159 /** the total number of available ports (size of the array) */
160 int avail_total;
161
162 /** array of the commpoints currently in use.
163 * allocated for max number of fds, first part in use. */
164 struct port_comm** out;
165 /** max number of fds, size of out array */
166 int maxout;
167 /** number of commpoints (and thus also ports) in use */
168 int inuse;
169};
170
171/**
172 * Outgoing commpoint for UDP port.
173 */
174struct port_comm {
175 /** next in free list */
176 struct port_comm* next;
177 /** which port number (when in use) */
178 int number;
179 /** interface it is used in */
180 struct port_if* pif;
181 /** index in the out array of the interface */
182 int index;
183 /** number of outstanding queries on this port */
184 int num_outstanding;
185 /** UDP commpoint, fd=-1 if not in use */
186 struct comm_point* cp;
187};
188
189/**
190 * A query that has an answer pending for it.
191 */
192struct pending {
193 /** redblacktree entry, key is the pending struct(id, addr). */
194 rbnode_t node;
195 /** the ID for the query. int so that a value out of range can
196 * be used to signify a pending that is for certain not present in
197 * the rbtree. (and for which deletion is safe). */
198 unsigned int id;
199 /** remote address. */
200 struct sockaddr_storage addr;
201 /** length of addr field in use. */
202 socklen_t addrlen;
203 /** comm point it was sent on (and reply must come back on). */
204 struct port_comm* pc;
205 /** timeout event */
206 struct comm_timer* timer;
207 /** callback for the timeout, error or reply to the message */
208 comm_point_callback_t* cb;
209 /** callback user argument */
210 void* cb_arg;
211 /** the outside network it is part of */
212 struct outside_network* outnet;
147 /** list of tcp comm points that are free for use */
148 struct pending_tcp* tcp_free;
149 /** list of tcp queries waiting for a buffer */
150 struct waiting_tcp* tcp_wait_first;
151 /** last of waiting query list */
152 struct waiting_tcp* tcp_wait_last;
153};
154
155/**
156 * Outgoing interface. Ports available and currently used are tracked
157 * per interface
158 */
159struct port_if {
160 /** address ready to allocate new socket (except port no). */
161 struct sockaddr_storage addr;
162 /** length of addr field */
163 socklen_t addrlen;
164
165 /** the available ports array. These are unused.
166 * Only the first total-inuse part is filled. */
167 int* avail_ports;
168 /** the total number of available ports (size of the array) */
169 int avail_total;
170
171 /** array of the commpoints currently in use.
172 * allocated for max number of fds, first part in use. */
173 struct port_comm** out;
174 /** max number of fds, size of out array */
175 int maxout;
176 /** number of commpoints (and thus also ports) in use */
177 int inuse;
178};
179
180/**
181 * Outgoing commpoint for UDP port.
182 */
183struct port_comm {
184 /** next in free list */
185 struct port_comm* next;
186 /** which port number (when in use) */
187 int number;
188 /** interface it is used in */
189 struct port_if* pif;
190 /** index in the out array of the interface */
191 int index;
192 /** number of outstanding queries on this port */
193 int num_outstanding;
194 /** UDP commpoint, fd=-1 if not in use */
195 struct comm_point* cp;
196};
197
198/**
199 * A query that has an answer pending for it.
200 */
201struct pending {
202 /** redblacktree entry, key is the pending struct(id, addr). */
203 rbnode_t node;
204 /** the ID for the query. int so that a value out of range can
205 * be used to signify a pending that is for certain not present in
206 * the rbtree. (and for which deletion is safe). */
207 unsigned int id;
208 /** remote address. */
209 struct sockaddr_storage addr;
210 /** length of addr field in use. */
211 socklen_t addrlen;
212 /** comm point it was sent on (and reply must come back on). */
213 struct port_comm* pc;
214 /** timeout event */
215 struct comm_timer* timer;
216 /** callback for the timeout, error or reply to the message */
217 comm_point_callback_t* cb;
218 /** callback user argument */
219 void* cb_arg;
220 /** the outside network it is part of */
221 struct outside_network* outnet;
222 /** the corresponding serviced_query */
223 struct serviced_query* sq;
213
214 /*---- filled if udp pending is waiting -----*/
215 /** next in waiting list. */
216 struct pending* next_waiting;
217 /** timeout in msec */
218 int timeout;
219 /** The query itself, the query packet to send. */
220 uint8_t* pkt;
221 /** length of query packet. */
222 size_t pkt_len;
223};
224
225/**
226 * Pending TCP query to server.
227 */
228struct pending_tcp {
229 /** next in list of free tcp comm points, or NULL. */
230 struct pending_tcp* next_free;
231 /** the ID for the query; checked in reply */
232 uint16_t id;
233 /** tcp comm point it was sent on (and reply must come back on). */
234 struct comm_point* c;
235 /** the query being serviced, NULL if the pending_tcp is unused. */
236 struct waiting_tcp* query;
237};
238
239/**
240 * Query waiting for TCP buffer.
241 */
242struct waiting_tcp {
243 /**
244 * next in waiting list.
245 * if pkt==0, this points to the pending_tcp structure.
246 */
247 struct waiting_tcp* next_waiting;
248 /** timeout event; timer keeps running whether the query is
249 * waiting for a buffer or the tcp reply is pending */
250 struct comm_timer* timer;
251 /** the outside network it is part of */
252 struct outside_network* outnet;
253 /** remote address. */
254 struct sockaddr_storage addr;
255 /** length of addr field in use. */
256 socklen_t addrlen;
257 /**
258 * The query itself, the query packet to send.
259 * allocated after the waiting_tcp structure.
260 * set to NULL when the query is serviced and it part of pending_tcp.
261 * if this is NULL, the next_waiting points to the pending_tcp.
262 */
263 uint8_t* pkt;
264 /** length of query packet. */
265 size_t pkt_len;
266 /** callback for the timeout, error or reply to the message */
267 comm_point_callback_t* cb;
268 /** callback user argument */
269 void* cb_arg;
270 /** if it uses ssl upstream */
271 int ssl_upstream;
272};
273
274/**
275 * Callback to party interested in serviced query results.
276 */
277struct service_callback {
278 /** next in callback list */
279 struct service_callback* next;
280 /** callback function */
281 comm_point_callback_t* cb;
282 /** user argument for callback function */
283 void* cb_arg;
284};
285
286/** fallback size for fragmentation for EDNS in IPv4 */
287#define EDNS_FRAG_SIZE_IP4 1472
288/** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
289#define EDNS_FRAG_SIZE_IP6 1232
290
291/**
292 * Query service record.
293 * Contains query and destination. UDP, TCP, EDNS are all tried.
294 * complete with retries and timeouts. A number of interested parties can
295 * receive a callback.
296 */
297struct serviced_query {
298 /** The rbtree node, key is this record */
299 rbnode_t node;
300 /** The query that needs to be answered. Starts with flags u16,
301 * then qdcount, ..., including qname, qtype, qclass. Does not include
302 * EDNS record. */
303 uint8_t* qbuf;
304 /** length of qbuf. */
305 size_t qbuflen;
306 /** If an EDNS section is included, the DO/CD bit will be turned on. */
307 int dnssec;
308 /** We want signatures, or else the answer is likely useless */
309 int want_dnssec;
224
225 /*---- filled if udp pending is waiting -----*/
226 /** next in waiting list. */
227 struct pending* next_waiting;
228 /** timeout in msec */
229 int timeout;
230 /** The query itself, the query packet to send. */
231 uint8_t* pkt;
232 /** length of query packet. */
233 size_t pkt_len;
234};
235
236/**
237 * Pending TCP query to server.
238 */
239struct pending_tcp {
240 /** next in list of free tcp comm points, or NULL. */
241 struct pending_tcp* next_free;
242 /** the ID for the query; checked in reply */
243 uint16_t id;
244 /** tcp comm point it was sent on (and reply must come back on). */
245 struct comm_point* c;
246 /** the query being serviced, NULL if the pending_tcp is unused. */
247 struct waiting_tcp* query;
248};
249
250/**
251 * Query waiting for TCP buffer.
252 */
253struct waiting_tcp {
254 /**
255 * next in waiting list.
256 * if pkt==0, this points to the pending_tcp structure.
257 */
258 struct waiting_tcp* next_waiting;
259 /** timeout event; timer keeps running whether the query is
260 * waiting for a buffer or the tcp reply is pending */
261 struct comm_timer* timer;
262 /** the outside network it is part of */
263 struct outside_network* outnet;
264 /** remote address. */
265 struct sockaddr_storage addr;
266 /** length of addr field in use. */
267 socklen_t addrlen;
268 /**
269 * The query itself, the query packet to send.
270 * allocated after the waiting_tcp structure.
271 * set to NULL when the query is serviced and it part of pending_tcp.
272 * if this is NULL, the next_waiting points to the pending_tcp.
273 */
274 uint8_t* pkt;
275 /** length of query packet. */
276 size_t pkt_len;
277 /** callback for the timeout, error or reply to the message */
278 comm_point_callback_t* cb;
279 /** callback user argument */
280 void* cb_arg;
281 /** if it uses ssl upstream */
282 int ssl_upstream;
283};
284
285/**
286 * Callback to party interested in serviced query results.
287 */
288struct service_callback {
289 /** next in callback list */
290 struct service_callback* next;
291 /** callback function */
292 comm_point_callback_t* cb;
293 /** user argument for callback function */
294 void* cb_arg;
295};
296
297/** fallback size for fragmentation for EDNS in IPv4 */
298#define EDNS_FRAG_SIZE_IP4 1472
299/** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
300#define EDNS_FRAG_SIZE_IP6 1232
301
302/**
303 * Query service record.
304 * Contains query and destination. UDP, TCP, EDNS are all tried.
305 * complete with retries and timeouts. A number of interested parties can
306 * receive a callback.
307 */
308struct serviced_query {
309 /** The rbtree node, key is this record */
310 rbnode_t node;
311 /** The query that needs to be answered. Starts with flags u16,
312 * then qdcount, ..., including qname, qtype, qclass. Does not include
313 * EDNS record. */
314 uint8_t* qbuf;
315 /** length of qbuf. */
316 size_t qbuflen;
317 /** If an EDNS section is included, the DO/CD bit will be turned on. */
318 int dnssec;
319 /** We want signatures, or else the answer is likely useless */
320 int want_dnssec;
321 /** ignore capsforid */
322 int nocaps;
310 /** tcp upstream used, use tcp, or ssl_upstream for SSL */
311 int tcp_upstream, ssl_upstream;
312 /** where to send it */
313 struct sockaddr_storage addr;
314 /** length of addr field in use. */
315 socklen_t addrlen;
316 /** zone name, uncompressed domain name in wireformat */
317 uint8_t* zone;
318 /** length of zone name */
319 size_t zonelen;
320 /** qtype */
321 int qtype;
322 /** current status */
323 enum serviced_query_status {
324 /** initial status */
325 serviced_initial,
326 /** UDP with EDNS sent */
327 serviced_query_UDP_EDNS,
328 /** UDP without EDNS sent */
329 serviced_query_UDP,
330 /** TCP with EDNS sent */
331 serviced_query_TCP_EDNS,
332 /** TCP without EDNS sent */
333 serviced_query_TCP,
334 /** probe to test EDNS lameness (EDNS is dropped) */
335 serviced_query_PROBE_EDNS,
336 /** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
337 serviced_query_UDP_EDNS_fallback,
338 /** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
339 serviced_query_TCP_EDNS_fallback,
340 /** send UDP query with EDNS1480 (or 1280) */
341 serviced_query_UDP_EDNS_FRAG
342 }
343 /** variable with current status */
344 status;
345 /** true if serviced_query is scheduled for deletion already */
346 int to_be_deleted;
347 /** number of UDP retries */
348 int retry;
349 /** time last UDP was sent */
350 struct timeval last_sent_time;
351 /** rtt of last (UDP) message */
352 int last_rtt;
353 /** do we know edns probe status already, for UDP_EDNS queries */
354 int edns_lame_known;
355 /** outside network this is part of */
356 struct outside_network* outnet;
357 /** list of interested parties that need callback on results. */
358 struct service_callback* cblist;
359 /** the UDP or TCP query that is pending, see status which */
360 void* pending;
361};
362
363/**
364 * Create outside_network structure with N udp ports.
365 * @param base: the communication base to use for event handling.
366 * @param bufsize: size for network buffers.
367 * @param num_ports: number of udp ports to open per interface.
368 * @param ifs: interface names (or NULL for default interface).
369 * These interfaces must be able to access all authoritative servers.
370 * @param num_ifs: number of names in array ifs.
371 * @param do_ip4: service IP4.
372 * @param do_ip6: service IP6.
373 * @param num_tcp: number of outgoing tcp buffers to preallocate.
374 * @param infra: pointer to infra cached used for serviced queries.
375 * @param rnd: stored to create random numbers for serviced queries.
376 * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
377 * @param availports: array of available ports.
378 * @param numavailports: number of available ports in array.
379 * @param unwanted_threshold: when to take defensive action.
380 * @param unwanted_action: the action to take.
381 * @param unwanted_param: user parameter to action.
382 * @param do_udp: if udp is done.
383 * @param sslctx: context to create outgoing connections with (if enabled).
384 * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
385 * msec to wait on timeouted udp sockets.
323 /** tcp upstream used, use tcp, or ssl_upstream for SSL */
324 int tcp_upstream, ssl_upstream;
325 /** where to send it */
326 struct sockaddr_storage addr;
327 /** length of addr field in use. */
328 socklen_t addrlen;
329 /** zone name, uncompressed domain name in wireformat */
330 uint8_t* zone;
331 /** length of zone name */
332 size_t zonelen;
333 /** qtype */
334 int qtype;
335 /** current status */
336 enum serviced_query_status {
337 /** initial status */
338 serviced_initial,
339 /** UDP with EDNS sent */
340 serviced_query_UDP_EDNS,
341 /** UDP without EDNS sent */
342 serviced_query_UDP,
343 /** TCP with EDNS sent */
344 serviced_query_TCP_EDNS,
345 /** TCP without EDNS sent */
346 serviced_query_TCP,
347 /** probe to test EDNS lameness (EDNS is dropped) */
348 serviced_query_PROBE_EDNS,
349 /** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
350 serviced_query_UDP_EDNS_fallback,
351 /** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
352 serviced_query_TCP_EDNS_fallback,
353 /** send UDP query with EDNS1480 (or 1280) */
354 serviced_query_UDP_EDNS_FRAG
355 }
356 /** variable with current status */
357 status;
358 /** true if serviced_query is scheduled for deletion already */
359 int to_be_deleted;
360 /** number of UDP retries */
361 int retry;
362 /** time last UDP was sent */
363 struct timeval last_sent_time;
364 /** rtt of last (UDP) message */
365 int last_rtt;
366 /** do we know edns probe status already, for UDP_EDNS queries */
367 int edns_lame_known;
368 /** outside network this is part of */
369 struct outside_network* outnet;
370 /** list of interested parties that need callback on results. */
371 struct service_callback* cblist;
372 /** the UDP or TCP query that is pending, see status which */
373 void* pending;
374};
375
376/**
377 * Create outside_network structure with N udp ports.
378 * @param base: the communication base to use for event handling.
379 * @param bufsize: size for network buffers.
380 * @param num_ports: number of udp ports to open per interface.
381 * @param ifs: interface names (or NULL for default interface).
382 * These interfaces must be able to access all authoritative servers.
383 * @param num_ifs: number of names in array ifs.
384 * @param do_ip4: service IP4.
385 * @param do_ip6: service IP6.
386 * @param num_tcp: number of outgoing tcp buffers to preallocate.
387 * @param infra: pointer to infra cached used for serviced queries.
388 * @param rnd: stored to create random numbers for serviced queries.
389 * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
390 * @param availports: array of available ports.
391 * @param numavailports: number of available ports in array.
392 * @param unwanted_threshold: when to take defensive action.
393 * @param unwanted_action: the action to take.
394 * @param unwanted_param: user parameter to action.
395 * @param do_udp: if udp is done.
396 * @param sslctx: context to create outgoing connections with (if enabled).
397 * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
398 * msec to wait on timeouted udp sockets.
399 * @param dtenv: environment to send dnstap events with (if enabled).
386 * @return: the new structure (with no pending answers) or NULL on error.
387 */
388struct outside_network* outside_network_create(struct comm_base* base,
389 size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
390 int do_ip4, int do_ip6, size_t num_tcp, struct infra_cache* infra,
391 struct ub_randstate* rnd, int use_caps_for_id, int* availports,
392 int numavailports, size_t unwanted_threshold,
393 void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
400 * @return: the new structure (with no pending answers) or NULL on error.
401 */
402struct outside_network* outside_network_create(struct comm_base* base,
403 size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
404 int do_ip4, int do_ip6, size_t num_tcp, struct infra_cache* infra,
405 struct ub_randstate* rnd, int use_caps_for_id, int* availports,
406 int numavailports, size_t unwanted_threshold,
407 void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
394 void* sslctx, int delayclose);
408 void* sslctx, int delayclose, struct dt_env *dtenv);
395
396/**
397 * Delete outside_network structure.
398 * @param outnet: object to delete.
399 */
400void outside_network_delete(struct outside_network* outnet);
401
402/**
403 * Prepare for quit. Sends no more queries, even if queued up.
404 * @param outnet: object to prepare for removal
405 */
406void outside_network_quit_prepare(struct outside_network* outnet);
407
408/**
409 * Send UDP query, create pending answer.
410 * Changes the ID for the query to be random and unique for that destination.
409
410/**
411 * Delete outside_network structure.
412 * @param outnet: object to delete.
413 */
414void outside_network_delete(struct outside_network* outnet);
415
416/**
417 * Prepare for quit. Sends no more queries, even if queued up.
418 * @param outnet: object to prepare for removal
419 */
420void outside_network_quit_prepare(struct outside_network* outnet);
421
422/**
423 * Send UDP query, create pending answer.
424 * Changes the ID for the query to be random and unique for that destination.
411 * @param outnet: provides the event handling
425 * @param sq: serviced query.
412 * @param packet: wireformat query to send to destination.
426 * @param packet: wireformat query to send to destination.
413 * @param addr: address to send to.
414 * @param addrlen: length of addr.
415 * @param timeout: in milliseconds from now.
416 * @param callback: function to call on error, timeout or reply.
417 * @param callback_arg: user argument for callback function.
418 * @return: NULL on error for malloc or socket. Else the pending query object.
419 */
427 * @param timeout: in milliseconds from now.
428 * @param callback: function to call on error, timeout or reply.
429 * @param callback_arg: user argument for callback function.
430 * @return: NULL on error for malloc or socket. Else the pending query object.
431 */
420struct pending* pending_udp_query(struct outside_network* outnet,
421 struct sldns_buffer* packet, struct sockaddr_storage* addr,
422 socklen_t addrlen, int timeout, comm_point_callback_t* callback,
432struct pending* pending_udp_query(struct serviced_query* sq,
433 struct sldns_buffer* packet, int timeout, comm_point_callback_t* callback,
423 void* callback_arg);
424
425/**
426 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
427 * checks id.
434 void* callback_arg);
435
436/**
437 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
438 * checks id.
428 * @param outnet: provides the event handling.
439 * @param sq: serviced query.
429 * @param packet: wireformat query to send to destination. copied from.
440 * @param packet: wireformat query to send to destination. copied from.
430 * @param addr: address to send to.
431 * @param addrlen: length of addr.
432 * @param timeout: in seconds from now.
433 * Timer starts running now. Timer may expire if all buffers are used,
434 * without any query been sent to the server yet.
435 * @param callback: function to call on error, timeout or reply.
436 * @param callback_arg: user argument for callback function.
441 * @param timeout: in seconds from now.
442 * Timer starts running now. Timer may expire if all buffers are used,
443 * without any query been sent to the server yet.
444 * @param callback: function to call on error, timeout or reply.
445 * @param callback_arg: user argument for callback function.
437 * @param ssl_upstream: if the tcp connection must use SSL.
438 * @return: false on error for malloc or socket. Else the pending TCP object.
439 */
446 * @return: false on error for malloc or socket. Else the pending TCP object.
447 */
440struct waiting_tcp* pending_tcp_query(struct outside_network* outnet,
441 struct sldns_buffer* packet, struct sockaddr_storage* addr,
442 socklen_t addrlen, int timeout, comm_point_callback_t* callback,
443 void* callback_arg, int ssl_upstream);
448struct waiting_tcp* pending_tcp_query(struct serviced_query* sq,
449 struct sldns_buffer* packet, int timeout, comm_point_callback_t* callback,
450 void* callback_arg);
444
445/**
446 * Delete pending answer.
447 * @param outnet: outside network the pending query is part of.
448 * Internal feature: if outnet is NULL, p is not unlinked from rbtree.
449 * @param p: deleted
450 */
451void pending_delete(struct outside_network* outnet, struct pending* p);
452
453/**
454 * Perform a serviced query to the authoritative servers.
455 * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
456 * @param outnet: outside network, with rbtree of serviced queries.
457 * @param qname: what qname to query.
458 * @param qnamelen: length of qname in octets including 0 root label.
459 * @param qtype: rrset type to query (host format)
460 * @param qclass: query class. (host format)
461 * @param flags: flags u16 (host format), includes opcode, CD bit.
462 * @param dnssec: if set, DO bit is set in EDNS queries.
463 * If the value includes BIT_CD, CD bit is set when in EDNS queries.
464 * If the value includes BIT_DO, DO bit is set when in EDNS queries.
465 * @param want_dnssec: signatures are needed, without EDNS the answer is
466 * likely to be useless.
451
452/**
453 * Delete pending answer.
454 * @param outnet: outside network the pending query is part of.
455 * Internal feature: if outnet is NULL, p is not unlinked from rbtree.
456 * @param p: deleted
457 */
458void pending_delete(struct outside_network* outnet, struct pending* p);
459
460/**
461 * Perform a serviced query to the authoritative servers.
462 * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
463 * @param outnet: outside network, with rbtree of serviced queries.
464 * @param qname: what qname to query.
465 * @param qnamelen: length of qname in octets including 0 root label.
466 * @param qtype: rrset type to query (host format)
467 * @param qclass: query class. (host format)
468 * @param flags: flags u16 (host format), includes opcode, CD bit.
469 * @param dnssec: if set, DO bit is set in EDNS queries.
470 * If the value includes BIT_CD, CD bit is set when in EDNS queries.
471 * If the value includes BIT_DO, DO bit is set when in EDNS queries.
472 * @param want_dnssec: signatures are needed, without EDNS the answer is
473 * likely to be useless.
474 * @param nocaps: ignore use_caps_for_id and use unperturbed qname.
467 * @param tcp_upstream: use TCP for upstream queries.
468 * @param ssl_upstream: use SSL for upstream queries.
469 * @param callback: callback function.
470 * @param callback_arg: user argument to callback function.
471 * @param addr: to which server to send the query.
472 * @param addrlen: length of addr.
473 * @param zone: name of the zone of the delegation point. wireformat dname.
474 This is the delegation point name for which the server is deemed
475 authoritative.
476 * @param zonelen: length of zone.
477 * @param buff: scratch buffer to create query contents in. Empty on exit.
478 * @return 0 on error, or pointer to serviced query that is used to answer
479 * this serviced query may be shared with other callbacks as well.
480 */
481struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
482 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
475 * @param tcp_upstream: use TCP for upstream queries.
476 * @param ssl_upstream: use SSL for upstream queries.
477 * @param callback: callback function.
478 * @param callback_arg: user argument to callback function.
479 * @param addr: to which server to send the query.
480 * @param addrlen: length of addr.
481 * @param zone: name of the zone of the delegation point. wireformat dname.
482 This is the delegation point name for which the server is deemed
483 authoritative.
484 * @param zonelen: length of zone.
485 * @param buff: scratch buffer to create query contents in. Empty on exit.
486 * @return 0 on error, or pointer to serviced query that is used to answer
487 * this serviced query may be shared with other callbacks as well.
488 */
489struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
490 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
483 uint16_t flags, int dnssec, int want_dnssec, int tcp_upstream,
484 int ssl_upstream, struct sockaddr_storage* addr, socklen_t addrlen,
485 uint8_t* zone, size_t zonelen, comm_point_callback_t* callback,
486 void* callback_arg, struct sldns_buffer* buff);
491 uint16_t flags, int dnssec, int want_dnssec, int nocaps,
492 int tcp_upstream, int ssl_upstream, struct sockaddr_storage* addr,
493 socklen_t addrlen, uint8_t* zone, size_t zonelen,
494 comm_point_callback_t* callback, void* callback_arg,
495 struct sldns_buffer* buff);
487
488/**
489 * Remove service query callback.
490 * If that leads to zero callbacks, the query is completely cancelled.
491 * @param sq: serviced query to adjust.
492 * @param cb_arg: callback argument of callback that needs removal.
493 * same as the callback_arg to outnet_serviced_query().
494 */
495void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
496
497/**
498 * Get memory size in use by outside network.
499 * Counts buffers and outstanding query (serviced queries) malloced data.
500 * @param outnet: outside network structure.
501 * @return size in bytes.
502 */
503size_t outnet_get_mem(struct outside_network* outnet);
504
505/**
506 * Get memory size in use by serviced query while it is servicing callbacks.
507 * This takes into account the pre-deleted status of it; it will be deleted
508 * when the callbacks are done.
509 * @param sq: serviced query.
510 * @return size in bytes.
511 */
512size_t serviced_get_mem(struct serviced_query* sq);
513
514/** callback for incoming udp answers from the network */
515int outnet_udp_cb(struct comm_point* c, void* arg, int error,
516 struct comm_reply *reply_info);
517
518/** callback for pending tcp connections */
519int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
520 struct comm_reply *reply_info);
521
522/** callback for udp timeout */
523void pending_udp_timer_cb(void *arg);
524
525/** callback for udp delay for timeout */
526void pending_udp_timer_delay_cb(void *arg);
527
528/** callback for outgoing TCP timer event */
529void outnet_tcptimer(void* arg);
530
531/** callback for serviced query UDP answers */
532int serviced_udp_callback(struct comm_point* c, void* arg, int error,
533 struct comm_reply* rep);
534
535/** TCP reply or error callback for serviced queries */
536int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
537 struct comm_reply* rep);
538
539/** compare function of pending rbtree */
540int pending_cmp(const void* key1, const void* key2);
541
542/** compare function of serviced query rbtree */
543int serviced_cmp(const void* key1, const void* key2);
544
545#endif /* OUTSIDE_NETWORK_H */
496
497/**
498 * Remove service query callback.
499 * If that leads to zero callbacks, the query is completely cancelled.
500 * @param sq: serviced query to adjust.
501 * @param cb_arg: callback argument of callback that needs removal.
502 * same as the callback_arg to outnet_serviced_query().
503 */
504void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
505
506/**
507 * Get memory size in use by outside network.
508 * Counts buffers and outstanding query (serviced queries) malloced data.
509 * @param outnet: outside network structure.
510 * @return size in bytes.
511 */
512size_t outnet_get_mem(struct outside_network* outnet);
513
514/**
515 * Get memory size in use by serviced query while it is servicing callbacks.
516 * This takes into account the pre-deleted status of it; it will be deleted
517 * when the callbacks are done.
518 * @param sq: serviced query.
519 * @return size in bytes.
520 */
521size_t serviced_get_mem(struct serviced_query* sq);
522
523/** callback for incoming udp answers from the network */
524int outnet_udp_cb(struct comm_point* c, void* arg, int error,
525 struct comm_reply *reply_info);
526
527/** callback for pending tcp connections */
528int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
529 struct comm_reply *reply_info);
530
531/** callback for udp timeout */
532void pending_udp_timer_cb(void *arg);
533
534/** callback for udp delay for timeout */
535void pending_udp_timer_delay_cb(void *arg);
536
537/** callback for outgoing TCP timer event */
538void outnet_tcptimer(void* arg);
539
540/** callback for serviced query UDP answers */
541int serviced_udp_callback(struct comm_point* c, void* arg, int error,
542 struct comm_reply* rep);
543
544/** TCP reply or error callback for serviced queries */
545int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
546 struct comm_reply* rep);
547
548/** compare function of pending rbtree */
549int pending_cmp(const void* key1, const void* key2);
550
551/** compare function of serviced query rbtree */
552int serviced_cmp(const void* key1, const void* key2);
553
554#endif /* OUTSIDE_NETWORK_H */