]> git.sesse.net Git - nbtscanner/blobdiff - nbtscanner.c
Import nbtscanner 0.1.2.
[nbtscanner] / nbtscanner.c
index cbc894527014a6924d46e65b6cfb57cf2b2c5b49..223965cecacd61994ebe507dc3311dc526803554 100644 (file)
@@ -32,7 +32,6 @@
 #include <unistd.h>
 #include <arpa/inet.h>
 #include <stdlib.h>
-#include <malloc.h>
 
 #include "nbtscanner.h"
 #include "mysql_interface.h"
@@ -299,43 +298,59 @@ void scan_range(struct in_addr ip, int rangesize)
 
 int main(int argc, char *argv[])
 {
-       int junk = mtrace();
        int fd = open_sockets();
        int i;
 
-       id_list_init();
-       init_stats();
-
        server_fd = fd;
        parse_configfile();
 
-       /* really ugly to have here */
        if (use_mysql) {
                init_mysql(mysql_host, mysql_username, mysql_password);
        }
 
-       for (i = 0; i < ranges; i++) {
-               scan_range(scanrange[i], scanrangesize[i]);
-       }
+       for ( ;; ) {
+               struct timeval start, now;
+               unsigned int delay_ms;
+
+               id_list_init();
+               init_stats();
+
+               gettimeofday(&start, NULL);
+
+               for (i = 0; i < ranges; i++) {
+                       scan_range(scanrange[i], scanrangesize[i]);
+               }
        
-       /*
-        * receive answers and resend packets until all answers have
-        * been received, or all packets have been timed out
-        */
-       status = RETRYING;
-       while (get_num_free_ids() != 65536) {
-               recv_nbt_packets(fd);
-               resend_timed_out_packets(fd);
-       }
+               /*
+                * receive answers and resend packets until all answers have
+                * been received, or all packets have been timed out
+                */
+               status = RETRYING;
+               while (get_num_free_ids() != 65536) {
+                       recv_nbt_packets(fd);
+                       resend_timed_out_packets(fd);
+               }
 
-       if (use_mysql) {
-               finish_mysql();
-       }
+               if (verbosity >= 3) {
+                       print_stats(1);
+                       fprintf(stderr, "\n");
+               }
 
-       if (verbosity >= 3) {
-               print_stats(1);
-               fprintf(stderr, "\n");
-       }
+               id_list_destroy();
 
+               gettimeofday(&now, NULL);
+
+               delay_ms = scan_interval - mydifftime(start, now);
+               if (scan_wait > delay_ms) delay_ms = scan_wait;
+
+               return 0;
+
+               if (verbosity >= 3) {
+                       fprintf(stderr, "Scan took %d ms, waiting %d ms for next scan...\r",
+                               mydifftime(start, now), delay_ms);
+                       usleep(delay_ms * 1000);
+               }
+       }
+       /* finish_mysql(); */
        return 0;
 }