--- trunk/darkstattype.c 2014/03/19 08:51:24 4 +++ trunk/darkstattype.c 2014/03/21 16:04:33 12 @@ -30,15 +30,17 @@ */ #include +#include #include #include #include #include #include -const char * progname = NULL; -const char * filename = NULL; +const char *progname = NULL; +const char *filename = NULL; FILE *file = NULL; +bool follow_specification = false; int main(int argc, char **argv) { @@ -51,8 +53,12 @@ int main(int argc, char **argv) progname = argv[0]; opterr = 0; - while ( (i = getopt(argc, argv, "hv")) != -1) { + while ( (i = getopt(argc, argv, "fhv")) != -1) { switch (i) { + case 'f': + follow_specification = !follow_specification; + break; + case 'h': show_usage(EXIT_SUCCESS); break; @@ -87,8 +93,13 @@ void show_usage(int exitcode) void show_usage(int exitcode) { fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr, - "Usage: %s [-h] [-v] filename\n" - " E.g.: %s darkstat.db\n", + "Usage: %s [-f] [-h] [-v] filename\n" + " E.g.: %s darkstat.db\n\n" + + "Options:\n" + "-f\tToggle follow strict specification, default is false.\n" + "-h\tShow this help message and exit.\n" + "-v\tShow version and copyright and exit.\n", progname, progname); exit(exitcode); @@ -164,8 +175,10 @@ void decode_file(void) exit(EXIT_FAILURE); } // if - if ( (fileheader = read32u()) != 0xDA314159U) { // not darkstat export format - fprintf(stderr, "%s:%s: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, 0xDA314159U); +#define FILE_HEADER_V1 0xDA314159U + + if ( (fileheader = read32u()) != FILE_HEADER_V1) { // not darkstat export format + fprintf(stderr, "%s:%s:%ld: file header = 0x%x, not 0x%x\n", progname, filename, ftell(file), fileheader, FILE_HEADER_V1); exit(EXIT_FAILURE); } // if @@ -174,19 +187,22 @@ void decode_file(void) // Possible section header for host_db v1 and later graph_db v1. indent(); +#define HOST_DB_V1 0xDA485301U +#define GRAPH_DB_V1 0xDA475201U + for (i = 0; i < 2; i++) { - if ( (sectionheader = read32u()) == 0xDA485301U) { + if ( (sectionheader = read32u()) == HOST_DB_V1) { print_indentation(); - printf("Section header 0x%x\n", sectionheader); + printf("Section header host_db v1 0x%x\n", sectionheader); decode_host_db_v1(); } // if - else if (sectionheader == 0xDA475201U) { + else if (sectionheader == GRAPH_DB_V1) { print_indentation(); - printf("Section header 0x%x\n", sectionheader); + printf("Section header graph_db v1 0x%x\n", sectionheader); decode_graph_db_v1(); } // else if else { - fprintf(stderr, "%s:%s: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, sectionheader, 0xDA485301U, 0xDA475201U); + 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); exit(EXIT_FAILURE); } // else } // for @@ -218,23 +234,27 @@ void decode_host_db_v1(void) indent(); - if ( (hostheader = read32u()) == 0x48535403U) { // host header v3 +#define HOST_HEADER_V3 0x48535403U +#define HOST_HEADER_V2 0x48535402U +#define HOST_HEADER_V1 0x48535401U + + if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3 print_indentation(); printf("Host header v3 0x%x\n", hostheader); decode_host_header_v3(); } // if - else if (hostheader == 0x48535402U) { // host header v2 + else if (hostheader == HOST_HEADER_V2) { // host header v2 print_indentation(); printf("Host header v2 0x%x\n", hostheader); decode_host_header_v2(); } // else if - else if (hostheader == 0x48535401U) { // host header v1 + else if (hostheader == HOST_HEADER_V1) { // host header v1 print_indentation(); printf("Host header v1 0x%x\n", hostheader); decode_host_header_v1(); } // else if else { // unknown host header version - fprintf(stderr, "%s:%s: unknown host header = 0x%x, neither 0x%x nor 0x%x nor 0x%x\n", progname, filename, hostheader, 0x48535403U, 0x48535402U, 0x48535401U); + 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); exit(EXIT_FAILURE); } // else @@ -307,21 +327,21 @@ void decode_host_header_v1(void) printf("Bytes out %lu\n", bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - 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); exit(EXIT_FAILURE); } // if decode_protos_data(); if ( (tcpdata = read8u()) != 'T') { // missing tcp data - 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); exit(EXIT_FAILURE); } // if decode_tcp_data(); if ( (udpdata = read8u()) != 'U') { // missing udp data - 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); exit(EXIT_FAILURE); } // if @@ -356,23 +376,43 @@ void decode_host_header_v2(void) printf("IPv4 address %d.%d.%d.%d\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); - macaddress[0] = read8u(); - macaddress[1] = read8u(); - macaddress[2] = read8u(); - macaddress[3] = read8u(); - macaddress[4] = read8u(); - macaddress[5] = read8u(); + if (follow_specification == true) { + macaddress[0] = read8u(); + macaddress[1] = read8u(); + macaddress[2] = read8u(); + macaddress[3] = read8u(); + macaddress[4] = read8u(); + macaddress[5] = read8u(); - print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + print_indentation(); + printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); - lastseen = read64s(); + lastseen = read64s(); - print_indentation(); - printf("Last seen %ld = ", lastseen); - print_time_t(lastseen); - puts(""); + print_indentation(); + printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + print_time_t(lastseen); + puts(""); + } // if + else { + lastseen = read64s(); + print_indentation(); + printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + print_time_t(lastseen); + puts(""); + + macaddress[0] = read8u(); + macaddress[1] = read8u(); + macaddress[2] = read8u(); + macaddress[3] = read8u(); + macaddress[4] = read8u(); + macaddress[5] = read8u(); + + print_indentation(); + printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + } // else + hostnamelen = read8u(); print_indentation(); @@ -397,21 +437,21 @@ void decode_host_header_v2(void) printf("Bytes out %lu\n", bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - 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); exit(EXIT_FAILURE); } // if decode_protos_data(); if ( (tcpdata = read8u()) != 'T') { // missing tcp data - 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); exit(EXIT_FAILURE); } // if decode_tcp_data(); if ( (udpdata = read8u()) != 'U') { // missing udp data - 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); exit(EXIT_FAILURE); } // if @@ -422,20 +462,20 @@ void decode_host_header_v3(void) void decode_host_header_v3(void) { - unsigned char addressfamily; - unsigned char ipv4address[4]; - unsigned char ipv6address[16]; - unsigned char macaddress[6]; - signed long lastseen; - unsigned char hostnamelen; - unsigned char hostname[256]; - unsigned long bytesin; - unsigned long bytesout; - unsigned char protosdata; - unsigned char tcpdata; - unsigned char udpdata; + unsigned char addressfamily; + unsigned char ipv4address[4]; + unsigned short ipv6address[8]; + unsigned char macaddress[6]; + signed long lastseen; + unsigned char hostnamelen; + unsigned char hostname[256]; + unsigned long bytesin; + unsigned long bytesout; + unsigned char protosdata; + unsigned char tcpdata; + unsigned char udpdata; - unsigned char i; + unsigned char i; indent(); @@ -456,64 +496,68 @@ void decode_host_header_v3(void) print_indentation(); printf("IPv6 address family (0x%02x)\n", addressfamily); - ipv6address[ 0] = read8u(); - ipv6address[ 1] = read8u(); - ipv6address[ 2] = read8u(); - ipv6address[ 3] = read8u(); - ipv6address[ 4] = read8u(); - ipv6address[ 5] = read8u(); - ipv6address[ 6] = read8u(); - ipv6address[ 7] = read8u(); - ipv6address[ 8] = read8u(); - ipv6address[ 9] = read8u(); - ipv6address[10] = read8u(); - ipv6address[11] = read8u(); - ipv6address[12] = read8u(); - ipv6address[13] = read8u(); - ipv6address[14] = read8u(); - ipv6address[15] = read8u(); + ipv6address[0] = read16u(); + ipv6address[1] = read16u(); + ipv6address[2] = read16u(); + ipv6address[3] = read16u(); + ipv6address[4] = read16u(); + ipv6address[5] = read16u(); + ipv6address[6] = read16u(); + ipv6address[7] = read16u(); print_indentation(); - printf("IPv6 address %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", - ipv6address[ 0], - ipv6address[ 1], - ipv6address[ 2], - ipv6address[ 3], - ipv6address[ 4], - ipv6address[ 5], - ipv6address[ 6], - ipv6address[ 7], - ipv6address[ 8], - ipv6address[ 9], - ipv6address[10], - ipv6address[11], - ipv6address[12], - ipv6address[13], - ipv6address[14], - ipv6address[15]); + printf("IPv6 address %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", + ipv6address[0], + ipv6address[1], + ipv6address[2], + ipv6address[3], + ipv6address[4], + ipv6address[5], + ipv6address[6], + ipv6address[7]); } // else if else { // unknown address family - 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); exit(EXIT_FAILURE); } // else - macaddress[0] = read8u(); - macaddress[1] = read8u(); - macaddress[2] = read8u(); - macaddress[3] = read8u(); - macaddress[4] = read8u(); - macaddress[5] = read8u(); + if (follow_specification == true) { + macaddress[0] = read8u(); + macaddress[1] = read8u(); + macaddress[2] = read8u(); + macaddress[3] = read8u(); + macaddress[4] = read8u(); + macaddress[5] = read8u(); - print_indentation(); - printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + print_indentation(); + printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); - lastseen = read64s(); + lastseen = read64s(); - print_indentation(); - printf("Last seen %ld = ", lastseen); - print_time_t(lastseen); - puts(""); + print_indentation(); + printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + print_time_t(lastseen); + puts(""); + } // if + else { + lastseen = read64s(); + print_indentation(); + printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + print_time_t(lastseen); + puts(""); + + macaddress[0] = read8u(); + macaddress[1] = read8u(); + macaddress[2] = read8u(); + macaddress[3] = read8u(); + macaddress[4] = read8u(); + macaddress[5] = read8u(); + + print_indentation(); + printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); + } // else + hostnamelen = read8u(); print_indentation(); @@ -538,21 +582,21 @@ void decode_host_header_v3(void) printf("Bytes out %lu\n", bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - 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); exit(EXIT_FAILURE); } // if decode_protos_data(); if ( (tcpdata = read8u()) != 'T') { // missing tcp data - 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); exit(EXIT_FAILURE); } // if decode_tcp_data(); if ( (udpdata = read8u()) != 'U') { // missing udp data - 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); exit(EXIT_FAILURE); } // if @@ -586,7 +630,7 @@ void decode_protos_data(void) indent(); print_indentation(); - printf("Protocol 0x%x\n", proto); + printf("Protocol 0x%02x\n", proto); in = read64u(); @@ -700,7 +744,7 @@ void decode_graph_db_v1(void) indent(); print_indentation(); - printf("Last time %ld = ", lasttime); + printf("Last time 0x%lx = %ld = ", lasttime, lasttime); print_time_t(lasttime); puts(""); @@ -863,8 +907,13 @@ unsigned long read64u(void) { size_t r; unsigned long v; - unsigned int *p = (unsigned int *)&v; +#ifdef __LITTLE_ENDIAN__ + unsigned long tmp; + unsigned int *p1 = (unsigned int *)&v; + unsigned int *p2 = (unsigned int *)&tmp; +#endif + //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v)); if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { @@ -872,8 +921,9 @@ unsigned long read64u(void) } // if #ifdef __LITTLE_ENDIAN__ - p[0] = ntohl((unsigned int)p[0]); - p[1] = ntohl((unsigned int)p[1]); + p2[1] = ntohl(p1[0]); + p2[0] = ntohl(p1[1]); + v = tmp; #endif return v; @@ -883,8 +933,13 @@ signed long read64s(void) { size_t r; signed long v; - signed int *p = (signed int *)&v; +#ifdef __LITTLE_ENDIAN__ + signed long tmp; + signed int *p1 = (signed int *)&v; + signed int *p2 = (signed int *)&tmp; +#endif + //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v)); if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) { @@ -892,8 +947,9 @@ signed long read64s(void) } // if #ifdef __LITTLE_ENDIAN__ - p[0] = ntohl((signed int)p[0]); - p[1] = ntohl((signed int)p[1]); + p2[1] = ntohl(p1[0]); + p2[0] = ntohl(p1[1]); + v = tmp; #endif return v; @@ -901,13 +957,15 @@ void handle_file_error(void) void handle_file_error(void) { + int saved_errno = errno; + if (feof(file) != 0) { - 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)); exit(EXIT_FAILURE); } // if if (ferror(file) != 0) { - 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); exit(EXIT_FAILURE); } // if } // handle_file_error() @@ -946,7 +1004,9 @@ void print_time_t(time_t t) // ISO 8601 format if (strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S%z", stm) == 0) { - fprintf(stderr, "%s:%s: strftime() error, errno = %s (%d)\n", progname, filename, strerror(errno), errno); + int saved_errno = errno; + + fprintf(stderr, "%s:%s:%ld: strftime() error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_errno); exit(EXIT_FAILURE); } // if