Commit 1d795f51 authored by Mark Karpilovskij's avatar Mark Karpilovskij Committed by Daniel Salzman

modules: move addr_range_match up to module.h

parent 1c4c5908
......@@ -336,6 +336,17 @@ knotd_conf_t knotd_conf_zone(knotd_mod_t *mod, const yp_name_t *item_name,
knotd_conf_t knotd_conf_check_item(knotd_conf_check_args_t *args,
const yp_name_t *item_name);
/*!
* \brief Checks if address is in at least one of given ranges.
*
* \param[in] range
* \param[in] addr
*
* \return true if addr is in at least one range, false otherwise.
*/
bool knotd_conf_addr_range_match(const knotd_conf_t *range,
const struct sockaddr_storage *addr);
/*!
* Deallocates multi-valued configuration values.
*
......
......@@ -14,7 +14,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "contrib/sockaddr.h"
#include "knot/include/module.h"
#include "knot/nameserver/process_query.h" // Dependency on qdata->extra!
#include "knot/modules/rrl/functions.h"
......@@ -75,30 +74,6 @@ static const knot_dname_t *name_from_authrr(const knot_rrset_t *rr)
return rr->owner;
}
static bool addr_range_match(const knotd_conf_t *range, const struct sockaddr_storage *addr)
{
assert(range && addr);
for (size_t i = 0; i < range->count; i++) {
knotd_conf_val_t *val = &range->multi[i];
if (val->addr_max.ss_family == AF_UNSPEC) {
if (sockaddr_net_match((struct sockaddr *)addr,
(struct sockaddr *)&val->addr,
val->addr_mask)) {
return true;
}
} else {
if (sockaddr_range_match((struct sockaddr *)addr,
(struct sockaddr *)&val->addr,
(struct sockaddr *)&val->addr_max)) {
return true;
}
}
}
return false;
}
static knotd_state_t ratelimit_apply(knotd_state_t state, knot_pkt_t *pkt,
knotd_qdata_t *qdata, knotd_mod_t *mod)
{
......@@ -117,7 +92,7 @@ static knotd_state_t ratelimit_apply(knotd_state_t state, knot_pkt_t *pkt,
}
// Exempt clients.
if (addr_range_match(&ctx->whitelist, qdata->params->remote)) {
if (knotd_conf_addr_range_match(&ctx->whitelist, qdata->params->remote)) {
return state;
}
......
......@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <string.h>
#include "contrib/sockaddr.h"
#include "libknot/attribute.h"
#include "knot/common/log.h"
#include "knot/conf/module.h"
......@@ -498,6 +499,34 @@ knotd_conf_t knotd_conf_check_item(knotd_conf_check_args_t *args,
return out;
}
_public_
bool knotd_conf_addr_range_match(const knotd_conf_t *range,
const struct sockaddr_storage *addr)
{
if (range == NULL || addr == NULL) {
return false;
}
for (size_t i = 0; i < range->count; i++) {
knotd_conf_val_t *val = &range->multi[i];
if (val->addr_max.ss_family == AF_UNSPEC) {
if (sockaddr_net_match((struct sockaddr *)addr,
(struct sockaddr *)&val->addr,
val->addr_mask)) {
return true;
}
} else {
if (sockaddr_range_match((struct sockaddr *)addr,
(struct sockaddr *)&val->addr,
(struct sockaddr *)&val->addr_max)) {
return true;
}
}
}
return false;
}
_public_
void knotd_conf_free(knotd_conf_t *conf)
{
......
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