source: trunk/libtransmission/transmission.h @ 1594

Last change on this file since 1594 was 1594, checked in by joshe, 15 years ago

Don't save or load cached peers for private torrents.

  • Property svn:keywords set to Date Rev Author Id
File size: 19.7 KB
Line 
1/******************************************************************************
2 * $Id: transmission.h 1594 2007-03-26 19:19:33Z joshe $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifndef TR_TRANSMISSION_H
26#define TR_TRANSMISSION_H 1
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include "version.h"
33
34#include <inttypes.h>
35#ifndef PRIu64
36# define PRIu64 "lld"
37#endif
38#include <time.h>
39
40#define SHA_DIGEST_LENGTH 20
41#ifdef __BEOS__
42# include <StorageDefs.h>
43# define MAX_PATH_LENGTH  B_FILE_NAME_LENGTH
44#else
45# define MAX_PATH_LENGTH  1024
46#endif
47
48#ifndef INET_ADDRSTRLEN
49#define INET_ADDRSTRLEN 16
50#endif
51
52#define TR_DEFAULT_PORT   9090
53
54#define TR_PEER_FROM__MAX       4
55#define TR_PEER_FROM_INCOMING   0 /* connections made to the listening port */
56#define TR_PEER_FROM_TRACKER    1 /* peers received from a tracker */
57#define TR_PEER_FROM_CACHE      2 /* peers read from the peer cache */
58#define TR_PEER_FROM_PEX        3 /* peers discovered via PEX */
59
60/***********************************************************************
61 * Error codes
62 **********************************************************************/
63/* General errors */
64#define TR_OK                   0x00000000
65#define TR_ERROR                0x81000000
66#define TR_ERROR_ASSERT         0x82000000
67/* I/O errors */
68#define TR_ERROR_IO_MASK        0x0000000F
69#define TR_ERROR_IO_PARENT      0x80000001
70#define TR_ERROR_IO_PERMISSIONS 0x80000002
71#define TR_ERROR_IO_OTHER       0x80000008
72/* Misc */
73#define TR_ERROR_TC_MASK        0x000000F0
74#define TR_ERROR_TC_ERROR       0x80000010
75#define TR_ERROR_TC_WARNING     0x80000020
76
77/***********************************************************************
78 * tr_init
79 ***********************************************************************
80 * Initializes a libtransmission instance. Returns a obscure handle to
81 * be passed to all functions below. The tag argument is a short string
82 * unique to the program invoking tr_init(), it is currently used as
83 * part of saved torrent files' names to prevent one frontend from
84 * deleting a torrent used by another. The following tags are used:
85 *   beos cli daemon gtk macosx
86 **********************************************************************/
87typedef struct tr_handle_s tr_handle_t;
88tr_handle_t * tr_init( const char * tag );
89
90typedef struct tr_tracker_info_s tr_tracker_info_t;
91
92/***********************************************************************
93 * tr_setMessageLevel
94 ***********************************************************************
95 * Set the level of messages to be output or queued
96 **********************************************************************/
97#define TR_MSG_ERR 1
98#define TR_MSG_INF 2
99#define TR_MSG_DBG 3
100void tr_setMessageLevel( int );
101int tr_getMessageLevel( void );
102
103/***********************************************************************
104 * tr_setMessageQueuing
105 ***********************************************************************
106 * Enable or disable message queuing
107 **********************************************************************/
108typedef struct tr_msg_list_s tr_msg_list_t;
109void tr_setMessageQueuing( int );
110
111/***********************************************************************
112 * tr_getQueuedMessages
113 ***********************************************************************
114 * Return a list of queued messages
115 **********************************************************************/
116tr_msg_list_t * tr_getQueuedMessages( void );
117void tr_freeMessageList( tr_msg_list_t * list );
118
119/***********************************************************************
120 * tr_getPrefsDirectory
121 ***********************************************************************
122 * Returns the full path to a directory which can be used to store
123 * preferences. The string belongs to libtransmission, do not free it.
124 **********************************************************************/
125char * tr_getPrefsDirectory();
126
127/***********************************************************************
128 * tr_setBindPort
129 ***********************************************************************
130 * Sets the port to listen for incoming peer connections.
131 * This can be safely called even with active torrents.
132 **********************************************************************/
133void tr_setBindPort( tr_handle_t *, int );
134
135/***********************************************************************
136 * tr_natTraversalEnable
137 * tr_natTraversalDisable
138 ***********************************************************************
139 * Enable or disable NAT traversal using NAT-PMP or UPnP IGD.
140 **********************************************************************/
141void tr_natTraversalEnable( tr_handle_t *, int enable );
142
143/***********************************************************************
144 * tr_handleStatus
145 ***********************************************************************
146 * Returns some status info for the given handle.
147 **********************************************************************/
148typedef struct tr_handle_status_s tr_handle_status_t;
149tr_handle_status_t * tr_handleStatus( tr_handle_t * );
150
151/***********************************************************************
152 * tr_setGlobalUploadLimit
153 ***********************************************************************
154 * Sets the total upload rate limit in KB/s
155 **********************************************************************/
156void tr_setGlobalUploadLimit( tr_handle_t *, int );
157
158/***********************************************************************
159 * tr_setGlobalDownloadLimit
160 ***********************************************************************
161 * Sets the total download rate limit in KB/s
162 **********************************************************************/
163void tr_setGlobalDownloadLimit( tr_handle_t *, int );
164
165/***********************************************************************
166 * tr_torrentCount
167 ***********************************************************************
168 * Returns the count of open torrents
169 **********************************************************************/
170int tr_torrentCount( tr_handle_t * h );
171
172/***********************************************************************
173 * tr_torrentIterate
174 ***********************************************************************
175 * Iterates on open torrents
176 **********************************************************************/
177typedef struct tr_torrent_s tr_torrent_t;
178typedef void (*tr_callback_t) ( tr_torrent_t *, void * );
179void tr_torrentIterate( tr_handle_t *, tr_callback_t, void * );
180
181void tr_setUseCustomLimit( tr_torrent_t * tor, int limit );
182void tr_setUploadLimit( tr_torrent_t * tor, int limit );
183void tr_setDownloadLimit( tr_torrent_t * tor, int limit );
184
185/***********************************************************************
186 * tr_torrentRates
187 ***********************************************************************
188 * Gets the total download and upload rates
189 **********************************************************************/
190void tr_torrentRates( tr_handle_t *, float *, float * );
191
192/***********************************************************************
193 * tr_close
194 ***********************************************************************
195 * Frees memory allocated by tr_init.
196 **********************************************************************/
197void tr_close( tr_handle_t * );
198
199/***********************************************************************
200 * tr_torrentInit
201 ***********************************************************************
202 * Opens and parses torrent file at 'path'. If the file exists and is
203 * a valid torrent file, returns an handle and adds it to the list of
204 * torrents (but doesn't start it). Returns NULL and sets *error
205 * otherwise. If hash is not NULL and the torrent is already loaded
206 * then it's 20-byte hash will be copied in. If the TR_FLAG_SAVE flag
207 * is passed then a copy of the torrent file will be saved.
208 **********************************************************************/
209#define TR_EINVALID     1
210#define TR_EUNSUPPORTED 2
211#define TR_EDUPLICATE   3
212#define TR_EOTHER       666
213tr_torrent_t * tr_torrentInit( tr_handle_t *, const char * path,
214                               uint8_t * hash, int flags, int * error );
215
216/***********************************************************************
217 * tr_torrentInitSaved
218 ***********************************************************************
219 * Opens and parses a torrent file as with tr_torrentInit, only taking
220 * the hash string of a saved torrent file instead of a filename. There
221 * are currently no valid flags for this function.
222 **********************************************************************/
223tr_torrent_t * tr_torrentInitSaved( tr_handle_t *, const char * hashStr,
224                                    int flags, int * error );
225
226/***********************************************************************
227 * tr_torrentDisablePex
228 ***********************************************************************
229 * Disable or enable peer exchange for this torrent. Peer exchange is
230 * enabled by default, except for private torrents where pex is
231 * disabled and cannot be enabled.
232 **********************************************************************/
233void tr_torrentDisablePex( tr_torrent_t *, int disable );
234
235/***********************************************************************
236 * tr_torrentScrape
237 ***********************************************************************
238 * Return torrent metainfo.
239 **********************************************************************/
240typedef struct tr_info_s tr_info_t;
241tr_info_t * tr_torrentInfo( tr_torrent_t * );
242
243/***********************************************************************
244 * tr_torrentScrape
245 ***********************************************************************
246 * Asks the tracker for the count of seeders and leechers. Returns 0
247 * and fills 's' and 'l' if successful. Otherwise returns 1 if the
248 * tracker doesn't support the scrape protocol, is unreachable or
249 * replied with some error. tr_torrentScrape may block up to 20 seconds
250 * before returning.
251 **********************************************************************/
252int tr_torrentScrape( tr_torrent_t *, int * s, int * l, int * d );
253
254/***********************************************************************
255 * tr_torrentStart
256 ***********************************************************************
257 * Starts downloading. The download is launched in a seperate thread,
258 * therefore tr_torrentStart returns immediately.
259 **********************************************************************/
260void   tr_torrentSetFolder( tr_torrent_t *, const char * );
261char * tr_torrentGetFolder( tr_torrent_t * );
262void   tr_torrentStart( tr_torrent_t * );
263
264/***********************************************************************
265 * tr_torrentStop
266 ***********************************************************************
267 * Stops downloading and notices the tracker that we are leaving. The
268 * thread keeps running while doing so.
269 * The thread will eventually be joined, either:
270 * - by tr_torrentStat when the tracker has been successfully noticed,
271 * - by tr_torrentStat if the tracker could not be noticed within 60s,
272 * - by tr_torrentClose if you choose to remove the torrent without
273 *   waiting any further.
274 **********************************************************************/
275void tr_torrentStop( tr_torrent_t * );
276
277/***********************************************************************
278 * tr_getFinished
279 ***********************************************************************
280 * The first call after a torrent is completed returns 1. Returns 0
281 * in other cases.
282 **********************************************************************/
283int tr_getFinished( tr_torrent_t * );
284
285/***********************************************************************
286 * tr_manualUpdate
287 ***********************************************************************
288 * Reannounce to tracker regardless of wait interval
289 **********************************************************************/
290void tr_manualUpdate( tr_torrent_t * );
291
292/***********************************************************************
293 * tr_torrentStat
294 ***********************************************************************
295 * Returns a pointer to an tr_stat_t structure with updated information
296 * on the torrent. The structure belongs to libtransmission (do not
297 * free it) and is guaranteed to be unchanged until the next call to
298 * tr_torrentStat.
299 * The interface should call this function every second or so in order
300 * to update itself.
301 **********************************************************************/
302typedef struct tr_stat_s tr_stat_t;
303tr_stat_t * tr_torrentStat( tr_torrent_t * );
304
305/***********************************************************************
306 * tr_torrentPeers
307 ***********************************************************************/
308typedef struct tr_peer_stat_s tr_peer_stat_t;
309tr_peer_stat_t * tr_torrentPeers( tr_torrent_t *, int * peerCount );
310void tr_torrentPeersFree( tr_peer_stat_t *, int peerCount );
311
312/***********************************************************************
313 * tr_torrentAvailability
314 ***********************************************************************
315 * Use this to draw an advanced progress bar which is 'size' pixels
316 * wide. Fills 'tab' which you must have allocated: each byte is set
317 * to either -1 if we have the piece, otherwise it is set to the number
318 * of connected peers who have the piece.
319 **********************************************************************/
320void tr_torrentAvailability( tr_torrent_t *, int8_t * tab, int size );
321
322void tr_torrentAmountFinished( tr_torrent_t * tor, float * tab, int size );
323
324/***********************************************************************
325 * tr_torrentCompletion
326 ***********************************************************************
327 * Returns the completion progress for each file in the torrent as an
328 * array of floats the same size and order as in tr_info_t. Free the
329 * array when done.
330 **********************************************************************/
331float * tr_torrentCompletion( tr_torrent_t * tor );
332
333/***********************************************************************
334 * tr_torrentRemoveSaved
335 ***********************************************************************
336 * Removes the saved copy of a torrent file for torrents which the
337 * TR_FLAG_SAVE flag is set.
338 **********************************************************************/
339void tr_torrentRemoveSaved( tr_torrent_t * );
340
341/***********************************************************************
342 * tr_torrentClose
343 ***********************************************************************
344 * Frees memory allocated by tr_torrentInit. If the torrent was running,
345 * you must call tr_torrentStop() before closing it.
346 **********************************************************************/
347void tr_torrentClose( tr_handle_t *, tr_torrent_t * );
348
349/***********************************************************************
350 * tr_info_s
351 **********************************************************************/
352typedef struct tr_file_s
353{
354    uint64_t length;                /* Length of the file, in bytes */
355    char     name[MAX_PATH_LENGTH]; /* Path to the file */
356}
357tr_file_t;
358struct tr_info_s
359{
360    /* Path to torrent */
361    char                 torrent[MAX_PATH_LENGTH];
362
363    /* General info */
364    uint8_t              hash[SHA_DIGEST_LENGTH];
365    char                 hashString[2*SHA_DIGEST_LENGTH+1];
366    char                 name[MAX_PATH_LENGTH];
367
368    /* Flags */
369#define TR_FLAG_SAVE    0x01 /* save a copy of the torrent file */
370#define TR_FLAG_PRIVATE 0x02 /* do not share information for this torrent */
371    int                  flags;
372
373    /* Tracker info */
374    struct
375    {
376        tr_tracker_info_t * list;
377        int                 count;
378    }                  * trackerList;
379    int                  trackerTiers;
380
381    /* Torrent info */
382    char                 comment[MAX_PATH_LENGTH];
383    char                 creator[MAX_PATH_LENGTH];
384    int                  dateCreated;
385
386    /* Pieces info */
387    int                  pieceSize;
388    int                  pieceCount;
389    uint64_t             totalSize;
390    uint8_t            * pieces;
391
392    /* Files info */
393    int                  multifile;
394    int                  fileCount;
395    tr_file_t          * files;
396};
397
398/***********************************************************************
399 * tr_stat_s
400 **********************************************************************/
401struct tr_stat_s
402{
403#define TR_STATUS_CHECK    0x001 /* Checking files */
404#define TR_STATUS_DOWNLOAD 0x002 /* Downloading */
405#define TR_STATUS_SEED     0x004 /* Seeding */
406#define TR_STATUS_STOPPING 0x008 /* Sending 'stopped' to the tracker */
407#define TR_STATUS_STOPPED  0x010 /* Sent 'stopped' but thread still
408                                    running (for internal use only) */
409#define TR_STATUS_PAUSE    0x020 /* Paused */
410
411#define TR_STATUS_ACTIVE   (TR_STATUS_CHECK|TR_STATUS_DOWNLOAD|TR_STATUS_SEED)
412#define TR_STATUS_INACTIVE (TR_STATUS_STOPPING|TR_STATUS_STOPPED|TR_STATUS_PAUSE)
413    int                 status;
414
415    int                 error;
416    char                errorString[128];
417    int                 cannotConnect;
418
419    tr_tracker_info_t * tracker;
420
421    float               progress;
422    float               rateDownload;
423    float               rateUpload;
424    int                 eta;
425    int                 peersTotal;
426    int                 peersFrom[TR_PEER_FROM__MAX];
427    int                 peersUploading;
428    int                 peersDownloading;
429    int                 seeders;
430    int                 leechers;
431    int                 completedFromTracker;
432
433    uint64_t            downloaded;
434    uint64_t            uploaded;
435    float               swarmspeed;
436
437#define TR_RATIO_NA  -1
438#define TR_RATIO_INF -2
439    float               ratio;
440};
441
442struct tr_peer_stat_s
443{
444    char    addr[INET_ADDRSTRLEN];
445    char *  client;
446   
447    int     isConnected;
448    int     from;
449    float   progress;
450    int     port;
451   
452    int     isDownloading;
453    int     isUploading;
454    float   downloadFromRate;
455    float   uploadToRate;
456};
457
458struct tr_msg_list_s
459{
460    int                    level;
461    time_t                 when;
462    char                 * message;
463    struct tr_msg_list_s * next;
464};
465
466struct tr_tracker_info_s
467{
468    char * address;
469    int    port;
470    char * announce;
471    char * scrape;
472};
473
474struct tr_handle_status_s
475{
476#define TR_NAT_TRAVERSAL_MAPPING        1
477#define TR_NAT_TRAVERSAL_MAPPED         2
478#define TR_NAT_TRAVERSAL_NOTFOUND       3
479#define TR_NAT_TRAVERSAL_ERROR          4
480#define TR_NAT_TRAVERSAL_UNMAPPING      5
481#define TR_NAT_TRAVERSAL_DISABLED       6
482#define TR_NAT_TRAVERSAL_IS_DISABLED( st ) \
483  ( TR_NAT_TRAVERSAL_DISABLED == (st) || TR_NAT_TRAVERSAL_UNMAPPING == (st) )
484    int natTraversalStatus;
485    int publicPort;
486};
487
488#ifdef __TRANSMISSION__
489#  include "internal.h"
490#endif
491
492#ifdef __cplusplus
493}
494#endif
495
496#endif
Note: See TracBrowser for help on using the repository browser.