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 10858 2010-06-25 20:36:10Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> /* free() */ |
---|
15 | #include <string.h> |
---|
16 | |
---|
17 | #ifdef WIN32 |
---|
18 | #include <w32api.h> |
---|
19 | #define WINVER WindowsXP |
---|
20 | #include <windows.h> |
---|
21 | #define PROT_READ PAGE_READONLY |
---|
22 | #define MAP_PRIVATE FILE_MAP_COPY |
---|
23 | #endif |
---|
24 | |
---|
25 | #ifndef WIN32 |
---|
26 | #include <sys/mman.h> |
---|
27 | #endif |
---|
28 | #include <sys/types.h> |
---|
29 | #include <sys/stat.h> |
---|
30 | #include <fcntl.h> |
---|
31 | #include <unistd.h> |
---|
32 | #include <assert.h> |
---|
33 | |
---|
34 | #include "transmission.h" |
---|
35 | #include "platform.h" |
---|
36 | #include "blocklist.h" |
---|
37 | #include "net.h" |
---|
38 | #include "utils.h" |
---|
39 | |
---|
40 | #ifndef O_BINARY |
---|
41 | #define O_BINARY 0 |
---|
42 | #endif |
---|
43 | |
---|
44 | |
---|
45 | /*** |
---|
46 | **** PRIVATE |
---|
47 | ***/ |
---|
48 | |
---|
49 | struct tr_ip_range |
---|
50 | { |
---|
51 | uint32_t begin; |
---|
52 | uint32_t end; |
---|
53 | }; |
---|
54 | |
---|
55 | struct tr_blocklist |
---|
56 | { |
---|
57 | tr_bool isEnabled; |
---|
58 | int fd; |
---|
59 | size_t ruleCount; |
---|
60 | size_t byteCount; |
---|
61 | char * filename; |
---|
62 | struct tr_ip_range * rules; |
---|
63 | }; |
---|
64 | |
---|
65 | static void |
---|
66 | blocklistClose( tr_blocklist * b ) |
---|
67 | { |
---|
68 | if( b->rules ) |
---|
69 | { |
---|
70 | munmap( b->rules, b->byteCount ); |
---|
71 | close( b->fd ); |
---|
72 | b->rules = NULL; |
---|
73 | b->ruleCount = 0; |
---|
74 | b->byteCount = 0; |
---|
75 | b->fd = -1; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | static void |
---|
80 | blocklistLoad( tr_blocklist * b ) |
---|
81 | { |
---|
82 | int fd; |
---|
83 | struct stat st; |
---|
84 | const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); |
---|
85 | |
---|
86 | blocklistClose( b ); |
---|
87 | |
---|
88 | if( stat( b->filename, &st ) == -1 ) |
---|
89 | return; |
---|
90 | |
---|
91 | fd = open( b->filename, O_RDONLY | O_BINARY ); |
---|
92 | if( fd == -1 ) |
---|
93 | { |
---|
94 | tr_err( err_fmt, b->filename, tr_strerror( errno ) ); |
---|
95 | return; |
---|
96 | } |
---|
97 | |
---|
98 | b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 ); |
---|
99 | if( !b->rules ) |
---|
100 | { |
---|
101 | tr_err( err_fmt, b->filename, tr_strerror( errno ) ); |
---|
102 | close( fd ); |
---|
103 | return; |
---|
104 | } |
---|
105 | |
---|
106 | b->byteCount = st.st_size; |
---|
107 | b->ruleCount = st.st_size / sizeof( struct tr_ip_range ); |
---|
108 | b->fd = fd; |
---|
109 | |
---|
110 | { |
---|
111 | char * base = tr_basename( b->filename ); |
---|
112 | tr_inf( _( "Blocklist \"%s\" contains %'zu entries" ), base, b->ruleCount ); |
---|
113 | tr_free( base ); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | static void |
---|
118 | blocklistEnsureLoaded( tr_blocklist * b ) |
---|
119 | { |
---|
120 | if( !b->rules ) |
---|
121 | blocklistLoad( b ); |
---|
122 | } |
---|
123 | |
---|
124 | static int |
---|
125 | compareAddressToRange( const void * va, |
---|
126 | const void * vb ) |
---|
127 | { |
---|
128 | const uint32_t * a = va; |
---|
129 | const struct tr_ip_range * b = vb; |
---|
130 | |
---|
131 | if( *a < b->begin ) return -1; |
---|
132 | if( *a > b->end ) return 1; |
---|
133 | return 0; |
---|
134 | } |
---|
135 | |
---|
136 | static void |
---|
137 | blocklistDelete( tr_blocklist * b ) |
---|
138 | { |
---|
139 | blocklistClose( b ); |
---|
140 | unlink( b->filename ); |
---|
141 | } |
---|
142 | |
---|
143 | /*** |
---|
144 | **** PACKAGE-VISIBLE |
---|
145 | ***/ |
---|
146 | |
---|
147 | tr_blocklist * |
---|
148 | _tr_blocklistNew( const char * filename, |
---|
149 | int isEnabled ) |
---|
150 | { |
---|
151 | tr_blocklist * b; |
---|
152 | |
---|
153 | b = tr_new0( tr_blocklist, 1 ); |
---|
154 | b->fd = -1; |
---|
155 | b->filename = tr_strdup( filename ); |
---|
156 | b->isEnabled = isEnabled; |
---|
157 | |
---|
158 | return b; |
---|
159 | } |
---|
160 | |
---|
161 | const char* |
---|
162 | _tr_blocklistGetFilename( const tr_blocklist * b ) |
---|
163 | { |
---|
164 | return b->filename; |
---|
165 | } |
---|
166 | |
---|
167 | void |
---|
168 | _tr_blocklistFree( tr_blocklist * b ) |
---|
169 | { |
---|
170 | blocklistClose( b ); |
---|
171 | tr_free( b->filename ); |
---|
172 | tr_free( b ); |
---|
173 | } |
---|
174 | |
---|
175 | int |
---|
176 | _tr_blocklistExists( const tr_blocklist * b ) |
---|
177 | { |
---|
178 | struct stat st; |
---|
179 | |
---|
180 | return !stat( b->filename, &st ); |
---|
181 | } |
---|
182 | |
---|
183 | int |
---|
184 | _tr_blocklistGetRuleCount( const tr_blocklist * b ) |
---|
185 | { |
---|
186 | blocklistEnsureLoaded( (tr_blocklist*)b ); |
---|
187 | |
---|
188 | return b->ruleCount; |
---|
189 | } |
---|
190 | |
---|
191 | int |
---|
192 | _tr_blocklistIsEnabled( tr_blocklist * b ) |
---|
193 | { |
---|
194 | return b->isEnabled; |
---|
195 | } |
---|
196 | |
---|
197 | void |
---|
198 | _tr_blocklistSetEnabled( tr_blocklist * b, |
---|
199 | int isEnabled ) |
---|
200 | { |
---|
201 | b->isEnabled = isEnabled ? 1 : 0; |
---|
202 | } |
---|
203 | |
---|
204 | int |
---|
205 | _tr_blocklistHasAddress( tr_blocklist * b, |
---|
206 | const tr_address * addr ) |
---|
207 | { |
---|
208 | uint32_t needle; |
---|
209 | const struct tr_ip_range * range; |
---|
210 | |
---|
211 | assert( tr_isAddress( addr ) ); |
---|
212 | |
---|
213 | if( !b->isEnabled || addr->type == TR_AF_INET6 ) |
---|
214 | return 0; |
---|
215 | |
---|
216 | blocklistEnsureLoaded( b ); |
---|
217 | |
---|
218 | if( !b->rules || !b->ruleCount ) |
---|
219 | return 0; |
---|
220 | |
---|
221 | needle = ntohl( addr->addr.addr4.s_addr ); |
---|
222 | |
---|
223 | range = bsearch( &needle, |
---|
224 | b->rules, |
---|
225 | b->ruleCount, |
---|
226 | sizeof( struct tr_ip_range ), |
---|
227 | compareAddressToRange ); |
---|
228 | |
---|
229 | return range != NULL; |
---|
230 | } |
---|
231 | |
---|
232 | /* |
---|
233 | * level1 format: "comment:x.x.x.x-y.y.y.y" |
---|
234 | */ |
---|
235 | static tr_bool |
---|
236 | parseLine1( const char * line, struct tr_ip_range * range ) |
---|
237 | { |
---|
238 | char * walk; |
---|
239 | int b[4]; |
---|
240 | int e[4]; |
---|
241 | char str[64]; |
---|
242 | tr_address addr; |
---|
243 | |
---|
244 | walk = strrchr( line, ':' ); |
---|
245 | if( !walk ) |
---|
246 | return FALSE; |
---|
247 | ++walk; /* walk past the colon */ |
---|
248 | |
---|
249 | if( sscanf( walk, "%d.%d.%d.%d-%d.%d.%d.%d", |
---|
250 | &b[0], &b[1], &b[2], &b[3], |
---|
251 | &e[0], &e[1], &e[2], &e[3] ) != 8 ) |
---|
252 | return FALSE; |
---|
253 | |
---|
254 | tr_snprintf( str, sizeof( str ), "%d.%d.%d.%d", b[0], b[1], b[2], b[3] ); |
---|
255 | if( tr_pton( str, &addr ) == NULL ) |
---|
256 | return FALSE; |
---|
257 | range->begin = ntohl( addr.addr.addr4.s_addr ); |
---|
258 | |
---|
259 | tr_snprintf( str, sizeof( str ), "%d.%d.%d.%d", e[0], e[1], e[2], e[3] ); |
---|
260 | if( tr_pton( str, &addr ) == NULL ) |
---|
261 | return FALSE; |
---|
262 | range->end = ntohl( addr.addr.addr4.s_addr ); |
---|
263 | |
---|
264 | return TRUE; |
---|
265 | } |
---|
266 | |
---|
267 | /* |
---|
268 | * "000.000.000.000 - 000.255.255.255 , 000 , invalid ip" |
---|
269 | */ |
---|
270 | static tr_bool |
---|
271 | parseLine2( const char * line, struct tr_ip_range * range ) |
---|
272 | { |
---|
273 | int unk; |
---|
274 | int a[4]; |
---|
275 | int b[4]; |
---|
276 | char str[32]; |
---|
277 | tr_address addr; |
---|
278 | |
---|
279 | if( sscanf( line, "%3d.%3d.%3d.%3d - %3d.%3d.%3d.%3d , %3d , ", |
---|
280 | &a[0], &a[1], &a[2], &a[3], |
---|
281 | &b[0], &b[1], &b[2], &b[3], |
---|
282 | &unk ) != 9 ) |
---|
283 | return FALSE; |
---|
284 | |
---|
285 | tr_snprintf( str, sizeof(str), "%d.%d.%d.%d", a[0], a[1], a[2], a[3] ); |
---|
286 | if( tr_pton( str, &addr ) == NULL ) |
---|
287 | return FALSE; |
---|
288 | range->begin = ntohl( addr.addr.addr4.s_addr ); |
---|
289 | |
---|
290 | tr_snprintf( str, sizeof(str), "%d.%d.%d.%d", b[0], b[1], b[2], b[3] ); |
---|
291 | if( tr_pton( str, &addr ) == NULL ) |
---|
292 | return FALSE; |
---|
293 | range->end = ntohl( addr.addr.addr4.s_addr ); |
---|
294 | |
---|
295 | return TRUE; |
---|
296 | } |
---|
297 | |
---|
298 | static int |
---|
299 | parseLine( const char * line, struct tr_ip_range * range ) |
---|
300 | { |
---|
301 | return parseLine1( line, range ) |
---|
302 | || parseLine2( line, range ); |
---|
303 | } |
---|
304 | |
---|
305 | int |
---|
306 | _tr_blocklistSetContent( tr_blocklist * b, |
---|
307 | const char * filename ) |
---|
308 | { |
---|
309 | FILE * in; |
---|
310 | FILE * out; |
---|
311 | int inCount = 0; |
---|
312 | int outCount = 0; |
---|
313 | char line[2048]; |
---|
314 | const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); |
---|
315 | |
---|
316 | if( !filename ) |
---|
317 | { |
---|
318 | blocklistDelete( b ); |
---|
319 | return 0; |
---|
320 | } |
---|
321 | |
---|
322 | in = fopen( filename, "rb" ); |
---|
323 | if( !in ) |
---|
324 | { |
---|
325 | tr_err( err_fmt, filename, tr_strerror( errno ) ); |
---|
326 | return 0; |
---|
327 | } |
---|
328 | |
---|
329 | blocklistClose( b ); |
---|
330 | |
---|
331 | out = fopen( b->filename, "wb+" ); |
---|
332 | if( !out ) |
---|
333 | { |
---|
334 | tr_err( err_fmt, b->filename, tr_strerror( errno ) ); |
---|
335 | fclose( in ); |
---|
336 | return 0; |
---|
337 | } |
---|
338 | |
---|
339 | while( fgets( line, sizeof( line ), in ) != NULL ) |
---|
340 | { |
---|
341 | char * walk; |
---|
342 | struct tr_ip_range range; |
---|
343 | |
---|
344 | ++inCount; |
---|
345 | |
---|
346 | /* zap the linefeed */ |
---|
347 | if(( walk = strchr( line, '\r' ))) *walk = '\0'; |
---|
348 | if(( walk = strchr( line, '\n' ))) *walk = '\0'; |
---|
349 | |
---|
350 | if( !parseLine( line, &range ) ) |
---|
351 | { |
---|
352 | /* don't try to display the actual lines - it causes issues */ |
---|
353 | tr_err( _( "blocklist skipped invalid address at line %d" ), inCount ); |
---|
354 | continue; |
---|
355 | } |
---|
356 | |
---|
357 | if( fwrite( &range, sizeof( struct tr_ip_range ), 1, out ) != 1 ) |
---|
358 | { |
---|
359 | tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), b->filename, |
---|
360 | tr_strerror( errno ) ); |
---|
361 | break; |
---|
362 | } |
---|
363 | |
---|
364 | ++outCount; |
---|
365 | } |
---|
366 | |
---|
367 | { |
---|
368 | char * base = tr_basename( b->filename ); |
---|
369 | tr_inf( _( "Blocklist \"%s\" updated with %'d entries" ), base, outCount ); |
---|
370 | tr_free( base ); |
---|
371 | } |
---|
372 | |
---|
373 | fclose( out ); |
---|
374 | fclose( in ); |
---|
375 | |
---|
376 | blocklistLoad( b ); |
---|
377 | |
---|
378 | return outCount; |
---|
379 | } |
---|
380 | |
---|