2 * stats.c - various routines to do stats on the key graph
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
19 * initcolour - Clear the key graph ready for use.
20 * @parent: Do we want to clear the parent pointers too?
22 * Clears the parent and colour information on all elements in the key
25 void initcolour(bool parent)
31 * Init the colour/parent values. We get each entry list from the hash
32 * table and walk along it, zeroing the values.
34 for (loop = 0; loop < HASHSIZE; loop++) {
35 curkey = gethashtableentry(loop);
36 while (curkey != NULL) {
37 ((struct stats_key *)curkey->object)->colour = 0;
39 ((struct stats_key *)curkey->object)->parent =
42 curkey = curkey->next;
48 * findpath - Given 2 keys finds a path between them.
49 * @have: The key we have.
50 * @want: The key we want to get to.
52 * This does a breadth first search on the key tree, starting with the
53 * key we have. It returns as soon as a path is found or when we run out
54 * of keys; whichever comes sooner.
56 unsigned long findpath(struct stats_key *have, struct stats_key *want)
58 struct ll *keys = NULL;
59 struct ll *oldkeys = NULL;
60 struct ll *sigs = NULL;
61 struct ll *nextkeys = NULL;
66 keys = lladd(NULL, want);
69 while (keys != NULL && have->colour == 0) {
70 sigs = cached_getkeysigs(((struct stats_key *)
71 keys->object)->keyid);
72 while (sigs != NULL && have->colour == 0) {
74 * Check if we've seen this key before and if not mark
75 * it and add its sigs to the list we want to look at.
77 if (!((struct stats_key *)sigs->object)->disabled &&
78 ((struct stats_key *)sigs->object)->colour == 0) {
80 ((struct stats_key *)sigs->object)->colour =
82 ((struct stats_key *)sigs->object)->parent =
85 nextkeys = lladd(nextkeys, sigs->object);
92 llfree(oldkeys, NULL);
98 if (oldkeys != NULL) {
99 llfree(oldkeys, NULL);
102 if (nextkeys != NULL) {
103 llfree(nextkeys, NULL);
111 * dofindpath - Given 2 keys displays a path between them.
112 * @have: The key we have.
113 * @want: The key we want to get to.
114 * @html: Should we output in html.
115 * @count: How many paths we should look for.
117 * This does a breadth first search on the key tree, starting with the
118 * key we have. It returns as soon as a path is found or when we run out
119 * of keys; whichever comes sooner.
121 void dofindpath(uint64_t have, uint64_t want, bool html, int count)
123 struct stats_key *keyinfoa, *keyinfob, *curkey;
124 uint64_t fullhave, fullwant;
129 fullhave = getfullkeyid(have);
130 fullwant = getfullkeyid(want);
133 * Make sure the keys we have and want are in the cache.
135 cached_getkeysigs(fullhave);
136 cached_getkeysigs(fullwant);
138 if ((keyinfoa = findinhash(fullhave)) == NULL) {
139 printf("Couldn't find key 0x%llX.\n", have);
142 if ((keyinfob = findinhash(fullwant)) == NULL) {
143 printf("Couldn't find key 0x%llX.\n", want);
149 while (pathnum < count) {
151 * Fill the tree info up.
154 rec = findpath(keyinfoa, keyinfob);
155 keyinfob->parent = 0;
157 printf("%s%d nodes examined. %ld elements in the hash%s\n",
162 if (keyinfoa->colour == 0) {
163 printf("Can't find a link from 0x%08llX to 0x%08llX"
169 printf("%d steps from 0x%08llX to 0x%08llX%s\n",
170 keyinfoa->colour, have & 0xFFFFFFFF,
174 while (curkey != NULL && curkey->keyid != 0) {
175 uid = keyid2uid(curkey->keyid);
176 if (html && uid == NULL) {
177 printf("<a href=\"lookup?op=get&search="
178 "0x%08llX\">0x%08llX</a> (["
179 "User id not found])%s<BR>\n",
180 curkey->keyid & 0xFFFFFFFF,
181 curkey->keyid & 0xFFFFFFFF,
182 (curkey->keyid == fullwant) ?
184 } else if (html && uid != NULL) {
185 printf("<a href=\"lookup?op=get&search="
186 "0x%08llX\">0x%08llX</a>"
187 " (<a href=\"lookup?op=vindex&"
188 "search=0x%08llX\">%s</a>)%s"
190 curkey->keyid & 0xFFFFFFFF,
191 curkey->keyid & 0xFFFFFFFF,
192 curkey->keyid & 0xFFFFFFFF,
194 (curkey->keyid == fullwant) ?
197 printf("0x%08llX (%s)%s\n",
198 curkey->keyid & 0xFFFFFFFF,
200 "[User id not found]" :
202 (curkey->keyid == fullwant) ?
209 if (curkey != keyinfoa && curkey != keyinfob) {
210 curkey->disabled = true;
212 curkey = findinhash(curkey->parent);
215 puts("<P>List of key ids in path:</P>");
217 puts("List of key ids in path:");
220 while (curkey != NULL && curkey->keyid != 0) {
221 printf("0x%08llX ", curkey->keyid & 0xFFFFFFFF);
222 curkey = findinhash(curkey->parent);
226 printf("<BR><A HREF=\"gpgwww?from=0x%08llX&"
228 "Find reverse path</A>\n",
239 struct stats_key *furthestkey(struct stats_key *have)
241 unsigned long count = 0;
242 unsigned long curdegree = 0;
243 struct ll *curll, *nextll, *tmp;
244 struct ll *sigs = NULL;
245 struct stats_key *max;
255 curll = lladd(NULL, have);
257 while (curll != NULL) {
258 sigs = cached_getkeysigs(((struct stats_key *)
259 curll->object)->keyid);
260 while (sigs != NULL) {
261 if (((struct stats_key *) sigs->object)->colour == 0) {
263 * We've never seen it. Count it, mark it and
264 * explore its subtree.
267 max = (struct stats_key *)sigs->object;
268 ((struct stats_key *)sigs->object)->colour =
270 ((struct stats_key *)sigs->object)->parent =
271 ((struct stats_key *)
272 curll->object)->keyid;
274 nextll=lladd(nextll, sigs->object);