1266077Sdes/*
2266077Sdes * unbound-event.h - unbound validating resolver public API with events
3266077Sdes *
4266077Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5266077Sdes *
6266077Sdes * This software is open source.
7266077Sdes *
8266077Sdes * Redistribution and use in source and binary forms, with or without
9266077Sdes * modification, are permitted provided that the following conditions
10266077Sdes * are met:
11266077Sdes *
12266077Sdes * Redistributions of source code must retain the above copyright notice,
13266077Sdes * this list of conditions and the following disclaimer.
14266077Sdes *
15266077Sdes * Redistributions in binary form must reproduce the above copyright notice,
16266077Sdes * this list of conditions and the following disclaimer in the documentation
17266077Sdes * and/or other materials provided with the distribution.
18266077Sdes *
19266077Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20266077Sdes * be used to endorse or promote products derived from this software without
21266077Sdes * specific prior written permission.
22266077Sdes *
23266077Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24266077Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25266077Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26266077Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27266077Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28266077Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29266077Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30266077Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31266077Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32266077Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33266077Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34266077Sdes */
35266077Sdes
36266077Sdes/**
37266077Sdes * \file
38266077Sdes *
39266077Sdes * This file contains the unbound interface for use with libevent.
40266077Sdes * You have to use the same libevent that unbound was compiled with,
41266077Sdes * otherwise it wouldn't work, the event and event_base structures would
42266077Sdes * be different.  If unbound is compiled without libevent support then
43266077Sdes * this header file is not supposed to be installed on the system.
44266077Sdes *
45266077Sdes * Use ub_ctx_create_event_base() to create an unbound context that uses
46266077Sdes * the event base that you have made.  Then, use the ub_resolve_event call
47266077Sdes * to add DNS resolve queries to the context.  Those then run when you
48266077Sdes * call event_dispatch() on your event_base, and when they are done you
49266077Sdes * get a function callback.
50266077Sdes *
51266077Sdes * This method does not fork another process or create a thread, the effort
52266077Sdes * is done by the unbound state machines that are connected to the event_base.
53266077Sdes */
54266077Sdes#ifndef _UB_UNBOUND_EVENT_H
55266077Sdes#define _UB_UNBOUND_EVENT_H
56266077Sdes
57266077Sdes#ifdef __cplusplus
58266077Sdesextern "C" {
59266077Sdes#endif
60266077Sdes
61266077Sdesstruct ub_ctx;
62266077Sdesstruct ub_result;
63266077Sdesstruct event_base;
64266077Sdes
65266077Sdestypedef void (*ub_event_callback_t)(void*, int, void*, int, int, char*);
66266077Sdes
67266077Sdes/**
68266077Sdes * Create a resolving and validation context.
69266077Sdes * The information from /etc/resolv.conf and /etc/hosts is not utilised by
70266077Sdes * default. Use ub_ctx_resolvconf and ub_ctx_hosts to read them.
71266077Sdes * @param base: the event base that the caller has created.  The unbound
72266077Sdes *	context uses this event base.
73266077Sdes * @return a new context. default initialisation.
74266077Sdes * 	returns NULL on error.
75266077Sdes * You must use ub_resolve_event with this context.
76266077Sdes * Do not call ub_ctx_async, ub_poll, ub_wait, ub_process, this is all done
77266077Sdes * with the event_base.  Setup the options you like with the other functions.
78266077Sdes */
79266077Sdesstruct ub_ctx* ub_ctx_create_event(struct event_base* base);
80266077Sdes
81266077Sdes/**
82266077Sdes * Set a new event_base on a context created with ub_ctx_create_event.
83266077Sdes * Any outbound queries will be canceled.
84266077Sdes * @param ctx the ub_ctx to update.  Must have been created with ub_ctx_create_event
85266077Sdes * @param base the new event_base to attach to the ctx
86266077Sdes * @return 0 if OK, else error
87266077Sdes */
88266077Sdesint ub_ctx_set_event(struct ub_ctx* ctx, struct event_base* base);
89266077Sdes
90266077Sdes/**
91266077Sdes * Perform resolution and validation of the target name.
92266077Sdes * Asynchronous, after a while, the callback will be called with your
93266077Sdes * data and the result.  Uses the event_base user installed by creating the
94266077Sdes * context with ub_ctx_create_event().
95266077Sdes * @param ctx: context with event_base in it.
96266077Sdes *	The context is finalized, and can no longer accept all config changes.
97266077Sdes * @param name: domain name in text format (a string).
98266077Sdes * @param rrtype: type of RR in host order, 1 is A.
99266077Sdes * @param rrclass: class of RR in host order, 1 is IN (for internet).
100266077Sdes * @param mydata: this data is your own data (you can pass NULL),
101266077Sdes * 	and is passed on to the callback function.
102266077Sdes * @param callback: this is called on completion of the resolution.
103266077Sdes * 	It is called as:
104266077Sdes * 	void callback(void* mydata, int rcode, void* packet, int packet_len,
105266077Sdes * 		int sec, char* why_bogus)
106266077Sdes * 	with mydata: the same as passed here, you may pass NULL,
107266077Sdes * 	with rcode: 0 on no error, nonzero for mostly SERVFAIL situations,
108266077Sdes *		this is a DNS rcode.
109266077Sdes *	with packet: a buffer with DNS wireformat packet with the answer.
110266077Sdes *		do not inspect if rcode != 0.
111266077Sdes *		do not write or free the packet buffer, it is used internally
112266077Sdes *		in unbound (for other callbacks that want the same data).
113266077Sdes *	with packet_len: length in bytes of the packet buffer.
114266077Sdes *	with sec: 0 if insecure, 1 if bogus, 2 if DNSSEC secure.
115266077Sdes *	with why_bogus: text string explaining why it is bogus (or NULL).
116266077Sdes *	These point to buffers inside unbound; do not deallocate the packet or
117266077Sdes *	error string.
118266077Sdes *
119266077Sdes * 	If an error happens during processing, your callback will be called
120266077Sdes * 	with error set to a nonzero value (and result==NULL).
121266077Sdes * 	For localdata (etc/hosts) the callback is called immediately, before
122266077Sdes * 	resolve_event returns, async_id=0 is returned.
123266077Sdes * @param async_id: if you pass a non-NULL value, an identifier number is
124266077Sdes *	returned for the query as it is in progress. It can be used to
125266077Sdes *	cancel the query.
126266077Sdes * @return 0 if OK, else error.
127266077Sdes */
128266077Sdesint ub_resolve_event(struct ub_ctx* ctx, const char* name, int rrtype,
129266077Sdes	int rrclass, void* mydata, ub_event_callback_t callback, int* async_id);
130266077Sdes
131266077Sdes#ifdef __cplusplus
132266077Sdes}
133266077Sdes#endif
134266077Sdes
135266077Sdes#endif /* _UB_UNBOUND_H */
136