rplan.h 4.3 KB
Newer Older
Marek Vavruša's avatar
Marek Vavruša committed
1
/*  Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2

Marek Vavruša's avatar
Marek Vavruša committed
3 4 5 6
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
7

Marek Vavruša's avatar
Marek Vavruša committed
8 9 10 11
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
12

Marek Vavruša's avatar
Marek Vavruša committed
13 14 15
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
16

Marek Vavruša's avatar
Marek Vavruša committed
17
/** \addtogroup rplan
18
 * @{
Marek Vavruša's avatar
Marek Vavruša committed
19 20
 */

21 22
#pragma once

Marek Vavruša's avatar
Marek Vavruša committed
23 24
#include <sys/time.h>

25
#include <libknot/dname.h>
26
#include <libknot/internal/lists.h>
27 28
#include <libknot/internal/namedb/namedb.h>
#include <libknot/internal/sockaddr.h>
29

30
#include "lib/cache.h"
31
#include "lib/zonecut.h"
32

33
/** Query flags */
34
enum kr_query_flag {
35 36
	QUERY_NO_MINIMIZE = 1 << 0, /**< Don't minimize QNAME. */
	QUERY_TCP         = 1 << 1  /**< Use TCP for this query. */
37 38
};

39 40
/**
 * Single query representation.
41
 */
42 43
struct kr_query {
	node_t node;
44
	struct kr_query *parent;
45
	struct kr_zonecut zone_cut;
46
	struct timeval timestamp;
47
	knot_dname_t *sname;
48
	bool resolved;
49 50
	uint16_t stype;
	uint16_t sclass;
51
	uint16_t id;
52 53 54
	uint16_t flags;
};

55 56
/**
 * Query resolution plan structure.
57 58 59 60 61
 *
 * The structure most importantly holds the original query, answer and the
 * list of pending queries required to resolve the original query.
 * It also keeps a notion of current zone cut.
 */
62
struct kr_rplan {
63 64 65 66 67 68
	unsigned txn_flags;          /**< Current transaction flags. */
	namedb_txn_t txn;            /**< Current transaction (may be r/o). */
	list_t pending;              /**< List of pending queries. */
	list_t resolved;             /**< List of resolved queries. */
	struct kr_context *context;  /**< Parent resolution context. */
	mm_ctx_t *pool;              /**< Temporary memory pool. */
69 70
};

71 72 73 74 75
/**
 * Initialize resolution plan (empty).
 * @param rplan plan instance
 * @param context resolution context
 * @param pool ephemeral memory pool for whole resolution
76
 */
77
int kr_rplan_init(struct kr_rplan *rplan, struct kr_context *context, mm_ctx_t *pool);
78

79 80 81
/**
 * Deinitialize resolution plan, aborting any uncommited transactions.
 * @param rplan plan instance
82 83 84
 */
void kr_rplan_deinit(struct kr_rplan *rplan);

85 86 87
/**
 * Return true if the resolution plan is empty (i.e. finished or initialized)
 * @param rplan plan instance
88
 * @return true or false
89 90 91
 */
bool kr_rplan_empty(struct kr_rplan *rplan);

92 93 94
/**
 * Acquire rplan transaction (read or write only).
 * @note The transaction is shared during the whole resolution, read only transactions
95
 *       may be promoted to write-enabled transactions if requested, but never demoted.
96 97
 * @param rplan plan instance
 * @param flags transaction flags
98
 * @return transaction instance or NULL
99 100
 */
namedb_txn_t *kr_rplan_txn_acquire(struct kr_rplan *rplan, unsigned flags);
101

102 103 104
/**
 * Commit any existing transaction, read-only transactions may be just aborted.
 * @param rplan plan instance
105
 * @return KNOT_E*
106 107 108
 */
int kr_rplan_txn_commit(struct kr_rplan *rplan);

109 110 111 112 113 114 115 116
/**
 * Push a query to the top of the resolution plan.
 * @note This means that this query takes precedence before all pending queries.
 * @param rplan plan instance
 * @param parent query parent (or NULL)
 * @param name resolved name
 * @param cls  resolved class
 * @param type resolved type
117
 * @return query instance or NULL
118
 */
119 120
struct kr_query *kr_rplan_push(struct kr_rplan *rplan, struct kr_query *parent,
                               const knot_dname_t *name, uint16_t cls, uint16_t type);
121

122 123 124 125 126
/**
 * Pop existing query from the resolution plan.
 * @note Popped queries are not discarded, but moved to the resolved list.
 * @param rplan plan instance
 * @param qry resolved query
127
 * @return KNOT_E*
128
 */
129 130
int kr_rplan_pop(struct kr_rplan *rplan, struct kr_query *qry);

131 132 133
/**
 * Currently resolved query (at the top).
 * @param rplan plan instance
134
 * @return query instance or NULL if empty
135 136
 */
struct kr_query *kr_rplan_current(struct kr_rplan *rplan);
137

138 139
/**
 * Return true if resolution chain satisfies given query.
140 141
 */
bool kr_rplan_satisfies(struct kr_query *closure, const knot_dname_t *name, uint16_t cls, uint16_t type);
Marek Vavruša's avatar
Marek Vavruša committed
142 143

/** @} */