|
|
|
@@ -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<LocalSubnet> EnumerateLocalSubnets() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Parse "<a>:<b>:<name>:<firmware>" 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<std::string> 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<bool> 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<std::mutex> 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<LocalSubnet>& 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<LocalSubnet>& 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<INTERNET_PORT>(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<LocalSubnet>& subnets) {
|
|
|
|
|
std::queue<std::string> q;
|
|
|
|
|
struct Target { std::string ip; unsigned short port; };
|
|
|
|
|
std::queue<Target> 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<LocalSubnet>& subnets) {
|
|
|
|
|
auto worker = [&]() {
|
|
|
|
|
for (;;) {
|
|
|
|
|
if (state.stop.load(std::memory_order_relaxed)) return;
|
|
|
|
|
std::string ip;
|
|
|
|
|
Target t;
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> 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<unsigned>(t.port), ver.c_str());
|
|
|
|
|
state.merge(t.ip, std::string(), ver, static_cast<int>(t.port), kSrcSweep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|