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

Diff of /trunk/darkstattype.c

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

Revision 15 by trond, 2014-03-21T18:57:27Z Revision 19 by trond, 2014-04-09T09:20:17Z
# 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
# Line 59  int main(int argc, char **argv) Line 59  int main(int argc, char **argv)
59        case 'f':        case 'f':
60          follow_specification = !follow_specification;          follow_specification = !follow_specification;
61          break;          break;
62    
63        case 'h':        case 'h':
64          show_usage(EXIT_SUCCESS);          show_usage(EXIT_SUCCESS);
65          break;          break;
66    
67        case 'v':        case 'v':
68          show_version();          show_version();
69          break;          break;
70    
71        case '?':        case '?':
72          /*FALLTHROUGH*/          /*FALLTHROUGH*/
73        default:        default:
74          fprintf(stderr, "%s: illegal option -%c\n\n", progname, optopt);          fprintf(stderr, "%s: illegal option -%c\n\n",
75                    progname, optopt);
76          show_usage(EXIT_FAILURE);          show_usage(EXIT_FAILURE);
77          break;          break;
78      } // switch      } // switch
79    }  // while    }  // while
80    argc -= optind;    argc -= optind;
81    argv += optind;    argv += optind;
82    
83    if (*argv == NULL) {    if (*argv == NULL) {
84      fprintf(stderr, "%s: missing filename\n\n", progname);      fprintf(stderr, "%s: missing filename\n\n",
85                progname);
86      show_usage(EXIT_FAILURE);      show_usage(EXIT_FAILURE);
87    } // if    } // if
88    
89    while (*argv != NULL) {    while (*argv != NULL) {
90      filename = *argv;      filename = *argv;
91    
92      decode_file();      decode_file();
93    
94      argv++;      argv++;
95    } // while    } // while
96    
97    return EXIT_SUCCESS;    return EXIT_SUCCESS;
98  } // main()  } // main()
99    
100  void show_usage(int exitcode)  void show_usage(int exitcode)
101  {  {
102    fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr,    fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr,
103      "Usage: %s [-f] [-h] [-v] filename ...\n"      "Usage: %s [-f | -h | -v] filename [...]\n"
104      " E.g.: %s darkstat.db\n\n"      " E.g.: %s darkstat.db\n\n"
105    
106      "Options:\n"      "Options:\n"
107      "-f\tToggle follow strict specification, default is false.\n"      "-f\tToggle follow strict specification, default is false.\n"
108      "-h\tShow this help message and exit.\n"      "-h\tShow this help message and exit.\n"
109      "-v\tShow version and copyright and exit.\n",      "-v\tShow version and copyright and exit.\n",
110      progname, progname);      progname, progname);
111    
112    exit(exitcode);    exit(exitcode);
113  } // show_usage()  } // show_usage()
114    
115  void show_version(void)  void show_version(void)
116  {  {
117    puts("darkstattype 1.0");    puts("darkstattype 1.0");
118    puts("$Ximalas$");    puts("$Ximalas$");
119    puts("");    puts("");
120    
121    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");
122    puts("All rights reserved.");    puts("All rights reserved.");
123    puts("");    puts("");
124    puts("Redistribution and use in source and binary forms, with or without");    puts("Redistribution and use in source and binary forms, with or without");
125    puts("modification, are permitted provided that the following conditions are met:");    puts("modification, are permitted provided that the following conditions are met:");
126    puts("");    puts("");
127    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");
128    puts("   list of conditions and the following disclaimer.");    puts("   list of conditions and the following disclaimer.");
129    puts("2. Redistributions in binary form must reproduce the above copyright notice,");    puts("2. Redistributions in binary form must reproduce the above copyright notice,");
130    puts("   this list of conditions and the following disclaimer in the documentation");    puts("   this list of conditions and the following disclaimer in the documentation");
131    puts("   and/or other materials provided with the distribution.");    puts("   and/or other materials provided with the distribution.");
132    puts("");    puts("");
133    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");
134    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");
135    puts("WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE");    puts("WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE");
136    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");
# Line 156  void decode_file(void) Line 158  void decode_file(void)
158  void print_indentation(void);  void print_indentation(void);
159    
160  void print_time_t(time_t t);  void print_time_t(time_t t);
161    
162  void decode_file(void)  void decode_file(void)
163  {  {
164    void decode_host_db_v1(void);    void decode_host_db_v1(void);
165    void decode_graph_db_v1(void);    void decode_graph_db_v1(void);
166    
167    uint32_t fileheader;    uint32_t fileheader;
168    uint32_t sectionheader;    uint32_t sectionheader;
169    
170    uint32_t i;    uint32_t i;
171    
172    if ( (file = fopen(filename, "rb")) == NULL) {    if ( (file = fopen(filename, "rb")) == NULL) {
173      fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s: fopen(\"%s\") = %s (%d)\n",
174                progname, filename, strerror(errno), errno);
175      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
176    } // if    } // if
177    
178  #define FILE_HEADER_V1 0xDA314159U  #define FILE_HEADER_V1 0xDA314159U
179    
180    if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format    if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format
181      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",
182                progname, filename, ftell(file), fileheader, FILE_HEADER_V1);
183      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
184    } // if    } // if
185    
186    printf("File header 0x%x\n", fileheader);    printf("File header 0x%x\n", fileheader);
187    
188    // Possible section header for host_db v1 and later graph_db v1.    // Possible section header for host_db v1 and later graph_db v1.
189    indent();    indent();
190    
191  #define HOST_DB_V1 0xDA485301U  #define HOST_DB_V1 0xDA485301U
192  #define GRAPH_DB_V1 0xDA475201U  #define GRAPH_DB_V1 0xDA475201U
193    
194    for (i = 0; i < 2; i++) {    for (i = 0; i < 2; i++) {
195      if ( (sectionheader = read32u()) == HOST_DB_V1) {      if ( (sectionheader = read32u()) == HOST_DB_V1) {
196        print_indentation();        print_indentation();
197        printf("Section header host_db v1 0x%x\n", sectionheader);        printf("Section header host_db v1 0x%x\n", sectionheader);
198        decode_host_db_v1();        decode_host_db_v1();
199      } // if      } // if
200      else if (sectionheader == GRAPH_DB_V1) {      else if (sectionheader == GRAPH_DB_V1) {
201        print_indentation();        print_indentation();
202        printf("Section header graph_db v1 0x%x\n", sectionheader);        printf("Section header graph_db v1 0x%x\n", sectionheader);
203        decode_graph_db_v1();        decode_graph_db_v1();
204      } // else if      } // else if
205      else {      else {
206        fprintf(stderr, "%s:%s:%ld: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, ftell(file), sectionheader, HOST_DB_V1, GRAPH_DB_V1);        fprintf(stderr,
207                  "%s:%s:%ld: unknown section header = 0x%x, neither 0x%x nor 0x%x\n",
208                  progname, filename, ftell(file), sectionheader, HOST_DB_V1,
209                  GRAPH_DB_V1);
210        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
211      } // else      } // else
212    } // for    } // for
213    
214    exdent();    exdent();
215    
216    if (fclose(file) != 0) {    if (fclose(file) != 0) {
217      fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n",
218                progname, filename, strerror(errno), errno);
219      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
220    } // if    } // if
221  } // decode_file()  } // decode_file()
222    
223  void decode_host_db_v1(void)  void decode_host_db_v1(void)
224  {  {
225    void decode_host_header_v1(void);    void decode_host_header_v1(void);
226    void decode_host_header_v2(void);    void decode_host_header_v2(void);
227    void decode_host_header_v3(void);    void decode_host_header_v3(void);
228    
229    uint32_t hostcount;    uint32_t hostcount;
230    uint32_t i;    uint32_t i;
231    
232    indent();    indent();
233    
234    hostcount = read32u();    hostcount = read32u();
235    
236    print_indentation();    print_indentation();
237    printf("Host count %u\n", hostcount);    printf("Host count %u\n",
238             hostcount);
239    
240    for (i = 0; i < hostcount; i++) {    for (i = 0; i < hostcount; i++) {
241      uint32_t hostheader;      uint32_t hostheader;
242    
243      print_indentation();      print_indentation();
244      printf("Host #%u of %u:\n", i + 1, hostcount);      printf("Host #%u of %u:\n",
245               i + 1, hostcount);
246    
247      indent();      indent();
248    
249  #define HOST_HEADER_V3 0x48535403U  #define HOST_HEADER_V3 0x48535403U
250  #define HOST_HEADER_V2 0x48535402U  #define HOST_HEADER_V2 0x48535402U
251  #define HOST_HEADER_V1 0x48535401U  #define HOST_HEADER_V1 0x48535401U
252    
253      if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3      if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3
254        print_indentation();        print_indentation();
255        printf("Host header v3 0x%x\n", hostheader);        printf("Host header v3 0x%x\n",
256                 hostheader);
257        decode_host_header_v3();        decode_host_header_v3();
258      } // if      } // if
259      else if (hostheader == HOST_HEADER_V2) { // host header v2      else if (hostheader == HOST_HEADER_V2) { // host header v2
260        print_indentation();        print_indentation();
261        printf("Host header v2 0x%x\n", hostheader);        printf("Host header v2 0x%x\n",
262                 hostheader);
263        decode_host_header_v2();        decode_host_header_v2();
264      } // else if      } // else if
265      else if (hostheader == HOST_HEADER_V1) { // host header v1      else if (hostheader == HOST_HEADER_V1) { // host header v1
266        print_indentation();        print_indentation();
267        printf("Host header v1 0x%x\n", hostheader);        printf("Host header v1 0x%x\n",
268                 hostheader);
269        decode_host_header_v1();        decode_host_header_v1();
270      } // else if      } // else if
271      else { // unknown host header version      else { // unknown host header version
272        fprintf(stderr, "%s:%s:%ld: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n", progname, filename, ftell(file), hostheader, HOST_HEADER_V3, HOST_HEADER_V2, HOST_HEADER_V1);        fprintf(stderr,
273                  "%s:%s:%ld: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n",
274                  progname, filename, ftell(file), hostheader, HOST_HEADER_V3,
275                  HOST_HEADER_V2, HOST_HEADER_V1);
276        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
277      } // else      } // else
278    
279      exdent();      exdent();
280    } // for    } // for
281    
282    exdent();    exdent();
283  } // decode_host_db_v1()  } // decode_host_db_v1()
284    
285  void decode_protos_data(void);  void decode_protos_data(void);
286  void decode_tcp_data(void);  void decode_tcp_data(void);
287  void decode_udp_data(void);  void decode_udp_data(void);
288    
289  void decode_host_header_v1(void)  void decode_host_header_v1(void)
290  {  {
# Line 280  void decode_host_header_v1(void) Line 296  void decode_host_header_v1(void)
296    uint64_t bytesout;    uint64_t bytesout;
297    uint8_t  protosdata;    uint8_t  protosdata;
298    uint8_t  tcpdata;    uint8_t  tcpdata;
299    uint8_t  udpdata;    uint8_t  udpdata;
300    
301    uint8_t  i;    uint8_t  i;
302    
303    indent();    indent();
304    
305    ipv4address[0] = read8u();    ipv4address[0] = read8u();
306    ipv4address[1] = read8u();    ipv4address[1] = read8u();
307    ipv4address[2] = read8u();    ipv4address[2] = read8u();
308    ipv4address[3] = read8u();    ipv4address[3] = read8u();
309    
310    print_indentation();    print_indentation();
311    printf("IPv4 address %u.%u.%u.%u\n",    printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n",
312           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
313    
314    macaddress[0] = read8u();    macaddress[0] = read8u();
315    macaddress[1] = read8u();    macaddress[1] = read8u();
316    macaddress[2] = read8u();    macaddress[2] = read8u();
317    macaddress[3] = read8u();    macaddress[3] = read8u();
318    macaddress[4] = read8u();    macaddress[4] = read8u();
319    macaddress[5] = read8u();    macaddress[5] = read8u();
320    
321    print_indentation();    print_indentation();
322    printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);    printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
323             macaddress[0], macaddress[1], macaddress[2],
324             macaddress[3], macaddress[4], macaddress[5]);
325    
326    hostnamelen = read8u();    hostnamelen = read8u();
327    
328    print_indentation();    print_indentation();
329    printf("Hostname length %u\n", hostnamelen);    printf("Hostname length %u\n",
330             hostnamelen);
331    
332    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
333      hostname[i] = read8u();      hostname[i] = read8u();
334    } // for    } // for
335    hostname[i] = '\0';    hostname[i] = '\0';
336    
337    print_indentation();    print_indentation();
338    printf("Hostname %s\n", hostname);    printf("Hostname %s\n",
339             hostname);
340    
341    bytesin = read64u();    bytesin = read64u();
342    
343    print_indentation();    print_indentation();
344    printf("Bytes in %lu\n", bytesin);    printf("Bytes in %lu\n",
345             bytesin);
346    
347    bytesout = read64u();    bytesout = read64u();
348    
349    print_indentation();    print_indentation();
350    printf("Bytes out %lu\n", bytesout);    printf("Bytes out %lu\n",
351             bytesout);
352    
353    if ( (protosdata = read8u()) != 'P') { // missing protos data    if ( (protosdata = read8u()) != 'P') { // missing protos data
354      fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), protosdata);      fprintf(stderr,
355                "%s:%s:%ld: expecting character P, not %c\n",
356                progname, filename, ftell(file), protosdata);
357      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
358    } // if    } // if
359    
360    decode_protos_data();    decode_protos_data();
361    
362    if ( (tcpdata = read8u()) != 'T') { // missing tcp data    if ( (tcpdata = read8u()) != 'T') { // missing tcp data
363      fprintf(stderr, "%s:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), tcpdata);      fprintf(stderr,
364                "%s:%s:%ld: expecting character T, not %c\n",
365                progname, filename, ftell(file), tcpdata);
366      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
367    } // if    } // if
368    
369    decode_tcp_data();    decode_tcp_data();
370    
371    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
372      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);      fprintf(stderr,
373                "%s:%s:%ld: expecting character U, not %c\n",
374                progname, filename, ftell(file), udpdata);
375      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
376    } // if    } // if
377    
378    decode_udp_data();    decode_udp_data();
379    
380    exdent();    exdent();
381  } // decode_host_header_v1  } // decode_host_header_v1
382    
383  void decode_host_header_v2(void)  void decode_host_header_v2(void)
384  {  {
385    uint8_t  ipv4address[4];    uint8_t  ipv4address[4];
386    uint8_t  macaddress[6];    uint8_t  macaddress[6];
387     int64_t lastseen;     int64_t lastseen;
388    uint8_t  hostnamelen;    uint8_t  hostnamelen;
389    uint8_t  hostname[256];    uint8_t  hostname[256];
# Line 363  void decode_host_header_v2(void) Line 391  void decode_host_header_v2(void)
391    uint64_t bytesout;    uint64_t bytesout;
392    uint8_t  protosdata;    uint8_t  protosdata;
393    uint8_t  tcpdata;    uint8_t  tcpdata;
394    uint8_t  udpdata;    uint8_t  udpdata;
395    
396    uint8_t  i;    uint8_t  i;
397    
398    indent();    indent();
399    
400    ipv4address[0] = read8u();    ipv4address[0] = read8u();
401    ipv4address[1] = read8u();    ipv4address[1] = read8u();
402    ipv4address[2] = read8u();    ipv4address[2] = read8u();
403    ipv4address[3] = read8u();    ipv4address[3] = read8u();
404    
405    print_indentation();    print_indentation();
406    printf("IPv4 address %u.%u.%u.%u\n",    printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n",
407           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
408    
409    if (follow_specification == true) {    if (follow_specification == true) {
410      macaddress[0] = read8u();      macaddress[0] = read8u();
411      macaddress[1] = read8u();      macaddress[1] = read8u();
412      macaddress[2] = read8u();      macaddress[2] = read8u();
413      macaddress[3] = read8u();      macaddress[3] = read8u();
414      macaddress[4] = read8u();      macaddress[4] = read8u();
415      macaddress[5] = read8u();      macaddress[5] = read8u();
416    
417      print_indentation();      print_indentation();
418      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);      printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
419               macaddress[0], macaddress[1], macaddress[2],
420               macaddress[3], macaddress[4], macaddress[5]);
421    
422      lastseen = read64s();      lastseen = read64s();
423    
424      print_indentation();      print_indentation();
425      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);      printf("Last seen 0x%lx = %ld = ",
426               lastseen, lastseen);
427      print_time_t(lastseen);      print_time_t(lastseen);
428      puts("");      puts("");
429    } // if    } // if
430    else {    else {
431      lastseen = read64s();      lastseen = read64s();
432    
433      print_indentation();      print_indentation();
434      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);      printf("Last seen 0x%lx = %ld = ",
435               lastseen, lastseen);
436      print_time_t(lastseen);      print_time_t(lastseen);
437      puts("");      puts("");
438    
439      macaddress[0] = read8u();      macaddress[0] = read8u();
440      macaddress[1] = read8u();      macaddress[1] = read8u();
441      macaddress[2] = read8u();      macaddress[2] = read8u();
442      macaddress[3] = read8u();      macaddress[3] = read8u();
443      macaddress[4] = read8u();      macaddress[4] = read8u();
444      macaddress[5] = read8u();      macaddress[5] = read8u();
445    
446      print_indentation();      print_indentation();
447      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);      printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
448               macaddress[0], macaddress[1], macaddress[2],
449               macaddress[3], macaddress[4], macaddress[5]);
450    } // else    } // else
451    
452    hostnamelen = read8u();    hostnamelen = read8u();
453    
454    print_indentation();    print_indentation();
455    printf("Hostname length %u\n", hostnamelen);    printf("Hostname length %hhu\n",
456             hostnamelen);
457    
458    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
459      hostname[i] = read8u();      hostname[i] = read8u();
460    } // for    } // for
461    hostname[i] = '\0';    hostname[i] = '\0';
462    
463    print_indentation();    print_indentation();
464    printf("Hostname %s\n", hostname);    printf("Hostname %s\n",
465             hostname);
466    
467    bytesin = read64u();    bytesin = read64u();
468    
469    print_indentation();    print_indentation();
470    printf("Bytes in %lu\n", bytesin);    printf("Bytes in %lu\n",
471             bytesin);
472    
473    bytesout = read64u();    bytesout = read64u();
474    
475    print_indentation();    print_indentation();
476    printf("Bytes out %lu\n", bytesout);    printf("Bytes out %lu\n",
477             bytesout);
478    
479    if ( (protosdata = read8u()) != 'P') { // missing protos data    if ( (protosdata = read8u()) != 'P') { // missing protos data
480      fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), protosdata);      fprintf(stderr,
481                "%s:%s:%ld: expecting character P, not %c\n",
482                progname, filename, ftell(file), protosdata);
483      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
484    } // if    } // if
485    
486    decode_protos_data();    decode_protos_data();
487    
488    if ( (tcpdata = read8u()) != 'T') { // missing tcp data    if ( (tcpdata = read8u()) != 'T') { // missing tcp data
489      fprintf(stderr, "%s:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), tcpdata);      fprintf(stderr,
490                "%s:%s:%ld: expecting character T, not %c\n",
491                progname, filename, ftell(file), tcpdata);
492      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
493    } // if    } // if
494    
495    decode_tcp_data();    decode_tcp_data();
496    
497    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
498      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);      fprintf(stderr,
499                "%s:%s:%ld: expecting character U, not %c\n",
500                progname, filename, ftell(file), udpdata);
501      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
502    } // if    } // if
503    
504    decode_udp_data();    decode_udp_data();
505    
506    exdent();    exdent();
507  } // decode_host_header_v2  } // decode_host_header_v2
508    
509  void decode_host_header_v3(void)  void decode_host_header_v3(void)
510  {  {
511    uint8_t  addressfamily;    uint8_t  addressfamily;
512    uint8_t  ipv4address[4];    uint8_t  ipv4address[4];
513    uint16_t ipv6address[8];    uint16_t ipv6address[8];
514    uint8_t  macaddress[6];    uint8_t  macaddress[6];
515     int64_t lastseen;     int64_t lastseen;
516    uint8_t  hostnamelen;    uint8_t  hostnamelen;
517    uint8_t  hostname[256];    uint8_t  hostname[256];
518    uint64_t bytesin;    uint64_t bytesin;
519    uint64_t bytesout;    uint64_t bytesout;
520    uint8_t  protosdata;    uint8_t  protosdata;
521    uint8_t  tcpdata;    uint8_t  tcpdata;
522    uint8_t  udpdata;    uint8_t  udpdata;
523    
524    uint8_t  i;    uint8_t  i;
525    
526    indent();    indent();
527    
528    if ( (addressfamily = read8u()) == 0x04) { // IPv4 address  #define A_F_4 0x04
529    #define A_F_6 0x06
530    
531      if ( (addressfamily = read8u()) == A_F_4) { // IPv4 address
532      print_indentation();      print_indentation();
533      printf("IPv4 address family (0x%02x)\n", addressfamily);      printf("IPv4 address family (0x%02hhx)\n",
534               addressfamily);
535    
536      ipv4address[0] = read8u();      ipv4address[0] = read8u();
537      ipv4address[1] = read8u();      ipv4address[1] = read8u();
538      ipv4address[2] = read8u();      ipv4address[2] = read8u();
539      ipv4address[3] = read8u();      ipv4address[3] = read8u();
540    
541      print_indentation();      print_indentation();
542      printf("IPv4 address %u.%u.%u.%u\n",      printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n",
543             ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);             ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
544    } // if    } // if
545    else if (addressfamily == 0x06) { // IPv6 address    else if (addressfamily == A_F_6) { // IPv6 address
546      print_indentation();      print_indentation();
547      printf("IPv6 address family (0x%02x)\n", addressfamily);      printf("IPv6 address family (0x%02hhx)\n",
548               addressfamily);
549    
550      ipv6address[0] = read16u();      ipv6address[0] = read16u();
551      ipv6address[1] = read16u();      ipv6address[1] = read16u();
552      ipv6address[2] = read16u();      ipv6address[2] = read16u();
553      ipv6address[3] = read16u();      ipv6address[3] = read16u();
554      ipv6address[4] = read16u();      ipv6address[4] = read16u();
555      ipv6address[5] = read16u();      ipv6address[5] = read16u();
556      ipv6address[6] = read16u();      ipv6address[6] = read16u();
557      ipv6address[7] = read16u();      ipv6address[7] = read16u();
558    
559      print_indentation();      print_indentation();
560      printf("IPv6 address %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",      printf("IPv6 address %04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx\n",
561             ipv6address[0],             ipv6address[0], ipv6address[1], ipv6address[2], ipv6address[3],
562             ipv6address[1],             ipv6address[4], ipv6address[5], ipv6address[6], ipv6address[7]);
            ipv6address[2],  
            ipv6address[3],  
            ipv6address[4],  
            ipv6address[5],  
            ipv6address[6],  
            ipv6address[7]);  
563    } // else if    } // else if
564    else { // unknown address family    else { // unknown address family
565      fprintf(stderr, "%s:%s:%ld: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, ftell(file), addressfamily, 0x04, 0x06);      fprintf(stderr,
566                "%s:%s:%ld: unknown address family = 0x%hhx, neither 0x%x nor 0x%x\n",
567                progname, filename, ftell(file), addressfamily, A_F_4, A_F_6);
568      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
569    } // else    } // else
570    
571    if (follow_specification == true) {    if (follow_specification == true) {
572      macaddress[0] = read8u();      macaddress[0] = read8u();
573      macaddress[1] = read8u();      macaddress[1] = read8u();
574      macaddress[2] = read8u();      macaddress[2] = read8u();
575      macaddress[3] = read8u();      macaddress[3] = read8u();
576      macaddress[4] = read8u();      macaddress[4] = read8u();
577      macaddress[5] = read8u();      macaddress[5] = read8u();
578    
579      print_indentation();      print_indentation();
580      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);      printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
581               macaddress[0], macaddress[1], macaddress[2],
582               macaddress[3], macaddress[4], macaddress[5]);
583    
584      lastseen = read64s();      lastseen = read64s();
585    
586      print_indentation();      print_indentation();
587      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);      printf("Last seen 0x%lx = %ld = ",
588               lastseen, lastseen);
589      print_time_t(lastseen);      print_time_t(lastseen);
590      puts("");      puts("");
591    } // if    } // if
592    else {    else {
593      lastseen = read64s();      lastseen = read64s();
594    
595      print_indentation();      print_indentation();
596      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);      printf("Last seen 0x%lx = %ld = ",
597               lastseen, lastseen);
598      print_time_t(lastseen);      print_time_t(lastseen);
599      puts("");      puts("");
600    
601      macaddress[0] = read8u();      macaddress[0] = read8u();
602      macaddress[1] = read8u();      macaddress[1] = read8u();
603      macaddress[2] = read8u();      macaddress[2] = read8u();
604      macaddress[3] = read8u();      macaddress[3] = read8u();
605      macaddress[4] = read8u();      macaddress[4] = read8u();
606      macaddress[5] = read8u();      macaddress[5] = read8u();
607    
608      print_indentation();      print_indentation();
609      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);      printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
610               macaddress[0], macaddress[1], macaddress[2],
611               macaddress[3], macaddress[4], macaddress[5]);
612    } // else    } // else
613    
614    hostnamelen = read8u();    hostnamelen = read8u();
615    
616    print_indentation();    print_indentation();
617    printf("Hostname length %u\n", hostnamelen);    printf("Hostname length %hhu\n",
618             hostnamelen);
619    
620    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
621      hostname[i] = read8u();      hostname[i] = read8u();
622    } // for    } // for
623    hostname[i] = '\0';    hostname[i] = '\0';
624    
625    print_indentation();    print_indentation();
626    printf("Hostname %s\n", hostname);    printf("Hostname %s\n",
627             hostname);
628    
629    bytesin = read64u();    bytesin = read64u();
630    
631    print_indentation();    print_indentation();
632    printf("Bytes in %lu\n", bytesin);    printf("Bytes in %lu\n",
633             bytesin);
634    
635    bytesout = read64u();    bytesout = read64u();
636    
637    print_indentation();    print_indentation();
638    printf("Bytes out %lu\n", bytesout);    printf("Bytes out %lu\n",
639             bytesout);
640    
641    if ( (protosdata = read8u()) != 'P') { // missing protos data    if ( (protosdata = read8u()) != 'P') { // missing protos data
642      fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), protosdata);      fprintf(stderr,
643                "%s:%s:%ld: expecting character P, not %c\n",
644                progname, filename, ftell(file), protosdata);
645      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
646    } // if    } // if
647    
648    decode_protos_data();    decode_protos_data();
649    
650    if ( (tcpdata = read8u()) != 'T') { // missing tcp data    if ( (tcpdata = read8u()) != 'T') { // missing tcp data
651      fprintf(stderr, "%s:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), tcpdata);      fprintf(stderr,
652                "%s:%s:%ld: expecting character T, not %c\n",
653                progname, filename, ftell(file), tcpdata);
654      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
655    } // if    } // if
656    
657    decode_tcp_data();    decode_tcp_data();
658    
659    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
660      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);      fprintf(stderr,
661                "%s:%s:%ld: expecting character U, not %c\n",
662                progname, filename, ftell(file), udpdata);
663      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
664    } // if    } // if
665    
666    decode_udp_data();    decode_udp_data();
667    
668    exdent();    exdent();
669  } // decode_host_header_v3  } // decode_host_header_v3
670    
671  void decode_protos_data(void)  void decode_protos_data(void)
672  {  {
673    uint8_t ipprotocount;    uint8_t ipprotocount;
674    
675    uint8_t i;    uint8_t i;
676    
677    ipprotocount = read8u();    ipprotocount = read8u();
678    
679    print_indentation();    print_indentation();
680    printf("IP Proto count %u\n", ipprotocount);    printf("IP Proto count %hhu\n",
681             ipprotocount);
682    
683    indent();    indent();
684    
685    for (i = 0; i < ipprotocount; i++) {    for (i = 0; i < ipprotocount; i++) {
686      uint8_t  proto;      uint8_t  proto;
687      uint64_t in;      uint64_t in;
688      uint64_t out;      uint64_t out;
689    
690      print_indentation();      print_indentation();
691      printf("Protocol #%u of %u:\n", i + 1, ipprotocount);      printf("Protocol #%u of %hhu:\n",
692               i + 1, ipprotocount);
693    
694      proto = read8u();      proto = read8u();
695    
696      indent();      indent();
697      print_indentation();      print_indentation();
698      printf("Protocol 0x%02x\n", proto);      printf("Protocol 0x%02hhx\n",
699               proto);
700    
701      in = read64u();      in = read64u();
702    
703      print_indentation();      print_indentation();
704      printf("In %lu\n", in);      printf("In %lu\n",
705               in);
706    
707      out = read64u();      out = read64u();
708    
709      print_indentation();      print_indentation();
710      printf("Out %lu\n", out);      printf("Out %lu\n",
711               out);
712    
713      exdent();      exdent();
714    } // for    } // for
715    
716    exdent();    exdent();
717  } // decode_protos_data();  } // decode_protos_data();
718    
719  void decode_tcp_data(void)  void decode_tcp_data(void)
720  {  {
721    uint8_t  tcpprotocount;    uint8_t  tcpprotocount;
722    
723    uint16_t i;    uint16_t i;
724    
725    tcpprotocount = read16u();    tcpprotocount = read16u();
726    
727    print_indentation();    print_indentation();
728    printf("TCP proto count %u\n", tcpprotocount);    printf("TCP proto count %hhu\n",
729             tcpprotocount);
730    
731    indent();    indent();
732    
733    for (i = 0; i < tcpprotocount; i++) {    for (i = 0; i < tcpprotocount; i++) {
734      uint16_t port;      uint16_t port;
735      uint64_t syn;      uint64_t syn;
736      uint64_t in;      uint64_t in;
737      uint64_t out;      uint64_t out;
738    
739      port = read16u();      port = read16u();
740    
741      print_indentation();      print_indentation();
742      printf("Port %u:\n", port);      printf("Port %hu:\n",
743               port);
744    
745      syn = read64u();      syn = read64u();
746    
747      indent();      indent();
748      print_indentation();      print_indentation();
749      printf("SYN %lu\n", syn);      printf("SYN %lu\n",
750               syn);
751    
752      in = read64u();      in = read64u();
753    
754      print_indentation();      print_indentation();
755      printf("In %lu\n", in);      printf("In %lu\n",
756               in);
757    
758      out = read64u();      out = read64u();
759    
760      print_indentation();      print_indentation();
761      printf("Out %lu\n", out);      printf("Out %lu\n",
762               out);
763    
764      exdent();      exdent();
765    } // for    } // for
766    
767    exdent();    exdent();
768  } // decode_tcp_data()  } // decode_tcp_data()
769    
770  void decode_udp_data(void)  void decode_udp_data(void)
771  {  {
772    uint8_t  udpprotocount;    uint8_t  udpprotocount;
773    
774    uint16_t i;    uint16_t i;
775    
776    udpprotocount = read16u();    udpprotocount = read16u();
777    
778    print_indentation();    print_indentation();
779    printf("UDP proto count %u\n", udpprotocount);    printf("UDP proto count %hhu\n",
780             udpprotocount);
781    
782    indent();    indent();
783    
784    for (i = 0; i < udpprotocount; i++) {    for (i = 0; i < udpprotocount; i++) {
785      uint16_t port;      uint16_t port;
786      uint64_t in;      uint64_t in;
787      uint64_t out;      uint64_t out;
788    
789      port = read16u();      port = read16u();
790    
791      print_indentation();      print_indentation();
792      printf("Port %u:\n", port);      printf("Port %hu:\n",
793               port);
794    
795      in = read64u();      in = read64u();
796    
797      indent();      indent();
798      print_indentation();      print_indentation();
799      printf("In %lu\n", in);      printf("In %lu\n",
800               in);
801    
802      out = read64u();      out = read64u();
803    
804      print_indentation();      print_indentation();
805      printf("Out %lu\n", out);      printf("Out %lu\n",
806               out);
807    
808      exdent();      exdent();
809    } // for    } // for
810    
811    exdent();    exdent();
812  } // decode_udp_data()  } // decode_udp_data()
813    
814  void decode_graph_db_v1(void)  void decode_graph_db_v1(void)
815  {  {
816    int64_t  lasttime;    int64_t  lasttime;
817    
818    uint32_t i;    uint32_t i;
819    
820    lasttime = read64s();    lasttime = read64s();
821    
822    indent();    indent();
823    print_indentation();    print_indentation();
824    printf("Last time 0x%lx = %ld = ", lasttime, lasttime);    printf("Last time 0x%lx = %ld = ",
825             lasttime, lasttime);
826    print_time_t(lasttime);    print_time_t(lasttime);
827    puts("");    puts("");
828    
829    for (i = 0; i < 4; i++) {    for (i = 0; i < 4; i++) {
830      uint8_t  nbars;      uint8_t  nbars;
831      uint8_t  idxlastbar;      uint8_t  idxlastbar;
832    
833      uint32_t j;      uint32_t j;
834    
835      print_indentation();      print_indentation();
836      printf("Graph #%u of 4:\n", i + 1);      printf("Graph #%u of 4:\n",
837               i + 1);
838    
839      nbars = read8u();      nbars = read8u();
840    
841      indent();      indent();
842      print_indentation();      print_indentation();
843      printf("Number of bars %u\n", nbars);      printf("Number of bars %hhu\n",
844               nbars);
845    
846      idxlastbar = read8u();      idxlastbar = read8u();
847    
848      print_indentation();      print_indentation();
849      printf("Index of last bar %u\n", idxlastbar);      printf("Index of last bar %hhu\n",
850               idxlastbar);
851    
852      indent();      indent();
853      for (j = 0; j < idxlastbar; j++) {      for (j = 0; j < idxlastbar; j++) {
854        uint64_t in;        uint64_t in;
855        uint64_t out;        uint64_t out;
856    
857        print_indentation();        print_indentation();
858        printf("Bar #%u of %u:\n", j + 1, idxlastbar);        printf("Bar #%u of %hhu:\n",
859                 j + 1, idxlastbar);
860    
861        in = read64u();        in = read64u();
862    
863        indent();        indent();
864        print_indentation();        print_indentation();
865        printf("In %lu\n", in);        printf("In %lu\n",
866                 in);
867    
868        out = read64u();        out = read64u();
869    
870        print_indentation();        print_indentation();
871        printf("Out %lu\n", out);        printf("Out %lu\n",
872                 out);
873    
874        exdent();        exdent();
875      } // for      } // for
876      exdent();      exdent();
877    
878      exdent();      exdent();
879    } // for    } // for
880    
881    exdent();    exdent();
882  } // decode_graph_db_v1()  } // decode_graph_db_v1()
883    
884  void handle_file_error(void);  void handle_file_error(void);
885    
886  uint8_t read8u(void)  uint8_t read8u(void)
887  {  {
# Line 934  void handle_file_error(void) Line 1016  void handle_file_error(void)
1016    
1017  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
1018    p2[1] = ntohl(p1[0]);    p2[1] = ntohl(p1[0]);
1019    p2[0] = ntohl(p1[1]);    p2[0] = ntohl(p1[1]);
1020    v = tmp;    v = tmp;
1021  #endif  #endif
1022    
1023    return v;    return v;
1024  } // read64s()  } // read64s()
1025    
1026  void handle_file_error(void)  void handle_file_error(void)
1027  {  {
1028    int saved_errno = errno;    int saved_errno = errno;
1029    
1030    if (feof(file) != 0) {    if (feof(file) != 0) {
1031      fprintf(stderr, "%s:%s:%ld: premature end-of-file\n", progname, filename, ftell(file));      fprintf(stderr,
1032                "%s:%s:%ld: premature end-of-file\n",
1033                progname, filename, ftell(file));
1034      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
1035    } // if    } // if
1036    
1037    if (ferror(file) != 0) {    if (ferror(file) != 0) {
1038      fprintf(stderr, "%s:%s:%ld: file error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno);      fprintf(stderr,
1039                "%s:%s:%ld: file error, errno = %s (%d)\n",
1040                progname, filename, ftell(file),
1041                strerror(saved_errno), saved_errno);
1042      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
1043    } // if    } // if
1044  } // handle_file_error()  } // handle_file_error()
1045    
1046  int64_t indentation = 0LL;  int64_t indentation = 0LL;
1047    
1048  void indent(void)  void indent(void)
1049  {  {
1050    if (indentation < (INT64_MAX - 2LL)) {    if (indentation < (INT64_MAX - 2LL)) {
1051      indentation += 2LL;      indentation += 2LL;
1052    } // if    } // if
1053  } // indent()  } // indent()
1054    
1055  void exdent(void)  void exdent(void)
1056  {  {
# Line 982  void print_time_t(time_t t) Line 1069  void print_time_t(time_t t)
1069      putchar(' ');      putchar(' ');
1070    } // for    } // for
1071  } // print_indentation()  } // print_indentation()
1072    
1073  void print_time_t(time_t t)  void print_time_t(time_t t)
1074  {  {
1075    struct tm *stm;    struct tm *stm;
1076    char buffer[1024];    char buffer[1024];
1077    
1078    stm = gmtime(&t);    stm = gmtime(&t);
1079    
1080    // ISO 8601 format    // ISO 8601 format
1081    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) {
1082      int saved_errno = errno;      int saved_errno = errno;
1083    
1084      fprintf(stderr, "%s:%s:%ld: strftime() error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno);      fprintf(stderr,
1085                "%s:%s:%ld: strftime() error, errno = %s (%d)\n",
1086                progname, filename, ftell(file),
1087                strerror(saved_errno), saved_errno);
1088      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
1089    } // if    } // if
1090    
1091    fputs(buffer, stdout);    fputs(buffer, stdout);
1092  } // print_time_t()  } // print_time_t()
1093    
1094  // darkstattype.c  // darkstattype.c


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

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