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, see <https://www.gnu.org/licenses/>.
31 * initcolour - Clear the key graph ready for use.
32 * @parent: Do we want to clear the parent pointers too?
34 * Clears the parent and colour information on all elements in the key
37 void initcolour(bool parent)
43 * Init the colour/parent values. We get each entry list from the hash
44 * table and walk along it, zeroing the values.
46 for (loop = 0; loop < HASHSIZE; loop++) {
47 curkey = gethashtableentry(loop);
48 while (curkey != NULL) {
49 ((struct stats_key *)curkey->object)->colour = 0;
51 ((struct stats_key *)curkey->object)->parent =
54 curkey = curkey->next;
60 * findpath - Given 2 keys finds a path between them.
61 * @have: The key we have.
62 * @want: The key we want to get to.
64 * This does a breadth first search on the key tree, starting with the
65 * key we have. It returns as soon as a path is found or when we run out
66 * of keys; whichever comes sooner.
68 unsigned long findpath(struct onak_dbctx *dbctx,
69 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 = dbctx->cached_getkeysigs(dbctx, ((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(struct onak_dbctx *dbctx,
136 uint64_t have, uint64_t want, bool html, int count)
138 struct stats_key *keyinfoa, *keyinfob, *curkey;
144 * Make sure the keys we have and want are in the cache.
146 (void) dbctx->cached_getkeysigs(dbctx, have);
147 (void) dbctx->cached_getkeysigs(dbctx, want);
149 if ((keyinfoa = findinhash(have)) == NULL) {
150 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
153 if ((keyinfob = findinhash(want)) == NULL) {
154 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
160 while ((!cleanup()) && (pathnum < count)) {
162 * Fill the tree info up.
165 rec = findpath(dbctx, keyinfoa, keyinfob);
166 keyinfob->parent = 0;
168 printf("%s%d nodes examined. %ld elements in the hash%s\n",
173 if (keyinfoa->colour == 0) {
175 printf("Can't find a link from 0x%016" PRIX64
176 " to 0x%016" PRIX64 "%s\n",
181 printf("Can't find any further paths%s\n",
186 printf("%d steps from 0x%016" PRIX64 " to 0x%016"
188 keyinfoa->colour, have,
192 while (curkey != NULL && curkey->keyid != 0) {
193 uid = dbctx->keyid2uid(dbctx,
195 if (html && uid == NULL) {
196 printf("<a href=\"lookup?op=get&search="
197 "0x%016" PRIX64 "\">0x%016"
199 "User id not found])%s<BR>\n",
202 (curkey->keyid == want) ?
204 } else if (html && uid != NULL) {
205 printf("<a href=\"lookup?op=get&search="
206 "0x%016" PRIX64 "\">0x%016"
208 " (<a href=\"lookup?op=vindex&"
209 "search=0x%016" PRIX64
216 (curkey->keyid == want) ?
219 printf("0x%016" PRIX64 " (%s)%s\n",
222 "[User id not found]" :
224 (curkey->keyid == want) ?
231 if (curkey != keyinfoa && curkey != keyinfob) {
232 curkey->disabled = true;
234 curkey = findinhash(curkey->parent);
237 puts("<P>List of key ids in path:</P>");
239 puts("List of key ids in path:");
242 while (curkey != NULL && curkey->keyid != 0) {
243 printf("0x%016" PRIX64 " ",
245 curkey = findinhash(curkey->parent);
255 struct stats_key *furthestkey(struct onak_dbctx *dbctx, struct stats_key *have)
257 unsigned long count = 0;
258 unsigned long curdegree = 0;
259 struct ll *curll, *nextll, *tmp;
260 struct ll *sigs = NULL;
261 struct stats_key *max;
271 curll = lladd(NULL, have);
273 while (curll != NULL) {
274 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
275 curll->object)->keyid);
276 while (sigs != NULL) {
277 if (((struct stats_key *) sigs->object)->colour == 0) {
279 * We've never seen it. Count it, mark it and
280 * explore its subtree.
283 max = (struct stats_key *)sigs->object;
284 ((struct stats_key *)sigs->object)->colour =
286 ((struct stats_key *)sigs->object)->parent =
287 ((struct stats_key *)
288 curll->object)->keyid;
290 nextll=lladd(nextll, sigs->object);