rplan.h 4.13 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 17 18

#pragma once

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

21
#include <libknot/dname.h>
22
#include <libknot/internal/lists.h>
Marek Vavruša's avatar
Marek Vavruša committed
23 24
#include <libknot/internal/namedb/namedb.h>
#include <libknot/internal/sockaddr.h>
25

Marek Vavruša's avatar
Marek Vavruša committed
26 27
#include "lib/context.h"
#include "lib/zonecut.h"
28

29 30
/* Query flags */
enum {
31 32
	QUERY_NO_MINIMIZE = 1 << 0, /*!< Don't minimize QNAME. */
	QUERY_TCP         = 1 << 1  /*!< Use TCP for this query. */
33 34
};

Marek Vavruša's avatar
Marek Vavruša committed
35 36 37
/*!
 * \brief Single query representation.
 */
38 39
struct kr_query {
	node_t node;
40
	struct kr_query *parent;
41
	struct kr_zonecut zone_cut;
Marek Vavruša's avatar
Marek Vavruša committed
42
	struct timeval timestamp;
43 44 45
	knot_dname_t *sname;
	uint16_t stype;
	uint16_t sclass;
Marek Vavruša's avatar
Marek Vavruša committed
46
	uint16_t id;
47 48 49
	uint16_t flags;
};

Marek Vavruša's avatar
Marek Vavruša committed
50 51 52 53 54 55 56
/*!
 * \brief Query resolution plan structure.
 *
 * 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.
 */
57
struct kr_rplan {
Marek Vavruša's avatar
Marek Vavruša committed
58 59 60 61 62 63
	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. */
64 65
};

Marek Vavruša's avatar
Marek Vavruša committed
66 67 68 69 70 71
/*!
 * \brief Initialize resolution plan (empty).
 * \param rplan plan instance
 * \param context resolution context
 * \param pool ephemeral memory pool for whole resolution
 */
72
int kr_rplan_init(struct kr_rplan *rplan, struct kr_context *context, mm_ctx_t *pool);
Marek Vavruša's avatar
Marek Vavruša committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

/*!
 * \brief Deinitialize resolution plan, aborting any uncommited transactions.
 * \param rplan plan instance
 */
void kr_rplan_deinit(struct kr_rplan *rplan);

/*!
 * \brief Return true if the resolution plan is empty (i.e. finished or initialized)
 * \param rplan plan instance
 * \return true or false
 */
bool kr_rplan_empty(struct kr_rplan *rplan);

/*!
 * \brief Acquire rplan transaction (read or write only).
 * \note The transaction is shared during the whole resolution, read only transactions
 *       may be promoted to write-enabled transactions if requested, but never demoted.
 * \param rplan plan instance
 * \param flags transaction flags
 * \return transaction instance or NULL
 */
namedb_txn_t *kr_rplan_txn_acquire(struct kr_rplan *rplan, unsigned flags);
96

Marek Vavruša's avatar
Marek Vavruša committed
97 98 99 100 101 102 103 104 105 106 107
/*!
 * \brief Commit any existing transaction, read-only transactions may be just aborted.
 * \param rplan plan instance
 * \return KNOT_E*
 */
int kr_rplan_txn_commit(struct kr_rplan *rplan);

/*!
 * \brief 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
108
 * \param parent query parent (or NULL)
Marek Vavruša's avatar
Marek Vavruša committed
109 110 111 112 113
 * \param name resolved name
 * \param cls  resolved class
 * \param type resolved type
 * \return query instance or NULL
 */
114 115
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);
Marek Vavruša's avatar
Marek Vavruša committed
116 117 118 119 120 121 122 123

/*!
 * \brief 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
 * \return KNOT_E*
 */
124 125
int kr_rplan_pop(struct kr_rplan *rplan, struct kr_query *qry);

Marek Vavruša's avatar
Marek Vavruša committed
126 127 128 129 130 131
/*!
 * \brief Currently resolved query (at the top).
 * \param rplan plan instance
 * \return query instance or NULL if empty
 */
struct kr_query *kr_rplan_current(struct kr_rplan *rplan);