Skip to content
Snippets Groups Projects
Commit 1b769bce authored by Adrien Béraud's avatar Adrien Béraud Committed by Philippe Gorley
Browse files

ringaccount: protect pending messages access


Change-Id: If2f26577d1e48812bb0b49e00bcd716e5b73fa7f
Reviewed-by: default avatarPhilippe Gorley <philippe.gorley@savoirfairelinux.com>
parent d3e94622
Branches
No related tags found
No related merge requests found
...@@ -2279,9 +2279,12 @@ RingAccount::doRegister_() ...@@ -2279,9 +2279,12 @@ RingAccount::doRegister_()
dht_.listen<dht::ImMessage>( dht_.listen<dht::ImMessage>(
inboxDeviceKey, inboxDeviceKey,
[this,inboxDeviceKey](dht::ImMessage&& v) { [this,inboxDeviceKey](dht::ImMessage&& v) {
{
std::lock_guard<std::mutex> lock(messageMutex_);
auto res = treatedMessages_.insert(v.id); auto res = treatedMessages_.insert(v.id);
if (!res.second) if (!res.second)
return true; return true;
}
saveTreatedMessages(); saveTreatedMessages();
onPeerMessage(v.from, [this, v, inboxDeviceKey](const std::shared_ptr<dht::crypto::Certificate>&, onPeerMessage(v.from, [this, v, inboxDeviceKey](const std::shared_ptr<dht::crypto::Certificate>&,
const dht::InfoHash& peer_account) const dht::InfoHash& peer_account)
...@@ -2689,19 +2692,27 @@ RingAccount::saveTreatedCalls() const ...@@ -2689,19 +2692,27 @@ RingAccount::saveTreatedCalls() const
void void
RingAccount::loadTreatedMessages() RingAccount::loadTreatedMessages()
{ {
std::lock_guard<std::mutex> lock(messageMutex_);
treatedMessages_ = loadIdList(cachePath_+DIR_SEPARATOR_STR "treatedMessages"); treatedMessages_ = loadIdList(cachePath_+DIR_SEPARATOR_STR "treatedMessages");
} }
void void
RingAccount::saveTreatedMessages() const RingAccount::saveTreatedMessages() const
{ {
fileutils::check_dir(cachePath_.c_str()); ThreadPool::instance().run([w = weak()](){
saveIdList(cachePath_+DIR_SEPARATOR_STR "treatedMessages", treatedMessages_); if (auto sthis = w.lock()) {
auto& this_ = *sthis;
std::lock_guard<std::mutex> lock(this_.messageMutex_);
fileutils::check_dir(this_.cachePath_.c_str());
saveIdList(this_.cachePath_+DIR_SEPARATOR_STR "treatedMessages", this_.treatedMessages_);
}
});
} }
bool bool
RingAccount::isMessageTreated(unsigned int id) RingAccount::isMessageTreated(unsigned int id)
{ {
std::lock_guard<std::mutex> lock(messageMutex_);
auto res = treatedMessages_.insert(id); auto res = treatedMessages_.insert(id);
if (res.second) { if (res.second) {
saveTreatedMessages(); saveTreatedMessages();
...@@ -3387,8 +3398,11 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string, ...@@ -3387,8 +3398,11 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string,
// Find listening devices for this account // Find listening devices for this account
forEachDevice(toH, [confirm,token,payloads,now](const std::shared_ptr<RingAccount>& this_, const dht::InfoHash& dev) forEachDevice(toH, [confirm,token,payloads,now](const std::shared_ptr<RingAccount>& this_, const dht::InfoHash& dev)
{ {
{
std::lock_guard<std::mutex> lock(this_->messageMutex_);
auto e = this_->sentMessages_.emplace(token, PendingMessage {}); auto e = this_->sentMessages_.emplace(token, PendingMessage {});
e.first->second.to = dev; e.first->second.to = dev;
}
auto h = dht::InfoHash::get("inbox:"+dev.toString()); auto h = dht::InfoHash::get("inbox:"+dev.toString());
std::weak_ptr<RingAccount> w = this_; std::weak_ptr<RingAccount> w = this_;
...@@ -3398,6 +3412,9 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string, ...@@ -3398,6 +3412,9 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string,
// check expected message confirmation // check expected message confirmation
if (msg.id != token) if (msg.id != token)
return true; return true;
{
std::lock_guard<std::mutex> lock(this_.messageMutex_);
auto e = this_.sentMessages_.find(msg.id); auto e = this_.sentMessages_.find(msg.id);
if (e == this_.sentMessages_.end() or e->second.to != msg.from) { if (e == this_.sentMessages_.end() or e->second.to != msg.from) {
RING_DBG() << "[Account " << this_.getAccountID() << "] [message " << token << "] Message not found"; RING_DBG() << "[Account " << this_.getAccountID() << "] [message " << token << "] Message not found";
...@@ -3410,7 +3427,7 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string, ...@@ -3410,7 +3427,7 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string,
auto res = this_.treatedMessages_.insert(msg.id); auto res = this_.treatedMessages_.insert(msg.id);
if (!res.second) if (!res.second)
return true; return true;
}
this_.saveTreatedMessages(); this_.saveTreatedMessages();
// report message as confirmed received // report message as confirmed received
......
...@@ -519,6 +519,7 @@ class RingAccount : public SIPAccountBase { ...@@ -519,6 +519,7 @@ class RingAccount : public SIPAccountBase {
std::set<dht::Value::Id> treatedCalls_ {}; std::set<dht::Value::Id> treatedCalls_ {};
mutable std::mutex callsMutex_ {}; mutable std::mutex callsMutex_ {};
mutable std::mutex messageMutex_ {};
std::map<dht::Value::Id, PendingMessage> sentMessages_; std::map<dht::Value::Id, PendingMessage> sentMessages_;
std::set<dht::Value::Id> treatedMessages_ {}; std::set<dht::Value::Id> treatedMessages_ {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment