--- trunk/darkstattype.c 2014/03/19 14:02:41 6 +++ trunk/darkstattype.c 2014/03/20 09:49:12 9 @@ -30,6 +30,7 @@ */ #include +#include #include #include #include @@ -39,6 +40,7 @@ FILE *file = 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); @@ -365,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 0x%lx = %ld = ", lastseen, 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(); @@ -506,23 +537,43 @@ void decode_host_header_v3(void) 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 0x%lx = %ld = ", lastseen, 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(); @@ -872,8 +923,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 +937,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 +949,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 +963,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;