--- trunk/darkstattype.c 2014/03/20 09:49:12 9 +++ trunk/darkstattype.c 2014/03/21 18:35:50 14 @@ -78,22 +78,26 @@ int main(int argc, char **argv) argc -= optind; argv += optind; - if (argv[0] == NULL) { + if (*argv == NULL) { fprintf(stderr, "%s: missing filename\n\n", progname); show_usage(EXIT_FAILURE); } // if - filename = argv[0]; + while (*argv != NULL) { + filename = *argv; - decode_file(); + decode_file(); + argv++; + } // while + return EXIT_SUCCESS; } // main() 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" @@ -178,7 +182,7 @@ void decode_file(void) #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); + 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 @@ -202,12 +206,17 @@ void decode_file(void) 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, 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 exdent(); + + if (fclose(file) != 0) { + fprintf(stderr, "%s: fclose(\"%s\") = %s (%d)\n", progname, filename, strerror(errno), errno); + exit(EXIT_FAILURE); + } // if } // decode_file() void decode_host_db_v1(void) @@ -254,7 +263,7 @@ void decode_host_db_v1(void) 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, 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 @@ -290,7 +299,7 @@ void decode_host_header_v1(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %d.%d.%d.%d\n", + printf("IPv4 address %u.%u.%u.%u\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); macaddress[0] = read8u(); @@ -306,7 +315,7 @@ void decode_host_header_v1(void) hostnamelen = read8u(); print_indentation(); - printf("Hostname length %d\n", hostnamelen); + printf("Hostname length %u\n", hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -327,21 +336,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 @@ -373,7 +382,7 @@ void decode_host_header_v2(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %d.%d.%d.%d\n", + printf("IPv4 address %u.%u.%u.%u\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); if (follow_specification == true) { @@ -416,7 +425,7 @@ void decode_host_header_v2(void) hostnamelen = read8u(); print_indentation(); - printf("Hostname length %d\n", hostnamelen); + printf("Hostname length %u\n", hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -437,21 +446,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 @@ -462,20 +471,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(); @@ -489,51 +498,35 @@ void decode_host_header_v3(void) ipv4address[3] = read8u(); print_indentation(); - printf("IPv4 address %d.%d.%d.%d\n", + printf("IPv4 address %u.%u.%u.%u\n", ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); } // if else if (addressfamily == 0x06) { // IPv6 address 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 @@ -577,7 +570,7 @@ void decode_host_header_v3(void) hostnamelen = read8u(); print_indentation(); - printf("Hostname length %d\n", hostnamelen); + printf("Hostname length %u\n", hostnamelen); for (i = 0; i < hostnamelen; i++) { hostname[i] = read8u(); @@ -598,21 +591,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 @@ -625,22 +618,22 @@ void decode_protos_data(void) { unsigned char ipprotocount; - unsigned char u; + unsigned char i; ipprotocount = read8u(); print_indentation(); - printf("IP Proto count %d\n", ipprotocount); + printf("IP Proto count %u\n", ipprotocount); indent(); - for (u = 0; u < ipprotocount; u++) { + for (i = 0; i < ipprotocount; i++) { unsigned char proto; unsigned long in; unsigned long out; print_indentation(); - printf("Protocol #%u of %u:\n", u + 1, ipprotocount); + printf("Protocol #%u of %u:\n", i + 1, ipprotocount); proto = read8u(); @@ -673,7 +666,7 @@ void decode_tcp_data(void) tcpprotocount = read16u(); print_indentation(); - printf("TCP proto count %d\n", tcpprotocount); + printf("TCP proto count %u\n", tcpprotocount); indent(); @@ -686,7 +679,7 @@ void decode_tcp_data(void) port = read16u(); print_indentation(); - printf("Port %d:\n", port); + printf("Port %u:\n", port); syn = read64u(); @@ -719,7 +712,7 @@ void decode_udp_data(void) udpprotocount = read16u(); print_indentation(); - printf("UDP proto count %d\n", udpprotocount); + printf("UDP proto count %u\n", udpprotocount); indent(); @@ -731,7 +724,7 @@ void decode_udp_data(void) port = read16u(); print_indentation(); - printf("Port %d:\n", port); + printf("Port %u:\n", port); in = read64u(); @@ -771,7 +764,7 @@ void decode_graph_db_v1(void) unsigned int j; print_indentation(); - printf("Graph #%d of 4:\n", i + 1); + printf("Graph #%u of 4:\n", i + 1); nbars = read8u(); @@ -973,13 +966,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() @@ -1018,7 +1013,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