1 /*
2  * This file Copyright (C) 2010-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 __LIBTRANSMISSION_ANNOUNCER_MODULE__
12 #error only the libtransmission announcer module should #include this header.
13 #endif
14 
15 #include "transmission.h" /* SHA_DIGEST_LENGTH */
16 #include "session.h" /* PEER_ID_LEN */
17 
18 /***
19 ****  SCRAPE
20 ***/
21 
22 enum
23 {
24     /* pick a number small enough for common tracker software:
25      *  - ocelot has no upper bound
26      *  - opentracker has an upper bound of 64
27      *  - udp protocol has an upper bound of 74
28      *  - xbtt has no upper bound
29      *
30      * This is only an upper bound: if the tracker complains about
31      * length, announcer will incrementally lower the batch size.
32      */
33     TR_MULTISCRAPE_MAX = 100
34 };
35 
36 typedef struct
37 {
38     /* the scrape URL */
39     char* url;
40 
41     /* the name to use when deep logging is enabled */
42     char log_name[128];
43 
44     /* info hashes of the torrents to scrape */
45     uint8_t info_hash[TR_MULTISCRAPE_MAX][SHA_DIGEST_LENGTH];
46 
47     /* how many hashes to use in the info_hash field */
48     int info_hash_count;
49 }
50 tr_scrape_request;
51 
52 struct tr_scrape_response_row
53 {
54     /* the torrent's info_hash */
55     uint8_t info_hash[SHA_DIGEST_LENGTH];
56 
57     /* how many peers are seeding this torrent */
58     int seeders;
59 
60     /* how many peers are downloading this torrent */
61     int leechers;
62 
63     /* how many times this torrent has been downloaded */
64     int downloads;
65 
66     /* the number of active downloaders in the swarm.
67      * this is a BEP 21 extension that some trackers won't support.
68      * http://www.bittorrent.org/beps/bep_0021.html#tracker-scrapes  */
69     int downloaders;
70 };
71 
72 typedef struct
73 {
74     /* whether or not we managed to connect to the tracker */
75     bool did_connect;
76 
77     /* whether or not the scrape timed out */
78     bool did_timeout;
79 
80     /* how many info hashes are in the 'rows' field */
81     int row_count;
82 
83     /* the individual torrents' scrape results */
84     struct tr_scrape_response_row rows[TR_MULTISCRAPE_MAX];
85 
86     /* the raw scrape url */
87     char* url;
88 
89     /* human-readable error string on failure, or NULL */
90     char* errmsg;
91 
92     /* minimum interval (in seconds) allowed between scrapes.
93      * this is an unofficial extension that some trackers won't support. */
94     int min_request_interval;
95 }
96 tr_scrape_response;
97 
98 typedef void (* tr_scrape_response_func)(tr_scrape_response const* response, void* user_data);
99 
100 void tr_tracker_http_scrape(tr_session* session, tr_scrape_request const* req, tr_scrape_response_func response_func,
101     void* user_data);
102 
103 void tr_tracker_udp_scrape(tr_session* session, tr_scrape_request const* req, tr_scrape_response_func response_func,
104     void* user_data);
105 
106 /***
107 ****  ANNOUNCE
108 ***/
109 
110 typedef enum
111 {
112     TR_ANNOUNCE_EVENT_NONE,
113     TR_ANNOUNCE_EVENT_COMPLETED,
114     TR_ANNOUNCE_EVENT_STARTED,
115     TR_ANNOUNCE_EVENT_STOPPED
116 }
117 tr_announce_event;
118 
119 char const* tr_announce_event_get_string(tr_announce_event);
120 
121 typedef struct
122 {
123     tr_announce_event event;
124     bool partial_seed;
125 
126     /* the port we listen for incoming peers on */
127     int port;
128 
129     /* per-session key */
130     int key;
131 
132     /* the number of peers we'd like to get back in the response */
133     int numwant;
134 
135     /* the number of bytes we uploaded since the last 'started' event */
136     uint64_t up;
137 
138     /* the number of good bytes we downloaded since the last 'started' event */
139     uint64_t down;
140 
141     /* the number of bad bytes we downloaded since the last 'started' event */
142     uint64_t corrupt;
143 
144     /* the total size of the torrent minus the number of bytes completed */
145     uint64_t leftUntilComplete;
146 
147     /* the tracker's announce URL */
148     char* url;
149 
150     /* key generated by and returned from an http tracker.
151      * see tr_announce_response.tracker_id_str */
152     char* tracker_id_str;
153 
154     /* the torrent's peer id.
155      * this changes when a torrent is stopped -> restarted. */
156     char peer_id[PEER_ID_LEN];
157 
158     /* the torrent's info_hash */
159     uint8_t info_hash[SHA_DIGEST_LENGTH];
160 
161     /* the name to use when deep logging is enabled */
162     char log_name[128];
163 }
164 tr_announce_request;
165 
166 struct tr_pex;
167 
168 typedef struct
169 {
170     /* the torrent's info hash */
171     uint8_t info_hash[SHA_DIGEST_LENGTH];
172 
173     /* whether or not we managed to connect to the tracker */
174     bool did_connect;
175 
176     /* whether or not the scrape timed out */
177     bool did_timeout;
178 
179     /* preferred interval between announces.
180      * transmission treats this as the interval for periodic announces */
181     int interval;
182 
183     /* minimum interval between announces. (optional)
184      * transmission treats this as the min interval for manual announces */
185     int min_interval;
186 
187     /* how many peers are seeding this torrent */
188     int seeders;
189 
190     /* how many peers are downloading this torrent */
191     int leechers;
192 
193     /* how many times this torrent has been downloaded */
194     int downloads;
195 
196     /* number of items in the 'pex' field */
197     size_t pex_count;
198 
199     /* IPv4 peers that we acquired from the tracker */
200     struct tr_pex* pex;
201 
202     /* number of items in the 'pex6' field */
203     size_t pex6_count;
204 
205     /* IPv6 peers that we acquired from the tracker */
206     struct tr_pex* pex6;
207 
208     /* human-readable error string on failure, or NULL */
209     char* errmsg;
210 
211     /* human-readable warning string or NULL */
212     char* warning;
213 
214     /* key generated by and returned from an http tracker.
215      * if this is provided, subsequent http announces must include this. */
216     char* tracker_id_str;
217 }
218 tr_announce_response;
219 
220 typedef void (* tr_announce_response_func)(tr_announce_response const* response, void* userdata);
221 
222 void tr_tracker_http_announce(tr_session* session, tr_announce_request const* req, tr_announce_response_func response_func,
223     void* user_data);
224 
225 void tr_tracker_udp_announce(tr_session* session, tr_announce_request const* req, tr_announce_response_func response_func,
226     void* user_data);
227 
228 void tr_tracker_udp_start_shutdown(tr_session* session);
229