Skip to content

Commit

Permalink
Refactor dnscurve plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Feb 12, 2014
1 parent fd79189 commit 0fd8575
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 48 deletions.
4 changes: 2 additions & 2 deletions include/rdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct rdns_async_context {
* Type of rdns plugin
*/
enum rdns_plugin_type {
RDNS_PLUGIN_NETWORK = 0//!< use the specified plugin instead of send/recv functions
RDNS_PLUGIN_CURVE = 0//!< use the specified plugin instead of send/recv functions
};

typedef ssize_t (*rdns_network_send_callback) (struct rdns_request *req, void *plugin_data);
Expand All @@ -142,7 +142,7 @@ struct rdns_plugin {
rdns_network_send_callback send_cb;
rdns_network_recv_callback recv_cb;
rdns_network_finish_callback finish_cb;
} network_plugin;
} curve_plugin;
} cb;
rdns_plugin_dtor_callback dtor;
void *data;
Expand Down
57 changes: 27 additions & 30 deletions src/curve.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "dns_private.h"
#include "rdns_curve.h"
#include "ottery.h"
#include "ref.h"

#ifdef HAVE_SODIUM
#include <sodium.h>
Expand Down Expand Up @@ -59,7 +60,7 @@ struct rdns_curve_client_key {
struct rdns_curve_nm_entry *nms;
uint64_t counter;
unsigned int uses;
unsigned int ref;
ref_entry_t ref;
};

struct rdns_curve_request {
Expand Down Expand Up @@ -116,26 +117,17 @@ rdns_curve_find_nm (struct rdns_curve_client_key *key, struct rdns_curve_entry *
return NULL;
}

static struct rdns_curve_client_key *
rdns_curve_client_key_ref (struct rdns_curve_client_key *key)
{
key->ref ++;
return key;
}

static void
rdns_curve_client_key_unref (struct rdns_curve_client_key *key)
rdns_curve_client_key_free (struct rdns_curve_client_key *key)
{
struct rdns_curve_nm_entry *nm, *tmp;

if (--key->ref == 0) {
DL_FOREACH_SAFE (key->nms, nm, tmp) {
sodium_memzero (nm->k, sizeof (nm->k));
free (nm);
}
sodium_memzero (key->sk, sizeof (key->sk));
free (key);
DL_FOREACH_SAFE (key->nms, nm, tmp) {
sodium_memzero (nm->k, sizeof (nm->k));
free (nm);
}
sodium_memzero (key->sk, sizeof (key->sk));
free (key);
}

struct rdns_curve_ctx*
Expand Down Expand Up @@ -218,7 +210,8 @@ rdns_curve_key_from_hex (const char *hex)
return res;
}

void rdns_curve_ctx_destroy (struct rdns_curve_ctx *ctx)
void
rdns_curve_ctx_destroy (struct rdns_curve_ctx *ctx)
{
struct rdns_curve_entry *entry, *tmp;

Expand All @@ -236,8 +229,9 @@ rdns_curve_refresh_key_callback (void *user_data)
struct rdns_curve_ctx *ctx = user_data;

DNS_DEBUG ("refresh curve keys");
rdns_curve_client_key_unref (ctx->cur_key);
ctx->cur_key = rdns_curve_client_key_ref (rdns_curve_client_key_new (ctx));
REF_RELEASE (ctx->cur_key);
ctx->cur_key = rdns_curve_client_key_new (ctx);
REF_INIT_RETAIN (ctx->cur_key, rdns_curve_client_key_free);
}

void
Expand All @@ -253,13 +247,15 @@ rdns_curve_register_plugin (struct rdns_resolver *resolver,
plugin = calloc (1, sizeof (struct rdns_plugin));
if (plugin != NULL) {
plugin->data = ctx;
plugin->type = RDNS_PLUGIN_NETWORK;
plugin->cb.network_plugin.send_cb = rdns_curve_send;
plugin->cb.network_plugin.recv_cb = rdns_curve_recv;
plugin->cb.network_plugin.finish_cb = rdns_curve_finish_request;
plugin->type = RDNS_PLUGIN_CURVE;
plugin->cb.curve_plugin.send_cb = rdns_curve_send;
plugin->cb.curve_plugin.recv_cb = rdns_curve_recv;
plugin->cb.curve_plugin.finish_cb = rdns_curve_finish_request;
plugin->dtor = rdns_curve_dtor;
sodium_init ();
ctx->cur_key = rdns_curve_client_key_ref (rdns_curve_client_key_new (ctx));
ctx->cur_key = rdns_curve_client_key_new (ctx);
REF_INIT_RETAIN (ctx->cur_key, rdns_curve_client_key_free);

if (ctx->key_refresh_interval > 0) {
ctx->key_refresh_event = resolver->async->add_periodic (
resolver->async->data, ctx->key_refresh_interval,
Expand Down Expand Up @@ -311,12 +307,13 @@ rdns_curve_send (struct rdns_request *req, void *plugin_data)
return -1;
}

creq->key = rdns_curve_client_key_ref (ctx->cur_key);
creq->key = ctx->cur_key;
REF_RETAIN (ctx->cur_key);
creq->entry = entry;
creq->req = req;
creq->nm = nm;
HASH_ADD_KEYPTR (hh, ctx->requests, creq->nonce, 12, creq);
req->network_plugin_data = creq;
req->curve_plugin_data = creq;

ctx->cur_key->counter ++;
ctx->cur_key->uses ++;
Expand All @@ -337,7 +334,7 @@ rdns_curve_send (struct rdns_request *req, void *plugin_data)
}
else {
ret = write (req->io->sock, req->packet, req->pos);
req->network_plugin_data = NULL;
req->curve_plugin_data = NULL;
}

return ret;
Expand Down Expand Up @@ -401,10 +398,10 @@ void
rdns_curve_finish_request (struct rdns_request *req, void *plugin_data)
{
struct rdns_curve_ctx *ctx = (struct rdns_curve_ctx *)plugin_data;
struct rdns_curve_request *creq = req->network_plugin_data;
struct rdns_curve_request *creq = req->curve_plugin_data;

if (creq != NULL) {
rdns_curve_client_key_unref (creq->key);
REF_RELEASE (creq->key);
HASH_DELETE (hh, ctx->requests, creq);
}
}
Expand All @@ -418,7 +415,7 @@ rdns_curve_dtor (struct rdns_resolver *resolver, void *plugin_data)
resolver->async->del_periodic (resolver->async->data,
ctx->key_refresh_event);
}
rdns_curve_client_key_unref (ctx->cur_key);
REF_RELEASE (ctx->cur_key);
}

#else
Expand Down
6 changes: 4 additions & 2 deletions src/dns_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ struct rdns_request {

void *async_event;

void *network_plugin_data;
#ifdef HAVE_SODIUM
void *curve_plugin_data;
#endif

UT_hash_handle hh;
ref_entry_t ref;
Expand Down Expand Up @@ -123,7 +125,7 @@ struct rdns_resolver {
struct rdns_async_context *async; /** async callbacks */
void *periodic; /** periodic event for resolver */

struct rdns_plugin *network_plugin;
struct rdns_plugin *curve_plugin;

bool async_binded;
bool initialized;
Expand Down
24 changes: 13 additions & 11 deletions src/resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req)
}
}

if (resolver->network_plugin == NULL) {
if (resolver->curve_plugin == NULL) {
r = send (fd, req->packet, req->pos, 0);
}
else {
r = resolver->network_plugin->cb.network_plugin.send_cb (req,
resolver->network_plugin->data);
r = resolver->curve_plugin->cb.curve_plugin.send_cb (req,
resolver->curve_plugin->data);
}
if (r == -1) {
if (errno == EAGAIN || errno == EINTR) {
Expand Down Expand Up @@ -250,15 +250,15 @@ rdns_process_read (int fd, void *arg)
resolver = ioc->resolver;

/* First read packet from socket */
if (resolver->network_plugin == NULL) {
if (resolver->curve_plugin == NULL) {
r = read (fd, in, sizeof (in));
if (r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) {
req = rdns_find_dns_request (in, ioc);
}
}
else {
r = resolver->network_plugin->cb.network_plugin.recv_cb (ioc, in,
sizeof (in), resolver->network_plugin->data, &req);
r = resolver->curve_plugin->cb.curve_plugin.recv_cb (ioc, in,
sizeof (in), resolver->curve_plugin->data, &req);
if (req == NULL &&
r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) {
req = rdns_find_dns_request (in, ioc);
Expand Down Expand Up @@ -392,7 +392,9 @@ rdns_make_request_full (
req->func = cb;
req->arg = cbdata;
req->reply = NULL;
req->network_plugin_data = NULL;
#ifdef HAVE_SODIUM
req->curve_plugin_data = NULL;
#endif
REF_INIT_RETAIN (req, rdns_request_free);

va_start (args, queries);
Expand Down Expand Up @@ -513,8 +515,8 @@ rdns_resolver_register_plugin (struct rdns_resolver *resolver,
{
if (resolver != NULL && plugin != NULL) {
/* XXX: support only network plugin now, and only a single one */
if (plugin->type == RDNS_PLUGIN_NETWORK) {
resolver->network_plugin = plugin;
if (plugin->type == RDNS_PLUGIN_CURVE) {
resolver->curve_plugin = plugin;
}
}
}
Expand Down Expand Up @@ -571,8 +573,8 @@ rdns_resolver_free (struct rdns_resolver *resolver)
if (resolver->periodic != NULL) {
resolver->async->del_periodic (resolver->async->data, resolver->periodic);
}
if (resolver->network_plugin != NULL && resolver->network_plugin->dtor != NULL) {
resolver->network_plugin->dtor (resolver, resolver->network_plugin->data);
if (resolver->curve_plugin != NULL && resolver->curve_plugin->dtor != NULL) {
resolver->curve_plugin->dtor (resolver, resolver->curve_plugin->data);
}
/* Stop IO watch on all IO channels */
UPSTREAM_FOREACH_SAFE (resolver->servers, serv, stmp) {
Expand Down
8 changes: 5 additions & 3 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ rdns_request_free (struct rdns_request *req)
req->async->del_write (req->async->data,
req->async_event);
}
if (req->network_plugin_data != NULL) {
req->resolver->network_plugin->cb.network_plugin.finish_cb (
req, req->resolver->network_plugin->data);
#ifdef HAVE_SODIUM
if (req->curve_plugin_data != NULL) {
req->resolver->curve_plugin->cb.curve_plugin.finish_cb (
req, req->resolver->curve_plugin->data);
}
#endif
if (req->io != NULL && req->state > RDNS_REQUEST_NEW) {
/* Remove from id hashes */
HASH_DEL (req->io->requests, req);
Expand Down

0 comments on commit 0fd8575

Please sign in to comment.