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

Diff of /trunk/darkstattype.c

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

Revision 9 by trond, 2014-03-20T09:49:12Z Revision 10 by trond, 2014-03-21T15:40:57Z
# Line 166  void decode_file(void) Line 166  void decode_file(void)
166    void decode_graph_db_v1(void);    void decode_graph_db_v1(void);
167    
168    unsigned int fileheader;    unsigned int fileheader;
169    unsigned int sectionheader;    unsigned int sectionheader;
170    
171    unsigned int i;    unsigned int i;
172    
173    if ( (file = fopen(filename, "rb")) == NULL) {    if ( (file = fopen(filename, "rb")) == NULL) {
174      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);
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: file header = 0x%x, not 0x%x\n", progname, filename, 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);
182      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
183    } // if    } // if
184    
185    printf("File header 0x%x\n", fileheader);    printf("File header 0x%x\n", fileheader);
186    
187    // Possible section header for host_db v1 and later graph_db v1.    // Possible section header for host_db v1 and later graph_db v1.
188    indent();    indent();
189    
190  #define HOST_DB_V1 0xDA485301U  #define HOST_DB_V1 0xDA485301U
191  #define GRAPH_DB_V1 0xDA475201U  #define GRAPH_DB_V1 0xDA475201U
192    
193    for (i = 0; i < 2; i++) {    for (i = 0; i < 2; i++) {
194      if ( (sectionheader = read32u()) == HOST_DB_V1) {      if ( (sectionheader = read32u()) == HOST_DB_V1) {
195        print_indentation();        print_indentation();
196        printf("Section header host_db v1 0x%x\n", sectionheader);        printf("Section header host_db v1 0x%x\n", sectionheader);
197        decode_host_db_v1();        decode_host_db_v1();
198      } // if      } // if
199      else if (sectionheader == GRAPH_DB_V1) {      else if (sectionheader == GRAPH_DB_V1) {
200        print_indentation();        print_indentation();
201        printf("Section header graph_db v1 0x%x\n", sectionheader);        printf("Section header graph_db v1 0x%x\n", sectionheader);
202        decode_graph_db_v1();        decode_graph_db_v1();
203      } // else if      } // else if
204      else {      else {
205        fprintf(stderr, "%s:%s: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, sectionheader, HOST_DB_V1, GRAPH_DB_V1);        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);
206        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
207      } // else      } // else
208    } // for    } // for
209    
210    exdent();    exdent();
211  } // decode_file()  } // decode_file()
212    
213  void decode_host_db_v1(void)  void decode_host_db_v1(void)
214  {  {
215    void decode_host_header_v1(void);    void decode_host_header_v1(void);
216    void decode_host_header_v2(void);    void decode_host_header_v2(void);
217    void decode_host_header_v3(void);    void decode_host_header_v3(void);
218    
219    unsigned int hostcount;    unsigned int hostcount;
220    unsigned int i;    unsigned int i;
# Line 242  void decode_host_db_v1(void) Line 242  void decode_host_db_v1(void)
242        print_indentation();        print_indentation();
243        printf("Host header v3 0x%x\n", hostheader);        printf("Host header v3 0x%x\n", hostheader);
244        decode_host_header_v3();        decode_host_header_v3();
245      } // if      } // if
246      else if (hostheader == HOST_HEADER_V2) { // host header v2      else if (hostheader == HOST_HEADER_V2) { // host header v2
247        print_indentation();        print_indentation();
248        printf("Host header v2 0x%x\n", hostheader);        printf("Host header v2 0x%x\n", hostheader);
249        decode_host_header_v2();        decode_host_header_v2();
250      } // else if      } // else if
251      else if (hostheader == HOST_HEADER_V1) { // host header v1      else if (hostheader == HOST_HEADER_V1) { // host header v1
252        print_indentation();        print_indentation();
253        printf("Host header v1 0x%x\n", hostheader);        printf("Host header v1 0x%x\n", hostheader);
254        decode_host_header_v1();        decode_host_header_v1();
255      } // else if      } // else if
256      else { // unknown host header version      else { // unknown host header version
257        fprintf(stderr, "%s:%s: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n", progname, filename, hostheader, HOST_HEADER_V3, HOST_HEADER_V2, HOST_HEADER_V1);        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);
258        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
259      } // else      } // else
260    
261      exdent();      exdent();
262    } // for    } // for
263    
264    exdent();    exdent();
265  } // decode_host_db_v1()  } // decode_host_db_v1()
266    
267  void decode_protos_data(void);  void decode_protos_data(void);
268  void decode_tcp_data(void);  void decode_tcp_data(void);
269  void decode_udp_data(void);  void decode_udp_data(void);
270    
271  void decode_host_header_v1(void)  void decode_host_header_v1(void)
272  {  {
# Line 315  void decode_host_header_v1(void) Line 315  void decode_host_header_v1(void)
315    
316    print_indentation();    print_indentation();
317    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
318    
319    bytesin = read64u();    bytesin = read64u();
320    
321    print_indentation();    print_indentation();
322    printf("Bytes in %lu\n", bytesin);    printf("Bytes in %lu\n", bytesin);
323    
324    bytesout = read64u();    bytesout = read64u();
325    
326    print_indentation();    print_indentation();
327    printf("Bytes out %lu\n", bytesout);    printf("Bytes out %lu\n", bytesout);
328    
329    if ( (protosdata = read8u()) != 'P') { // missing protos data    if ( (protosdata = read8u()) != 'P') { // missing protos data
330      fprintf(stderr, "%s: expecting character P, not %c\n", progname, protosdata);      fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), protosdata);
331      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
332    } // if    } // if
333    
334    decode_protos_data();    decode_protos_data();
335    
336    if ( (tcpdata = read8u()) != 'T') { // missing tcp data    if ( (tcpdata = read8u()) != 'T') { // missing tcp data
337      fprintf(stderr, "%s: expecting character T, not %c\n", progname, tcpdata);      fprintf(stderr, "%s:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), tcpdata);
338      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
339    } // if    } // if
340    
341    decode_tcp_data();    decode_tcp_data();
342    
343    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
344      fprintf(stderr, "%s: expecting character U, not %c\n", progname, udpdata);      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);
345      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
346    } // if    } // if
347    
348    decode_udp_data();    decode_udp_data();
349    
350    exdent();    exdent();
351  } // decode_host_header_v1  } // decode_host_header_v1
352    
353  void decode_host_header_v2(void)  void decode_host_header_v2(void)
354  {  {
355    unsigned char ipv4address[4];    unsigned char ipv4address[4];
356    unsigned char macaddress[6];    unsigned char macaddress[6];
357      signed long lastseen;      signed long lastseen;
358    unsigned char hostnamelen;    unsigned char hostnamelen;
359    unsigned char hostname[256];    unsigned char hostname[256];
# Line 425  void decode_host_header_v2(void) Line 425  void decode_host_header_v2(void)
425    
426    print_indentation();    print_indentation();
427    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
428    
429    bytesin = read64u();    bytesin = read64u();
430    
431    print_indentation();    print_indentation();
432    printf("Bytes in %lu\n", bytesin);    printf("Bytes in %lu\n", bytesin);
433    
434    bytesout = read64u();    bytesout = read64u();
435    
436    print_indentation();    print_indentation();
437    printf("Bytes out %lu\n", bytesout);    printf("Bytes out %lu\n", bytesout);
438    
439    if ( (protosdata = read8u()) != 'P') { // missing protos data    if ( (protosdata = read8u()) != 'P') { // missing protos data
440      fprintf(stderr, "%s: expecting character P, not %c\n", progname, protosdata);      fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), protosdata);
441      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
442    } // if    } // if
443    
444    decode_protos_data();    decode_protos_data();
445    
446    if ( (tcpdata = read8u()) != 'T') { // missing tcp data    if ( (tcpdata = read8u()) != 'T') { // missing tcp data
447      fprintf(stderr, "%s: expecting character T, not %c\n", progname, tcpdata);      fprintf(stderr, "%s:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), tcpdata);
448      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
449    } // if    } // if
450    
451    decode_tcp_data();    decode_tcp_data();
452    
453    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
454      fprintf(stderr, "%s: expecting character U, not %c\n", progname, udpdata);      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);
455      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
456    } // if    } // if
457    
458    decode_udp_data();    decode_udp_data();
459    
460    exdent();    exdent();
461  } // decode_host_header_v2  } // decode_host_header_v2
462    
463  void decode_host_header_v3(void)  void decode_host_header_v3(void)
464  {  {
465    unsigned char addressfamily;    unsigned char addressfamily;
466    unsigned char ipv4address[4];    unsigned char ipv4address[4];
467    unsigned char ipv6address[16];    unsigned char ipv6address[16];
468    unsigned char macaddress[6];    unsigned char macaddress[6];
469      signed long lastseen;      signed long lastseen;
# Line 521  void decode_host_header_v3(void) Line 521  void decode_host_header_v3(void)
521             ipv6address[ 3],             ipv6address[ 3],
522             ipv6address[ 4],             ipv6address[ 4],
523             ipv6address[ 5],             ipv6address[ 5],
524             ipv6address[ 6],             ipv6address[ 6],
525             ipv6address[ 7],             ipv6address[ 7],
526             ipv6address[ 8],             ipv6address[ 8],
527             ipv6address[ 9],             ipv6address[ 9],
528             ipv6address[10],             ipv6address[10],
529             ipv6address[11],             ipv6address[11],
530             ipv6address[12],             ipv6address[12],
531             ipv6address[13],             ipv6address[13],
532             ipv6address[14],             ipv6address[14],
533             ipv6address[15]);             ipv6address[15]);
534    } // else if    } // else if
535    else { // unknown address family    else { // unknown address family
536      fprintf(stderr, "%s:%s: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, addressfamily, 0x04, 0x06);      fprintf(stderr, "%s:%s:%ld: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, ftell(file), addressfamily, 0x04, 0x06);
537      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
538    } // else    } // else
539    
540    if (follow_specification == true) {    if (follow_specification == true) {
541      macaddress[0] = read8u();      macaddress[0] = read8u();
542      macaddress[1] = read8u();      macaddress[1] = read8u();
543      macaddress[2] = read8u();      macaddress[2] = read8u();
544      macaddress[3] = read8u();      macaddress[3] = read8u();
545      macaddress[4] = read8u();      macaddress[4] = read8u();
546      macaddress[5] = read8u();      macaddress[5] = read8u();
547    
548      print_indentation();      print_indentation();
549      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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
550    
551      lastseen = read64s();      lastseen = read64s();
# Line 586  void decode_host_header_v3(void) Line 586  void decode_host_header_v3(void)
586    
587    print_indentation();    print_indentation();
588    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
589    
590    bytesin = read64u();    bytesin = read64u();
591    
592    print_indentation();    print_indentation();
593    printf("Bytes in %lu\n", bytesin);    printf("Bytes in %lu\n", bytesin);
594    
595    bytesout = read64u();    bytesout = read64u();
596    
597    print_indentation();    print_indentation();
598    printf("Bytes out %lu\n", bytesout);    printf("Bytes out %lu\n", bytesout);
599    
600    if ( (protosdata = read8u()) != 'P') { // missing protos data    if ( (protosdata = read8u()) != 'P') { // missing protos data
601      fprintf(stderr, "%s: expecting character P, not %c\n", progname, protosdata);      fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), protosdata);
602      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
603    } // if    } // if
604    
605    decode_protos_data();    decode_protos_data();
606    
607    if ( (tcpdata = read8u()) != 'T') { // missing tcp data    if ( (tcpdata = read8u()) != 'T') { // missing tcp data
608      fprintf(stderr, "%s: expecting character T, not %c\n", progname, tcpdata);      fprintf(stderr, "%s:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), tcpdata);
609      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
610    } // if    } // if
611    
612    decode_tcp_data();    decode_tcp_data();
613    
614    if ( (udpdata = read8u()) != 'U') { // missing udp data    if ( (udpdata = read8u()) != 'U') { // missing udp data
615      fprintf(stderr, "%s: expecting character U, not %c\n", progname, udpdata);      fprintf(stderr, "%s:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata);
616      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
617    } // if    } // if
618    
619    decode_udp_data();    decode_udp_data();
620    
621    exdent();    exdent();
622  } // decode_host_header_v3  } // decode_host_header_v3
623    
624  void decode_protos_data(void)  void decode_protos_data(void)
625  {  {
626    unsigned char ipprotocount;    unsigned char ipprotocount;
627    
628    unsigned char u;    unsigned char u;
629    
630    ipprotocount = read8u();    ipprotocount = read8u();
# Line 961  void handle_file_error(void) Line 961  void handle_file_error(void)
961    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
962      handle_file_error();      handle_file_error();
963    } // if    } // if
964    
965  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
966    p2[1] = ntohl(p1[0]);    p2[1] = ntohl(p1[0]);
967    p2[0] = ntohl(p1[1]);    p2[0] = ntohl(p1[1]);
968    v = tmp;    v = tmp;
969  #endif  #endif
970    
971    return v;    return v;
972  } // read64s()  } // read64s()
973    
974  void handle_file_error(void)  void handle_file_error(void)
975  {  {
976      int saved_errno = errno;
977    
978    if (feof(file) != 0) {    if (feof(file) != 0) {
979      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);      fprintf(stderr, "%s:%s:%ld: premature end-of-file\n", progname, filename, ftell(file));
980      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
981    } // if    } // if
982    
983    if (ferror(file) != 0) {    if (ferror(file) != 0) {
984      fprintf(stderr, "%s:%s: file error, errno = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s:%s:%ld: file error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno);
985      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
986    } // if    } // if
987  } // handle_file_error()  } // handle_file_error()
988    
989  signed long long indentation = 0LL;  signed long long indentation = 0LL;
990    
991  void indent(void)  void indent(void)
992  {  {
993    indentation += 2LL;    indentation += 2LL;
994  } // indent()  } // indent()
995    
996  void exdent(void)  void exdent(void)
997  {  {
998    indentation -= 2LL;    indentation -= 2LL;
999    
# Line 1006  void print_time_t(time_t t) Line 1008  void print_time_t(time_t t)
1008    
1009    for (i = 0; i < indentation; i++) {    for (i = 0; i < indentation; i++) {
1010      putchar(' ');      putchar(' ');
1011    } // for    } // for
1012  } // print_indentation()  } // print_indentation()
1013    
1014  void print_time_t(time_t t)  void print_time_t(time_t t)
1015  {  {
1016    struct tm *stm;    struct tm *stm;
1017    char buffer[1024];    char buffer[1024];
1018    
1019    stm = gmtime(&t);    stm = gmtime(&t);
1020    
1021    // ISO 8601 format    // ISO 8601 format
1022    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) {
1023      fprintf(stderr, "%s:%s: strftime() error, errno = %s (%d)\n", progname, filename, strerror(errno), errno);      int saved_errno = errno;
1024    
1025        fprintf(stderr, "%s:%s:%ld: strftime() error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno);
1026      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
1027    } // if    } // if
1028    
1029    fputs(buffer, stdout);    fputs(buffer, stdout);
1030  } // print_time_t()  } // print_time_t()
1031    
1032  // 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