summaryrefslogtreecommitdiffstats
path: root/utils/cache_gc/categories.c
blob: 19dec45c5a0718e6a2c8f1d38ef5cf53e5c27c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "categories.h"

#include <libknot/libknot.h>
#include "lib/utils.h"

static bool rrtype_is_infrastructure(uint16_t r)
{
	switch (r) {
	case KNOT_RRTYPE_NS:
	case KNOT_RRTYPE_DS:
	case KNOT_RRTYPE_DNSKEY:
	case KNOT_RRTYPE_A:
	case KNOT_RRTYPE_AAAA:
		return true;
	default:
		return false;
	}
}

static int get_random(int to)
{
	// We don't need these to be really unpredictable,
	// but this should be cheap enough not to be noticeable.
	return kr_rand_bytes(1) % to;
}

// TODO this is just an example, make this more clever
category_t kr_gc_categorize(gc_record_info_t * info)
{
	category_t res;

	if (!info->valid)
		return CATEGORIES - 1;

	switch (info->no_labels) {
	case 0:		/* root zone */
		res = 5;
		break;
	case 1:		/* TLD */
		res = 10;
		break;
	default:		/* SLD and below */
		res = (rrtype_is_infrastructure(info->rrtype) ? 15 : 20);
		if (info->entry_size > 300)
			/* Penalty for big answers */
			res += 30;
		break;
	}

	if (info->expires_in <= 0) {
		res += 40;
	}

	return res + get_random(5);
}