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.
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 onak_dbctx *dbctx,
70 struct stats_key *have, struct stats_key *want)
72 struct ll *keys = NULL;
73 struct ll *oldkeys = NULL;
74 struct ll *sigs = NULL;
75 struct ll *nextkeys = NULL;
77 unsigned long count = 0;
80 keys = lladd(NULL, want);
83 while ((!cleanup()) && keys != NULL && have->colour == 0) {
84 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
85 keys->object)->keyid);
86 while ((!cleanup()) && sigs != NULL && have->colour == 0) {
88 * Check if we've seen this key before and if not mark
89 * it and add its sigs to the list we want to look at.
91 if (!((struct stats_key *)sigs->object)->disabled &&
92 !((struct stats_key *)sigs->object)->revoked &&
93 ((struct stats_key *)sigs->object)->colour == 0) {
95 ((struct stats_key *)sigs->object)->colour =
97 ((struct stats_key *)sigs->object)->parent =
100 nextkeys = lladd(nextkeys, sigs->object);
107 llfree(oldkeys, NULL);
113 if (oldkeys != NULL) {
114 llfree(oldkeys, NULL);
117 if (nextkeys != NULL) {
118 llfree(nextkeys, NULL);
126 * dofindpath - Given 2 keys displays a path between them.
127 * @have: The key we have.
128 * @want: The key we want to get to.
129 * @html: Should we output in html.
130 * @count: How many paths we should look for.
132 * This does a breadth first search on the key tree, starting with the
133 * key we have. It returns as soon as a path is found or when we run out
134 * of keys; whichever comes sooner.
136 void dofindpath(struct onak_dbctx *dbctx,
137 uint64_t have, uint64_t want, bool html, int count)
139 struct stats_key *keyinfoa, *keyinfob, *curkey;
140 uint64_t fullhave, fullwant;
145 fullhave = dbctx->getfullkeyid(dbctx, have);
146 fullwant = dbctx->getfullkeyid(dbctx, want);
149 * Make sure the keys we have and want are in the cache.
151 (void) dbctx->cached_getkeysigs(dbctx, fullhave);
152 (void) dbctx->cached_getkeysigs(dbctx, fullwant);
154 if ((keyinfoa = findinhash(fullhave)) == NULL) {
155 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
158 if ((keyinfob = findinhash(fullwant)) == NULL) {
159 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
165 while ((!cleanup()) && (pathnum < count)) {
167 * Fill the tree info up.
170 rec = findpath(dbctx, keyinfoa, keyinfob);
171 keyinfob->parent = 0;
173 printf("%s%d nodes examined. %ld elements in the hash%s\n",
178 if (keyinfoa->colour == 0) {
180 printf("Can't find a link from 0x%08" PRIX64
181 " to 0x%08" PRIX64 "%s\n",
186 printf("Can't find any further paths%s\n",
191 printf("%d steps from 0x%08" PRIX64 " to 0x%08" PRIX64
193 keyinfoa->colour, have & 0xFFFFFFFF,
197 while (curkey != NULL && curkey->keyid != 0) {
198 uid = dbctx->keyid2uid(dbctx,
200 if (html && uid == NULL) {
201 printf("<a href=\"lookup?op=get&search="
202 "0x%08" PRIX64 "\">0x%08" PRIX64
204 "User id not found])%s<BR>\n",
205 curkey->keyid & 0xFFFFFFFF,
206 curkey->keyid & 0xFFFFFFFF,
207 (curkey->keyid == fullwant) ?
209 } else if (html && uid != NULL) {
210 printf("<a href=\"lookup?op=get&search="
211 "0x%08" PRIX64 "\">0x%08"
213 " (<a href=\"lookup?op=vindex&"
214 "search=0x%08" PRIX64
217 curkey->keyid & 0xFFFFFFFF,
218 curkey->keyid & 0xFFFFFFFF,
219 curkey->keyid & 0xFFFFFFFF,
221 (curkey->keyid == fullwant) ?
224 printf("0x%08" PRIX64 " (%s)%s\n",
225 curkey->keyid & 0xFFFFFFFF,
227 "[User id not found]" :
229 (curkey->keyid == fullwant) ?
236 if (curkey != keyinfoa && curkey != keyinfob) {
237 curkey->disabled = true;
239 curkey = findinhash(curkey->parent);
242 puts("<P>List of key ids in path:</P>");
244 puts("List of key ids in path:");
247 while (curkey != NULL && curkey->keyid != 0) {
248 printf("0x%08" PRIX64 " ",
249 curkey->keyid & 0xFFFFFFFF);
250 curkey = findinhash(curkey->parent);
260 struct stats_key *furthestkey(struct onak_dbctx *dbctx, struct stats_key *have)
262 unsigned long count = 0;
263 unsigned long curdegree = 0;
264 struct ll *curll, *nextll, *tmp;
265 struct ll *sigs = NULL;
266 struct stats_key *max;
276 curll = lladd(NULL, have);
278 while (curll != NULL) {
279 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
280 curll->object)->keyid);
281 while (sigs != NULL) {
282 if (((struct stats_key *) sigs->object)->colour == 0) {
284 * We've never seen it. Count it, mark it and
285 * explore its subtree.
288 max = (struct stats_key *)sigs->object;
289 ((struct stats_key *)sigs->object)->colour =
291 ((struct stats_key *)sigs->object)->parent =
292 ((struct stats_key *)
293 curll->object)->keyid;
295 nextll=lladd(nextll, sigs->object);