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;
139 uint64_t fullhave, fullwant;
144 fullhave = dbctx->getfullkeyid(dbctx, have);
145 fullwant = dbctx->getfullkeyid(dbctx, want);
148 * Make sure the keys we have and want are in the cache.
150 (void) dbctx->cached_getkeysigs(dbctx, fullhave);
151 (void) dbctx->cached_getkeysigs(dbctx, fullwant);
153 if ((keyinfoa = findinhash(fullhave)) == NULL) {
154 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
157 if ((keyinfob = findinhash(fullwant)) == NULL) {
158 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
164 while ((!cleanup()) && (pathnum < count)) {
166 * Fill the tree info up.
169 rec = findpath(dbctx, keyinfoa, keyinfob);
170 keyinfob->parent = 0;
172 printf("%s%d nodes examined. %ld elements in the hash%s\n",
177 if (keyinfoa->colour == 0) {
179 printf("Can't find a link from 0x%08" PRIX64
180 " to 0x%08" PRIX64 "%s\n",
185 printf("Can't find any further paths%s\n",
190 printf("%d steps from 0x%08" PRIX64 " to 0x%08" PRIX64
192 keyinfoa->colour, have & 0xFFFFFFFF,
196 while (curkey != NULL && curkey->keyid != 0) {
197 uid = dbctx->keyid2uid(dbctx,
199 if (html && uid == NULL) {
200 printf("<a href=\"lookup?op=get&search="
201 "0x%08" PRIX64 "\">0x%08" PRIX64
203 "User id not found])%s<BR>\n",
204 curkey->keyid & 0xFFFFFFFF,
205 curkey->keyid & 0xFFFFFFFF,
206 (curkey->keyid == fullwant) ?
208 } else if (html && uid != NULL) {
209 printf("<a href=\"lookup?op=get&search="
210 "0x%08" PRIX64 "\">0x%08"
212 " (<a href=\"lookup?op=vindex&"
213 "search=0x%08" PRIX64
216 curkey->keyid & 0xFFFFFFFF,
217 curkey->keyid & 0xFFFFFFFF,
218 curkey->keyid & 0xFFFFFFFF,
220 (curkey->keyid == fullwant) ?
223 printf("0x%08" PRIX64 " (%s)%s\n",
224 curkey->keyid & 0xFFFFFFFF,
226 "[User id not found]" :
228 (curkey->keyid == fullwant) ?
235 if (curkey != keyinfoa && curkey != keyinfob) {
236 curkey->disabled = true;
238 curkey = findinhash(curkey->parent);
241 puts("<P>List of key ids in path:</P>");
243 puts("List of key ids in path:");
246 while (curkey != NULL && curkey->keyid != 0) {
247 printf("0x%08" PRIX64 " ",
248 curkey->keyid & 0xFFFFFFFF);
249 curkey = findinhash(curkey->parent);
259 struct stats_key *furthestkey(struct onak_dbctx *dbctx, struct stats_key *have)
261 unsigned long count = 0;
262 unsigned long curdegree = 0;
263 struct ll *curll, *nextll, *tmp;
264 struct ll *sigs = NULL;
265 struct stats_key *max;
275 curll = lladd(NULL, have);
277 while (curll != NULL) {
278 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
279 curll->object)->keyid);
280 while (sigs != NULL) {
281 if (((struct stats_key *) sigs->object)->colour == 0) {
283 * We've never seen it. Count it, mark it and
284 * explore its subtree.
287 max = (struct stats_key *)sigs->object;
288 ((struct stats_key *)sigs->object)->colour =
290 ((struct stats_key *)sigs->object)->parent =
291 ((struct stats_key *)
292 curll->object)->keyid;
294 nextll=lladd(nextll, sigs->object);