From 7efd5cbce5d87dd0b9009fb408d0d02812f65188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=88=D0=B5=20=D0=98=D0=BC=D1=8F?= Date: Tue, 7 Oct 2025 18:42:05 +0400 Subject: [PATCH] feat(scanner): add subnet ping scan and TCP port scans for alive hosts Introduce subnet scanning using CIDR prefix notation and perform TCP port scans on each discovered alive host. The change replaces the full port scan with a two-step process: first pinging the subnet to find active devices, then scanning their open TCP ports. --- src/main.cpp | 59 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5189aec..2cd3b05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,8 @@ -#include "cli_base.hpp" +#include "local_ping_scanner.hpp" #include "logger.hpp" #include "port_scanner.hpp" #include "portscan_cli.hpp" - #include -#include - -static std::vector allPorts() -{ - std::vector v; - v.reserve(65535); - for (uint32_t p = 1; p <= 65535; ++p) { - v.push_back(static_cast(p)); - } - return v; -} int main(int argc, char **argv) { @@ -27,12 +15,47 @@ int main(int argc, char **argv) return 0; } - const std::string ip = cli.getIp(); - auto ports = allPorts(); + const std::string baseIp = cli.getBaseIp(); + + const int prefix = cli.getPrefix(); + if (prefix < 0 || prefix > 32) { + Logger::fatal("Prefix must be in range 0..32\n"); + return 2; + } + + LocalPingScanner scanner(baseIp, prefix); + Logger::info(std::format("Scanning subnet: {}/{}", baseIp, prefix)); - Logger::info("Starting full port scan (1..65535)..."); - PortScanner scanner(ip, ports); scanner.run(); - Logger::info("Scan completed."); + + const auto &alive = scanner.alive(); + if (alive.empty()) { + Logger::info("No alive hosts found.\n"); + return 0; + } + + std::cout << "Alive hosts:\n"; + for (const auto &ip : alive) { + std::cout << " " << ip << "\n"; + } + + // For each alive host, run full TCP port scan + for (const auto &ip : alive) { + std::cout << "Scanning ports on " << ip << " ...\n"; + + PortScanner ps(ip); + ps.run(); + + const auto &open = ps.openPorts(); + if (open.empty()) { + std::cout << " No open TCP ports found on " << ip << "\n"; + } else { + std::cout << " Open ports on " << ip << ":\n"; + for (uint16_t port : open) { + std::cout << " " << port << "\n"; + } + } + } + return 0; }