Commit 818a04d8 authored by Vladimír Čunát's avatar Vladimír Čunát

Merge !709: misc -Wpedantic fixes, including a class of bugs

parents f121fe55 9969ee4f
......@@ -12,6 +12,7 @@ Module API changes
- kr_request keeps ::qsource.packet beyond the begin layer
- kr_request::qsource.tcp renamed to ::qsource.flags.tcp
- kr_request::has_tls renamed to ::qsource.flags.tls
- kr_zonecut_add() and kr_zonecut_del() changed parameters slightly
Bugfixes
--------
......
......@@ -1172,9 +1172,9 @@ void emit_string(SB *out, const char *str)
strcpy(b, "\\uFFFD");
b += 6;
} else {
*b++ = 0xEF;
*b++ = 0xBF;
*b++ = 0xBD;
*b++ = (char)0xEF;
*b++ = (char)0xBF;
*b++ = (char)0xBD;
}
s++;
} else if (c < 0x1F || (c >= 0x80 && escape_unicode)) {
......
......@@ -118,7 +118,7 @@
* === Basic logging functions (see <<log:,Logging>> and <ucw/log.h> for more)
***/
#define DBG(x,y...) do { } while(0)
#define DBG(x, ...) do { } while(0)
#define DBG_SPOT do { } while(0)
#define ASSERT(x)
......
......@@ -14,6 +14,9 @@
#include <stdio.h>
#include <string.h>
/* FIXME: migrate to Knot DNS version of mempools. */
#pragma GCC diagnostic ignored "-Wpointer-arith"
static char *
mp_vprintf_at(struct mempool *mp, size_t ofs, const char *fmt, va_list args)
{
......
......@@ -18,6 +18,9 @@
#include <string.h>
#include <stdlib.h>
/* FIXME: migrate to Knot DNS version of mempools. */
#pragma GCC diagnostic ignored "-Wpointer-arith"
#define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN)
#define MP_SIZE_MAX (SIZE_MAX - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
......
......@@ -258,6 +258,8 @@ int engine_set_moduledir(struct engine *engine, const char *moduledir) {
/* Use module path for including Lua scripts */
char l_paths[MAXPATHLEN] = { 0 };
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat" /* %1$ is not in C standard */
/* Save original package.path to package._path */
snprintf(l_paths, MAXPATHLEN - 1,
"if package._path == nil then package._path = package.path end\n"
......@@ -265,6 +267,7 @@ int engine_set_moduledir(struct engine *engine, const char *moduledir) {
"if package._cpath == nil then package._cpath = package.cpath end\n"
"package.cpath = '%1$s/?%2$s;'..package._cpath\n",
new_moduledir, LIBEXT);
#pragma GCC diagnostic pop
int ret = l_dobytecode(engine->L, l_paths, strlen(l_paths), "");
if (ret != 0) {
......@@ -377,9 +380,7 @@ static void roothints_add(zs_scanner_t *zs)
return;
}
if (zs->r_type == KNOT_RRTYPE_A || zs->r_type == KNOT_RRTYPE_AAAA) {
knot_rdata_t rdata[RDATA_ARR_MAX];
knot_rdata_init(rdata, zs->r_data_length, zs->r_data);
kr_zonecut_add(hints, zs->r_owner, rdata);
kr_zonecut_add(hints, zs->r_owner, zs->r_data, zs->r_data_length);
}
}
const char* engine_hint_root_file(struct kr_context *ctx, const char *file)
......@@ -601,6 +602,8 @@ static int l_trampoline(lua_State *L)
args = lua_tostring(L, 1);
}
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" /* void* vs. function pointer */
if (callback == module->config) {
module->config(module, args);
} else {
......@@ -618,6 +621,7 @@ static int l_trampoline(lua_State *L)
json_delete(root_node);
return 1;
}
#pragma GCC diagnostic pop
/* No results */
return 0;
......@@ -914,6 +918,8 @@ void engine_stop(struct engine *engine)
/** Register module properties in Lua environment, if any. */
static int register_properties(struct engine *engine, struct kr_module *module)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" /* casts in lua_pushlightuserdata() */
if (!module->config && !module->props) {
return kr_ok();
}
......@@ -929,6 +935,7 @@ static int register_properties(struct engine *engine, struct kr_module *module)
}
}
lua_setglobal(engine->L, module->name);
#pragma GCC diagnostic pop
/* Register module in Lua env */
lua_getglobal(engine->L, "modules_register");
......
......@@ -311,7 +311,7 @@ struct sockaddr *kr_straddr_socket(const char *, int);
int kr_ranked_rrarray_add(ranked_rr_array_t *, const knot_rrset_t *, uint8_t, _Bool, uint32_t, knot_mm_t *);
void kr_qflags_set(struct kr_qflags *, struct kr_qflags);
void kr_qflags_clear(struct kr_qflags *, struct kr_qflags);
int kr_zonecut_add(struct kr_zonecut *, const knot_dname_t *, const knot_rdata_t *);
int kr_zonecut_add(struct kr_zonecut *, const knot_dname_t *, const void *, int);
_Bool kr_zonecut_is_empty(struct kr_zonecut *);
void kr_zonecut_set(struct kr_zonecut *, const knot_dname_t *);
uint64_t kr_now();
......
......@@ -41,15 +41,15 @@
/** @internal Debugging facility. */
#ifdef DEBUG
#define DEBUG_MSG(fmt...) kr_log_verbose("[tls] " fmt)
#define DEBUG_MSG(...) kr_log_verbose("[tls] " __VA_ARGS__)
#else
#define DEBUG_MSG(fmt...)
#define DEBUG_MSG(...)
#endif
struct async_write_ctx {
uv_write_t write_req;
struct tls_common_ctx *t;
char buf[0];
char buf[];
};
static char const server_logstring[] = "tls";
......@@ -731,7 +731,7 @@ void tls_credentials_free(struct tls_credentials *tls_credentials) {
static int client_paramlist_entry_free(struct tls_client_paramlist_entry *entry)
{
DEBUG_MSG("freeing TLS parameters %p\n", entry);
DEBUG_MSG("freeing TLS parameters %p\n", (void *)entry);
while (entry->ca_files.len > 0) {
if (entry->ca_files.at[0] != NULL) {
......
......@@ -54,7 +54,7 @@
#define MAX_PIPELINED 100
#endif
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "wrkr", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "wrkr", __VA_ARGS__)
/** Client request state. */
struct request_ctx
......@@ -1717,7 +1717,7 @@ int worker_task_finalize(struct qr_task *task, int state)
void worker_task_complete(struct qr_task *task)
{
return qr_task_complete(task);
qr_task_complete(task);
}
void worker_task_ref(struct qr_task *task)
......
......@@ -58,7 +58,7 @@
#include "lib/generic/map.h"
#include "lib/generic/array.h"
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "zimport", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "zimport", __VA_ARGS__)
/* Pause between parse and import stages, milliseconds.
* See comment in zi_zone_import() */
......
......@@ -391,7 +391,7 @@ int nsec3_src_synth(struct key *k, struct answer *ans, const knot_dname_t *clenc
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE((qry), "cach", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE((qry), "cach", ## __VA_ARGS__)
/** Shorthand for operations on cache backend */
#define cache_op(cache, op, ...) (cache)->api->op((cache)->db, ## __VA_ARGS__)
......
......@@ -256,7 +256,7 @@ static const char * find_leq_NSEC1(struct kr_cache *cache, const struct kr_query
};
assert((ssize_t)(kwz_sname.len) >= 0);
bool covers = /* we know for sure that the low end is before kwz_sname */
3 == kwz_between((knot_db_val_t){}, kwz_sname, kwz_hi);
3 == kwz_between((knot_db_val_t){ NULL, 0 }, kwz_sname, kwz_hi);
if (!covers) {
return "range search miss (!covers)";
}
......
......@@ -129,7 +129,7 @@ static inline void empty_root(node_t *root) {
static void assert_portability(void) {
#if FLAGS_HACK
assert(((union node){ .leaf = {
.key = ((void *)NULL) + 1,
.key = (tkey_t *)(((uint8_t *)NULL) + 1),
.val = NULL
} }).branch.flags == 1);
#endif
......
......@@ -19,22 +19,22 @@
#include "lib/defines.h"
#include "lib/utils.h"
#ifndef NOVERBOSELOG
/** @internal Print a debug message related to resolution. */
#define QRVERBOSE(query, cls, fmt, ...) { \
const struct kr_query *q = (query); \
if (kr_log_trace_enabled(q)) { \
kr_log_trace(q, cls, fmt, ## __VA_ARGS__); \
} else WITH_VERBOSE(q) { \
unsigned _ind = 0; \
uint32_t _q_uid = q ? q->uid : 0; \
uint32_t _req_uid = q && q->request ? q->request->uid : 0; \
for (; q; q = q->parent, _ind += 2); \
kr_log_verbose("[%05u.%02u][%s] %*s" fmt, _req_uid, _q_uid, cls, _ind, "", ## __VA_ARGS__); \
} \
}
#ifdef NOVERBOSELOG
#define QRVERBOSE(query, cls, ...)
#else
#define QRVERBOSE(query, cls, fmt, ...)
/** Print a debug message related to resolution.
* \param _query associated kr_query, may be NULL
* \param _cls identifying string, typically of length exactly four (padded)
* \param ... printf-compatible list of parameters
*/
#define QRVERBOSE(_query, _cls, ...) do { \
const struct kr_query *_qry = (_query); \
if (kr_log_trace_enabled(_qry)) { \
kr_log_trace(_qry, (_cls), __VA_ARGS__); \
} else if (VERBOSE_STATUS) { \
kr_log_qverbose_impl(_qry, (_cls), __VA_ARGS__); \
} \
} while (false)
#endif
/** Layer processing states. Only one value at a time (but see TODO).
......
......@@ -44,8 +44,8 @@
#include "lib/module.h"
#include "lib/dnssec/ta.h"
#define VERBOSE_MSG(fmt...) QRVERBOSE(req->current_query, "iter", fmt)
#define QVERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "iter", fmt)
#define VERBOSE_MSG(...) QRVERBOSE(req->current_query, "iter", __VA_ARGS__)
#define QVERBOSE_MSG(qry, ...) QRVERBOSE(qry, "iter", __VA_ARGS__)
/* Iterator often walks through packet section, this is an abstraction. */
typedef int (*rr_callback_t)(const knot_rrset_t *, unsigned, struct kr_request *);
......@@ -180,7 +180,7 @@ static int update_nsaddr(const knot_rrset_t *rr, struct kr_query *query, int *gl
"'%s': '%s'\n", name_str, addr_str);
return KR_STATE_CONSUME; /* Ignore invalid addresses */
}
int ret = kr_zonecut_add(&query->zone_cut, rr->owner, rdata);
int ret = kr_zonecut_add(&query->zone_cut, rr->owner, rdata->data, rdata->len);
if (ret != 0) {
return KR_STATE_FAIL;
}
......@@ -309,7 +309,7 @@ static int update_cut(knot_pkt_t *pkt, const knot_rrset_t *rr,
}
continue;
}
int ret = kr_zonecut_add(cut, ns_name, NULL);
int ret = kr_zonecut_add(cut, ns_name, NULL, 0);
assert(!ret); (void)ret;
/* Choose when to use glue records. */
......@@ -690,7 +690,7 @@ static int process_answer(knot_pkt_t *pkt, struct kr_request *req)
const knot_pktsection_t *ans = knot_pkt_section(pkt, KNOT_ANSWER);
if ((pkt_class & (PKT_NOERROR)) && ans->count > 0 &&
knot_dname_is_equal(pkt_qname, query->zone_cut.name)) {
VERBOSE_MSG("<= continuing with qname minimization\n")
VERBOSE_MSG("<= continuing with qname minimization\n");
} else {
/* fall back to disabling minimization */
VERBOSE_MSG("<= retrying with non-minimized name\n");
......
......@@ -36,7 +36,7 @@
#include "lib/defines.h"
#include "lib/module.h"
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "vldr", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "vldr", __VA_ARGS__)
#define MAX_REVALIDATION_CNT 2
......
......@@ -81,6 +81,8 @@ const struct kr_module * kr_module_embedded(const char *name)
/** Load C module symbols. */
static int load_sym_c(struct kr_module *module, uint32_t api_required)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" /* casts after load_symbol() */
/* Check if it's embedded first */
const struct kr_module *embedded = kr_module_embedded(module->name);
if (embedded) {
......@@ -114,6 +116,7 @@ static int load_sym_c(struct kr_module *module, uint32_t api_required)
#undef ML
return kr_ok();
#pragma GCC diagnostic pop
}
int kr_module_load(struct kr_module *module, const char *name, const char *path)
......
......@@ -37,7 +37,7 @@
#define KNOT_EDNS_OPTION_COOKIE 10
#endif /* defined(ENABLE_COOKIES) */
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE((qry), "resl", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE((qry), "resl", __VA_ARGS__)
bool kr_rank_check(uint8_t rank)
{
......@@ -161,11 +161,8 @@ static int invalidate_ns(struct kr_rplan *rplan, struct kr_query *qry)
{
if (qry->ns.addr[0].ip.sa_family != AF_UNSPEC) {
const char *addr = kr_inaddr(&qry->ns.addr[0].ip);
size_t addr_len = kr_inaddr_len(&qry->ns.addr[0].ip);
/* @warning _NOT_ thread-safe */
static knot_rdata_t rdata_arr[RDATA_ARR_MAX];
knot_rdata_init(rdata_arr, addr_len, (const uint8_t *)addr);
return kr_zonecut_del(&qry->zone_cut, qry->ns.name, rdata_arr);
int addr_len = kr_inaddr_len(&qry->ns.addr[0].ip);
return kr_zonecut_del(&qry->zone_cut, qry->ns.name, addr, addr_len);
} else {
return kr_zonecut_del_all(&qry->zone_cut, qry->ns.name);
}
......
......@@ -23,7 +23,7 @@
#include "lib/defines.h"
#include "lib/layer.h"
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "plan", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "plan", __VA_ARGS__)
#define QUERY_PROVIDES(q, name, cls, type) \
((q)->sclass == (cls) && (q)->stype == type && knot_dname_is_equal((q)->sname, name))
......
......@@ -133,6 +133,23 @@ void kr_log_verbose(const char *fmt, ...)
}
}
void kr_log_qverbose_impl(const struct kr_query *qry, const char *cls, const char *fmt, ...)
{
unsigned ind = 0;
for (const struct kr_query *q = qry; q; q = q->parent)
ind += 2;
uint32_t qry_uid = qry ? qry->uid : 0;
uint32_t req_uid = qry && qry->request ? qry->request->uid : 0;
/* Simplified kr_log_verbose() calls, first prefix then passed fmt...
* Calling it would take about the same amount of code. */
printf("[%05u.%02u][%s] %*s", req_uid, qry_uid, cls, ind, "");
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
bool kr_log_trace(const struct kr_query *query, const char *source, const char *fmt, ...)
{
if (!kr_log_trace_enabled(query)) {
......@@ -327,12 +344,10 @@ int kr_pkt_put(knot_pkt_t *pkt, const knot_dname_t *name, uint32_t ttl,
/* Create empty RR */
knot_rrset_t rr;
knot_rrset_init(&rr, knot_dname_copy(name, &pkt->mm), rtype, rclass, ttl);
/* Create RDATA
* @warning _NOT_ thread safe.
*/
static knot_rdata_t rdata_arr[RDATA_ARR_MAX];
knot_rdata_init(rdata_arr, rdlen, rdata);
knot_rdataset_add(&rr.rrs, rdata_arr, &pkt->mm);
/* Create RDATA */
knot_rdata_t *rdata_tmp = mm_malloc(&pkt->mm, offsetof(knot_rdata_t, data) + rdlen);
knot_rdata_init(rdata_tmp, rdlen, rdata);
knot_rdataset_add(&rr.rrs, rdata_tmp, &pkt->mm);
/* Append RR */
return knot_pkt_put(pkt, 0, &rr, KNOT_PF_FREE);
}
......
......@@ -42,8 +42,8 @@ typedef void (*trace_callback_f)(struct kr_request *request);
/** @brief Callback for request logging handler. */
typedef void (*trace_log_f)(const struct kr_query *query, const char *source, const char *msg);
#define kr_log_info(fmt, ...) do { printf((fmt), ## __VA_ARGS__); fflush(stdout); } while(0)
#define kr_log_error(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__)
#define kr_log_info(...) do { printf(__VA_ARGS__); fflush(stdout); } while(0)
#define kr_log_error(...) fprintf(stderr, ## __VA_ARGS__)
/* Always export these, but override direct calls by macros conditionally. */
/** Whether in --verbose mode. Only use this for reading. */
......@@ -56,10 +56,16 @@ KR_EXPORT bool kr_verbose_set(bool status);
KR_EXPORT KR_PRINTF(1)
void kr_log_verbose(const char *fmt, ...);
/** Utility for QRVERBOSE - use that instead. */
KR_EXPORT KR_PRINTF(3)
void kr_log_qverbose_impl(const struct kr_query *qry, const char *cls, const char *fmt, ...);
/**
* @brief Return true if the query has request log handler installed.
*/
#define kr_log_trace_enabled(query) ((query) && (query)->request && (query)->request->trace_log)
#define kr_log_trace_enabled(query) (__builtin_expect( \
(query) && (query)->request && (query)->request->trace_log, \
false))
/**
* Log a message through the request log handler.
......@@ -168,9 +174,6 @@ typedef struct ranked_rr_array_entry ranked_rr_array_entry_t;
typedef array_t(ranked_rr_array_entry_t *) ranked_rr_array_t;
/* @endcond */
/** @internal RDATA array maximum size. */
#define RDATA_ARR_MAX (UINT16_MAX + sizeof(uint64_t))
/** Concatenate N strings. */
KR_EXPORT
char* kr_strcatdup(unsigned n, ...);
......@@ -424,7 +427,7 @@ static inline int kr_dname_lf(uint8_t *dst, const knot_dname_t *src, bool add_wi
}
dst[0] = len;
return KNOT_EOK;
};
}
/* Trivial non-inline wrappers, to be used in lua. */
KR_EXPORT void kr_rrset_init(knot_rrset_t *rrset, knot_dname_t *owner,
......
......@@ -27,7 +27,7 @@
#include <libknot/packet/wire.h>
#include <libknot/rrtype/rdname.h>
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "zcut", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "zcut", __VA_ARGS__)
/** Information for one NS name + address type. */
typedef enum {
......@@ -174,15 +174,15 @@ int kr_zonecut_copy_trust(struct kr_zonecut *dst, const struct kr_zonecut *src)
return kr_ok();
}
int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rdata_t *rdata)
int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const void *data, int len)
{
if (!cut || !ns || !cut->nsset) {
if (!cut || !ns || !cut->nsset || (data && len <= 0)) {
assert(!EINVAL);
return kr_error(EINVAL);
}
/* Disabled; add_reverse_pair() misuses this for domain name in rdata. */
if (false && rdata && rdata->len != sizeof(struct in_addr)
&& rdata->len != sizeof(struct in6_addr)) {
if (false && data && len != sizeof(struct in_addr)
&& len != sizeof(struct in6_addr)) {
assert(!EINVAL);
return kr_error(EINVAL);
}
......@@ -196,24 +196,24 @@ int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd
pack_init(**pack);
}
/* Insert data (if has any) */
if (rdata == NULL) {
if (data == NULL) {
return kr_ok();
}
/* Check for duplicates */
if (pack_obj_find(*pack, rdata->data, rdata->len)) {
if (pack_obj_find(*pack, data, len)) {
return kr_ok();
}
/* Push new address */
int ret = pack_reserve_mm(**pack, 1, rdata->len, kr_memreserve, cut->pool);
int ret = pack_reserve_mm(**pack, 1, len, kr_memreserve, cut->pool);
if (ret != 0) {
return kr_error(ENOMEM);
}
return pack_obj_push(*pack, rdata->data, rdata->len);
return pack_obj_push(*pack, data, len);
}
int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rdata_t *rdata)
int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const void *data, int len)
{
if (!cut || !ns) {
if (!cut || !ns || (data && len <= 0)) {
return kr_error(EINVAL);
}
......@@ -224,8 +224,8 @@ int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rd
return kr_error(ENOENT);
}
/* Remove address from the pack. */
if (rdata) {
ret = pack_obj_del(pack, rdata->data, rdata->len);
if (data) {
ret = pack_obj_del(pack, data, len);
}
/* No servers left, remove NS from the set. */
if (pack->len == 0) {
......
......@@ -98,21 +98,23 @@ int kr_zonecut_copy_trust(struct kr_zonecut *dst, const struct kr_zonecut *src);
*
* @param cut zone cut to be populated
* @param ns nameserver name
* @param rdata nameserver address (as rdata)
* @param data typically knot_rdata_t::data
* @param len typically knot_rdata_t::len
* @return 0 or error code
*/
KR_EXPORT
int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rdata_t *rdata);
int kr_zonecut_add(struct kr_zonecut *cut, const knot_dname_t *ns, const void *data, int len);
/**
* Delete nameserver/address pair from the zone cut.
* @param cut
* @param ns name server name
* @param rdata name server address
* @param data typically knot_rdata_t::data
* @param len typically knot_rdata_t::len
* @return 0 or error code
*/
KR_EXPORT
int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const knot_rdata_t *rdata);
int kr_zonecut_del(struct kr_zonecut *cut, const knot_dname_t *ns, const void *data, int len);
/**
* Delete all addresses associated with the given name.
......
......@@ -136,7 +136,7 @@ As almost all the functions are optional, the minimal module looks like this:
#include "lib/module.h"
/* Convenience macro to declare module API. */
KR_MODULE_EXPORT(mymodule);
KR_MODULE_EXPORT(mymodule)
Let's define an observer thread for the module as well. It's going to be stub for the sake of brevity,
......
......@@ -35,4 +35,4 @@ const kr_layer_api_t *bogus_log_layer(struct kr_module *module)
return &_layer;
}
KR_MODULE_EXPORT(bogus_log);
KR_MODULE_EXPORT(bogus_log)
......@@ -33,7 +33,7 @@
#include "lib/rplan.h"
#include "modules/cookies/cookiemonster.h"
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "cookies", fmt)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "cookies", __VA_ARGS__)
/**
* Obtain address from query/response context if if can be obtained.
......
......@@ -97,4 +97,4 @@ struct kr_prop *cookies_props(void)
return prop_list;
}
KR_MODULE_EXPORT(cookies);
KR_MODULE_EXPORT(cookies)
......@@ -378,5 +378,5 @@ const kr_layer_api_t *dnstap_layer(struct kr_module *module) {
return &_layer;
}
KR_MODULE_EXPORT(dnstap);
KR_MODULE_EXPORT(dnstap)
......@@ -73,5 +73,5 @@ const kr_layer_api_t *edns_keepalive_layer(struct kr_module *module)
return &_layer;
}
KR_MODULE_EXPORT(edns_keepalive);
KR_MODULE_EXPORT(edns_keepalive)
......@@ -34,8 +34,8 @@
#include "lib/layer.h"
/* Defaults */
#define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "hint", fmt)
#define ERR_MSG(fmt, ...) kr_log_error("[ ][hint] " fmt, ## __VA_ARGS__)
#define VERBOSE_MSG(qry, ...) QRVERBOSE(qry, "hint", __VA_ARGS__)
#define ERR_MSG(...) kr_log_error("[ ][hint] " __VA_ARGS__)
struct hints_data {
struct kr_zonecut hints;
......@@ -167,34 +167,18 @@ static int query(kr_layer_t *ctx, knot_pkt_t *pkt)
return KR_STATE_DONE;
}
static int parse_addr_str(struct sockaddr_storage *sa, const char *addr)
static int parse_addr_str(union inaddr *sa, const char *addr)
{
int family = strchr(addr, ':') ? AF_INET6 : AF_INET;
memset(sa, 0, sizeof(struct sockaddr_storage));
sa->ss_family = family;
char *addr_bytes = (char *)kr_inaddr((struct sockaddr *)sa);
memset(sa, 0, sizeof(*sa));
sa->ip.sa_family = family;
char *addr_bytes = (/*const*/char *)kr_inaddr(&sa->ip);
if (inet_pton(family, addr, addr_bytes) < 1) {
return kr_error(EILSEQ);
}
return 0;
}
/** @warning _NOT_ thread-safe; returns a pointer to static data! */
static const knot_rdata_t * addr2rdata(const char *addr) {
/* Parse address string */
struct sockaddr_storage ss;
if (parse_addr_str(&ss, addr) != 0) {
return NULL;
}
/* Build RDATA */
static knot_rdata_t rdata_arr[RDATA_ARR_MAX];
size_t addr_len = kr_inaddr_len((struct sockaddr *)&ss);
const uint8_t *raw_addr = (const uint8_t *)kr_inaddr((struct sockaddr *)&ss);
knot_rdata_init(rdata_arr, addr_len, raw_addr);
return rdata_arr;
}
/** @warning _NOT_ thread-safe; returns a pointer to static data! */
static const knot_dname_t * raw_addr2reverse(const uint8_t *raw_addr, int family)
{
......@@ -233,18 +217,15 @@ static const knot_dname_t * raw_addr2reverse(const uint8_t *raw_addr, int family
return dname;
}
/** @warning _NOT_ thread-safe; returns a pointer to static data! */
static const knot_dname_t * addr2reverse(const char *addr)
{
/* Parse address string */
struct sockaddr_storage ss;
if (parse_addr_str(&ss, addr) != 0) {
union inaddr ia;
if (parse_addr_str(&ia, addr) != 0) {
return NULL;
}
const struct sockaddr *sa = (const struct sockaddr *)&ss;
const uint8_t *raw_addr = (const uint8_t *)kr_inaddr(sa);
int family = kr_inaddr_family(sa);
return raw_addr2reverse(raw_addr, family);
return raw_addr2reverse((const /*sign*/uint8_t *)kr_inaddr(&ia.ip),
kr_inaddr_family(&ia.ip));
}
static int add_pair(struct kr_zonecut *hints, const char *name, const char *addr)
......@@ -255,12 +236,13 @@ static int add_pair(struct kr_zonecut *hints, const char *name, const char *addr
return kr_error(EINVAL);
}
knot_dname_to_lower(key);
const knot_rdata_t *rdata = addr2rdata(addr);
if (!rdata) {
union inaddr ia;
if (parse_addr_str(&ia, addr) != 0) {
return kr_error(EINVAL);
}
return kr_zonecut_add(hints, key, rdata);
return kr_zonecut_add(hints, key, kr_inaddr(&ia.ip), kr_inaddr_len(&ia.ip));
}
static int add_reverse_pair(struct kr_zonecut *hints, const char *name, const char *addr)
......@@ -276,11 +258,7 @@ static int add_reverse_pair(struct kr_zonecut *hints, const char *name, const ch
return kr_error(EINVAL);
}
/* Build RDATA */
knot_rdata_t rdata[RDATA_ARR_MAX];
knot_rdata_init(rdata, knot_dname_size(ptr_name), ptr_name);
return kr_zonecut_add(hints, key, rdata);
return kr_zonecut_add(hints, key, ptr_name, knot_dname_size(ptr_name));
}
/** For a given name, remove either one address or all of them (if == NULL).
......@@ -294,18 +272,19 @@ static int del_pair(struct hints_data *data, const char *name, const char *addr)
if (!knot_dname_from_str(key, name, sizeof(key))) {
return kr_error(EINVAL);
}
knot_rdata_t ptr_rdata[RDATA_ARR_MAX];
knot_rdata_init(ptr_rdata, knot_dname_size(key), key);
int key_len = knot_dname_size(key);
if (addr) {
/* Remove the pair. */
const knot_rdata_t *rdata = addr2rdata(addr);
if (!rdata) {
union inaddr ia;
if (parse_addr_str(&ia, addr) != 0) {
return kr_error(EINVAL);
}
const knot_dname_t *reverse_key = addr2reverse(addr);
kr_zonecut_del(&data->reverse_hints, reverse_key, ptr_rdata);
return kr_zonecut_del(&data->hints, key, rdata);
kr_zonecut_del(&data->reverse_hints, reverse_key, key, key_len);
return kr_zonecut_del(&data->hints, key,
kr_inaddr(&ia.ip), kr_inaddr_len(&ia.ip));
} else {
/* Find a matching name */
pack_t *addr_set = kr_zonecut_find(&data->hints, key);
......@@ -321,7 +300,7 @@ static int del_pair(struct hints_data *data, const char *name, const char *addr)
? AF_INET : AF_INET6;
const knot_dname_t *reverse_key = raw_addr2reverse(addr_val, family);
if (reverse_key != NULL) {
kr_zonecut_del(&data->reverse_hints, reverse_key, ptr_rdata);
kr_zonecut_del(&data->reverse_hints, reverse_key, key, key_len);
}
addr = pack_obj_next(addr);
}
......@@ -682,6 +661,6 @@ struct kr_prop *hints_props(void)
return prop_list;
}
KR_MODULE_EXPORT(hints);
KR_MODULE_EXPORT(hints)
#undef VERBOSE_MSG
......@@ -119,4 +119,4 @@ int nsid_deinit(struct kr_module *module) {
return kr_ok();
}
KR_MODULE_EXPORT(nsid);
KR_MODULE_EXPORT(nsid)
......@@ -21,7 +21,8 @@ local function publish_hints(nsset)
ffi.C.kr_zonecut_set(roothints, kres.str2dname("."))
for dname, addrsets in pairs(nsset) do
for i = 0, addrsets:rdcount() - 1 do
ffi.C.kr_zonecut_add(roothints, dname, addrsets:rdata_pt(i))