/[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 7 by trond, 2014-03-19T15:40:13Z
# 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 353  void decode_host_header_v2(void) Line 359  void decode_host_header_v2(void)
359    unsigned char udpdata;    unsigned char udpdata;
360    
361    unsigned char i;    unsigned char i;
362    
363    indent();    indent();
364    
365    ipv4address[0] = read8u();    ipv4address[0] = read8u();
366    ipv4address[1] = read8u();    ipv4address[1] = read8u();
367    ipv4address[2] = read8u();    ipv4address[2] = read8u();
368    ipv4address[3] = read8u();    ipv4address[3] = read8u();
369    
370    print_indentation();    print_indentation();
371    printf("IPv4 address %d.%d.%d.%d\n",    printf("IPv4 address %d.%d.%d.%d\n",
372           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);           ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]);
373    
374    #if FOLLOW_SPECIFICATION == 1
375    macaddress[0] = read8u();    macaddress[0] = read8u();
376    macaddress[1] = read8u();    macaddress[1] = read8u();
377    macaddress[2] = read8u();    macaddress[2] = read8u();
378    macaddress[3] = read8u();    macaddress[3] = read8u();
379    macaddress[4] = read8u();    macaddress[4] = read8u();
380    macaddress[5] = read8u();    macaddress[5] = read8u();
381    
382    print_indentation();    print_indentation();
383    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]);
384    
385    lastseen = read64s();    lastseen = read64s();
386    
387    print_indentation();    print_indentation();
388    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
389    print_time_t(lastseen);    print_time_t(lastseen);
390    puts("");    puts("");
391    #else
392      lastseen = read64s();
393    
394      print_indentation();
395      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
396      print_time_t(lastseen);
397      puts("");
398    
399      macaddress[0] = read8u();
400      macaddress[1] = read8u();
401      macaddress[2] = read8u();
402      macaddress[3] = read8u();
403      macaddress[4] = read8u();
404      macaddress[5] = read8u();
405    
406      print_indentation();
407      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
408    #endif
409    
410    hostnamelen = read8u();    hostnamelen = read8u();
411    
412    print_indentation();    print_indentation();
413    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
414    
415    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
416      hostname[i] = read8u();      hostname[i] = read8u();
417    } // for    } // for
418    hostname[i] = '\0';    hostname[i] = '\0';
419    
420    print_indentation();    print_indentation();
421    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
422    
423    bytesin = read64u();    bytesin = read64u();
424    
# Line 494  void decode_host_header_v3(void) Line 519  void decode_host_header_v3(void)
519             ipv6address[ 7],             ipv6address[ 7],
520             ipv6address[ 8],             ipv6address[ 8],
521             ipv6address[ 9],             ipv6address[ 9],
522             ipv6address[10],             ipv6address[10],
523             ipv6address[11],             ipv6address[11],
524             ipv6address[12],             ipv6address[12],
525             ipv6address[13],             ipv6address[13],
526             ipv6address[14],             ipv6address[14],
527             ipv6address[15]);             ipv6address[15]);
528    } // else if    } // else if
529    else { // unknown address family    else { // unknown address family
530      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);
531      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
532    } // else    } // else
533    
534    #if FOLLOW_SPECIFICATION == 1
535    macaddress[0] = read8u();    macaddress[0] = read8u();
536    macaddress[1] = read8u();    macaddress[1] = read8u();
537    macaddress[2] = read8u();    macaddress[2] = read8u();
538    macaddress[3] = read8u();    macaddress[3] = read8u();
539    macaddress[4] = read8u();    macaddress[4] = read8u();
540    macaddress[5] = read8u();    macaddress[5] = read8u();
541    
542    print_indentation();    print_indentation();
543    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]);
544    
545    lastseen = read64s();    lastseen = read64s();
546    
547    print_indentation();    print_indentation();
548    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);    printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
549    print_time_t(lastseen);    print_time_t(lastseen);
550    puts("");    puts("");
551    #else
552      lastseen = read64s();
553    
554      print_indentation();
555      printf("Last seen 0x%lx = %ld = ", lastseen, lastseen);
556      print_time_t(lastseen);
557      puts("");
558    
559      macaddress[0] = read8u();
560      macaddress[1] = read8u();
561      macaddress[2] = read8u();
562      macaddress[3] = read8u();
563      macaddress[4] = read8u();
564      macaddress[5] = read8u();
565    
566      print_indentation();
567      printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", macaddress[0], macaddress[1], macaddress[2], macaddress[3], macaddress[4], macaddress[5]);
568    #endif
569    
570    hostnamelen = read8u();    hostnamelen = read8u();
571    
572    print_indentation();    print_indentation();
573    printf("Hostname length %d\n", hostnamelen);    printf("Hostname length %d\n", hostnamelen);
574    
575    for (i = 0; i < hostnamelen; i++) {    for (i = 0; i < hostnamelen; i++) {
576      hostname[i] = read8u();      hostname[i] = read8u();
577    } // for    } // for
578    hostname[i] = '\0';    hostname[i] = '\0';
579    
580    print_indentation();    print_indentation();
581    printf("Hostname %s\n", hostname);    printf("Hostname %s\n", hostname);
582    
583    bytesin = read64u();    bytesin = read64u();
584    
# Line 860  unsigned long read64u(void) Line 904  unsigned long read64u(void)
904    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
905      handle_file_error();      handle_file_error();
906    } // if    } // if
907    
908  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
909    v = ntohl(v);    v = ntohl(v);
910  #endif  #endif
911    
912    return v;    return v;
913  } // read32s()  } // read32s()
914    
915  unsigned long read64u(void)  unsigned long read64u(void)
916  {  {
917    size_t r;    size_t r;
918    unsigned long v;    unsigned long v;
   unsigned int *p = (unsigned int *)&v;  
919    
920    #ifdef __LITTLE_ENDIAN__
921      unsigned long tmp;
922      unsigned int *p1 = (unsigned int *)&v;
923      unsigned int *p2 = (unsigned int *)&tmp;
924    #endif
925    
926    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(unsigned long) = %ld\n", progname, sizeof(v));
927    
928    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
929      handle_file_error();      handle_file_error();
930    } // if    } // if
931    
932  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
933    p[0] = ntohl((unsigned int)p[0]);    p2[1] = ntohl(p1[0]);
934    p[1] = ntohl((unsigned int)p[1]);    p2[0] = ntohl(p1[1]);
935      v = tmp;
936  #endif  #endif
937    
938    return v;    return v;
939  } // read64u()  } // read64u()
940    
941  signed long read64s(void)  signed long read64s(void)
942  {  {
943    size_t r;    size_t r;
944    signed long v;    signed long v;
   signed int *p = (signed int *)&v;  
945    
946    #ifdef __LITTLE_ENDIAN__
947      signed long tmp;
948      signed int *p1 = (signed int *)&v;
949      signed int *p2 = (signed int *)&tmp;
950    #endif
951    
952    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));    //fprintf(stderr, "%s: sizeof(signed long) = %ld\n", progname, sizeof(v));
953    
954    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {    if ( (r = fread((void *)&v, sizeof(v), 1, file)) != 1) {
955      handle_file_error();      handle_file_error();
956    } // if    } // if
957    
958  #ifdef __LITTLE_ENDIAN__  #ifdef __LITTLE_ENDIAN__
959    p[0] = ntohl((signed int)p[0]);    p2[1] = ntohl(p1[0]);
960    p[1] = ntohl((signed int)p[1]);    p2[0] = ntohl(p1[1]);
961      v = tmp;
962  #endif  #endif
963    
964    return v;    return v;
965  } // read64s()  } // read64s()
966    
967  void handle_file_error(void)  void handle_file_error(void)
968  {  {
969    if (feof(file) != 0) {    if (feof(file) != 0) {
970      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);      fprintf(stderr, "%s:%s: premature end-of-file\n", progname, filename);
971      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
972    } // if    } // if
973    
974    if (ferror(file) != 0) {    if (ferror(file) != 0) {
975      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);
976      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