mirror of
https://github.com/NLnetLabs/unbound.git
synced 2025-12-20 23:00:56 -05:00
cleaner allocation code.
git-svn-id: file:///svn/unbound/trunk@355 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
parent
28f9864b53
commit
ef6480b696
4 changed files with 42 additions and 23 deletions
|
|
@ -45,11 +45,13 @@
|
||||||
#include "iterator/iter_hints.h"
|
#include "iterator/iter_hints.h"
|
||||||
#include "iterator/iter_delegpt.h"
|
#include "iterator/iter_delegpt.h"
|
||||||
#include "services/cache/infra.h"
|
#include "services/cache/infra.h"
|
||||||
|
#include "services/cache/dns.h"
|
||||||
#include "util/net_help.h"
|
#include "util/net_help.h"
|
||||||
#include "util/module.h"
|
#include "util/module.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/config_file.h"
|
#include "util/config_file.h"
|
||||||
#include "util/region-allocator.h"
|
#include "util/region-allocator.h"
|
||||||
|
#include "util/data/msgparse.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
|
iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
|
||||||
|
|
@ -151,3 +153,14 @@ struct delegpt_addr* iter_server_selection(struct iter_env* iter_env,
|
||||||
}
|
}
|
||||||
return got;
|
return got;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct dns_msg*
|
||||||
|
dns_alloc_msg(struct msg_parse* msg, struct region* region)
|
||||||
|
{
|
||||||
|
struct dns_msg* m = (struct dns_msg*)region_alloc(region,
|
||||||
|
sizeof(struct dns_msg));
|
||||||
|
if(!m)
|
||||||
|
return NULL;
|
||||||
|
memset(m, 0, sizeof(*m));
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,8 @@ struct config_file;
|
||||||
struct module_env;
|
struct module_env;
|
||||||
struct delegpt_addr;
|
struct delegpt_addr;
|
||||||
struct delegpt;
|
struct delegpt;
|
||||||
|
struct region;
|
||||||
|
struct msg_parse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process config options and set iterator module state.
|
* Process config options and set iterator module state.
|
||||||
|
|
@ -74,4 +76,12 @@ struct delegpt_addr* iter_server_selection(struct iter_env* iter_env,
|
||||||
struct module_env* env, struct delegpt* dp, uint8_t* name,
|
struct module_env* env, struct delegpt* dp, uint8_t* name,
|
||||||
size_t namelen);
|
size_t namelen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate dns_msg from parsed msg, in region.
|
||||||
|
* @param msg: parsed message (cleaned and ready for region allocation).
|
||||||
|
* @param region: region to use for allocation.
|
||||||
|
* @return newly allocated dns_msg, or NULL on memory error.
|
||||||
|
*/
|
||||||
|
struct dns_msg* dns_alloc_msg(struct msg_parse* msg, struct region* region);
|
||||||
|
|
||||||
#endif /* ITERATOR_ITER_UTILS_H */
|
#endif /* ITERATOR_ITER_UTILS_H */
|
||||||
|
|
|
||||||
|
|
@ -1223,9 +1223,7 @@ process_response(struct module_qstate* qstate, struct iter_qstate* iq,
|
||||||
goto handle_it;
|
goto handle_it;
|
||||||
|
|
||||||
/* allocate response dns_msg in region */
|
/* allocate response dns_msg in region */
|
||||||
/* TODO:
|
iq->response = dns_alloc_msg(prs, qstate->region);
|
||||||
iq->response = dns_parse_to_msg(prs, qstate->region);
|
|
||||||
*/
|
|
||||||
if(!iq->response)
|
if(!iq->response)
|
||||||
goto handle_it;
|
goto handle_it;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,7 @@ parse_rr_copy(ldns_buffer* pkt, struct rrset_parse* pset,
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** create rrset return 0 or rcode */
|
/** create rrset return 0 on failure */
|
||||||
static int
|
static int
|
||||||
parse_create_rrset(ldns_buffer* pkt, struct rrset_parse* pset,
|
parse_create_rrset(ldns_buffer* pkt, struct rrset_parse* pset,
|
||||||
struct packed_rrset_data** data)
|
struct packed_rrset_data** data)
|
||||||
|
|
@ -237,11 +237,11 @@ parse_create_rrset(ldns_buffer* pkt, struct rrset_parse* pset,
|
||||||
(sizeof(size_t)+sizeof(uint8_t*)+sizeof(uint32_t)) +
|
(sizeof(size_t)+sizeof(uint8_t*)+sizeof(uint32_t)) +
|
||||||
pset->size);
|
pset->size);
|
||||||
if(!*data)
|
if(!*data)
|
||||||
return LDNS_RCODE_SERVFAIL;
|
return 0;
|
||||||
/* copy & decompress */
|
/* copy & decompress */
|
||||||
if(!parse_rr_copy(pkt, pset, *data))
|
if(!parse_rr_copy(pkt, pset, *data))
|
||||||
return LDNS_RCODE_SERVFAIL;
|
return 0;
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** get trust value for rrset */
|
/** get trust value for rrset */
|
||||||
|
|
@ -274,13 +274,12 @@ get_rrset_trust(struct reply_info* rep, size_t i)
|
||||||
* @param pkt: the packet for compression pointer resolution.
|
* @param pkt: the packet for compression pointer resolution.
|
||||||
* @param msg: the parsed message
|
* @param msg: the parsed message
|
||||||
* @param rep: reply info to put rrs into.
|
* @param rep: reply info to put rrs into.
|
||||||
* @return 0 or rcode.
|
* @return 0 on failure.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
parse_copy_decompress(ldns_buffer* pkt, struct msg_parse* msg,
|
parse_copy_decompress(ldns_buffer* pkt, struct msg_parse* msg,
|
||||||
struct reply_info* rep)
|
struct reply_info* rep)
|
||||||
{
|
{
|
||||||
int ret;
|
|
||||||
size_t i;
|
size_t i;
|
||||||
struct rrset_parse *pset = msg->rrset_first;
|
struct rrset_parse *pset = msg->rrset_first;
|
||||||
struct packed_rrset_data* data;
|
struct packed_rrset_data* data;
|
||||||
|
|
@ -294,15 +293,15 @@ parse_copy_decompress(ldns_buffer* pkt, struct msg_parse* msg,
|
||||||
rep->rrsets[i]->rk.dname_len = pset->dname_len;
|
rep->rrsets[i]->rk.dname_len = pset->dname_len;
|
||||||
rep->rrsets[i]->rk.dname = (uint8_t*)malloc(pset->dname_len);
|
rep->rrsets[i]->rk.dname = (uint8_t*)malloc(pset->dname_len);
|
||||||
if(!rep->rrsets[i]->rk.dname)
|
if(!rep->rrsets[i]->rk.dname)
|
||||||
return LDNS_RCODE_SERVFAIL;
|
return 0;
|
||||||
/** copy & decompress dname */
|
/** copy & decompress dname */
|
||||||
dname_pkt_copy(pkt, rep->rrsets[i]->rk.dname, pset->dname);
|
dname_pkt_copy(pkt, rep->rrsets[i]->rk.dname, pset->dname);
|
||||||
/** copy over type and class */
|
/** copy over type and class */
|
||||||
rep->rrsets[i]->rk.type = htons(pset->type);
|
rep->rrsets[i]->rk.type = htons(pset->type);
|
||||||
rep->rrsets[i]->rk.rrset_class = pset->rrset_class;
|
rep->rrsets[i]->rk.rrset_class = pset->rrset_class;
|
||||||
/** read data part. */
|
/** read data part. */
|
||||||
if((ret=parse_create_rrset(pkt, pset, &data)) != 0)
|
if(!parse_create_rrset(pkt, pset, &data))
|
||||||
return ret;
|
return 0;
|
||||||
rep->rrsets[i]->entry.data = (void*)data;
|
rep->rrsets[i]->entry.data = (void*)data;
|
||||||
rep->rrsets[i]->entry.key = (void*)rep->rrsets[i];
|
rep->rrsets[i]->entry.key = (void*)rep->rrsets[i];
|
||||||
rep->rrsets[i]->entry.hash = pset->hash;
|
rep->rrsets[i]->entry.hash = pset->hash;
|
||||||
|
|
@ -312,26 +311,25 @@ parse_copy_decompress(ldns_buffer* pkt, struct msg_parse* msg,
|
||||||
|
|
||||||
pset = pset->rrset_all_next;
|
pset = pset->rrset_all_next;
|
||||||
}
|
}
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** allocate and decompress message and rrsets, returns 0 or rcode. */
|
/** allocate and decompress message and rrsets, returns 0 if failed. */
|
||||||
static int
|
static int
|
||||||
parse_create_msg(ldns_buffer* pkt, struct msg_parse* msg,
|
parse_create_msg(ldns_buffer* pkt, struct msg_parse* msg,
|
||||||
struct alloc_cache* alloc, struct query_info* qinf,
|
struct alloc_cache* alloc, struct query_info* qinf,
|
||||||
struct reply_info** rep)
|
struct reply_info** rep)
|
||||||
{
|
{
|
||||||
int ret;
|
|
||||||
log_assert(pkt && msg);
|
log_assert(pkt && msg);
|
||||||
if(!parse_create_qinfo(pkt, msg, qinf))
|
if(!parse_create_qinfo(pkt, msg, qinf))
|
||||||
return LDNS_RCODE_SERVFAIL;
|
return 0;
|
||||||
if(!parse_create_repinfo(msg, rep))
|
if(!parse_create_repinfo(msg, rep))
|
||||||
return LDNS_RCODE_SERVFAIL;
|
return 0;
|
||||||
if(!parse_alloc_rrset_keys(msg, *rep, alloc))
|
if(!parse_alloc_rrset_keys(msg, *rep, alloc))
|
||||||
return LDNS_RCODE_SERVFAIL;
|
return 0;
|
||||||
if((ret=parse_copy_decompress(pkt, msg, *rep)) != 0)
|
if(!parse_copy_decompress(pkt, msg, *rep))
|
||||||
return ret;
|
return 0;
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
|
int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
|
||||||
|
|
@ -358,11 +356,11 @@ int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
|
||||||
|
|
||||||
/* parse OK, allocate return structures */
|
/* parse OK, allocate return structures */
|
||||||
/* this also performs dname decompression */
|
/* this also performs dname decompression */
|
||||||
if((ret = parse_create_msg(pkt, msg, alloc, qinf, rep)) != 0) {
|
if(!parse_create_msg(pkt, msg, alloc, qinf, rep)) {
|
||||||
query_info_clear(qinf);
|
query_info_clear(qinf);
|
||||||
reply_info_parsedelete(*rep, alloc);
|
reply_info_parsedelete(*rep, alloc);
|
||||||
*rep = NULL;
|
*rep = NULL;
|
||||||
return ret;
|
return LDNS_RCODE_SERVFAIL;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue