File: htkey.c

package info (click to toggle)
yaku-ns 0.2-4
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 432 kB
  • sloc: ansic: 5,176; sh: 81; makefile: 64
file content (78 lines) | stat: -rw-r--r-- 1,855 bytes parent folder | download | duplicates (3)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* htkey.c
 * Translate a Resource Record reference to the hash table key
 *
 * Copyright (C) 2000 Salvatore Sanfilippo
 * Copyright (C) 2001 Salvatore Sanfilippo
 * Copyright (C) 2002 Salvatore Sanfilippo
 * <antirez@invece.org>
 *
 * This code is under the GPL license
 * See the COPYING file for more information
 */

#include "ens.h"
#include <string.h>
#include <ctype.h>

#include "aht.h"

/* Key format:
 *
 * [klen][seq][type][class][name]
 *
 * The key is not nul terminated, fields description:
 *
 * klen:  2 bytes total key length (including itself)
 * seq:   4 bytes sequence number, to distinguish and access to multiple
 *        entries with the same RR type/class/name.
 * type:  RR type
 * class: RR class
 * name:  RR name
 *
 * The format was changed the 2002-09-09. The change was needed to
 * support the new AHT hash-table library */
size_t rr_to_key(char *dest, size_t dsize, char *name, u_int16_t type,
		u_int16_t class, u_int32_t seq)
{
	size_t l = strlen(name);
	u_int16_t ltwo;
	unsigned int i;
	char *p;

	yakuns_assert(dsize >= 33);
	memcpy(dest+2, &seq, 4);
	memcpy(dest+6, &type, 2);
	memcpy(dest+8, &class, 2);
	l = (l > dsize-10) ? (dsize-10) : l;
	memcpy(dest+10, name, l);
	p = dest+10;
	/* put it lowercase -- but note that we save the resource
	 * record with the original case to avoid useless information
	 * leak */
	for (i = 0; i < l; i++)
		p[i] = tolower(p[i]);
	ltwo = l;
	memcpy(dest, &ltwo, 2);
	return 10 + l;
}

/* Compare two keys */
int ht_dnskey_compare(void *key1, void *key2)
{
	u_int16_t k1l, k2l;

	memcpy(&k1l, key1, 2);
	memcpy(&k2l, key2, 2);
	if (k1l != k2l)
		return 0; /* keys of different length can't match */
	return !memcmp(key1, key2, k1l);
}

/* Hash a given key */
u_int32_t ht_dnskey_hash(void *key)
{
	u_int16_t l;

	memcpy(&l, key, 2);
	return ht_strong_hash(key, l, 0x11223344);
}