/[darkstattype]/trunk/darkstattype.c
ViewVC logotype

Diff of /trunk/darkstattype.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 6 by trond, 2014-03-19T14:02:41Z Revision 8 by trond, 2014-03-20T09:00:33Z
# Line 17  Line 17 
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    
32    // Define FOLLOW_SPECIFICATION to 0 (zero) to go with darkstat's own format as of 3.0.718.
33    #define FOLLOW_SPECIFICATION 0
34    
35    // Define FOLLOW_SPECIFICATION to 1 (one) to go with the format specified in the export-format.txt file.
36    //#define FOLLOW_SPECIFICATION 1
37    
38  #include <errno.h>  #include <errno.h>
39  #include <stdio.h>  #include <stdio.h>
40  #include <stdlib.h>  #include <stdlib.h>
41  #include <string.h>  #include <string.h>
42  #include <time.h>  #include <time.h>
43  #include <unistd.h>  #include <unistd.h>
44    
45  const char *progname = NULL;  const char *progname = NULL;
46  const char *filename = NULL;  const char *filename = NULL;
47  FILE *file = NULL;  FILE *file = NULL;
48    
49  int main(int argc, char **argv)  int main(int argc, char **argv)
50  {  {
51    void show_usage(int exitcode);    void show_usage(int exitcode);
52    void show_version(void);    void show_version(void);
# Line 76  void show_usage(int exitcode) Line 82  void show_usage(int exitcode)
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 [-h] [-v] filename\n"
97      " E.g.: %s darkstat.db\n",      " E.g.: %s darkstat.db\n\n"
98    
99        "Options:\n"
100        "-h\tShow this help message and exit.\n"
101        "-v\tShow version and copyright and exit.\n",
102      progname, progname);      progname, progname);
103    
104    exit(exitcode);    exit(exitcode);
105  } // show_usage()  } // show_usage()
106    
107  void show_version(void)  void show_version(void)
108  {  {
109    puts("darkstattype 1.0");    puts("darkstattype 1.0");
110    puts("$Ximalas$");    puts("$Ximalas$");
111    puts("");    puts("");
112    
113    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");    puts("Copyright © 2014, Trond Endrestøl <Trond.Endrestol@ximalas.info>");
114    puts("All rights reserved.");    puts("All rights reserved.");
115    puts("");    puts("");
116    puts("Redistribution and use in source and binary forms, with or without");    puts("Redistribution and use in source and binary forms, with or without");
# Line 353  void decode_host_header_v2(void) Line 363  void decode_host_header_v2(void)
363    unsigned char udpdata;    unsigned char udpdata;
364    
365    unsigned char i;    unsigned char i;
366    
367    indent();    indent();
368    
369    ipv4address[0] = read8u();    ipv4address[0] = read8u();
370    ipv4address[1] = read8u();    ipv4address[1] = read8u();
371    ipv4address[2] = read8u();    ipv4address[2] = read8u();
372    ipv4address[3] = read8u();    ipv4address[3] = read8u();
373    
374    print_indentation();    print_indentation();
375    printf("IPv4 address %d.%d.%d.%d\n",    printf("IPv4 address %d.%d.%d.%d\n",
376           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
377    
378    #if FOLLOW_SPECIFICATION == 1
379    macaddress[0] = read8u();    macaddress[0] = read8u();
380    macaddress[1] = read8u();    macaddress[1] = read8u();
381    macaddress[2] = read8u();    macaddress[2] = read8u();
382    macaddress[3] = read8u();    macaddress[3] = read8u();
383    macaddress[4] = read8u();    macaddress[4] = read8u();
384    macaddress[5] = read8u();    macaddress[5] = read8u();
385    
386    print_indentation();    print_indentation();
387    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]);
388    
389    lastseen = read64s();    lastseen = read64s();
390    
391    print_indentation();    print_indentation();
392    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
393    print_time_t(lastseen);    print_time_t(lastseen);
394    puts("");    puts("");
395    #else
396      lastseen = read64s();
397    
398      print_indentation();
399      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
400      print_time_t(lastseen);
401      puts("");
402    
403      macaddress[0] = read8u();
404      macaddress[1] = read8u();
405      macaddress[2] = read8u();
406      macaddress[3] = read8u();
407      macaddress[4] = read8u();
408      macaddress[5] = read8u();
409    
410      print_indentation();
411      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
412    #endif
413    
414    hostnamelen = read8u();    hostnamelen = read8u();
415    
416    print_indentation();    print_indentation();
417    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
418    
419    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
420      hostname[i] = read8u();      hostname[i] = read8u();
421    } // for    } // for
422    hostname[i] = '\0';    hostname[i] = '\0';
423    
424    print_indentation();    print_indentation();
425    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
426    
427    bytesin = read64u();    bytesin = read64u();
428    
# Line 494  void decode_host_header_v3(void) Line 523  void decode_host_header_v3(void)
523             ipv6address[ 7],             ipv6address[ 7],
524             ipv6address[ 8],             ipv6address[ 8],
525             ipv6address[ 9],             ipv6address[ 9],
526             ipv6address[10],             ipv6address[10],
527             ipv6address[11],             ipv6address[11],
528             ipv6address[12],             ipv6address[12],
529             ipv6address[13],             ipv6address[13],
530             ipv6address[14],             ipv6address[14],
531             ipv6address[15]);             ipv6address[15]);
532    } // else if    } // else if
533    else { // unknown address family    else { // unknown address family
534      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);
535      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
536    } // else    } // else
537    
538    #if FOLLOW_SPECIFICATION == 1
539    macaddress[0] = read8u();    macaddress[0] = read8u();
540    macaddress[1] = read8u();    macaddress[1] = read8u();
541    macaddress[2] = read8u();    macaddress[2] = read8u();
542    macaddress[3] = read8u();    macaddress[3] = read8u();
543    macaddress[4] = read8u();    macaddress[4] = read8u();
544    macaddress[5] = read8u();    macaddress[5] = read8u();
545    
546    print_indentation();    print_indentation();
547    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]);
548    
549    lastseen = read64s();    lastseen = read64s();
550    
551    print_indentation();    print_indentation();
552    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
553    print_time_t(lastseen);    print_time_t(lastseen);
554    puts("");    puts("");
555    #else
556      lastseen = read64s();
557    
558      print_indentation();
559      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
560      print_time_t(lastseen);
561      puts("");
562    
563      macaddress[0] = read8u();
564      macaddress[1] = read8u();
565      macaddress[2] = read8u();
566      macaddress[3] = read8u();
567      macaddress[4] = read8u();
568      macaddress[5] = read8u();
569    
570      print_indentation();
571      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
572    #endif
573    
574    hostnamelen = read8u();    hostnamelen = read8u();
575    
576    print_indentation();    print_indentation();
577    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
578    
579    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
580      hostname[i] = read8u();      hostname[i] = read8u();
581    } // for    } // for
582    hostname[i] = '\0';    hostname[i] = '\0';
583    
584    print_indentation();    print_indentation();
585    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
586    
587    bytesin = read64u();    bytesin = read64u();
588    
# Line 860  unsigned long read64u(void) Line 908  unsigned long read64u(void)
908    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
909      handle_file_error();      handle_file_error();
910    } // if    } // if
911    
912  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
913    v = ntohl(v);    v = ntohl(v);
914  #endif  #endif
915    
916    return v;    return v;
917  } // read32s()  } // read32s()
918    
919  unsigned long read64u(void)  unsigned long read64u(void)
920  {  {
921    size_t r;    size_t r;
922    unsigned long v;    unsigned long v;
   unsigned int *p = (unsigned int *)&v;  
923    
924    #ifdef __LITTLE_ENDIAN__
925      unsigned long tmp;
926      unsigned int *p1 = (unsigned int *)&v;
927      unsigned int *p2 = (unsigned int *)&tmp;
928    #endif
929    
930    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));
931    
932    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
933      handle_file_error();      handle_file_error();
934    } // if    } // if
935    
936  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
937    p[0] = ntohl((unsigned int)p[0]);    p2[1] = ntohl(p1[0]);
938    p[1] = ntohl((unsigned int)p[1]);    p2[0] = ntohl(p1[1]);
939      v = tmp;
940  #endif  #endif
941    
942    return v;    return v;
943  } // read64u()  } // read64u()
944    
945  signed long read64s(void)  signed long read64s(void)
946  {  {
947    size_t r;    size_t r;
948    signed long v;    signed long v;
   signed int *p = (signed int *)&v;  
949    
950    #ifdef __LITTLE_ENDIAN__
951      signed long tmp;
952      signed int *p1 = (signed int *)&v;
953      signed int *p2 = (signed int *)&tmp;
954    #endif
955    
956    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));
957    
958    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
959      handle_file_error();      handle_file_error();
960    } // if    } // if
961    
962  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
963    p[0] = ntohl((signed int)p[0]);    p2[1] = ntohl(p1[0]);
964    p[1] = ntohl((signed int)p[1]);    p2[0] = ntohl(p1[1]);
965      v = tmp;
966  #endif  #endif
967    
968    return v;    return v;
969  } // read64s()  } // read64s()
970    
971  void handle_file_error(void)  void handle_file_error(void)
972  {  {
973    if (feof(file) != 0) {    if (feof(file) != 0) {
974      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);
975      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
976    } // if    } // if
977    
978    if (ferror(file) != 0) {    if (ferror(file) != 0) {
979      fprintf(stderr, "%s:%s: file error, errno = %s (%d)\n", progname, filename, strerror(errno), errno);      fprintf(stderr, "%s:%s: file error, errno = %s (%d)\n", progname, filename, strerror(errno), errno);
980      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

svn@ximalas.info
ViewVC Help
Powered by ViewVC 1.3.0-dev