--- trunk/darkstattype.c 2014/03/19 09:13:57 5 +++ trunk/darkstattype.c 2014/03/19 15:40:13 7 @@ -29,6 +29,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +// Define FOLLOW_SPECIFICATION to 0 (zero) to go with darkstat's own format as of 3.0.718. +#define FOLLOW_SPECIFICATION 0 + +// Define FOLLOW_SPECIFICATION to 1 (one) to go with the format specified in the export-format.txt file. +//#define FOLLOW_SPECIFICATION 1 + #include #include #include @@ -36,8 +42,8 @@ #include #include -const char * progname = NULL; -const char * filename = NULL; +const char *progname = NULL; +const char *filename = NULL; FILE *file = NULL; int main(int argc, char **argv) @@ -164,8 +170,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: file header = 0x%x, not 0x%x\n", progname, filename, fileheader, FILE_HEADER_V1); exit(EXIT_FAILURE); } // if @@ -174,19 +182,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: unknown section header = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, sectionheader, HOST_DB_V1, GRAPH_DB_V1); exit(EXIT_FAILURE); } // else } // for @@ -218,23 +229,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: 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); exit(EXIT_FAILURE); } // else @@ -356,6 +371,7 @@ void decode_host_header_v2(void) printf("IPv4 address %d.%d.%d.%d\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); +#if FOLLOW_SPECIFICATION == 1 macaddress[0] = read8u(); macaddress[1] = read8u(); macaddress[2] = read8u(); @@ -369,10 +385,28 @@ void decode_host_header_v2(void) lastseen = read64s(); print_indentation(); - printf("Last seen %ld = ", lastseen); + printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); print_time_t(lastseen); puts(""); +#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]); +#endif + hostnamelen = read8u(); print_indentation(); @@ -497,6 +531,7 @@ void decode_host_header_v3(void) exit(EXIT_FAILURE); } // else +#if FOLLOW_SPECIFICATION == 1 macaddress[0] = read8u(); macaddress[1] = read8u(); macaddress[2] = read8u(); @@ -510,10 +545,28 @@ void decode_host_header_v3(void) lastseen = read64s(); print_indentation(); - printf("Last seen %ld = ", lastseen); + printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); print_time_t(lastseen); puts(""); +#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]); +#endif + hostnamelen = read8u(); print_indentation(); @@ -700,7 +753,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 +916,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 +930,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 +942,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 +956,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;