nsrep.c 2.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  Copyright (C) 2014 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
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

17
#include "lib/nsrep.h"
18
#include "lib/generic/pack.h"
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/** @internal Macro to set address structure. */
#define ADDR_SET(sa, family, addr, len) do {\
    	memcpy(&sa ## _addr, (addr), (len)); \
    	sa ## _family = (family); \
	sa ## _port = htons(KR_DNS_PORT); \
} while (0)

/** Update nameserver representation with current name/address pair. */
static void update_nsrep(struct kr_nsrep *ns, const knot_dname_t *name, uint8_t *addr, unsigned score)
{
	ns->name = name;
	ns->score = score;
	if (addr == NULL) {
		return;
	}

	size_t len = pack_obj_len(addr);
	void *addr_val = pack_obj_val(addr);
	switch(len) {
	case sizeof(struct in_addr):
		ADDR_SET(ns->addr.ip4.sin, AF_INET, addr_val, len); break;
	case sizeof(struct in6_addr):
		ADDR_SET(ns->addr.ip6.sin6, AF_INET6, addr_val, len); break;
	default: assert(0); break;
	}
}

#undef ADDR_SET

static int eval_nsrep(const char *k, void *v, void *baton)
{
	unsigned score = KR_NS_VALID;
	struct kr_nsrep *ns = baton;
	pack_t *addr_set = v;
	uint8_t *addr = NULL;

	/* Name server is better candidate if it has address record. */
	if (addr_set->len > 0) {
		addr = pack_head(*addr_set);
		score += 1;
	}

	/* Update best scoring nameserver. */
	if (ns->score < score) {
		update_nsrep(ns, (const knot_dname_t *)k, addr, score);
	}

	return kr_ok();
}

int kr_nsrep_elect(struct kr_nsrep *ns, map_t *nsset)
71
{
72 73 74
	ns->addr.ip.sa_family = AF_UNSPEC;
	ns->score = KR_NS_INVALID;
	return map_walk(nsset, eval_nsrep, ns);
75
}