1 | /* |
---|
2 | * This file Copyright (C) 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-common.h 13949 2013-02-03 23:29:34Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef TR_PEER_H |
---|
18 | #define TR_PEER_H |
---|
19 | |
---|
20 | #include "transmission.h" |
---|
21 | #include "bitfield.h" |
---|
22 | #include "history.h" |
---|
23 | #include "quark.h" |
---|
24 | |
---|
25 | /** |
---|
26 | * @addtogroup peers Peers |
---|
27 | * @{ |
---|
28 | */ |
---|
29 | |
---|
30 | enum |
---|
31 | { |
---|
32 | /* this is the maximum size of a block request. |
---|
33 | most bittorrent clients will reject requests |
---|
34 | larger than this size. */ |
---|
35 | MAX_BLOCK_SIZE = (1024 * 16) |
---|
36 | }; |
---|
37 | |
---|
38 | /** |
---|
39 | *** Peer Publish / Subscribe |
---|
40 | **/ |
---|
41 | |
---|
42 | typedef enum |
---|
43 | { |
---|
44 | TR_PEER_CLIENT_GOT_BLOCK, |
---|
45 | TR_PEER_CLIENT_GOT_CHOKE, |
---|
46 | TR_PEER_CLIENT_GOT_PIECE_DATA, |
---|
47 | TR_PEER_CLIENT_GOT_ALLOWED_FAST, |
---|
48 | TR_PEER_CLIENT_GOT_SUGGEST, |
---|
49 | TR_PEER_CLIENT_GOT_PORT, |
---|
50 | TR_PEER_CLIENT_GOT_REJ, |
---|
51 | TR_PEER_CLIENT_GOT_BITFIELD, |
---|
52 | TR_PEER_CLIENT_GOT_HAVE, |
---|
53 | TR_PEER_CLIENT_GOT_HAVE_ALL, |
---|
54 | TR_PEER_CLIENT_GOT_HAVE_NONE, |
---|
55 | TR_PEER_PEER_GOT_PIECE_DATA, |
---|
56 | TR_PEER_ERROR |
---|
57 | } |
---|
58 | PeerEventType; |
---|
59 | |
---|
60 | typedef struct |
---|
61 | { |
---|
62 | PeerEventType eventType; |
---|
63 | |
---|
64 | uint32_t pieceIndex; /* for GOT_BLOCK, GOT_HAVE, CANCEL, ALLOWED, SUGGEST */ |
---|
65 | struct tr_bitfield * bitfield; /* for GOT_BITFIELD */ |
---|
66 | uint32_t offset; /* for GOT_BLOCK */ |
---|
67 | uint32_t length; /* for GOT_BLOCK + GOT_PIECE_DATA */ |
---|
68 | int err; /* errno for GOT_ERROR */ |
---|
69 | tr_port port; /* for GOT_PORT */ |
---|
70 | } |
---|
71 | tr_peer_event; |
---|
72 | |
---|
73 | extern const tr_peer_event TR_PEER_EVENT_INIT; |
---|
74 | |
---|
75 | /** |
---|
76 | * State information about a connected peer. |
---|
77 | * |
---|
78 | * @see struct peer_atom |
---|
79 | * @see tr_peermsgs |
---|
80 | */ |
---|
81 | typedef struct tr_peer |
---|
82 | { |
---|
83 | /* whether or not we should free this peer soon. |
---|
84 | NOTE: private to peer-mgr.c */ |
---|
85 | bool doPurge; |
---|
86 | |
---|
87 | /* Whether or not we've choked this peer. |
---|
88 | Only applies to BitTorrent peers */ |
---|
89 | bool peerIsChoked; |
---|
90 | |
---|
91 | /* whether or not the peer has indicated it will download from us. |
---|
92 | Only applies to BitTorrent peers */ |
---|
93 | bool peerIsInterested; |
---|
94 | |
---|
95 | /* whether or the peer is choking us. |
---|
96 | Only applies to BitTorrent peers */ |
---|
97 | bool clientIsChoked; |
---|
98 | |
---|
99 | /* whether or not we've indicated to the peer that we would download from them if unchoked. |
---|
100 | Only applies to BitTorrent peers */ |
---|
101 | bool clientIsInterested; |
---|
102 | |
---|
103 | /* number of bad pieces they've contributed to */ |
---|
104 | uint8_t strikes; |
---|
105 | |
---|
106 | /* how many requests the peer has made that we haven't responded to yet */ |
---|
107 | int pendingReqsToClient; |
---|
108 | |
---|
109 | /* how many requests we've made and are currently awaiting a response for */ |
---|
110 | int pendingReqsToPeer; |
---|
111 | |
---|
112 | struct tr_peerIo * io; |
---|
113 | |
---|
114 | /* Hook to private peer-mgr information */ |
---|
115 | struct peer_atom * atom; |
---|
116 | |
---|
117 | /** how complete the peer's copy of the torrent is. [0.0...1.0] */ |
---|
118 | float progress; |
---|
119 | |
---|
120 | struct tr_bitfield blame; |
---|
121 | struct tr_bitfield have; |
---|
122 | |
---|
123 | /* the client name. |
---|
124 | For BitTorrent peers, this is the app name derived from the `v' string in LTEP's handshake dictionary */ |
---|
125 | tr_quark client; |
---|
126 | |
---|
127 | time_t chokeChangedAt; |
---|
128 | |
---|
129 | tr_recentHistory blocksSentToClient; |
---|
130 | tr_recentHistory blocksSentToPeer; |
---|
131 | |
---|
132 | tr_recentHistory cancelsSentToClient; |
---|
133 | tr_recentHistory cancelsSentToPeer; |
---|
134 | |
---|
135 | struct tr_peermsgs * msgs; |
---|
136 | } |
---|
137 | tr_peer; |
---|
138 | |
---|
139 | typedef void tr_peer_callback (struct tr_peer * peer, |
---|
140 | const tr_peer_event * event, |
---|
141 | void * client_data); |
---|
142 | |
---|
143 | /** Update the tr_peer.progress field based on the 'have' bitset. */ |
---|
144 | void tr_peerUpdateProgress (tr_torrent * tor, struct tr_peer *); |
---|
145 | |
---|
146 | |
---|
147 | #ifdef WIN32 |
---|
148 | #define EMSGSIZE WSAEMSGSIZE |
---|
149 | #endif |
---|
150 | |
---|
151 | /** @} */ |
---|
152 | |
---|
153 | #endif |
---|