- metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());
- assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
- uint32_t size = ntohl(hdr->size);
- uint32_t flags = ntohl(hdr->flags);
-
+ metacube2_block_header hdr;
+ memcpy(&hdr, pending_data.data(), sizeof(hdr));
+ assert(memcmp(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync)) == 0);
+ uint32_t size = ntohl(hdr.size);
+ uint16_t flags = ntohs(hdr.flags);
+ uint16_t expected_csum = metacube2_compute_crc(&hdr);
+
+ if (expected_csum != ntohs(hdr.csum)) {
+ log(WARNING, "[%s] Metacube checksum failed (expected 0x%x, got 0x%x), "
+ "not reading block claiming to be %d bytes (flags=%x).",
+ url.c_str(), expected_csum, ntohs(hdr.csum),
+ size, flags);
+
+ // Drop only the first byte, and let the rest of the code handle resync.
+ pending_data.erase(pending_data.begin(), pending_data.begin() + 1);
+ has_metacube_header = false;
+ continue;
+ }