Skip to content
Snippets Groups Projects
Commit f867eb26 authored by Pierre Nicolas's avatar Pierre Nicolas :joy:
Browse files

namedirectory: ignore lookup result if name already registered

Fix a race condition occurring when doing simultaneously lookup and register.

GitLab: #1012
Change-Id: I9fddebb43a06875a1d027ad4c79edb27a39e5eb5
parent 96684fb5
No related branches found
No related tags found
No related merge requests found
...@@ -168,6 +168,15 @@ NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb) ...@@ -168,6 +168,15 @@ NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
request->add_on_done_callback( request->add_on_done_callback(
[this, cb = std::move(cb), addr](const dht::http::Response& response) { [this, cb = std::move(cb), addr](const dht::http::Response& response) {
if (response.status_code >= 400 && response.status_code < 500) { if (response.status_code >= 400 && response.status_code < 500) {
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); cb("", Response::notFound);
} else if (response.status_code != 200) { } else if (response.status_code != 200) {
JAMI_ERR("Address lookup for %s failed with code=%i", JAMI_ERR("Address lookup for %s failed with code=%i",
...@@ -356,6 +365,14 @@ NameDirectory::registerName(const std::string& addr, ...@@ -356,6 +365,14 @@ NameDirectory::registerName(const std::string& addr,
cb(RegistrationResponse::alreadyTaken, name); cb(RegistrationResponse::alreadyTaken, name);
return; return;
} }
{
std::lock_guard l(cacheLock_);
if (isRegisteringName_) {
cb(RegistrationResponse::error, name);
return;
}
isRegisteringName_ = true;
}
std::string body = fmt::format("{{\"addr\":\"{}\",\"owner\":\"{}\",\"signature\":\"{}\",\"publickey\":\"{}\"}}", std::string body = fmt::format("{{\"addr\":\"{}\",\"owner\":\"{}\",\"signature\":\"{}\",\"publickey\":\"{}\"}}",
addr, addr,
owner, owner,
...@@ -373,6 +390,10 @@ NameDirectory::registerName(const std::string& addr, ...@@ -373,6 +390,10 @@ NameDirectory::registerName(const std::string& addr,
request->add_on_done_callback( request->add_on_done_callback(
[this, name, addr, cb = std::move(cb)](const dht::http::Response& response) { [this, name, addr, cb = std::move(cb)](const dht::http::Response& response) {
{
std::lock_guard l(cacheLock_);
isRegisteringName_ = false;
}
if (response.status_code == 400) { if (response.status_code == 400) {
cb(RegistrationResponse::incompleteRequest, name); cb(RegistrationResponse::incompleteRequest, name);
JAMI_ERR("RegistrationResponse::incompleteRequest"); JAMI_ERR("RegistrationResponse::incompleteRequest");
...@@ -411,6 +432,8 @@ NameDirectory::registerName(const std::string& addr, ...@@ -411,6 +432,8 @@ NameDirectory::registerName(const std::string& addr,
success ? "success" : "failure"); success ? "success" : "failure");
if (success) { if (success) {
std::lock_guard l(cacheLock_); std::lock_guard l(cacheLock_);
registeredAddr_ = addr;
registeredName_ = name;
addrCache_.emplace(name, addr); addrCache_.emplace(name, addr);
nameCache_.emplace(addr, name); nameCache_.emplace(addr, name);
} }
......
...@@ -117,6 +117,10 @@ private: ...@@ -117,6 +117,10 @@ private:
std::mutex requestsMtx_ {}; std::mutex requestsMtx_ {};
std::set<std::shared_ptr<dht::http::Request>> requests_; 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> nameCache_ {};
std::map<std::string, std::string> addrCache_ {}; std::map<std::string, std::string> addrCache_ {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment