From 9197a6118d810356263e92cbb48c406f8e3d3a05 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 1 May 2018 14:57:29 +0200 Subject: Update ntp dependency with bug fix, make error messages better --- src/Database.cpp | 26 ++++++++++++++++++++++---- src/DatabaseStorage.cpp | 8 ++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp index 8cca298..85e56d3 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -521,8 +521,16 @@ namespace odhtdb } u64 creationDate = deserializer.extract(); - if(creationDate > getSyncedTimestampUtc().getCombined()) - throw sibs::DeserializeException("Packet is from the future"); + auto currentTimestamp = getSyncedTimestampUtc(); + if(creationDate > currentTimestamp.getCombined()) + { + auto creationDateTimestamp = ntp::NtpTimestamp::fromCombined(creationDate); + string errMsg = "Packet is from the future. Packet creation time: "; + errMsg += to_string((double)creationDateTimestamp.seconds + creationDateTimestamp.getFractionAsSeconds()); + errMsg += ", current time: "; + errMsg += to_string((double)currentTimestamp.seconds + currentTimestamp.getFractionAsSeconds()); + throw sibs::DeserializeException(errMsg); + } char creatorPublicKeyRaw[PUBLIC_KEY_NUM_BYTES]; deserializer.extract((u8*)creatorPublicKeyRaw, PUBLIC_KEY_NUM_BYTES); @@ -562,8 +570,16 @@ namespace odhtdb } u64 creationDate = deserializerUnsigned.extract(); - if(creationDate > getSyncedTimestampUtc().getCombined()) - throw sibs::DeserializeException("Packet is from the future"); + auto currentTimestamp = getSyncedTimestampUtc(); + if(creationDate > currentTimestamp.getCombined()) + { + auto creationDateTimestamp = ntp::NtpTimestamp::fromCombined(creationDate); + string errMsg = "Packet is from the future. Packet creation time: "; + errMsg += to_string((double)creationDateTimestamp.seconds + creationDateTimestamp.getFractionAsSeconds()); + errMsg += ", current time: "; + errMsg += to_string((double)currentTimestamp.seconds + currentTimestamp.getFractionAsSeconds()); + throw sibs::DeserializeException(errMsg); + } DatabaseOperation operation = deserializerUnsigned.extract(); #if 0 @@ -691,6 +707,8 @@ namespace odhtdb Log::debug("Got add data"); try { + if(databaseStorage.getDataById(requestDataHash)) + throw DatabaseStorageAlreadyExists("Add data request hash is equal to hash already in storage (duplicate data?)"); deserializeAddRequest(value, requestDataHash, nodeHash, encryptionKey); //Log::debug("Got add object, timestamp: %zu", addObject.timestamp); } diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index 233f7c6..9c64a86 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -822,6 +822,14 @@ namespace odhtdb return localUsers; } + DatabaseStorageObject* DatabaseStorage::getDataById(const Hash &dataHash) + { + auto storageIt = storedDataHash.find(dataHash); + if(storageIt != storedDataHash.end()) + return storageIt->second; + return nullptr; + } + std::pair> DatabaseStorage::getNodeDecryptionKey(const Hash &nodeHash) { auto nodeDecryptionKeyIt = nodeDecryptionKeyMap.find(nodeHash); -- cgit v1.2.3