Commit 0afdb24c authored by Daniel Salzman's avatar Daniel Salzman

requestor: code cleanup

parent 84fab142
......@@ -137,7 +137,7 @@ static int try_ds(const knot_dname_t *zone_name, const conf_remote_t *parent, zo
.result_logged = false,
};
struct knot_requestor requestor;
knot_requestor_t requestor;
knot_requestor_init(&requestor, &ds_query_api, &data, NULL);
knot_pkt_t *pkt = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, NULL);
......@@ -148,7 +148,7 @@ static int try_ds(const knot_dname_t *zone_name, const conf_remote_t *parent, zo
const struct sockaddr *dst = (struct sockaddr *)&parent->addr;
const struct sockaddr *src = (struct sockaddr *)&parent->via;
struct knot_request *req = knot_request_make(NULL, dst, src, pkt, &parent->key, 0);
knot_request_t *req = knot_request_make(NULL, dst, src, pkt, &parent->key, 0);
if (!req) {
knot_request_free(req, NULL);
knot_requestor_clear(&requestor);
......
......@@ -87,7 +87,7 @@ static int send_notify(conf_t *conf, zone_t *zone, const knot_rrset_t *soa,
query_edns_data_init(&data.edns, conf, zone->name, slave->addr.ss_family);
struct knot_requestor requestor = { 0 };
knot_requestor_t requestor;
knot_requestor_init(&requestor, &NOTIFY_API, &data, NULL);
knot_pkt_t *pkt = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, NULL);
......@@ -98,7 +98,7 @@ static int send_notify(conf_t *conf, zone_t *zone, const knot_rrset_t *soa,
const struct sockaddr *dst = (struct sockaddr *)&slave->addr;
const struct sockaddr *src = (struct sockaddr *)&slave->via;
struct knot_request *req = knot_request_make(NULL, dst, src, pkt, &slave->key, 0);
knot_request_t *req = knot_request_make(NULL, dst, src, pkt, &slave->key, 0);
if (!req) {
knot_request_free(req, NULL);
knot_requestor_clear(&requestor);
......
......@@ -1057,7 +1057,7 @@ static int try_refresh(conf_t *conf, zone_t *zone, const conf_remote_t *master,
query_edns_data_init(&data.edns, conf, zone->name, master->addr.ss_family);
struct knot_requestor requestor;
knot_requestor_t requestor;
knot_requestor_init(&requestor, &REFRESH_API, &data, NULL);
knot_pkt_t *pkt = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, NULL);
......@@ -1068,7 +1068,7 @@ static int try_refresh(conf_t *conf, zone_t *zone, const conf_remote_t *master,
const struct sockaddr *dst = (struct sockaddr *)&master->addr;
const struct sockaddr *src = (struct sockaddr *)&master->via;
struct knot_request *req = knot_request_make(NULL, dst, src, pkt, &master->key, 0);
knot_request_t *req = knot_request_make(NULL, dst, src, pkt, &master->key, 0);
if (!req) {
knot_request_free(req, NULL);
knot_requestor_clear(&requestor);
......@@ -1087,7 +1087,7 @@ static int try_refresh(conf_t *conf, zone_t *zone, const conf_remote_t *master,
ixfr_cleanup(&data);
data.xfr_type = XFR_TYPE_AXFR;
requestor.layer.state = KNOT_STATE_RESET;
requestor.layer.flags |= KNOT_RQ_LAYER_CLOSE;
requestor.layer.flags |= KNOT_REQUESTOR_CLOSE;
}
knot_request_free(req, NULL);
knot_requestor_clear(&requestor);
......
......@@ -81,7 +81,7 @@ static knotd_state_t dnsproxy_fwd(knotd_state_t state, knot_pkt_t *pkt,
};
/* Create a forwarding request. */
struct knot_requestor re;
knot_requestor_t re;
int ret = knot_requestor_init(&re, capture, &capture_param, qdata->mm);
if (ret != KNOT_EOK) {
return state; /* Ignore, not enough memory. */
......@@ -90,8 +90,8 @@ static knotd_state_t dnsproxy_fwd(knotd_state_t state, knot_pkt_t *pkt,
bool is_tcp = net_is_stream(qdata->params->socket);
const struct sockaddr *dst = (const struct sockaddr *)&proxy->remote;
const struct sockaddr *src = (const struct sockaddr *)&proxy->via;
struct knot_request *req = knot_request_make(re.mm, dst, src, qdata->query, NULL,
is_tcp ? 0 : KNOT_RQ_UDP);
knot_request_t *req = knot_request_make(re.mm, dst, src, qdata->query, NULL,
is_tcp ? 0 : KNOT_REQUEST_UDP);
if (req == NULL) {
knot_requestor_clear(&re);
return state; /* Ignore, not enough memory. */
......
......@@ -39,7 +39,7 @@
static void init_qdata_from_request(knotd_qdata_t *qdata,
const zone_t *zone,
struct knot_request *req,
knot_request_t *req,
knotd_qdata_params_t *params,
knotd_qdata_extra_t *extra)
{
......@@ -52,7 +52,7 @@ static void init_qdata_from_request(knotd_qdata_t *qdata,
qdata->extra->zone = zone;
}
static int check_prereqs(struct knot_request *request,
static int check_prereqs(knot_request_t *request,
const zone_t *zone, zone_update_t *update,
knotd_qdata_t *qdata)
{
......@@ -69,7 +69,7 @@ static int check_prereqs(struct knot_request *request,
return KNOT_EOK;
}
static int process_single_update(struct knot_request *request,
static int process_single_update(knot_request_t *request,
const zone_t *zone, zone_update_t *update,
knotd_qdata_t *qdata)
{
......@@ -90,7 +90,7 @@ static void set_rcodes(list_t *requests, const uint16_t rcode)
{
ptrnode_t *node = NULL;
WALK_LIST(node, *requests) {
struct knot_request *req = node->d;
knot_request_t *req = node->d;
if (knot_wire_get_rcode(req->resp->wire) == KNOT_RCODE_NOERROR) {
knot_wire_set_rcode(req->resp->wire, rcode);
}
......@@ -107,7 +107,7 @@ static int process_bulk(zone_t *zone, list_t *requests, zone_update_t *up)
// Walk all the requests and process.
ptrnode_t *node = NULL;
WALK_LIST(node, *requests) {
struct knot_request *req = node->d;
knot_request_t *req = node->d;
// Init qdata structure for logging (unique per-request).
knotd_qdata_params_t params = {
.remote = &req->remote
......@@ -217,7 +217,7 @@ static void process_requests(conf_t *conf, zone_t *zone, list_t *requests)
zone_events_schedule_at(zone, ZONE_EVENT_NOTIFY, time(NULL) + 1);
}
static int remote_forward(conf_t *conf, struct knot_request *request, conf_remote_t *remote)
static int remote_forward(conf_t *conf, knot_request_t *request, conf_remote_t *remote)
{
/* Copy request and assign new ID. */
knot_pkt_t *query = knot_pkt_new(NULL, request->query->max_size, NULL);
......@@ -236,7 +236,7 @@ static int remote_forward(conf_t *conf, struct knot_request *request, conf_remot
};
/* Create requestor instance. */
struct knot_requestor re;
knot_requestor_t re;
ret = knot_requestor_init(&re, capture, &capture_param, NULL);
if (ret != KNOT_EOK) {
knot_pkt_free(query);
......@@ -246,7 +246,7 @@ static int remote_forward(conf_t *conf, struct knot_request *request, conf_remot
/* Create a request. */
const struct sockaddr *dst = (const struct sockaddr *)&remote->addr;
const struct sockaddr *src = (const struct sockaddr *)&remote->via;
struct knot_request *req = knot_request_make(re.mm, dst, src, query, NULL, 0);
knot_request_t *req = knot_request_make(re.mm, dst, src, query, NULL, 0);
if (req == NULL) {
knot_requestor_clear(&re);
knot_pkt_free(query);
......@@ -263,7 +263,7 @@ static int remote_forward(conf_t *conf, struct knot_request *request, conf_remot
return ret;
}
static void forward_request(conf_t *conf, zone_t *zone, struct knot_request *request)
static void forward_request(conf_t *conf, zone_t *zone, knot_request_t *request)
{
/* Read the ddns master or the first master. */
conf_val_t remote = conf_zone_get(conf, C_DDNS_MASTER, zone->name);
......@@ -309,7 +309,7 @@ static void forward_requests(conf_t *conf, zone_t *zone, list_t *requests)
ptrnode_t *node = NULL;
WALK_LIST(node, *requests) {
struct knot_request *req = node->d;
knot_request_t *req = node->d;
forward_request(conf, zone, req);
}
}
......@@ -329,7 +329,7 @@ static bool update_tsig_check(conf_t *conf, knotd_qdata_t *qdata, struct knot_re
return true;
}
static void send_update_response(conf_t *conf, const zone_t *zone, struct knot_request *req)
static void send_update_response(conf_t *conf, const zone_t *zone, knot_request_t *req)
{
if (req->resp) {
if (!zone_is_slave(conf, zone)) {
......@@ -352,7 +352,7 @@ static void send_update_response(conf_t *conf, const zone_t *zone, struct knot_r
}
}
static void free_request(struct knot_request *req)
static void free_request(knot_request_t *req)
{
close(req->fd);
knot_pkt_free(req->query);
......@@ -364,7 +364,7 @@ static void send_update_responses(conf_t *conf, const zone_t *zone, list_t *upda
{
ptrnode_t *node = NULL, *nxt = NULL;
WALK_LIST_DELSAFE(node, nxt, *updates) {
struct knot_request *req = node->d;
knot_request_t *req = node->d;
send_update_response(conf, zone, req);
free_request(req);
}
......@@ -376,7 +376,7 @@ static int init_update_responses(conf_t *conf, const zone_t *zone, list_t *updat
{
ptrnode_t *node = NULL, *nxt = NULL;
WALK_LIST_DELSAFE(node, nxt, *updates) {
struct knot_request *req = node->d;
knot_request_t *req = node->d;
req->resp = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, NULL);
if (req->resp == NULL) {
return KNOT_ENOMEM;
......
......@@ -23,9 +23,9 @@
#include "contrib/net.h"
#include "contrib/sockaddr.h"
static bool use_tcp(struct knot_request *request)
static bool use_tcp(knot_request_t *request)
{
return (request->flags & KNOT_RQ_UDP) == 0;
return (request->flags & KNOT_REQUEST_UDP) == 0;
}
static bool is_answer_to_query(const knot_pkt_t *query, const knot_pkt_t *answer)
......@@ -34,7 +34,7 @@ static bool is_answer_to_query(const knot_pkt_t *query, const knot_pkt_t *answer
}
/*! \brief Ensure a socket is connected. */
static int request_ensure_connected(struct knot_request *request)
static int request_ensure_connected(knot_request_t *request)
{
if (request->fd >= 0) {
return KNOT_EOK;
......@@ -51,7 +51,7 @@ static int request_ensure_connected(struct knot_request *request)
return KNOT_EOK;
}
static int request_send(struct knot_request *request, int timeout_ms)
static int request_send(knot_request_t *request, int timeout_ms)
{
/* Initiate non-blocking connect if not connected. */
int ret = request_ensure_connected(request);
......@@ -77,7 +77,7 @@ static int request_send(struct knot_request *request, int timeout_ms)
return KNOT_EOK;
}
static int request_recv(struct knot_request *request, int timeout_ms)
static int request_recv(knot_request_t *request, int timeout_ms)
{
knot_pkt_t *resp = request->resp;
knot_pkt_clear(resp);
......@@ -106,22 +106,21 @@ static int request_recv(struct knot_request *request, int timeout_ms)
return ret;
}
struct knot_request *knot_request_make(knot_mm_t *mm,
const struct sockaddr *remote,
const struct sockaddr *source,
knot_pkt_t *query,
const knot_tsig_key_t *tsig_key,
unsigned flags)
knot_request_t *knot_request_make(knot_mm_t *mm,
const struct sockaddr *remote,
const struct sockaddr *source,
knot_pkt_t *query,
const knot_tsig_key_t *tsig_key,
knot_request_flag_t flags)
{
if (remote == NULL || query == NULL) {
return NULL;
}
struct knot_request *request = mm_alloc(mm, sizeof(*request));
knot_request_t *request = mm_calloc(mm, 1, sizeof(*request));
if (request == NULL) {
return NULL;
}
memset(request, 0, sizeof(*request));
request->resp = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, mm);
if (request->resp == NULL) {
......@@ -147,7 +146,7 @@ struct knot_request *knot_request_make(knot_mm_t *mm,
return request;
}
void knot_request_free(struct knot_request *request, knot_mm_t *mm)
void knot_request_free(knot_request_t *request, knot_mm_t *mm)
{
if (request == NULL) {
return;
......@@ -163,7 +162,7 @@ void knot_request_free(struct knot_request *request, knot_mm_t *mm)
mm_free(mm, request);
}
int knot_requestor_init(struct knot_requestor *requestor,
int knot_requestor_init(knot_requestor_t *requestor,
const knot_layer_api_t *proc, void *proc_param,
knot_mm_t *mm)
{
......@@ -180,7 +179,7 @@ int knot_requestor_init(struct knot_requestor *requestor,
return KNOT_EOK;
}
void knot_requestor_clear(struct knot_requestor *requestor)
void knot_requestor_clear(knot_requestor_t *requestor)
{
if (requestor == NULL) {
return;
......@@ -191,14 +190,13 @@ void knot_requestor_clear(struct knot_requestor *requestor)
memset(requestor, 0, sizeof(*requestor));
}
static int request_reset(struct knot_requestor *req,
struct knot_request *last)
static int request_reset(knot_requestor_t *req, knot_request_t *last)
{
knot_layer_reset(&req->layer);
tsig_reset(&last->tsig);
if (req->layer.flags & KNOT_RQ_LAYER_CLOSE) {
req->layer.flags &= ~KNOT_RQ_LAYER_CLOSE;
if (req->layer.flags & KNOT_REQUESTOR_CLOSE) {
req->layer.flags &= ~KNOT_REQUESTOR_CLOSE;
if (last->fd >= 0) {
close(last->fd);
last->fd = -1;
......@@ -212,8 +210,7 @@ static int request_reset(struct knot_requestor *req,
return KNOT_EOK;
}
static int request_produce(struct knot_requestor *req,
struct knot_request *last,
static int request_produce(knot_requestor_t *req, knot_request_t *last,
int timeout_ms)
{
knot_layer_produce(&req->layer, last->query);
......@@ -231,8 +228,7 @@ static int request_produce(struct knot_requestor *req,
return ret;
}
static int request_consume(struct knot_requestor *req,
struct knot_request *last,
static int request_consume(knot_requestor_t *req, knot_request_t *last,
int timeout_ms)
{
int ret = request_recv(last, timeout_ms);
......@@ -275,7 +271,7 @@ static bool layer_active(knot_layer_state_t state)
}
}
static int request_io(struct knot_requestor *req, struct knot_request *last,
static int request_io(knot_requestor_t *req, knot_request_t *last,
int timeout_ms)
{
switch (req->layer.state) {
......@@ -290,11 +286,10 @@ static int request_io(struct knot_requestor *req, struct knot_request *last,
}
}
int knot_requestor_exec(struct knot_requestor *requestor,
struct knot_request *request,
int knot_requestor_exec(knot_requestor_t *requestor, knot_request_t *request,
int timeout_ms)
{
if (!requestor || !request) {
if (requestor == NULL || request == NULL) {
return KNOT_EINVAL;
}
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -24,38 +24,34 @@
#include "libknot/mm_ctx.h"
#include "libknot/rrtype/tsig.h"
struct knot_request;
typedef enum {
KNOT_REQUEST_UDP = 1 << 0 /*!< Use UDP for requests. */
} knot_request_flag_t;
/* Requestor flags. */
enum {
KNOT_RQ_UDP = 1 << 0 /* Use UDP for requests. */
};
enum {
KNOT_RQ_LAYER_CLOSE = 1 << 0
};
typedef enum {
KNOT_REQUESTOR_CLOSE = 1 << 0
} knot_requestor_flag_t;
/*! \brief Requestor structure.
*
* Requestor holds a FIFO of pending queries.
*/
struct knot_requestor {
typedef struct {
knot_mm_t *mm; /*!< Memory context. */
knot_layer_t layer; /*!< Response processing layer. */
};
} knot_requestor_t;
/*! \brief Request data (socket, payload, response, TSIG and endpoints). */
struct knot_request {
typedef struct {
int fd;
unsigned flags;
knot_request_flag_t flags;
struct sockaddr_storage remote, source;
knot_pkt_t *query;
knot_pkt_t *resp;
tsig_ctx_t tsig;
knot_sign_context_t sign; /* TODO: Remove. Used in updates only, should
be part of the zone update context. */
};
knot_sign_context_t sign; /*!< Required for async. DDNS processing. */
} knot_request_t;
/*!
* \brief Make request out of endpoints and query.
......@@ -69,12 +65,12 @@ struct knot_request {
*
* \return Prepared request or NULL in case of error.
*/
struct knot_request *knot_request_make(knot_mm_t *mm,
const struct sockaddr *dst,
const struct sockaddr *src,
knot_pkt_t *query,
const knot_tsig_key_t *key,
unsigned flags);
knot_request_t *knot_request_make(knot_mm_t *mm,
const struct sockaddr *dst,
const struct sockaddr *src,
knot_pkt_t *query,
const knot_tsig_key_t *key,
knot_request_flag_t flags);
/*!
* \brief Free request and associated data.
......@@ -82,7 +78,7 @@ struct knot_request *knot_request_make(knot_mm_t *mm,
* \param request Freed request.
* \param mm Memory context.
*/
void knot_request_free(struct knot_request *request, knot_mm_t *mm);
void knot_request_free(knot_request_t *request, knot_mm_t *mm);
/*!
* \brief Initialize requestor structure.
......@@ -94,7 +90,7 @@ void knot_request_free(struct knot_request *request, knot_mm_t *mm);
*
* \return KNOT_EOK or error
*/
int knot_requestor_init(struct knot_requestor *requestor,
int knot_requestor_init(knot_requestor_t *requestor,
const knot_layer_api_t *proc, void *proc_param,
knot_mm_t *mm);
......@@ -103,7 +99,7 @@ int knot_requestor_init(struct knot_requestor *requestor,
*
* \param requestor Requestor instance.
*/
void knot_requestor_clear(struct knot_requestor *requestor);
void knot_requestor_clear(knot_requestor_t *requestor);
/*!
* \brief Execute a request.
......@@ -114,6 +110,6 @@ void knot_requestor_clear(struct knot_requestor *requestor);
*
* \return KNOT_EOK or error
*/
int knot_requestor_exec(struct knot_requestor *requestor,
struct knot_request *request,
int knot_requestor_exec(knot_requestor_t *requestor,
knot_request_t *request,
int timeout_ms);
/* Copyright (C) 2013 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -78,9 +78,9 @@ static void *responder_thread(void *arg)
/* Test implementations. */
static struct knot_request *make_query(struct knot_requestor *requestor,
const struct sockaddr_storage *dst,
const struct sockaddr_storage *src)
static knot_request_t *make_query(knot_requestor_t *requestor,
const struct sockaddr_storage *dst,
const struct sockaddr_storage *src)
{
knot_pkt_t *pkt = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, requestor->mm);
assert(pkt);
......@@ -91,23 +91,23 @@ static struct knot_request *make_query(struct knot_requestor *requestor,
(struct sockaddr *)src, pkt, NULL, 0);
}
static void test_disconnected(struct knot_requestor *requestor,
static void test_disconnected(knot_requestor_t *requestor,
const struct sockaddr_storage *dst,
const struct sockaddr_storage *src)
{
struct knot_request *req = make_query(requestor, dst, src);
knot_request_t *req = make_query(requestor, dst, src);
int ret = knot_requestor_exec(requestor, req, TIMEOUT);
is_int(KNOT_ECONN, ret, "requestor: disconnected/exec");
knot_request_free(req, requestor->mm);
}
static void test_connected(struct knot_requestor *requestor,
static void test_connected(knot_requestor_t *requestor,
const struct sockaddr_storage *dst,
const struct sockaddr_storage *src)
{
/* Enqueue packet. */
struct knot_request *req = make_query(requestor, dst, src);
knot_request_t *req = make_query(requestor, dst, src);
int ret = knot_requestor_exec(requestor, req, TIMEOUT);
is_int(KNOT_EOK, ret, "requestor: connected/exec");
knot_request_free(req, requestor->mm);
......@@ -121,7 +121,7 @@ int main(int argc, char *argv[])
mm_ctx_mempool(&mm, MM_DEFAULT_BLKSIZE);
/* Initialize requestor. */
struct knot_requestor requestor;
knot_requestor_t requestor;
knot_requestor_init(&requestor, &dummy_module, NULL, &mm);
/* Define endpoints. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment