fix some segfault errors

This commit is contained in:
Simon Désaulniers authored and Adrien Béraud committed 2016-05-02 15:25:15 -04:00
1 parent 24527e921a
commit 8b35e2bc09
4 files changed
+23 -19

No files matched your search

+2 -2
View File
@@ -449,7 +449,7 @@ private:
bool isAnnounced(Value::Id vid, const ValueType& type, time_point now) const {
auto ack = acked.find(vid);
if (ack == acked.end()) {
if (ack == acked.end() or not ack->second) {
return false;
}
return ack->second->reply_time + type.expiration > now;
@@ -462,7 +462,7 @@ private:
}
time_point getAnnounceTime(AnnounceStatusMap::const_iterator ack, const ValueType& type) const {
if (ack == acked.end())
if (ack == acked.end() or not ack->second)
return time_point::min();
return std::max(
ack->second->reply_time + type.expiration - REANNOUNCE_MARGIN,
+1
View File
@@ -101,6 +101,7 @@ public:
*/
struct RequestAnswer {
Blob ntoken;
Value::Id vid;
std::vector<std::shared_ptr<Value>> values;
std::vector<std::shared_ptr<Node>> nodes;
std::vector<std::shared_ptr<Node>> nodes6;
+13 -12
View File
@@ -2075,11 +2075,11 @@ Dht::dumpSearch(const Search& sr, std::ostream& out) const
out << ' ';
} else {
auto& astatus = ack->second;
if (astatus->reply_time + getType(a.value->type).expiration > now)
if (astatus and astatus->reply_time + getType(a.value->type).expiration > now)
out << 'a';
else if (astatus->pending(now))
else if (astatus and astatus->pending(now))
out << 'f';
else if (astatus->expired(now))
else if (astatus and astatus->expired(now))
out << 'x';
else
out << ' ';
@@ -2174,6 +2174,7 @@ Dht::Dht(int s, int s6, Config config)
std::bind(&Dht::onListen, this, _1, _2, _3, _4),
std::bind(&Dht::onAnnounce, this, _1, _2, _3, _4, _5))
{
scheduler.syncTime();
if (s < 0 && s6 < 0)
return;
@@ -2602,16 +2603,15 @@ Dht::onFindNode(std::shared_ptr<Node> node, InfoHash& hash, want_t want)
const auto& now = scheduler.time();
Blob ntoken = makeToken((sockaddr*)&node->ss, false);
std::vector<std::shared_ptr<Node>> nodes, nodes6;
if ((want & WANT4)) {
if (want & WANT4) {
auto tmp = buckets.findClosestNodes(hash, now, TARGET_NODES);
nodes.insert(nodes.begin(), tmp.begin(), tmp.end());
}
if ((want & WANT6)) {
if (want & WANT6) {
auto tmp = buckets6.findClosestNodes(hash, now, TARGET_NODES);
nodes6.insert(nodes6.begin(), tmp.begin(), tmp.end());
}
NetworkEngine::RequestAnswer answer {ntoken, {}, std::move(nodes), std::move(nodes6)};
return answer;
return NetworkEngine::RequestAnswer {ntoken, Value::INVALID_ID, {}, std::move(nodes), std::move(nodes6)};
}
NetworkEngine::RequestAnswer
@@ -2628,6 +2628,7 @@ Dht::onGetValues(std::shared_ptr<Node> node, InfoHash& hash, want_t)
Blob ntoken = makeToken((sockaddr*)&node->ss, false);
answer = new NetworkEngine::RequestAnswer {
ntoken,
0,
{},
buckets.findClosestNodes(hash, now, TARGET_NODES),
buckets6.findClosestNodes(hash, now, TARGET_NODES)
@@ -2795,8 +2796,7 @@ Dht::onAnnounce(std::shared_ptr<Node> node, InfoHash& hash, Blob& token, std::ve
}
void
Dht::onAnnounceDone(std::shared_ptr<NetworkEngine::RequestStatus>,
NetworkEngine::RequestAnswer& answer,
Dht::onAnnounceDone(std::shared_ptr<NetworkEngine::RequestStatus> status, NetworkEngine::RequestAnswer& answer,
std::shared_ptr<Search> sr)
{
const auto& now = scheduler.time();
@@ -2804,17 +2804,18 @@ Dht::onAnnounceDone(std::shared_ptr<NetworkEngine::RequestStatus>,
sr->id.toString().c_str(), sr->af == AF_INET ? '4' : '6',
answer.values.size());
auto& v = answer.values.front();
if (searchSendGetValues(sr))
sr->get_step_time = now;
status->reply_time = scheduler.time();
// If the value was just successfully announced, call the callback
sr->announce.erase(std::remove_if(sr->announce.begin(), sr->announce.end(),
[&](Announce& a) {
if (!a.value || a.value->id != v->id)
if (!a.value || a.value->id != answer.vid)
return false;
auto type = getType(a.value->type);
if (sr->isAnnounced(v->id, type, now)) {
if (sr->isAnnounced(answer.vid, type, now)) {
if (a.callback) {
a.callback(true, sr->getNodes());
a.callback = nullptr;
+7 -5
View File
@@ -183,7 +183,7 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const sockaddr
}
case MessageType::GetValues: {
DHT_LOG.DEBUG("[node %s %s] got 'get' request for %s.",
node->id.toString().c_str(), print_addr(node->ss, node->sslen).c_str(), hash.toString().c_str());
node->id.toString().c_str(), print_addr(node->ss, node->sslen).c_str(), msg.info_hash.toString().c_str());
++in_stats.get;
RequestAnswer answer = onGetValues(node, msg.info_hash, msg.want);
auto nnodes = bufferNodes(from->sa_family, msg.target, msg.want, answer.nodes, answer.nodes6);
@@ -193,7 +193,7 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const sockaddr
case MessageType::AnnounceValue: {
DHT_LOG.DEBUG("[node %s %s] got 'put' request for %s.",
node->id.toString().c_str(), print_addr(node->ss, node->sslen).c_str(),
hash.toString().c_str());
msg.info_hash.toString().c_str());
++in_stats.put;
onAnnounce(node, msg.info_hash, msg.token, msg.values, msg.created);
@@ -207,7 +207,7 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const sockaddr
}
case MessageType::Listen: {
DHT_LOG.DEBUG("[node %s %s] got 'listen' request for %s.",
node->id.toString().c_str(), print_addr(node->ss, node->sslen).c_str(), hash.toString().c_str());
node->id.toString().c_str(), print_addr(node->ss, node->sslen).c_str(), msg.info_hash.toString().c_str());
if (!msg.tid.matches(TransPrefix::LISTEN, &ttid)) {
break;
}
@@ -407,7 +407,7 @@ NetworkEngine::sendGetValues(std::shared_ptr<Node> n, const InfoHash& target, wa
NetworkEngine::RequestAnswer
NetworkEngine::deserializeNodesValues(ParsedMessage& msg) {
RequestAnswer req_a {msg.token, std::move(msg.values), {}, {}};
RequestAnswer req_a {msg.token, 0, std::move(msg.values), {}, {}};
if (msg.nodes4.size() % NODE4_INFO_BUF_LEN != 0 || msg.nodes6.size() % NODE6_INFO_BUF_LEN != 0) {
throw DhtProtocolException {DhtProtocolException::WRONG_NODE_INFO_BUF_LEN};
} else {
@@ -664,7 +664,9 @@ NetworkEngine::sendAnnounceValue(std::shared_ptr<Node> n, const InfoHash& infoha
DHT_LOG.DEBUG("Unknown search or announce!");
} else {
if (on_done) {
on_done(req_status, {});
RequestAnswer answer {};
answer.vid = msg.value_id;
on_done(req_status, std::move(answer));
}
}
},