redox 0.0.3

A library implementing the bittorrent protocol and a few key extensions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Read a Torrent from a file or some bytes.

use std::fs::{self};
use std::io::{Read};
use std::path::{Path};

use bencode::{Bencode, DecodeBencode};
use error::{TorrentError, TorrentResult, TorrentErrorKind};
use info_hash::{InfoHash};
use torrent::{TorrentView, ContactType, PieceInfo, Files, Nodes,
              FileInfo, File, FilePath};
use torrent::metainfo::{self};
use util::{Dictionary, Iter};

/// Specialized contact for values which may involve owned storage.
///
/// See torrent::ContactType<'a> for client facing interface.
#[derive(Debug, Eq, PartialEq, Clone)]
enum ContactTypeImpl {
    Tracker(String),
    Trackerless(Vec<(String, u16)>),
    Either(String, Vec<(String, u16)>),
    None
}

/// Torrent parser that parses all values during creation.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Metainfo {
    contact:       ContactTypeImpl,
    comment:       Option<String>,
    created_by:    Option<String>,
    creation_date: Option<i64>,
    info:          PieceInfoImpl,
    file:          FileInfoImpl,
    info_hash:     InfoHash
}

impl Metainfo {
    /// Create a new Metainfo object from the given bytes.
    pub fn new<B>(bytes: B) -> TorrentResult<Metainfo> 
        where B: AsRef<[u8]> {
        let mut bencode = try!(Bencode::decode(&bytes));
    
        // Should Be Calculated Before Anything Gets Moved Out
        let info_hash = try!(metainfo::generate_info_hash(&bencode));
    
        let info = try!(PieceInfoImpl::new(&mut bencode));
        let file = try!(FileInfoImpl::new(&mut bencode));
        
        let announce = move_announce(&mut bencode);
        let nodes = move_nodes(&mut bencode);
        let contact = match (announce, nodes) {
            (Ok(a), Ok(n))   => ContactTypeImpl::Either(a, n),
            (Ok(a), Err(_))  => ContactTypeImpl::Tracker(a),
            (Err(_), Ok(n))  => ContactTypeImpl::Trackerless(n),
            (Err(_), Err(_)) => ContactTypeImpl::None
        };
        
        let comment = try!(move_comment(&mut bencode));
        let created_by = try!(move_created_by(&mut bencode));
        let creation_date = try!(move_creation_date(&mut bencode));
        
        Ok(Metainfo{
            contact: contact,
            comment: comment,
            created_by: created_by,
            creation_date: creation_date,
            info: info,
            file: file,
            info_hash: info_hash
        })
    }
    
    /// Create a new Metainfo object from the file located at path.
    pub fn from_file<P>(path: P) -> TorrentResult<Metainfo>
        where P: AsRef<Path> {
        let mut torrent_file = try!(fs::File::open(path));
        let mut torrent_bytes = Vec::new();
        
        try!(torrent_file.read_to_end(&mut torrent_bytes));
        
        Metainfo::new(&torrent_bytes[..])
    }
}

impl TorrentView for Metainfo {
    fn contact<'a>(&'a self) -> ContactType<'a> {
        match self.contact {
            ContactTypeImpl::Tracker(ref tracker) => ContactType::Tracker(tracker),
            ContactTypeImpl::Trackerless(ref nodes) => {
                let iter = Box::new(nodes.iter().map(|&(ref host, port)| {
                    (&host[..], port)
                })) as Box<Iterator<Item=(&'a str, u16)> + 'a>;
                
                ContactType::Trackerless(Nodes{ iter: iter })
            },
            ContactTypeImpl::Either(ref tracker, ref nodes) => {
                let iter = Box::new(nodes.iter().map(|&(ref host, port)| {
                    (&host[..], port)
                })) as Box<Iterator<Item=(&'a str, u16)> + 'a>;
                
                ContactType::Either(tracker, Nodes{ iter: iter })
            },
            ContactTypeImpl::None => ContactType::None
        }
    }
    
    fn comment(&self) -> Option<&str> {
        self.comment.as_ref().map(|n| &n[..])
    }
    
    fn created_by(&self) -> Option<&str> {
        self.created_by.as_ref().map(|n| &n[..])
    }
    
    fn creation_date(&self) -> Option<i64> {
        self.creation_date
    }
    
    fn piece_info<'a>(&'a self) -> PieceInfo<'a> {
        PieceInfo::new(&self.info.pieces[..], self.info.length)
    }
    
    fn file_info<'a>(&'a self) -> FileInfo<'a> {
        self.file.file_info()
    }
    
    fn info_hash(&self) -> InfoHash {
        self.info_hash
    }
}

/// The info dictionary of the current torrent.
#[derive(Debug, Eq, PartialEq, Clone)]
struct PieceInfoImpl {
    pieces: Vec<u8>,
    length: i64
}

impl PieceInfoImpl {
    fn new(root: &mut Bencode) -> TorrentResult<PieceInfoImpl> {
        let length = try!(move_piece_length(root));
        let pieces = try!(move_pieces(root));
        
        Ok(PieceInfoImpl{ pieces: pieces, length: length })
    }
}

/// Type of file which signifies how different fields are interpreted.
/// 
/// We have to make the contents reside inside a tuple because we need to pass
/// a of all the contents to an iterator that maps the contents to the target
/// struct for the client to use.
#[derive(Debug, Eq, PartialEq, Clone)]
enum FileType {
    /// (FileName, Length, Checksum)
    Single((String, i64, Option<Vec<u8>>)),
    /// (BaseDirectory, Files<(Length, Checksum, Path)>)
    Multiple((String, Vec<(i64, Option<Vec<u8>>, Vec<String>)>))
}

#[derive(Debug, Eq, PartialEq, Clone)]
struct FileInfoImpl {
    file_type: FileType
}

impl FileInfoImpl {
    fn new(root: &mut Bencode) -> TorrentResult<FileInfoImpl> {
        Ok(FileInfoImpl{ file_type: try!(move_files(root)) })
    }
    
    fn file_info<'a>(&'a self) -> FileInfo<'a> {
        match self.file_type {
            FileType::Single(ref items) => {
                let file_iter = Box::new(Iter::new(items).map(|&(ref name, len, ref check)| {
                    let path_iter = Box::new(Iter::new(&name[..])) as Box<Iterator<Item=&str>>;
                    
                    File{
                        path_iter: FilePath{ iter: path_iter },
                        length: len,
                        checksum: check.as_ref().map(|n| &n[..])
                    }
                })) as Box<Iterator<Item=File<'a>>>;
                
                FileInfo{
                    directory: None,
                    file_iter: Files{ iter: file_iter }
                }
            },
            FileType::Multiple((ref name, ref items)) => {
                let file_iter = Box::new(items.iter().map(|&(len, ref check, ref path)| {
                    let path_iter = Box::new(path.iter().map(|path| &path[..])) as Box<Iterator<Item=&str>>;
                    
                    File{
                        path_iter: FilePath{ iter: path_iter },
                        length: len,
                        checksum: check.as_ref().map(|n| &n[..])
                    }
                })) as Box<Iterator<Item=File<'a>>>;
                
                FileInfo{
                    directory: Some(name),
                    file_iter: Files{ iter: file_iter }
                }
            }
        }
    }
}

//----------------------------------------------------------------------------//

/// Match a Bencode object to move the inner object.
///
/// Returns a WrongType error if it cannot convert to the given type.
macro_rules! move_ben {
    ($ben:ident, $key:expr, $var:path) => (
        match $ben {
            $var(n) => n,
            _ => return Err(TorrentError::new(TorrentErrorKind::WrongType, $key))
        }
    )
}

/// Match a Bencode object to get a mutable reference to the inner object.
///
/// Returns a WrongType error if it cannot convert to the given type.
macro_rules! ref_mut_ben {
    ($ben:ident, $key:expr, $var:path) => (
        match *$ben {
            $var(ref mut n) => n,
            _ => return Err(TorrentError::new(TorrentErrorKind::WrongType, $key))
        }
    )
}

/// Move a Bencode object out of a Dictionary object.
///
/// Returns a MissingKey error if the value is not in the dictionary or a WrongType
/// error if the value cannot be converted object.
macro_rules! remove_dict {
    ($dict:ident, $key:expr, $var:path) => (
        match $dict.remove($key) {
            Some(n) => match n {
                $var(n) => n,
                _ => return Err(TorrentError::new(TorrentErrorKind::WrongType, $key))
            },
            None => return Err(TorrentError::new(TorrentErrorKind::MissingKey, $key))
        }
    );
}

/// Optionally move a Bencode object out of a Dictionary object.
///
/// Returns a WrongType error if the value cannot be converted object.
macro_rules! remove_dict_opt {
    ($dict:ident, $key:expr, $var:path) => (
        match $dict.remove($key) {
            Some(n) => match n {
                $var(n) => Some(n),
                _ => return Err(TorrentError::new(TorrentErrorKind::WrongType, $key))
            },
            None => None
        }
    );
}

/// Get a mutable reference to a Bencode object from a Dictionary object.
///
/// Returns a MissingKey error if the value is not in the dictionary or a WrongType
/// error if the value cannot be converted object.
macro_rules! ref_mut_dict {
    ($dict:ident, $key:expr, $var:path) => (
        match $dict.lookup_mut($key) {
            Some(n) => match *n {
                $var(ref mut n) => n,
                _ => return Err(TorrentError::new(TorrentErrorKind::WrongType, $key))
            },
            None => return Err(TorrentError::new(TorrentErrorKind::MissingKey, $key))
        }
    );
}

//----------------------------------------------------------------------------//

pub fn slice_root_dict<'a, 'b>(root: &'b mut Bencode<'a>) -> TorrentResult<&'b mut Dictionary<'a, Bencode<'a>>> {
    Ok(ref_mut_ben!(root, metainfo::ROOT_IDENT, Bencode::Dict))
}

pub fn slice_info_dict<'a, 'b>(root: &'b mut Bencode<'a>) -> TorrentResult<&'b mut Dictionary<'a, Bencode<'a>>> {
    let root_dict = try!(slice_root_dict(root));
    
    Ok(ref_mut_dict!(root_dict, metainfo::INFO_KEY, Bencode::Dict))
}

pub fn bytes_into_string(bytes: Vec<u8>, key: &'static str) -> TorrentResult<String> {
    String::from_utf8(bytes).map_err(|_|
        TorrentError::with_detail(TorrentErrorKind::WrongType, key, "Bytes Were Not Valid UTF-8")

    )
}

//----------------------------------------------------------------------------//

fn move_announce(root: &mut Bencode) -> TorrentResult<String> {
    let root_dict = try!(slice_root_dict(root));
    let bytes = remove_dict!(root_dict, metainfo::ANNOUNCE_KEY, Bencode::Bytes);
    
    bytes_into_string(bytes.to_vec(), metainfo::ANNOUNCE_KEY)
}

fn move_nodes(root: &mut Bencode) -> TorrentResult<Vec<(String, u16)>> {
    let root_dict = try!(slice_root_dict(root));
    let nodes = remove_dict!(root_dict, metainfo::NODES_KEY, Bencode::List);
    
    let mut nodes_list = Vec::with_capacity(nodes.len());
    for node in nodes {
        let mut node_tuple = move_ben!(node, metainfo::NODES_KEY, Bencode::List);
        
        if node_tuple.len() != metainfo::NODE_LEN {
            return Err(TorrentError::with_detail(TorrentErrorKind::WrongType,
                metainfo::NODES_KEY, "Node Tuple Wrong Size"))
        }
        
        let port_ben = node_tuple.pop().unwrap();
        let host_ben = node_tuple.pop().unwrap();
        
        let port = move_ben!(port_ben, metainfo::NODES_KEY, Bencode::Int) as u16;
        let host = try!(bytes_into_string(
            move_ben!(host_ben, metainfo::NODES_KEY, Bencode::Bytes).to_vec(),
            metainfo::NODES_KEY
        ));
        
        nodes_list.push((host, port));
    }
    
    Ok(nodes_list)
}

fn move_comment(root: &mut Bencode) -> TorrentResult<Option<String>> {
    let root_dict = try!(slice_root_dict(root));
    
    match remove_dict_opt!(root_dict, metainfo::COMMENT_KEY, Bencode::Bytes) {
        Some(n) => bytes_into_string(n.to_vec(), metainfo::COMMENT_KEY).map(Some),
        None    => Ok(None)
    }
}

fn move_created_by(root: &mut Bencode) -> TorrentResult<Option<String>> {
    let root_dict = try!(slice_root_dict(root));
    
    match remove_dict_opt!(root_dict, metainfo::CREATED_BY_KEY, Bencode::Bytes) {
        Some(n) => bytes_into_string(n.to_vec(), metainfo::CREATED_BY_KEY).map(Some),
        None    => Ok(None)
    }
}

fn move_creation_date(root: &mut Bencode) -> TorrentResult<Option<i64>> {
    let root_dict = try!(slice_root_dict(root));
    
    Ok(remove_dict_opt!(root_dict, metainfo::CREATION_DATE_KEY, Bencode::Int))
}

fn move_piece_length(root: &mut Bencode) -> TorrentResult<i64> {
    let info_dict = try!(slice_info_dict(root));
    
    Ok(remove_dict!(info_dict, metainfo::PIECE_LENGTH_KEY, Bencode::Int))
}

fn move_pieces(root: &mut Bencode) -> TorrentResult<Vec<u8>> {
    let info_dict = try!(slice_info_dict(root));
    
    Ok(remove_dict!(info_dict, metainfo::PIECES_KEY, Bencode::Bytes).to_vec())
}

/// 
///
/// Returns an error if the checksum is present but of the wrong length.
fn move_checksum<'a>(dict: &mut Dictionary<'a, Bencode<'a>>) -> TorrentResult<Option<Vec<u8>>> {
    let checksum = remove_dict_opt!(dict, metainfo::MD5SUM_KEY, Bencode::Bytes);
    
    if checksum.is_some() && checksum.as_ref().unwrap().len() != metainfo::MD5SUM_LEN {
        Err(TorrentError::with_detail(TorrentErrorKind::WrongType, metainfo::MD5SUM_KEY,
            "Checksum Is The Wrong Length"))
    } else {
        Ok(checksum.map(|n| n.to_vec()))
    }
}

fn move_name(root: &mut Bencode) -> TorrentResult<String> {
    let info_dict = try!(slice_info_dict(root));
    let name_bytes = remove_dict!(info_dict, metainfo::NAME_KEY, Bencode::Bytes);
    
    bytes_into_string(name_bytes.to_vec(), metainfo::NAME_KEY)
}

/// 
///
/// Works for both single and multi file torrents where the name value will always
/// be the first entry in the paths list that is returned.
fn move_files(root: &mut Bencode) -> TorrentResult<FileType> {
    let name = try!(move_name(root));
    let info_dict = try!(slice_info_dict(root));
    
    // Length Key Only Exists Within The Info Dictionary In Single File Torrents
    if info_dict.lookup(metainfo::LENGTH_KEY).is_some() {
        let length = remove_dict!(info_dict, metainfo::LENGTH_KEY, Bencode::Int);
        let checksum = try!(move_checksum(info_dict));
        
        Ok(FileType::Single((name, length, checksum)))
    } else {
        let files = remove_dict!(info_dict, metainfo::FILES_KEY, Bencode::List);
        let mut file_list = Vec::with_capacity(files.len());
        
        for mut file in files {
            file_list.push(try!(move_file(&mut file)));
        }
        
        Ok(FileType::Multiple((name, file_list)))
    }
}

/// Tries to pull out all file fields from the file BencodeView value.
fn move_file(file: &mut Bencode) -> TorrentResult<(i64, Option<Vec<u8>>, Vec<String>)> {
    let file = ref_mut_ben!(file, metainfo::FILES_KEY, Bencode::Dict);
    
    let length = remove_dict!(file, metainfo::LENGTH_KEY, Bencode::Int);
    let checksum = try!(move_checksum(file));
    
    let paths = remove_dict!(file, metainfo::PATH_KEY, Bencode::List);
    
    let mut path_list = Vec::with_capacity(paths.len());
    
    for path in paths {
        let path_entry_bytes = move_ben!(path, metainfo::PATH_KEY, Bencode::Bytes);
        
        path_list.push(try!(bytes_into_string(path_entry_bytes.to_vec(), metainfo::PATH_KEY)));
    }
    
    Ok((length, checksum, path_list))
}