17 |
this list of conditions and the following disclaimer in the documentation |
this list of conditions and the following disclaimer in the documentation |
18 |
and/or other materials provided with the distribution. |
and/or other materials provided with the distribution. |
19 |
|
|
20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
21 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
22 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
23 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
24 |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
25 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
26 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
27 |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 |
*/ |
*/ |
31 |
|
|
|
// 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 |
|
|
|
|
32 |
#include <errno.h> |
#include <errno.h> |
33 |
|
#include <stdbool.h> |
34 |
#include <stdio.h> |
#include <stdio.h> |
35 |
#include <stdlib.h> |
#include <stdlib.h> |
36 |
#include <string.h> |
#include <string.h> |
37 |
#include <time.h> |
#include <time.h> |
38 |
#include <unistd.h> |
#include <unistd.h> |
39 |
|
|
40 |
const char *progname = NULL; |
const char *progname = NULL; |
41 |
const char *filename = NULL; |
const char *filename = NULL; |
42 |
FILE *file = NULL; |
FILE *file = NULL; |
43 |
|
bool follow_specification = false; |
44 |
|
|
45 |
int main(int argc, char **argv) |
int main(int argc, char **argv) |
46 |
{ |
{ |
47 |
void show_usage(int exitcode); |
void show_usage(int exitcode); |
48 |
void show_version(void); |
void show_version(void); |
49 |
void decode_file(void); |
void decode_file(void); |
50 |
|
|
51 |
int i; |
int i; |
52 |
|
|
53 |
progname = argv[0]; |
progname = argv[0]; |
54 |
|
|
55 |
opterr = 0; |
opterr = 0; |
56 |
while ( (i = getopt(argc, argv, "hv")) != -1) { |
while ( (i = getopt(argc, argv, "fhv")) != -1) { |
57 |
switch (i) { |
switch (i) { |
58 |
|
case 'f': |
59 |
|
follow_specification = !follow_specification; |
60 |
|
break; |
61 |
|
|
62 |
case 'h': |
case 'h': |
63 |
show_usage(EXIT_SUCCESS); |
show_usage(EXIT_SUCCESS); |
64 |
break; |
break; |
65 |
|
|
66 |
case 'v': |
case 'v': |
67 |
show_version(); |
show_version(); |
68 |
break; |
break; |
69 |
|
|
70 |
case '?': |
case '?': |
71 |
/*FALLTHROUGH*/ |
/*FALLTHROUGH*/ |
72 |
default: |
default: |
73 |
fprintf(stderr, "%s: illegal option -%c\n\n", progname, optopt); |
fprintf(stderr, "%s: illegal option -%c\n\n", progname, optopt); |
74 |
show_usage(EXIT_FAILURE); |
show_usage(EXIT_FAILURE); |
75 |
break; |
break; |
76 |
} // switch |
} // switch |
81 |
if (argv[0] == NULL) { |
if (argv[0] == NULL) { |
82 |
fprintf(stderr, "%s: missing filename\n\n", progname); |
fprintf(stderr, "%s: missing filename\n\n", progname); |
83 |
show_usage(EXIT_FAILURE); |
show_usage(EXIT_FAILURE); |
84 |
} // if |
} // if |
85 |
|
|
86 |
filename = argv[0]; |
filename = argv[0]; |
87 |
|
|
88 |
decode_file(); |
decode_file(); |
89 |
|
|
90 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
91 |
} // main() |
} // main() |
92 |
|
|
93 |
void show_usage(int exitcode) |
void show_usage(int exitcode) |
94 |
{ |
{ |
95 |
fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr, |
fprintf((exitcode == EXIT_SUCCESS) ? stdout : stderr, |
96 |
"Usage: %s [-h] [-v] filename\n" |
"Usage: %s [-f] [-h] [-v] filename\n" |
97 |
" E.g.: %s darkstat.db\n\n" |
" E.g.: %s darkstat.db\n\n" |
98 |
|
|
99 |
"Options:\n" |
"Options:\n" |
100 |
|
"-f\tToggle follow strict specification, default is false.\n" |
101 |
"-h\tShow this help message and exit.\n" |
"-h\tShow this help message and exit.\n" |
102 |
"-v\tShow version and copyright and exit.\n", |
"-v\tShow version and copyright and exit.\n", |
103 |
progname, progname); |
progname, progname); |
104 |
|
|
105 |
exit(exitcode); |
exit(exitcode); |
106 |
} // show_usage() |
} // show_usage() |
107 |
|
|
108 |
void show_version(void) |
void show_version(void) |
109 |
{ |
{ |
110 |
puts("darkstattype 1.0"); |
puts("darkstattype 1.0"); |
111 |
puts("$Ximalas$"); |
puts("$Ximalas$"); |
112 |
puts(""); |
puts(""); |
113 |
|
|
114 |
puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>"); |
puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>"); |
115 |
puts("All rights reserved."); |
puts("All rights reserved."); |
364 |
unsigned char udpdata; |
unsigned char udpdata; |
365 |
|
|
366 |
unsigned char i; |
unsigned char i; |
367 |
|
|
368 |
indent(); |
indent(); |
369 |
|
|
370 |
ipv4address[0] = read8u(); |
ipv4address[0] = read8u(); |
371 |
ipv4address[1] = read8u(); |
ipv4address[1] = read8u(); |
372 |
ipv4address[2] = read8u(); |
ipv4address[2] = read8u(); |
373 |
ipv4address[3] = read8u(); |
ipv4address[3] = read8u(); |
374 |
|
|
375 |
print_indentation(); |
print_indentation(); |
376 |
printf("IPv4 address %d.%d.%d.%d\n", |
printf("IPv4 address %d.%d.%d.%d\n", |
377 |
ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); |
ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]); |
378 |
|
|
379 |
#if FOLLOW_SPECIFICATION == 1 |
if (follow_specification == true) { |
380 |
macaddress[0] = read8u(); |
macaddress[0] = read8u(); |
381 |
macaddress[1] = read8u(); |
macaddress[1] = read8u(); |
382 |
macaddress[2] = read8u(); |
macaddress[2] = read8u(); |
383 |
macaddress[3] = read8u(); |
macaddress[3] = read8u(); |
384 |
macaddress[4] = read8u(); |
macaddress[4] = read8u(); |
385 |
macaddress[5] = read8u(); |
macaddress[5] = read8u(); |
386 |
|
|
387 |
print_indentation(); |
print_indentation(); |
388 |
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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); |
389 |
|
|
390 |
lastseen = read64s(); |
lastseen = read64s(); |
391 |
|
|
392 |
print_indentation(); |
print_indentation(); |
393 |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
394 |
print_time_t(lastseen); |
print_time_t(lastseen); |
395 |
puts(""); |
puts(""); |
396 |
#else |
} // if |
397 |
lastseen = read64s(); |
else { |
398 |
|
lastseen = read64s(); |
399 |
|
|
400 |
print_indentation(); |
print_indentation(); |
401 |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
402 |
print_time_t(lastseen); |
print_time_t(lastseen); |
403 |
puts(""); |
puts(""); |
404 |
|
|
405 |
macaddress[0] = read8u(); |
macaddress[0] = read8u(); |
406 |
macaddress[1] = read8u(); |
macaddress[1] = read8u(); |
407 |
macaddress[2] = read8u(); |
macaddress[2] = read8u(); |
408 |
macaddress[3] = read8u(); |
macaddress[3] = read8u(); |
409 |
macaddress[4] = read8u(); |
macaddress[4] = read8u(); |
410 |
macaddress[5] = read8u(); |
macaddress[5] = read8u(); |
411 |
|
|
412 |
print_indentation(); |
print_indentation(); |
413 |
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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); |
414 |
#endif |
} // else |
415 |
|
|
416 |
hostnamelen = read8u(); |
hostnamelen = read8u(); |
417 |
|
|
418 |
print_indentation(); |
print_indentation(); |
419 |
printf("Hostname length %d\n", hostnamelen); |
printf("Hostname length %d\n", hostnamelen); |
420 |
|
|
421 |
for (i = 0; i < hostnamelen; i++) { |
for (i = 0; i < hostnamelen; i++) { |
422 |
hostname[i] = read8u(); |
hostname[i] = read8u(); |
423 |
} // for |
} // for |
424 |
hostname[i] = '\0'; |
hostname[i] = '\0'; |
425 |
|
|
426 |
print_indentation(); |
print_indentation(); |
427 |
printf("Hostname %s\n", hostname); |
printf("Hostname %s\n", hostname); |
428 |
|
|
429 |
bytesin = read64u(); |
bytesin = read64u(); |
525 |
ipv6address[ 7], |
ipv6address[ 7], |
526 |
ipv6address[ 8], |
ipv6address[ 8], |
527 |
ipv6address[ 9], |
ipv6address[ 9], |
528 |
ipv6address[10], |
ipv6address[10], |
529 |
ipv6address[11], |
ipv6address[11], |
530 |
ipv6address[12], |
ipv6address[12], |
531 |
ipv6address[13], |
ipv6address[13], |
532 |
ipv6address[14], |
ipv6address[14], |
533 |
ipv6address[15]); |
ipv6address[15]); |
534 |
} // else if |
} // else if |
535 |
else { // unknown address family |
else { // unknown address family |
536 |
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: unknown address family = 0x%x, neither 0x%x nor 0x%x\n", progname, filename, addressfamily, 0x04, 0x06); |
537 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
538 |
} // else |
} // else |
539 |
|
|
540 |
#if FOLLOW_SPECIFICATION == 1 |
if (follow_specification == true) { |
541 |
macaddress[0] = read8u(); |
macaddress[0] = read8u(); |
542 |
macaddress[1] = read8u(); |
macaddress[1] = read8u(); |
543 |
macaddress[2] = read8u(); |
macaddress[2] = read8u(); |
544 |
macaddress[3] = read8u(); |
macaddress[3] = read8u(); |
545 |
macaddress[4] = read8u(); |
macaddress[4] = read8u(); |
546 |
macaddress[5] = read8u(); |
macaddress[5] = read8u(); |
547 |
|
|
548 |
print_indentation(); |
print_indentation(); |
549 |
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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); |
550 |
|
|
551 |
lastseen = read64s(); |
lastseen = read64s(); |
552 |
|
|
553 |
print_indentation(); |
print_indentation(); |
554 |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
555 |
print_time_t(lastseen); |
print_time_t(lastseen); |
556 |
puts(""); |
puts(""); |
557 |
#else |
} // if |
558 |
lastseen = read64s(); |
else { |
559 |
|
lastseen = read64s(); |
560 |
|
|
561 |
print_indentation(); |
print_indentation(); |
562 |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
printf("Last seen 0x%lx = %ld = ", lastseen, lastseen); |
563 |
print_time_t(lastseen); |
print_time_t(lastseen); |
564 |
puts(""); |
puts(""); |
565 |
|
|
566 |
macaddress[0] = read8u(); |
macaddress[0] = read8u(); |
567 |
macaddress[1] = read8u(); |
macaddress[1] = read8u(); |
568 |
macaddress[2] = read8u(); |
macaddress[2] = read8u(); |
569 |
macaddress[3] = read8u(); |
macaddress[3] = read8u(); |
570 |
macaddress[4] = read8u(); |
macaddress[4] = read8u(); |
571 |
macaddress[5] = read8u(); |
macaddress[5] = read8u(); |
572 |
|
|
573 |
print_indentation(); |
print_indentation(); |
574 |
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 %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]); |
575 |
#endif |
} // else |
576 |
|
|
577 |
hostnamelen = read8u(); |
hostnamelen = read8u(); |
578 |
|
|
579 |
print_indentation(); |
print_indentation(); |
580 |
printf("Hostname length %d\n", hostnamelen); |
printf("Hostname length %d\n", hostnamelen); |
581 |
|
|
582 |
for (i = 0; i < hostnamelen; i++) { |
for (i = 0; i < hostnamelen; i++) { |
583 |
hostname[i] = read8u(); |
hostname[i] = read8u(); |
584 |
} // for |
} // for |
585 |
hostname[i] = '\0'; |
hostname[i] = '\0'; |
586 |
|
|
587 |
print_indentation(); |
print_indentation(); |
588 |
printf("Hostname %s\n", hostname); |
printf("Hostname %s\n", hostname); |
589 |
|
|
590 |
bytesin = read64u(); |
bytesin = read64u(); |