1 | /****************************************************************************** |
---|
2 | * Copyright (c) 2005 Eric Petit |
---|
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 | /* Multithreading support: native threads on BeOS, pthreads elsewhere */ |
---|
75 | #ifdef SYS_BEOS |
---|
76 | # include <kernel/OS.h> |
---|
77 | # define tr_thread_t thread_id |
---|
78 | # define tr_threadCreate(pt,f,d) *(pt) = spawn_thread((void*)f,"",10,d); \ |
---|
79 | resume_thread(*(pt)); |
---|
80 | # define tr_threadJoin(t) { long e; wait_for_thread(t,&e); } |
---|
81 | # define tr_lock_t sem_id |
---|
82 | # define tr_lockInit(pl) *(pl) = create_sem(1,"") |
---|
83 | # define tr_lockLock(l) acquire_sem(l) |
---|
84 | # define tr_lockUnlock(l) release_sem(l) |
---|
85 | # define tr_lockClose(l) delete_sem(l) |
---|
86 | #else |
---|
87 | # include <pthread.h> |
---|
88 | # define tr_thread_t pthread_t |
---|
89 | # define tr_threadCreate(pt,f,d) pthread_create(pt,NULL,(void*)f,d) |
---|
90 | # define tr_threadJoin(t) pthread_join(t,NULL) |
---|
91 | # define tr_lock_t pthread_mutex_t |
---|
92 | # define tr_lockInit(pl) pthread_mutex_init(pl,NULL) |
---|
93 | # define tr_lockLock(l) pthread_mutex_lock(&l) |
---|
94 | # define tr_lockUnlock(l) pthread_mutex_unlock(&l) |
---|
95 | # define tr_lockClose(l) pthread_mutex_destroy(&l) |
---|
96 | #endif |
---|
97 | |
---|
98 | /* Sometimes the system defines MAX/MIN, sometimes not. In the latter |
---|
99 | case, define those here since we will use them */ |
---|
100 | #ifndef MAX |
---|
101 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
---|
102 | #endif |
---|
103 | #ifndef MIN |
---|
104 | #define MIN(a,b) ((a)>(b)?(b):(a)) |
---|
105 | #endif |
---|
106 | |
---|
107 | #define TR_MAX_PEER_COUNT 60 |
---|
108 | |
---|
109 | typedef struct tr_torrent_s tr_torrent_t; |
---|
110 | |
---|
111 | #include "bencode.h" |
---|
112 | #include "metainfo.h" |
---|
113 | #include "tracker.h" |
---|
114 | #include "peer.h" |
---|
115 | #include "net.h" |
---|
116 | #include "inout.h" |
---|
117 | #include "upload.h" |
---|
118 | #include "fdlimit.h" |
---|
119 | #include "clients.h" |
---|
120 | |
---|
121 | struct tr_torrent_s |
---|
122 | { |
---|
123 | tr_info_t info; |
---|
124 | |
---|
125 | tr_upload_t * upload; |
---|
126 | tr_fd_t * fdlimit; |
---|
127 | |
---|
128 | int status; |
---|
129 | char error[128]; |
---|
130 | |
---|
131 | char * id; |
---|
132 | |
---|
133 | /* An escaped string used to include the hash in HTTP queries */ |
---|
134 | char hashString[3*SHA_DIGEST_LENGTH+1]; |
---|
135 | |
---|
136 | char scrape[MAX_PATH_LENGTH]; |
---|
137 | |
---|
138 | /* Where to download */ |
---|
139 | char * destination; |
---|
140 | |
---|
141 | /* How many bytes we ask for per request */ |
---|
142 | int blockSize; |
---|
143 | int blockCount; |
---|
144 | |
---|
145 | /* Status for each block |
---|
146 | -1 = we have it |
---|
147 | n = we are downloading it from n peers */ |
---|
148 | char * blockHave; |
---|
149 | int blockHaveCount; |
---|
150 | uint8_t * bitfield; |
---|
151 | |
---|
152 | volatile char die; |
---|
153 | tr_thread_t thread; |
---|
154 | tr_lock_t lock; |
---|
155 | |
---|
156 | tr_tracker_t * tracker; |
---|
157 | tr_io_t * io; |
---|
158 | uint64_t stopDate; |
---|
159 | |
---|
160 | int bindSocket; |
---|
161 | int bindPort; |
---|
162 | int peerCount; |
---|
163 | tr_peer_t * peers[TR_MAX_PEER_COUNT]; |
---|
164 | |
---|
165 | uint64_t dates[10]; |
---|
166 | uint64_t downloaded[10]; |
---|
167 | uint64_t uploaded[10]; |
---|
168 | |
---|
169 | char * prefsDirectory; |
---|
170 | }; |
---|
171 | |
---|
172 | #include "utils.h" |
---|
173 | |
---|
174 | struct tr_handle_s |
---|
175 | { |
---|
176 | int torrentCount; |
---|
177 | tr_torrent_t * torrents[TR_MAX_TORRENT_COUNT]; |
---|
178 | |
---|
179 | tr_upload_t * upload; |
---|
180 | tr_fd_t * fdlimit; |
---|
181 | |
---|
182 | int bindPort; |
---|
183 | |
---|
184 | char id[21]; |
---|
185 | char prefsDirectory[256]; |
---|
186 | }; |
---|
187 | |
---|
188 | #endif |
---|