1 /*
2  *  Copyright (C) 2014-2019 Savoir-faire Linux Inc.
3  *  Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4  *              Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 
21 #include "node.h"
22 #include "request.h"
23 #include "rng.h"
24 
25 #include <sstream>
26 
27 namespace dht {
28 
29 constexpr std::chrono::minutes Node::NODE_EXPIRE_TIME;
30 constexpr std::chrono::minutes Node::NODE_GOOD_TIME;
31 constexpr std::chrono::seconds Node::MAX_RESPONSE_TIME;
32 
Node(const InfoHash & id,const SockAddr & addr,bool client)33 Node::Node(const InfoHash& id, const SockAddr& addr, bool client)
34 : id(id), addr(addr), is_client(client), sockets_()
35 {
36     thread_local crypto::random_device rd {};
37     transaction_id = std::uniform_int_distribution<Tid>{1}(rd);
38 }
39 
Node(const InfoHash & id,SockAddr && addr,bool client)40 Node::Node(const InfoHash& id, SockAddr&& addr, bool client)
41 : id(id), addr(std::move(addr)), is_client(client), sockets_()
42 {
43     thread_local crypto::random_device rd {};
44     transaction_id = std::uniform_int_distribution<Tid>{1}(rd);
45 }
46 
47 /* This is our definition of a known-good node. */
48 bool
isGood(time_point now) const49 Node::isGood(time_point now) const
50 {
51     return not expired_ &&
52         reply_time >= now - NODE_GOOD_TIME &&
53         time >= now - NODE_EXPIRE_TIME;
54 }
55 
56 bool
isPendingMessage() const57 Node::isPendingMessage() const
58 {
59     for (const auto& r : requests_) {
60         if (r.second->pending())
61             return true;
62     }
63     return false;
64 }
65 
66 size_t
getPendingMessageCount() const67 Node::getPendingMessageCount() const
68 {
69     size_t count {0};
70     for (const auto& r : requests_) {
71         if (r.second->pending())
72             count++;
73     }
74     return count;
75 }
76 
77 void
update(const SockAddr & new_addr)78 Node::update(const SockAddr& new_addr)
79 {
80     addr = new_addr;
81 }
82 
83 /** To be called when a message was sent to the node */
84 void
requested(const Sp<net::Request> & req)85 Node::requested(const Sp<net::Request>& req)
86 {
87     auto e = requests_.emplace(req->getTid(), req);
88     if (not e.second and req != e.first->second) {
89         // Should not happen !
90         // Try to handle this scenario as well as we can
91         e.first->second->setExpired();
92         e.first->second = req;
93     }
94 }
95 
96 /** To be called when a message was received from the node.
97  Req should be true if the message was an aswer to a request we made*/
98 void
received(time_point now,const Sp<net::Request> & req)99 Node::received(time_point now, const Sp<net::Request>& req)
100 {
101     time = now;
102     expired_ = false;
103     if (req) {
104         reply_time = now;
105         requests_.erase(req->getTid());
106     }
107 }
108 
109 Sp<net::Request>
getRequest(Tid tid)110 Node::getRequest(Tid tid)
111 {
112     auto it = requests_.find(tid);
113     return it != requests_.end() ? it->second : nullptr;
114 }
115 
116 void
cancelRequest(const Sp<net::Request> & req)117 Node::cancelRequest(const Sp<net::Request>& req)
118 {
119     if (req) {
120         req->cancel();
121         closeSocket(req->closeSocket());
122         requests_.erase(req->getTid());
123     }
124 }
125 
126 void
setExpired()127 Node::setExpired()
128 {
129     expired_ = true;
130     for (auto r : requests_) {
131         r.second->setExpired();
132     }
133     requests_.clear();
134     sockets_.clear();
135 }
136 
137 Tid
openSocket(SocketCb && cb)138 Node::openSocket(SocketCb&& cb)
139 {
140     if (++transaction_id == 0)
141         transaction_id = 1;
142 
143     auto sock = std::make_shared<Socket>(std::move(cb));
144     auto s = sockets_.emplace(transaction_id, std::move(sock));
145     if (not s.second)
146         s.first->second = std::move(sock);
147     return transaction_id;
148 }
149 
150 Sp<Socket>
getSocket(Tid id)151 Node::getSocket(Tid id)
152 {
153     auto it = sockets_.find(id);
154     return it == sockets_.end() ? nullptr : it->second;
155 }
156 
157 void
closeSocket(Tid id)158 Node::closeSocket(Tid id)
159 {
160     if (id) {
161         sockets_.erase(id);
162         //DHT_LOG.w("Closing socket (tid: %d), %lu remaining", socket->id, sockets_.size());
163     }
164 }
165 
166 std::string
toString() const167 Node::toString() const
168 {
169     std::stringstream ss;
170     ss << (*this);
171     return ss.str();
172 }
173 
operator <<(std::ostream & s,const Node & h)174 std::ostream& operator<< (std::ostream& s, const Node& h)
175 {
176     s << h.id << " " << h.addr.toString();
177     return s;
178 }
179 
180 }
181