|
| 1 | +/* |
| 2 | + Copyright (C) 2013 Tox project All Rights Reserved. |
| 3 | + Copyright © 2018 Namsoo CHO <[email protected]> |
| 4 | +
|
| 5 | + This file is part of Tox. |
| 6 | +
|
| 7 | + Tox is libre software: you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation, either version 3 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +
|
| 12 | + Tox is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU General Public License |
| 18 | + along with Tox. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +*/ |
| 20 | + |
| 21 | + |
| 22 | +/*! |
| 23 | +Serialize or deserialize states of tox daemon. |
| 24 | +When toxcore starts, it deserializes states from serialized file. |
| 25 | +Toxcore daemon may serialize its states to file with some interval. |
| 26 | +*/ |
| 27 | + |
| 28 | +use std::io::{Error, ErrorKind}; |
| 29 | + |
| 30 | +use futures::{future, Stream, stream}; |
| 31 | + |
| 32 | +use toxcore::dht::server::*; |
| 33 | +use toxcore::dht::packed_node::*; |
| 34 | +use toxcore::dht::server::client::*; |
| 35 | +use toxcore::state_format::old::*; |
| 36 | +use toxcore::binary_io::*; |
| 37 | +use toxcore::io_tokio::*; |
| 38 | +use toxcore::dht::kbucket::*; |
| 39 | + |
| 40 | +/// serialize or deserialize states of DHT close lists |
| 41 | +#[derive(Clone, Debug)] |
| 42 | +pub struct DaemonState; |
| 43 | + |
| 44 | +// close list has DhtNode, but when we access it with iter(), DhtNode is reformed to PackedNode |
| 45 | +const DHT_STATE_BUFFER_SIZE: usize = |
| 46 | + // Bucket size |
| 47 | + ( |
| 48 | + // PackedNode size |
| 49 | + ( |
| 50 | + 32 + // PK size |
| 51 | + 19 // SocketAddr maximum size |
| 52 | + ) * BUCKET_DEFAULT_SIZE // num of DhtNodes per Bucket : 8 |
| 53 | + ) * KBUCKET_MAX_ENTRIES as usize; // 255 |
| 54 | + |
| 55 | +impl DaemonState { |
| 56 | + /// serialize DHT states, old means that the format of seriaization is old version |
| 57 | + pub fn serialize_old(server: &Server) -> Vec<u8> { |
| 58 | + let nodes = server.close_nodes.read().iter() // DhtNode is reformed to PackedNode through iter() |
| 59 | + .map(|node| node) |
| 60 | + .collect::<Vec<PackedNode>>(); |
| 61 | + |
| 62 | + let mut buf = [0u8; DHT_STATE_BUFFER_SIZE]; |
| 63 | + let (_, buf_len) = DhtState(nodes).to_bytes((&mut buf, 0)).expect("DhtState(nodes).to_bytes has failed"); |
| 64 | + |
| 65 | + buf[..buf_len].to_vec() |
| 66 | + } |
| 67 | + |
| 68 | + /// deserialize DHT close list and then re-setup close list, old means that the format of deserialization is old version |
| 69 | + pub fn deserialize_old(server: &Server, serialized_data: Vec<u8>) -> IoFuture<()> { |
| 70 | + let nodes = match DhtState::from_bytes(&serialized_data) { |
| 71 | + IResult::Done(_, DhtState(nodes)) => nodes, |
| 72 | + e => return Box::new( |
| 73 | + future::err( |
| 74 | + Error::new(ErrorKind::Other, format!("Can't deserialize DHT states from serialized bytes {:?}", e)) |
| 75 | + ) |
| 76 | + ), |
| 77 | + }; |
| 78 | + |
| 79 | + let mut ping_map = server.ping_map.write(); |
| 80 | + let nodes_sender = nodes.iter() |
| 81 | + .map(|node| { |
| 82 | + let client = ping_map.entry(node.pk).or_insert_with(PingData::new); |
| 83 | + |
| 84 | + server.send_nodes_req(*node, server.pk, client) |
| 85 | + }); |
| 86 | + |
| 87 | + let nodes_stream = stream::futures_unordered(nodes_sender).then(|_| Ok(())); |
| 88 | + Box::new(nodes_stream.for_each(|()| Ok(()))) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::*; |
| 95 | + |
| 96 | + use toxcore::crypto_core::*; |
| 97 | + use toxcore::dht::packet::*; |
| 98 | + |
| 99 | + use futures::sync::mpsc; |
| 100 | + use std::net::SocketAddr; |
| 101 | + use futures::Future; |
| 102 | + |
| 103 | + macro_rules! unpack { |
| 104 | + ($variable:expr, $variant:path) => ( |
| 105 | + match $variable { |
| 106 | + $variant(inner) => inner, |
| 107 | + other => panic!("Expected {} but got {:?}", stringify!($variant), other), |
| 108 | + } |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn daemon_state_serialize_deserialize_test() { |
| 114 | + let (pk, sk) = gen_keypair(); |
| 115 | + let (tx, rx) = mpsc::unbounded::<(DhtPacket, SocketAddr)>(); |
| 116 | + let alice = Server::new(tx, pk, sk); |
| 117 | + |
| 118 | + let addr_org = "1.2.3.4:1234".parse().unwrap(); |
| 119 | + let pk_org = gen_keypair().0; |
| 120 | + let pn = PackedNode { pk: pk_org, saddr: addr_org }; |
| 121 | + alice.close_nodes.write().try_add(&pn); |
| 122 | + |
| 123 | + let serialized_vec = DaemonState::serialize_old(&alice); |
| 124 | + DaemonState::deserialize_old(&alice, serialized_vec).wait().unwrap(); |
| 125 | + |
| 126 | + let (received, _rx) = rx.into_future().wait().unwrap(); |
| 127 | + let (packet, addr_to_send) = received.unwrap(); |
| 128 | + |
| 129 | + assert_eq!(addr_to_send, addr_org); |
| 130 | + |
| 131 | + let sending_packet = unpack!(packet, DhtPacket::NodesRequest); |
| 132 | + |
| 133 | + assert_eq!(sending_packet.pk, pk); |
| 134 | + |
| 135 | + // test with incompleted serialized data |
| 136 | + let serialized_vec = DaemonState::serialize_old(&alice); |
| 137 | + let serialized_len = serialized_vec.len(); |
| 138 | + assert!(DaemonState::deserialize_old(&alice, serialized_vec[..serialized_len - 1].to_vec()).wait().is_err()); |
| 139 | + |
| 140 | + // test with empty close list |
| 141 | + alice.close_nodes.write().remove(&pk_org); |
| 142 | + let serialized_vec = DaemonState::serialize_old(&alice); |
| 143 | + assert!(DaemonState::deserialize_old(&alice, serialized_vec).wait().is_ok()); |
| 144 | + } |
| 145 | +} |
0 commit comments