diff --git a/modules/ANSUtilities/ANSUtilities.cpp b/modules/ANSUtilities/ANSUtilities.cpp index 0a5c2de..10ee816 100644 --- a/modules/ANSUtilities/ANSUtilities.cpp +++ b/modules/ANSUtilities/ANSUtilities.cpp @@ -1359,6 +1359,13 @@ constexpr int kUdpProbeIntervalMs = 2000; constexpr const wchar_t* kHttpUserAgent = L"ANSUtilities-Scan/1.0"; constexpr const wchar_t* kHttpPath = L"/ServerApi"; +// HTTP ports answered by AIBOX boxes / ANSVIS servers. AIBOX listens on the +// default HTTP port; the ANSVIS LabVIEW web server (App_serverConfig.httpPort +// in ANSVIS.lvproj) listens on 22211. Each host gets probed on both ports. +constexpr unsigned short kPortAibox = 80; +constexpr unsigned short kPortAnsvis = 22211; +constexpr unsigned short kSweepPorts[] = { kPortAibox, kPortAnsvis }; + // ---- Winsock init (refcounted; safe to call repeatedly) ----------------- struct WsaScope { bool ok{false}; @@ -1426,7 +1433,11 @@ std::vector EnumerateLocalSubnets() { } // ---- Parse ":::" reply ---------------------------- -void ParseUdpReply(const std::string& body, std::string& name, std::string& firmware) { +// parts[0] and parts[1] are not used by the AIBOX Flutter app but the device +// still sends them. Observed devices put their listen port in one of those +// two fields, so we try both — first numeric wins. Unparseable -> port = 0. +void ParseUdpReply(const std::string& body, std::string& name, + std::string& firmware, int& port) { std::vector parts; size_t start = 0; for (size_t i = 0; i <= body.size(); ++i) { @@ -1437,6 +1448,16 @@ void ParseUdpReply(const std::string& body, std::string& name, std::string& firm } if (parts.size() > 3) { name = parts[2]; firmware = parts[3]; } else { name = "Unknown"; firmware = "-"; } + + port = 0; + auto tryPort = [&](const std::string& s) { + if (port != 0 || s.empty() || s.size() > 5) return; + for (char c : s) if (c < '0' || c > '9') return; + int v = std::atoi(s.c_str()); + if (v > 0 && v < 65536) port = v; + }; + if (parts.size() > 0) tryPort(parts[0]); + if (parts.size() > 1) tryPort(parts[1]); } // ---- Parse {"OK":true,"Version":"x"} ------------------------------------ @@ -1475,7 +1496,7 @@ struct ScanState { std::atomic stop{false}; void merge(const std::string& ip, const std::string& name, - const std::string& firmware, int srcBit) { + const std::string& firmware, int port, int srcBit) { std::lock_guard lk(mu); auto& d = found[ip]; if (d.ip.empty()) d.ip = ip; @@ -1484,6 +1505,14 @@ struct ScanState { if (!firmware.empty() && firmware != "-" && (d.firmware.empty() || d.firmware == "-")) d.firmware = firmware; else if (!firmware.empty() && d.firmware.empty()) d.firmware = firmware; + // Port precedence: an HTTP probe proves the port actually responds, + // so a sweep-supplied port overwrites whatever UDP parsing guessed. + // A UDP-supplied port is only kept if HTTP hasn't filled it in. + if (port > 0) { + const bool fromHttp = (srcBit == kSrcSweep); + const bool fromHttpAlready = (d.source & kSrcSweep) != 0; + if (fromHttp || !fromHttpAlready) d.port = port; + } d.source |= srcBit; } }; @@ -1540,10 +1569,11 @@ void RunUdpListener(ScanState& state, const std::vector& subnets) { char ipStr[INET_ADDRSTRLEN] = {}; inet_ntop(AF_INET, &from.sin_addr, ipStr, sizeof(ipStr)); std::string name, fw; - ParseUdpReply(body, name, fw); - ANS_DBG("ANSDISC", "UDP reply from %s name=%s fw=%s", - ipStr, name.c_str(), fw.c_str()); - state.merge(ipStr, name, fw, kSrcMulticast); + int port = 0; + ParseUdpReply(body, name, fw, port); + ANS_DBG("ANSDISC", "UDP reply from %s name=%s fw=%s port=%d raw=%s", + ipStr, name.c_str(), fw.c_str(), port, body.c_str()); + state.merge(ipStr, name, fw, port, kSrcMulticast); } closesocket(s); } @@ -1601,7 +1631,7 @@ void RunUdpSender(ScanState& state, const std::vector& subnets) { // ---- One synchronous HTTP probe via WinHTTP ----------------------------- // On AIBOX/ANSVIS reply: out_version is set and returns true. Otherwise false. -bool ProbeHttp(const std::string& ip, std::string& out_version) { +bool ProbeHttp(const std::string& ip, unsigned short port, std::string& out_version) { HINTERNET hSession = WinHttpOpen(kHttpUserAgent, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); @@ -1612,7 +1642,7 @@ bool ProbeHttp(const std::string& ip, std::string& out_version) { std::wstring host(ip.begin(), ip.end()); HINTERNET hConn = WinHttpConnect(hSession, host.c_str(), - INTERNET_DEFAULT_HTTP_PORT, 0); + static_cast(port), 0); if (!hConn) { WinHttpCloseHandle(hSession); return false; } HINTERNET hReq = WinHttpOpenRequest(hConn, L"GET", kHttpPath, @@ -1651,12 +1681,16 @@ bool ProbeHttp(const std::string& ip, std::string& out_version) { return ok; } -// ---- HTTP sweep: 64-way worker pool consuming a queue of IPs ------------ +// ---- HTTP sweep: 64-way worker pool consuming a queue of (ip, port) ----- +// Each host is probed on every port in kSweepPorts (80 + 22211) so one +// sweep covers both AIBOX and ANSVIS. void RunHttpSweep(ScanState& state, const std::vector& subnets) { - std::queue q; + struct Target { std::string ip; unsigned short port; }; + std::queue q; for (const auto& net : subnets) { for (int h = 1; h <= 254; ++h) { - q.push(net.prefix + std::to_string(h)); + std::string ip = net.prefix + std::to_string(h); + for (unsigned short p : kSweepPorts) q.push({ ip, p }); } } ANS_DBG("ANSDISC", "HTTP sweep targets=%zu workers=%d", q.size(), kHttpConcurrency); @@ -1665,17 +1699,18 @@ void RunHttpSweep(ScanState& state, const std::vector& subnets) { auto worker = [&]() { for (;;) { if (state.stop.load(std::memory_order_relaxed)) return; - std::string ip; + Target t; { std::lock_guard lk(qmu); if (q.empty()) return; - ip = std::move(q.front()); + t = std::move(q.front()); q.pop(); } std::string ver; - if (ProbeHttp(ip, ver)) { - ANS_DBG("ANSDISC", "HTTP hit %s version=%s", ip.c_str(), ver.c_str()); - state.merge(ip, std::string(), ver, kSrcSweep); + if (ProbeHttp(t.ip, t.port, ver)) { + ANS_DBG("ANSDISC", "HTTP hit %s:%u version=%s", + t.ip.c_str(), static_cast(t.port), ver.c_str()); + state.merge(t.ip, std::string(), ver, static_cast(t.port), kSrcSweep); } } }; diff --git a/modules/ANSUtilities/ANSUtilities.h b/modules/ANSUtilities/ANSUtilities.h index f776cc3..70d96d4 100644 --- a/modules/ANSUtilities/ANSUtilities.h +++ b/modules/ANSUtilities/ANSUtilities.h @@ -25,6 +25,8 @@ namespace ANSCENTER { std::string ip; // IPv4 address (dotted-decimal) std::string name; // Device name from UDP probe, empty if HTTP-only std::string firmware; // Version string ("-" if device did not report one) + int port{ 0 }; // TCP port the device's HTTP API answers on + // (AIBOX = 80, ANSVIS = 22211). 0 = unknown. int source{ 0 }; // 1 = multicast, 2 = HTTP sweep, 3 = both }; diff --git a/modules/ANSUtilities/dllmain.cpp b/modules/ANSUtilities/dllmain.cpp index 382a246..53ed966 100644 --- a/modules/ANSUtilities/dllmain.cpp +++ b/modules/ANSUtilities/dllmain.cpp @@ -3010,6 +3010,7 @@ static std::string DevicesToJson(const std::vector& de j += "{\"ip\":\""; j += JsonEscape(devs[i].ip); j += "\","; j += "\"name\":\""; j += JsonEscape(devs[i].name); j += "\","; j += "\"firmware\":\""; j += JsonEscape(devs[i].firmware); j += "\","; + j += "\"port\":"; j += std::to_string(devs[i].port); j += ','; j += "\"source\":"; j += std::to_string(devs[i].source); j += '}'; } diff --git a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp index a1cf41b..3866a81 100644 --- a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp +++ b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp @@ -405,7 +405,7 @@ int ScanDevicesTest() : (d.source == 2) ? "sweep" : (d.source == 1) ? "multicast" : "unknown"; - std::cout << " " << d.ip + std::cout << " " << d.ip << ":" << d.port << " name=\"" << d.name << "\"" << " firmware=\"" << d.firmware << "\"" << " source=" << src << std::endl;