2 * util/module.h - DNS handling module interface
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains the interface for DNS handling modules.
44 #include "util/storage/lruhash.h"
45 #include "util/data/msgreply.h"
46 #include "util/data/msgparse.h"
65 /** Maximum number of modules in operation */
70 * Services and data provided to the module.
74 /** config file with config options */
75 struct config_file* cfg;
76 /** shared message cache */
77 struct slabhash* msg_cache;
78 /** shared rrset cache */
79 struct rrset_cache* rrset_cache;
80 /** shared infrastructure cache (edns, lameness) */
81 struct infra_cache* infra_cache;
82 /** shared key cache */
83 struct key_cache* key_cache;
85 /* --- services --- */
87 * Send serviced DNS query to server. UDP/TCP and EDNS is handled.
88 * operate() should return with wait_reply. Later on a callback
89 * will cause operate() to be called with event timeout or reply.
90 * The time until a timeout is calculated from roundtrip timing,
91 * several UDP retries are attempted.
92 * @param qname: query name. (host order)
93 * @param qnamelen: length in bytes of qname, including trailing 0.
94 * @param qtype: query type. (host order)
95 * @param qclass: query class. (host order)
96 * @param flags: host order flags word, with opcode and CD bit.
97 * @param dnssec: if set, EDNS record will have bits set.
98 * If EDNS_DO bit is set, DO bit is set in EDNS records.
99 * If BIT_CD is set, CD bit is set in queries with EDNS records.
100 * @param want_dnssec: if set, the validator wants DNSSEC. Without
101 * EDNS, the answer is likely to be useless for this domain.
102 * @param addr: where to.
103 * @param addrlen: length of addr.
104 * @param zone: delegation point name.
105 * @param zonelen: length of zone name.
106 * @param q: wich query state to reactivate upon return.
107 * @return: false on failure (memory or socket related). no query was
108 * sent. Or returns an outbound entry with qsent and qstate set.
109 * This outbound_entry will be used on later module invocations
110 * that involve this query (timeout, error or reply).
112 struct outbound_entry* (*send_query)(uint8_t* qname, size_t qnamelen,
113 uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec,
114 int want_dnssec, struct sockaddr_storage* addr,
115 socklen_t addrlen, uint8_t* zone, size_t zonelen,
116 struct module_qstate* q);
120 * Remove all sub-query references from this query state.
121 * Keeps super-references of those sub-queries correct.
122 * Updates stat items in mesh_area structure.
123 * @param qstate: used to find mesh state.
125 void (*detach_subs)(struct module_qstate* qstate);
129 * Creates it if it does not exist already.
130 * Keeps sub and super references correct.
131 * Updates stat items in mesh_area structure.
132 * Pass if it is priming query or not.
134 * o if error (malloc) happened.
135 * o need to initialise the new state (module init; it is a new state).
136 * so that the next run of the query with this module is successful.
137 * o no init needed, attachment successful.
139 * @param qstate: the state to find mesh state, and that wants to
140 * receive the results from the new subquery.
141 * @param qinfo: what to query for (copied).
142 * @param qflags: what flags to use (RD, CD flag or not).
143 * @param prime: if it is a (stub) priming query.
144 * @param newq: If the new subquery needs initialisation, it is
145 * returned, otherwise NULL is returned.
146 * @return: false on error, true if success (and init may be needed).
148 int (*attach_sub)(struct module_qstate* qstate,
149 struct query_info* qinfo, uint16_t qflags, int prime,
150 struct module_qstate** newq);
153 * Kill newly attached sub. If attach_sub returns newq for
154 * initialisation, but that fails, then this routine will cleanup and
155 * delete the fresly created sub.
156 * @param newq: the new subquery that is no longer needed.
159 void (*kill_sub)(struct module_qstate* newq);
162 * Detect if adding a dependency for qstate on name,type,class will
163 * create a dependency cycle.
164 * @param qstate: given mesh querystate.
165 * @param qinfo: query info for dependency.
166 * @param flags: query flags of dependency, RD/CD flags.
167 * @param prime: if dependency is a priming query or not.
168 * @return true if the name,type,class exists and the given
169 * qstate mesh exists as a dependency of that name. Thus
170 * if qstate becomes dependent on name,type,class then a
173 int (*detect_cycle)(struct module_qstate* qstate,
174 struct query_info* qinfo, uint16_t flags, int prime);
176 /** region for temporary usage. May be cleared after operate() call. */
177 struct regional* scratch;
178 /** buffer for temporary usage. May be cleared after operate() call. */
179 ldns_buffer* scratch_buffer;
180 /** internal data for daemon - worker thread. */
181 struct worker* worker;
182 /** mesh area with query state dependencies */
183 struct mesh_area* mesh;
184 /** allocation service */
185 struct alloc_cache* alloc;
186 /** random table to generate random numbers */
187 struct ub_randstate* rnd;
188 /** time in seconds, converted to integer */
190 /** time in microseconds. Relatively recent. */
191 struct timeval* now_tv;
192 /** is validation required for messages, controls client-facing
193 * validation status (AD bits) and servfails */
194 int need_to_validate;
195 /** trusted key storage; these are the configured keys, if not NULL,
196 * otherwise configured by validator. These are the trust anchors,
197 * and are not primed and ready for validation, but on the bright
198 * side, they are read only memory, thus no locks and fast. */
199 struct val_anchors* anchors;
200 /** negative cache, configured by the validator. if not NULL,
201 * contains NSEC record lookup trees. */
202 struct val_neg_cache* neg_cache;
203 /** the 5011-probe timer (if any) */
204 struct comm_timer* probe_timer;
205 /** Mapping of forwarding zones to targets.
206 * iterator forwarder information. per-thread, created by worker */
207 struct iter_forwards* fwds;
209 * iterator forwarder information. per-thread, created by worker.
210 * The hints -- these aren't stored in the cache because they don't
211 * expire. The hints are always used to "prime" the cache. Note
212 * that both root hints and stub zone "hints" are stored in this
215 struct iter_hints* hints;
216 /** module specific data. indexed by module id. */
217 void* modinfo[MAX_MODULE];
221 * External visible states of the module state machine
222 * Modules may also have an internal state.
223 * Modules are supposed to run to completion or until blocked.
225 enum module_ext_state {
226 /** initial state - new query */
227 module_state_initial = 0,
228 /** waiting for reply to outgoing network query */
230 /** module is waiting for another module */
232 /** module is waiting for another module; that other is restarted */
234 /** module is waiting for sub-query */
235 module_wait_subquery,
236 /** module could not finish the query */
238 /** module is finished with query */
243 * Events that happen to modules, that start or wakeup modules.
247 module_event_new = 0,
248 /** query passed by other module */
250 /** reply inbound from server */
252 /** no reply, timeout or other error */
253 module_event_noreply,
254 /** reply is there, but capitalisation check failed */
255 module_event_capsfail,
256 /** next module is done, and its reply is awaiting you */
257 module_event_moddone,
263 * Linked list of sockaddrs
264 * May be allocated such that only 'len' bytes of addr exist for the structure.
268 struct sock_list* next;
269 /** length of addr */
272 struct sockaddr_storage addr;
276 * Module state, per query.
278 struct module_qstate {
279 /** which query is being answered: name, type, class */
280 struct query_info qinfo;
281 /** flags uint16 from query */
282 uint16_t query_flags;
283 /** if this is a (stub or root) priming query (with hints) */
286 /** comm_reply contains server replies */
287 struct comm_reply* reply;
288 /** the reply message, with message for client and calling module */
289 struct dns_msg* return_msg;
290 /** the rcode, in case of error, instead of a reply message */
292 /** origin of the reply (can be NULL from cache, list for cnames) */
293 struct sock_list* reply_origin;
294 /** IP blacklist for queries */
295 struct sock_list* blacklist;
296 /** region for this query. Cleared when query process finishes. */
297 struct regional* region;
298 /** failure reason information if val-log-level is high */
299 struct config_strlist* errinf;
301 /** which module is executing */
304 enum module_ext_state ext_state[MAX_MODULE];
305 /** module specific data for query. indexed by module id. */
306 void* minfo[MAX_MODULE];
307 /** environment for this query */
308 struct module_env* env;
309 /** mesh related information for this query */
310 struct mesh_state* mesh_info;
311 /** how many seconds before expiry is this prefetched (0 if not) */
312 uint32_t prefetch_leeway;
316 * Module functionality block
318 struct module_func_block {
319 /** text string name of module */
323 * init the module. Called once for the global state.
324 * This is the place to apply settings from the config file.
325 * @param env: module environment.
326 * @param id: module id number.
329 int (*init)(struct module_env* env, int id);
332 * de-init, delete, the module. Called once for the global state.
333 * @param env: module environment.
334 * @param id: module id number.
336 void (*deinit)(struct module_env* env, int id);
339 * accept a new query, or work further on existing query.
340 * Changes the qstate->ext_state to be correct on exit.
341 * @param ev: event that causes the module state machine to
343 * @param qstate: the query state.
344 * Note that this method is not allowed to change the
345 * query state 'identity', that is query info, qflags,
346 * and priming status.
347 * Attach a subquery to get results to a different query.
348 * @param id: module id number that operate() is called on.
349 * @param outbound: if not NULL this event is due to the reply/timeout
350 * or error on this outbound query.
351 * @return: if at exit the ext_state is:
352 * o wait_module: next module is started. (with pass event).
353 * o error or finished: previous module is resumed.
354 * o otherwise it waits until that event happens (assumes
355 * the service routine to make subrequest or send message
358 void (*operate)(struct module_qstate* qstate, enum module_ev event,
359 int id, struct outbound_entry* outbound);
362 * inform super querystate about the results from this subquerystate.
363 * Is called when the querystate is finished. The method invoked is
364 * the one from the current module active in the super querystate.
365 * @param qstate: the query state that is finished.
366 * Examine return_rcode and return_reply in the qstate.
367 * @param id: module id for this module.
368 * This coincides with the current module for the super qstate.
369 * @param super: the super querystate that needs to be informed.
371 void (*inform_super)(struct module_qstate* qstate, int id,
372 struct module_qstate* super);
375 * clear module specific data
377 void (*clear)(struct module_qstate* qstate, int id);
380 * How much memory is the module specific data using.
381 * @param env: module environment.
382 * @param id: the module id.
383 * @return the number of bytes that are alloced.
385 size_t (*get_mem)(struct module_env* env, int id);
389 * Debug utility: module external qstate to string
390 * @param s: the state value.
391 * @return descriptive string.
393 const char* strextstate(enum module_ext_state s);
396 * Debug utility: module event to string
397 * @param e: the module event value.
398 * @return descriptive string.
400 const char* strmodulevent(enum module_ev e);
402 #endif /* UTIL_MODULE_H */