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

Diff of /trunk/darkstattype.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 14 by trond, 2014-03-21T18:35:50Z Revision 16 by trond, 2014-03-21T19:15:01Z
# Line 1  Line 1 
1  /*  /*                                               -*- coding: utf-8 -*-
2   darkstattype.c - Program to decode darkstat's dumpfile.   darkstattype.c - Program to decode darkstat's dumpfile.
3    
4   $Ximalas$   $Ximalas$
5    
6   Tested on FreeBSD/amd64 stable/9 r263290 with clang 3.3.   Tested on FreeBSD/amd64 stable/9 r263290 with clang 3.3.
7    
8   Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>   Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>
9   All rights reserved.   All rights reserved.
10    
11   Redistribution and use in source and binary forms, with or without   Redistribution and use in source and binary forms, with or without
12   modification, are permitted provided that the following conditions are met:   modification, are permitted provided that the following conditions are met:
13    
14   1. Redistributions of source code must retain the above copyright notice, this   1. Redistributions of source code must retain the above copyright notice, this
15      list of conditions and the following disclaimer.      list of conditions and the following disclaimer.
16   2. Redistributions in binary form must reproduce the above copyright notice,   2. Redistributions in binary form must reproduce the above copyright notice,
17      this list of conditions and the following disclaimer in the documentation      this list of conditions and the following disclaimer in the documentation
18      and/or other materials provided with the distribution.      and/or other materials provided with the distribution.
19    
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */  */
31    
32  #include <errno.h>  #include <errno.h>
33  #include <stdbool.h>  #include <stdbool.h>
34    #include <stdint.h>
35  #include <stdio.h>  #include <stdio.h>
36  #include <stdlib.h>  #include <stdlib.h>
37  #include <string.h>  #include <string.h>
38  #include <time.h>  #include <time.h>
39  #include <unistd.h>  #include <unistd.h>
40    
41  const char *progname = NULL;  const char *progname = NULL;
42  const char *filename = NULL;  const char *filename = NULL;
43  FILE *file = NULL;  FILE *file = NULL;
44  bool follow_specification = false;  bool follow_specification = false;
45    
46  int main(int argc, char **argv)  int main(int argc, char **argv)
47  {  {
48    void show_usage(int exitcode);    void show_usage(int exitcode);
49    void show_version(void);    void show_version(void);
# Line 103  void show_version(void) Line 104  void show_version(void)
104      "Options:\n"      "Options:\n"
105      "-f\tToggle follow strict specification, default is false.\n"      "-f\tToggle follow strict specification, default is false.\n"
106      "-h\tShow this help message and exit.\n"      "-h\tShow this help message and exit.\n"
107      "-v\tShow version and copyright and exit.\n",      "-v\tShow version and copyright and exit.\n",
108      progname, progname);      progname, progname);
109    
110    exit(exitcode);    exit(exitcode);
111  } // show_usage()  } // show_usage()
112    
113  void show_version(void)  void show_version(void)
114  {  {
115    puts("darkstattype 1.0");    puts("darkstattype 1.0");
116    puts("$Ximalas$");    puts("$Ximalas$");
117    puts("");    puts("");
118    
119    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");
120    puts("All rights reserved.");    puts("All rights reserved.");
121    puts("");    puts("");
122    puts("Redistribution and use in source and binary forms, with or without");    puts("Redistribution and use in source and binary forms, with or without");
123    puts("modification, are permitted provided that the following conditions are met:");    puts("modification, are permitted provided that the following conditions are met:");
124    puts("");    puts("");
125    puts("1. Redistributions of source code must retain the above copyright notice, this");    puts("1. Redistributions of source code must retain the above copyright notice, this");
126    puts("   list of conditions and the following disclaimer.");    puts("   list of conditions and the following disclaimer.");
127    puts("2. Redistributions in binary form must reproduce the above copyright notice,");    puts("2. Redistributions in binary form must reproduce the above copyright notice,");
128    puts("   this list of conditions and the following disclaimer in the documentation");    puts("   this list of conditions and the following disclaimer in the documentation");
129    puts("   and/or other materials provided with the distribution.");    puts("   and/or other materials provided with the distribution.");
130    puts("");    puts("");
131    puts("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND");    puts("THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND");
132    puts("ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED");    puts("ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED");
133    puts("WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE");    puts("WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE");
134    puts("DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR");    puts("DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR");
135    puts("ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES");    puts("ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES");
136    puts("(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;");    puts("(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;");
137    puts("LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND");    puts("LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND");
138    puts("ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT");    puts("ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT");
139    puts("(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS");    puts("(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS");
140    puts("SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.");    puts("SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.");
141    
142    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
143  } // show_version()  } // show_version()
144    
145  /*  uint8_t  read8u(void);
146  With clang 3.3 on FreeBSD/amd64 stable/9 r263290:   int8_t  read8s(void);
147    uint16_t read16u(void);
148     int16_t read16s(void);
149    uint32_t read32u(void);
150     int32_t read32s(void);
151    uint64_t read64u(void);
152     int64_t read64s(void);
153    
 char  = 1 byte  =  8 bits  
 short = 2 bytes = 16 bits  
 int   = 4 bytes = 32 bits  
 long  = 8 bytes = 64 bits  
 */  
 unsigned  char read8u(void);  
   signed  char read8s(void);  
 unsigned short read16u(void);  
   signed short read16s(void);  
 unsigned   int read32u(void);  
   signed   int read32s(void);  
 unsigned  long read64u(void);  
   signed  long read64s(void);  
   
154  void indent(void);  void indent(void);
155  void exdent(void);  void exdent(void);
156  void print_indentation(void);  void print_indentation(void);
157    
158  void print_time_t(time_t t);  void print_time_t(time_t t);
159    
160  void decode_file(void)  void decode_file(void)
161  {  {
162    void decode_host_db_v1(void);    void decode_host_db_v1(void);
163    void decode_graph_db_v1(void);    void decode_graph_db_v1(void);
164    
165    unsigned int fileheader;    uint32_t fileheader;
166    unsigned int sectionheader;    uint32_t sectionheader;
167    
168    unsigned int i;    uint32_t i;
169    
170    if ( (file = fopen(filename, "rb")) == NULL) {    if ( (file = fopen(filename, "rb")) == NULL) {
171      fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno);
172      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
173    } // if    } // if
174    
175  #define FILE_HEADER_V1 0xDA314159U  #define FILE_HEADER_V1 0xDA314159U
176    
177    if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format    if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format
178      fprintf(stderr, "%s:%s:%ld: file header = 0x%x, not 0x%x\n", progname, filename, ftell(file), fileheader, FILE_HEADER_V1);      fprintf(stderr, "%s:%s:%ld: file header = 0x%x, not 0x%x\n", progname, filename, ftell(file), fileheader, FILE_HEADER_V1);
179      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
180    } // if    } // if
181    
182    printf("File header 0x%x\n", fileheader);    printf("File header 0x%x\n", fileheader);
183    
# Line 213  void decode_host_db_v1(void) Line 206  void decode_host_db_v1(void)
206    
207    exdent();    exdent();
208    
209    if (fclose(file) != 0) {    if (fclose(file) != 0) {
210      fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno);
211      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
212    } // if    } // if
213  } // decode_file()  } // decode_file()
214    
215  void decode_host_db_v1(void)  void decode_host_db_v1(void)
216  {  {
217    void decode_host_header_v1(void);    void decode_host_header_v1(void);
218    void decode_host_header_v2(void);    void decode_host_header_v2(void);
219    void decode_host_header_v3(void);    void decode_host_header_v3(void);
220    
221    unsigned int hostcount;    uint32_t hostcount;
222    unsigned int i;    uint32_t i;
223    
224    indent();    indent();
225    
226    hostcount = read32u();    hostcount = read32u();
227    
228    print_indentation();    print_indentation();
229    printf("Host count %u\n", hostcount);    printf("Host count %u\n", hostcount);
230    
231    for (i = 0; i < hostcount; i++) {    for (i = 0; i < hostcount; i++) {
232      unsigned int hostheader;      uint32_t hostheader;
233    
234      print_indentation();      print_indentation();
235      printf("Host #%u of %u:\n", i + 1, hostcount);      printf("Host #%u of %u:\n", i + 1, hostcount);
236    
237      indent();      indent();
238    
239  #define HOST_HEADER_V3 0x48535403U  #define HOST_HEADER_V3 0x48535403U
240  #define HOST_HEADER_V2 0x48535402U  #define HOST_HEADER_V2 0x48535402U
241  #define HOST_HEADER_V1 0x48535401U  #define HOST_HEADER_V1 0x48535401U
242    
243      if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3      if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3
244        print_indentation();        print_indentation();
245        printf("Host header v3 0x%x\n", hostheader);        printf("Host header v3 0x%x\n", hostheader);
246        decode_host_header_v3();        decode_host_header_v3();
247      } // if      } // if
# Line 267  void decode_host_header_v1(void) Line 260  void decode_host_header_v1(void)
260        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
261      } // else      } // else
262    
263      exdent();      exdent();
264    } // for    } // for
265    
266    exdent();    exdent();
267  } // decode_host_db_v1()  } // decode_host_db_v1()
268    
269  void decode_protos_data(void);  void decode_protos_data(void);
270  void decode_tcp_data(void);  void decode_tcp_data(void);
271  void decode_udp_data(void);  void decode_udp_data(void);
272    
273  void decode_host_header_v1(void)  void decode_host_header_v1(void)
274  {  {
275    unsigned char ipv4address[4];    uint8_t  ipv4address[4];
276    unsigned char macaddress[6];    uint8_t  macaddress[6];
277    unsigned char hostnamelen;    uint8_t  hostnamelen;
278    unsigned char hostname[256];    uint8_t  hostname[256];
279    unsigned long bytesin;    uint64_t bytesin;
280    unsigned long bytesout;    uint64_t bytesout;
281    unsigned char protosdata;    uint8_t  protosdata;
282    unsigned char tcpdata;    uint8_t  tcpdata;
283    unsigned char udpdata;    uint8_t  udpdata;
284    
285    unsigned char i;    uint8_t  i;
286    
287    indent();    indent();
288    
289    ipv4address[0] = read8u();    ipv4address[0] = read8u();
290    ipv4address[1] = read8u();    ipv4address[1] = read8u();
291    ipv4address[2] = read8u();    ipv4address[2] = read8u();
292    ipv4address[3] = read8u();    ipv4address[3] = read8u();
293    
294    print_indentation();    print_indentation();
295    printf("IPv4 address %u.%u.%u.%u\n",    printf("IPv4 address %u.%u.%u.%u\n",
296           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
297    
298    macaddress[0] = read8u();    macaddress[0] = read8u();
299    macaddress[1] = read8u();    macaddress[1] = read8u();
300    macaddress[2] = read8u();    macaddress[2] = read8u();
# Line 349  void decode_host_header_v2(void) Line 342  void decode_host_header_v2(void)
342    
343    decode_tcp_data();    decode_tcp_data();
344    
345    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
346      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);
347      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
348    } // if    } // if
349    
350    decode_udp_data();    decode_udp_data();
351    
352    exdent();    exdent();
353  } // decode_host_header_v1  } // decode_host_header_v1
354    
355  void decode_host_header_v2(void)  void decode_host_header_v2(void)
356  {  {
357    unsigned char ipv4address[4];    uint8_t  ipv4address[4];
358    unsigned char macaddress[6];    uint8_t  macaddress[6];
359      signed long lastseen;     int64_t lastseen;
360    unsigned char hostnamelen;    uint8_t  hostnamelen;
361    unsigned char hostname[256];    uint8_t  hostname[256];
362    unsigned long bytesin;    uint64_t bytesin;
363    unsigned long bytesout;    uint64_t bytesout;
364    unsigned char protosdata;    uint8_t  protosdata;
365    unsigned char tcpdata;    uint8_t  tcpdata;
366    unsigned char udpdata;    uint8_t  udpdata;
367    
368    unsigned char i;    uint8_t  i;
369    
370    indent();    indent();
371    
372    ipv4address[0] = read8u();    ipv4address[0] = read8u();
373    ipv4address[1] = read8u();    ipv4address[1] = read8u();
374    ipv4address[2] = read8u();    ipv4address[2] = read8u();
375    ipv4address[3] = read8u();    ipv4address[3] = read8u();
376    
377    print_indentation();    print_indentation();
378    printf("IPv4 address %u.%u.%u.%u\n",    printf("IPv4 address %u.%u.%u.%u\n",
379           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
380    
381    if (follow_specification == true) {    if (follow_specification == true) {
382      macaddress[0] = read8u();      macaddress[0] = read8u();
383      macaddress[1] = read8u();      macaddress[1] = read8u();
# Line 459  void decode_host_header_v3(void) Line 452  void decode_host_header_v3(void)
452    
453    decode_tcp_data();    decode_tcp_data();
454    
455    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
456      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);
457      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
458    } // if    } // if
459    
460    decode_udp_data();    decode_udp_data();
461    
462    exdent();    exdent();
463  } // decode_host_header_v2  } // decode_host_header_v2
464    
465  void decode_host_header_v3(void)  void decode_host_header_v3(void)
466  {  {
467    unsigned char  addressfamily;    uint8_t  addressfamily;
468    unsigned char  ipv4address[4];    uint8_t  ipv4address[4];
469    unsigned short ipv6address[8];    uint16_t ipv6address[8];
470    unsigned char  macaddress[6];    uint8_t  macaddress[6];
471      signed long  lastseen;     int64_t lastseen;
472    unsigned char  hostnamelen;    uint8_t  hostnamelen;
473    unsigned char  hostname[256];    uint8_t  hostname[256];
474    unsigned long  bytesin;    uint64_t bytesin;
475    unsigned long  bytesout;    uint64_t bytesout;
476    unsigned char  protosdata;    uint8_t  protosdata;
477    unsigned char  tcpdata;    uint8_t  tcpdata;
478    unsigned char  udpdata;    uint8_t  udpdata;
479    
480    unsigned char  i;    uint8_t  i;
481    
482    indent();    indent();
483    
484    if ( (addressfamily = read8u()) == 0x04) { // IPv4 address    if ( (addressfamily = read8u()) == 0x04) { // IPv4 address
485      print_indentation();      print_indentation();
486      printf("IPv4 address family (0x%02x)\n", addressfamily);      printf("IPv4 address family (0x%02x)\n", addressfamily);
487    
488      ipv4address[0] = read8u();      ipv4address[0] = read8u();
489      ipv4address[1] = read8u();      ipv4address[1] = read8u();
490      ipv4address[2] = read8u();      ipv4address[2] = read8u();
491      ipv4address[3] = read8u();      ipv4address[3] = read8u();
492    
493      print_indentation();      print_indentation();
494      printf("IPv4 address %u.%u.%u.%u\n",      printf("IPv4 address %u.%u.%u.%u\n",
495             ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);             ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
# Line 604  void decode_protos_data(void) Line 597  void decode_protos_data(void)
597    
598    decode_tcp_data();    decode_tcp_data();
599    
600    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
601      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);
602      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
603    } // if    } // if
604    
605    decode_udp_data();    decode_udp_data();
606    
607    exdent();    exdent();
608  } // decode_host_header_v3  } // decode_host_header_v3
609    
610  void decode_protos_data(void)  void decode_protos_data(void)
611  {  {
612    unsigned char ipprotocount;    uint8_t ipprotocount;
613    
614    unsigned char i;    uint8_t i;
615    
616    ipprotocount = read8u();    ipprotocount = read8u();
617    
618    print_indentation();    print_indentation();
619    printf("IP Proto count %u\n", ipprotocount);    printf("IP Proto count %u\n", ipprotocount);
620    
621    indent();    indent();
622    
623    for (i = 0; i < ipprotocount; i++) {    for (i = 0; i < ipprotocount; i++) {
624      unsigned char proto;      uint8_t  proto;
625      unsigned long in;      uint64_t in;
626      unsigned long out;      uint64_t out;
627    
628      print_indentation();      print_indentation();
629      printf("Protocol #%u of %u:\n", i + 1, ipprotocount);      printf("Protocol #%u of %u:\n", i + 1, ipprotocount);
630    
631      proto = read8u();      proto = read8u();
632    
633      indent();      indent();
634      print_indentation();      print_indentation();
635      printf("Protocol 0x%02x\n", proto);      printf("Protocol 0x%02x\n", proto);
636    
637      in = read64u();      in = read64u();
638    
639      print_indentation();      print_indentation();
640      printf("In %lu\n", in);      printf("In %lu\n", in);
641    
642      out = read64u();      out = read64u();
643    
644      print_indentation();      print_indentation();
645      printf("Out %lu\n", out);      printf("Out %lu\n", out);
646    
647      exdent();      exdent();
648    } // for    } // for
649    
650    exdent();    exdent();
651  } // decode_protos_data();  } // decode_protos_data();
652    
653  void decode_tcp_data(void)  void decode_tcp_data(void)
654  {  {
655    unsigned char tcpprotocount;    uint8_t  tcpprotocount;
656    
657    unsigned short i;    uint16_t i;
658    
659    tcpprotocount = read16u();    tcpprotocount = read16u();
660    
661    print_indentation();    print_indentation();
662    printf("TCP proto count %u\n", tcpprotocount);    printf("TCP proto count %u\n", tcpprotocount);
663    
664    indent();    indent();
665    
666    for (i = 0; i < tcpprotocount; i++) {    for (i = 0; i < tcpprotocount; i++) {
667      unsigned short port;      uint16_t port;
668      unsigned long syn;      uint64_t syn;
669      unsigned long in;      uint64_t in;
670      unsigned long out;      uint64_t out;
671    
672      port = read16u();      port = read16u();
673    
674      print_indentation();      print_indentation();
675      printf("Port %u:\n", port);      printf("Port %u:\n", port);
676    
677      syn = read64u();      syn = read64u();
678    
679      indent();      indent();
680      print_indentation();      print_indentation();
681      printf("SYN %lu\n", syn);      printf("SYN %lu\n", syn);
682    
683      in = read64u();      in = read64u();
684    
685      print_indentation();      print_indentation();
686      printf("In %lu\n", in);      printf("In %lu\n", in);
687    
688      out = read64u();      out = read64u();
689    
690      print_indentation();      print_indentation();
691      printf("Out %lu\n", out);      printf("Out %lu\n", out);
692    
693      exdent();      exdent();
694    } // for    } // for
695    
696    exdent();    exdent();
697  } // decode_tcp_data()  } // decode_tcp_data()
698    
699  void decode_udp_data(void)  void decode_udp_data(void)
700  {  {
701    unsigned char udpprotocount;    uint8_t  udpprotocount;
702    
703    unsigned short i;    uint16_t i;
704    
705    udpprotocount = read16u();    udpprotocount = read16u();
706    
707    print_indentation();    print_indentation();
708    printf("UDP proto count %u\n", udpprotocount);    printf("UDP proto count %u\n", udpprotocount);
709    
710    indent();    indent();
711    
712    for (i = 0; i < udpprotocount; i++) {    for (i = 0; i < udpprotocount; i++) {
713      unsigned short port;      uint16_t port;
714      unsigned long in;      uint64_t in;
715      unsigned long out;      uint64_t out;
716    
717      port = read16u();      port = read16u();
718    
719      print_indentation();      print_indentation();
720      printf("Port %u:\n", port);      printf("Port %u:\n", port);
721    
722      in = read64u();      in = read64u();
723    
724      indent();      indent();
725      print_indentation();      print_indentation();
726      printf("In %lu\n", in);      printf("In %lu\n", in);
727    
728      out = read64u();      out = read64u();
729    
730      print_indentation();      print_indentation();
731      printf("Out %lu\n", out);      printf("Out %lu\n", out);
732    
733      exdent();      exdent();
734    } // for    } // for
735    
736    exdent();    exdent();
737  } // decode_udp_data()  } // decode_udp_data()
738    
739  void decode_graph_db_v1(void)  void decode_graph_db_v1(void)
740  {  {
741    signed long lasttime;    int64_t  lasttime;
742    
743    unsigned int i;    uint32_t i;
744    
745    lasttime = read64s();    lasttime = read64s();
746    
747    indent();    indent();
748    print_indentation();    print_indentation();
749    printf("Last time 0x%lx = %ld = ", lasttime, lasttime);    printf("Last time 0x%lx = %ld = ", lasttime, lasttime);
750    print_time_t(lasttime);    print_time_t(lasttime);
751    puts("");    puts("");
752    
753    for (i = 0; i < 4; i++) {    for (i = 0; i < 4; i++) {
754      unsigned char nbars;      uint8_t  nbars;
755      unsigned char idxlastbar;      uint8_t  idxlastbar;
756    
757      unsigned int j;      uint32_t j;
758    
759      print_indentation();      print_indentation();
760      printf("Graph #%u of 4:\n", i + 1);      printf("Graph #%u of 4:\n", i + 1);
761    
762      nbars = read8u();      nbars = read8u();
763    
764      indent();      indent();
765      print_indentation();      print_indentation();
766      printf("Number of bars %u\n", nbars);      printf("Number of bars %u\n", nbars);
767    
768      idxlastbar = read8u();      idxlastbar = read8u();
769    
770      print_indentation();      print_indentation();
771      printf("Index of last bar %u\n", idxlastbar);      printf("Index of last bar %u\n", idxlastbar);
772    
773      indent();      indent();
774      for (j = 0; j < idxlastbar; j++) {      for (j = 0; j < idxlastbar; j++) {
775        unsigned long in;        uint64_t in;
776        unsigned long out;        uint64_t out;
777    
778        print_indentation();        print_indentation();
779        printf("Bar #%u of %u:\n", j + 1, idxlastbar);        printf("Bar #%u of %u:\n", j + 1, idxlastbar);
780    
781        in = read64u();        in = read64u();
782    
783        indent();        indent();
784        print_indentation();        print_indentation();
785        printf("In %lu\n", in);        printf("In %lu\n", in);
786    
787        out = read64u();        out = read64u();
788    
789        print_indentation();        print_indentation();
790        printf("Out %lu\n", out);        printf("Out %lu\n", out);
791    
792        exdent();        exdent();
793      } // for      } // for
794      exdent();      exdent();
795    
796      exdent();      exdent();
797    } // for    } // for
798    
799    exdent();    exdent();
800  } // decode_graph_db_v1()  } // decode_graph_db_v1()
801    
802  void handle_file_error(void);  void handle_file_error(void);
803    
804  unsigned char read8u(void)  uint8_t read8u(void)
805  {  {
806    size_t r;    size_t r;
807    unsigned char v;    uint8_t v;
808    
   //fprintf(stderr, "%s: sizeof(unsigned char) = %ld\n", progname, sizeof(v));  
   
809    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
810      handle_file_error();      handle_file_error();
811    } // if    } // if
812    
813    return v;    return v;
814  } // read8u()  } // read8u()
815    
816  signed char read8s(void)  int8_t read8s(void)
817  {  {
818    size_t r;    size_t r;
819    signed char v;    int8_t v;
820    
   //fprintf(stderr, "%s: sizeof(signed char) = %ld\n", progname, sizeof(v));  
   
821    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
822      handle_file_error();      handle_file_error();
823    } // if    } // if
824    
825    return v;    return v;
826  } // read8s()  } // read8s()
827    
828  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
829  #include <netinet/in.h>  #include <netinet/in.h>
830  #endif  #endif
831    
832  unsigned short read16u(void)  uint16_t read16u(void)
833  {  {
834    size_t r;    size_t r;
835    unsigned short v;    uint16_t v;
836    
   //fprintf(stderr, "%s: sizeof(unsigned short) = %ld\n", progname, sizeof(v));  
   
837    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
838      handle_file_error();      handle_file_error();
839    } // if    } // if
840    
841  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
842    v = ntohs(v);    v = ntohs(v);
843  #endif  #endif
844    
845    return v;    return v;
846  } // read16u()  } // read16u()
847    
848  signed short read16s(void)  int16_t read16s(void)
849  {  {
850    size_t r;    size_t r;
851    signed short v;    int16_t v;
852    
   //fprintf(stderr, "%s: sizeof(signed short) = %ld\n", progname, sizeof(v));  
   
853    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
854      handle_file_error();      handle_file_error();
855    } // if    } // if
856    
857  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
858    v = ntohs(v);    v = ntohs(v);
859  #endif  #endif
860    
861    return v;    return v;
862  } // read16s()  } // read16s()
863    
864  unsigned int read32u(void)  uint32_t read32u(void)
865  {  {
866    size_t r;    size_t r;
867    unsigned int v;    uint32_t v;
868    
   //fprintf(stderr, "%s: sizeof(unsigned int) = %ld\n", progname, sizeof(v));  
   
869    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
870      handle_file_error();      handle_file_error();
871    } // if    } // if
872    
873  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
874    v = ntohl(v);    v = ntohl(v);
875  #endif  #endif
876    
877    return v;    return v;
878  } // read32u()  } // read32u()
879    
880  signed int read32s(void)  int32_t read32s(void)
881  {  {
882    size_t r;    size_t r;
883    signed int v;    int32_t v;
884    
   //fprintf(stderr, "%s: sizeof(signed int) = %ld\n", progname, sizeof(v));  
   
885    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
886      handle_file_error();      handle_file_error();
887    } // if    } // if
888    
889  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
890    v = ntohl(v);    v = ntohl(v);
891  #endif  #endif
892    
893    return v;    return v;
894  } // read32s()  } // read32s()
895    
896  unsigned long read64u(void)  uint64_t read64u(void)
897  {  {
898    size_t r;    size_t r;
899    unsigned long v;    uint64_t v;
900    
901  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
902    unsigned long tmp;    uint64_t tmp;
903    unsigned int *p1 = (unsigned int *)&v;    uint32_t *p1 = (uint32_t *)&v;
904    unsigned int *p2 = (unsigned int *)&tmp;    uint32_t *p2 = (uint32_t *)&tmp;
905  #endif  #endif
906    
   //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));  
   
907    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
908      handle_file_error();      handle_file_error();
909    } // if    } // if
910    
911  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
912    p2[1] = ntohl(p1[0]);    p2[1] = ntohl(p1[0]);
913    p2[0] = ntohl(p1[1]);    p2[0] = ntohl(p1[1]);
914    v = tmp;    v = tmp;
915  #endif  #endif
916    
917    return v;    return v;
918  } // read64u()  } // read64u()
919    
920  signed long read64s(void)  int64_t read64s(void)
921  {  {
922    size_t r;    size_t r;
923    signed long v;    int64_t v;
924    
925  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
926    signed long tmp;    int64_t tmp;
927    signed int *p1 = (signed int *)&v;    int32_t *p1 = (int32_t *)&v;
928    signed int *p2 = (signed int *)&tmp;    int32_t *p2 = (int32_t *)&tmp;
929  #endif  #endif
930    
   //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));  
   
931    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
932      handle_file_error();      handle_file_error();
933    } // if    } // if
934    
935  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
936    p2[1] = ntohl(p1[0]);    p2[1] = ntohl(p1[0]);
937    p2[0] = ntohl(p1[1]);    p2[0] = ntohl(p1[1]);
938    v = tmp;    v = tmp;
939  #endif  #endif
940    
941    return v;    return v;
942  } // read64s()  } // read64s()
943    
944  void handle_file_error(void)  void handle_file_error(void)
945  {  {
946    int saved_errno = errno;    int saved_errno = errno;
947    
948    if (feof(file) != 0) {    if (feof(file) != 0) {
949      fprintf(stderr, "%s:%s:%ld: premature end-of-file\n", progname, filename, ftell(file));      fprintf(stderr, "%s:%s:%ld: premature end-of-file\n", progname, filename, ftell(file));
950      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
951    } // if    } // if
952    
953    if (ferror(file) != 0) {    if (ferror(file) != 0) {
954      fprintf(stderr, "%s:%s:%ld: file error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno);      fprintf(stderr, "%s:%s:%ld: file error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno);
955      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
956    } // if    } // if
957  } // handle_file_error()  } // handle_file_error()
958    
959  signed long long indentation = 0LL;  int64_t indentation = 0LL;
960    
961  void indent(void)  void indent(void)
962  {  {
963    indentation += 2LL;    if (indentation < (INT64_MAX - 2LL)) {
964        indentation += 2LL;
965      } // if
966  } // indent()  } // indent()
967    
968  void exdent(void)  void exdent(void)
969  {  {
970    indentation -= 2LL;    indentation -= 2LL;
971    
972    if (indentation < 0LL) {    if (indentation < 0LL) {
973      indentation = 0LL;      indentation = 0LL;
974    }// if    }// if
975  } // exdent()  } // exdent()
976    
977  void print_indentation(void)  void print_indentation(void)
978  {  {
979    signed long long i;    int64_t i;
980    
981    for (i = 0; i < indentation; i++) {    for (i = 0; i < indentation; i++) {
982      putchar(' ');      putchar(' ');
983    } // for    } // for
984  } // print_indentation()  } // print_indentation()
985    
986  void print_time_t(time_t t)  void print_time_t(time_t t)
987  {  {
988    struct tm *stm;    struct tm *stm;
989    char buffer[1024];    char buffer[1024];
990    
991    stm = gmtime(&t);    stm = gmtime(&t);
992    
993    // ISO 8601 format    // ISO 8601 format
994    if (strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S%z", stm) == 0) {    if (strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S%z", stm) == 0) {


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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