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)->colour == 0) {
79 ((struct stats_key *)sigs->object)->colour =
81 ((struct stats_key *)sigs->object)->parent =
84 nextkeys = lladd(nextkeys, sigs->object);
91 llfree(oldkeys, NULL);
97 if (oldkeys != NULL) {
98 llfree(oldkeys, NULL);
101 if (nextkeys != NULL) {
102 llfree(nextkeys, NULL);
110 * dofindpath - Given 2 keys displays a path between them.
111 * @have: The key we have.
112 * @want: The key we want to get to.
113 * @html: Should we output in html.
115 * This does a breadth first search on the key tree, starting with the
116 * key we have. It returns as soon as a path is found or when we run out
117 * of keys; whichever comes sooner.
119 void dofindpath(uint64_t have, uint64_t want, bool html)
121 struct stats_key *keyinfoa, *keyinfob, *curkey;
122 uint64_t fullhave, fullwant;
126 fullhave = getfullkeyid(have);
127 fullwant = getfullkeyid(want);
130 * Make sure the keys we have and want are in the cache.
132 cached_getkeysigs(fullhave);
133 cached_getkeysigs(fullwant);
135 if ((keyinfoa = findinhash(fullhave)) == NULL) {
136 printf("Couldn't find key 0x%llX.\n", have);
139 if ((keyinfob = findinhash(fullwant)) == NULL) {
140 printf("Couldn't find key 0x%llX.\n", want);
145 * Fill the tree info up.
148 rec = findpath(keyinfoa, keyinfob);
149 keyinfob->parent = 0;
151 printf("%d nodes examined. %ld elements in the hash%s\n", rec,
154 if (keyinfoa->colour == 0) {
155 printf("Can't find a link from 0x%08llX to 0x%08llX%s\n",
160 printf("%d steps from 0x%08llX to 0x%08llX%s\n",
161 keyinfoa->colour, have & 0xFFFFFFFF,
165 while (curkey != NULL && curkey->keyid != 0) {
166 uid = keyid2uid(curkey->keyid);
167 if (html && uid == NULL) {
168 printf("<a href=\"lookup?op=get&search="
169 "0x%08llX\">0x%08llX</a> ([User id"
170 " not found])%s<BR>\n",
171 curkey->keyid & 0xFFFFFFFF,
172 curkey->keyid & 0xFFFFFFFF,
173 (curkey->keyid == fullwant) ? "" :
175 } else if (html && uid != NULL) {
176 printf("<a href=\"lookup?op=get&search="
177 "0x%08llX\">0x%08llX</a>"
178 " (<a href=\"lookup?op=vindex"
179 "&search=0x%08llX\">%s</a>)%s<BR>\n",
180 curkey->keyid & 0xFFFFFFFF,
181 curkey->keyid & 0xFFFFFFFF,
182 curkey->keyid & 0xFFFFFFFF,
184 (curkey->keyid == fullwant) ? "" :
187 printf("0x%08llX (%s)%s\n",
188 curkey->keyid & 0xFFFFFFFF,
189 (uid == NULL) ? "[User id not found]" :
191 (curkey->keyid == fullwant) ? "" :
198 curkey = findinhash(curkey->parent);
201 puts("<P>List of key ids in path:</P>");
203 puts("List of key ids in path:");
206 while (curkey != NULL && curkey->keyid != 0) {
207 printf("0x%08llX ", curkey->keyid & 0xFFFFFFFF);
208 curkey = findinhash(curkey->parent);
213 "<A HREF=\"gpgwww?from=0x%08llX&to=0x%08llX\">"
214 "Find reverse path</A>\n",
223 struct stats_key *furthestkey(struct stats_key *have)
225 unsigned long count = 0;
226 unsigned long curdegree = 0;
227 struct ll *curll, *nextll, *tmp;
228 struct ll *sigs = NULL;
229 struct stats_key *max;
239 curll = lladd(NULL, have);
241 while (curll != NULL) {
242 sigs = cached_getkeysigs(((struct stats_key *)
243 curll->object)->keyid);
244 while (sigs != NULL) {
245 if (((struct stats_key *) sigs->object)->colour == 0) {
247 * We've never seen it. Count it, mark it and
248 * explore its subtree.
251 max = (struct stats_key *)sigs->object;
252 ((struct stats_key *)sigs->object)->colour =
254 ((struct stats_key *)sigs->object)->parent =
255 ((struct stats_key *)
256 curll->object)->keyid;
258 nextll=lladd(nextll, sigs->object);