diff --git a/src/jamidht/namedirectory.cpp b/src/jamidht/namedirectory.cpp index 2b95521e5160f4fb2a9cda9fd5a2d441e26d4550..9ec7fdd252acbfafef49e96ca4a33e818523780f 100644 --- a/src/jamidht/namedirectory.cpp +++ b/src/jamidht/namedirectory.cpp @@ -168,7 +168,16 @@ NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb) request->add_on_done_callback( [this, cb = std::move(cb), addr](const dht::http::Response& response) { if (response.status_code >= 400 && response.status_code < 500) { - cb("", Response::notFound); + std::string regName, regAddr; + { + std::lock_guard l(cacheLock_); + regName = registeredName_; + regAddr = registeredAddr_; + } + if (not regAddr.empty() and addr == regAddr) + cb(regName, Response::found); + else + cb("", Response::notFound); } else if (response.status_code != 200) { JAMI_ERR("Address lookup for %s failed with code=%i", addr.c_str(), @@ -356,6 +365,14 @@ NameDirectory::registerName(const std::string& addr, cb(RegistrationResponse::alreadyTaken, name); return; } + { + std::lock_guard l(cacheLock_); + if (isRegisteringName_) { + cb(RegistrationResponse::error, name); + return; + } + isRegisteringName_ = true; + } std::string body = fmt::format("{{\"addr\":\"{}\",\"owner\":\"{}\",\"signature\":\"{}\",\"publickey\":\"{}\"}}", addr, owner, @@ -373,6 +390,10 @@ NameDirectory::registerName(const std::string& addr, request->add_on_done_callback( [this, name, addr, cb = std::move(cb)](const dht::http::Response& response) { + { + std::lock_guard l(cacheLock_); + isRegisteringName_ = false; + } if (response.status_code == 400) { cb(RegistrationResponse::incompleteRequest, name); JAMI_ERR("RegistrationResponse::incompleteRequest"); @@ -411,6 +432,8 @@ NameDirectory::registerName(const std::string& addr, success ? "success" : "failure"); if (success) { std::lock_guard l(cacheLock_); + registeredAddr_ = addr; + registeredName_ = name; addrCache_.emplace(name, addr); nameCache_.emplace(addr, name); } diff --git a/src/jamidht/namedirectory.h b/src/jamidht/namedirectory.h index 2d7a862ab54c744e4b4ba74a6155308a20bbca39..a20c6b95a5a478e52455755fa6636444312c643e 100644 --- a/src/jamidht/namedirectory.h +++ b/src/jamidht/namedirectory.h @@ -117,6 +117,10 @@ private: std::mutex requestsMtx_ {}; std::set<std::shared_ptr<dht::http::Request>> requests_; + bool isRegisteringName_ {false}; + std::string registeredAddr_ {}; + std::string registeredName_ {}; + std::map<std::string, std::string> nameCache_ {}; std::map<std::string, std::string> addrCache_ {};