1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #ifndef D_PEER_H
36 #define D_PEER_H
37 
38 #include "common.h"
39 
40 #include <cassert>
41 #include <string>
42 #include <set>
43 #include <algorithm>
44 
45 #include "TimerA2.h"
46 #include "BtConstants.h"
47 #include "PeerStat.h"
48 #include "a2functional.h"
49 #include "Command.h"
50 
51 namespace aria2 {
52 
53 class PeerSessionResource;
54 class BtMessageDispatcher;
55 
56 class Peer {
57 private:
58   std::string ipaddr_;
59   // TCP port of the other end of communication.  If incoming_ is
60   // true, then this port is not a port the peer is listening to and
61   // we cannot connect to it.
62   uint16_t port_;
63   // This is the port number passed in the constructor arguments. This
64   // is used to distinguish peer identity.
65   uint16_t origPort_;
66 
67   cuid_t cuid_;
68 
69   unsigned char peerId_[PEER_ID_LENGTH];
70 
71   Timer firstContactTime_;
72 
73   Timer dropStartTime_;
74 
75   bool seeder_;
76 
77   std::unique_ptr<PeerSessionResource> res_;
78 
79   // If true, port is assumed not to be a listening port.
80   bool incoming_;
81 
82   // If true, this peer is from local network.
83   bool localPeer_;
84 
85   // If true, this peer is disconnected gracefully.
86   bool disconnectedGracefully_;
87 
88   // Before calling updateSeeder(),  make sure that
89   // allocateSessionResource() is called and res_ is created.
90   // Otherwise assertion fails.
91   void updateSeeder();
92 
93 public:
94   Peer(std::string ipaddr, uint16_t port, bool incoming = false);
95 
96   ~Peer();
97 
getIPAddress()98   const std::string& getIPAddress() const { return ipaddr_; }
99 
getPort()100   uint16_t getPort() const { return port_; }
101 
setPort(uint16_t port)102   void setPort(uint16_t port) { port_ = port; }
103 
getOrigPort()104   uint16_t getOrigPort() const { return origPort_; }
105 
106   void usedBy(cuid_t cuid);
107 
usedBy()108   cuid_t usedBy() const { return cuid_; }
109 
unused()110   bool unused() const { return cuid_ == 0; }
111 
112   // Returns true iff res_ != 0.
isActive()113   bool isActive() const { return res_.get() != nullptr; }
114 
115   void setPeerId(const unsigned char* peerId);
116 
getPeerId()117   const unsigned char* getPeerId() const { return peerId_; }
118 
isSeeder()119   bool isSeeder() const { return seeder_; }
120 
121   void startDrop();
122 
123   void allocateSessionResource(int32_t pieceLength, int64_t totalLength);
124 
125   void reconfigureSessionResource(int32_t pieceLength, int64_t totalLength);
126 
127   void releaseSessionResource();
128 
getFirstContactTime()129   const Timer& getFirstContactTime() const { return firstContactTime_; }
130 
131   void setFirstContactTime(const Timer& time);
132 
getDropStartTime()133   const Timer& getDropStartTime() const { return dropStartTime_; }
134 
135   // Before calling following member functions,  make sure that
136   // allocateSessionResource() is called and res_ is created.
137   // Otherwise assertion fails.
138 
139   // localhost is choking this peer
140   bool amChoking() const;
141 
142   void amChoking(bool b) const;
143 
144   // localhost is interested in this peer
145   bool amInterested() const;
146 
147   void amInterested(bool b) const;
148 
149   // this peer is choking localhost
150   bool peerChoking() const;
151 
152   void peerChoking(bool b) const;
153 
154   // this peer is interested in localhost
155   bool peerInterested() const;
156 
157   void peerInterested(bool b);
158 
159   // this peer should be choked
160   bool chokingRequired() const;
161 
162   void chokingRequired(bool b);
163 
164   // this peer is eligible for unchoking optionally.
165   bool optUnchoking() const;
166 
167   void optUnchoking(bool b);
168 
169   // this peer is snubbing.
170   bool snubbing() const;
171 
172   void snubbing(bool b);
173 
174   void updateUploadSpeed(int32_t bytes);
175 
176   void updateUploadLength(int32_t bytes);
177 
178   void updateDownload(int32_t bytes);
179 
180   /**
181    * Returns the transfer rate from localhost to remote host.
182    */
183   int calculateUploadSpeed();
184 
185   /**
186    * Returns the transfer rate from remote host to localhost.
187    */
188   int calculateDownloadSpeed();
189 
190   /**
191    * Returns the number of bytes uploaded to the remote host.
192    */
193   int64_t getSessionUploadLength() const;
194 
195   /**
196    * Returns the number of bytes downloaded from the remote host.
197    */
198   int64_t getSessionDownloadLength() const;
199 
200   void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
201 
202   const unsigned char* getBitfield() const;
203 
204   size_t getBitfieldLength() const;
205 
206   void setAllBitfield();
207 
208   /**
209    * operation = 1: set index-th bit to 1
210    * operation = 0: set index-th bit to 0
211    */
212   void updateBitfield(size_t index, int operation);
213 
214   void setFastExtensionEnabled(bool enabled);
215 
216   bool isFastExtensionEnabled() const;
217 
218   void addPeerAllowedIndex(size_t index);
219 
220   bool isInPeerAllowedIndexSet(size_t index) const;
221 
222   size_t countPeerAllowedIndexSet() const;
223 
224   const std::set<size_t>& getPeerAllowedIndexSet() const;
225 
226   void addAmAllowedIndex(size_t index);
227 
228   bool isInAmAllowedIndexSet(size_t index) const;
229 
230   void setExtendedMessagingEnabled(bool enabled);
231 
232   bool isExtendedMessagingEnabled() const;
233 
234   void setDHTEnabled(bool enabled);
235 
236   bool isDHTEnabled() const;
237 
238   bool shouldBeChoking() const;
239 
240   bool hasPiece(size_t index) const;
241 
242   uint8_t getExtensionMessageID(int key) const;
243 
244   const char* getExtensionName(uint8_t id) const;
245 
246   void setExtension(int key, uint8_t id);
247 
248   const Timer& getLastDownloadUpdate() const;
249 
250   const Timer& getLastAmUnchoking() const;
251 
252   int64_t getCompletedLength() const;
253 
isIncomingPeer()254   bool isIncomingPeer() const { return incoming_; }
255 
256   void setIncomingPeer(bool incoming);
257 
isLocalPeer()258   bool isLocalPeer() const { return localPeer_; }
259 
setLocalPeer(bool flag)260   void setLocalPeer(bool flag) { localPeer_ = flag; }
261 
isDisconnectedGracefully()262   bool isDisconnectedGracefully() const { return disconnectedGracefully_; }
263 
setDisconnectedGracefully(bool f)264   void setDisconnectedGracefully(bool f) { disconnectedGracefully_ = f; }
265 
266   void setBtMessageDispatcher(BtMessageDispatcher* dpt);
267 
268   size_t countOutstandingUpload() const;
269 };
270 
271 template <typename InputIterator>
countSeeder(InputIterator first,InputIterator last)272 size_t countSeeder(InputIterator first, InputIterator last)
273 {
274   size_t res = 0;
275   for (; first != last; ++first) {
276     if ((*first)->isActive() && (*first)->isSeeder()) {
277       ++res;
278     }
279   }
280   return res;
281 }
282 
283 } // namespace aria2
284 
285 #endif // D_PEER_H
286