From 6e6644537911697424f92a790344f648378debaa Mon Sep 17 00:00:00 2001
From: Robert David Graham <robert_david_graham@yahoo.com>
Date: Sat, 5 May 2018 17:04:46 -0400
Subject: [PATCH] --top-ports

---
 src/main-conf.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/masscan.h   |  6 ++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/src/main-conf.c b/src/main-conf.c
index fa2980f..ba939ad 100644
--- a/src/main-conf.c
+++ b/src/main-conf.c
@@ -986,6 +986,20 @@ config_top_ports(struct Masscan *masscan, unsigned n)
     }
 }
 
+int
+isInteger(const char *value)
+{
+    size_t i;
+    
+    if (value == NULL)
+        return 0;
+    
+    for (i=0; value[i]; i++)
+        if (!isdigit(value[i]&0xFF))
+            return 0;
+    return 1;
+}
+
 /***************************************************************************
  * Called either from the "command-line" parser when it sees a --parm,
  * or from the "config-file" parser for normal options.
@@ -1798,7 +1812,10 @@ masscan_set_parameter(struct Masscan *masscan,
         exit(1);
     } else if (EQUALS("top-ports", name)) {
         unsigned n = (unsigned)parseInt(value);
-        config_top_ports(masscan, n);
+        if (!isInteger(value))
+            n = 100;
+        LOG(2, "top-ports = %u\n", n);
+        masscan->top_ports = n;
     } else if (EQUALS("traceroute", name)) {
         fprintf(stderr, "nmap(%s): unsupported\n", name);
         exit(1);
@@ -1934,6 +1951,21 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
         if (argv[i][0] == '-' && argv[i][1] == '-') {
             if (strcmp(argv[i], "--help") == 0) {
                 masscan_help();
+            } else if (EQUALS("top-ports", argv[i]+2)) {
+                /* special handling here since the following parameter
+                 * is optional */
+                const char *value = "1000";
+                unsigned n;
+                
+                /* Only consume the next parameter if it's a number,
+                 * otherwise default to 10000 */
+                if (i+1 < argc && isInteger(argv[i+1])) {
+                    value = argv[++i];
+                }
+                n = (unsigned)parseInt(value);
+                LOG(2, "top-ports = %u\n", n);
+                masscan->top_ports = n;
+               
             } else if (EQUALS("readscan", argv[i]+2)) {
                 /* Read in a binary file instead of scanning the network*/
                 masscan->op = Operation_ReadScan;
@@ -2269,6 +2301,21 @@ masscan_command_line(struct Masscan *masscan, int argc, char *argv[])
          */
         masscan_set_parameter(masscan, "range", argv[i]);
     }
+    
+    /*
+     * If no other "scan type" found, then default to TCP
+     */
+    if (masscan->scan_type.udp == 0 && masscan->scan_type.sctp == 0
+        && masscan->scan_type.ping == 0 && masscan->scan_type.arp == 0)
+        masscan->scan_type.tcp = 1;
+    
+    /*
+     * If "top-ports" specified, then add all those ports. This may be in
+     * addition to any other ports
+     */
+    if (masscan->top_ports) {
+        config_top_ports(masscan, masscan->top_ports);
+    }
 }
 
 /***************************************************************************
diff --git a/src/masscan.h b/src/masscan.h
index 234388e..e9554ee 100644
--- a/src/masscan.h
+++ b/src/masscan.h
@@ -101,7 +101,13 @@ struct Masscan
         unsigned udp:1;
         unsigned sctp:1;
         unsigned ping:1;
+        unsigned arp:1; /*TODO*/
     } scan_type;
+    
+    /**
+     * After scan type has been configured, add these ports
+     */
+    unsigned top_ports;
 
     /**
      * One or more network adapters that we'll use for scanning. Each adapter
-- 
GitLab