source: trunk/libtransmission/transmission.h @ 2217

Last change on this file since 2217 was 2217, checked in by charles, 15 years ago

better documentation on tr_torrentParse()'s behavior

  • Property svn:keywords set to Date Rev Author Id
File size: 24.7 KB
Line 
1/******************************************************************************
2 * $Id: transmission.h 2217 2007-06-28 01:20:10Z charles $
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 PRId64
36# define PRId64 "lld"
37#endif
38#ifndef PRIu64
39# define PRIu64 "llu"
40#endif
41#include <time.h>
42
43#define SHA_DIGEST_LENGTH 20
44#ifdef __BEOS__
45# include <StorageDefs.h>
46# define MAX_PATH_LENGTH  B_FILE_NAME_LENGTH
47#else
48# define MAX_PATH_LENGTH  1024
49#endif
50
51#ifndef INET_ADDRSTRLEN
52#define INET_ADDRSTRLEN 16
53#endif
54
55#if defined(__MINGW__)
56#define TR_PATH_DELIMITER '\\'
57#define TR_PATH_DELIMITER_STR "\\"
58#else
59#define TR_PATH_DELIMITER '/'
60#define TR_PATH_DELIMITER_STR "/"
61#endif
62
63#define TR_DEFAULT_PORT   9090
64
65#define TR_PEER_FROM__MAX       4
66#define TR_PEER_FROM_INCOMING   0 /* connections made to the listening port */
67#define TR_PEER_FROM_TRACKER    1 /* peers received from a tracker */
68#define TR_PEER_FROM_CACHE      2 /* peers read from the peer cache */
69#define TR_PEER_FROM_PEX        3 /* peers discovered via PEX */
70
71/***********************************************************************
72 * Error codes
73 **********************************************************************/
74/* General errors */
75#define TR_OK                       0x00000000
76#define TR_ERROR                    0x81000000
77#define TR_ERROR_ASSERT             0x82000000
78/* I/O errors */
79#define TR_ERROR_IO_MASK            0x000000FF
80#define TR_ERROR_IO_PARENT          0x80000001
81#define TR_ERROR_IO_PERMISSIONS     0x80000002
82#define TR_ERROR_IO_SPACE           0x80000004
83#define TR_ERROR_IO_RESOURCES       0x80000008
84#define TR_ERROR_IO_DUP_DOWNLOAD    0x8000000A
85#define TR_ERROR_IO_OTHER           0x80000010
86/* Misc */
87#define TR_ERROR_TC_MASK            0x00000F00
88#define TR_ERROR_TC_ERROR           0x80000100
89#define TR_ERROR_TC_WARNING         0x80000200
90
91#define TR_ERROR_ISSET( num, code ) ( (code) == ( (code) & (num) ) )
92
93/***********************************************************************
94 * tr_init
95 ***********************************************************************
96 * Initializes a libtransmission instance. Returns a obscure handle to
97 * be passed to all functions below. The tag argument is a short string
98 * unique to the program invoking tr_init(), it is currently used as
99 * part of saved torrent files' names to prevent one frontend from
100 * deleting a torrent used by another. The following tags are used:
101 *   beos cli daemon gtk macosx
102 **********************************************************************/
103typedef struct tr_handle_s tr_handle_t;
104tr_handle_t * tr_init( const char * tag );
105
106typedef struct tr_tracker_info_s tr_tracker_info_t;
107
108/***********************************************************************
109 * tr_setMessageLevel
110 ***********************************************************************
111 * Set the level of messages to be output or queued
112 **********************************************************************/
113#define TR_MSG_ERR 1
114#define TR_MSG_INF 2
115#define TR_MSG_DBG 3
116void tr_setMessageLevel( int );
117int tr_getMessageLevel( void );
118
119/***********************************************************************
120 * tr_setMessageQueuing
121 ***********************************************************************
122 * Enable or disable message queuing
123 **********************************************************************/
124typedef struct tr_msg_list_s tr_msg_list_t;
125void tr_setMessageQueuing( int );
126
127/***********************************************************************
128 * tr_getQueuedMessages
129 ***********************************************************************
130 * Return a list of queued messages
131 **********************************************************************/
132tr_msg_list_t * tr_getQueuedMessages( void );
133void tr_freeMessageList( tr_msg_list_t * list );
134
135/***********************************************************************
136 * tr_getPrefsDirectory
137 ***********************************************************************
138 * Returns the full path to a directory which can be used to store
139 * preferences. The string belongs to libtransmission, do not free it.
140 **********************************************************************/
141const char * tr_getPrefsDirectory( void );
142
143/***********************************************************************
144 * tr_setBindPort
145 ***********************************************************************
146 * Sets the port to listen for incoming peer connections.
147 * This can be safely called even with active torrents.
148 **********************************************************************/
149void tr_setBindPort( tr_handle_t *, int );
150
151/***********************************************************************
152 * tr_natTraversalEnable
153 * tr_natTraversalDisable
154 ***********************************************************************
155 * Enable or disable NAT traversal using NAT-PMP or UPnP IGD.
156 **********************************************************************/
157void tr_natTraversalEnable( tr_handle_t *, int enable );
158
159/***********************************************************************
160 * tr_handleStatus
161 ***********************************************************************
162 * Returns some status info for the given handle.
163 **********************************************************************/
164typedef struct tr_handle_status_s tr_handle_status_t;
165tr_handle_status_t * tr_handleStatus( tr_handle_t * );
166
167/***********************************************************************
168 * tr_setGlobalUploadLimit
169 ***********************************************************************
170 * Sets the total upload rate limit in KB/s
171 **********************************************************************/
172void tr_setGlobalUploadLimit( tr_handle_t *, int );
173
174/***********************************************************************
175 * tr_setGlobalDownloadLimit
176 ***********************************************************************
177 * Sets the total download rate limit in KB/s
178 **********************************************************************/
179void tr_setGlobalDownloadLimit( tr_handle_t *, int );
180
181/***********************************************************************
182 * tr_torrentCount
183 ***********************************************************************
184 * Returns the count of open torrents
185 **********************************************************************/
186int tr_torrentCount( tr_handle_t * h );
187
188
189/***********************************************************************
190 * tr_torrentIterate
191 ***********************************************************************
192 * Iterates on open torrents
193 **********************************************************************/
194typedef struct tr_torrent_s tr_torrent_t;
195typedef void (*tr_callback_t) ( tr_torrent_t *, void * );
196void tr_torrentIterate( tr_handle_t *, tr_callback_t, void * );
197
198void tr_setUseCustomLimit( tr_torrent_t * tor, int limit );
199void tr_setUploadLimit( tr_torrent_t * tor, int limit );
200void tr_setDownloadLimit( tr_torrent_t * tor, int limit );
201
202/***********************************************************************
203 * Torrent Priorities
204 **********************************************************************/
205
206enum
207{
208    TR_PRI_DND    = -2, /* Do Not Download */
209    TR_PRI_LOW    = -1,
210    TR_PRI_NORMAL =  0, /* since NORMAL is 0, memset initializes nicely */
211    TR_PRI_HIGH   =  1
212};
213
214typedef int8_t tr_priority_t;
215
216/* priorities should be an array of tor->info.fileCount bytes,
217 * each holding a value of TR_PRI_NORMAL, _HIGH, _LOW, or _DND. */
218void tr_torrentSetFilePriorities ( tr_torrent_t *, const tr_priority_t * priorities );
219
220/* single-file form of tr_torrentPrioritizeFiles.
221 * priority must be one of TR_PRI_NORMAL, _HIGH, _LOW, or _DND */
222void tr_torrentSetFilePriority( tr_torrent_t *, int file, tr_priority_t priority );
223
224/* returns a malloc()ed array of tor->info.fileCount items,
225 * each holding a value of TR_PRI_NORMAL, _HIGH, _LOW, or _DND.
226   free the array when done. */
227tr_priority_t* tr_torrentGetFilePriorities( const tr_torrent_t * );
228
229/* single-file form of tr_torrentGetFilePriorities.
230 * returns one of TR_PRI_NORMAL, _HIGH, _LOW, or _DND */
231tr_priority_t tr_torrentGetFilePriority( const tr_torrent_t *, int file );
232
233/* returns the priority for the specified piece,
234 * as ranked by the number of files of various priorities in that piece.
235 * a zero priority means DND */
236tr_priority_t tr_torrentGetPiecePriority( const tr_torrent_t *, int pieceIndex );
237
238/***********************************************************************
239 * tr_torrentRates
240 ***********************************************************************
241 * Gets the total download and upload rates
242 **********************************************************************/
243void tr_torrentRates( tr_handle_t *, float *, float * );
244
245/***********************************************************************
246 * tr_close
247 ***********************************************************************
248 * Frees memory allocated by tr_init.
249 **********************************************************************/
250void tr_close( tr_handle_t * );
251
252/***********************************************************************
253 * tr_torrentInit
254 ***********************************************************************
255 * Opens and parses torrent file at 'path'. If the file exists and is
256 * a valid torrent file, returns an handle and adds it to the list of
257 * torrents (but doesn't start it). Returns NULL and sets *error
258 * otherwise. If hash is not NULL and the torrent is already loaded
259 * then it's 20-byte hash will be copied in. If the TR_FLAG_SAVE flag
260 * is passed then a copy of the torrent file will be saved.
261 **********************************************************************/
262#define TR_EINVALID     1
263#define TR_EUNSUPPORTED 2
264#define TR_EDUPLICATE   3
265#define TR_EOTHER       666
266tr_torrent_t * tr_torrentInit( tr_handle_t  * handle,
267                               const char   * metainfo_filename,
268                               const char   * destination,
269                               int            flags,
270                               int          * setme_error );
271
272typedef struct tr_info_s tr_info_t;
273
274/**
275 * Parses the specified metainfo file.
276 *
277 * Returns TR_OK if it parsed and can be added to Transmission.
278 * Returns TR_INVALID if it couldn't be parsed.
279 * Returns TR_EDUPLICATE if it parsed but can't be added.
280 *
281 * "destination" can be NULL if you don't need to know whether
282 * or not the torrent can be added.
283 *
284 " "setme_info" can be NULL if you don't need the information.
285 * If the metainfo can be parsed and setme_info is non-NULL,
286 * it will be filled with the metadata's info.  You'll need to
287 * call tr_metainfoFree( setme_info ) when done with it.
288 */
289int tr_torrentParse( const tr_handle_t  * handle,
290                     const char         * metainfo_filename,
291                     const char         * destination,
292                     tr_info_t          * setme_info );
293
294/***********************************************************************
295 * tr_torrentInitData
296 ***********************************************************************
297 * Like tr_torrentInit, except the actual torrent data is passed in
298 * instead of the filename.
299 **********************************************************************/
300tr_torrent_t * tr_torrentInitData( tr_handle_t *,
301                                   const uint8_t * data, size_t size,
302                                   const char * destination,
303                                   int flags, int * error );
304
305/***********************************************************************
306 * tr_torrentInitSaved
307 ***********************************************************************
308 * Opens and parses a torrent file as with tr_torrentInit, only taking
309 * the hash string of a saved torrent file instead of a filename. There
310 * are currently no valid flags for this function.
311 **********************************************************************/
312tr_torrent_t * tr_torrentInitSaved( tr_handle_t *,
313                                    const char * hashStr,
314                                    const char * destination,
315                                    int flags, int * error );
316
317/***********************************************************************
318 * tr_torrentDisablePex
319 ***********************************************************************
320 * Disable or enable peer exchange for this torrent. Peer exchange is
321 * enabled by default, except for private torrents where pex is
322 * disabled and cannot be enabled.
323 **********************************************************************/
324void tr_torrentDisablePex( tr_torrent_t *, int disable );
325
326/***********************************************************************
327 * tr_torrentScrape
328 ***********************************************************************
329 * Return torrent metainfo.
330 **********************************************************************/
331tr_info_t * tr_torrentInfo( tr_torrent_t * );
332
333/***********************************************************************
334 * tr_torrentScrape
335 ***********************************************************************
336 * Asks the tracker for the count of seeders and leechers. Returns 0
337 * and fills 's' and 'l' if successful. Otherwise returns 1 if the
338 * tracker doesn't support the scrape protocol, is unreachable or
339 * replied with some error. tr_torrentScrape may block up to 20 seconds
340 * before returning.
341 **********************************************************************/
342int tr_torrentScrape( tr_torrent_t *, int * s, int * l, int * d );
343
344void   tr_torrentSetFolder( tr_torrent_t *, const char * );
345const char * tr_torrentGetFolder( const tr_torrent_t * );
346
347/***********************************************************************
348 * tr_torrentStart
349 ***********************************************************************
350 * Starts downloading. The download is launched in a seperate thread,
351 * therefore tr_torrentStart returns immediately.
352 **********************************************************************/
353void   tr_torrentStart( tr_torrent_t * );
354
355/***********************************************************************
356 * tr_torrentStop
357 ***********************************************************************
358 * Stops downloading and notices the tracker that we are leaving. The
359 * thread keeps running while doing so.
360 * The thread will eventually be joined, either:
361 * - by tr_torrentStat when the tracker has been successfully noticed,
362 * - by tr_torrentStat if the tracker could not be noticed within 60s,
363 * - by tr_torrentClose if you choose to remove the torrent without
364 *   waiting any further.
365 **********************************************************************/
366void tr_torrentStop( tr_torrent_t * );
367
368/***********************************************************************
369 * tr_getComplete, tr_getIncomplete and tr_getPartial
370 ***********************************************************************
371 * The first call after a torrent changed state returns 1. Returns 0
372 * in other cases.
373 **********************************************************************/
374int tr_getIncomplete( tr_torrent_t * tor );
375int tr_getDone( tr_torrent_t * tor );
376int tr_getComplete( tr_torrent_t * tor );
377
378
379/***********************************************************************
380 * tr_manualUpdate
381 ***********************************************************************
382 * Reannounce to tracker regardless of wait interval
383 **********************************************************************/
384void tr_manualUpdate( tr_torrent_t * );
385
386/***********************************************************************
387 * tr_torrentStat
388 ***********************************************************************
389 * Returns a pointer to an tr_stat_t structure with updated information
390 * on the torrent. The structure belongs to libtransmission (do not
391 * free it) and is guaranteed to be unchanged until the next call to
392 * tr_torrentStat.
393 * The interface should call this function every second or so in order
394 * to update itself.
395 **********************************************************************/
396typedef struct tr_stat_s tr_stat_t;
397tr_stat_t * tr_torrentStat( tr_torrent_t * );
398
399/***********************************************************************
400 * tr_torrentPeers
401 ***********************************************************************/
402typedef struct tr_peer_stat_s tr_peer_stat_t;
403tr_peer_stat_t * tr_torrentPeers( const tr_torrent_t *, int * peerCount );
404void tr_torrentPeersFree( tr_peer_stat_t *, int peerCount );
405
406/***********************************************************************
407 * tr_torrentAvailability
408 ***********************************************************************
409 * Use this to draw an advanced progress bar which is 'size' pixels
410 * wide. Fills 'tab' which you must have allocated: each byte is set
411 * to either -1 if we have the piece, otherwise it is set to the number
412 * of connected peers who have the piece.
413 **********************************************************************/
414void tr_torrentAvailability( const tr_torrent_t *, int8_t * tab, int size );
415
416void tr_torrentAmountFinished( const tr_torrent_t * tor, float * tab, int size );
417
418/***********************************************************************
419 * tr_torrentCompletion
420 ***********************************************************************
421 * Returns the completion progress for each file in the torrent as an
422 * array of floats the same size and order as in tr_info_t. Free the
423 * array when done.
424 **********************************************************************/
425float * tr_torrentCompletion( const tr_torrent_t * );
426
427float tr_torrentFileCompletion( const tr_torrent_t *, int fileIndex );
428
429uint64_t tr_torrentFileBytesCompleted( const tr_torrent_t *, int fileIndex ); 
430
431
432/***********************************************************************
433 * tr_torrentRemoveSaved
434 ***********************************************************************
435 * Removes the saved copy of a torrent file for torrents which the
436 * TR_FLAG_SAVE flag is set.
437 **********************************************************************/
438void tr_torrentRemoveSaved( tr_torrent_t * );
439
440void tr_torrentRecheck( tr_torrent_t * );
441
442/***********************************************************************
443 * tr_torrentClose
444 ***********************************************************************
445 * Frees memory allocated by tr_torrentInit. If the torrent was running,
446 * it is stopped first.
447 **********************************************************************/
448void tr_torrentClose( tr_torrent_t * );
449
450/***********************************************************************
451 * tr_info_s
452 **********************************************************************/
453
454typedef struct tr_file_s
455{
456    uint64_t length;                /* Length of the file, in bytes */
457    char     name[MAX_PATH_LENGTH]; /* Path to the file */
458    int8_t   priority;              /* TR_PRI_HIGH, _NORMAL, _LOW, or _DND */
459    int      firstPiece;            /* We need pieces [firstPiece... */
460    int      lastPiece;             /* ...lastPiece] to dl this file */
461    uint64_t offset;                /* file begins at the torrent's nth byte */
462}
463tr_file_t;
464
465typedef struct tr_piece_s
466{
467    uint8_t  hash[SHA_DIGEST_LENGTH];  /* pieces hash */
468    int8_t   priority;                 /* TR_PRI_HIGH, _NORMAL, _LOW, or _DND */
469}
470tr_piece_t;
471   
472struct tr_info_s
473{
474    /* Path to torrent */
475    char                 torrent[MAX_PATH_LENGTH];
476
477    /* General info */
478    uint8_t              hash[SHA_DIGEST_LENGTH];
479    char                 hashString[2*SHA_DIGEST_LENGTH+1];
480    char                 name[MAX_PATH_LENGTH];
481
482    /* Flags */
483#define TR_FLAG_SAVE    0x01 /* save a copy of the torrent file */
484#define TR_FLAG_PRIVATE 0x02 /* do not share information for this torrent */
485#define TR_FLAG_PAUSED  0x04 /* don't start the torrent when adding it */
486    int                  flags;
487
488    /* Tracker info */
489    struct
490    {
491        tr_tracker_info_t * list;
492        int                 count;
493    }                  * trackerList;
494    int                  trackerTiers;
495
496    /* Torrent info */
497    char                 comment[MAX_PATH_LENGTH];
498    char                 creator[MAX_PATH_LENGTH];
499    int                  dateCreated;
500
501    /* Pieces info */
502    int                  pieceSize;
503    int                  pieceCount;
504    uint64_t             totalSize;
505    tr_piece_t         * pieces;
506
507    /* Files info */
508    int                  multifile;
509    int                  fileCount;
510    tr_file_t          * files;
511};
512
513typedef enum
514{
515    TR_CP_INCOMPLETE,   /* doesn't have all the desired pieces */
516    TR_CP_DONE,         /* has all the pieces but the DND ones */
517    TR_CP_COMPLETE      /* has every piece */
518}
519cp_status_t;
520
521typedef enum
522{
523    TR_STATUS_CHECK_WAIT   = (1<<0), /* Waiting in queue to check files */
524    TR_STATUS_CHECK        = (1<<1), /* Checking files */
525    TR_STATUS_DOWNLOAD     = (1<<2), /* Downloading */
526    TR_STATUS_DONE         = (1<<3), /* not at 100% so can't tell the tracker
527                                        we're a seeder, but due to DND files
528                                        there's nothing we want right now */
529    TR_STATUS_SEED         = (1<<4), /* Seeding */
530    TR_STATUS_STOPPING     = (1<<5), /* Stopping -- closing connections, etc. */
531    TR_STATUS_STOPPED      = (1<<6)  /* Torrent is stopped */
532}
533torrent_status_t;
534
535#define TR_STATUS_ACTIVE \
536    (TR_STATUS_CHECK_WAIT|TR_STATUS_CHECK|TR_STATUS_DOWNLOAD|TR_STATUS_DONE|TR_STATUS_SEED)
537#define TR_STATUS_INACTIVE \
538    (TR_STATUS_STOPPING|TR_STATUS_STOPPED)
539
540/***********************************************************************
541 * tr_stat_s
542 **********************************************************************/
543struct tr_stat_s
544{
545    torrent_status_t    status;
546    cp_status_t         cpStatus;
547
548    int                 error;
549    char                errorString[128];
550    int                 cannotConnect;
551
552    tr_tracker_info_t * tracker;
553
554    float               percentComplete;
555    float               percentDone;
556    float               rateDownload;
557    float               rateUpload;
558    int                 eta;
559    int                 peersTotal;
560    int                 peersFrom[TR_PEER_FROM__MAX];
561    int                 peersUploading;
562    int                 peersDownloading;
563    int                 seeders;
564    int                 leechers;
565    int                 completedFromTracker;
566
567    uint64_t            left;
568    uint64_t            downloaded;
569    uint64_t            downloadedValid;
570    uint64_t            uploaded;
571    float               swarmspeed;
572
573#define TR_RATIO_NA  -1
574    float               ratio;
575   
576    uint64_t            startDate;
577    uint64_t            activityDate;
578};
579
580struct tr_peer_stat_s
581{
582    char    addr[INET_ADDRSTRLEN];
583    const char * client;
584   
585    int     isConnected;
586    int     from;
587    float   progress;
588    int     port;
589   
590    int     isDownloading;
591    int     isUploading;
592    float   downloadFromRate;
593    float   uploadToRate;
594};
595
596struct tr_msg_list_s
597{
598    int                    level;
599    time_t                 when;
600    char                 * message;
601    struct tr_msg_list_s * next;
602};
603
604struct tr_tracker_info_s
605{
606    char * address;
607    int    port;
608    char * announce;
609    char * scrape;
610};
611
612struct tr_handle_status_s
613{
614#define TR_NAT_TRAVERSAL_MAPPING        1
615#define TR_NAT_TRAVERSAL_MAPPED         2
616#define TR_NAT_TRAVERSAL_NOTFOUND       3
617#define TR_NAT_TRAVERSAL_ERROR          4
618#define TR_NAT_TRAVERSAL_UNMAPPING      5
619#define TR_NAT_TRAVERSAL_DISABLED       6
620#define TR_NAT_TRAVERSAL_IS_DISABLED( st ) \
621  ( TR_NAT_TRAVERSAL_DISABLED == (st) || TR_NAT_TRAVERSAL_UNMAPPING == (st) )
622    int natTraversalStatus;
623    int publicPort;
624};
625
626#ifdef __TRANSMISSION__
627#  include "internal.h"
628#endif
629
630#ifdef __cplusplus
631}
632#endif
633
634#endif
Note: See TracBrowser for help on using the repository browser.