]> the.earth.li Git - onak.git/blob - stats.c
Add de-duplication of subkeys on a key
[onak.git] / stats.c
1 /*
2  * stats.c - various routines to do stats on the key graph
3  *
4  * Copyright 2000-2004,2007-2009 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "cleanup.h"
24 #include "getcgi.h"
25 #include "hash.h"
26 #include "keydb.h"
27 #include "ll.h"
28 #include "onak-conf.h"
29 #include "stats.h"
30
31 /**
32  *      initcolour - Clear the key graph ready for use.
33  *      @parent: Do we want to clear the parent pointers too?
34  *
35  *      Clears the parent and colour information on all elements in the key
36  *      graph.
37  */
38 void initcolour(bool parent)
39 {
40         unsigned int loop;
41         struct ll *curkey;
42
43         /*
44          * Init the colour/parent values. We get each entry list from the hash
45          * table and walk along it, zeroing the values.
46          */
47         for (loop = 0; loop < HASHSIZE; loop++) {
48                 curkey = gethashtableentry(loop);
49                 while (curkey != NULL) {
50                         ((struct stats_key *)curkey->object)->colour = 0;
51                         if (parent) {
52                                 ((struct stats_key *)curkey->object)->parent =
53                                         0;
54                         }
55                         curkey = curkey->next;
56                 }
57         }
58 }
59
60 /**
61  *      findpath - Given 2 keys finds a path between them.
62  *      @have: The key we have.
63  *      @want: The key we want to get to.
64  *
65  *      This does a breadth first search on the key tree, starting with the
66  *      key we have. It returns as soon as a path is found or when we run out
67  *      of keys; whichever comes sooner.
68  */
69 unsigned long findpath(struct onak_dbctx *dbctx,
70                 struct stats_key *have, struct stats_key *want)
71 {
72         struct ll *keys = NULL;
73         struct ll *oldkeys = NULL;
74         struct ll *sigs = NULL;
75         struct ll *nextkeys = NULL;
76         long curdegree = 0;
77         unsigned long count = 0;
78         
79         curdegree = 1;
80         keys = lladd(NULL, want);
81         oldkeys = keys;
82
83         while ((!cleanup()) && keys != NULL && have->colour == 0) {
84                 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
85                                         keys->object)->keyid);
86                 while ((!cleanup()) && sigs != NULL && have->colour == 0) {
87                         /*
88                          * Check if we've seen this key before and if not mark
89                          * it and add its sigs to the list we want to look at.
90                          */
91                         if (!((struct stats_key *)sigs->object)->disabled &&
92                             !((struct stats_key *)sigs->object)->revoked &&
93                             ((struct stats_key *)sigs->object)->colour == 0) {
94                                 count++;
95                                 ((struct stats_key *)sigs->object)->colour =
96                                         curdegree;
97                                 ((struct stats_key *)sigs->object)->parent =
98                                         ((struct stats_key *)
99                                          keys->object)->keyid;
100                                 nextkeys = lladd(nextkeys, sigs->object);
101                         }
102                         sigs = sigs->next;
103                 }
104                 keys = keys->next;
105                 if (keys == NULL) {
106                         keys = nextkeys;
107                         llfree(oldkeys, NULL);
108                         oldkeys = keys;
109                         nextkeys = NULL;
110                         curdegree++;
111                 }
112         }
113         if (oldkeys != NULL) {
114                 llfree(oldkeys, NULL);
115                 oldkeys = NULL;
116         }
117         if (nextkeys != NULL) {
118                 llfree(nextkeys, NULL);
119                 nextkeys = NULL;
120         }
121
122         return count;
123 }
124
125 /**
126  *      dofindpath - Given 2 keys displays a path between them.
127  *      @have: The key we have.
128  *      @want: The key we want to get to.
129  *      @html: Should we output in html.
130  *      @count: How many paths we should look for.
131  *
132  *      This does a breadth first search on the key tree, starting with the
133  *      key we have. It returns as soon as a path is found or when we run out
134  *      of keys; whichever comes sooner.
135  */
136 void dofindpath(struct onak_dbctx *dbctx,
137                 uint64_t have, uint64_t want, bool html, int count)
138 {
139         struct stats_key *keyinfoa, *keyinfob, *curkey;
140         uint64_t fullhave, fullwant;
141         int rec;
142         int pathnum;
143         char *uid;
144
145         fullhave = dbctx->getfullkeyid(dbctx, have);
146         fullwant = dbctx->getfullkeyid(dbctx, want);
147
148         /*
149          * Make sure the keys we have and want are in the cache.
150          */
151         (void) dbctx->cached_getkeysigs(dbctx, fullhave);
152         (void) dbctx->cached_getkeysigs(dbctx, fullwant);
153
154         if ((keyinfoa = findinhash(fullhave)) == NULL) {
155                 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
156                 return;
157         }
158         if ((keyinfob = findinhash(fullwant)) == NULL) {
159                 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
160                 return;
161         }
162
163         pathnum = 0;
164         
165         while ((!cleanup()) && (pathnum < count)) {
166                 /*
167                  * Fill the tree info up.
168                  */
169                 initcolour(true);
170                 rec = findpath(dbctx, keyinfoa, keyinfob);
171                 keyinfob->parent = 0;
172
173                 printf("%s%d nodes examined. %ld elements in the hash%s\n",
174                         html ? "<HR>" : "",
175                         rec,
176                         hashelements(),
177                         html ? "<BR>" : "");
178                 if (keyinfoa->colour == 0) {
179                         if (pathnum == 0) {
180                                 printf("Can't find a link from 0x%08" PRIX64
181                                 " to 0x%08" PRIX64 "%s\n",
182                                 have,
183                                 want,
184                                 html ? "<BR>" : "");
185                         } else {
186                                 printf("Can't find any further paths%s\n",
187                                         html ? "<BR>" : "");
188                         }
189                         pathnum = count;
190                 } else {
191                         printf("%d steps from 0x%08" PRIX64 " to 0x%08" PRIX64
192                                 "%s\n",
193                                 keyinfoa->colour, have & 0xFFFFFFFF,
194                                 want & 0xFFFFFFFF,
195                                 html ? "<BR>" : "");
196                         curkey = keyinfoa;
197                         while (curkey != NULL && curkey->keyid != 0) {
198                                 uid = dbctx->keyid2uid(dbctx,
199                                                 curkey->keyid);
200                                 if (html && uid == NULL) {
201                                         printf("<a href=\"lookup?op=get&search="
202                                                 "0x%08" PRIX64 "\">0x%08" PRIX64
203                                                 "</a> (["
204                                                 "User id not found])%s<BR>\n",
205                                                 curkey->keyid & 0xFFFFFFFF,
206                                                 curkey->keyid & 0xFFFFFFFF,
207                                                 (curkey->keyid == fullwant) ?
208                                                         "" : " signs");
209                                 } else if (html && uid != NULL) {
210                                         printf("<a href=\"lookup?op=get&search="
211                                                 "0x%08" PRIX64 "\">0x%08"
212                                                 PRIX64 "</a>"
213                                                 " (<a href=\"lookup?op=vindex&"
214                                                 "search=0x%08" PRIX64 
215                                                 "\">%s</a>)%s"
216                                                 "<BR>\n",
217                                                 curkey->keyid & 0xFFFFFFFF,
218                                                 curkey->keyid & 0xFFFFFFFF,
219                                                 curkey->keyid & 0xFFFFFFFF,
220                                                 txt2html(uid),
221                                                 (curkey->keyid == fullwant) ?
222                                                 "" : " signs");
223                                 } else {
224                                         printf("0x%08" PRIX64 " (%s)%s\n",
225                                                 curkey->keyid & 0xFFFFFFFF,
226                                                 (uid == NULL) ?
227                                                         "[User id not found]" :
228                                                         uid,
229                                                 (curkey->keyid == fullwant) ?
230                                                 "" : " signs");
231                                 }
232                                 if (uid != NULL) {
233                                         free(uid);
234                                         uid = NULL;
235                                 }
236                                 if (curkey != keyinfoa && curkey != keyinfob) {
237                                         curkey->disabled = true;
238                                 }
239                                 curkey = findinhash(curkey->parent);
240                         }
241                         if (html) {
242                                 puts("<P>List of key ids in path:</P>");
243                         } else {
244                                 puts("List of key ids in path:");
245                         }
246                         curkey = keyinfoa;
247                         while (curkey != NULL && curkey->keyid != 0) {
248                                 printf("0x%08" PRIX64 " ",
249                                                 curkey->keyid & 0xFFFFFFFF);
250                                 curkey = findinhash(curkey->parent);
251                         }
252                         putchar('\n');
253                 }
254                 pathnum++;
255         }
256 }
257
258
259
260 struct stats_key *furthestkey(struct onak_dbctx *dbctx, struct stats_key *have)
261 {
262         unsigned long count = 0;
263         unsigned long curdegree = 0;
264         struct ll *curll, *nextll, *tmp;
265         struct ll *sigs = NULL;
266         struct stats_key *max;
267
268         if (have == NULL) {
269                 return NULL;
270         }
271
272         ++curdegree;
273
274         nextll = NULL;
275         max = have;
276         curll = lladd(NULL, have);
277
278         while (curll != NULL) {
279                 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
280                                 curll->object)->keyid);
281                 while (sigs != NULL) {
282                         if (((struct stats_key *) sigs->object)->colour == 0) {
283                                 /*
284                                  * We've never seen it. Count it, mark it and
285                                  * explore its subtree.
286                                  */
287                                 count++;
288                                 max = (struct stats_key *)sigs->object;
289                                 ((struct stats_key *)sigs->object)->colour = 
290                                         curdegree;
291                                 ((struct stats_key *)sigs->object)->parent = 
292                                         ((struct stats_key *)
293                                          curll->object)->keyid;
294                                 
295                                 nextll=lladd(nextll, sigs->object);
296                         }
297                         sigs=sigs->next;
298                 }
299                 tmp = curll->next;
300                 free(curll);
301                 curll = tmp;
302                 if (curll == NULL) {
303                         curll = nextll;
304                         nextll = NULL;
305                         ++curdegree;
306                 };
307         }
308
309         return max;
310 }