1 /*
2 * This file Copyright (C) 2007-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 */
8
9 #pragma once
10
11 #ifndef __TRANSMISSION__
12 #error only libtransmission should #include this header.
13 #endif
14
15 #include <inttypes.h> /* uint16_t */
16
17 #ifdef _WIN32
18 #include <winsock2.h> /* struct in_addr */
19 #endif
20
21 #include "net.h" /* tr_address */
22 #include "peer-common.h"
23 #include "quark.h"
24
25 /**
26 * @addtogroup peers Peers
27 * @{
28 */
29
30 struct UTPSocket;
31 struct tr_peer_stat;
32 struct tr_torrent;
33 typedef struct tr_peerMgr tr_peerMgr;
34
35 /* added_f's bitwise-or'ed flags */
36 enum
37 {
38 /* true if the peer supports encryption */
39 ADDED_F_ENCRYPTION_FLAG = 1,
40 /* true if the peer is a seed or partial seed */
41 ADDED_F_SEED_FLAG = 2,
42 /* true if the peer supports uTP */
43 ADDED_F_UTP_FLAGS = 4,
44 /* true if the peer has holepunch support */
45 ADDED_F_HOLEPUNCH = 8,
46 /* true if the peer telling us about this peer
47 * initiated the connection (implying that it is connectible) */
48 ADDED_F_CONNECTABLE = 16
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 struct peer_atom;
60 struct tr_peerIo;
61 struct tr_peerMsgs;
62 struct tr_swarm;
63
tr_isPex(tr_pex const * pex)64 static inline bool tr_isPex(tr_pex const* pex)
65 {
66 return pex && tr_address_is_valid(&pex->addr);
67 }
68
69 tr_address const* tr_peerAddress(tr_peer const*);
70
71 int tr_pexCompare(void const* a, void const* b);
72
73 tr_peerMgr* tr_peerMgrNew(tr_session* session);
74
75 void tr_peerMgrFree(tr_peerMgr* manager);
76
77 bool tr_peerMgrPeerIsSeed(tr_torrent const* tor, tr_address const* addr);
78
79 void tr_peerMgrSetUtpSupported(tr_torrent* tor, tr_address const* addr);
80
81 void tr_peerMgrSetUtpFailed(tr_torrent* tor, tr_address const* addr, bool failed);
82
83 void tr_peerMgrGetNextRequests(tr_torrent* torrent, tr_peer* peer, int numwant, tr_block_index_t* setme, int* numgot,
84 bool get_intervals);
85
86 bool tr_peerMgrDidPeerRequest(tr_torrent const* torrent, tr_peer const* peer, tr_block_index_t block);
87
88 void tr_peerMgrRebuildRequests(tr_torrent* torrent);
89
90 void tr_peerMgrAddIncoming(tr_peerMgr* manager, tr_address* addr, tr_port port, struct tr_peer_socket socket);
91
92 tr_pex* tr_peerMgrCompactToPex(void const* compact, size_t compactLen, uint8_t const* added_f, size_t added_f_len,
93 size_t* setme_pex_count);
94
95 tr_pex* tr_peerMgrCompact6ToPex(void const* compact, size_t compactLen, uint8_t const* added_f, size_t added_f_len,
96 size_t* pexCount);
97
98 /**
99 * @param seedProbability [0..100] for likelihood that the peer is a seed; -1 for unknown
100 */
101 void tr_peerMgrAddPex(tr_torrent* tor, uint8_t from, tr_pex const* pex, int8_t seedProbability);
102
103 enum
104 {
105 TR_PEERS_CONNECTED,
106 TR_PEERS_INTERESTING
107 };
108
109 int tr_peerMgrGetPeers(tr_torrent* tor, tr_pex** setme_pex, uint8_t address_type, uint8_t peer_list_mode, int max_peer_count);
110
111 void tr_peerMgrStartTorrent(tr_torrent* tor);
112
113 void tr_peerMgrStopTorrent(tr_torrent* tor);
114
115 void tr_peerMgrAddTorrent(tr_peerMgr* manager, struct tr_torrent* tor);
116
117 void tr_peerMgrRemoveTorrent(tr_torrent* tor);
118
119 void tr_peerMgrTorrentAvailability(tr_torrent const* tor, int8_t* tab, unsigned int tabCount);
120
121 uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor);
122
123 void tr_peerMgrOnTorrentGotMetainfo(tr_torrent* tor);
124
125 void tr_peerMgrOnBlocklistChanged(tr_peerMgr* manager);
126
127 struct tr_peer_stat* tr_peerMgrPeerStats(tr_torrent const* tor, int* setmeCount);
128
129 double* tr_peerMgrWebSpeeds_KBps(tr_torrent const* tor);
130
131 unsigned int tr_peerGetPieceSpeed_Bps(tr_peer const* peer, uint64_t now, tr_direction direction);
132
133 void tr_peerMgrClearInterest(tr_torrent* tor);
134
135 void tr_peerMgrGotBadPiece(tr_torrent* tor, tr_piece_index_t pieceIndex);
136
137 void tr_peerMgrPieceCompleted(tr_torrent* tor, tr_piece_index_t pieceIndex);
138
139 /* @} */
140