source: trunk/libtransmission/transmission.h @ 2154

Last change on this file since 2154 was 2154, checked in by charles, 15 years ago
  • fix error checking large files reported by Gimp_
  • portability changes to pathname/filename building
  • small gratuitous changes
  • Property svn:keywords set to Date Rev Author Id
File size: 23.8 KB
Line 
1/******************************************************************************
2 * $Id: transmission.h 2154 2007-06-18 19:39:52Z 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
216void tr_torrentInitFilePieces( tr_torrent_t * tor );
217
218/* priorities should be an array of tor->info.fileCount bytes,
219 * each holding a value of TR_PRI_NORMAL, _HIGH, _LOW, or _DND. */
220void tr_torrentSetFilePriorities ( tr_torrent_t *, const tr_priority_t * priorities );
221
222/* single-file form of tr_torrentPrioritizeFiles.
223 * priority must be one of TR_PRI_NORMAL, _HIGH, _LOW, or _DND */
224void tr_torrentSetFilePriority( tr_torrent_t *, int file, tr_priority_t priority );
225
226/* returns a malloc()ed array of tor->info.fileCount items,
227 * each holding a value of TR_PRI_NORMAL, _HIGH, _LOW, or _DND.
228   free the array when done. */
229tr_priority_t* tr_torrentGetFilePriorities( const tr_torrent_t * );
230
231/* single-file form of tr_torrentGetFilePriorities.
232 * returns one of TR_PRI_NORMAL, _HIGH, _LOW, or _DND */
233tr_priority_t tr_torrentGetFilePriority( const tr_torrent_t *, int file );
234
235/* returns the priority for the specified piece,
236 * as ranked by the number of files of various priorities in that piece.
237 * a zero priority means DND */
238tr_priority_t tr_torrentGetPiecePriority( const tr_torrent_t *, int pieceIndex );
239
240/***********************************************************************
241 * tr_torrentRates
242 ***********************************************************************
243 * Gets the total download and upload rates
244 **********************************************************************/
245void tr_torrentRates( tr_handle_t *, float *, float * );
246
247/***********************************************************************
248 * tr_close
249 ***********************************************************************
250 * Frees memory allocated by tr_init.
251 **********************************************************************/
252void tr_close( tr_handle_t * );
253
254/***********************************************************************
255 * tr_torrentInit
256 ***********************************************************************
257 * Opens and parses torrent file at 'path'. If the file exists and is
258 * a valid torrent file, returns an handle and adds it to the list of
259 * torrents (but doesn't start it). Returns NULL and sets *error
260 * otherwise. If hash is not NULL and the torrent is already loaded
261 * then it's 20-byte hash will be copied in. If the TR_FLAG_SAVE flag
262 * is passed then a copy of the torrent file will be saved.
263 **********************************************************************/
264#define TR_EINVALID     1
265#define TR_EUNSUPPORTED 2
266#define TR_EDUPLICATE   3
267#define TR_EOTHER       666
268tr_torrent_t * tr_torrentInit( tr_handle_t *, const char * path,
269                               uint8_t * hash, int flags, int * error );
270
271/***********************************************************************
272 * tr_torrentInitData
273 ***********************************************************************
274 * Like tr_torrentInit, except the actual torrent data is passed in
275 * instead of the filename.
276 **********************************************************************/
277tr_torrent_t * tr_torrentInitData( tr_handle_t *, uint8_t * data,
278                                   size_t size, uint8_t * hash,
279                                   int flags, int * error );
280
281/***********************************************************************
282 * tr_torrentInitSaved
283 ***********************************************************************
284 * Opens and parses a torrent file as with tr_torrentInit, only taking
285 * the hash string of a saved torrent file instead of a filename. There
286 * are currently no valid flags for this function.
287 **********************************************************************/
288tr_torrent_t * tr_torrentInitSaved( tr_handle_t *, const char * hashStr,
289                                    int flags, int * error );
290
291/***********************************************************************
292 * tr_torrentDisablePex
293 ***********************************************************************
294 * Disable or enable peer exchange for this torrent. Peer exchange is
295 * enabled by default, except for private torrents where pex is
296 * disabled and cannot be enabled.
297 **********************************************************************/
298void tr_torrentDisablePex( tr_torrent_t *, int disable );
299
300/***********************************************************************
301 * tr_torrentScrape
302 ***********************************************************************
303 * Return torrent metainfo.
304 **********************************************************************/
305typedef struct tr_info_s tr_info_t;
306tr_info_t * tr_torrentInfo( tr_torrent_t * );
307
308/***********************************************************************
309 * tr_torrentScrape
310 ***********************************************************************
311 * Asks the tracker for the count of seeders and leechers. Returns 0
312 * and fills 's' and 'l' if successful. Otherwise returns 1 if the
313 * tracker doesn't support the scrape protocol, is unreachable or
314 * replied with some error. tr_torrentScrape may block up to 20 seconds
315 * before returning.
316 **********************************************************************/
317int tr_torrentScrape( tr_torrent_t *, int * s, int * l, int * d );
318
319void   tr_torrentSetFolder( tr_torrent_t *, const char * );
320char * tr_torrentGetFolder( tr_torrent_t * );
321
322int tr_torrentDuplicateDownload( tr_torrent_t * tor );
323
324/***********************************************************************
325 * tr_torrentStart
326 ***********************************************************************
327 * Starts downloading. The download is launched in a seperate thread,
328 * therefore tr_torrentStart returns immediately.
329 **********************************************************************/
330void   tr_torrentStart( tr_torrent_t * );
331
332/***********************************************************************
333 * tr_torrentStop
334 ***********************************************************************
335 * Stops downloading and notices the tracker that we are leaving. The
336 * thread keeps running while doing so.
337 * The thread will eventually be joined, either:
338 * - by tr_torrentStat when the tracker has been successfully noticed,
339 * - by tr_torrentStat if the tracker could not be noticed within 60s,
340 * - by tr_torrentClose if you choose to remove the torrent without
341 *   waiting any further.
342 **********************************************************************/
343void tr_torrentStop( tr_torrent_t * );
344
345/***********************************************************************
346 * tr_getComplete, tr_getIncomplete and tr_getPartial
347 ***********************************************************************
348 * The first call after a torrent changed state returns 1. Returns 0
349 * in other cases.
350 **********************************************************************/
351int tr_getIncomplete( tr_torrent_t * tor );
352int tr_getDone( tr_torrent_t * tor );
353int tr_getComplete( tr_torrent_t * tor );
354
355
356/***********************************************************************
357 * tr_manualUpdate
358 ***********************************************************************
359 * Reannounce to tracker regardless of wait interval
360 **********************************************************************/
361void tr_manualUpdate( tr_torrent_t * );
362
363/***********************************************************************
364 * tr_torrentStat
365 ***********************************************************************
366 * Returns a pointer to an tr_stat_t structure with updated information
367 * on the torrent. The structure belongs to libtransmission (do not
368 * free it) and is guaranteed to be unchanged until the next call to
369 * tr_torrentStat.
370 * The interface should call this function every second or so in order
371 * to update itself.
372 **********************************************************************/
373typedef struct tr_stat_s tr_stat_t;
374tr_stat_t * tr_torrentStat( tr_torrent_t * );
375
376/***********************************************************************
377 * tr_torrentPeers
378 ***********************************************************************/
379typedef struct tr_peer_stat_s tr_peer_stat_t;
380tr_peer_stat_t * tr_torrentPeers( tr_torrent_t *, int * peerCount );
381void tr_torrentPeersFree( tr_peer_stat_t *, int peerCount );
382
383/***********************************************************************
384 * tr_torrentAvailability
385 ***********************************************************************
386 * Use this to draw an advanced progress bar which is 'size' pixels
387 * wide. Fills 'tab' which you must have allocated: each byte is set
388 * to either -1 if we have the piece, otherwise it is set to the number
389 * of connected peers who have the piece.
390 **********************************************************************/
391void tr_torrentAvailability( tr_torrent_t *, int8_t * tab, int size );
392
393void tr_torrentAmountFinished( tr_torrent_t * tor, float * tab, int size );
394
395/***********************************************************************
396 * tr_torrentCompletion
397 ***********************************************************************
398 * Returns the completion progress for each file in the torrent as an
399 * array of floats the same size and order as in tr_info_t. Free the
400 * array when done.
401 **********************************************************************/
402float * tr_torrentCompletion( tr_torrent_t * );
403
404float tr_torrentFileCompletion( const tr_torrent_t *, int fileIndex );
405
406uint64_t tr_torrentFileBytesCompleted( const tr_torrent_t *, int fileIndex ); 
407
408
409/***********************************************************************
410 * tr_torrentRemoveSaved
411 ***********************************************************************
412 * Removes the saved copy of a torrent file for torrents which the
413 * TR_FLAG_SAVE flag is set.
414 **********************************************************************/
415void tr_torrentRemoveSaved( tr_torrent_t * );
416
417void tr_torrentRemoveFastResume( tr_torrent_t * tor );
418
419/***********************************************************************
420 * tr_torrentClose
421 ***********************************************************************
422 * Frees memory allocated by tr_torrentInit. If the torrent was running,
423 * you must call tr_torrentStop() before closing it.
424 **********************************************************************/
425void tr_torrentClose( tr_torrent_t * );
426
427/***********************************************************************
428 * tr_info_s
429 **********************************************************************/
430
431typedef struct tr_file_s
432{
433    uint64_t length;                /* Length of the file, in bytes */
434    char     name[MAX_PATH_LENGTH]; /* Path to the file */
435    int8_t   priority;              /* TR_PRI_HIGH, _NORMAL, _LOW, or _DND */
436    int      firstPiece;            /* We need pieces [firstPiece... */
437    int      lastPiece;             /* ...lastPiece] to dl this file */
438    uint64_t offset;                /* file begins at the torrent's nth byte */
439}
440tr_file_t;
441
442typedef struct tr_piece_s
443{
444    uint8_t  hash[SHA_DIGEST_LENGTH];  /* pieces hash */
445    int8_t   priority;                 /* TR_PRI_HIGH, _NORMAL, _LOW, or _DND */
446}
447tr_piece_t;
448   
449struct tr_info_s
450{
451    /* Path to torrent */
452    char                 torrent[MAX_PATH_LENGTH];
453
454    /* General info */
455    uint8_t              hash[SHA_DIGEST_LENGTH];
456    char                 hashString[2*SHA_DIGEST_LENGTH+1];
457    char                 name[MAX_PATH_LENGTH];
458
459    /* Flags */
460#define TR_FLAG_SAVE    0x01 /* save a copy of the torrent file */
461#define TR_FLAG_PRIVATE 0x02 /* do not share information for this torrent */
462    int                  flags;
463
464    /* Tracker info */
465    struct
466    {
467        tr_tracker_info_t * list;
468        int                 count;
469    }                  * trackerList;
470    int                  trackerTiers;
471
472    /* Torrent info */
473    char                 comment[MAX_PATH_LENGTH];
474    char                 creator[MAX_PATH_LENGTH];
475    int                  dateCreated;
476
477    /* Pieces info */
478    int                  pieceSize;
479    int                  pieceCount;
480    uint64_t             totalSize;
481    tr_piece_t         * pieces;
482
483    /* Files info */
484    int                  multifile;
485    int                  fileCount;
486    tr_file_t          * files;
487};
488
489typedef enum
490{
491   TR_CP_COMPLETE,     /* has every piece */
492   TR_CP_DONE,         /* has all the pieces but the DND ones */
493   TR_CP_INCOMPLETE    /* doesn't have all the desired pieces */
494}
495cp_status_t;
496
497/***********************************************************************
498 * tr_stat_s
499 **********************************************************************/
500struct tr_stat_s
501{
502#define TR_STATUS_CHECK_WAIT (1<<0) /* Waiting in queue to check files */
503#define TR_STATUS_CHECK      (1<<1) /* Checking files */
504#define TR_STATUS_DOWNLOAD   (1<<2) /* Downloading */
505#define TR_STATUS_DONE       (1<<3) /* not at 100% so can't tell the tracker
506                                       we're a seeder, but due to DND files
507                                       there's nothing we want right now */
508#define TR_STATUS_SEED       (1<<4) /* Seeding */
509#define TR_STATUS_STOPPING   (1<<5) /* Sending 'stopped' to the tracker */
510#define TR_STATUS_STOPPED    (1<<6) /* Sent 'stopped' but thread still
511                                       running (for internal use only) */
512#define TR_STATUS_PAUSE      (1<<7) /* Paused */
513
514#define TR_STATUS_ACTIVE   (TR_STATUS_CHECK_WAIT|TR_STATUS_CHECK|TR_STATUS_DOWNLOAD|TR_STATUS_DONE|TR_STATUS_SEED)
515#define TR_STATUS_INACTIVE (TR_STATUS_STOPPING|TR_STATUS_STOPPED|TR_STATUS_PAUSE)
516    int                 status;
517    cp_status_t         cpStatus;
518
519    int                 error;
520    char                errorString[128];
521    int                 cannotConnect;
522
523    tr_tracker_info_t * tracker;
524
525    float               percentComplete;
526    float               percentDone;
527    float               rateDownload;
528    float               rateUpload;
529    int                 eta;
530    int                 peersTotal;
531    int                 peersFrom[TR_PEER_FROM__MAX];
532    int                 peersUploading;
533    int                 peersDownloading;
534    int                 seeders;
535    int                 leechers;
536    int                 completedFromTracker;
537
538    uint64_t            left;
539    uint64_t            downloaded;
540    uint64_t            downloadedValid;
541    uint64_t            uploaded;
542    float               swarmspeed;
543
544#define TR_RATIO_NA  -1
545    float               ratio;
546   
547    uint64_t            startDate;
548    uint64_t            activityDate;
549};
550
551struct tr_peer_stat_s
552{
553    char    addr[INET_ADDRSTRLEN];
554    const char * client;
555   
556    int     isConnected;
557    int     from;
558    float   progress;
559    int     port;
560   
561    int     isDownloading;
562    int     isUploading;
563    float   downloadFromRate;
564    float   uploadToRate;
565};
566
567struct tr_msg_list_s
568{
569    int                    level;
570    time_t                 when;
571    char                 * message;
572    struct tr_msg_list_s * next;
573};
574
575struct tr_tracker_info_s
576{
577    char * address;
578    int    port;
579    char * announce;
580    char * scrape;
581};
582
583struct tr_handle_status_s
584{
585#define TR_NAT_TRAVERSAL_MAPPING        1
586#define TR_NAT_TRAVERSAL_MAPPED         2
587#define TR_NAT_TRAVERSAL_NOTFOUND       3
588#define TR_NAT_TRAVERSAL_ERROR          4
589#define TR_NAT_TRAVERSAL_UNMAPPING      5
590#define TR_NAT_TRAVERSAL_DISABLED       6
591#define TR_NAT_TRAVERSAL_IS_DISABLED( st ) \
592  ( TR_NAT_TRAVERSAL_DISABLED == (st) || TR_NAT_TRAVERSAL_UNMAPPING == (st) )
593    int natTraversalStatus;
594    int publicPort;
595};
596
597#ifdef __TRANSMISSION__
598#  include "internal.h"
599#endif
600
601#ifdef __cplusplus
602}
603#endif
604
605#endif
Note: See TracBrowser for help on using the repository browser.