File: categories.c

package info (click to toggle)
knot-resolver 5.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,976 kB
  • sloc: javascript: 42,732; ansic: 34,752; python: 4,225; cpp: 2,110; sh: 1,887; makefile: 199; xml: 193
file content (56 lines) | stat: -rw-r--r-- 1,085 bytes parent folder | download | duplicates (2)
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 unsigned 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);
}