From 4f392ce95e43a2065d4b6557aff36a36c305cfb0 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Mon, 27 Jul 2026 09:29:05 +1000 Subject: [PATCH 1/3] anslicensing: fix 3-byte heap overflow in hardware-id BitStream3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GetCurrentHardwareId sized its bit stream at 125 bits (16 bytes) but writes 147 (3 version + 9 x 16-bit hashes), so the last two hash slots overflowed the heap block by 3 bytes on every id computation. The Windows heap absorbed it silently for years; it SIGSEGVs under macOS malloc — found when ANSGateway enabled this code on POSIX (fixed there in ANSGateway 3a34c68/6c3cdfd; this is the upstream port). Two parts, both id-preserving (only the first 16 bytes feed the base32 encode — verified byte-identical ids before/after in the gateway): - hwid.cpp: size the stream to the 147 bits actually written. - BitStream3: bounds-safe byteAt/setByte accessors on every Write/Read, Attach records the real buffer size, default ctor zero-inits — an under-sized stream can no longer corrupt the heap, and out-of-range reads return 0 (identical to the base32 padding zeros the Match path always read there). Co-Authored-By: Claude Fable 5 --- core/anslicensing/bitstream3.cpp | 31 +++++++++++++++++-------------- core/anslicensing/bitstream3.h | 14 ++++++++++++++ core/anslicensing/hwid.cpp | 8 +++++++- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/core/anslicensing/bitstream3.cpp b/core/anslicensing/bitstream3.cpp index 1663d6e..e22e296 100644 --- a/core/anslicensing/bitstream3.cpp +++ b/core/anslicensing/bitstream3.cpp @@ -8,9 +8,11 @@ const unsigned char BitStream3::hiMask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0x BitStream3::BitStream3() : ownMemory(false), +buffer(NULL), +bufSize(0), +bitSize(0), currentBit(0), -currentByte(0), -buffer(NULL) +currentByte(0) { } @@ -41,6 +43,7 @@ void BitStream3::Attach(void * buf, int bitCount) { buffer = (unsigned char *)buf; bitSize = bitCount; + bufSize = (bitCount + 7) >> 3; currentBit = 0; currentByte = 0; } @@ -53,20 +56,20 @@ int BitStream3::Write(const unsigned char * buf, int bitCount) { if ((gap & 7) != 0) { - buffer[currentByte] = (unsigned char)((buffer[currentByte] << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap))); + setByte(currentByte, (unsigned char)((byteAt(currentByte) << gap) | (buf[0] >> (bitCount < 8 ? bitCount - gap : 8 - gap)))); currentBit += gap; bitCount -= gap; currentByte++; for (; bitCount >= 8; i++, currentByte++, currentBit += 8, bitCount -= 8) - buffer[currentByte] = (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap))); + setByte(currentByte, (unsigned char)((buf[i] << gap) | (buf[i + 1] >> (8 - gap)))); } else for (; bitCount >= 8; currentByte++, i++, currentBit += 8, bitCount -= 8) - buffer[currentByte] = buf[i]; + setByte(currentByte, buf[i]); } if (bitCount > 0) { - buffer[currentByte] = (unsigned char)((buffer[currentByte] << bitCount) | (buf[i] & ((1 << bitCount) - 1))); + setByte(currentByte, (unsigned char)((byteAt(currentByte) << bitCount) | (buf[i] & ((1 << bitCount) - 1)))); currentBit += bitCount; } @@ -86,20 +89,20 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount) if ((gap & 7) != 0) { for (; bitCount >= 8 + 8 - gap; bitCount -= 8, currentByte++, currentBit += 8, i++) - readBuf[i] = (unsigned char)((buffer[currentByte] << gap) | (buffer[currentByte + 1] >> (8 - gap))); + readBuf[i] = (unsigned char)((byteAt(currentByte) << gap) | (byteAt(currentByte + 1) >> (8 - gap))); - // byte align + // byte align if (bitCount > 0) { if (bitCount < 8 - gap) { - readBuf[i] = (unsigned char)((buffer[currentByte] >> (8 - gap - bitCount)) & ((1 << bitCount) - 1)); + readBuf[i] = (unsigned char)((byteAt(currentByte) >> (8 - gap - bitCount)) & ((1 << bitCount) - 1)); currentBit += bitCount; bitCount = 0; } else { - readBuf[i] = (unsigned char)(buffer[currentByte] & ((1 << (8 - gap)) - 1)); + readBuf[i] = (unsigned char)(byteAt(currentByte) & ((1 << (8 - gap)) - 1)); bitCount -= (8 - gap); currentBit += (8 - gap); @@ -111,15 +114,15 @@ int BitStream3::Read(unsigned char * readBuf, int bitCount) } else for (; bitCount >= 8; bitCount -= 8, currentByte++, currentBit += 8, i++) - readBuf[i] = buffer[currentByte]; + readBuf[i] = byteAt(currentByte); // already aligned if (bitCount > 0) { if (currentBit + 8 <= bitSize) // not last byte - readBuf[i] |= (unsigned char)((buffer[currentByte] >> (8 - bitCount)) & ((1 << bitCount) - 1)); - else // last byte - readBuf[i] = (unsigned char)((buffer[currentByte] >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1)); + readBuf[i] |= (unsigned char)((byteAt(currentByte) >> (8 - bitCount)) & ((1 << bitCount) - 1)); + else // last byte + readBuf[i] = (unsigned char)((byteAt(currentByte) >> ((bitSize & 7) - bitCount)) & ((1 << bitCount) - 1)); currentBit += bitCount; } diff --git a/core/anslicensing/bitstream3.h b/core/anslicensing/bitstream3.h index 841bd3b..c968a0f 100644 --- a/core/anslicensing/bitstream3.h +++ b/core/anslicensing/bitstream3.h @@ -16,6 +16,20 @@ public: unsigned char * GetBuffer(); protected: + // Bounds-safe accessors: every Write/Read goes through these so a caller + // that under-sizes the stream can no longer corrupt the heap (the 125-vs-147 + // bug in GetCurrentHardwareId overflowed 3 bytes for years). Out-of-range + // reads return 0 — identical to the base32 padding zeros the hardware-id + // Match path has always effectively read there. + unsigned char byteAt(int i) const + { + return (buffer && i >= 0 && i < bufSize) ? buffer[i] : 0; + } + void setByte(int i, unsigned char v) + { + if (buffer && i >= 0 && i < bufSize) buffer[i] = v; + } + bool ownMemory; unsigned char * buffer; int bufSize, bitSize; diff --git a/core/anslicensing/hwid.cpp b/core/anslicensing/hwid.cpp index d86d98f..1b72a00 100644 --- a/core/anslicensing/hwid.cpp +++ b/core/anslicensing/hwid.cpp @@ -220,7 +220,13 @@ const char* HardwareId::GetCurrentHardwareId() WmiHelper wmi; #endif - BitStream3 bits(125); + // 147 bits are actually written below (3 version + 9 x 16-bit hashes). The + // previous size of 125 allocated only 16 bytes and the last two hash slots + // overflowed the heap block by 3 bytes (asymptomatic on the Windows heap, + // SIGSEGV under macOS malloc — found when ANSGateway enabled this code on + // POSIX). Only the first 16 bytes feed the base32 encode, so sizing the + // stream correctly changes no id on any platform. + BitStream3 bits(147); list propList; unsigned char version = 1; unsigned short hash1, hash2, hash3, hash4; From 6f1d069d14a7eef062bd5840c8217d176bdbd5a7 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Tue, 28 Jul 2026 19:34:26 +1000 Subject: [PATCH 2/3] anslicensing: Linux hardware id uses PHYSICAL NICs only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The id is built from up to 6 enumerated MACs. On Linux getifaddrs() also returns virtual interfaces — docker0 and the veth* pairs each container creates, tun*/wg* from a VPN, br-*/virbr* bridges. Container veths carry RANDOM MACs and appear/disappear with the containers, so the enumerated set can differ between calls: the hardware id changes and an activated licence silently stops matching its machine. Boxes running Docker (the Coder feature) would hit this routinely. This matters more on Linux than elsewhere because two of the four id components are already constant there: the system-UUID slot is a literal and GetHDDPropertyList is an empty function on non-Windows. The id rests on CPU id + MACs, so unstable MACs are most of the instability. Filter mirrors what the Windows branch already does via WMI (PNPDeviceId LIKE %PCI% — physical adapters only). The test is /sys/class/net//device, the kernel's own record of a hardware-backed interface, so it stays correct as new virtual-interface flavours appear; a name-prefix denylist is kept only for environments without /sys. MACs are bucketed during the walk and chosen after it, so a veth appearing earlier in the list cannot consume a slot and push a real NIC out. FAIL-SAFE: if no physical NIC is found, fall back to the unfiltered set. An empty list would drop all MAC entropy and — with the UUID and disk slots already constant — leave near-identical ids across different machines. Windows and macOS/BSD paths are untouched (both #else branches preserve the existing behaviour exactly). Logic verified against a simulated Docker host: enp3s0/wlp2s0 accepted; lo, docker0, veth*, br-*, tun0 rejected. ORDERING: this CHANGES the id on any Linux box that has virtual interfaces, so it must land BEFORE the first Linux licences are issued — afterwards it would invalidate them. Co-Authored-By: Claude Fable 5 --- core/anslicensing/hwid.cpp | 77 +++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/core/anslicensing/hwid.cpp b/core/anslicensing/hwid.cpp index 1b72a00..349d960 100644 --- a/core/anslicensing/hwid.cpp +++ b/core/anslicensing/hwid.cpp @@ -21,9 +21,10 @@ #include #ifdef __linux__ # include +# include // stat() — physical-NIC test in linuxIsPhysicalNic # include # include -# include +# include // strncmp/strlen (same test) // Match Linux to FreeBSD # define AF_LINK AF_PACKET @@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list* propList) propList->push_back(buf); } +#ifdef __linux__ +// A PHYSICAL network interface, i.e. one backed by a real hardware device. +// +// Why this exists: the hardware id is built from up to 6 enumerated MACs, and +// on Linux getifaddrs() also returns VIRTUAL interfaces — docker0 and the +// veth* pairs a container creates, tun*/wg* from a VPN, br-*/virbr* bridges. +// Container veths get RANDOM MACs and come and go with the containers, so an +// unfiltered enumeration can hand back a different set on every call: the id +// changes and an activated licence silently stops matching its machine. Boxes +// running Docker (the Coder feature) hit this routinely. +// +// The test mirrors what the Windows branch already does via WMI ("PNPDeviceId +// LIKE %PCI%" — physical adapters only). /sys/class/net//device is a +// symlink to the backing device and exists ONLY for hardware-backed +// interfaces; virtual ones have no such link. That is the kernel's own answer, +// so it stays correct as new virtual-interface flavours appear — unlike a name +// denylist, which is kept only as a cheap second line of defence. +static bool linuxIsPhysicalNic(const char* ifname) +{ + if (ifname == NULL || ifname[0] == '\0') return false; + + // Names that are virtual by construction. Not exhaustive on purpose — the + // /sys check below is the real test; this only catches oddities where /sys + // is unavailable (unusual containers, minimal namespaces). + static const char* kVirtualPrefixes[] = { + "lo", "docker", "veth", "br-", "virbr", "tun", "tap", "wg", + "vmnet", "vboxnet", "zt", "ppp", "dummy", "kube", "cni", "flannel", + "lxc", "lxd", "nerdctl", "podman", "cali", "weave" + }; + for (const char* prefix : kVirtualPrefixes) { + if (strncmp(ifname, prefix, strlen(prefix)) == 0) return false; + } + + std::string path = std::string("/sys/class/net/") + ifname + "/device"; + struct stat st; + // stat() (not lstat) so the symlink is followed — we want "the target + // exists", i.e. a real backing device. + return stat(path.c_str(), &st) == 0; +} +#endif // __linux__ + void GetMACPropertyList(std::list* propList, int maxCount) { // Head of the interface address linked list @@ -99,6 +141,14 @@ void GetMACPropertyList(std::list* propList, int maxCount) return; } +#ifdef __linux__ + // Collected separately so physical NICs can be preferred wholesale. Taking + // them inline would let a veth that merely appears EARLIER in the list + // consume one of the maxCount slots and push a real NIC out. + std::list physical; + std::list fallback; +#endif + while (current != NULL && maxCount > 0) { const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr); @@ -127,8 +177,18 @@ void GetMACPropertyList(std::list* propList, int maxCount) { char buf[18]; sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]); +#ifdef __linux__ + // Bucket now, choose after the walk (see the lists above). + // maxCount is NOT decremented here: the cap applies to the + // SELECTED set, not to whatever happened to be enumerated. + if (linuxIsPhysicalNic(current->ifa_name)) + physical.push_back(buf); + else + fallback.push_back(buf); +#else propList->push_back(buf); maxCount--; +#endif } } @@ -137,6 +197,21 @@ void GetMACPropertyList(std::list* propList, int maxCount) freeifaddrs(ifap); ifap = NULL; + +#ifdef __linux__ + // Physical NICs only — that is the whole point. FAIL-SAFE: if the filter + // found none (no /sys, an exotic NIC driver, a network namespace with only + // virtual links) fall back to the unfiltered set rather than returning + // nothing. An empty list would drop ALL MAC entropy from the id, and with + // the system-UUID and disk-serial slots already constant on Linux that + // would leave near-identical ids across different machines — far worse + // than an id that occasionally drifts. + std::list& chosen = physical.empty() ? fallback : physical; + for (std::list::const_iterator it = chosen.begin(); + it != chosen.end() && maxCount > 0; ++it, --maxCount) { + propList->push_back(*it); + } +#endif } void GetHDDPropertyList(std::list* propList, int maxCount) From 86b520d85c4fb69c2fc3cf7ca5f933dd708075c9 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Wed, 12 Aug 2026 21:24:12 +1000 Subject: [PATCH 3/3] ANSAWSS3: recover from dead sockets, and report why an upload failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Field logs showed clusters of uploads taking exactly 20004/20007/20003 ms — the default _idleTimeoutMs — across several workers at the same instant, then recurring. Cause: a connection whose request failed was returned to the pool, so a socket silently dropped by a middlebox was handed to worker after worker, each paying the full idle timeout to rediscover it. Chilkat's autoReconnect only covers a cleanly closed connection, not a half-open one. Connection handling: - DiscardConnection(): a TRANSPORT failure closes the connection instead of pooling it (23 sites). An HTTP error status is not a transport failure - the server answered - so those connections are still reused. - Pooled connections idle longer than 15 s are dropped at acquire time. - First request on a reused connection uses a short idle timeout (a quarter of the configured one, 5 s by default); a fresh connection keeps the full one. A dead socket now costs ~5 s once, not 20 s repeatedly. - One upload call is capped at 30 s wall-clock. The attempt count alone was not a time bound: 20 attempts x 20 s could block the caller for ~400 s. - RetryLoop no longer gives up after one failed connect. Callers call Connect() once at startup, so returning left the handle dead for the process lifetime. Now backs off 3 s -> 30 s until connected or stopped. - Connections carry the config generation they were built against, so a connect completing just after SetAuthentication cleared the pool cannot leave an unauthenticated connection behind (those uploads would 403). - Stale per-request state is cleared on acquire: multipart uploadId/partNumber query params, and the gzip Content-Encoding set by UploadTextData. Not ClearAllHeaders - that wipes SigV4 state (fcc985e). Return codes - upload exports now say whether a retry is worthwhile: -20 permanent (bad argument, licence, or HTTP 4xx), -21 not connected yet, -22 transient. Recorded per thread, since one handle is shared by many workers. Success stays the only positive value, so rc == 1 and rc != 1 are unchanged. Handle and exception codes (-1/-3/-4) made consistent across all upload wrappers; some previously returned 0 for these. Also fixes a misclassification this surfaced: Chilkat reports failure for an HTTP 4xx delivered during an Expect: 100-continue exchange, so NoSuchBucket was treated as a network error and retried 20 times before reporting "transient". The failure path now checks the response status. Chilkat's ~2 KB error dump per failure is compressed to one line carrying the actual S3 error code. Verified against live S3: 14,701 uploads across 15 threads with a 150 s idle gap, 0 failures, no call over 5 s (avg 90 ms, p95 136 ms), matching the pre-change baseline; and all six return-code classes confirmed end to end. Co-Authored-By: Claude Opus 5 --- docs/ANSAWSS3_Return_Codes.md | 79 +++++ modules/ANSUtilities/ANSAWSS3.cpp | 454 ++++++++++++++++++++-------- modules/ANSUtilities/ANSUtilities.h | 96 ++++++ modules/ANSUtilities/dllmain.cpp | 183 ++++++----- 4 files changed, 606 insertions(+), 206 deletions(-) create mode 100644 docs/ANSAWSS3_Return_Codes.md diff --git a/docs/ANSAWSS3_Return_Codes.md b/docs/ANSAWSS3_Return_Codes.md new file mode 100644 index 0000000..2666f39 --- /dev/null +++ b/docs/ANSAWSS3_Return_Codes.md @@ -0,0 +1,79 @@ +# ANSAWSS3 — return codes and retry behaviour + +Reference for callers of the `*ANSAWSHandle` exports in `ANSUtilities.dll` +(LabVIEW, C++, .NET). Source of truth: `modules/ANSUtilities/ANSUtilities.h` +(`kCall*` constants) and `modules/ANSUtilities/dllmain.cpp`. + +## The one rule + +**`rc == 1` means success. Everything else is a failure.** Success is the only +positive value, so `rc == 1` and `rc != 1` are both always correct tests. The +negative values exist so a caller can tell *why* it failed, and therefore +whether retrying is worthwhile. + +## Upload functions + +Applies to every `Upload*ANSAWSHandle`, `Upload*ANSAWSHandle_CPP` and +`Upload*ANSAWSHandle_V2` export (JPEG, binary, text, file-stream, multipart, +with or without prefix). + +| Code | Meaning | Retry? | +|-----:|---------|--------| +| `1` | Uploaded. `uploadedFilePath` holds the object URL. | — | +| `0` | A required pointer argument was `NULL`. | **No** — caller bug | +| `-1` | Handle is not in the registry: never created, already released, or a stale value. | **No** — re-create the handle | +| `-2` | Failed, reason not classified. Should not occur; kept as a fallback. | Yes | +| `-3` | A `std::exception` escaped. | Yes | +| `-4` | An unknown exception escaped. | Yes | +| `-20` | **Permanent.** Bad argument (empty bucket/filename, part size < 5 MB, unreadable file), invalid licence or unlock code, or S3 answered with a 4xx — wrong bucket, no permission, bad key. | **No** — fix the configuration; retrying burns time and changes nothing | +| `-21` | **Not connected.** `Connect()` has not succeeded yet, or the link went down and the background retry thread is still working on it. | **Yes, later** — back off a few seconds | +| `-22` | **Transient.** Network error, timeout, or HTTP 5xx, still failing after the DLL's own internal retries. | **Yes** | + +A practical LabVIEW policy: + +* `rc = 1` → done. +* `rc = -20` or `0` → log it and stop; it will never succeed as-is. +* `rc = -21` → queue the image and try again in a few seconds. +* anything else negative → retry, with backoff. + +Note that `-22` already means the DLL retried internally (up to 20 attempts +within a 30 s budget) before giving up, so an outer retry should not be tight. + +## Connect + +`ConnectANSAWSHandle` / `_V2`: + +| Code | Meaning | +|-----:|---------| +| `1` | Connected; ready for uploads once `SetAuthentication` is called. | +| `0` | A required argument was `NULL`, the handle is unknown, the licence is invalid, or the endpoint resolves but refuses the connection. | +| `2` | The endpoint could not be resolved at all (no internet). A background thread keeps retrying — 3 s, doubling to a 30 s ceiling — until it connects or the handle is released. **You do not need to call `Connect` again.** Uploads return `-21` in the meantime. | + +## Other functions + +`CreateANSAWSHandle`, `ReleaseANSAWSHandle`, `SetAuthenticationANSAWSHandle`, +`SetProxyANSAWSHandle`, the `ListBucket*` / `CreateBucket*` / `DeleteBucket*` +family, `DownloadFileStreamANSAWSHandle` and `GeneratePresignedURLANSAWSHandle` +return `1` on success and `0` on failure. `GeneratePresignedURLANSAWSHandle` +(the `LStrHandle` variant) additionally uses `-1`, `-2`, `-3`, `-4` with the +same meanings as the table above. + +## How connections behave + +* `SetAuthentication` opens and keeps 8 pooled HTTPS connections; uploads borrow + and return them, so the TLS handshake is not repeated per image. +* A connection whose **transport** failed is closed, never returned to the pool. + A connection that merely received an HTTP error status is healthy and is kept. +* A pooled connection idle longer than 15 s is closed and replaced rather than + handed out, since middleboxes silently drop idle flows. +* The first request on a reused connection uses a short idle timeout (a quarter + of the configured one, 5 s by default); a fresh connection gets the full + timeout. This bounds what a dead socket costs. +* One upload call is capped at 30 s of wall clock across all internal retries. + +## Diagnostics + +`ANSAWSS3.log` and DebugView (`AWSS3` / `ANSAWS` tags) record the decision +points: pool evictions, discarded connections, the permanent/transient +classification with the S3 error code, deadline expiry, and background +reconnect attempts. diff --git a/modules/ANSUtilities/ANSAWSS3.cpp b/modules/ANSUtilities/ANSAWSS3.cpp index 2ea36e8..3aa756e 100644 --- a/modules/ANSUtilities/ANSAWSS3.cpp +++ b/modules/ANSUtilities/ANSAWSS3.cpp @@ -168,6 +168,78 @@ namespace ANSCENTER return out.str(); } + // ── Per-thread call status ─────────────────────────────────────────────── + // Written by every upload entry point, read by the C exports in dllmain so + // the caller learns WHY a call failed (and therefore whether to retry) + // without any change to the exported signatures. Thread-local because a + // single ANSAWSS3 handle is routinely shared by many worker threads — a + // per-instance field would report another worker's outcome. + static thread_local int t_callStatus = ANSAWSS3::kCallPermanentError; + + void ANSAWSS3::SetCallStatus(int status) { t_callStatus = status; } + int ANSAWSS3::LastCallStatus() { return t_callStatus; } + + // ──────────────────────────────────────────────────────────────────────── + // SummarizeChilkatRequestError: one line out of a request-level Chilkat log. + // + // The raw log is ~2 KB of call-stack scaffolding per failure. Written once + // per retry, per worker, it buries the one thing an operator needs: what S3 + // actually said. S3 returns its reason as XML in the response body, which + // Chilkat echoes into the log: + // NoSuchBucketThe specified bucket does + // not exist... + // Pull that out, prefix the status code, and drop the rest. + // ──────────────────────────────────────────────────────────────────────── + static std::string SummarizeChilkatRequestError(const char* raw, int statusCode) { + std::ostringstream out; + if (statusCode > 0) out << "HTTP " << statusCode; + + auto tagValue = [](const std::string& s, const char* tag) -> std::string { + const std::string open = std::string("<") + tag + ">"; + const std::string close = std::string(""; + const size_t a = s.find(open); + if (a == std::string::npos) return {}; + const size_t b = s.find(close, a + open.size()); + if (b == std::string::npos) return {}; + return s.substr(a + open.size(), b - a - open.size()); + }; + + if (raw && *raw) { + const std::string s(raw); + const std::string code = tagValue(s, "Code"); + const std::string msg = tagValue(s, "Message"); + if (!code.empty()) { + if (statusCode > 0) out << " "; + out << code; + if (!msg.empty()) out << ": " << msg; + } + else { + // No S3 error document — fall back to the deepest concrete line + // (same idea as SummarizeChilkatError, without the connect + // specific fields). + std::istringstream iss(s); + std::string ln, leaf; + while (std::getline(iss, ln)) { + const size_t a = ln.find_first_not_of(" \t\r\n"); + if (a == std::string::npos) continue; + const size_t b = ln.find_last_not_of(" \t\r\n"); + ln = ln.substr(a, b - a + 1); + if (ln.rfind("--", 0) == 0) continue; // close marker + if (!ln.empty() && ln.back() == ':') continue; // stack label + if (ln == "Failed.") continue; + leaf = ln; + } + if (!leaf.empty()) { + if (statusCode > 0) out << " — "; + out << leaf; + } + } + } + + std::string result = out.str(); + return result.empty() ? "(no detail in Chilkat log)" : result; + } + // Private helper function to extract file name from a path // Helper function to extract filename from path std::string ANSAWSS3::ExtractFileName(const std::string& filePath) { @@ -302,12 +374,23 @@ namespace ANSCENTER return true; } + // Background connect loop, started by Connect() when the endpoint could not + // be reached at all (return code 2). + // + // This loop NEVER gives up on its own. The caller — a LabVIEW VI, an + // unattended AIBOX — calls Connect() exactly once at startup, so if this + // thread returned while still unconnected the handle would stay dead for + // the life of the process and every upload would fail instantly. Backoff + // grows 3 s -> 30 s so a long outage costs little, and the loop only exits + // once connected or when StopRetry() is called. void ANSAWSS3::RetryLoop() { int attempt = 0; + int backoffSec = 3; + constexpr int kMaxBackoffSec = 30; while (!_stopRetry) { - // Wait 3 seconds (in 1-second increments so we can respond to stop quickly) - for (int i = 0; i < 3 && !_stopRetry; ++i) { + // Sleep in 1-second increments so we can respond to stop quickly. + for (int i = 0; i < backoffSec && !_stopRetry; ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); } if (_stopRetry) break; @@ -327,8 +410,12 @@ namespace ANSCENTER } if (!resolved) { _logger.LogDebug("ANSAWSS3::RetryLoop", - "Retry #" + std::to_string(attempt) + " - no internet, retrying in 3s", + "Retry #" + std::to_string(attempt) + " - no internet, retrying in " + + std::to_string(backoffSec) + "s", __FILE__, __LINE__); + ANS_DBG("AWSS3", "RetryLoop #%d: DNS still failing, next try in %ds", + attempt, backoffSec); + backoffSec = (std::min)(kMaxBackoffSec, backoffSec * 2); continue; } } @@ -341,19 +428,24 @@ namespace ANSCENTER _logger.LogDebug("ANSAWSS3::RetryLoop", "Connected successfully after " + std::to_string(attempt) + " retries", __FILE__, __LINE__); - _retryInProgress = false; - return; - } - else { - // Internet is available but connection failed (bad auth/URL) - // Stop retrying — no point, the parameters are wrong - _logger.LogError("ANSAWSS3::RetryLoop", - "Internet available but connection failed (check authentication or URL). Stopping retry.", - __FILE__, __LINE__); + ANS_DBG("AWSS3", "RetryLoop: connected after %d attempt(s)", attempt); _retryInProgress = false; return; } } + + // DNS resolves but the connection failed — bad credentials, wrong + // URL, or the endpoint is refusing us. Keep trying anyway: the + // caller has no other way back, and this state is often temporary + // (endpoint rebooting, TLS interception clearing, clock skew being + // corrected). Backoff keeps the cost negligible. + _logger.LogError("ANSAWSS3::RetryLoop", + "Retry #" + std::to_string(attempt) + + " - endpoint resolves but connection failed (check authentication or URL). " + "Retrying in " + std::to_string(backoffSec) + "s", + __FILE__, __LINE__); + ANS_DBG("AWSS3", "RetryLoop #%d: connect failed, next try in %ds", attempt, backoffSec); + backoffSec = (std::min)(kMaxBackoffSec, backoffSec * 2); } _retryInProgress = false; } @@ -366,9 +458,16 @@ namespace ANSCENTER std::unique_ptr ANSAWSS3::CreateConnection(bool logOnFailure) { auto conn = std::make_unique(); + // Stamp the config generation this connection is built against so a + // later Connect / SetAuthentication / SetServerProxy can invalidate it + // even if it lands in the pool after that call cleared it. + conn->configGen = _configGen.load(); + // Apply request timeouts BEFORE Connect() so ConnectTimeoutMs takes // effect on this very handshake. IdleTimeoutMs is sticky and applies - // to every subsequent request on this CkRest. + // to every subsequent request on this CkRest. A brand-new connection + // gets the FULL idle timeout — it is known-good, so it should be given + // every chance to complete; only reuse gets the short leash. conn->rest.put_ConnectTimeoutMs(_connectTimeoutMs.load()); conn->rest.put_IdleTimeoutMs(_idleTimeoutMs.load()); @@ -423,26 +522,82 @@ namespace ANSCENTER return conn; } + // Take a connection from the pool, skipping any that are stale (idle too + // long) or built against superseded config. Falls through to a fresh + // connection when nothing reusable is left. std::unique_ptr ANSAWSS3::AcquireConnection() { - { - std::lock_guard lk(_poolMutex); - if (!_pool.empty()) { - auto conn = std::move(_pool.back()); + const uint64_t gen = _configGen.load(); + const auto now = std::chrono::steady_clock::now(); + + for (;;) { + std::unique_ptr conn; + { + std::lock_guard lk(_poolMutex); + if (_pool.empty()) break; + conn = std::move(_pool.back()); _pool.pop_back(); - return conn; } + + if (conn->configGen != gen) { + ANS_DBG("AWSS3", "Acquire: dropping pooled conn from older config gen %llu (now %llu)", + (unsigned long long)conn->configGen, (unsigned long long)gen); + DiscardConnection(std::move(conn)); // pool lock released first + continue; + } + + const long long idleMs = std::chrono::duration_cast( + now - conn->lastUsed).count(); + if (idleMs > kMaxPooledIdleMs) { + ANS_DBG("AWSS3", "Acquire: dropping pooled conn idle for %lldms (> %dms)", + idleMs, kMaxPooledIdleMs); + DiscardConnection(std::move(conn)); + continue; + } + + // Clear per-request state left behind by whoever used this + // connection last. Multipart uploads set uploadId / partNumber + // query params; a plain PUT that inherited them would target the + // wrong resource. Content-Encoding is set by UploadTextData only, + // and a JPEG that inherited "gzip" would be stored with metadata + // saying it is compressed — browsers would then fail to display it. + // NOTE: do NOT call ClearAllHeaders here — it wipes Chilkat's SigV4 + // state (see commit fcc985e). Headers that every upload sets + // explicitly (Content-Type, Expect) are overwritten by AddHeader + // and need no clearing. + conn->rest.ClearAllQueryParams(); + conn->rest.RemoveHeader("Content-Encoding"); + + // Short leash on reuse: if this socket died while idle, fail fast + // and let the retry run on a fresh connection. + conn->rest.put_IdleTimeoutMs(_reusedIdleTimeoutMs.load()); + return conn; } - // Pool empty — create a new connection (no lock held during network I/O) + + // Pool empty (or everything in it was stale) — create a new connection. std::lock_guard cfgLk(_configMutex); return CreateConnection(); } void ANSAWSS3::ReleaseConnection(std::unique_ptr conn) { if (!conn) return; + // Mark as fresh and restore the full idle timeout; AcquireConnection + // re-applies the short one when it hands the connection out again. + conn->lastUsed = std::chrono::steady_clock::now(); + conn->rest.put_IdleTimeoutMs(_idleTimeoutMs.load()); std::lock_guard lk(_poolMutex); _pool.push_back(std::move(conn)); } + // Close a suspect connection instead of pooling it. Disconnect is given a + // small budget only — the point is to release the socket, not to wait on a + // peer that has already stopped answering. + void ANSAWSS3::DiscardConnection(std::unique_ptr conn) { + if (!conn) return; + conn->rest.Disconnect(100); + ANS_DBG("AWSS3", "Discarded connection (not returned to pool)"); + // unique_ptr destructor tears the CkRest down here. + } + // Pre-populate the pool with `count` ready-to-use connections. Each // connection performs a TLS handshake; doing `count` of them in parallel // (rather than serially as AcquireConnection would on a cold pool) cuts @@ -509,6 +664,16 @@ namespace ANSCENTER _connectTimeoutMs.store(connectMs); _idleTimeoutMs.store(idleMs); + // Reuse timeout scales with the configured idle timeout (a quarter of + // it, clamped to a sane floor) so a deployment on a slow link that + // raises idleMs does not end up with an over-aggressive leash on + // pooled sockets. + // Never longer than the configured idle timeout itself (idleMs can be + // as low as 1000), and never so short that a healthy but busy server + // gets cut off. + const int reusedMs = (std::min)(idleMs, (std::max)(2000, idleMs / 4)); + _reusedIdleTimeoutMs.store(reusedMs); + // Propagate to existing pooled connections so already-warm workers // start using the new values on their next request. { @@ -522,16 +687,68 @@ namespace ANSCENTER } _logger.LogDebug("ANSAWSS3::SetTimeouts", - "connect=" + std::to_string(connectMs) + "ms, idle=" + std::to_string(idleMs) + "ms", + "connect=" + std::to_string(connectMs) + "ms, idle=" + std::to_string(idleMs) + + "ms, idle-on-reuse=" + std::to_string(reusedMs) + "ms", __FILE__, __LINE__); return true; } + // Shared precondition check for every upload entry point. The distinction + // matters to the caller: a licence problem needs an operator, whereas "not + // connected" clears itself once the background RetryLoop gets through — so + // the two report different status codes. + bool ANSAWSS3::CheckUploadPreconditions(const std::string& opName) { + if (!_isLicenseValid || !_isUnlockCodeValid) { + _logger.LogError(opName, + !_isLicenseValid ? "Invalid license" : "Invalid unlock code", + __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); + return false; + } + if (!_bConnected) { + const bool retrying = _retryInProgress.load(); + _logger.LogError(opName, + retrying ? "Not connected (waiting for internet, retrying in background)" + : "Not connected (no internet or connection not established)", + __FILE__, __LINE__); + ANS_DBG("AWSS3", "%s: not connected (background retry %s) — caller should retry later", + opName.c_str(), retrying ? "active" : "inactive"); + SetCallStatus(kCallNotConnected); + return false; + } + return true; + } + + // See the declaration comment in ANSUtilities.h for why a failed Chilkat + // request is not automatically a network problem. + ANSAWSS3::AttemptResult ANSAWSS3::ClassifyFailedRequest( + std::unique_ptr conn, std::string& lastError) { + const int statusCode = conn ? conn->rest.get_ResponseStatusCode() : 0; + lastError = SummarizeChilkatRequestError( + conn ? conn->rest.lastErrorText() : nullptr, statusCode); + + // The server answered, and its answer will not change on a retry: + // wrong bucket, no permission, bad key. 408 and 429 are the exceptions + // — those explicitly ask the client to try again. + if (statusCode >= 400 && statusCode <= 499 && + statusCode != 408 && statusCode != 429) { + ANS_DBG("AWSS3", "Request failed permanently: %s", lastError.c_str()); + ReleaseConnection(std::move(conn)); // the socket itself is fine + return AttemptResult::Permanent; + } + + ANS_DBG("AWSS3", "Request failed transiently: %s", lastError.c_str()); + DiscardConnection(std::move(conn)); // suspect socket — do not pool + return AttemptResult::Transient; + } + // Drives a retry loop for a single upload operation. See the declaration // comment in ANSUtilities.h for the AttemptResult contract. // // Retry policy (shared across all upload entry points): - // - kUploadMaxAttempts attempts total + // - kUploadMaxAttempts attempts total, AND kUploadDeadlineMs wall-clock + // (whichever comes first — the attempt count alone does not bound how + // long the caller blocks, since one attempt can sit on a timeout) // - kUploadRetryDelayMs between attempts (no delay after the last one) // - std::exception thrown inside attemptFn is treated as Transient // - Any other exception is treated as Permanent (bail immediately) @@ -540,6 +757,11 @@ namespace ANSCENTER const std::function& attemptFn) { std::string lastError; + const auto started = std::chrono::steady_clock::now(); + auto elapsedMs = [&started]() { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - started).count(); + }; for (int attempt = 1; attempt <= kUploadMaxAttempts; ++attempt) { AttemptResult result = AttemptResult::Transient; @@ -556,6 +778,7 @@ namespace ANSCENTER } if (result == AttemptResult::Success) { + SetCallStatus(kCallSuccess); return true; } @@ -563,14 +786,29 @@ namespace ANSCENTER _logger.LogError(opName, "Permanent failure (no retry): " + lastError, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); + return false; + } + + // Transient — retry if both budgets allow it. + if (elapsedMs() >= kUploadDeadlineMs) { + _logger.LogError(opName, + "Upload gave up after " + std::to_string(attempt) + " attempt(s) / " + + std::to_string(elapsedMs()) + "ms (deadline " + + std::to_string(kUploadDeadlineMs) + "ms). Last error: " + lastError, + __FILE__, __LINE__); + ANS_DBG("AWSS3", "%s: deadline exceeded after %d attempt(s), %lldms", + opName.c_str(), attempt, elapsedMs()); + // Ran out of time on transient errors — the caller should retry. + SetCallStatus(kCallTransientError); return false; } - // Transient — retry if attempts remain. if (attempt < kUploadMaxAttempts) { _logger.LogDebug(opName, "Attempt " + std::to_string(attempt) + "/" + - std::to_string(kUploadMaxAttempts) + " failed: " + lastError + + std::to_string(kUploadMaxAttempts) + " failed after " + + std::to_string(elapsedMs()) + "ms: " + lastError + " — retrying in " + std::to_string(kUploadRetryDelayMs) + "ms", __FILE__, __LINE__); std::this_thread::sleep_for( @@ -580,8 +818,10 @@ namespace ANSCENTER _logger.LogError(opName, "Upload failed after " + std::to_string(kUploadMaxAttempts) + - " attempts. Last error: " + lastError, + " attempts / " + std::to_string(elapsedMs()) + "ms. Last error: " + lastError, __FILE__, __LINE__); + // Every attempt hit a transient error — worth retrying from the caller. + SetCallStatus(kCallTransientError); return false; } @@ -652,7 +892,10 @@ namespace ANSCENTER // Simplified proxy validation logic _bProxy = !proxyHost.empty() && proxyPort > 0; - // Clear pool so new connections pick up proxy config + // Clear pool so new connections pick up proxy config. Bumping the + // generation also invalidates any connection still in flight + // elsewhere, which would otherwise be pooled after this clear. + _configGen.fetch_add(1); { std::lock_guard lk(_poolMutex); _pool.clear(); @@ -701,7 +944,9 @@ namespace ANSCENTER _bTls = bTls; _bAutoReconnect = autoReconnect; - // Clear pool so new connections use updated config + // Clear pool so new connections use updated config (see the + // generation note in SetServerProxy). + _configGen.fetch_add(1); { std::lock_guard lk(_poolMutex); _pool.clear(); @@ -770,7 +1015,13 @@ namespace ANSCENTER _secretKey = secretKey; _authReady = true; - // Clear pool so new connections pick up new credentials. + // Clear pool so new connections pick up new credentials. The + // generation bump matters most here: the background RetryLoop + // can complete a connect at any moment, and a connection it + // built before _authReady was set carries no SigV4 state — if + // it landed in the pool after this clear, every upload using it + // would come back 403 (a permanent failure, so not retried). + _configGen.fetch_add(1); std::lock_guard lk(_poolMutex); _pool.clear(); } @@ -809,7 +1060,7 @@ namespace ANSCENTER if (!conn->rest.FullRequestNoBodySb("GET", "/", sbResponse)) { _logger.LogError("ANSAWSS3::ListBuckets - Request failed", conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return {}; } @@ -909,7 +1160,7 @@ namespace ANSCENTER if (!success) { _logger.LogError("ANSAWSS3::ListBucketObjects - Request failed on page " + std::to_string(pageCount), conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return objectList; // Return what we have so far } @@ -1071,7 +1322,7 @@ namespace ANSCENTER if (!success) { _logger.LogError("ANSAWSS3::ListBucketObjectsWithPrefix - Request failed on page " + std::to_string(pageCount), conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return objectList; // Return what we have so far } @@ -1227,7 +1478,7 @@ namespace ANSCENTER if (conn->rest.get_LastMethodSuccess() != true) { _logger.LogError("ANSAWSS3::CreateBucket - Request failed", conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return false; } @@ -1314,7 +1565,7 @@ namespace ANSCENTER _logger.LogError("ANSAWSS3::DeleteBucket", "Failed to set AWS authentication: " + std::string(conn->rest.lastErrorText()), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return false; } @@ -1440,7 +1691,7 @@ namespace ANSCENTER if (conn->rest.get_LastMethodSuccess() != true) { _logger.LogError("ANSAWSS3::CreateFolder - Request failed", conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return false; } @@ -1608,7 +1859,7 @@ namespace ANSCENTER if (conn->rest.get_LastMethodSuccess() != true) { _logger.LogError("ANSAWSS3::GetBucketRegion - Request failed", conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return ""; } @@ -1679,15 +1930,11 @@ namespace ANSCENTER // _logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || textFilePath.empty()) { _logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -1697,6 +1944,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Failed to extract filename from path: " + textFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } const std::string contentType = GetContentType(textFilePath); @@ -1740,9 +1988,7 @@ namespace ANSCENTER conn->rest.fullRequestString("PUT", objectPath.c_str(), fileContents); if (!conn->rest.get_LastMethodSuccess()) { - lastError = conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + return ClassifyFailedRequest(std::move(conn), lastError); } int statusCode = conn->rest.get_ResponseStatusCode(); @@ -1772,15 +2018,11 @@ namespace ANSCENTER // _logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || dataFilePath.empty()) { _logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -1789,6 +2031,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Failed to extract filename from path: " + dataFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } const std::string contentType = GetContentType(dataFilePath); @@ -1821,9 +2064,7 @@ namespace ANSCENTER CkStringBuilder sbResponse; if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) { - lastError = conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + return ClassifyFailedRequest(std::move(conn), lastError); } int statusCode = conn->rest.get_ResponseStatusCode(); @@ -1852,15 +2093,11 @@ namespace ANSCENTER //_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || dataFilePath.empty()) { _logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -1872,6 +2109,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Failed to extract filename from path: " + dataFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } } @@ -1923,9 +2161,7 @@ namespace ANSCENTER CkStringBuilder sbResponse; if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) { - lastError = conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + return ClassifyFailedRequest(std::move(conn), lastError); } int statusCode = conn->rest.get_ResponseStatusCode(); @@ -1956,15 +2192,11 @@ namespace ANSCENTER //_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || dataFilePath.empty()) { _logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -1994,9 +2226,7 @@ namespace ANSCENTER conn->rest.fullRequestStream("PUT", objectPath.c_str(), fileStream); if (conn->rest.get_LastMethodSuccess() != true) { - lastError = conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + return ClassifyFailedRequest(std::move(conn), lastError); } int statusCode = conn->rest.get_ResponseStatusCode(); @@ -2026,15 +2256,11 @@ namespace ANSCENTER //_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || dataFilePath.empty()) { _logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -2042,6 +2268,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Part size must be at least 5MB (5242880 bytes)", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -2049,6 +2276,7 @@ namespace ANSCENTER CkFileAccess fac; if (!fac.FileExists(dataFilePath.c_str())) { _logger.LogError(kOp, "File not found: " + dataFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } } @@ -2058,6 +2286,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Failed to extract filename from path: " + dataFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } @@ -2106,9 +2335,8 @@ namespace ANSCENTER const char* responseXml = conn->rest.fullRequestNoBody("POST", objectPath.c_str()); if (!conn->rest.get_LastMethodSuccess()) { - lastError = std::string("Initiate failed: ") + conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + { AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError); + lastError = "Initiate: " + lastError; return r; } } int initStatus = conn->rest.get_ResponseStatusCode(); if (initStatus != 200) { @@ -2160,9 +2388,8 @@ namespace ANSCENTER const char* responseStr = conn->rest.fullRequestStream("PUT", objectPath.c_str(), fileStream); if (!conn->rest.get_LastMethodSuccess()) { - lastError = "Part " + std::to_string(partNumber) + " failed: " + conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + { AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError); + lastError = "Part " + std::to_string(partNumber) + ": " + lastError; return r; } } int partStatus = conn->rest.get_ResponseStatusCode(); if (partStatus != 200) { @@ -2175,7 +2402,7 @@ namespace ANSCENTER const char* etag = conn->rest.responseHdrByName("ETag"); if (!conn->rest.get_LastMethodSuccess() || etag == nullptr || etag[0] == '\0') { lastError = "Part " + std::to_string(partNumber) + ": ETag missing from response"; - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return AttemptResult::Transient; } @@ -2194,9 +2421,8 @@ namespace ANSCENTER responseXml = conn->rest.fullRequestString("POST", objectPath.c_str(), partsListXml.getXml()); if (!conn->rest.get_LastMethodSuccess()) { - lastError = std::string("Complete failed: ") + conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + { AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError); + lastError = "Complete: " + lastError; return r; } } int completeStatus = conn->rest.get_ResponseStatusCode(); if (completeStatus != 200) { @@ -2227,15 +2453,11 @@ namespace ANSCENTER //_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || dataFilePath.empty()) { _logger.LogError(kOp, "Bucket name or file path is empty", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -2243,6 +2465,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Part size must be at least 5MB (5242880 bytes)", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -2250,6 +2473,7 @@ namespace ANSCENTER CkFileAccess fac; if (!fac.FileExists(dataFilePath.c_str())) { _logger.LogError(kOp, "File not found: " + dataFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } } @@ -2261,6 +2485,7 @@ namespace ANSCENTER _logger.LogError(kOp, "Failed to extract filename from path: " + dataFilePath, __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // bad input path — retrying cannot help return false; } } @@ -2323,9 +2548,8 @@ namespace ANSCENTER const char* responseXml = conn->rest.fullRequestNoBody("POST", objectPath.c_str()); if (!conn->rest.get_LastMethodSuccess()) { - lastError = std::string("Initiate failed: ") + conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + { AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError); + lastError = "Initiate: " + lastError; return r; } } int initStatus = conn->rest.get_ResponseStatusCode(); if (initStatus != 200) { @@ -2377,9 +2601,8 @@ namespace ANSCENTER const char* responseStr = conn->rest.fullRequestStream("PUT", objectPath.c_str(), fileStream); if (!conn->rest.get_LastMethodSuccess()) { - lastError = "Part " + std::to_string(partNumber) + " failed: " + conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + { AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError); + lastError = "Part " + std::to_string(partNumber) + ": " + lastError; return r; } } int partStatus = conn->rest.get_ResponseStatusCode(); if (partStatus != 200) { @@ -2392,7 +2615,7 @@ namespace ANSCENTER const char* etag = conn->rest.responseHdrByName("ETag"); if (!conn->rest.get_LastMethodSuccess() || etag == nullptr || etag[0] == '\0') { lastError = "Part " + std::to_string(partNumber) + ": ETag missing from response"; - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return AttemptResult::Transient; } @@ -2410,9 +2633,8 @@ namespace ANSCENTER responseXml = conn->rest.fullRequestString("POST", objectPath.c_str(), partsListXml.getXml()); if (!conn->rest.get_LastMethodSuccess()) { - lastError = std::string("Complete failed: ") + conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + { AttemptResult r = ClassifyFailedRequest(std::move(conn), lastError); + lastError = "Complete: " + lastError; return r; } } int completeStatus = conn->rest.get_ResponseStatusCode(); if (completeStatus != 200) { @@ -2444,17 +2666,13 @@ namespace ANSCENTER const std::string kOp = "ANSAWSS3::UploadJpegImage"; // _logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation checks (permanent — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || bufferLength <= 0) { _logger.LogError(kOp, "Bucket name or buffer length is invalid", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -2486,9 +2704,7 @@ namespace ANSCENTER CkStringBuilder sbResponse; if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) { - lastError = conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + return ClassifyFailedRequest(std::move(conn), lastError); } int statusCode = conn->rest.get_ResponseStatusCode(); @@ -2516,17 +2732,13 @@ namespace ANSCENTER const std::string kOp = "ANSAWSS3::UploadPrefixJpegImage"; //_logger.LogError(kOp,"Start Uploading...",__FILE__, __LINE__); // Early validation checks (permanent failures — do NOT retry) - if (!_isLicenseValid || !_isUnlockCodeValid || !_bConnected) { - _logger.LogError(kOp, - !_isLicenseValid ? "Invalid license" : !_isUnlockCodeValid ? "Invalid unlock code" : _retryInProgress.load() ? "Not connected (waiting for internet, retrying in background)" : "Not connected (no internet or connection not established)", - __FILE__, __LINE__); - return false; - } + if (!CheckUploadPreconditions(kOp)) return false; if (bucketName.empty() || bufferLength <= 0 || fileName.empty()) { _logger.LogError(kOp, "Bucket name, buffer length, or filename is invalid", __FILE__, __LINE__); + SetCallStatus(kCallPermanentError); // caller bug — retrying cannot help return false; } @@ -2574,9 +2786,7 @@ namespace ANSCENTER CkStringBuilder sbResponse; if (!conn->rest.FullRequestBd("PUT", objectPath.c_str(), binData, sbResponse)) { - lastError = conn->rest.lastErrorText(); - ReleaseConnection(std::move(conn)); - return AttemptResult::Transient; + return ClassifyFailedRequest(std::move(conn), lastError); } int statusCode = conn->rest.get_ResponseStatusCode(); @@ -2764,7 +2974,7 @@ namespace ANSCENTER if (!conn->rest.SendReqNoBody("GET", objectPath.c_str())) { _logger.LogError("ANSAWSS3::DownloadFile - Request failed", conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return false; } @@ -2773,7 +2983,7 @@ namespace ANSCENTER if (responseStatusCode < 0) { _logger.LogError("ANSAWSS3::DownloadFile - Failed to read response header", conn->rest.lastErrorText(), __FILE__, __LINE__); - ReleaseConnection(std::move(conn)); + DiscardConnection(std::move(conn)); // transport failed — do not pool a suspect socket return false; } diff --git a/modules/ANSUtilities/ANSUtilities.h b/modules/ANSUtilities/ANSUtilities.h index b7e056c..c8bb2ae 100644 --- a/modules/ANSUtilities/ANSUtilities.h +++ b/modules/ANSUtilities/ANSUtilities.h @@ -17,6 +17,7 @@ #include #include #include +#include #include namespace ANSCENTER { @@ -214,6 +215,19 @@ namespace ANSCENTER { CkRest rest; CkSocket socket; CkAuthAws authAws; + + // When this connection last completed a request successfully. A socket + // that a NAT/firewall silently killed while idle costs a full + // IdleTimeoutMs to discover, so anything idle longer than + // kMaxPooledIdleMs is dropped rather than handed out again. + std::chrono::steady_clock::time_point lastUsed{ std::chrono::steady_clock::now() }; + + // Value of ANSAWSS3::_configGen when this connection was built. + // A mismatch means it predates a Connect / SetAuthentication / + // SetServerProxy call — e.g. the background RetryLoop finished a + // connect just after SetAuthentication cleared the pool — so its + // endpoint, proxy or SigV4 state is stale and it must not be reused. + uint64_t configGen{ 0 }; }; class ANSULT_API ANSAWSS3 { @@ -252,6 +266,24 @@ namespace ANSCENTER { std::atomic _connectTimeoutMs{ 10000 }; // 10 s std::atomic _idleTimeoutMs { 20000 }; // 20 s + // Shorter idle timeout used for the FIRST request on a connection + // taken from the pool. A pooled socket may have been silently + // dropped by a middlebox since it was last used; without this the + // caller pays the full _idleTimeoutMs (20 s by default) just to + // discover that. A fresh connection still gets the full + // _idleTimeoutMs, so the retry after a stale-socket failure is not + // penalised. Derived from _idleTimeoutMs by SetTimeouts(). + std::atomic _reusedIdleTimeoutMs{ 5000 }; // 5 s + + // Bumped by Connect / SetAuthentication / SetServerProxy. Stamped + // into every S3Connection at creation; see S3Connection::configGen. + std::atomic _configGen{ 1 }; + + // A pooled connection idle longer than this is closed instead of + // reused. Kept below the ~20 s after which S3 (and most NAT + // devices) stop caring about an idle keep-alive flow. + static constexpr int kMaxPooledIdleMs = 15000; + // Proxy settings std::string _proxyHost; int _proxyPort{ 0 }; @@ -265,7 +297,14 @@ namespace ANSCENTER { // Used by TryConnect's first attempt, which is allowed to fail for path-style/MinIO servers. std::unique_ptr CreateConnection(bool logOnFailure = true); std::unique_ptr AcquireConnection(); + // Return a healthy connection to the pool for reuse. void ReleaseConnection(std::unique_ptr conn); + // Close and destroy a connection instead of pooling it. Use on any + // TRANSPORT failure (send/recv failed, timeout): that socket is + // suspect, and putting it back means the next worker inherits the + // same stall. An HTTP error status is NOT a transport failure — the + // server answered — so those still go through ReleaseConnection. + void DiscardConnection(std::unique_ptr conn); std::string ExtractFileName(const std::string& filePath); std::string GetContentType(const std::string& filePath); @@ -279,8 +318,34 @@ namespace ANSCENTER { // The lambda should write a short human-readable reason into // `lastError` on any non-Success outcome so it can be logged. enum class AttemptResult { Success, Transient, Permanent }; + // Record the outcome of the current call for the calling thread. + static void SetCallStatus(int status); + // Shared licence / unlock / connected check for the upload entry + // points. Logs the reason and sets the per-thread call status + // (permanent for a licence problem, not-connected for a link that + // has not come up yet). Returns false if the call must not proceed. + bool CheckUploadPreconditions(const std::string& opName); + + // Decide what a failed Chilkat request means, and dispose of the + // connection accordingly. Chilkat reports failure for an HTTP 4xx + // delivered during an Expect: 100-continue exchange, so "the call + // returned false" does NOT imply the network failed — a 404 + // NoSuchBucket arrives this way. Retrying those wastes the whole + // retry budget and reports the wrong class to the caller. + // 4xx (except 408/429) -> Permanent, socket healthy -> pooled + // anything else -> Transient, socket suspect -> discarded + // Takes ownership of `conn`, and rewrites `lastError` into one + // readable line. + AttemptResult ClassifyFailedRequest(std::unique_ptr conn, + std::string& lastError); static constexpr int kUploadMaxAttempts = 20; static constexpr int kUploadRetryDelayMs = 100; + // Wall-clock budget for one upload call, across all attempts. The + // attempt cap alone is not a bound on time: 20 attempts that each + // hit a 20 s idle timeout would block the caller for ~400 s. A new + // attempt is only started while inside this budget, so the worst + // case is the budget plus one in-flight attempt. + static constexpr int kUploadDeadlineMs = 30000; bool UploadWithRetry( const std::string& opName, const std::function& attemptFn); @@ -294,6 +359,37 @@ namespace ANSCENTER { bool TryConnect(bool& awsPath); public: + // ---- Call status codes ------------------------------------------- + // Every upload entry point records one of these for the CALLING + // THREAD, so the C exports can tell the caller whether a retry is + // worth attempting without changing any function signature. + // + // These EXTEND the codes the C wrappers already return; they do not + // redefine any of them. Full set seen by a LabVIEW caller: + // 1 success + // 0 null / invalid argument passed to the export + // -1 handle not in the registry + // -2 upload failed, reason not classified (fallback) + // -3 std::exception escaped + // -4 unknown exception escaped + // -20 permanent: bad argument, licence, bucket, or HTTP 4xx + // -21 not connected yet / reconnect in progress + // -22 transient: network, timeout or 5xx after internal retries + // + // Retry rule for the caller: retry on -21 and -22 (and -2/-3/-4 if + // it wants to be generous); do NOT retry on -20 or 0 — those need a + // configuration or code fix. Success stays the only positive value, + // so both `rc == 1` and `rc != 1` remain correct tests. + static constexpr int kCallSuccess = 1; + static constexpr int kCallPermanentError = -20; + static constexpr int kCallNotConnected = -21; + static constexpr int kCallTransientError = -22; + + // Status of the most recent upload call made ON THIS THREAD. + // Thread-local, so concurrent workers sharing one handle never see + // each other's result. Read it immediately after the call. + static int LastCallStatus(); + ANSAWSS3(); ~ANSAWSS3(); [[nodiscard]] bool Initialize(const std::string& licenseKey); diff --git a/modules/ANSUtilities/dllmain.cpp b/modules/ANSUtilities/dllmain.cpp index b14524f..a7f65a9 100644 --- a/modules/ANSUtilities/dllmain.cpp +++ b/modules/ANSUtilities/dllmain.cpp @@ -209,6 +209,20 @@ public: AWSHandleGuard& operator=(const AWSHandleGuard&) = delete; }; +// Failure code for an upload whose C++ call returned false. ANSAWSS3 records +// WHY on the calling thread, which is what lets the caller decide whether a +// retry is worthwhile: +// -20 permanent — bad argument / licence / bucket / HTTP 4xx: do not retry +// -21 not connected — link not up yet, background retry running: retry later +// -22 transient — network/timeout/5xx after internal retries: retry +// Falls back to the historical -2 if no status was recorded. Never returns a +// success code, so `rc == 1` remains an exact success test. +static int AwsUploadFailureCode() { + const int status = ANSCENTER::ANSAWSS3::LastCallStatus(); + if (status == ANSCENTER::ANSAWSS3::kCallSuccess) return -2; + return status; +} + BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved @@ -1910,7 +1924,7 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle(ANSCENTER::ANSAWSS3** Hand return 0; } AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadTextData(bucketName, textDataPath, outPath)) { @@ -1922,13 +1936,13 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle(ANSCENTER::ANSAWSS3** Hand } return 1; } - else return 0; + else return AwsUploadFailureCode(); } catch (std::exception& e) { - return 0; + return -3; } catch (...) { - return 0; + return -4; } } extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, LStrHandle uploadedFilePath) { @@ -1936,7 +1950,7 @@ extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Ha return 0; } AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadBinaryData(bucketName, binaryDataPath, outPath)) { @@ -1948,13 +1962,13 @@ extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Ha } return 1; } - else return 0; + else return AwsUploadFailureCode(); } catch (std::exception& e) { - return 0; + return -3; } catch (...) { - return 0; + return -4; } } extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, LStrHandle uploadedFilePath) { @@ -1962,7 +1976,7 @@ extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3* return 0; } AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadFileStream(bucketName, fileDataPath, outPath)) { @@ -1974,13 +1988,13 @@ extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3* } return 1; } - else return 0; + else return AwsUploadFailureCode(); } catch (std::exception& e) { - return 0; + return -3; } catch (...) { - return 0; + return -4; } } extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, int fileSize, LStrHandle uploadedFilePath) { @@ -1988,7 +2002,7 @@ extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** return 0; } AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadMultipartData(bucketName, fileDataPath, outPath, fileSize)) { @@ -2000,13 +2014,13 @@ extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** } return 1; } - else return 0; + else return AwsUploadFailureCode(); } catch (std::exception& e) { - return 0; + return -3; } catch (...) { - return 0; + return -4; } } extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* objectName, const char* savedFilePath) { @@ -2043,7 +2057,7 @@ extern "C" ANSULT_API int UploadJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3** Han } return 1; } - else return -2;// failed upload + else return AwsUploadFailureCode(); } catch (std::exception& e) { return -3;//excception @@ -2088,9 +2102,10 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3 return 1; } else { - ANS_DBG("ANSAWS", "Upload FAILED (-2): file=%s, UploadPrefixJpegImage returned false", - fileName ? fileName : "(null)"); - return -2; + const int rc = AwsUploadFailureCode(); + ANS_DBG("ANSAWS", "Upload FAILED (%d): file=%s, UploadPrefixJpegImage returned false", + rc, fileName ? fileName : "(null)"); + return rc; } } catch (std::exception& e) { @@ -2159,7 +2174,7 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle(ANSCENTER::ANSA return 0; } AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, outPath, fileSize)) { @@ -2171,13 +2186,13 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle(ANSCENTER::ANSA } return 1; } - else return 0; + else return AwsUploadFailureCode(); } catch (std::exception& e) { - return 0; + return -3; } catch (...) { - return 0; + return -4; } } extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, LStrHandle uploadedFilePath) { @@ -2185,7 +2200,7 @@ extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS return 0; } AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, outPath)) { @@ -2197,79 +2212,79 @@ extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS } return 1; } - else return 0; + else return AwsUploadFailureCode(); } catch (std::exception& e) { - return 0; + return -3; } catch (...) { - return 0; + return -4; } } // ── C++ test APIs (std::string& instead of LStrHandle) ── extern "C" ANSULT_API int UploadTextDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* textDataPath, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || textDataPath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadTextData(bucketName, textDataPath, uploadedFilePath) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadTextData(bucketName, textDataPath, uploadedFilePath) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || binaryDataPath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadBinaryData(bucketName, binaryDataPath, uploadedFilePath) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadBinaryData(bucketName, binaryDataPath, uploadedFilePath) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadFileStream(bucketName, fileDataPath, uploadedFilePath) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadFileStream(bucketName, fileDataPath, uploadedFilePath) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, int fileSize, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadMultipartData(bucketName, fileDataPath, uploadedFilePath, fileSize) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadMultipartData(bucketName, fileDataPath, uploadedFilePath, fileSize) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, uploadedFilePath) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, uploadedFilePath) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, uploadedFilePath, fileSize) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, uploadedFilePath, fileSize) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(*Handle)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { - return guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0; - } catch (...) { return 0; } + return guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : AwsUploadFailureCode(); + } catch (...) { return -4; } } extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* objectName, int validDays, std::string& presignedURL) { if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || objectName == nullptr) return 0; @@ -2767,7 +2782,7 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle_V2(uint64_t handleVal, cons auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || textDataPath == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadTextData(bucketName, textDataPath, outPath)) { @@ -2779,17 +2794,17 @@ extern "C" ANSULT_API int UploadTextDataANSAWSHandle_V2(uint64_t handleVal, cons } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* binaryDataPath, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || binaryDataPath == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadBinaryData(bucketName, binaryDataPath, outPath)) { @@ -2801,17 +2816,17 @@ extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_V2(uint64_t handleVal, co } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* fileDataPath, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadFileStream(bucketName, fileDataPath, outPath)) { @@ -2823,17 +2838,17 @@ extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_V2(uint64_t handleVal } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* fileDataPath, int fileSize, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadMultipartData(bucketName, fileDataPath, outPath, fileSize)) { @@ -2845,17 +2860,17 @@ extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_V2(uint64_t handleVal, } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, outPath)) { @@ -2867,17 +2882,17 @@ extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_V2(uint64_t handleVal, con } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, outPath)) { @@ -2889,10 +2904,10 @@ extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_V2(uint64_t handleVa } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int GeneratePresignedURLANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* objectName, int validDays, LStrHandle presignedURL) { @@ -2921,7 +2936,7 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_V2(uint64_t hand auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0 || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, outPath, fileSize)) { @@ -2933,17 +2948,17 @@ extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_V2(uint64_t hand } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, LStrHandle uploadedFilePath) { auto* _v2h = reinterpret_cast(handleVal); if (!_v2h) return 0; if (bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr || uploadedFilePath == nullptr) return 0; AWSHandleGuard guard(AcquireAWSHandle(_v2h)); - if (!guard) return 0; + if (!guard) return -1; // handle not in registry try { std::string outPath; if (guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, outPath)) { @@ -2955,10 +2970,10 @@ extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_V2(uint64_t handleV } return 1; } - else return 0; + else return AwsUploadFailureCode(); } - catch (std::exception& e) { return 0; } - catch (...) { return 0; } + catch (std::exception& e) { return -3; } + catch (...) { return -4; } } extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* objectName, const char* savedFilePath) {