Skip to content

Commit

Permalink
Implement logger interaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Feb 14, 2014
1 parent 286d6ba commit 4aa1511
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 46 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ENDIF(ENABLE_CURVE MATCHES "ON")


SET(LIBRDNSSRC src/util.c
src/logger.c
src/punycode.c
src/curve.c
src/parse.c
Expand Down
42 changes: 41 additions & 1 deletion include/rdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Expand Down Expand Up @@ -148,7 +149,29 @@ struct rdns_plugin {
void *data;
};

/* Rspamd DNS API */
/*
* RDNS logger types
*/
/*
* These types are somehow compatible with glib
*/
enum rdns_log_level {
RDNS_LOG_ERROR = 1 << 3,
RDNS_LOG_WARNING = 1 << 4,
RDNS_LOG_INFO = 1 << 6,
RDNS_LOG_DEBUG = 1 << 7
};
typedef void (*rdns_log_function) (
void *log_data, //!< opaque data pointer
enum rdns_log_level level, //!< level of message
const char *function, //!< calling function
const char *format, //!< format
va_list args //!< set of arguments
);

/*
* RDNS API
*/

/**
* Create DNS resolver structure
Expand Down Expand Up @@ -184,6 +207,23 @@ bool rdns_resolver_add_server (struct rdns_resolver *resolver,
bool rdns_resolver_parse_resolv_conf (struct rdns_resolver *resolver,
const char *path);

/**
* Set an external logger function to log messages from the resolver
* @param resolver resolver object
* @param logger logger callback
* @param log_data opaque data
*/
void rdns_resolver_set_logger (struct rdns_resolver *resolver,
rdns_log_function logger, void *log_data);

/**
* Set log level for an internal logger (stderr one)
* @param resolver resolver object
* @param level desired log level
*/
void rdns_resolver_set_log_level (struct rdns_resolver *resolver,
enum rdns_log_level level);

/**
* Register new plugin for rdns resolver
* @param resolver
Expand Down
13 changes: 10 additions & 3 deletions src/curve.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "rdns_curve.h"
#include "ottery.h"
#include "ref.h"
#include "logger.h"

#ifdef HAVE_SODIUM
#include <sodium.h>
Expand Down Expand Up @@ -78,6 +79,7 @@ struct rdns_curve_ctx {
struct rdns_curve_request *requests;
double key_refresh_interval;
void *key_refresh_event;
struct rdns_resolver *resolver;
};

static struct rdns_curve_client_key *
Expand Down Expand Up @@ -225,8 +227,10 @@ static void
rdns_curve_refresh_key_callback (void *user_data)
{
struct rdns_curve_ctx *ctx = user_data;
struct rdns_resolver *resolver;

DNS_DEBUG ("refresh curve keys");
resolver = ctx->resolver;
rdns_info ("refresh dnscurve keys");
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);
Expand Down Expand Up @@ -259,6 +263,7 @@ rdns_curve_register_plugin (struct rdns_resolver *resolver,
resolver->async->data, ctx->key_refresh_interval,
rdns_curve_refresh_key_callback, ctx);
}
ctx->resolver = resolver;
rdns_resolver_register_plugin (resolver, plugin);
}
}
Expand Down Expand Up @@ -348,7 +353,9 @@ rdns_curve_recv (struct rdns_io_channel *ioc, void *buf, size_t len, void *plugi
unsigned char *p, *box;
unsigned char enonce[crypto_box_NONCEBYTES];
struct rdns_curve_request *creq;
struct rdns_resolver *resolver;

resolver = ctx->resolver;
ret = read (ioc->sock, buf, len);

if (ret <= 0 || ret < 64) {
Expand All @@ -361,7 +368,7 @@ rdns_curve_recv (struct rdns_io_channel *ioc, void *buf, size_t len, void *plugi
p = ((unsigned char *)buf) + 8;
HASH_FIND (hh, ctx->requests, p, 12, creq);
if (creq == NULL) {
DNS_DEBUG ("rdns_curve_recv: unable to find nonce");
rdns_info ("unable to find nonce in the internal hash");
return ret;
}
memcpy (enonce, p, crypto_box_NONCEBYTES);
Expand All @@ -384,7 +391,7 @@ rdns_curve_recv (struct rdns_io_channel *ioc, void *buf, size_t len, void *plugi
*req_out = creq->req;
}
else {
DNS_DEBUG ("rdns_curve_recv: unable open cryptobox of size %d", (int)boxlen);
rdns_info ("unable open cryptobox of size %d", (int)boxlen);
}
free (box);
}
Expand Down
8 changes: 5 additions & 3 deletions src/dns_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include "upstream.h"
#include "ref.h"

#define DNS_DEBUG(...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0);

static const unsigned base = 36;
static const unsigned t_min = 1;
static const unsigned t_max = 26;
Expand All @@ -56,7 +54,7 @@ static const int default_io_cnt = 8;
* Represents DNS server
*/
struct rdns_server {
char *name; /**< name of DNS server */
char *name;
unsigned int port;
unsigned int io_cnt;

Expand Down Expand Up @@ -123,6 +121,10 @@ struct rdns_resolver {

struct rdns_plugin *curve_plugin;

rdns_log_function logger;
void *log_data;
enum rdns_log_level log_level;

bool async_binded;
bool initialized;
ref_entry_t ref;
Expand Down
53 changes: 53 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright (c) 2014, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include "dns_private.h"
#include "logger.h"

void
rdns_logger_internal (void *log_data, enum rdns_log_level level,
const char *function, const char *format,
va_list args)
{
struct rdns_resolver *resolver = log_data;

if (level >= resolver->log_level) {
fprintf (stderr, "rdns: %s: ", function);
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
}
}

void rdns_logger_helper (struct rdns_resolver *resolver,
enum rdns_log_level level,
const char *function, const char *format, ...)
{
va_list va;

if (resolver->logger != NULL) {
va_start (va, format);
resolver->logger (resolver->log_data, level, function, format, va);
va_end (va);
}
}
42 changes: 42 additions & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright (c) 2014, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LOGGER_H_
#define LOGGER_H_

#include <stdarg.h>
#include "dns_private.h"

void rdns_logger_internal (void *log_data, enum rdns_log_level level,
const char *function, const char *format,
va_list args);

void rdns_logger_helper (struct rdns_resolver *resolver,
enum rdns_log_level level,
const char *function, const char *format, ...);

#define rdns_err(...) do { rdns_logger_helper (resolver, RDNS_LOG_ERROR, __FUNCTION__, __VA_ARGS__); } while (0)
#define rdns_warn(...) do { rdns_logger_helper (resolver, RDNS_LOG_WARNING, __FUNCTION__, __VA_ARGS__); } while (0)
#define rdns_info(...) do { rdns_logger_helper (resolver, RDNS_LOG_INFO, __FUNCTION__, __VA_ARGS__); } while (0)
#define rdns_debug(...) do { rdns_logger_helper (resolver, RDNS_LOG_DEBUG, __FUNCTION__, __VA_ARGS__); } while (0)

#endif /* LOGGER_H_ */
14 changes: 8 additions & 6 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "punycode.h"
#include "packet.h"
#include "util.h"
#include "logger.h"

void
rdns_allocate_packet (struct rdns_request* req, unsigned int namelen)
Expand Down Expand Up @@ -92,6 +93,7 @@ rdns_format_dns_name (struct rdns_request *req, const char *name, unsigned int n
uint32_t *uclabel;
size_t punylabel_len, uclabel_len;
uint8_t tmp_label[DNS_D_MAXLABEL];
struct rdns_resolver *resolver = req->resolver;

if (namelen == 0) {
namelen = strlen (name);
Expand Down Expand Up @@ -126,16 +128,16 @@ rdns_format_dns_name (struct rdns_request *req, const char *name, unsigned int n
else {
if (dot) {
if (label_len > DNS_D_MAXLABEL) {
DNS_DEBUG ("dns name component is longer than 63 bytes, should be stripped");
rdns_info ("dns name component is longer than 63 bytes, should be stripped");
label_len = DNS_D_MAXLABEL;
}
if (remain < label_len + 1) {
label_len = remain - 1;
DNS_DEBUG ("no buffer remain for constructing query, strip to %ud", label_len);
rdns_info ("no buffer remain for constructing query, strip to %d", (int)label_len);
}
if (label_len == 0) {
/* Two dots in order, skip this */
DNS_DEBUG ("name contains two or more dots in a row, replace it with one dot");
rdns_info ("name contains two or more dots in a row, replace it with one dot");
begin = dot + 1;
continue;
}
Expand All @@ -151,12 +153,12 @@ rdns_format_dns_name (struct rdns_request *req, const char *name, unsigned int n
break;
}
if (label_len > DNS_D_MAXLABEL) {
DNS_DEBUG ("dns name component is longer than 63 bytes, should be stripped");
rdns_info ("dns name component is longer than 63 bytes, should be stripped");
label_len = DNS_D_MAXLABEL;
}
if (remain < label_len + 1) {
label_len = remain - 1;
DNS_DEBUG ("no buffer remain for constructing query, strip to %ud", label_len);
rdns_info ("no buffer remain for constructing query, strip to %d", (int)label_len);
}
*pos++ = (uint8_t)label_len;
memcpy (pos, begin, label_len);
Expand All @@ -165,7 +167,7 @@ rdns_format_dns_name (struct rdns_request *req, const char *name, unsigned int n
}
}
if (remain == 0) {
DNS_DEBUG ("no buffer space available, aborting");
rdns_warn ("no buffer space available, aborting");
break;
}
}
Expand Down
Loading

0 comments on commit 4aa1511

Please sign in to comment.