1 | /* |
---|
2 | * This file Copyright (C) 2007-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: peer-mgr.h 10751 2010-06-14 12:01:50Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef TR_PEER_MGR_H |
---|
18 | #define TR_PEER_MGR_H |
---|
19 | |
---|
20 | #include <inttypes.h> /* uint16_t */ |
---|
21 | |
---|
22 | #ifdef WIN32 |
---|
23 | #include <winsock2.h> /* struct in_addr */ |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "bitfield.h" |
---|
27 | #include "bitset.h" |
---|
28 | #include "history.h" |
---|
29 | #include "net.h" |
---|
30 | #include "peer-common.h" /* struct peer_request */ |
---|
31 | #include "publish.h" /* tr_publisher_tag */ |
---|
32 | |
---|
33 | /** |
---|
34 | * @addtogroup peers Peers |
---|
35 | * @{ |
---|
36 | */ |
---|
37 | |
---|
38 | struct tr_peer_stat; |
---|
39 | struct tr_torrent; |
---|
40 | typedef struct tr_peerMgr tr_peerMgr; |
---|
41 | |
---|
42 | enum |
---|
43 | { |
---|
44 | /* corresponds to ut_pex's added.f flags */ |
---|
45 | ADDED_F_ENCRYPTION_FLAG = 1, |
---|
46 | |
---|
47 | /* corresponds to ut_pex's added.f flags */ |
---|
48 | ADDED_F_SEED_FLAG = 2, |
---|
49 | }; |
---|
50 | |
---|
51 | typedef struct tr_pex |
---|
52 | { |
---|
53 | tr_address addr; |
---|
54 | tr_port port; /* this field is in network byte order */ |
---|
55 | uint8_t flags; |
---|
56 | } |
---|
57 | tr_pex; |
---|
58 | |
---|
59 | |
---|
60 | struct tr_bandwidth; |
---|
61 | struct tr_peerIo; |
---|
62 | struct tr_peermsgs; |
---|
63 | |
---|
64 | enum |
---|
65 | { |
---|
66 | ENCRYPTION_PREFERENCE_UNKNOWN, |
---|
67 | ENCRYPTION_PREFERENCE_YES, |
---|
68 | ENCRYPTION_PREFERENCE_NO |
---|
69 | }; |
---|
70 | |
---|
71 | /* opaque forward declaration */ |
---|
72 | struct peer_atom; |
---|
73 | |
---|
74 | /** |
---|
75 | * State information about a connected peer. |
---|
76 | * |
---|
77 | * @see struct peer_atom |
---|
78 | * @see tr_peermsgs |
---|
79 | */ |
---|
80 | typedef struct tr_peer |
---|
81 | { |
---|
82 | tr_bool peerIsChoked; |
---|
83 | tr_bool peerIsInterested; |
---|
84 | tr_bool clientIsChoked; |
---|
85 | tr_bool clientIsInterested; |
---|
86 | tr_bool doPurge; |
---|
87 | |
---|
88 | /* number of bad pieces they've contributed to */ |
---|
89 | uint8_t strikes; |
---|
90 | |
---|
91 | uint8_t encryption_preference; |
---|
92 | tr_port dht_port; |
---|
93 | |
---|
94 | /* how many requests the peer has made that we haven't responded to yet */ |
---|
95 | int pendingReqsToClient; |
---|
96 | |
---|
97 | /* how many requests we've made and are currently awaiting a response for */ |
---|
98 | int pendingReqsToPeer; |
---|
99 | |
---|
100 | struct tr_peerIo * io; |
---|
101 | struct peer_atom * atom; |
---|
102 | |
---|
103 | struct tr_bitfield * blame; |
---|
104 | struct tr_bitset have; |
---|
105 | |
---|
106 | /** how complete the peer's copy of the torrent is. [0.0...1.0] */ |
---|
107 | float progress; |
---|
108 | |
---|
109 | /* the client name from the `v' string in LTEP's handshake dictionary */ |
---|
110 | char * client; |
---|
111 | |
---|
112 | time_t chokeChangedAt; |
---|
113 | |
---|
114 | time_t lastBlocksAtTime; |
---|
115 | int blocksAt[60]; |
---|
116 | |
---|
117 | time_t lastCancelTime; |
---|
118 | int cancelAt[60]; |
---|
119 | |
---|
120 | tr_recentHistory * blocksSentToClient; |
---|
121 | tr_recentHistory * blocksSentToPeer; |
---|
122 | |
---|
123 | tr_recentHistory * cancelsSentToClient; |
---|
124 | tr_recentHistory * cancelsSentToPeer; |
---|
125 | |
---|
126 | struct tr_peermsgs * msgs; |
---|
127 | tr_publisher_tag msgsTag; |
---|
128 | } |
---|
129 | tr_peer; |
---|
130 | |
---|
131 | const tr_address * tr_peerAddress( const tr_peer * ); |
---|
132 | |
---|
133 | int tr_pexCompare( const void * a, const void * b ); |
---|
134 | |
---|
135 | tr_peerMgr* tr_peerMgrNew( tr_session * ); |
---|
136 | |
---|
137 | void tr_peerMgrFree( tr_peerMgr * manager ); |
---|
138 | |
---|
139 | tr_bool tr_peerMgrPeerIsSeed( const tr_torrent * tor, |
---|
140 | const tr_address * addr ); |
---|
141 | |
---|
142 | void tr_peerMgrGetNextRequests( tr_torrent * torrent, |
---|
143 | tr_peer * peer, |
---|
144 | int numwant, |
---|
145 | tr_block_index_t * setme, |
---|
146 | int * numgot ); |
---|
147 | |
---|
148 | tr_bool tr_peerMgrDidPeerRequest( const tr_torrent * torrent, |
---|
149 | const tr_peer * peer, |
---|
150 | tr_block_index_t block ); |
---|
151 | |
---|
152 | void tr_peerMgrRebuildRequests( tr_torrent * torrent ); |
---|
153 | |
---|
154 | void tr_peerMgrAddIncoming( tr_peerMgr * manager, |
---|
155 | tr_address * addr, |
---|
156 | tr_port port, |
---|
157 | int socket ); |
---|
158 | |
---|
159 | tr_pex * tr_peerMgrCompactToPex( const void * compact, |
---|
160 | size_t compactLen, |
---|
161 | const uint8_t * added_f, |
---|
162 | size_t added_f_len, |
---|
163 | size_t * setme_pex_count ); |
---|
164 | |
---|
165 | tr_pex * tr_peerMgrCompact6ToPex( const void * compact, |
---|
166 | size_t compactLen, |
---|
167 | const uint8_t * added_f, |
---|
168 | size_t added_f_len, |
---|
169 | size_t * pexCount ); |
---|
170 | |
---|
171 | tr_pex * tr_peerMgrArrayToPex( const void * array, |
---|
172 | size_t arrayLen, |
---|
173 | size_t * setme_pex_count ); |
---|
174 | |
---|
175 | /** |
---|
176 | * @param seedProbability [0..100] for likelihood that the peer is a seed; -1 for unknown |
---|
177 | */ |
---|
178 | void tr_peerMgrAddPex( tr_torrent * tor, |
---|
179 | uint8_t from, |
---|
180 | const tr_pex * pex, |
---|
181 | int8_t seedProbability ); |
---|
182 | |
---|
183 | void tr_peerMgrMarkAllAsSeeds( tr_torrent * tor ); |
---|
184 | |
---|
185 | void tr_peerMgrSetBlame( tr_torrent * tor, |
---|
186 | tr_piece_index_t pieceIndex, |
---|
187 | int success ); |
---|
188 | |
---|
189 | enum |
---|
190 | { |
---|
191 | TR_PEERS_CONNECTED, |
---|
192 | TR_PEERS_ALL |
---|
193 | }; |
---|
194 | |
---|
195 | int tr_peerMgrGetPeers( tr_torrent * tor, |
---|
196 | tr_pex ** setme_pex, |
---|
197 | uint8_t address_type, |
---|
198 | uint8_t peer_list_mode, |
---|
199 | int max_peer_count ); |
---|
200 | |
---|
201 | void tr_peerMgrStartTorrent( tr_torrent * tor ); |
---|
202 | |
---|
203 | void tr_peerMgrStopTorrent( tr_torrent * tor ); |
---|
204 | |
---|
205 | void tr_peerMgrAddTorrent( tr_peerMgr * manager, |
---|
206 | struct tr_torrent * tor ); |
---|
207 | |
---|
208 | void tr_peerMgrRemoveTorrent( tr_torrent * tor ); |
---|
209 | |
---|
210 | void tr_peerMgrTorrentAvailability( const tr_torrent * tor, |
---|
211 | int8_t * tab, |
---|
212 | unsigned int tabCount ); |
---|
213 | |
---|
214 | struct tr_bitfield* tr_peerMgrGetAvailable( const tr_torrent * tor ); |
---|
215 | |
---|
216 | void tr_peerMgrOnBlocklistChanged( tr_peerMgr * manager ); |
---|
217 | |
---|
218 | void tr_peerMgrTorrentStats( tr_torrent * tor, |
---|
219 | int * setmePeersKnown, |
---|
220 | int * setmePeersConnected, |
---|
221 | int * setmeSeedsConnected, |
---|
222 | int * setmeWebseedsSendingToUs, |
---|
223 | int * setmePeersSendingToUs, |
---|
224 | int * setmePeersGettingFromUs, |
---|
225 | int * setmePeersFrom ); /* TR_PEER_FROM__MAX */ |
---|
226 | |
---|
227 | struct tr_peer_stat* tr_peerMgrPeerStats( const tr_torrent * tor, |
---|
228 | int * setmeCount ); |
---|
229 | |
---|
230 | float tr_peerMgrGetWebseedSpeed( const tr_torrent * tor, uint64_t now ); |
---|
231 | |
---|
232 | float* tr_peerMgrWebSpeeds( const tr_torrent * tor ); |
---|
233 | |
---|
234 | |
---|
235 | double tr_peerGetPieceSpeed( const tr_peer * peer, |
---|
236 | uint64_t now, |
---|
237 | tr_direction direction ); |
---|
238 | |
---|
239 | /* @} */ |
---|
240 | |
---|
241 | #endif |
---|