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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include <string.h>
#include "cache.h"
static void remove_entry(flxCache *c, flxCacheEntry *e, gboolean remove_from_hash_table) {
g_assert(c);
g_assert(e);
if (remove_from_hash_table) {
flxCacheEntry *t;
t = g_hash_table_lookup(c->hash_table, &e->record->key);
FLX_LLIST_REMOVE(flxCacheEntry, by_name, t, e);
if (t)
g_hash_table_replace(c->hash_table, &t->record->key, t);
else
g_hash_table_remove(c->hash_table, &e->record->key);
}
flx_record_unref(e->record);
g_free(e);
}
flxCache *flx_cache_new(flxServer *server, flxInterface *iface) {
flxCache *c;
g_assert(server);
c = g_new(flxCache, 1);
c->server = server;
c->interface = iface;
c->hash_table = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal);
return c;
}
gboolean remove_func(gpointer key, gpointer value, gpointer user_data) {
flxCacheEntry *e, *next;
for (e = value; e; e = next) {
next = e->by_name_next;
remove_entry(user_data, e, FALSE);
}
return TRUE;
}
void flx_cache_free(flxCache *c) {
g_assert(c);
g_hash_table_foreach_remove(c->hash_table, remove_func, c);
g_hash_table_destroy(c->hash_table);
g_free(c);
}
flxCacheEntry *flx_cache_lookup_key(flxCache *c, flxKey *k) {
g_assert(c);
g_assert(k);
return g_hash_table_lookup(c->hash_table, k);
}
flxCacheEntry *flx_cache_lookup_record(flxCache *c, flxRecord *r) {
flxCacheEntry *e;
g_assert(c);
g_assert(r);
for (e = flx_cache_lookup_key(c, r->key); e; e = e->by_name_next)
if (e->record->size == r->size && !memcmp(e->record->data, r->data, r->size))
return e;
return NULL;
}
flxCacheEntry *flx_cache_update(flxCache *c, flxRecord *r, gboolean unique, const flxAddress *a) {
flxCacheEntry *e, *t;
g_assert(c);
g_assert(r);
if ((t = e = flx_cache_lookup_key(c, r->key))) {
if (unique) {
flxCacheEntry *n;
/* Drop all entries but the first which we replace */
while (e->by_name_next)
remove_entry(c, e->by_name_next, TRUE);
g_free(e->record->data);
e->record->data = g_memdup(r->data, r->size);
e->record->size = r->size;
e->record->ttl = r->ttl;
} else {
/* Look for exactly the same entry */
for (; e; e = e->by_name_next) {
if (e->record->size == r->size &&
!memcmp(e->record->data, r->data, r->size)) {
/* We found it, so let's update the TTL */
e->record->ttl = r->ttl;
break;
}
}
}
}
if (!e) {
/* No entry found, therefore we create a new one */
e = g_new(flxCacheEntry, 1);
e->node = NULL;
e->record = flx_record_ref(r);
FLX_LLIST_PREPEND(flxCacheEntry, by_name, t, e);
g_hash_table_replace(c->hash_table, e->record->key, e);
}
e->origin = *a;
g_get_current_time(&e->timestamp);
e->expiry = e->timestamp;
g_time_val_add(&e->expiry, e->record->ttl * 1000000);
e->state = FLX_CACHE_VALID;
return e;
}
void flx_cache_drop_key(flxCache *c, flxKey *k) {
flxCacheEntry *e;
g_assert(c);
g_assert(k);
while ((e = flx_cache_lookup_key(c, k)))
remove_entry(c, e, TRUE);
}
void flx_cache_drop_record(flxCache *c, flxRecord *r) {
flxCacheEntry *e;
g_assert(c);
g_assert(r);
if ((e = flx_cache_lookup_record(c, r)))
remove_entry(c, e, TRUE);
}
|