From ced720ad9947fb5c9a0b93cfe4e9985f2feda99d Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Thu, 10 May 2007 11:53:55 +0000 Subject: [PATCH] Fix for compression. Module interface setup. git-svn-id: file:///svn/unbound/trunk@306 be551aaa-1e26-0410-a405-d3ace91eadb9 --- doc/Changelog | 2 + util/data/msgreply.c | 22 ++++- util/module.c | 41 ++++++++++ util/module.h | 186 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 util/module.c create mode 100644 util/module.h diff --git a/doc/Changelog b/doc/Changelog index 40fd6e9f7..7ffad134f 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,5 +1,7 @@ 10 May 2007: Wouter - created release-0.3 svn tag. + - util/module.h + - fixed compression - no longer compresses root name. 9 May 2007: Wouter - outside network cleans up waiting tcp queries on exit. diff --git a/util/data/msgreply.c b/util/data/msgreply.c index a93f4518d..cdb5d1db2 100644 --- a/util/data/msgreply.c +++ b/util/data/msgreply.c @@ -722,6 +722,14 @@ write_compressed_dname(ldns_buffer* pkt, uint8_t* dname, int labs, uint8_t lablen; uint16_t ptr; + if(labs == 1) { + /* write root label */ + if(ldns_buffer_remaining(pkt) < 1) + return 0; + ldns_buffer_write_u8(pkt, 0); + return 1; + } + /* copy the first couple of labels */ while(labcopy--) { lablen = *dname++; @@ -776,9 +784,15 @@ compress_owner(struct ub_packed_rrset_key* key, ldns_buffer* pkt, return RETVAL_OUTMEM; } else { /* always compress 2nd-further RRs in RRset */ - if(ldns_buffer_remaining(pkt) < 2+4+4+2) - return RETVAL_TRUNC; - ldns_buffer_write(pkt, owner_ptr, 2); + if(owner_labs == 1) { + if(ldns_buffer_remaining(pkt) < 1+4+4+2) + return RETVAL_TRUNC; + ldns_buffer_write_u8(pkt, 0); + } else { + if(ldns_buffer_remaining(pkt) < 2+4+4+2) + return RETVAL_TRUNC; + ldns_buffer_write(pkt, owner_ptr, 2); + } ldns_buffer_write(pkt, &key->rk.dname[key->rk.dname_len], 4); } return RETVAL_OK; @@ -910,7 +924,7 @@ packed_rrset_encode(struct ub_packed_rrset_key* key, ldns_buffer* pkt, if(do_sig) { size_t total = data->count+data->rrsig_count; for(i=data->count; irr_len[i]) return RETVAL_TRUNC; diff --git a/util/module.c b/util/module.c new file mode 100644 index 000000000..f8182beb3 --- /dev/null +++ b/util/module.c @@ -0,0 +1,41 @@ +/* + * util/module.c - module interface + * + * Copyright (c) 2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * 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. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "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 THE REGENTS OR CONTRIBUTORS 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. + */ +/** + * \file + * Implementation of module.h. + */ + +#include "config.h" +#include "util/module.h" diff --git a/util/module.h b/util/module.h new file mode 100644 index 000000000..ae1b36e37 --- /dev/null +++ b/util/module.h @@ -0,0 +1,186 @@ +/* + * util/module.h - DNS handling module interface + * + * Copyright (c) 2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * 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. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "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 THE REGENTS OR CONTRIBUTORS 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. + */ + +/** + * \file + * + * This file contains the interface for DNS handling modules. + */ + +#ifndef UTIL_MODULE_H +#define UTIL_MODULE_H +#include "util/storage/lruhash.h" +struct config_file; +struct slabhash; +struct query_info; +struct edns_data; +struct region; +struct worker; + +/** Maximum number of modules in operation */ +#define MAX_MODULE 2 +/** module id for the iterator */ +#define MODULE_ID_ITERATOR 0 +/** module id for the validator */ +#define MODULE_ID_VALIDATOR 1 + +/** + * Module environment. + * Services and data provided to the module. + */ +struct module_env { + /* --- data --- */ + /** config file with config options */ + struct config_file* cfg; + /** shared message cache */ + struct slabhash* msg_cache; + /** shared rrset cache */ + struct slabhash* rrset_cache; + + /* --- services --- */ + /** send DNS query to server */ + /** create a subquery */ + + /** internal data for daemon - worker thread. */ + struct worker* worker; + /** module specific data. indexed by module id. */ + void* modinfo[MAX_MODULE]; +}; + +/** + * External visible states of the module state machine + * Modules may also have an internal state. + * Modules are supposed to run to completion or until blocked. + */ +enum module_ext_state { + /** initial state - new query */ + module_state_initial = 0, + /** waiting for reply to outgoing network query */ + module_wait_reply, + /** module is waiting for another module */ + module_wait_module, + /** module is waiting for sub-query */ + module_wait_subquery, + /** module could not finish the query */ + module_error, + /** module is finished with query */ + module_finished +}; + +/** + * Events that happen to modules, that start or wakeup modules. + */ +enum module_ev { + /** new query */ + module_event_new = 0, + /** query passed by other module */ + module_event_pass, + /** reply inbound from server */ + module_event_reply, + /** timeout */ + module_event_timeout, + /** other module finished */ + module_event_mod_done, + /** subquery finished */ + module_event_subq_done, + /** error */ + module_event_error +}; + +/** + * Module state, per query. + */ +struct module_qstate { + /** which query is being answered: name, type, class */ + struct query_info* qinfo; + /** hash value of the query qinfo */ + hashvalue_t query_hash; + /** flags uint16 from query */ + uint16_t query_flags; + /** edns data from the query */ + struct edns_data* edns; + + /** buffer, contains server replies, store resulting reply here. + * May be cleared when module blocks. */ + ldns_buffer* buf; + /** parsed message from server */ + struct msg_parse* msg_parse; + /** region for temporary usage. May be cleared when module blocks. */ + struct region* scratch; + + /** module states */ + enum module_ext_state ext_state[MAX_MODULE]; + /** module specific data for query. indexed by module id. */ + void* modinfo[MAX_MODULE]; + /** environment for this query */ + struct module_env* module_env; +}; + +/** + * Module functionality block + */ +struct module_func_block { + /** text string name of module */ + char* name; + /** id number of module */ + int id; + + /** + * init the module + * @param env: module environment. + * return: 0 on error + */ + int (*init)(struct module_env* env); + /** + * de-init, delete, the module. + * @param env: module environment. + */ + void (*deinit)(struct module_env* env); + + /** + * accept a new query, or work further on existing query. + * Changes the qstate->ext_state to be correct on exit. + * @param ev: event that causes the module state machine to + * (re-)activate. + * @param qstate: the query state. + */ + void (*operate)(struct module_qstate* qstate, enum module_ev event); + /** + * clear module specific data + */ + void (*clear)(struct module_qstate* qstate); +}; + +#endif /* UTIL_MODULE_H */ -- 2.47.2