1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 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: blocklist.c 9868 2010-01-04 21:00:47Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> /* free */ |
---|
15 | #include <string.h> |
---|
16 | |
---|
17 | #ifdef WIN32 |
---|
18 | #include <windows.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #ifndef WIN32 |
---|
22 | #include <sys/mman.h> |
---|
23 | #endif |
---|
24 | #include <sys/types.h> |
---|
25 | #include <sys/stat.h> |
---|
26 | #include <fcntl.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include <assert.h> |
---|
29 | |
---|
30 | #include "ggets.h" |
---|
31 | |
---|
32 | #include "transmission.h" |
---|
33 | #include "platform.h" |
---|
34 | #include "blocklist.h" |
---|
35 | #include "net.h" |
---|
36 | #include "utils.h" |
---|
37 | |
---|
38 | |
---|
39 | /*** |
---|
40 | **** PRIVATE |
---|
41 | ***/ |
---|
42 | |
---|
43 | struct tr_ip_range |
---|
44 | { |
---|
45 | uint32_t begin; |
---|
46 | uint32_t end; |
---|
47 | }; |
---|
48 | |
---|
49 | struct tr_blocklist |
---|
50 | { |
---|
51 | tr_bool isEnabled; |
---|
52 | int fd; |
---|
53 | size_t ruleCount; |
---|
54 | size_t byteCount; |
---|
55 | char * filename; |
---|
56 | struct tr_ip_range * rules; |
---|
57 | }; |
---|
58 | |
---|
59 | static void |
---|
60 | blocklistClose( tr_blocklist * b ) |
---|
61 | { |
---|
62 | if( b->rules ) |
---|
63 | { |
---|
64 | munmap( b->rules, b->byteCount ); |
---|
65 | close( b->fd ); |
---|
66 | b->rules = NULL; |
---|
67 | b->ruleCount = 0; |
---|
68 | b->byteCount = 0; |
---|
69 | b->fd = -1; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | static void |
---|
74 | blocklistLoad( tr_blocklist * b ) |
---|
75 | { |
---|
76 | int fd; |
---|
77 | struct stat st; |
---|
78 | const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); |
---|
79 | |
---|
80 | blocklistClose( b ); |
---|
81 | |
---|
82 | if( stat( b->filename, &st ) == -1 ) |
---|
83 | return; |
---|
84 | |
---|
85 | fd = open( b->filename, O_RDONLY ); |
---|
86 | if( fd == -1 ) |
---|
87 | { |
---|
88 | tr_err( err_fmt, b->filename, tr_strerror( errno ) ); |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | #ifndef WIN32 |
---|
93 | b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 ); |
---|
94 | #else |
---|
95 | b->rules = mmap( NULL, st.st_size, 0, 0, fd, 0 ); |
---|
96 | #endif |
---|
97 | if( !b->rules ) |
---|
98 | { |
---|
99 | tr_err( err_fmt, b->filename, tr_strerror( errno ) ); |
---|
100 | close( fd ); |
---|
101 | return; |
---|
102 | } |
---|
103 | |
---|
104 | b->byteCount = st.st_size; |
---|
105 | b->ruleCount = st.st_size / sizeof( struct tr_ip_range ); |
---|
106 | b->fd = fd; |
---|
107 | |
---|
108 | { |
---|
109 | char * base = tr_basename( b->filename ); |
---|
110 | tr_inf( _( "Blocklist \"%s\" contains %'zu entries" ), base, b->ruleCount ); |
---|
111 | tr_free( base ); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | blocklistEnsureLoaded( tr_blocklist * b ) |
---|
117 | { |
---|
118 | if( !b->rules ) |
---|
119 | blocklistLoad( b ); |
---|
120 | } |
---|
121 | |
---|
122 | static int |
---|
123 | compareAddressToRange( const void * va, |
---|
124 | const void * vb ) |
---|
125 | { |
---|
126 | const uint32_t * a = va; |
---|
127 | const struct tr_ip_range * b = vb; |
---|
128 | |
---|
129 | if( *a < b->begin ) return -1; |
---|
130 | if( *a > b->end ) return 1; |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | static void |
---|
135 | blocklistDelete( tr_blocklist * b ) |
---|
136 | { |
---|
137 | blocklistClose( b ); |
---|
138 | unlink( b->filename ); |
---|
139 | } |
---|
140 | |
---|
141 | /*** |
---|
142 | **** PACKAGE-VISIBLE |
---|
143 | ***/ |
---|
144 | |
---|
145 | tr_blocklist * |
---|
146 | _tr_blocklistNew( const char * filename, |
---|
147 | int isEnabled ) |
---|
148 | { |
---|
149 | tr_blocklist * b; |
---|
150 | |
---|
151 | b = tr_new0( tr_blocklist, 1 ); |
---|
152 | b->fd = -1; |
---|
153 | b->filename = tr_strdup( filename ); |
---|
154 | b->isEnabled = isEnabled; |
---|
155 | |
---|
156 | return b; |
---|
157 | } |
---|
158 | |
---|
159 | const char* |
---|
160 | _tr_blocklistGetFilename( const tr_blocklist * b ) |
---|
161 | { |
---|
162 | return b->filename; |
---|
163 | } |
---|
164 | |
---|
165 | void |
---|
166 | _tr_blocklistFree( tr_blocklist * b ) |
---|
167 | { |
---|
168 | blocklistClose( b ); |
---|
169 | tr_free( b->filename ); |
---|
170 | tr_free( b ); |
---|
171 | } |
---|
172 | |
---|
173 | int |
---|
174 | _tr_blocklistExists( const tr_blocklist * b ) |
---|
175 | { |
---|
176 | struct stat st; |
---|
177 | |
---|
178 | return !stat( b->filename, &st ); |
---|
179 | } |
---|
180 | |
---|
181 | int |
---|
182 | _tr_blocklistGetRuleCount( const tr_blocklist * b ) |
---|
183 | { |
---|
184 | blocklistEnsureLoaded( (tr_blocklist*)b ); |
---|
185 | |
---|
186 | return b->ruleCount; |
---|
187 | } |
---|
188 | |
---|
189 | int |
---|
190 | _tr_blocklistIsEnabled( tr_blocklist * b ) |
---|
191 | { |
---|
192 | return b->isEnabled; |
---|
193 | } |
---|
194 | |
---|
195 | void |
---|
196 | _tr_blocklistSetEnabled( tr_blocklist * b, |
---|
197 | int isEnabled ) |
---|
198 | { |
---|
199 | b->isEnabled = isEnabled ? 1 : 0; |
---|
200 | } |
---|
201 | |
---|
202 | int |
---|
203 | _tr_blocklistHasAddress( tr_blocklist * b, |
---|
204 | const tr_address * addr ) |
---|
205 | { |
---|
206 | uint32_t needle; |
---|
207 | const struct tr_ip_range * range; |
---|
208 | |
---|
209 | assert( tr_isAddress( addr ) ); |
---|
210 | |
---|
211 | if( !b->isEnabled || addr->type == TR_AF_INET6 ) |
---|
212 | return 0; |
---|
213 | |
---|
214 | blocklistEnsureLoaded( b ); |
---|
215 | |
---|
216 | if( !b->rules || !b->ruleCount ) |
---|
217 | return 0; |
---|
218 | |
---|
219 | needle = ntohl( addr->addr.addr4.s_addr ); |
---|
220 | |
---|
221 | range = bsearch( &needle, |
---|
222 | b->rules, |
---|
223 | b->ruleCount, |
---|
224 | sizeof( struct tr_ip_range ), |
---|
225 | compareAddressToRange ); |
---|
226 | |
---|
227 | return range != NULL; |
---|
228 | } |
---|
229 | |
---|
230 | int |
---|
231 | _tr_blocklistSetContent( tr_blocklist * b, |
---|
232 | const char * filename ) |
---|
233 | { |
---|
234 | FILE * in; |
---|
235 | FILE * out; |
---|
236 | char * line; |
---|
237 | int lineCount = 0; |
---|
238 | const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); |
---|
239 | |
---|
240 | if( !filename ) |
---|
241 | { |
---|
242 | blocklistDelete( b ); |
---|
243 | return 0; |
---|
244 | } |
---|
245 | |
---|
246 | in = fopen( filename, "r" ); |
---|
247 | if( !in ) |
---|
248 | { |
---|
249 | tr_err( err_fmt, filename, tr_strerror( errno ) ); |
---|
250 | return 0; |
---|
251 | } |
---|
252 | |
---|
253 | blocklistClose( b ); |
---|
254 | |
---|
255 | out = fopen( b->filename, "wb+" ); |
---|
256 | if( !out ) |
---|
257 | { |
---|
258 | tr_err( err_fmt, b->filename, tr_strerror( errno ) ); |
---|
259 | fclose( in ); |
---|
260 | return 0; |
---|
261 | } |
---|
262 | |
---|
263 | while( !fggets( &line, in ) ) |
---|
264 | { |
---|
265 | char * rangeBegin; |
---|
266 | char * rangeEnd; |
---|
267 | char * crpos; |
---|
268 | tr_address addr; |
---|
269 | struct tr_ip_range range; |
---|
270 | |
---|
271 | rangeBegin = strrchr( line, ':' ); |
---|
272 | if( !rangeBegin ){ free( line ); continue; } |
---|
273 | ++rangeBegin; |
---|
274 | |
---|
275 | rangeEnd = strchr( rangeBegin, '-' ); |
---|
276 | if( !rangeEnd ){ free( line ); continue; } |
---|
277 | *rangeEnd++ = '\0'; |
---|
278 | if(( crpos = strchr( rangeEnd, '\r' ))) |
---|
279 | *crpos = '\0'; |
---|
280 | |
---|
281 | if( !tr_pton( rangeBegin, &addr ) ) |
---|
282 | tr_err( "blocklist skipped invalid address [%s]\n", rangeBegin ); |
---|
283 | range.begin = ntohl( addr.addr.addr4.s_addr ); |
---|
284 | |
---|
285 | if( !tr_pton( rangeEnd, &addr ) ) |
---|
286 | tr_err( "blocklist skipped invalid address [%s]\n", rangeEnd ); |
---|
287 | range.end = ntohl( addr.addr.addr4.s_addr ); |
---|
288 | |
---|
289 | free( line ); |
---|
290 | |
---|
291 | if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1 ) |
---|
292 | { |
---|
293 | tr_err( _( |
---|
294 | "Couldn't save file \"%1$s\": %2$s" ), b->filename, |
---|
295 | tr_strerror( errno ) ); |
---|
296 | break; |
---|
297 | } |
---|
298 | |
---|
299 | ++lineCount; |
---|
300 | } |
---|
301 | |
---|
302 | { |
---|
303 | char * base = tr_basename( b->filename ); |
---|
304 | tr_inf( _( "Blocklist \"%1$s\" updated with %2$'d entries" ), base, lineCount ); |
---|
305 | tr_free( base ); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | fclose( out ); |
---|
310 | fclose( in ); |
---|
311 | |
---|
312 | blocklistLoad( b ); |
---|
313 | |
---|
314 | return lineCount; |
---|
315 | } |
---|
316 | |
---|