--- trunk/darkstattype.c 2014/03/21 19:15:01 16 +++ trunk/darkstattype.c 2014/04/09 09:20:17 19 @@ -71,7 +71,8 @@ int main(int argc, char **argv) case '?': /*FALLTHROUGH*/ default: - fprintf(stderr, "%s: illegal option -%c\n\n", progname, optopt); + fprintf(stderr, "%s: illegal option -%c\n\n", + progname, optopt); show_usage(EXIT_FAILURE); break; } // switch @@ -80,7 +81,8 @@ int main(int argc, char **argv) argv += optind; if (*argv == NULL) { - fprintf(stderr, "%s: missing filename\n\n", progname); + fprintf(stderr, "%s: missing filename\n\n", + progname); show_usage(EXIT_FAILURE); } // if @@ -98,7 +100,7 @@ void show_usage(int exitcode) void show_usage(int exitcode) { fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr, - "Usage: %s [-f] [-h] [-v] filename ...\n" + "Usage: %s [-f | -h | -v] filename [...]\n" " E.g.: %s darkstat.db\n\n" "Options:\n" @@ -168,14 +170,16 @@ void decode_file(void) uint32_t i; if ( (file = fopen(filename, "rb")) == NULL) { - 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); exit(EXIT_FAILURE); } // if #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); + 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 @@ -199,7 +203,10 @@ void decode_file(void) decode_graph_db_v1(); } // else if else { - 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); + 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 @@ -207,7 +214,8 @@ void decode_file(void) exdent(); if (fclose(file) != 0) { - fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno); + fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", + progname, filename, strerror(errno), errno); exit(EXIT_FAILURE); } // if } // decode_file() @@ -226,13 +234,15 @@ void decode_host_db_v1(void) hostcount = read32u(); print_indentation(); - printf("Host count %u\n", hostcount); + printf("Host count %u\n", + hostcount); for (i = 0; i < hostcount; i++) { uint32_t hostheader; print_indentation(); - printf("Host #%u of %u:\n", i + 1, hostcount); + printf("Host #%u of %u:\n", + i + 1, hostcount); indent(); @@ -242,21 +252,27 @@ void decode_host_db_v1(void) if ( (hostheader = read32u()) == HOST_HEADER_V3) { // host header v3 print_indentation(); - printf("Host header v3 0x%x\n", hostheader); + printf("Host header v3 0x%x\n", + hostheader); decode_host_header_v3(); } // if else if (hostheader == HOST_HEADER_V2) { // host header v2 print_indentation(); - printf("Host header v2 0x%x\n", hostheader); + printf("Host header v2 0x%x\n", + hostheader); decode_host_header_v2(); } // else if else if (hostheader == HOST_HEADER_V1) { // host header v1 print_indentation(); - printf("Host header v1 0x%x\n", hostheader); + printf("Host header v1 0x%x\n", + hostheader); decode_host_header_v1(); } // else if else { // unknown host header version - 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); + 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 @@ -292,7 +308,7 @@ void decode_host_header_v1(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %u.%u.%u.%u\n", + printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); macaddress[0] = read8u(); @@ -303,12 +319,15 @@ void decode_host_header_v1(void) 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]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); hostnamelen = read8u(); print_indentation(); - printf("Hostname length %u\n", hostnamelen); + printf("Hostname length %u\n", + hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -316,34 +335,43 @@ void decode_host_header_v1(void) hostname[i] = '\0'; print_indentation(); - printf("Hostname %s\n", hostname); + printf("Hostname %s\n", + hostname); bytesin = read64u(); print_indentation(); - printf("Bytes in %lu\n", bytesin); + printf("Bytes in %lu\n", + bytesin); bytesout = read64u(); print_indentation(); - printf("Bytes out %lu\n", bytesout); + printf("Bytes out %lu\n", + bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), 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:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), 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:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character U, not %c\n", + progname, filename, ftell(file), udpdata); exit(EXIT_FAILURE); } // if @@ -375,7 +403,7 @@ void decode_host_header_v2(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %u.%u.%u.%u\n", + printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); if (follow_specification == true) { @@ -387,12 +415,15 @@ void decode_host_header_v2(void) 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]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); } // if @@ -400,7 +431,8 @@ void decode_host_header_v2(void) lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); @@ -412,13 +444,16 @@ void decode_host_header_v2(void) 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]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); } // else hostnamelen = read8u(); print_indentation(); - printf("Hostname length %u\n", hostnamelen); + printf("Hostname length %hhu\n", + hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -426,34 +461,43 @@ void decode_host_header_v2(void) hostname[i] = '\0'; print_indentation(); - printf("Hostname %s\n", hostname); + printf("Hostname %s\n", + hostname); bytesin = read64u(); print_indentation(); - printf("Bytes in %lu\n", bytesin); + printf("Bytes in %lu\n", + bytesin); bytesout = read64u(); print_indentation(); - printf("Bytes out %lu\n", bytesout); + printf("Bytes out %lu\n", + bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), 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:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), 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:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character U, not %c\n", + progname, filename, ftell(file), udpdata); exit(EXIT_FAILURE); } // if @@ -481,9 +525,13 @@ void decode_host_header_v3(void) indent(); - if ( (addressfamily = read8u()) == 0x04) { // IPv4 address +#define A_F_4 0x04 +#define A_F_6 0x06 + + if ( (addressfamily = read8u()) == A_F_4) { // IPv4 address print_indentation(); - printf("IPv4 address family (0x%02x)\n", addressfamily); + printf("IPv4 address family (0x%02hhx)\n", + addressfamily); ipv4address[0] = read8u(); ipv4address[1] = read8u(); @@ -491,12 +539,13 @@ void decode_host_header_v3(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %u.%u.%u.%u\n", + printf("IPv4 address %hhu.%hhu.%hhu.%hhu\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); } // if - else if (addressfamily == 0x06) { // IPv6 address + else if (addressfamily == A_F_6) { // IPv6 address print_indentation(); - printf("IPv6 address family (0x%02x)\n", addressfamily); + printf("IPv6 address family (0x%02hhx)\n", + addressfamily); ipv6address[0] = read16u(); ipv6address[1] = read16u(); @@ -508,18 +557,14 @@ void decode_host_header_v3(void) ipv6address[7] = read16u(); print_indentation(); - 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]); + printf("IPv6 address %04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx:%04hx\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:%ld: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, ftell(file), addressfamily, 0x04, 0x06); + fprintf(stderr, + "%s:%s:%ld: unknown address family = 0x%hhx, neither 0x%x nor 0x%x\n", + progname, filename, ftell(file), addressfamily, A_F_4, A_F_6); exit(EXIT_FAILURE); } // else @@ -532,12 +577,15 @@ void decode_host_header_v3(void) 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]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); } // if @@ -545,7 +593,8 @@ void decode_host_header_v3(void) lastseen = read64s(); print_indentation(); - printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); + printf("Last seen 0x%lx = %ld = ", + lastseen, lastseen); print_time_t(lastseen); puts(""); @@ -557,13 +606,16 @@ void decode_host_header_v3(void) 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]); + printf("MAC address %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + macaddress[0], macaddress[1], macaddress[2], + macaddress[3], macaddress[4], macaddress[5]); } // else hostnamelen = read8u(); print_indentation(); - printf("Hostname length %u\n", hostnamelen); + printf("Hostname length %hhu\n", + hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -571,34 +623,43 @@ void decode_host_header_v3(void) hostname[i] = '\0'; print_indentation(); - printf("Hostname %s\n", hostname); + printf("Hostname %s\n", + hostname); bytesin = read64u(); print_indentation(); - printf("Bytes in %lu\n", bytesin); + printf("Bytes in %lu\n", + bytesin); bytesout = read64u(); print_indentation(); - printf("Bytes out %lu\n", bytesout); + printf("Bytes out %lu\n", + bytesout); if ( (protosdata = read8u()) != 'P') { // missing protos data - fprintf(stderr, "%s:%s:%ld: expecting character P, not %c\n", progname, filename, ftell(file), 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:%s:%ld: expecting character T, not %c\n", progname, filename, ftell(file), 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:%s:%ld: expecting character U, not %c\n", progname, filename, ftell(file), udpdata); + fprintf(stderr, + "%s:%s:%ld: expecting character U, not %c\n", + progname, filename, ftell(file), udpdata); exit(EXIT_FAILURE); } // if @@ -616,7 +677,8 @@ void decode_protos_data(void) ipprotocount = read8u(); print_indentation(); - printf("IP Proto count %u\n", ipprotocount); + printf("IP Proto count %hhu\n", + ipprotocount); indent(); @@ -626,23 +688,27 @@ void decode_protos_data(void) uint64_t out; print_indentation(); - printf("Protocol #%u of %u:\n", i + 1, ipprotocount); + printf("Protocol #%u of %hhu:\n", + i + 1, ipprotocount); proto = read8u(); indent(); print_indentation(); - printf("Protocol 0x%02x\n", proto); + printf("Protocol 0x%02hhx\n", + proto); in = read64u(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -659,7 +725,8 @@ void decode_tcp_data(void) tcpprotocount = read16u(); print_indentation(); - printf("TCP proto count %u\n", tcpprotocount); + printf("TCP proto count %hhu\n", + tcpprotocount); indent(); @@ -672,23 +739,27 @@ void decode_tcp_data(void) port = read16u(); print_indentation(); - printf("Port %u:\n", port); + printf("Port %hu:\n", + port); syn = read64u(); indent(); print_indentation(); - printf("SYN %lu\n", syn); + printf("SYN %lu\n", + syn); in = read64u(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -705,7 +776,8 @@ void decode_udp_data(void) udpprotocount = read16u(); print_indentation(); - printf("UDP proto count %u\n", udpprotocount); + printf("UDP proto count %hhu\n", + udpprotocount); indent(); @@ -717,18 +789,21 @@ void decode_udp_data(void) port = read16u(); print_indentation(); - printf("Port %u:\n", port); + printf("Port %hu:\n", + port); in = read64u(); indent(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -746,7 +821,8 @@ void decode_graph_db_v1(void) indent(); print_indentation(); - printf("Last time 0x%lx = %ld = ", lasttime, lasttime); + printf("Last time 0x%lx = %ld = ", + lasttime, lasttime); print_time_t(lasttime); puts(""); @@ -757,18 +833,21 @@ void decode_graph_db_v1(void) uint32_t j; print_indentation(); - printf("Graph #%u of 4:\n", i + 1); + printf("Graph #%u of 4:\n", + i + 1); nbars = read8u(); indent(); print_indentation(); - printf("Number of bars %u\n", nbars); + printf("Number of bars %hhu\n", + nbars); idxlastbar = read8u(); print_indentation(); - printf("Index of last bar %u\n", idxlastbar); + printf("Index of last bar %hhu\n", + idxlastbar); indent(); for (j = 0; j < idxlastbar; j++) { @@ -776,18 +855,21 @@ void decode_graph_db_v1(void) uint64_t out; print_indentation(); - printf("Bar #%u of %u:\n", j + 1, idxlastbar); + printf("Bar #%u of %hhu:\n", + j + 1, idxlastbar); in = read64u(); indent(); print_indentation(); - printf("In %lu\n", in); + printf("In %lu\n", + in); out = read64u(); print_indentation(); - printf("Out %lu\n", out); + printf("Out %lu\n", + out); exdent(); } // for @@ -946,12 +1028,17 @@ void handle_file_error(void) int saved_errno = errno; if (feof(file) != 0) { - fprintf(stderr, "%s:%s:%ld: premature end-of-file\n", progname, filename, ftell(file)); + 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:%ld: file error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_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() @@ -994,7 +1081,10 @@ void print_time_t(time_t t) if (strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S%z", stm) == 0) { int saved_errno = errno; - fprintf(stderr, "%s:%s:%ld: strftime() error, errno = %s (%d)\n", progname, filename, ftell(file), strerror(saved_errno), saved_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