1 # Written by Bram Cohen
2 # see LICENSE.txt for license information
5 from types import UnicodeType, StringType, LongType, IntType, ListType, DictType
8 #reg = compile(r'^[^/\\.~][^/\\]*$')
9 #reg = compile(r'^[^/\\]*$')
11 ints = (LongType, IntType)
14 if type(info) != DictType:
15 raise ValueError, 'bad metainfo - not a dictionary'
17 if info.has_key('pieces'):
18 pieces = info.get('pieces')
19 if type(pieces) != StringType or len(pieces) % 20 != 0:
20 raise ValueError, 'bad metainfo - bad pieces key'
21 elif info.has_key('root hash'):
23 root_hash = info.get('root hash')
24 if type(root_hash) != StringType or len(root_hash) != 20:
25 raise ValueError, 'bad metainfo - bad root hash key'
26 piecelength = info.get('piece length')
27 if type(piecelength) not in ints or piecelength <= 0:
28 raise ValueError, 'bad metainfo - illegal piece length'
29 name = info.get('name')
30 if StringType != type(name) != UnicodeType:
31 raise ValueError, 'bad metainfo - bad name'
32 #if not reg.match(name):
33 # raise ValueError, 'name %s disallowed for security reasons' % name
34 if info.has_key('files') == info.has_key('length'):
35 raise ValueError, 'single/multiple file mix'
36 if info.has_key('length'):
37 length = info.get('length')
38 if type(length) not in ints or length < 0:
39 raise ValueError, 'bad metainfo - bad length'
41 files = info.get('files')
42 if type(files) != ListType:
45 if type(f) != DictType:
46 raise ValueError, 'bad metainfo - bad file value'
47 length = f.get('length')
48 if type(length) not in ints or length < 0:
49 raise ValueError, 'bad metainfo - bad length'
51 if type(path) != ListType or path == []:
52 raise ValueError, 'bad metainfo - bad path'
54 if StringType != type(p) != UnicodeType:
55 raise ValueError, 'bad metainfo - bad path dir'
57 # raise ValueError, 'path %s disallowed for security reasons' % p
58 for i in xrange(len(files)):
60 if files[i]['path'] == files[j]['path']:
61 raise ValueError, 'bad metainfo - duplicate path'
63 def check_message(message):
64 if type(message) != DictType:
66 check_info(message.get('info'))
67 if StringType != type(message.get('announce')) != UnicodeType:
70 def check_peers(message):
71 if type(message) != DictType:
73 if message.has_key('failure reason'):
74 if type(message['failure reason']) != StringType:
77 peers = message.get('peers')
79 if type(peers) == ListType:
81 if type(p) != DictType:
83 if type(p.get('ip')) != StringType:
86 if type(port) not in ints or p <= 0:
88 if p.has_key('peer id'):
90 if type(id) != StringType or len(id) != 20:
92 elif type(peers) != StringType or len(peers) % 6 != 0:
95 # IPv6 Tracker extension. http://www.bittorrent.org/beps/bep_0007.html
96 peers6 = message.get('peers6')
97 if peers6 is not None:
98 if type(peers6) == ListType:
100 if type(p) != DictType:
102 if type(p.get('ip')) != StringType:
105 if type(port) not in ints or p <= 0:
107 if p.has_key('peer id'):
109 if type(id) != StringType or len(id) != 20:
111 elif type(peers6) != StringType or len(peers6) % 18 != 0:
114 interval = message.get('interval', 1)
115 if type(interval) not in ints or interval <= 0:
117 minint = message.get('min interval', 1)
118 if type(minint) not in ints or minint <= 0:
120 if type(message.get('tracker id', '')) != StringType:
122 npeers = message.get('num peers', 0)
123 if type(npeers) not in ints or npeers < 0:
125 dpeers = message.get('done peers', 0)
126 if type(dpeers) not in ints or dpeers < 0:
128 last = message.get('last', 0)
129 if type(last) not in ints or last < 0: