1/* 2 * This file Copyright (C) Mnemosyne LLC 3 * 4 * This file is licensed by the GPL version 2. Works owned by the 5 * Transmission project are granted a special exemption to clause 2(b) 6 * so that the bulk of its code can remain under the MIT license. 7 * This exemption does not extend to derived works not owned by 8 * the Transmission project. 9 * 10 * $Id: session.h 13499 2012-09-18 00:58:10Z livings124 $ 11 */ 12 13#ifndef __TRANSMISSION__ 14 #error only libtransmission should #include this header. 15#endif 16 17#ifndef TR_INTERNAL_H 18#define TR_INTERNAL_H 1 19 20#define TR_NAME "Transmission" 21 22#ifndef UNUSED 23 #ifdef __GNUC__ 24 #define UNUSED __attribute__ ( ( unused ) ) 25 #else 26 #define UNUSED 27 #endif 28#endif 29 30#include "bandwidth.h" 31#include "bencode.h" 32#include "bitfield.h" 33#include "utils.h" 34 35typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t; 36 37typedef enum { 38 TR_AUTO_SWITCH_UNUSED, 39 TR_AUTO_SWITCH_ON, 40 TR_AUTO_SWITCH_OFF, 41} tr_auto_switch_state_t; 42 43enum 44{ 45 PEER_ID_LEN = 20 46}; 47 48void tr_peerIdInit( uint8_t * setme ); 49 50struct event_base; 51struct evdns_base; 52 53struct tr_address; 54struct tr_announcer; 55struct tr_announcer_udp; 56struct tr_bindsockets; 57struct tr_cache; 58struct tr_fdInfo; 59 60typedef void ( tr_web_config_func )( tr_session * session, void * curl_pointer, const char * url, void * user_data ); 61 62struct tr_turtle_info 63{ 64 /* TR_UP and TR_DOWN speed limits */ 65 unsigned int speedLimit_Bps[2]; 66 67 /* is turtle mode on right now? */ 68 bool isEnabled; 69 70 /* does turtle mode turn itself on and off at given times? */ 71 bool isClockEnabled; 72 73 /* when clock mode is on, minutes after midnight to turn on turtle mode */ 74 int beginMinute; 75 76 /* when clock mode is on, minutes after midnight to turn off turtle mode */ 77 int endMinute; 78 79 /* only use clock mode on these days of the week */ 80 tr_sched_day days; 81 82 /* called when isEnabled changes */ 83 tr_altSpeedFunc * callback; 84 85 /* the callback's user_data argument */ 86 void * callbackUserData; 87 88 /* the callback's changedByUser argument. 89 * indicates whether the change came from the user or from the clock. */ 90 bool changedByUser; 91 92 /* bitfield of all the minutes in a week. 93 * Each bit's value indicates whether the scheduler wants turtle 94 * limits on or off at that given minute in the week. */ 95 tr_bitfield minutes; 96 97 /* recent action that was done by turtle's automatic switch */ 98 tr_auto_switch_state_t autoTurtleState; 99}; 100 101/** @brief handle to an active libtransmission session */ 102struct tr_session 103{ 104 bool isPortRandom; 105 bool isPexEnabled; 106 bool isDHTEnabled; 107 bool isUTPEnabled; 108 bool isLPDEnabled; 109 bool isBlocklistEnabled; 110 bool isPrefetchEnabled; 111 bool isTorrentDoneScriptEnabled; 112 bool isClosed; 113 bool isIncompleteFileNamingEnabled; 114 bool isRatioLimited; 115 bool isIdleLimited; 116 bool isIncompleteDirEnabled; 117 bool pauseAddedTorrent; 118 bool deleteSourceTorrent; 119 bool scrapePausedTorrents; 120 121 tr_benc removedTorrents; 122 123 bool stalledEnabled; 124 bool queueEnabled[2]; 125 int queueSize[2]; 126 int queueStalledMinutes; 127 128 int umask; 129 130 unsigned int speedLimit_Bps[2]; 131 bool speedLimitEnabled[2]; 132 133 struct tr_turtle_info turtle; 134 135 struct tr_fdInfo * fdInfo; 136 137 int magicNumber; 138 139 tr_encryption_mode encryptionMode; 140 141 tr_preallocation_mode preallocationMode; 142 143 struct event_base * event_base; 144 struct evdns_base * evdns_base; 145 struct tr_event_handle * events; 146 147 uint16_t peerLimit; 148 uint16_t peerLimitPerTorrent; 149 150 int uploadSlotsPerTorrent; 151 152 /* The UDP sockets used for the DHT and uTP. */ 153 tr_port udp_port; 154 int udp_socket; 155 int udp6_socket; 156 unsigned char * udp6_bound; 157 struct event *udp_event; 158 struct event *udp6_event; 159 160 /* The open port on the local machine for incoming peer requests */ 161 tr_port private_peer_port; 162 163 /** 164 * The open port on the public device for incoming peer requests. 165 * This is usually the same as private_peer_port but can differ 166 * if the public device is a router and it decides to use a different 167 * port than the one requested by Transmission. 168 */ 169 tr_port public_peer_port; 170 171 tr_port randomPortLow; 172 tr_port randomPortHigh; 173 174 int peerSocketTOS; 175 char * peer_congestion_algorithm; 176 177 int torrentCount; 178 tr_torrent * torrentList; 179 180 char * torrentDoneScript; 181 182 char * tag; 183 char * configDir; 184 char * downloadDir; 185 char * resumeDir; 186 char * torrentDir; 187 char * incompleteDir; 188 189 char * blocklist_url; 190 191 struct tr_list * blocklists; 192 struct tr_peerMgr * peerMgr; 193 struct tr_shared * shared; 194 195 struct tr_cache * cache; 196 197 struct tr_lock * lock; 198 199 struct tr_web * web; 200 201 struct tr_rpc_server * rpcServer; 202 tr_rpc_func rpc_func; 203 void * rpc_func_user_data; 204 205 struct tr_stats_handle * sessionStats; 206 207 struct tr_announcer * announcer; 208 struct tr_announcer_udp * announcer_udp; 209 210 tr_benc * metainfoLookup; 211 212 struct event * nowTimer; 213 struct event * saveTimer; 214 215 /* monitors the "global pool" speeds */ 216 struct tr_bandwidth bandwidth; 217 218 float desiredRatio; 219 220 uint16_t idleLimitMinutes; 221 222 struct tr_bindinfo * public_ipv4; 223 struct tr_bindinfo * public_ipv6; 224 225 uint8_t peer_id[PEER_ID_LEN+1]; 226}; 227 228static inline tr_port 229tr_sessionGetPublicPeerPort( const tr_session * session ) 230{ 231 return session->public_peer_port; 232} 233 234static inline const uint8_t* 235tr_getPeerId( tr_session * session ) 236{ 237 return session->peer_id; 238} 239 240bool tr_sessionAllowsDHT( const tr_session * session ); 241 242bool tr_sessionAllowsLPD( const tr_session * session ); 243 244const char * tr_sessionFindTorrentFile( const tr_session * session, 245 const char * hashString ); 246 247void tr_sessionSetTorrentFile( tr_session * session, 248 const char * hashString, 249 const char * filename ); 250 251bool tr_sessionIsAddressBlocked( const tr_session * session, 252 const struct tr_address * addr ); 253 254void tr_sessionLock( tr_session * ); 255 256void tr_sessionUnlock( tr_session * ); 257 258bool tr_sessionIsLocked( const tr_session * ); 259 260const struct tr_address* tr_sessionGetPublicAddress( const tr_session * session, 261 int tr_af_type, 262 bool * is_default_value ); 263 264 265struct tr_bindsockets * tr_sessionGetBindSockets( tr_session * ); 266 267int tr_sessionCountTorrents( const tr_session * session ); 268 269enum 270{ 271 SESSION_MAGIC_NUMBER = 3845, 272}; 273 274static inline bool tr_isSession( const tr_session * session ) 275{ 276 return ( session != NULL ) && ( session->magicNumber == SESSION_MAGIC_NUMBER ); 277} 278 279static inline bool tr_isPreallocationMode( tr_preallocation_mode m ) 280{ 281 return ( m == TR_PREALLOCATE_NONE ) 282 || ( m == TR_PREALLOCATE_SPARSE ) 283 || ( m == TR_PREALLOCATE_FULL ); 284} 285 286static inline bool tr_isEncryptionMode( tr_encryption_mode m ) 287{ 288 return ( m == TR_CLEAR_PREFERRED ) 289 || ( m == TR_ENCRYPTION_PREFERRED ) 290 || ( m == TR_ENCRYPTION_REQUIRED ); 291} 292 293static inline bool tr_isPriority( tr_priority_t p ) 294{ 295 return ( p == TR_PRI_LOW ) 296 || ( p == TR_PRI_NORMAL ) 297 || ( p == TR_PRI_HIGH ); 298} 299 300/*** 301**** 302***/ 303 304static inline unsigned int 305toSpeedBytes ( unsigned int KBps ) { return KBps * tr_speed_K; } 306static inline double 307toSpeedKBps ( unsigned int Bps ) { return Bps / (double)tr_speed_K; } 308 309static inline uint64_t 310toMemBytes ( unsigned int MB ) { uint64_t B = tr_mem_K * tr_mem_K; B *= MB; return B; } 311static inline int 312toMemMB ( uint64_t B ) { return B / ( tr_mem_K * tr_mem_K ); } 313 314/** 315**/ 316 317unsigned int tr_sessionGetSpeedLimit_Bps( const tr_session *, tr_direction ); 318unsigned int tr_sessionGetAltSpeed_Bps ( const tr_session *, tr_direction ); 319unsigned int tr_sessionGetRawSpeed_Bps ( const tr_session *, tr_direction ); 320unsigned int tr_sessionGetPieceSpeed_Bps( const tr_session *, tr_direction ); 321 322void tr_sessionSetSpeedLimit_Bps( tr_session *, tr_direction, unsigned int Bps ); 323void tr_sessionSetAltSpeed_Bps ( tr_session *, tr_direction, unsigned int Bps ); 324 325bool tr_sessionGetActiveSpeedLimit_Bps( const tr_session * session, 326 tr_direction dir, 327 unsigned int * setme ); 328 329tr_torrent * tr_sessionGetNextQueuedSeed( tr_session * session ); 330tr_torrent * tr_sessionGetNextQueuedTorrent( tr_session * session, tr_direction ); 331 332int tr_sessionCountQueueFreeSlots( tr_session * session, tr_direction ); 333 334 335#endif 336