1 | /****************************************************************************** |
---|
2 | * Copyright (c) 2005-2006 Transmission authors and contributors |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
5 | * copy of this software and associated documentation files (the "Software"), |
---|
6 | * to deal in the Software without restriction, including without limitation |
---|
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
8 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
9 | * Software is furnished to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in |
---|
12 | * all copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
20 | * DEALINGS IN THE SOFTWARE. |
---|
21 | *****************************************************************************/ |
---|
22 | |
---|
23 | #ifndef TR_INTERNAL_H |
---|
24 | #define TR_INTERNAL_H 1 |
---|
25 | |
---|
26 | /* Standard headers used here and there. |
---|
27 | That is probably ugly to put them all here, but it is sooo |
---|
28 | convenient */ |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdarg.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #include <string.h> |
---|
33 | #include <unistd.h> |
---|
34 | #include <errno.h> |
---|
35 | #include <limits.h> |
---|
36 | #include <signal.h> |
---|
37 | #include <time.h> |
---|
38 | #include <sys/time.h> |
---|
39 | #include <sys/types.h> |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <sys/resource.h> |
---|
42 | #include <netdb.h> |
---|
43 | #include <sys/socket.h> |
---|
44 | #include <netinet/in.h> |
---|
45 | #include <fcntl.h> |
---|
46 | #ifdef BEOS_NETSERVER |
---|
47 | # define in_port_t uint16_t |
---|
48 | #else |
---|
49 | # include <arpa/inet.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | /* We use OpenSSL whenever possible, since it is likely to be more |
---|
53 | optimized and it is ok to use it with a MIT-licensed application. |
---|
54 | Otherwise, we use the included implementation by vi@nwr.jp. */ |
---|
55 | #ifdef HAVE_OPENSSL |
---|
56 | # undef SHA_DIGEST_LENGTH |
---|
57 | # include <openssl/sha.h> |
---|
58 | #else |
---|
59 | # include "sha1.h" |
---|
60 | # define SHA1(p,i,h) \ |
---|
61 | { \ |
---|
62 | sha1_state_s pms; \ |
---|
63 | sha1_init( &pms ); \ |
---|
64 | sha1_update( &pms, (sha1_byte_t *) p, i ); \ |
---|
65 | sha1_finish( &pms, (sha1_byte_t *) h ); \ |
---|
66 | } |
---|
67 | #endif |
---|
68 | |
---|
69 | /* Convenient macros to perform uint32_t endian conversions with |
---|
70 | char pointers */ |
---|
71 | #define TR_NTOHL(p,a) (a) = ntohl(*((uint32_t*)(p))) |
---|
72 | #define TR_HTONL(a,p) *((uint32_t*)(p)) = htonl((a)) |
---|
73 | |
---|
74 | /* Sometimes the system defines MAX/MIN, sometimes not. In the latter |
---|
75 | case, define those here since we will use them */ |
---|
76 | #ifndef MAX |
---|
77 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
---|
78 | #endif |
---|
79 | #ifndef MIN |
---|
80 | #define MIN(a,b) ((a)>(b)?(b):(a)) |
---|
81 | #endif |
---|
82 | |
---|
83 | #define TR_MAX_PEER_COUNT 60 |
---|
84 | |
---|
85 | typedef struct tr_completion_s tr_completion_t; |
---|
86 | |
---|
87 | #include "platform.h" |
---|
88 | #include "bencode.h" |
---|
89 | #include "metainfo.h" |
---|
90 | #include "tracker.h" |
---|
91 | #include "fdlimit.h" |
---|
92 | #include "peer.h" |
---|
93 | #include "net.h" |
---|
94 | #include "inout.h" |
---|
95 | #include "ratecontrol.h" |
---|
96 | #include "clients.h" |
---|
97 | #include "choking.h" |
---|
98 | |
---|
99 | struct tr_torrent_s |
---|
100 | { |
---|
101 | tr_info_t info; |
---|
102 | |
---|
103 | tr_ratecontrol_t * globalUpload; |
---|
104 | tr_ratecontrol_t * globalDownload; |
---|
105 | tr_ratecontrol_t * upload; |
---|
106 | tr_ratecontrol_t * download; |
---|
107 | tr_fd_t * fdlimit; |
---|
108 | |
---|
109 | int status; |
---|
110 | int error; |
---|
111 | char trackerError[128]; |
---|
112 | int finished; |
---|
113 | |
---|
114 | char * id; |
---|
115 | char * key; |
---|
116 | int * bindPort; |
---|
117 | |
---|
118 | /* An escaped string used to include the hash in HTTP queries */ |
---|
119 | char hashString[3*SHA_DIGEST_LENGTH+1]; |
---|
120 | |
---|
121 | char scrape[MAX_PATH_LENGTH]; |
---|
122 | |
---|
123 | /* Where to download */ |
---|
124 | char * destination; |
---|
125 | |
---|
126 | /* How many bytes we ask for per request */ |
---|
127 | int blockSize; |
---|
128 | int blockCount; |
---|
129 | |
---|
130 | tr_completion_t * completion; |
---|
131 | |
---|
132 | volatile char die; |
---|
133 | tr_thread_t thread; |
---|
134 | tr_lock_t lock; |
---|
135 | |
---|
136 | tr_tracker_t * tracker; |
---|
137 | tr_io_t * io; |
---|
138 | uint64_t stopDate; |
---|
139 | |
---|
140 | int peerCount; |
---|
141 | tr_peer_t * peers[TR_MAX_PEER_COUNT]; |
---|
142 | |
---|
143 | uint64_t date; |
---|
144 | uint64_t downloaded; |
---|
145 | uint64_t uploaded; |
---|
146 | |
---|
147 | tr_stat_t stats[2]; |
---|
148 | int statCur; |
---|
149 | |
---|
150 | tr_torrent_t * prev; |
---|
151 | tr_torrent_t * next; |
---|
152 | }; |
---|
153 | |
---|
154 | #include "utils.h" |
---|
155 | #include "completion.h" |
---|
156 | |
---|
157 | struct tr_handle_s |
---|
158 | { |
---|
159 | int torrentCount; |
---|
160 | tr_torrent_t * torrentList; |
---|
161 | |
---|
162 | tr_ratecontrol_t * upload; |
---|
163 | tr_ratecontrol_t * download; |
---|
164 | tr_fd_t * fdlimit; |
---|
165 | tr_choking_t * choking; |
---|
166 | |
---|
167 | int bindPort; |
---|
168 | int bindSocket; |
---|
169 | |
---|
170 | int acceptPeerCount; |
---|
171 | tr_peer_t * acceptPeers[TR_MAX_PEER_COUNT]; |
---|
172 | |
---|
173 | char id[21]; |
---|
174 | char key[21]; |
---|
175 | |
---|
176 | volatile char acceptDie; |
---|
177 | tr_thread_t acceptThread; |
---|
178 | tr_lock_t acceptLock; |
---|
179 | }; |
---|
180 | |
---|
181 | #endif |
---|