1
# Written by Bram Cohen
2
# see LICENSE.txt for license information
4
from types import StringType, LongType, IntType, ListType, DictType
7
reg = compile(r'^[^/\\.~][^/\\]*$')
9
ints = (LongType, IntType)
12
if type(info) != DictType:
13
raise ValueError, 'bad metainfo - not a dictionary'
14
pieces = info.get('pieces')
15
if type(pieces) != StringType or len(pieces) % 20 != 0:
16
raise ValueError, 'bad metainfo - bad pieces key'
17
piecelength = info.get('piece length')
18
if type(piecelength) not in ints or piecelength <= 0:
19
raise ValueError, 'bad metainfo - illegal piece length'
20
name = info.get('name')
21
if type(name) != StringType:
22
raise ValueError, 'bad metainfo - bad name'
23
if not reg.match(name):
24
raise ValueError, 'name %s disallowed for security reasons' % name
25
if info.has_key('files') == info.has_key('length'):
26
raise ValueError, 'single/multiple file mix'
27
if info.has_key('length'):
28
length = info.get('length')
29
if type(length) not in ints or length < 0:
30
raise ValueError, 'bad metainfo - bad length'
32
files = info.get('files')
33
if type(files) != ListType:
36
if type(f) != DictType:
37
raise ValueError, 'bad metainfo - bad file value'
38
length = f.get('length')
39
if type(length) not in ints or length < 0:
40
raise ValueError, 'bad metainfo - bad length'
42
if type(path) != ListType or path == []:
43
raise ValueError, 'bad metainfo - bad path'
45
if type(p) != StringType:
46
raise ValueError, 'bad metainfo - bad path dir'
48
raise ValueError, 'path %s disallowed for security reasons' % p
49
for i in xrange(len(files)):
51
if files[i]['path'] == files[j]['path']:
52
raise ValueError, 'bad metainfo - duplicate path'
54
def check_message(message):
55
if type(message) != DictType:
57
check_info(message.get('info'))
58
if type(message.get('announce')) != StringType:
61
def check_peers(message):
62
if type(message) != DictType:
64
if message.has_key('failure reason'):
65
if type(message['failure reason']) != StringType:
68
peers = message.get('peers')
69
if type(peers) == ListType:
71
if type(p) != DictType:
73
if type(p.get('ip')) != StringType:
76
if type(port) not in ints or p <= 0:
78
if p.has_key('peer id'):
80
if type(id) != StringType or len(id) != 20:
82
elif type(peers) != StringType or len(peers) % 6 != 0:
84
interval = message.get('interval', 1)
85
if type(interval) not in ints or interval <= 0:
87
minint = message.get('min interval', 1)
88
if type(minint) not in ints or minint <= 0:
90
if type(message.get('tracker id', '')) != StringType:
92
npeers = message.get('num peers', 0)
93
if type(npeers) not in ints or npeers < 0:
95
dpeers = message.get('done peers', 0)
96
if type(dpeers) not in ints or dpeers < 0:
98
last = message.get('last', 0)
99
if type(last) not in ints or last < 0: