source: trunk/libtransmission/transmission.h @ 2210

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

simplify tr_torrentParse() based on BWM's feedback

  • Property svn:keywords set to Date Rev Author Id
File size: 24.7 KB
Line 
1/******************************************************************************
2 * $Id: transmission.h 2210 2007-06-27 23:22:09Z 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.
287 */
288int tr_torrentParse( const tr_handle_t  * handle,
289                     const char         * metainfo_filename,
290                     const char         * destination,
291                     tr_info_t          * setme_info );
292
293/***********************************************************************
294 * tr_torrentInitData
295 ***********************************************************************
296 * Like tr_torrentInit, except the actual torrent data is passed in
297 * instead of the filename.
298 **********************************************************************/
299tr_torrent_t * tr_torrentInitData( tr_handle_t *,
300                                   const uint8_t * data, size_t size,
301                                   const char * destination,
302                                   int flags, int * error );
303
304/***********************************************************************
305 * tr_torrentInitSaved
306 ***********************************************************************
307 * Opens and parses a torrent file as with tr_torrentInit, only taking
308 * the hash string of a saved torrent file instead of a filename. There
309 * are currently no valid flags for this function.
310 **********************************************************************/
311tr_torrent_t * tr_torrentInitSaved( tr_handle_t *,
312                                    const char * hashStr,
313                                    const char * destination,
314                                    int flags, int * error );
315
316/***********************************************************************
317 * tr_torrentDisablePex
318 ***********************************************************************
319 * Disable or enable peer exchange for this torrent. Peer exchange is
320 * enabled by default, except for private torrents where pex is
321 * disabled and cannot be enabled.
322 **********************************************************************/
323void tr_torrentDisablePex( tr_torrent_t *, int disable );
324
325/***********************************************************************
326 * tr_torrentScrape
327 ***********************************************************************
328 * Return torrent metainfo.
329 **********************************************************************/
330tr_info_t * tr_torrentInfo( tr_torrent_t * );
331
332/***********************************************************************
333 * tr_torrentScrape
334 ***********************************************************************
335 * Asks the tracker for the count of seeders and leechers. Returns 0
336 * and fills 's' and 'l' if successful. Otherwise returns 1 if the
337 * tracker doesn't support the scrape protocol, is unreachable or
338 * replied with some error. tr_torrentScrape may block up to 20 seconds
339 * before returning.
340 **********************************************************************/
341int tr_torrentScrape( tr_torrent_t *, int * s, int * l, int * d );
342
343void   tr_torrentSetFolder( tr_torrent_t *, const char * );
344const char * tr_torrentGetFolder( const tr_torrent_t * );
345
346/***********************************************************************
347 * tr_torrentStart
348 ***********************************************************************
349 * Starts downloading. The download is launched in a seperate thread,
350 * therefore tr_torrentStart returns immediately.
351 **********************************************************************/
352void   tr_torrentStart( tr_torrent_t * );
353
354/***********************************************************************
355 * tr_torrentStop
356 ***********************************************************************
357 * Stops downloading and notices the tracker that we are leaving. The
358 * thread keeps running while doing so.
359 * The thread will eventually be joined, either:
360 * - by tr_torrentStat when the tracker has been successfully noticed,
361 * - by tr_torrentStat if the tracker could not be noticed within 60s,
362 * - by tr_torrentClose if you choose to remove the torrent without
363 *   waiting any further.
364 **********************************************************************/
365void tr_torrentStop( tr_torrent_t * );
366
367/***********************************************************************
368 * tr_getComplete, tr_getIncomplete and tr_getPartial
369 ***********************************************************************
370 * The first call after a torrent changed state returns 1. Returns 0
371 * in other cases.
372 **********************************************************************/
373int tr_getIncomplete( tr_torrent_t * tor );
374int tr_getDone( tr_torrent_t * tor );
375int tr_getComplete( tr_torrent_t * tor );
376
377
378/***********************************************************************
379 * tr_manualUpdate
380 ***********************************************************************
381 * Reannounce to tracker regardless of wait interval
382 **********************************************************************/
383void tr_manualUpdate( tr_torrent_t * );
384
385/***********************************************************************
386 * tr_torrentStat
387 ***********************************************************************
388 * Returns a pointer to an tr_stat_t structure with updated information
389 * on the torrent. The structure belongs to libtransmission (do not
390 * free it) and is guaranteed to be unchanged until the next call to
391 * tr_torrentStat.
392 * The interface should call this function every second or so in order
393 * to update itself.
394 **********************************************************************/
395typedef struct tr_stat_s tr_stat_t;
396tr_stat_t * tr_torrentStat( tr_torrent_t * );
397
398/***********************************************************************
399 * tr_torrentPeers
400 ***********************************************************************/
401typedef struct tr_peer_stat_s tr_peer_stat_t;
402tr_peer_stat_t * tr_torrentPeers( const tr_torrent_t *, int * peerCount );
403void tr_torrentPeersFree( tr_peer_stat_t *, int peerCount );
404
405/***********************************************************************
406 * tr_torrentAvailability
407 ***********************************************************************
408 * Use this to draw an advanced progress bar which is 'size' pixels
409 * wide. Fills 'tab' which you must have allocated: each byte is set
410 * to either -1 if we have the piece, otherwise it is set to the number
411 * of connected peers who have the piece.
412 **********************************************************************/
413void tr_torrentAvailability( const tr_torrent_t *, int8_t * tab, int size );
414
415void tr_torrentAmountFinished( const tr_torrent_t * tor, float * tab, int size );
416
417/***********************************************************************
418 * tr_torrentCompletion
419 ***********************************************************************
420 * Returns the completion progress for each file in the torrent as an
421 * array of floats the same size and order as in tr_info_t. Free the
422 * array when done.
423 **********************************************************************/
424float * tr_torrentCompletion( const tr_torrent_t * );
425
426float tr_torrentFileCompletion( const tr_torrent_t *, int fileIndex );
427
428uint64_t tr_torrentFileBytesCompleted( const tr_torrent_t *, int fileIndex ); 
429
430
431/***********************************************************************
432 * tr_torrentRemoveSaved
433 ***********************************************************************
434 * Removes the saved copy of a torrent file for torrents which the
435 * TR_FLAG_SAVE flag is set.
436 **********************************************************************/
437void tr_torrentRemoveSaved( tr_torrent_t * );
438
439void tr_torrentRecheck( tr_torrent_t * );
440
441/***********************************************************************
442 * tr_torrentClose
443 ***********************************************************************
444 * Frees memory allocated by tr_torrentInit. If the torrent was running,
445 * it is stopped first.
446 **********************************************************************/
447void tr_torrentClose( tr_torrent_t * );
448
449/***********************************************************************
450 * tr_info_s
451 **********************************************************************/
452
453typedef struct tr_file_s
454{
455    uint64_t length;                /* Length of the file, in bytes */
456    char     name[MAX_PATH_LENGTH]; /* Path to the file */
457    int8_t   priority;              /* TR_PRI_HIGH, _NORMAL, _LOW, or _DND */
458    int      firstPiece;            /* We need pieces [firstPiece... */
459    int      lastPiece;             /* ...lastPiece] to dl this file */
460    uint64_t offset;                /* file begins at the torrent's nth byte */
461}
462tr_file_t;
463
464typedef struct tr_piece_s
465{
466    uint8_t  hash[SHA_DIGEST_LENGTH];  /* pieces hash */
467    int8_t   priority;                 /* TR_PRI_HIGH, _NORMAL, _LOW, or _DND */
468}
469tr_piece_t;
470   
471struct tr_info_s
472{
473    /* Path to torrent */
474    char                 torrent[MAX_PATH_LENGTH];
475
476    /* General info */
477    uint8_t              hash[SHA_DIGEST_LENGTH];
478    char                 hashString[2*SHA_DIGEST_LENGTH+1];
479    char                 name[MAX_PATH_LENGTH];
480
481    /* Flags */
482#define TR_FLAG_SAVE    0x01 /* save a copy of the torrent file */
483#define TR_FLAG_PRIVATE 0x02 /* do not share information for this torrent */
484#define TR_FLAG_PAUSED  0x04 /* don't start the torrent when adding it */
485    int                  flags;
486
487    /* Tracker info */
488    struct
489    {
490        tr_tracker_info_t * list;
491        int                 count;
492    }                  * trackerList;
493    int                  trackerTiers;
494
495    /* Torrent info */
496    char                 comment[MAX_PATH_LENGTH];
497    char                 creator[MAX_PATH_LENGTH];
498    int                  dateCreated;
499
500    /* Pieces info */
501    int                  pieceSize;
502    int                  pieceCount;
503    uint64_t             totalSize;
504    tr_piece_t         * pieces;
505
506    /* Files info */
507    int                  multifile;
508    int                  fileCount;
509    tr_file_t          * files;
510};
511
512typedef enum
513{
514    TR_CP_INCOMPLETE,   /* doesn't have all the desired pieces */
515    TR_CP_DONE,         /* has all the pieces but the DND ones */
516    TR_CP_COMPLETE      /* has every piece */
517}
518cp_status_t;
519
520typedef enum
521{
522    TR_STATUS_CHECK_WAIT   = (1<<0), /* Waiting in queue to check files */
523    TR_STATUS_CHECK        = (1<<1), /* Checking files */
524    TR_STATUS_DOWNLOAD     = (1<<2), /* Downloading */
525    TR_STATUS_DONE         = (1<<3), /* not at 100% so can't tell the tracker
526                                        we're a seeder, but due to DND files
527                                        there's nothing we want right now */
528    TR_STATUS_SEED         = (1<<4), /* Seeding */
529    TR_STATUS_STOPPING     = (1<<5), /* Stopping -- closing connections, etc. */
530    TR_STATUS_STOPPED      = (1<<6)  /* Torrent is stopped */
531}
532torrent_status_t;
533
534#define TR_STATUS_ACTIVE \
535    (TR_STATUS_CHECK_WAIT|TR_STATUS_CHECK|TR_STATUS_DOWNLOAD|TR_STATUS_DONE|TR_STATUS_SEED)
536#define TR_STATUS_INACTIVE \
537    (TR_STATUS_STOPPING|TR_STATUS_STOPPED)
538
539/***********************************************************************
540 * tr_stat_s
541 **********************************************************************/
542struct tr_stat_s
543{
544    torrent_status_t    status;
545    cp_status_t         cpStatus;
546
547    int                 error;
548    char                errorString[128];
549    int                 cannotConnect;
550
551    tr_tracker_info_t * tracker;
552
553    float               percentComplete;
554    float               percentDone;
555    float               rateDownload;
556    float               rateUpload;
557    int                 eta;
558    int                 peersTotal;
559    int                 peersFrom[TR_PEER_FROM__MAX];
560    int                 peersUploading;
561    int                 peersDownloading;
562    int                 seeders;
563    int                 leechers;
564    int                 completedFromTracker;
565
566    uint64_t            left;
567    uint64_t            downloaded;
568    uint64_t            downloadedValid;
569    uint64_t            uploaded;
570    float               swarmspeed;
571
572#define TR_RATIO_NA  -1
573    float               ratio;
574   
575    uint64_t            startDate;
576    uint64_t            activityDate;
577};
578
579struct tr_peer_stat_s
580{
581    char    addr[INET_ADDRSTRLEN];
582    const char * client;
583   
584    int     isConnected;
585    int     from;
586    float   progress;
587    int     port;
588   
589    int     isDownloading;
590    int     isUploading;
591    float   downloadFromRate;
592    float   uploadToRate;
593};
594
595struct tr_msg_list_s
596{
597    int                    level;
598    time_t                 when;
599    char                 * message;
600    struct tr_msg_list_s * next;
601};
602
603struct tr_tracker_info_s
604{
605    char * address;
606    int    port;
607    char * announce;
608    char * scrape;
609};
610
611struct tr_handle_status_s
612{
613#define TR_NAT_TRAVERSAL_MAPPING        1
614#define TR_NAT_TRAVERSAL_MAPPED         2
615#define TR_NAT_TRAVERSAL_NOTFOUND       3
616#define TR_NAT_TRAVERSAL_ERROR          4
617#define TR_NAT_TRAVERSAL_UNMAPPING      5
618#define TR_NAT_TRAVERSAL_DISABLED       6
619#define TR_NAT_TRAVERSAL_IS_DISABLED( st ) \
620  ( TR_NAT_TRAVERSAL_DISABLED == (st) || TR_NAT_TRAVERSAL_UNMAPPING == (st) )
621    int natTraversalStatus;
622    int publicPort;
623};
624
625#ifdef __TRANSMISSION__
626#  include "internal.h"
627#endif
628
629#ifdef __cplusplus
630}
631#endif
632
633#endif
Note: See TracBrowser for help on using the repository browser.