/[darkstattype]/trunk/darkstattype.c
ViewVC logotype

Annotation of /trunk/darkstattype.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide annotations) (download)
2014-04-09T10:31:01Z (9 years, 11 months ago) by trond
Content type: text/plain
File size: 25250 byte(s)
stable/9 has switched to clang 3.4. Silly mistake.

1 trond 16 /* -*- coding: utf-8 -*-
2 trond 3 darkstattype.c - Program to decode darkstat's dumpfile.
3    
4     $Ximalas$
5    
6 trond 20 Tested on FreeBSD/i386 stable/8 r255624 with gcc 4.2.1.
7 trond 21 Tested on FreeBSD/amd64 stable/9 r263963 with clang 3.4.
8     Tested on FreeBSD/amd64 stable/9 r263963 with clang 3.4 using -m32.
9 trond 3
10 trond 16 Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>
11 trond 3 All rights reserved.
12    
13     Redistribution and use in source and binary forms, with or without
14     modification, are permitted provided that the following conditions are met:
15    
16     1. Redistributions of source code must retain the above copyright notice, this
17     list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19     this list of conditions and the following disclaimer in the documentation
20     and/or other materials provided with the distribution.
21    
22     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32     */
33    
34     #include <errno.h>
35 trond 9 #include <stdbool.h>
36 trond 15 #include <stdint.h>
37 trond 3 #include <stdio.h>
38     #include <stdlib.h>
39     #include <string.h>
40     #include <time.h>
41     #include <unistd.h>
42    
43 trond 6 const char *progname = NULL;
44     const char *filename = NULL;
45 trond 3 FILE *file = NULL;
46 trond 9 bool follow_specification = false;
47 trond 3
48     int main(int argc, char **argv)
49     {
50     void show_usage(int exitcode);
51     void show_version(void);
52     void decode_file(void);
53    
54     int i;
55    
56     progname = argv[0];
57    
58     opterr = 0;
59 trond 9 while ( (i = getopt(argc, argv, "fhv")) != -1) {
60 trond 3 switch (i) {
61 trond 9 case 'f':
62     follow_specification = !follow_specification;
63     break;
64    
65 trond 3 case 'h':
66     show_usage(EXIT_SUCCESS);
67     break;
68    
69     case 'v':
70     show_version();
71     break;
72    
73     case '?':
74     /*FALLTHROUGH*/
75     default:
76 trond 19 fprintf(stderr, "%s: illegal option -%c\n\n",
77     progname, optopt);
78 trond 3 show_usage(EXIT_FAILURE);
79     break;
80     } // switch
81     } // while
82     argc -= optind;
83     argv += optind;
84    
85 trond 14 if (*argv == NULL) {
86 trond 19 fprintf(stderr, "%s: missing filename\n\n",
87     progname);
88 trond 3 show_usage(EXIT_FAILURE);
89     } // if
90    
91 trond 14 while (*argv != NULL) {
92     filename = *argv;
93 trond 3
94 trond 14 decode_file();
95 trond 3
96 trond 14 argv++;
97     } // while
98    
99 trond 3 return EXIT_SUCCESS;
100     } // main()
101    
102     void show_usage(int exitcode)
103     {
104     fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr,
105 trond 19 "Usage: %s [-f | -h | -v] filename [...]\n"
106 trond 8 " E.g.: %s darkstat.db\n\n"
107    
108     "Options:\n"
109 trond 9 "-f\tToggle follow strict specification, default is false.\n"
110 trond 8 "-h\tShow this help message and exit.\n"
111     "-v\tShow version and copyright and exit.\n",
112 trond 3 progname, progname);
113    
114     exit(exitcode);
115     } // show_usage()
116    
117     void show_version(void)
118     {
119     puts("darkstattype 1.0");
120 trond 4 puts("$Ximalas$");
121 trond 3 puts("");
122    
123 trond 16 puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");
124 trond 3 puts("All rights reserved.");
125     puts("");
126     puts("Redistribution and use in source and binary forms, with or without");
127     puts("modification, are permitted provided that the following conditions are met:");
128     puts("");
129     puts("1. Redistributions of source code must retain the above copyright notice, this");
130     puts(" list of conditions and the following disclaimer.");
131     puts("2. Redistributions in binary form must reproduce the above copyright notice,");
132     puts(" this list of conditions and the following disclaimer in the documentation");
133     puts(" and/or other materials provided with the distribution.");
134     puts("");
135     puts("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND");
136     puts("ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED");
137     puts("WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE");
138     puts("DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR");
139     puts("ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES");
140     puts("(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;");
141     puts("LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND");
142     puts("ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT");
143     puts("(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS");
144     puts("SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.");
145    
146     exit(EXIT_SUCCESS);
147     } // show_version()
148    
149 trond 15 uint8_t read8u(void);
150     int8_t read8s(void);
151     uint16_t read16u(void);
152     int16_t read16s(void);
153     uint32_t read32u(void);
154     int32_t read32s(void);
155     uint64_t read64u(void);
156     int64_t read64s(void);
157 trond 3
158     void indent(void);
159     void exdent(void);
160     void print_indentation(void);
161    
162     void print_time_t(time_t t);
163    
164     void decode_file(void)
165     {
166     void decode_host_db_v1(void);
167     void decode_graph_db_v1(void);
168    
169 trond 15 uint32_t fileheader;
170     uint32_t sectionheader;
171 trond 3
172 trond 15 uint32_t i;
173 trond 3
174     if ( (file = fopen(filename, "rb")) == NULL) {
175 trond 19 fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n",
176     progname, filename, strerror(errno), errno);
177 trond 3 exit(EXIT_FAILURE);
178     } // if
179    
180 trond 6 #define FILE_HEADER_V1 0xDA314159U
181    
182     if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format
183 trond 19 fprintf(stderr, "%s:%s:%ld: file header = 0x%x, not 0x%x\n",
184     progname, filename, ftell(file), fileheader, FILE_HEADER_V1);
185 trond 3 exit(EXIT_FAILURE);
186     } // if
187    
188     printf("File header 0x%x\n", fileheader);
189    
190     // Possible section header for host_db v1 and later graph_db v1.
191     indent();
192    
193 trond 6 #define HOST_DB_V1 0xDA485301U
194     #define GRAPH_DB_V1 0xDA475201U
195    
196 trond 3 for (i = 0; i < 2; i++) {
197 trond 6 if ( (sectionheader = read32u()) == HOST_DB_V1) {
198 trond 3 print_indentation();
199 trond 6 printf("Section header host_db v1 0x%x\n", sectionheader);
200 trond 3 decode_host_db_v1();
201     } // if
202 trond 6 else if (sectionheader == GRAPH_DB_V1) {
203 trond 3 print_indentation();
204 trond 6 printf("Section header graph_db v1 0x%x\n", sectionheader);
205 trond 3 decode_graph_db_v1();
206     } // else if
207     else {
208 trond 19 fprintf(stderr,
209     "%s:%s:%ld: unknown section header = 0x%x, neither 0x%x nor 0x%x\n",
210     progname, filename, ftell(file), sectionheader, HOST_DB_V1,
211     GRAPH_DB_V1);
212 trond 3 exit(EXIT_FAILURE);
213     } // else
214     } // for
215    
216     exdent();
217 trond 14
218     if (fclose(file) != 0) {
219 trond 19 fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n",
220     progname, filename, strerror(errno), errno);
221 trond 14 exit(EXIT_FAILURE);
222     } // if
223 trond 3 } // decode_file()
224    
225     void decode_host_db_v1(void)
226     {
227     void decode_host_header_v1(void);
228     void decode_host_header_v2(void);
229     void decode_host_header_v3(void);
230    
231 trond 15 uint32_t hostcount;
232     uint32_t i;
233 trond 3
234     indent();
235    
236     hostcount = read32u();
237    
238     print_indentation();
239 trond 19 printf("Host count %u\n",
240     hostcount);
241 trond 3
242     for (i = 0; i < hostcount; i++) {
243 trond 15 uint32_t hostheader;
244 trond 3
245     print_indentation();
246 trond 19 printf("Host #%u of %u:\n",
247     i + 1, hostcount);
248 trond 3
249     indent();
250    
251 trond 6 #define HOST_HEADER_V3 0x48535403U
252     #define HOST_HEADER_V2 0x48535402U
253     #define HOST_HEADER_V1 0x48535401U
254    
255     if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3
256 trond 3 print_indentation();
257 trond 19 printf("Host header v3 0x%x\n",
258     hostheader);
259 trond 3 decode_host_header_v3();
260     } // if
261 trond 6 else if (hostheader == HOST_HEADER_V2) { // host header v2
262 trond 3 print_indentation();
263 trond 19 printf("Host header v2 0x%x\n",
264     hostheader);
265 trond 3 decode_host_header_v2();
266     } // else if
267 trond 6 else if (hostheader == HOST_HEADER_V1) { // host header v1
268 trond 3 print_indentation();
269 trond 19 printf("Host header v1 0x%x\n",
270     hostheader);
271 trond 3 decode_host_header_v1();
272     } // else if
273     else { // unknown host header version
274 trond 19 fprintf(stderr,
275     "%s:%s:%ld: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n",
276     progname, filename, ftell(file), hostheader, HOST_HEADER_V3,
277     HOST_HEADER_V2, HOST_HEADER_V1);
278 trond 3 exit(EXIT_FAILURE);
279     } // else
280    
281     exdent();
282     } // for
283    
284     exdent();
285     } // decode_host_db_v1()
286    
287     void decode_protos_data(void);
288     void decode_tcp_data(void);
289     void decode_udp_data(void);
290    
291     void decode_host_header_v1(void)
292     {
293 trond 15 uint8_t ipv4address[4];
294     uint8_t macaddress[6];
295     uint8_t hostnamelen;
296     uint8_t hostname[256];
297     uint64_t bytesin;
298     uint64_t bytesout;
299     uint8_t protosdata;
300     uint8_t tcpdata;
301     uint8_t udpdata;
302 trond 3
303 trond 15 uint8_t i;
304 trond 3
305     indent();
306    
307     ipv4address[0] = read8u();
308     ipv4address[1] = read8u();
309     ipv4address[2] = read8u();
310     ipv4address[3] = read8u();
311    
312     print_indentation();
313 trond 19 printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n",
314 trond 3 ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
315    
316     macaddress[0] = read8u();
317     macaddress[1] = read8u();
318     macaddress[2] = read8u();
319     macaddress[3] = read8u();
320     macaddress[4] = read8u();
321     macaddress[5] = read8u();
322    
323     print_indentation();
324 trond 19 printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
325     macaddress[0], macaddress[1], macaddress[2],
326     macaddress[3], macaddress[4], macaddress[5]);
327 trond 3
328     hostnamelen = read8u();
329    
330     print_indentation();
331 trond 19 printf("Hostname length %u\n",
332     hostnamelen);
333 trond 3
334     for (i = 0; i < hostnamelen; i++) {
335     hostname[i] = read8u();
336     } // for
337     hostname[i] = '\0';
338    
339     print_indentation();
340 trond 19 printf("Hostname %s\n",
341     hostname);
342 trond 3
343     bytesin = read64u();
344    
345     print_indentation();
346 trond 20 printf("Bytes in %llu\n",
347     (unsigned long long)bytesin);
348 trond 3
349     bytesout = read64u();
350    
351     print_indentation();
352 trond 20 printf("Bytes out %llu\n",
353     (unsigned long long)bytesout);
354 trond 3
355     if ( (protosdata = read8u()) != 'P') { // missing protos data
356 trond 19 fprintf(stderr,
357     "%s:%s:%ld: expecting character P, not %c\n",
358     progname, filename, ftell(file), protosdata);
359 trond 3 exit(EXIT_FAILURE);
360     } // if
361    
362     decode_protos_data();
363    
364     if ( (tcpdata = read8u()) != 'T') { // missing tcp data
365 trond 19 fprintf(stderr,
366     "%s:%s:%ld: expecting character T, not %c\n",
367     progname, filename, ftell(file), tcpdata);
368 trond 3 exit(EXIT_FAILURE);
369     } // if
370    
371     decode_tcp_data();
372    
373     if ( (udpdata = read8u()) != 'U') { // missing udp data
374 trond 19 fprintf(stderr,
375     "%s:%s:%ld: expecting character U, not %c\n",
376     progname, filename, ftell(file), udpdata);
377 trond 3 exit(EXIT_FAILURE);
378     } // if
379    
380     decode_udp_data();
381    
382     exdent();
383     } // decode_host_header_v1
384    
385     void decode_host_header_v2(void)
386     {
387 trond 15 uint8_t ipv4address[4];
388     uint8_t macaddress[6];
389     int64_t lastseen;
390     uint8_t hostnamelen;
391     uint8_t hostname[256];
392     uint64_t bytesin;
393     uint64_t bytesout;
394     uint8_t protosdata;
395     uint8_t tcpdata;
396     uint8_t udpdata;
397 trond 3
398 trond 15 uint8_t i;
399 trond 3
400     indent();
401    
402     ipv4address[0] = read8u();
403     ipv4address[1] = read8u();
404     ipv4address[2] = read8u();
405     ipv4address[3] = read8u();
406    
407     print_indentation();
408 trond 19 printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n",
409 trond 3 ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
410    
411 trond 9 if (follow_specification == true) {
412     macaddress[0] = read8u();
413     macaddress[1] = read8u();
414     macaddress[2] = read8u();
415     macaddress[3] = read8u();
416     macaddress[4] = read8u();
417     macaddress[5] = read8u();
418 trond 3
419 trond 9 print_indentation();
420 trond 19 printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
421     macaddress[0], macaddress[1], macaddress[2],
422     macaddress[3], macaddress[4], macaddress[5]);
423 trond 3
424 trond 9 lastseen = read64s();
425 trond 3
426 trond 9 print_indentation();
427 trond 20 printf("Last seen 0x%llx = %lld = ",
428     (unsigned long long)lastseen, (long long)lastseen);
429 trond 9 print_time_t(lastseen);
430     puts("");
431     } // if
432     else {
433     lastseen = read64s();
434 trond 3
435 trond 9 print_indentation();
436 trond 20 printf("Last seen 0x%llx = %lld = ",
437     (unsigned long long)lastseen, (long long)lastseen);
438 trond 9 print_time_t(lastseen);
439     puts("");
440 trond 7
441 trond 9 macaddress[0] = read8u();
442     macaddress[1] = read8u();
443     macaddress[2] = read8u();
444     macaddress[3] = read8u();
445     macaddress[4] = read8u();
446     macaddress[5] = read8u();
447 trond 7
448 trond 9 print_indentation();
449 trond 19 printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
450     macaddress[0], macaddress[1], macaddress[2],
451     macaddress[3], macaddress[4], macaddress[5]);
452 trond 9 } // else
453 trond 7
454 trond 3 hostnamelen = read8u();
455    
456     print_indentation();
457 trond 19 printf("Hostname length %hhu\n",
458     hostnamelen);
459 trond 3
460     for (i = 0; i < hostnamelen; i++) {
461     hostname[i] = read8u();
462     } // for
463     hostname[i] = '\0';
464    
465     print_indentation();
466 trond 19 printf("Hostname %s\n",
467     hostname);
468 trond 3
469     bytesin = read64u();
470    
471     print_indentation();
472 trond 20 printf("Bytes in %llu\n",
473     (unsigned long long)bytesin);
474 trond 3
475     bytesout = read64u();
476    
477     print_indentation();
478 trond 20 printf("Bytes out %llu\n",
479     (unsigned long long)bytesout);
480 trond 3
481     if ( (protosdata = read8u()) != 'P') { // missing protos data
482 trond 19 fprintf(stderr,
483     "%s:%s:%ld: expecting character P, not %c\n",
484     progname, filename, ftell(file), protosdata);
485 trond 3 exit(EXIT_FAILURE);
486     } // if
487    
488     decode_protos_data();
489    
490     if ( (tcpdata = read8u()) != 'T') { // missing tcp data
491 trond 19 fprintf(stderr,
492     "%s:%s:%ld: expecting character T, not %c\n",
493     progname, filename, ftell(file), tcpdata);
494 trond 3 exit(EXIT_FAILURE);
495     } // if
496    
497     decode_tcp_data();
498    
499     if ( (udpdata = read8u()) != 'U') { // missing udp data
500 trond 19 fprintf(stderr,
501     "%s:%s:%ld: expecting character U, not %c\n",
502     progname, filename, ftell(file), udpdata);
503 trond 3 exit(EXIT_FAILURE);
504     } // if
505    
506     decode_udp_data();
507    
508     exdent();
509     } // decode_host_header_v2
510    
511     void decode_host_header_v3(void)
512     {
513 trond 15 uint8_t addressfamily;
514     uint8_t ipv4address[4];
515     uint16_t ipv6address[8];
516     uint8_t macaddress[6];
517     int64_t lastseen;
518     uint8_t hostnamelen;
519     uint8_t hostname[256];
520     uint64_t bytesin;
521     uint64_t bytesout;
522     uint8_t protosdata;
523     uint8_t tcpdata;
524     uint8_t udpdata;
525 trond 3
526 trond 15 uint8_t i;
527 trond 3
528     indent();
529    
530 trond 19 #define A_F_4 0x04
531     #define A_F_6 0x06
532    
533     if ( (addressfamily = read8u()) == A_F_4) { // IPv4 address
534 trond 3 print_indentation();
535 trond 19 printf("IPv4 address family (0x%02hhx)\n",
536     addressfamily);
537 trond 3
538     ipv4address[0] = read8u();
539     ipv4address[1] = read8u();
540     ipv4address[2] = read8u();
541     ipv4address[3] = read8u();
542    
543     print_indentation();
544 trond 19 printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n",
545 trond 3 ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
546     } // if
547 trond 19 else if (addressfamily == A_F_6) { // IPv6 address
548 trond 3 print_indentation();
549 trond 19 printf("IPv6 address family (0x%02hhx)\n",
550     addressfamily);
551 trond 3
552 trond 12 ipv6address[0] = read16u();
553     ipv6address[1] = read16u();
554     ipv6address[2] = read16u();
555     ipv6address[3] = read16u();
556     ipv6address[4] = read16u();
557     ipv6address[5] = read16u();
558     ipv6address[6] = read16u();
559     ipv6address[7] = read16u();
560 trond 3
561     print_indentation();
562 trond 19 printf("IPv6 address %04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx\n",
563     ipv6address[0], ipv6address[1], ipv6address[2], ipv6address[3],
564     ipv6address[4], ipv6address[5], ipv6address[6], ipv6address[7]);
565 trond 3 } // else if
566     else { // unknown address family
567 trond 19 fprintf(stderr,
568     "%s:%s:%ld: unknown address family = 0x%hhx, neither 0x%x nor 0x%x\n",
569     progname, filename, ftell(file), addressfamily, A_F_4, A_F_6);
570 trond 3 exit(EXIT_FAILURE);
571     } // else
572    
573 trond 9 if (follow_specification == true) {
574     macaddress[0] = read8u();
575     macaddress[1] = read8u();
576     macaddress[2] = read8u();
577     macaddress[3] = read8u();
578     macaddress[4] = read8u();
579     macaddress[5] = read8u();
580 trond 3
581 trond 9 print_indentation();
582 trond 19 printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
583     macaddress[0], macaddress[1], macaddress[2],
584     macaddress[3], macaddress[4], macaddress[5]);
585 trond 3
586 trond 9 lastseen = read64s();
587 trond 3
588 trond 9 print_indentation();
589 trond 20 printf("Last seen 0x%llx = %lld = ",
590     (unsigned long long)lastseen, (long long)lastseen);
591 trond 9 print_time_t(lastseen);
592     puts("");
593     } // if
594     else {
595     lastseen = read64s();
596 trond 3
597 trond 9 print_indentation();
598 trond 20 printf("Last seen 0x%llx = %lld = ",
599     (unsigned long long)lastseen, (long long)lastseen);
600 trond 9 print_time_t(lastseen);
601     puts("");
602 trond 7
603 trond 9 macaddress[0] = read8u();
604     macaddress[1] = read8u();
605     macaddress[2] = read8u();
606     macaddress[3] = read8u();
607     macaddress[4] = read8u();
608     macaddress[5] = read8u();
609 trond 7
610 trond 9 print_indentation();
611 trond 19 printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
612     macaddress[0], macaddress[1], macaddress[2],
613     macaddress[3], macaddress[4], macaddress[5]);
614 trond 9 } // else
615 trond 7
616 trond 3 hostnamelen = read8u();
617    
618     print_indentation();
619 trond 19 printf("Hostname length %hhu\n",
620     hostnamelen);
621 trond 3
622     for (i = 0; i < hostnamelen; i++) {
623     hostname[i] = read8u();
624     } // for
625     hostname[i] = '\0';
626    
627     print_indentation();
628 trond 19 printf("Hostname %s\n",
629     hostname);
630 trond 3
631     bytesin = read64u();
632    
633     print_indentation();
634 trond 20 printf("Bytes in %llu\n",
635     (unsigned long long)bytesin);
636 trond 3
637     bytesout = read64u();
638    
639     print_indentation();
640 trond 20 printf("Bytes out %llu\n",
641     (unsigned long long)bytesout);
642 trond 3
643     if ( (protosdata = read8u()) != 'P') { // missing protos data
644 trond 19 fprintf(stderr,
645     "%s:%s:%ld: expecting character P, not %c\n",
646     progname, filename, ftell(file), protosdata);
647 trond 3 exit(EXIT_FAILURE);
648     } // if
649    
650     decode_protos_data();
651    
652     if ( (tcpdata = read8u()) != 'T') { // missing tcp data
653 trond 19 fprintf(stderr,
654     "%s:%s:%ld: expecting character T, not %c\n",
655     progname, filename, ftell(file), tcpdata);
656 trond 3 exit(EXIT_FAILURE);
657     } // if
658    
659     decode_tcp_data();
660    
661     if ( (udpdata = read8u()) != 'U') { // missing udp data
662 trond 19 fprintf(stderr,
663     "%s:%s:%ld: expecting character U, not %c\n",
664     progname, filename, ftell(file), udpdata);
665 trond 3 exit(EXIT_FAILURE);
666     } // if
667    
668     decode_udp_data();
669    
670     exdent();
671     } // decode_host_header_v3
672    
673     void decode_protos_data(void)
674     {
675 trond 15 uint8_t ipprotocount;
676 trond 3
677 trond 15 uint8_t i;
678 trond 3
679     ipprotocount = read8u();
680    
681     print_indentation();
682 trond 19 printf("IP Proto count %hhu\n",
683     ipprotocount);
684 trond 3
685     indent();
686    
687 trond 13 for (i = 0; i < ipprotocount; i++) {
688 trond 15 uint8_t proto;
689     uint64_t in;
690     uint64_t out;
691 trond 3
692     print_indentation();
693 trond 19 printf("Protocol #%u of %hhu:\n",
694     i + 1, ipprotocount);
695 trond 3
696     proto = read8u();
697    
698     indent();
699     print_indentation();
700 trond 19 printf("Protocol 0x%02hhx\n",
701     proto);
702 trond 3
703     in = read64u();
704    
705     print_indentation();
706 trond 20 printf("In %llu\n",
707     (unsigned long long)in);
708 trond 3
709     out = read64u();
710    
711     print_indentation();
712 trond 20 printf("Out %llu\n",
713     (unsigned long long)out);
714 trond 3
715     exdent();
716     } // for
717    
718     exdent();
719     } // decode_protos_data();
720    
721     void decode_tcp_data(void)
722     {
723 trond 15 uint8_t tcpprotocount;
724 trond 3
725 trond 15 uint16_t i;
726 trond 3
727     tcpprotocount = read16u();
728    
729     print_indentation();
730 trond 19 printf("TCP proto count %hhu\n",
731     tcpprotocount);
732 trond 3
733     indent();
734    
735     for (i = 0; i < tcpprotocount; i++) {
736 trond 15 uint16_t port;
737     uint64_t syn;
738     uint64_t in;
739     uint64_t out;
740 trond 3
741     port = read16u();
742    
743     print_indentation();
744 trond 19 printf("Port %hu:\n",
745     port);
746 trond 3
747     syn = read64u();
748    
749     indent();
750     print_indentation();
751 trond 20 printf("SYN %llu\n",
752     (unsigned long long)syn);
753 trond 3
754     in = read64u();
755    
756     print_indentation();
757 trond 20 printf("In %llu\n",
758     (unsigned long long)in);
759 trond 3
760     out = read64u();
761    
762     print_indentation();
763 trond 20 printf("Out %llu\n",
764     (unsigned long long)out);
765 trond 3
766     exdent();
767     } // for
768    
769     exdent();
770     } // decode_tcp_data()
771    
772     void decode_udp_data(void)
773     {
774 trond 15 uint8_t udpprotocount;
775 trond 3
776 trond 15 uint16_t i;
777 trond 3
778     udpprotocount = read16u();
779    
780     print_indentation();
781 trond 19 printf("UDP proto count %hhu\n",
782     udpprotocount);
783 trond 3
784     indent();
785    
786     for (i = 0; i < udpprotocount; i++) {
787 trond 15 uint16_t port;
788     uint64_t in;
789     uint64_t out;
790 trond 3
791     port = read16u();
792    
793     print_indentation();
794 trond 19 printf("Port %hu:\n",
795     port);
796 trond 3
797     in = read64u();
798    
799     indent();
800     print_indentation();
801 trond 20 printf("In %llu\n",
802     (unsigned long long)in);
803 trond 3
804     out = read64u();
805    
806     print_indentation();
807 trond 20 printf("Out %llu\n",
808     (unsigned long long)out);
809 trond 3
810     exdent();
811     } // for
812    
813     exdent();
814     } // decode_udp_data()
815    
816     void decode_graph_db_v1(void)
817     {
818 trond 15 int64_t lasttime;
819 trond 3
820 trond 15 uint32_t i;
821 trond 3
822     lasttime = read64s();
823    
824     indent();
825     print_indentation();
826 trond 20 printf("Last time 0x%llx = %lld = ",
827     (unsigned long long)lasttime, (long long)lasttime);
828 trond 3 print_time_t(lasttime);
829     puts("");
830    
831     for (i = 0; i < 4; i++) {
832 trond 15 uint8_t nbars;
833     uint8_t idxlastbar;
834 trond 3
835 trond 15 uint32_t j;
836 trond 3
837     print_indentation();
838 trond 19 printf("Graph #%u of 4:\n",
839     i + 1);
840 trond 3
841     nbars = read8u();
842    
843     indent();
844     print_indentation();
845 trond 19 printf("Number of bars %hhu\n",
846     nbars);
847 trond 3
848     idxlastbar = read8u();
849    
850     print_indentation();
851 trond 19 printf("Index of last bar %hhu\n",
852     idxlastbar);
853 trond 3
854     indent();
855     for (j = 0; j < idxlastbar; j++) {
856 trond 15 uint64_t in;
857     uint64_t out;
858 trond 3
859     print_indentation();
860 trond 19 printf("Bar #%u of %hhu:\n",
861     j + 1, idxlastbar);
862 trond 3
863     in = read64u();
864    
865     indent();
866     print_indentation();
867 trond 20 printf("In %llu\n",
868     (unsigned long long)in);
869 trond 3
870     out = read64u();
871    
872     print_indentation();
873 trond 20 printf("Out %llu\n",
874     (unsigned long long)out);
875 trond 3
876     exdent();
877     } // for
878     exdent();
879    
880     exdent();
881     } // for
882    
883     exdent();
884     } // decode_graph_db_v1()
885    
886     void handle_file_error(void);
887    
888 trond 15 uint8_t read8u(void)
889 trond 3 {
890     size_t r;
891 trond 15 uint8_t v;
892 trond 3
893     if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
894     handle_file_error();
895     } // if
896    
897     return v;
898     } // read8u()
899    
900 trond 15 int8_t read8s(void)
901 trond 3 {
902     size_t r;
903 trond 15 int8_t v;
904 trond 3
905     if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
906     handle_file_error();
907     } // if
908    
909     return v;
910     } // read8s()
911    
912     #ifdef __LITTLE_ENDIAN__
913     #include <netinet/in.h>
914     #endif
915    
916 trond 15 uint16_t read16u(void)
917 trond 3 {
918     size_t r;
919 trond 15 uint16_t v;
920 trond 3
921     if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
922     handle_file_error();
923     } // if
924    
925     #ifdef __LITTLE_ENDIAN__
926     v = ntohs(v);
927     #endif
928    
929     return v;
930     } // read16u()
931    
932 trond 15 int16_t read16s(void)
933 trond 3 {
934     size_t r;
935 trond 15 int16_t v;
936 trond 3
937     if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
938     handle_file_error();
939     } // if
940    
941     #ifdef __LITTLE_ENDIAN__
942     v = ntohs(v);
943     #endif
944    
945     return v;
946     } // read16s()
947    
948 trond 15 uint32_t read32u(void)
949 trond 3 {
950     size_t r;
951 trond 15 uint32_t v;
952 trond 3
953     if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
954     handle_file_error();
955     } // if
956    
957     #ifdef __LITTLE_ENDIAN__
958     v = ntohl(v);
959     #endif
960    
961     return v;
962     } // read32u()
963    
964 trond 15 int32_t read32s(void)
965 trond 3 {
966     size_t r;
967 trond 15 int32_t v;
968 trond 3
969     if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
970     handle_file_error();
971     } // if
972    
973     #ifdef __LITTLE_ENDIAN__
974     v = ntohl(v);
975     #endif
976    
977     return v;
978     } // read32s()
979    
980 trond 15 uint64_t read64u(void)
981 trond 3 {
982     size_t r;
983 trond 15 uint64_t v;
984 trond 3
985 trond 7 #ifdef __LITTLE_ENDIAN__
986 trond 15 uint64_t tmp;
987     uint32_t *p1 = (uint32_t *)&v;
988     uint32_t *p2 = (uint32_t *)&tmp;
989 trond 7 #endif
990    
991 trond 3 if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
992     handle_file_error();
993     } // if
994    
995     #ifdef __LITTLE_ENDIAN__
996 trond 7 p2[1] = ntohl(p1[0]);
997     p2[0] = ntohl(p1[1]);
998     v = tmp;
999 trond 3 #endif
1000    
1001     return v;
1002     } // read64u()
1003    
1004 trond 15 int64_t read64s(void)
1005 trond 3 {
1006     size_t r;
1007 trond 15 int64_t v;
1008 trond 3
1009 trond 7 #ifdef __LITTLE_ENDIAN__
1010 trond 15 int64_t tmp;
1011     int32_t *p1 = (int32_t *)&v;
1012     int32_t *p2 = (int32_t *)&tmp;
1013 trond 7 #endif
1014    
1015 trond 3 if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
1016     handle_file_error();
1017     } // if
1018    
1019     #ifdef __LITTLE_ENDIAN__
1020 trond 7 p2[1] = ntohl(p1[0]);
1021     p2[0] = ntohl(p1[1]);
1022     v = tmp;
1023 trond 3 #endif
1024    
1025     return v;
1026     } // read64s()
1027    
1028     void handle_file_error(void)
1029     {
1030 trond 10 int saved_errno = errno;
1031    
1032 trond 3 if (feof(file) != 0) {
1033 trond 19 fprintf(stderr,
1034     "%s:%s:%ld: premature end-of-file\n",
1035     progname, filename, ftell(file));
1036 trond 3 exit(EXIT_FAILURE);
1037     } // if
1038    
1039     if (ferror(file) != 0) {
1040 trond 19 fprintf(stderr,
1041     "%s:%s:%ld: file error, errno = %s (%d)\n",
1042     progname, filename, ftell(file),
1043     strerror(saved_errno), saved_errno);
1044 trond 3 exit(EXIT_FAILURE);
1045     } // if
1046     } // handle_file_error()
1047    
1048 trond 15 int64_t indentation = 0LL;
1049 trond 3
1050     void indent(void)
1051     {
1052 trond 15 if (indentation < (INT64_MAX - 2LL)) {
1053     indentation += 2LL;
1054     } // if
1055 trond 3 } // indent()
1056    
1057     void exdent(void)
1058     {
1059     indentation -= 2LL;
1060    
1061     if (indentation < 0LL) {
1062     indentation = 0LL;
1063     }// if
1064     } // exdent()
1065    
1066     void print_indentation(void)
1067     {
1068 trond 15 int64_t i;
1069 trond 3
1070     for (i = 0; i < indentation; i++) {
1071     putchar(' ');
1072     } // for
1073     } // print_indentation()
1074    
1075     void print_time_t(time_t t)
1076     {
1077     struct tm *stm;
1078     char buffer[1024];
1079    
1080     stm = gmtime(&t);
1081    
1082     // ISO 8601 format
1083     if (strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S%z", stm) == 0) {
1084 trond 10 int saved_errno = errno;
1085    
1086 trond 19 fprintf(stderr,
1087     "%s:%s:%ld: strftime() error, errno = %s (%d)\n",
1088     progname, filename, ftell(file),
1089     strerror(saved_errno), saved_errno);
1090 trond 3 exit(EXIT_FAILURE);
1091     } // if
1092    
1093     fputs(buffer, stdout);
1094     } // print_time_t()
1095    
1096     // darkstattype.c

Properties

Name Value
svn:eol-style native
svn:keywords Ximalas=%H
svn:mime-type text/plain

svn@ximalas.info
ViewVC Help
Powered by ViewVC 1.3.0-dev