]> git.sommitrealweird.co.uk Git - onak.git/blob - stats.c
2e6347976fccf04b337525268001ede24a36ea00
[onak.git] / stats.c
1 /*
2  * stats.c - various routines to do stats on the key graph
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "getcgi.h"
13 #include "hash.h"
14 #include "keydb.h"
15 #include "ll.h"
16 #include "stats.h"
17
18 /**
19  *      initcolour - Clear the key graph ready for use.
20  *      @parent: Do we want to clear the parent pointers too?
21  *
22  *      Clears the parent and colour information on all elements in the key
23  *      graph.
24  */
25 void initcolour(bool parent)
26 {
27         unsigned long loop;
28         struct ll *curkey;
29
30         /*
31          * Init the colour/parent values. We get each entry list from the hash
32          * table and walk along it, zeroing the values.
33          */
34         for (loop = 0; loop < HASHSIZE; loop++) {
35                 curkey = gethashtableentry(loop);
36                 while (curkey != NULL) {
37                         ((struct stats_key *)curkey->object)->colour = 0;
38                         if (parent) {
39                                 ((struct stats_key *)curkey->object)->parent =
40                                         0;
41                         }
42                         curkey = curkey->next;
43                 }
44         }
45 }
46
47 /**
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.
51  *
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.
55  */
56 unsigned long findpath(struct stats_key *have, struct stats_key *want)
57 {
58         struct ll *keys = NULL;
59         struct ll *oldkeys = NULL;
60         struct ll *sigs = NULL;
61         struct ll *nextkeys = NULL;
62         long curdegree = 0;
63         long count = 0;
64         
65         curdegree = 1;
66         keys = lladd(NULL, want);
67         oldkeys = keys;
68
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) {
73                         /*
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.
76                          */
77                         if (((struct stats_key *)sigs->object)->colour == 0) {
78                                 count++;
79                                 ((struct stats_key *)sigs->object)->colour =
80                                         curdegree;
81                                 ((struct stats_key *)sigs->object)->parent =
82                                         ((struct stats_key *)
83                                          keys->object)->keyid;
84                                 nextkeys = lladd(nextkeys, sigs->object);
85                         }
86                         sigs = sigs->next;
87                 }
88                 keys = keys->next;
89                 if (keys == NULL) {
90                         keys = nextkeys;
91                         llfree(oldkeys, NULL);
92                         oldkeys = keys;
93                         nextkeys = NULL;
94                         curdegree++;
95                 }
96         }
97         if (oldkeys != NULL) {
98                 llfree(oldkeys, NULL);
99                 oldkeys = NULL;
100         }
101         if (nextkeys != NULL) {
102                 llfree(nextkeys, NULL);
103                 nextkeys = NULL;
104         }
105
106         return count;
107 }
108
109 /**
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.
114  *
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.
118  */
119 void dofindpath(uint64_t have, uint64_t want, bool html)
120 {
121         struct stats_key *keyinfoa, *keyinfob, *curkey;
122         uint64_t fullhave, fullwant;
123         int rec;
124         char *uid;
125
126         fullhave = getfullkeyid(have);
127         fullwant = getfullkeyid(want);
128
129         /*
130          * Make sure the keys we have and want are in the cache.
131          */
132         cached_getkeysigs(fullhave);
133         cached_getkeysigs(fullwant);
134
135         if ((keyinfoa = findinhash(fullhave)) == NULL) {
136                 printf("Couldn't find key 0x%llX.\n", have);
137                 return;
138         }
139         if ((keyinfob = findinhash(fullwant)) == NULL) {
140                 printf("Couldn't find key 0x%llX.\n", want);
141                 return;
142         }
143         
144         /*
145          * Fill the tree info up.
146          */
147         initcolour(true);
148         rec = findpath(keyinfoa, keyinfob);
149         keyinfob->parent = 0;
150
151         printf("%d nodes examined. %ld elements in the hash%s\n", rec,
152                         hashelements(),
153                         html ? "<BR>" : "");
154         if (keyinfoa->colour == 0) {
155                 printf("Can't find a link from 0x%08llX to 0x%08llX%s\n",
156                                 have,
157                                 want,
158                                 html ? "<BR>" : "");
159         } else {
160                 printf("%d steps from 0x%08llX to 0x%08llX%s\n",
161                                 keyinfoa->colour, have & 0xFFFFFFFF,
162                                 want & 0xFFFFFFFF,
163                                 html ? "<BR>" : "");
164                 curkey = keyinfoa;
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) ? "" :
174                                          " signs");
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,
183                                         txt2html(uid),
184                                         (curkey->keyid == fullwant) ? "" :
185                                          " signs");
186                         } else {
187                                 printf("0x%08llX (%s)%s\n",
188                                         curkey->keyid & 0xFFFFFFFF,
189                                         (uid == NULL) ? "[User id not found]" :
190                                                 uid,
191                                         (curkey->keyid == fullwant) ? "" :
192                                          " signs");
193                         }
194                         if (uid != NULL) {
195                                 free(uid);
196                                 uid = NULL;
197                         }
198                         curkey = findinhash(curkey->parent);
199                 }
200                 if (html) {
201                         puts("<P>List of key ids in path:</P>");
202                 } else {
203                         puts("List of key ids in path:");
204                 }
205                 curkey = keyinfoa;
206                 while (curkey != NULL && curkey->keyid != 0) {
207                         printf("0x%08llX ", curkey->keyid & 0xFFFFFFFF);
208                         curkey = findinhash(curkey->parent);
209                 }
210                 putchar('\n');
211                 if (html) {
212                         printf("<BR>"
213                                 "<A HREF=\"gpgwww?from=0x%08llX&to=0x%08llX\">"
214                                         "Find reverse path</A>\n",
215                                         want,
216                                         have);
217                 }
218         }
219 }
220
221
222
223 struct stats_key *furthestkey(struct stats_key *have)
224 {
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;
230
231         if (have == NULL) {
232                 return NULL;
233         }
234
235         ++curdegree;
236
237         nextll = NULL;
238         max = have;
239         curll = lladd(NULL, have);
240
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) {
246                                 /*
247                                  * We've never seen it. Count it, mark it and
248                                  * explore its subtree.
249                                  */
250                                 count++;
251                                 max = (struct stats_key *)sigs->object;
252                                 ((struct stats_key *)sigs->object)->colour = 
253                                         curdegree;
254                                 ((struct stats_key *)sigs->object)->parent = 
255                                         ((struct stats_key *)
256                                          curll->object)->keyid;
257                                 
258                                 nextll=lladd(nextll, sigs->object);
259                         }
260                         sigs=sigs->next;
261                 }
262                 tmp = curll->next;
263                 free(curll);
264                 curll = tmp;
265                 if (curll == NULL) {
266                         curll = nextll;
267                         nextll = NULL;
268                         ++curdegree;
269                 };
270         }
271
272         return max;
273 }