Defines | Functions
context.c File Reference

This file contains the validator context structure. More...

#include "config.h"
#include "libunbound/context.h"
#include "util/module.h"
#include "util/config_file.h"
#include "util/net_help.h"
#include "services/modstack.h"
#include "services/localzone.h"
#include "services/cache/rrset.h"
#include "services/cache/infra.h"
#include "util/data/msgreply.h"
#include "util/storage/slabhash.h"

Defines

#define NUM_ID_TRIES   100000
 How many times to try to find an unused query-id-number for async.

Functions

int context_finalize (struct ub_ctx *ctx)
 finalize a context.
int context_query_cmp (const void *a, const void *b)
 compare two ctx_query elements
void context_query_delete (struct ctx_query *q)
 delete context query
static int find_id (struct ub_ctx *ctx, int *id)
 find next useful id number of 0 on error
struct ctx_querycontext_new (struct ub_ctx *ctx, char *name, int rrtype, int rrclass, ub_callback_t cb, void *cbarg)
 Create new query in context, add to querynum list.
struct alloc_cachecontext_obtain_alloc (struct ub_ctx *ctx, int locking)
 Get a new alloc.
void context_release_alloc (struct ub_ctx *ctx, struct alloc_cache *alloc, int locking)
 Release an alloc.
uint8_t * context_serialize_new_query (struct ctx_query *q, uint32_t *len)
 Serialize a context query that questions data.
struct ctx_querycontext_deserialize_new_query (struct ub_ctx *ctx, uint8_t *p, uint32_t len)
 Deserialize a new_query buffer.
struct ctx_querycontext_lookup_new_query (struct ub_ctx *ctx, uint8_t *p, uint32_t len)
 Lookup query from new_query buffer.
uint8_t * context_serialize_answer (struct ctx_query *q, int err, ldns_buffer *pkt, uint32_t *len)
 Serialize a context_query result to hand back to user.
struct ctx_querycontext_deserialize_answer (struct ub_ctx *ctx, uint8_t *p, uint32_t len, int *err)
 Deserialize an answer buffer.
uint8_t * context_serialize_cancel (struct ctx_query *q, uint32_t *len)
 Serialize a query cancellation.
struct ctx_querycontext_deserialize_cancel (struct ub_ctx *ctx, uint8_t *p, uint32_t len)
 Deserialize a cancel buffer.
uint8_t * context_serialize_quit (uint32_t *len)
 Serialize a 'quit' command.
enum ub_ctx_cmd context_serial_getcmd (uint8_t *p, uint32_t len)
 Obtain command code from serialized buffer.

Detailed Description

This file contains the validator context structure.


Function Documentation

int context_finalize ( struct ub_ctx ctx)
void context_query_delete ( struct ctx_query q)

delete context query

Parameters:
q,:query to delete, including message packet and prealloc result

References ub_resolve_free(), ctx_query::res, and ctx_query::msg.

Referenced by delq(), process_answer_detail(), ub_resolve(), ub_resolve_async(), ub_cancel(), add_bg_result(), and libworker_bg_done_cb().

struct ctx_query* context_new ( struct ub_ctx ctx,
char *  name,
int  rrtype,
int  rrclass,
ub_callback_t  cb,
void *  cbarg 
) [read]

Create new query in context, add to querynum list.

Parameters:
ctx,:context
name,:query name
rrtype,:type
rrclass,:class
cb,:callback for async, or NULL for sync.
cbarg,:user arg for async queries.
Returns:
new ctx_query or NULL for malloc failure.

References ub_ctx::cfglock, find_id(), ctx_query::querynum, ctx_query::node, rbnode_t::key, ctx_query::async, ctx_query::cb, ctx_query::cb_arg, ctx_query::res, ub_result::qname, ub_result::qtype, ub_result::qclass, ub_ctx::num_async, rbtree_insert(), and ub_ctx::queries.

Referenced by ub_resolve(), and ub_resolve_async().

struct alloc_cache* context_obtain_alloc ( struct ub_ctx ctx,
int  locking 
) [read]

Get a new alloc.

Creates a new one or uses a cached one.

Parameters:
ctx,:context
locking,:if true, cfglock is locked while getting alloc.
Returns:
an alloc, or NULL on mem error.

References ub_ctx::cfglock, ub_ctx::alloc_list, alloc_cache::super, ub_ctx::thr_next_num, ub_ctx::superalloc, and alloc_init().

Referenced by libworker_setup().

void context_release_alloc ( struct ub_ctx ctx,
struct alloc_cache alloc,
int  locking 
)

Release an alloc.

Puts it into the cache.

Parameters:
ctx,:context
locking,:if true, cfglock is locked while releasing alloc.
alloc,:alloc to relinquish.

References ub_ctx::cfglock, alloc_cache::super, and ub_ctx::alloc_list.

Referenced by libworker_delete().

uint8_t* context_serialize_new_query ( struct ctx_query q,
uint32_t *  len 
)

Serialize a context query that questions data.

This serializes the query name, type, ... As well as command code 'new_query'.

Parameters:
q,:context query
len,:the length of the allocation is returned.
Returns:
: an alloc, or NULL on mem error.

References ctx_query::res, ub_result::qname, UB_LIBCMD_NEWQUERY, ctx_query::querynum, ub_result::qtype, and ub_result::qclass.

Referenced by ub_resolve_async().

struct ctx_query* context_deserialize_new_query ( struct ub_ctx ctx,
uint8_t *  p,
uint32_t  len 
) [read]

Deserialize a new_query buffer.

Parameters:
ctx,:context
p,:buffer serialized.
len,:length of buffer.
Returns:
new ctx_query or NULL for malloc failure.

add to query list

References log_assert, UB_LIBCMD_NEWQUERY, ctx_query::querynum, ctx_query::node, rbnode_t::key, ctx_query::async, ctx_query::res, ub_result::qtype, ub_result::qclass, ub_result::qname, ub_ctx::num_async, rbtree_insert(), and ub_ctx::queries.

Referenced by handle_newq().

struct ctx_query* context_lookup_new_query ( struct ub_ctx ctx,
uint8_t *  p,
uint32_t  len 
) [read]

Lookup query from new_query buffer.

Parameters:
ctx,:context
p,:buffer serialized.
len,:length of buffer.
Returns:
looked up ctx_query or NULL for malloc failure.

References ctx_query::querynum, log_assert, UB_LIBCMD_NEWQUERY, rbtree_search(), ub_ctx::queries, and ctx_query::async.

Referenced by handle_newq().

uint8_t* context_serialize_answer ( struct ctx_query q,
int  err,
ldns_buffer *  pkt,
uint32_t *  len 
)

Serialize a context_query result to hand back to user.

This serializes the query name, type, ..., and result. As well as command code 'answer'.

Parameters:
q,:context query
err,:error code to pass to client.
pkt,:the packet to add, can be NULL.
len,:the length of the allocation is returned.
Returns:
: an alloc, or NULL on mem error.

References ctx_query::res, ub_result::why_bogus, UB_LIBCMD_ANSWER, ctx_query::querynum, and ctx_query::msg_security.

Referenced by add_bg_result().

struct ctx_query* context_deserialize_answer ( struct ub_ctx ctx,
uint8_t *  p,
uint32_t  len,
int *  err 
) [read]

Deserialize an answer buffer.

Parameters:
ctx,:context
p,:buffer serialized.
len,:length of buffer.
err,:error code to be returned to client is passed.
Returns:
ctx_query with answer added or NULL for malloc failure.

References log_assert, UB_LIBCMD_ANSWER, rbtree_search(), ub_ctx::queries, ctx_query::msg_security, ctx_query::res, ub_result::why_bogus, memdup(), ctx_query::msg_len, UB_NOMEM, and ctx_query::msg.

Referenced by process_answer_detail().

uint8_t* context_serialize_cancel ( struct ctx_query q,
uint32_t *  len 
)

Serialize a query cancellation.

Serializes query async id as well as command code 'cancel'

Parameters:
q,:context query
len,:the length of the allocation is returned.
Returns:
: an alloc, or NULL on mem error.

References UB_LIBCMD_CANCEL, and ctx_query::querynum.

Referenced by ub_cancel().

struct ctx_query* context_deserialize_cancel ( struct ub_ctx ctx,
uint8_t *  p,
uint32_t  len 
) [read]

Deserialize a cancel buffer.

Parameters:
ctx,:context
p,:buffer serialized.
len,:length of buffer.
Returns:
ctx_query to cancel or NULL for failure.

References log_assert, UB_LIBCMD_CANCEL, rbtree_search(), and ub_ctx::queries.

Referenced by handle_cancel().

uint8_t* context_serialize_quit ( uint32_t *  len)

Serialize a 'quit' command.

Parameters:
len,:the length of the allocation is returned.
Returns:
: an alloc, or NULL on mem error.

References UB_LIBCMD_QUIT.

enum ub_ctx_cmd context_serial_getcmd ( uint8_t *  p,
uint32_t  len 
)

Obtain command code from serialized buffer.

Parameters:
p,:buffer serialized.
len,:length of buffer.
Returns:
command code or QUIT on error.

References UB_LIBCMD_QUIT.

Referenced by ub_ctx_delete(), process_answer_detail(), and libworker_do_cmd().