/[pkg-mixmaster]/trunk/Mix/Src/stats.c
ViewVC logotype

Contents of /trunk/Mix/Src/stats.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 262 - (hide annotations) (download)
Wed Sep 18 23:26:17 2002 UTC (10 years, 8 months ago) by rabbi
File MIME type: text/plain
File size: 11137 byte(s)
Added closing comments for all #ifdef statements. All #endif's, as well as
nested braces, should be commented to reference their start.

We need to provide comments before every function as well.
1 rabbi 1 /* Mixmaster version 3 -- (C) 1999 Anonymizer Inc.
2    
3     Mixmaster may be redistributed and modified under certain conditions.
4     This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
5     ANY KIND, either express or implied. See the file COPYRIGHT for
6     details.
7    
8     Remailer statistics
9 rabbi 262 $Id: stats.c,v 1.14 2002/09/18 23:26:17 rabbi Exp $ */
10 rabbi 1
11    
12     #include "mix3.h"
13     #include <stdio.h>
14     #include <string.h>
15     #include <time.h>
16    
17     /* log that a message of type t has been received. Statistics for type 2
18     messages are taken from the IDLOG instead of calling this function */
19     int stats_log(int t)
20     {
21     FILE *f;
22    
23     f = mix_openfile(STATS, "a");
24     if (f == NULL) {
25     errlog(ERRORMSG, "Can't open %s!\n", STATS);
26     return (-1);
27     }
28     lock(f);
29     fprintf(f, "%d 1 %ld\n", t, (long) time(NULL));
30     unlock(f);
31     fclose(f);
32     return (0);
33     }
34    
35     /* log the current pool size after sending messages */
36     int stats_out(int pool)
37     {
38     FILE *f;
39    
40     f = mix_openfile(STATS, "a");
41     if (f == NULL) {
42     errlog(ERRORMSG, "Can't open %s!\n", STATS);
43     return (-1);
44     }
45     lock(f);
46     fprintf(f, "p 1 %d %ld\n", pool, (long) time(NULL));
47     unlock(f);
48     fclose(f);
49     return (0);
50     }
51    
52     int stats(BUFFER *b)
53     {
54     FILE *s, *f;
55     char line[LINELEN];
56     long now, today, then;
57     time_t t;
58     long updated = 0, havestats = 0;
59     int msgd[3][24], msg[3][80];
60     int poold[2][24], pool[2][80];
61 weaselp 178 int i, num, type, assigned, daysum;
62     char c;
63 rabbi 82 idlog_t idbuf;
64 rabbi 1
65     now = (time(NULL) / (60 * 60) + 1) * 60 * 60;
66     today = (now / SECONDSPERDAY) * SECONDSPERDAY;
67    
68     for (i = 0; i < 24; i++)
69     msgd[0][i] = msgd[1][i] = msgd[2][i] = poold[0][i] = poold[1][i] = 0;
70     for (i = 0; i < 80; i++)
71     msg[0][i] = msg[1][i] = msg[2][i] = pool[0][i] = pool[1][i] = 0;
72    
73     s = mix_openfile(STATS, "r");
74     if (s != NULL) {
75     lock(s);
76     fscanf(s, "%ld", &updated);
77     while (fgets(line, sizeof(line), s) != NULL) {
78     switch (line[0]) {
79     case '0':
80     case '1':
81     case '2':
82 weaselp 178 c = '\0';
83     assigned = sscanf(line, "%d %d %ld %c", &type, &num, &then, &c);
84     daysum = (assigned == 4 && c == 'd');
85    
86     if (now - then < 0 || (daysum && today - then < 0))
87 rabbi 1 break; /* keep memory consistent even if the time
88     suddenly goes backwards :) */
89 weaselp 178 if (now - then < SECONDSPERDAY && !daysum)
90 rabbi 1 msgd[type][(now - then) / (60 * 60)] += num;
91     else if (today - then < 80 * SECONDSPERDAY)
92     msg[type][(today - then) / SECONDSPERDAY] += num;
93     if (havestats == 0 || then < havestats)
94     havestats = then;
95     break;
96     case 'p':
97 weaselp 178 c = '\0';
98     assigned = sscanf(line, "p %d %d %ld %c", &num, &i, &then, &c);
99     daysum = (assigned == 4 && c == 'd');
100    
101     if (now - then < 0 || (daysum && today - then < 0))
102 rabbi 1 break;
103 weaselp 178 if (now - then < SECONDSPERDAY && !daysum) {
104 rabbi 1 poold[0][(now - then) / (60 * 60)] += num;
105     poold[1][(now - then) / (60 * 60)] += i;
106     } else if (today - then < 80 * SECONDSPERDAY) {
107     poold[0][(today - then) / (24 * 60 * 60)] += num;
108     poold[1][(today - then) / (24 * 60 * 60)] += i;
109     }
110     if (havestats == 0 || then < havestats)
111     havestats = then;
112     break;
113     }
114     }
115     unlock(s);
116     fclose(s);
117     }
118 rabbi 82 f = mix_openfile(IDLOG, "rb");
119 rabbi 1 if (f != NULL) {
120 rabbi 82 while (fread(&idbuf, 1, sizeof(idlog_t), f) == sizeof(idlog_t)) {
121     then = idbuf.time;
122 rabbi 1 if (then < updated || now - then < 0)
123     continue;
124     if (now - then < SECONDSPERDAY)
125     msgd[2][(now - then) / (60 * 60)]++;
126     else if (today - then < 80 * SECONDSPERDAY)
127     msg[2][(today - then) / SECONDSPERDAY]++;
128     if (havestats == 0 || then < havestats)
129     havestats = then;
130     }
131     fclose(f);
132     }
133     if (havestats == 0) {
134     if (b != NULL)
135     errlog(NOTICE, "No statistics available.\n");
136     return (-1);
137     }
138     s = mix_openfile(STATS, "w");
139     if (s == NULL) {
140     errlog(ERRORMSG, "Can't create %s!\n", STATS);
141     return (-1);
142     }
143     lock(s);
144     fprintf(s, "%ld\n", (long) time(NULL)); /* time of stats.log update */
145     for (i = 0; i < 24; i++) {
146     for (type = 0; type < 3; type++)
147     if (msgd[type][i] > 0)
148     fprintf(s, "%d %d %ld\n", type, msgd[type][i], now - i * 60 * 60);
149     if (poold[0][i] > 0)
150     fprintf(s, "p %d %d %ld\n", poold[0][i], poold[1][i], now - i * 60 * 60);
151     }
152     for (i = 0; i < 80; i++) {
153     for (type = 0; type < 3; type++)
154     if (msg[type][i] > 0)
155 weaselp 178 fprintf(s, "%d %d %ld d\n", type, msg[type][i],
156 rabbi 1 today - i * 24 * 60 * 60);
157     if (pool[0][i] > 0)
158 weaselp 178 fprintf(s, "p %d %d %ld d\n", pool[0][i], pool[1][i],
159 rabbi 1 today - i * 24 * 60 * 60);
160     }
161     unlock(s);
162     fclose(s);
163     if (b != NULL) {
164     struct tm *gt;
165    
166     buf_sets(b, "Subject: Statistics for the ");
167     buf_appends(b, SHORTNAME);
168     buf_appends(b, " remailer\n\n");
169    
170     buf_appends(b, "Number of messages in the past 24 hours:\n");
171     t = now;
172     gt = gmtime(&t);
173     for (i = 23; i >= 0; i--) {
174     buf_appendf(b, " %2dh: ", (24 + gt->tm_hour - i) % 24);
175     if (MIX) {
176     if (PGP || UNENCRYPTED)
177     buf_appends(b, " Mix:");
178     buf_appendf(b, "%4d", msgd[2][i]);
179     }
180     if (PGP)
181     buf_appendf(b, " PGP: %4d", msgd[1][i]);
182     if (UNENCRYPTED)
183     buf_appendf(b, " Unencrypted:%4d", msgd[0][i]);
184     if (poold[0][i] > 0)
185     buf_appendf(b, " [Pool size:%4d]", poold[1][i] / poold[0][i]);
186     #if 0
187     else
188     buf_appends(b, " [ no remailing ]");
189 rabbi 262 #endif /* 0 */
190 rabbi 1 buf_nl(b);
191     }
192     if ((today - havestats) / SECONDSPERDAY >= 1)
193     buf_appends(b, "\nNumber of messages per day:\n");
194     for ((i = (today - havestats) / SECONDSPERDAY) > 79 ? 79 : i;
195     i >= 1; i--) {
196     t = now - i * SECONDSPERDAY;
197     gt = localtime(&t);
198     strftime(line, LINELEN, "%d %b: ", gt);
199     buf_appends(b, line);
200    
201     if (MIX) {
202     if (PGP || UNENCRYPTED)
203     buf_appends(b, " Mix:");
204     buf_appendf(b, "%4d", msg[2][i]);
205     }
206     if (PGP)
207     buf_appendf(b, " PGP: %4d", msg[1][i]);
208     if (UNENCRYPTED)
209     buf_appendf(b, " Unencrypted:%4d", msg[0][i]);
210     if (pool[0][i] > 0)
211     buf_appendf(b, " [Pool size:%4d]", pool[1][i] / pool[0][i]);
212     #if 0
213     else
214     buf_appends(b, " [ no remailing ]");
215 rabbi 262 #endif /* 0 */
216 rabbi 1 buf_nl(b);
217     }
218     }
219     return (0);
220     }
221    
222     int conf(BUFFER *out)
223     {
224     FILE *f;
225     BUFFER *b, *line;
226     int flag = 0;
227 weaselp 171 REMAILER remailer[MAXREM];
228     int pgpkeyid[MAXREM];
229     int i, num;
230     char tmpline[LINELEN];
231    
232 rabbi 1 b = buf_new();
233     line = buf_new();
234    
235     buf_sets(out, "Subject: Capabilities of the ");
236     buf_appends(out, SHORTNAME);
237     buf_appends(out, " remailer\n\n");
238     buf_appends(out, remailer_type);
239     buf_appends(out, VERSION);
240     buf_nl(out);
241    
242     if (MIX + PGP + UNENCRYPTED == 1)
243     buf_appends(out, "Supported format:");
244     else
245     buf_appends(out, "Supported formats:\n");
246     if (MIX)
247     buf_appends(out, " Mixmaster\n");
248     if (PGP)
249     buf_appends(out, " Cypherpunk with PGP encryption\n");
250     if (UNENCRYPTED)
251     buf_appends(out, " Cypherpunk (unencrypted)\n");
252    
253     buf_appendf(out, "Pool size: %d\n", POOLSIZE);
254     if (SIZELIMIT)
255     buf_appendf(out, "Maximum message size: %d kB\n", SIZELIMIT);
256    
257 rabbi 143 /* display destinations to which delivery is explicitly permitted
258 weaselp 149 when in middleman mode (contents of DESTALLOW file.) */
259 rabbi 143
260 weaselp 90 if (MIDDLEMAN) {
261     f = mix_openfile(DESTALLOW, "r");
262     if (f != NULL) {
263     buf_read(b, f);
264     fclose(f);
265     while(buf_getline(b, line) != -1) {
266     if (line->length > 0 && line->data[0] != '#') {
267     if (flag == 0) {
268 rabbi 143 buf_appends(out, "In addition to other remailers, this remailer also sends mail to these\n addresses directly:\n");
269 weaselp 90 flag = 1;
270     }
271     buf_appendf(out, " %b\n", line);
272     }
273     }
274     }
275     }
276    
277 weaselp 107 flag = 0;
278 rabbi 1 f = mix_openfile(HDRFILTER, "r");
279     if (f != NULL) {
280     buf_read(b, f);
281     fclose(f);
282     while(buf_getline(b, line) != -1)
283     if (line->length > 0 && line->data[0] != '#') {
284     if (flag == 0) {
285     buf_appends(out, "The following header lines will be filtered:\n");
286     flag = 1;
287     }
288     buf_appends(out, " ");
289     if (line->length > 3 && streq(line->data + line->length - 2, "/q")) {
290     buf_append(out, line->data, line->length - 1);
291     buf_appends(out, " => delete message");
292     }
293     else
294     buf_cat(out, line);
295     buf_nl(out);
296     }
297     buf_free(b);
298     }
299     flag = 0;
300     b = readdestblk( );
301     if ( b != NULL ) {
302     while(buf_getline(b, line) != -1)
303     if (line->length > 0 && !bufleft(line, "#") && !buffind(line, "@")) {
304     /* mail addresses are not listed */
305     if (flag == 0) {
306     if (NEWS[0])
307     buf_appends(out,
308     "The following newsgroups/domains are blocked:\n");
309     else
310     buf_appends(out, "The following domains are blocked:\n");
311     flag = 1;
312     }
313     buf_appendf(out, " %b\n", line);
314     }
315     if (flag == 0 && NEWS[0])
316     buf_appends(out, "Note that other newsgroups may be unavailable at the remailer's news server.\n");
317     }
318    
319     buf_nl(out);
320     conf_premail(out);
321 weaselp 168
322     if (LISTSUPPORTED) {
323     /* SUPPORTED CPUNK (TYPE I) REMAILERS
324     * 0xDC7532F9 "Heex Remailer <remailer@xmailer.ods.org>"
325     * 0x759ED311 "znar <ka5tkn@cox-internet.com>"
326     *
327     * SUPPORTED MIXMASTER (TYPE II) REMAILERS
328     * aarg remailer@aarg.net 475f3f9fe8da22896c10082695a92c2d 2.9b33 C
329     * anon mixmaster@anon.978.org 7384ba1eec585bfd7d2b0e9b307f0b1d 2.9b36 MCNm
330     */
331    
332 weaselp 169 buf_nl(out);
333     if (PGP) {
334     buf_appends(out, "SUPPORTED CPUNK (TYPE I) REMAILERS\n");
335 weaselp 171 num = t1_rlist(remailer);
336     pgp_rkeylist(remailer, pgpkeyid, num);
337     for (i=1; i<=num; i++) {
338     if (remailer[i].flags.pgp) {
339     snprintf(tmpline, LINELEN, "0x%08X \"%s <%s>\"\n", pgpkeyid[i], remailer[i].name, remailer[i].addr);
340     tmpline[LINELEN-1] = '\0';
341     buf_appends(out, tmpline);
342     }
343     }
344 weaselp 169 buf_nl(out);
345     }
346     if (MIX) {
347     buf_appends(out, "SUPPORTED MIXMASTER (TYPE II) REMAILERS\n");
348     prepare_type2list(out);
349     buf_nl(out);
350     }
351 weaselp 168 }
352    
353    
354 rabbi 1 if ( b ) buf_free(b);
355     buf_free(line);
356     return (0);
357     }
358    
359     void conf_premail(BUFFER *out)
360     {
361     buf_appends(out, "$remailer{\"");
362     buf_appends(out, SHORTNAME);
363     buf_appends(out, "\"} = \"<");
364     buf_appends(out, REMAILERADDR);
365     buf_appendc(out, '>');
366     if (PGP || UNENCRYPTED)
367     buf_appends(out, " cpunk");
368     if (MIX)
369     buf_appends(out, " mix");
370     if (MIDDLEMAN)
371     buf_appends(out, " middle");
372     if (PGP)
373     buf_appends(out, " pgp");
374     if (PGP && !UNENCRYPTED)
375     buf_appends(out, " pgponly");
376 rabbi 64 if (PGP && REPGP) {
377 rabbi 62 if (REMIX == 1)
378     buf_appends(out, " repgp");
379     else
380     buf_appends(out, " repgp2");
381     }
382 rabbi 1 if (REMIX == 1)
383     buf_appends(out, " remix");
384     else if (REMIX)
385     buf_appends(out, " remix2");
386     if (PGP || UNENCRYPTED)
387     buf_appends(out, " latent hash cut test");
388     if (PGP) {
389     #ifdef USE_IDEA
390     buf_appends(out, " ek");
391 rabbi 262 #endif /* USE_IDEA */
392 rabbi 1 buf_appends(out, " ekx");
393     }
394     #ifdef USE_IDEA
395     buf_appends(out, " esub");
396 rabbi 262 #endif /* USE_IDEA */
397 rabbi 1 #if 0 /* obsolete */
398     #ifdef USE_NSUB
399     buf_appends(out, " nsub");
400 rabbi 262 #else /* end of USE_NSUB */
401 rabbi 1 buf_appends(out, " ksub");
402 rabbi 262 #endif /* else if not USE_NSUB */
403     #endif /* 0 */
404 rabbi 1 if (INFLATEMAX)
405     buf_appendf(out, " inflt%d", INFLATEMAX);
406     if (MAXRANDHOPS)
407     buf_appendf(out, " rhop%d", MAXRANDHOPS);
408     if (POOLSIZE >= 5)
409     buf_appends(out, " reord");
410     if (NEWS[0])
411     buf_appends(out, " post");
412     if (SIZELIMIT)
413     buf_appendf(out, " klen%d", SIZELIMIT);
414     buf_appends(out, "\";\n");
415     }

  ViewVC Help
Powered by ViewVC 1.1.5