]> the.earth.li Git - onak.git/blob - stats.c
0.6.3 release
[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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "cleanup.h"
25 #include "hash.h"
26 #include "keydb.h"
27 #include "keyindex.h"
28 #include "ll.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         int rec;
141         int pathnum;
142         char *uid;
143         char buf[1024];
144
145         /*
146          * Make sure the keys we have and want are in the cache.
147          */
148         (void) dbctx->cached_getkeysigs(dbctx, have);
149         (void) dbctx->cached_getkeysigs(dbctx, want);
150
151         if ((keyinfoa = findinhash(have)) == NULL) {
152                 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
153                 return;
154         }
155         if ((keyinfob = findinhash(want)) == NULL) {
156                 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
157                 return;
158         }
159
160         pathnum = 0;
161         
162         while ((!cleanup()) && (pathnum < count)) {
163                 /*
164                  * Fill the tree info up.
165                  */
166                 initcolour(true);
167                 rec = findpath(dbctx, keyinfoa, keyinfob);
168                 keyinfob->parent = 0;
169
170                 printf("%s%d nodes examined. %ld elements in the hash%s\n",
171                         html ? "<HR>" : "",
172                         rec,
173                         hashelements(),
174                         html ? "<BR>" : "");
175                 if (keyinfoa->colour == 0) {
176                         if (pathnum == 0) {
177                                 printf("Can't find a link from 0x%016" PRIX64
178                                 " to 0x%016" PRIX64 "%s\n",
179                                 have,
180                                 want,
181                                 html ? "<BR>" : "");
182                         } else {
183                                 printf("Can't find any further paths%s\n",
184                                         html ? "<BR>" : "");
185                         }
186                         pathnum = count;
187                 } else {
188                         printf("%d steps from 0x%016" PRIX64 " to 0x%016"
189                                 PRIX64 "%s\n",
190                                 keyinfoa->colour, have,
191                                 want,
192                                 html ? "<BR>" : "");
193                         curkey = keyinfoa;
194                         while (curkey != NULL && curkey->keyid != 0) {
195                                 uid = dbctx->keyid2uid(dbctx,
196                                                 curkey->keyid);
197                                 if (html && uid == NULL) {
198                                         printf("<a href=\"lookup?op=get&search="
199                                                 "0x%016" PRIX64 "\">0x%016"
200                                                 PRIX64 "</a> (["
201                                                 "User id not found])%s<BR>\n",
202                                                 curkey->keyid,
203                                                 curkey->keyid,
204                                                 (curkey->keyid == want) ?
205                                                         "" : " signs");
206                                 } else if (html && uid != NULL) {
207                                         printf("<a href=\"lookup?op=get&search="
208                                                 "0x%016" PRIX64 "\">0x%016"
209                                                 PRIX64 "</a>"
210                                                 " (<a href=\"lookup?op=vindex&"
211                                                 "search=0x%016" PRIX64
212                                                 "\">%s</a>)%s"
213                                                 "<BR>\n",
214                                                 curkey->keyid,
215                                                 curkey->keyid,
216                                                 curkey->keyid,
217                                                 html_escape(uid, strlen(uid),
218                                                         buf, sizeof(buf)),
219                                                 (curkey->keyid == want) ?
220                                                 "" : " signs");
221                                 } else {
222                                         printf("0x%016" PRIX64 " (%s)%s\n",
223                                                 curkey->keyid,
224                                                 (uid == NULL) ?
225                                                         "[User id not found]" :
226                                                         uid,
227                                                 (curkey->keyid == want) ?
228                                                 "" : " signs");
229                                 }
230                                 if (uid != NULL) {
231                                         free(uid);
232                                         uid = NULL;
233                                 }
234                                 if (curkey != keyinfoa && curkey != keyinfob) {
235                                         curkey->disabled = true;
236                                 }
237                                 curkey = findinhash(curkey->parent);
238                         }
239                         if (html) {
240                                 puts("<P>List of key ids in path:</P>");
241                         } else {
242                                 puts("List of key ids in path:");
243                         }
244                         curkey = keyinfoa;
245                         while (curkey != NULL && curkey->keyid != 0) {
246                                 printf("0x%016" PRIX64 " ",
247                                                 curkey->keyid);
248                                 curkey = findinhash(curkey->parent);
249                         }
250                         putchar('\n');
251                 }
252                 pathnum++;
253         }
254 }
255
256
257
258 struct stats_key *furthestkey(struct onak_dbctx *dbctx, struct stats_key *have)
259 {
260         unsigned long count = 0;
261         unsigned long curdegree = 0;
262         struct ll *curll, *nextll, *tmp;
263         struct ll *sigs = NULL;
264         struct stats_key *max;
265
266         if (have == NULL) {
267                 return NULL;
268         }
269
270         ++curdegree;
271
272         nextll = NULL;
273         max = have;
274         curll = lladd(NULL, have);
275
276         while (curll != NULL) {
277                 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
278                                 curll->object)->keyid);
279                 while (sigs != NULL) {
280                         if (((struct stats_key *) sigs->object)->colour == 0) {
281                                 /*
282                                  * We've never seen it. Count it, mark it and
283                                  * explore its subtree.
284                                  */
285                                 count++;
286                                 max = (struct stats_key *)sigs->object;
287                                 ((struct stats_key *)sigs->object)->colour = 
288                                         curdegree;
289                                 ((struct stats_key *)sigs->object)->parent = 
290                                         ((struct stats_key *)
291                                          curll->object)->keyid;
292                                 
293                                 nextll=lladd(nextll, sigs->object);
294                         }
295                         sigs=sigs->next;
296                 }
297                 tmp = curll->next;
298                 free(curll);
299                 curll = tmp;
300                 if (curll == NULL) {
301                         curll = nextll;
302                         nextll = NULL;
303                         ++curdegree;
304                 };
305         }
306
307         return max;
308 }