2 * stats.c - various routines to do stats on the key graph
4 * Copyright 2000-2004,2007-2009 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "onak-conf.h"
32 * initcolour - Clear the key graph ready for use.
33 * @parent: Do we want to clear the parent pointers too?
35 * Clears the parent and colour information on all elements in the key
38 void initcolour(bool parent)
44 * Init the colour/parent values. We get each entry list from the hash
45 * table and walk along it, zeroing the values.
47 for (loop = 0; loop < HASHSIZE; loop++) {
48 curkey = gethashtableentry(loop);
49 while (curkey != NULL) {
50 ((struct stats_key *)curkey->object)->colour = 0;
52 ((struct stats_key *)curkey->object)->parent =
55 curkey = curkey->next;
61 * findpath - Given 2 keys finds a path between them.
62 * @have: The key we have.
63 * @want: The key we want to get to.
65 * This does a breadth first search on the key tree, starting with the
66 * key we have. It returns as soon as a path is found or when we run out
67 * of keys; whichever comes sooner.
69 unsigned long findpath(struct stats_key *have, struct stats_key *want)
71 struct ll *keys = NULL;
72 struct ll *oldkeys = NULL;
73 struct ll *sigs = NULL;
74 struct ll *nextkeys = NULL;
76 unsigned long count = 0;
79 keys = lladd(NULL, want);
82 while ((!cleanup()) && keys != NULL && have->colour == 0) {
83 sigs = config.dbbackend->cached_getkeysigs(((struct stats_key *)
84 keys->object)->keyid);
85 while ((!cleanup()) && sigs != NULL && have->colour == 0) {
87 * Check if we've seen this key before and if not mark
88 * it and add its sigs to the list we want to look at.
90 if (!((struct stats_key *)sigs->object)->disabled &&
91 !((struct stats_key *)sigs->object)->revoked &&
92 ((struct stats_key *)sigs->object)->colour == 0) {
94 ((struct stats_key *)sigs->object)->colour =
96 ((struct stats_key *)sigs->object)->parent =
99 nextkeys = lladd(nextkeys, sigs->object);
106 llfree(oldkeys, NULL);
112 if (oldkeys != NULL) {
113 llfree(oldkeys, NULL);
116 if (nextkeys != NULL) {
117 llfree(nextkeys, NULL);
125 * dofindpath - Given 2 keys displays a path between them.
126 * @have: The key we have.
127 * @want: The key we want to get to.
128 * @html: Should we output in html.
129 * @count: How many paths we should look for.
131 * This does a breadth first search on the key tree, starting with the
132 * key we have. It returns as soon as a path is found or when we run out
133 * of keys; whichever comes sooner.
135 void dofindpath(uint64_t have, uint64_t want, bool html, int count)
137 struct stats_key *keyinfoa, *keyinfob, *curkey;
138 uint64_t fullhave, fullwant;
143 fullhave = config.dbbackend->getfullkeyid(have);
144 fullwant = config.dbbackend->getfullkeyid(want);
147 * Make sure the keys we have and want are in the cache.
149 (void) config.dbbackend->cached_getkeysigs(fullhave);
150 (void) config.dbbackend->cached_getkeysigs(fullwant);
152 if ((keyinfoa = findinhash(fullhave)) == NULL) {
153 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
156 if ((keyinfob = findinhash(fullwant)) == NULL) {
157 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
163 while ((!cleanup()) && (pathnum < count)) {
165 * Fill the tree info up.
168 rec = findpath(keyinfoa, keyinfob);
169 keyinfob->parent = 0;
171 printf("%s%d nodes examined. %ld elements in the hash%s\n",
176 if (keyinfoa->colour == 0) {
178 printf("Can't find a link from 0x%08" PRIX64
179 " to 0x%08" PRIX64 "%s\n",
184 printf("Can't find any further paths%s\n",
189 printf("%d steps from 0x%08" PRIX64 " to 0x%08" PRIX64
191 keyinfoa->colour, have & 0xFFFFFFFF,
195 while (curkey != NULL && curkey->keyid != 0) {
196 uid = config.dbbackend->keyid2uid(
198 if (html && uid == NULL) {
199 printf("<a href=\"lookup?op=get&search="
200 "0x%08" PRIX64 "\">0x%08" PRIX64
202 "User id not found])%s<BR>\n",
203 curkey->keyid & 0xFFFFFFFF,
204 curkey->keyid & 0xFFFFFFFF,
205 (curkey->keyid == fullwant) ?
207 } else if (html && uid != NULL) {
208 printf("<a href=\"lookup?op=get&search="
209 "0x%08" PRIX64 "\">0x%08"
211 " (<a href=\"lookup?op=vindex&"
212 "search=0x%08" PRIX64
215 curkey->keyid & 0xFFFFFFFF,
216 curkey->keyid & 0xFFFFFFFF,
217 curkey->keyid & 0xFFFFFFFF,
219 (curkey->keyid == fullwant) ?
222 printf("0x%08" PRIX64 " (%s)%s\n",
223 curkey->keyid & 0xFFFFFFFF,
225 "[User id not found]" :
227 (curkey->keyid == fullwant) ?
234 if (curkey != keyinfoa && curkey != keyinfob) {
235 curkey->disabled = true;
237 curkey = findinhash(curkey->parent);
240 puts("<P>List of key ids in path:</P>");
242 puts("List of key ids in path:");
245 while (curkey != NULL && curkey->keyid != 0) {
246 printf("0x%08" PRIX64 " ",
247 curkey->keyid & 0xFFFFFFFF);
248 curkey = findinhash(curkey->parent);
258 struct stats_key *furthestkey(struct stats_key *have)
260 unsigned long count = 0;
261 unsigned long curdegree = 0;
262 struct ll *curll, *nextll, *tmp;
263 struct ll *sigs = NULL;
264 struct stats_key *max;
274 curll = lladd(NULL, have);
276 while (curll != NULL) {
277 sigs = config.dbbackend->cached_getkeysigs(((struct stats_key *)
278 curll->object)->keyid);
279 while (sigs != NULL) {
280 if (((struct stats_key *) sigs->object)->colour == 0) {
282 * We've never seen it. Count it, mark it and
283 * explore its subtree.
286 max = (struct stats_key *)sigs->object;
287 ((struct stats_key *)sigs->object)->colour =
289 ((struct stats_key *)sigs->object)->parent =
290 ((struct stats_key *)
291 curll->object)->keyid;
293 nextll=lladd(nextll, sigs->object);