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