Commit 1b769bce authored by Adrien Béraud's avatar Adrien Béraud Committed by Philippe Gorley

ringaccount: protect pending messages access

Change-Id: If2f26577d1e48812bb0b49e00bcd716e5b73fa7f
Reviewed-by: Philippe Gorley's avatarPhilippe Gorley <philippe.gorley@savoirfairelinux.com>
parent d3e94622
...@@ -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) {
auto res = treatedMessages_.insert(v.id); {
if (!res.second) std::lock_guard<std::mutex> lock(messageMutex_);
return true; auto res = treatedMessages_.insert(v.id);
if (!res.second)
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)
{ {
auto e = this_->sentMessages_.emplace(token, PendingMessage {}); {
e.first->second.to = dev; std::lock_guard<std::mutex> lock(this_->messageMutex_);
auto e = this_->sentMessages_.emplace(token, PendingMessage {});
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,19 +3412,22 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string, ...@@ -3398,19 +3412,22 @@ 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;
auto e = this_.sentMessages_.find(msg.id);
if (e == this_.sentMessages_.end() or e->second.to != msg.from) {
RING_DBG() << "[Account " << this_.getAccountID() << "] [message " << token << "] Message not found";
return true;
}
this_.sentMessages_.erase(e);
RING_DBG() << "[Account " << this_.getAccountID() << "] [message " << token << "] Received text message reply";
// add treated message {
auto res = this_.treatedMessages_.insert(msg.id); std::lock_guard<std::mutex> lock(this_.messageMutex_);
if (!res.second) auto e = this_.sentMessages_.find(msg.id);
return true; if (e == this_.sentMessages_.end() or e->second.to != msg.from) {
RING_DBG() << "[Account " << this_.getAccountID() << "] [message " << token << "] Message not found";
return true;
}
this_.sentMessages_.erase(e);
RING_DBG() << "[Account " << this_.getAccountID() << "] [message " << token << "] Received text message reply";
// add treated message
auto res = this_.treatedMessages_.insert(msg.id);
if (!res.second)
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_ {};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment