source: trunk/libtransmission/transmission.c @ 2004

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

uninteresting minor stuff like adding const. this commit is just to reduce the shear between trunk and what lands in the file-selection branch.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.1 KB
Line 
1/******************************************************************************
2 * $Id: transmission.c 2004 2007-06-09 15:36:46Z 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#include "transmission.h"
26#include "shared.h"
27
28/***********************************************************************
29 * tr_init
30 ***********************************************************************
31 * Allocates a tr_handle_t structure and initializes a few things
32 **********************************************************************/
33tr_handle_t * tr_init( const char * tag )
34{
35    tr_handle_t * h;
36    int           i, r;
37
38    tr_msgInit();
39    tr_netResolveThreadInit();
40
41    h = calloc( 1, sizeof( tr_handle_t ) );
42    if( NULL == h )
43    {
44        return NULL;
45    }
46
47    h->tag = strdup( tag );
48    if( NULL == h->tag )
49    {
50        free( h );
51        return NULL;
52    }
53
54    /* Generate a peer id : "-TRxxyz-" + 12 random alphanumeric
55       characters, where xx is the major version number, y is the
56       minor version number, and z is the maintenance number (Azureus-style) */
57    snprintf( h->id, sizeof h->id, "-TR%02d%01d%01d-",
58              VERSION_MAJOR, VERSION_MINOR, VERSION_MAINTENANCE );
59    for( i = 8; i < TR_ID_LEN; i++ )
60    {
61        r        = tr_rand( 36 );
62        h->id[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
63    }
64
65    /* Random key */
66    for( i = 0; i < TR_KEY_LEN; i++ )
67    {
68        r         = tr_rand( 36 );
69        h->key[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
70    }
71
72    /* Azureus identity */
73    for( i = 0; i < TR_AZ_ID_LEN; i++ )
74    {
75        h->azId[i] = tr_rand( 0xff );
76    }
77
78    /* Don't exit when writing on a broken socket */
79    signal( SIGPIPE, SIG_IGN );
80
81    /* Initialize rate and file descripts controls */
82    h->upload   = tr_rcInit();
83    h->download = tr_rcInit();
84
85    tr_fdInit();
86    h->shared = tr_sharedInit( h );
87
88    return h;
89}
90
91/***********************************************************************
92 * tr_setBindPort
93 ***********************************************************************
94 *
95 **********************************************************************/
96void tr_setBindPort( tr_handle_t * h, int port )
97{
98    h->isPortSet = 1;
99    tr_sharedSetPort( h->shared, port );
100}
101
102void tr_natTraversalEnable( tr_handle_t * h, int enable )
103{
104    tr_sharedLock( h->shared );
105    tr_sharedTraversalEnable( h->shared, enable );
106    tr_sharedUnlock( h->shared );
107}
108
109tr_handle_status_t * tr_handleStatus( tr_handle_t * h )
110{
111    tr_handle_status_t * s;
112
113    h->statCur = ( h->statCur + 1 ) % 2;
114    s = &h->stats[h->statCur];
115
116    tr_sharedLock( h->shared );
117
118    s->natTraversalStatus = tr_sharedTraversalStatus( h->shared );
119    s->publicPort = tr_sharedGetPublicPort( h->shared );
120
121    tr_sharedUnlock( h->shared );
122
123    return s;
124}
125
126void tr_setGlobalUploadLimit( tr_handle_t * h, int limit )
127{
128    tr_rcSetLimit( h->upload, limit );
129    tr_sharedSetLimit( h->shared, limit );
130}
131
132void tr_setGlobalDownloadLimit( tr_handle_t * h, int limit )
133{
134    tr_rcSetLimit( h->download, limit );
135}
136
137void tr_torrentRates( tr_handle_t * h, float * dl, float * ul )
138{
139    tr_torrent_t * tor;
140
141    *dl = 0.0;
142    *ul = 0.0;
143    tr_sharedLock( h->shared );
144    for( tor = h->torrentList; tor; tor = tor->next )
145    {
146        tr_lockLock( &tor->lock );
147        if( tor->status & TR_STATUS_DOWNLOAD )
148            *dl += tr_rcRate( tor->download );
149        *ul += tr_rcRate( tor->upload );
150        tr_lockUnlock( &tor->lock );
151    }
152    tr_sharedUnlock( h->shared );
153}
154
155int tr_torrentCount( tr_handle_t * h )
156{
157    return h->torrentCount;
158}
159
160void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d )
161{
162    tr_torrent_t * tor, * next;
163
164    for( tor = h->torrentList; tor; tor = next )
165    {
166        next = tor->next;
167        func( tor, d );
168    }
169}
170
171void tr_close( tr_handle_t * h )
172{
173    tr_rcClose( h->upload );
174    tr_rcClose( h->download );
175   
176    tr_sharedClose( h->shared );
177    tr_fdClose();
178    free( h->tag );
179    free( h );
180
181    tr_netResolveThreadClose();
182}
Note: See TracBrowser for help on using the repository browser.