]> git.sesse.net Git - nbtscanner/blobdiff - nbtscanner.c
Import nbtscanner 0.2.0.
[nbtscanner] / nbtscanner.c
index a7c5e25859f33a879c6ee4bd3cefa543ed43c4e0..ab454180682ffc9f9356a2f8bf8b3157c90fa6fb 100644 (file)
@@ -51,6 +51,7 @@ void interpret_node_status(char *p, struct in_addr in)
 {
        int numnames = CVAL(p,0);
        char hostname[32], fileservername[32], group[32];
+       char messenger;
 
        /*
         * hopefully this will be enough to identify the host as unknown
@@ -60,6 +61,7 @@ void interpret_node_status(char *p, struct in_addr in)
        strcpy(hostname, "-unknown-nbtscanner-");
        strcpy(fileservername, "-unknown-nbtscanner-");
        strcpy(group, "-unknown-nbtscanner-");
+       messenger = 'n';
 
        p += 1;
        while (numnames--) {
@@ -100,14 +102,18 @@ void interpret_node_status(char *p, struct in_addr in)
                if ((p[0] & 0x80) == 0x80 && ((p[0] & 0x1f) == 0x04) && type == 0x00) {
                        strcpy(group, qname);
                }
+               /* Messenger up (type 0x03) */
+               if (type == 0x03) {
+                       messenger = 'y';
+               }
 
                p += 2;
        }
 
        if (use_mysql) {
-               add_record_mysql(inet_ntoa(in), hostname, fileservername, group);
+               add_record_mysql(inet_ntoa(in), hostname, fileservername, group, messenger);
        } else {
-               printf("%s,%s,%s,%s\n", inet_ntoa(in), hostname, fileservername, group);
+               printf("%s,%s,%s,%s,%c\n", inet_ntoa(in), hostname, fileservername, group, messenger);
        }
 }
 
@@ -299,40 +305,58 @@ void scan_range(struct in_addr ip, int rangesize)
 int main(int argc, char *argv[])
 {
        int fd = open_sockets();
+       int i;
 
-       id_list_init();
-       init_stats();
-
-       /* note that parse_configfile() will call scan_range()! */
        server_fd = fd;
-       parse_configfile(1);
+       parse_configfile();
 
-/*     for (i=1;i<argc;i++) {
-               struct in_addr ip;
+       if (use_mysql) {
+               init_mysql(mysql_host, mysql_username, mysql_password);
+       }
 
-               ip.s_addr = inet_addr(argv[i]); //interpret_addr2(argv[i]);
-               send_nbt_packet(fd, ip);
-               recv_nbt_packets(fd);
-       } */
+       for ( ;; ) {
+               struct timeval start, now;
+               unsigned int delay_ms;
 
-       /*
-        * 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);
-       }
+               id_list_init();
+               init_stats();
 
-       if (use_mysql) {
-               finish_mysql();
-       }
+               gettimeofday(&start, NULL);
 
-       if (verbosity >= 3) {
-               print_stats(1);
-               fprintf(stderr, "\n");
-       }
+               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);
+               }
+
+               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;
 }