--- trunk/darkstattype.c 2014/03/19 14:02:41 6 +++ trunk/darkstattype.c 2014/03/20 09:00:33 8 @@ -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 @@ -88,7 +94,11 @@ void show_usage(int exitcode) { fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr, "Usage: %s [-h] [-v] filename\n" - " E.g.: %s darkstat.db\n", + " E.g.: %s darkstat.db\n\n" + + "Options:\n" + "-h\tShow this help message and exit.\n" + "-v\tShow version and copyright and exit.\n", progname, progname); exit(exitcode); @@ -365,6 +375,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(); @@ -381,7 +392,25 @@ void decode_host_header_v2(void) 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(); @@ -506,6 +535,7 @@ void decode_host_header_v3(void) exit(EXIT_FAILURE); } // else +#if FOLLOW_SPECIFICATION == 1 macaddress[0] = read8u(); macaddress[1] = read8u(); macaddress[2] = read8u(); @@ -522,7 +552,25 @@ void decode_host_header_v3(void) 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(); @@ -872,8 +920,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) { @@ -881,8 +934,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; @@ -892,8 +946,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) { @@ -901,8 +960,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;