Blob Blame History Raw
diff -up nmap-5.51/nmap_dns.cc.tcpdns nmap-5.51/nmap_dns.cc
--- a/nmap_dns.cc	2011-01-21 01:04:16.000000000 +0100
+++ b/nmap_dns.cc	2016-10-26 11:04:49.057723356 +0200
@@ -677,9 +677,9 @@ static void read_evt_handler(nsock_pool
 
   packet_id = buf[1] + (buf[0] << 8);
 
-  // Check that this is a response, standard query, and that no truncation was performed
+  // Check that this is a response, standard query
   // 0xFA == 11111010 (we're not concerned with AA or RD bits)
-  if ((buf[2] & 0xFA) != 0x80) return;
+  if ((buf[2] & 0xF8) != 0x80) return;
 
   // Check that the zero field is all zeros and there is no error condition.
   // We don't care if recursion is available or not since we might be querying
@@ -715,7 +715,8 @@ static void read_evt_handler(nsock_pool
     return;
   }
 
-  if (queries <= 0 || answers <= 0) return;
+  bool processing_successful = false;
+//   if (queries <= 0 || answers <= 0) return;
 
   curbuf = 12;
 
@@ -732,7 +733,7 @@ static void read_evt_handler(nsock_pool
 
   // We're now at the ANSWER section
 
-  for (i=0; i<answers; i++) {
+  for (i=0; i<answers && !processing_successful; i++) {
     curbuf = advance_past_dns_name(buf, buflen, curbuf, &nameloc);
     if (curbuf == -1) return;
 
@@ -758,7 +759,7 @@ static void read_evt_handler(nsock_pool
 
       if (encoded_name_to_normal(buf+nameloc, outbuf, sizeof(outbuf)) == -1) return;
 
-      if (process_result(ia.s_addr, outbuf, ACTION_FINISHED, packet_id)) {
+      if ((processing_successful = process_result(ia.s_addr, outbuf, ACTION_FINISHED, packet_id))) {
         if (o.debugging >= TRACE_DEBUG_LEVEL) log_write(LOG_STDOUT, "mass_rdns: OK MATCHED <%s> to <%s>\n", inet_ntoa(ia), outbuf);
         output_summary();
         stat_ok++;
@@ -771,15 +772,26 @@ static void read_evt_handler(nsock_pool
       if (ia.s_addr == 0) return;
 
       if (o.debugging >= TRACE_DEBUG_LEVEL) log_write(LOG_STDOUT, "mass_rdns: CNAME found for <%s>\n", inet_ntoa(ia));
-      process_result(ia.s_addr, NULL, ACTION_CNAME_LIST, packet_id);
+      processing_successful = process_result(ia.s_addr, NULL, ACTION_SYSTEM_RESOLVE, packet_id);
     } else {
       if (rdlen < 0 || rdlen + curbuf >= buflen) return;
       curbuf += rdlen;
     }
 
-    if (curbuf >= buflen) return;
+    if (curbuf >= buflen) break;//return;
   }
 
+  if (!processing_successful) {
+    if ((buf[2] & 0x02)) { //truncated
+      u32 discard = 0;
+      process_result(discard, NULL, ACTION_SYSTEM_RESOLVE, packet_id);
+    }
+    else {
+      if (o.debugging >= TRACE_DEBUG_LEVEL) {
+        log_write(LOG_STDOUT, "mass_rdns: Unable to process the response\n");
+      }
+    }
+  }
 }
 
 
@@ -1225,20 +1237,20 @@ static void nmap_mass_rdns_core(Target *
 
   nsp_delete(dnspool);
 
-  if (cname_reqs.size() && o.debugging)
-    log_write(LOG_STDOUT, "Performing system-dns for %d domain names that use CNAMEs\n", (int) cname_reqs.size());
+  if (deferred_reqs.size() && o.debugging)
+    log_write(LOG_STDOUT, "Performing system-dns for %d domain names that were deferred\n", (int) deferred_reqs.size());
 
-  if (cname_reqs.size()) {
-    Snprintf(spmobuf, sizeof(spmobuf), "System CNAME DNS resolution of %u host%s.", (unsigned) cname_reqs.size(), cname_reqs.size()-1 ? "s" : "");
+  if (deferred_reqs.size()) {
+    Snprintf(spmobuf, sizeof(spmobuf), "System DNS resolution of %u host%s.", (unsigned) deferred_reqs.size(), deferred_reqs.size()-1 ? "s" : "");
     SPM = new ScanProgressMeter(spmobuf);
 
-    for(i=0, reqI = cname_reqs.begin(); reqI != cname_reqs.end(); reqI++, i++) {
+    for(i=0, reqI = deferred_reqs.begin(); reqI != deferred_reqs.end(); reqI++, i++) {
       struct sockaddr_storage ss;
       size_t sslen;
       char hostname[MAXHOSTNAMELEN + 1] = "";
 
       if (keyWasPressed())
-        SPM->printStats((double) i / cname_reqs.size(), NULL);
+        SPM->printStats((double) i / deferred_reqs.size(), NULL);
 
       tpreq = *reqI;
 
diff --git a/nmap_dns.cc b/nmap_dns.cc
index 9a72336..e01230c 100644
--- a/nmap_dns.cc
+++ b/nmap_dns.cc
@@ -309,7 +309,7 @@ struct host_elem {
 
 static std::list<dns_server> servs;
 static std::list<request *> new_reqs;
-static std::list<request *> cname_reqs;
+static std::list<request *> deferred_reqs;
 static int total_reqs;
 static nsock_pool dnspool=NULL;
 
diff --git a/nmap_dns.cc b/nmap_dns.cc
index e01230c..540d39d 100644
--- a/nmap_dns.cc
+++ b/nmap_dns.cc
@@ -327,12 +327,13 @@ static ScanProgressMeter *SPM;
 
 //------------------- Prototypes and macros ---------------------
 
+static void read_evt_handler(nsock_pool, nsock_event, void *);
 static void put_dns_packet_on_wire(request *req);
 static const char *lookup_etchosts(u32 ip);
 static void addto_etchosts(u32 ip, const char *hname);
 
 #define ACTION_FINISHED 0
-#define ACTION_CNAME_LIST 1
+#define ACTION_SYSTEM_RESOLVE 1
 #define ACTION_TIMEOUT 2
 
 //------------------- Misc code ---------------------
diff --git a/nmap_dns.cc b/nmap_dns.cc
index 540d39d..e0c6f6c 100644
--- a/nmap_dns.cc
+++ b/nmap_dns.cc
@@ -1240,7 +1240,7 @@ static void nmap_mass_rdns_core(Target **targets, int num_targets) {
 
   connect_dns_servers();
 
-  cname_reqs.clear();
+  deferred_reqs.clear();
 
   read_timeout_index = MIN(sizeof(read_timeouts)/sizeof(read_timeouts[0]), servs.size()) - 1;
 
diff --git a/nmap_dns.cc b/nmap_dns.cc
index 9363614..815b3a4 100644
--- a/nmap_dns.cc
+++ b/nmap_dns.cc
@@ -558,7 +558,7 @@ static int process_result(u32 ia, char *result, int action, u16 id) {
         if (ia != 0 && tpreq->targ->v4host().s_addr != ia)
           continue;
 
-        if (action == ACTION_CNAME_LIST || action == ACTION_FINISHED) {
+        if (action == ACTION_SYSTEM_RESOLVE || action == ACTION_FINISHED) {
         servI->capacity += CAPACITY_UP_STEP;
         check_capacities(&*servI);
 
@@ -572,7 +572,7 @@ static int process_result(u32 ia, char *result, int action, u16 id) {
 
         total_reqs--;
 
-          if (action == ACTION_CNAME_LIST) cname_reqs.push_back(tpreq);
+          if (action == ACTION_SYSTEM_RESOLVE) deferred_reqs.push_back(tpreq);
           if (action == ACTION_FINISHED) delete tpreq;
         } else {
           memcpy(&tpreq->timeout, nsock_gettimeofday(), sizeof(struct timeval));
diff --git a/nmap_dns.cc b/nmap_dns.cc
index 815b3a4..f00ac18 100644
--- a/nmap_dns.cc
+++ b/nmap_dns.cc
@@ -1302,7 +1302,7 @@ static void nmap_mass_rdns_core(Target **targets, int num_targets) {
     delete SPM;
   }
 
-  cname_reqs.clear();
+  deferred_reqs.clear();
 
 }