More style

This commit is contained in:
arkpar 2016-01-08 13:49:00 +01:00
parent e6623c0edd
commit 396a1755e3
4 changed files with 154 additions and 175 deletions

View File

@ -313,8 +313,6 @@ pub mod aes {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::str::FromStr;
use hash::*; use hash::*;
use crypto::*; use crypto::*;

View File

@ -1,4 +1,3 @@
#![allow(dead_code)] //TODO: remove this after everything is done
use std::collections::VecDeque; use std::collections::VecDeque;
use mio::{Token, EventSet, EventLoop, Timeout, PollOpt, TryRead, TryWrite}; use mio::{Token, EventSet, EventLoop, Timeout, PollOpt, TryRead, TryWrite};
use mio::tcp::*; use mio::tcp::*;
@ -183,7 +182,6 @@ impl EncryptedConnection {
key_material.sha3().copy_to(&mut key_material[32..64]); key_material.sha3().copy_to(&mut key_material[32..64]);
let mac_encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key_material[32..64]), NoPadding); let mac_encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key_material[32..64]), NoPadding);
println!("SESSION key: {}", H256::from_slice(&key_material[32..64]).hex());
let mut egress_mac = Keccak::new_keccak256(); let mut egress_mac = Keccak::new_keccak256();
let mut mac_material = &H256::from_slice(&key_material[32..64]) ^ &handshake.remote_nonce; let mut mac_material = &H256::from_slice(&key_material[32..64]) ^ &handshake.remote_nonce;
@ -292,17 +290,10 @@ impl EncryptedConnection {
let mut prev = H128::new(); let mut prev = H128::new();
mac.clone().finalize(&mut prev); mac.clone().finalize(&mut prev);
let mut enc = H128::new(); let mut enc = H128::new();
println!("before: {}", prev.hex());
mac_encoder.encrypt(&mut RefReadBuffer::new(&prev), &mut RefWriteBuffer::new(&mut enc), true).unwrap(); mac_encoder.encrypt(&mut RefReadBuffer::new(&prev), &mut RefWriteBuffer::new(&mut enc), true).unwrap();
mac_encoder.reset(); mac_encoder.reset();
println!("after {}", enc.hex());
if !seed.is_empty() { enc = enc ^ if seed.is_empty() { prev } else { H128::from_slice(seed) };
enc = enc ^ H128::from_slice(seed);
}
else {
enc = enc ^ prev;
}
mac.update(&enc); mac.update(&enc);
} }
@ -349,7 +340,7 @@ impl EncryptedConnection {
} }
#[test] #[test]
pub fn ctest() { pub fn test_encryption() {
use hash::*; use hash::*;
use std::str::FromStr; use std::str::FromStr;
let key = H256::from_str("2212767d793a7a3d66f869ae324dd11bd17044b82c9f463b8a541a4d089efec5").unwrap(); let key = H256::from_str("2212767d793a7a3d66f869ae324dd11bd17044b82c9f463b8a541a4d089efec5").unwrap();
@ -363,7 +354,6 @@ pub fn ctest() {
let mut encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key), NoPadding); let mut encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key), NoPadding);
encoder.encrypt(&mut RefReadBuffer::new(&before), &mut RefWriteBuffer::new(&mut got), true).unwrap(); encoder.encrypt(&mut RefReadBuffer::new(&before), &mut RefWriteBuffer::new(&mut got), true).unwrap();
encoder.reset(); encoder.reset();
println!("got: {} ", got.hex());
assert_eq!(got, after); assert_eq!(got, after);
got = H128::new(); got = H128::new();
encoder.encrypt(&mut RefReadBuffer::new(&before2), &mut RefWriteBuffer::new(&mut got), true).unwrap(); encoder.encrypt(&mut RefReadBuffer::new(&before2), &mut RefWriteBuffer::new(&mut got), true).unwrap();

View File

@ -30,8 +30,8 @@ pub struct Handshake {
pub ack_cipher: Bytes pub ack_cipher: Bytes
} }
const AUTH_PACKET_SIZE:usize = 307; const AUTH_PACKET_SIZE: usize = 307;
const ACK_PACKET_SIZE:usize = 210; const ACK_PACKET_SIZE: usize = 210;
impl Handshake { impl Handshake {
pub fn new(token: Token, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result<Handshake, Error> { pub fn new(token: Token, id: &NodeId, socket: TcpStream, nonce: &H256) -> Result<Handshake, Error> {

View File

@ -1,4 +1,3 @@
//TODO: remove all unwraps
use std::net::{SocketAddr, ToSocketAddrs}; use std::net::{SocketAddr, ToSocketAddrs};
use std::collections::{HashMap}; use std::collections::{HashMap};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -19,7 +18,7 @@ const _DEFAULT_PORT: u16 = 30304;
const MAX_CONNECTIONS: usize = 1024; const MAX_CONNECTIONS: usize = 1024;
const MAX_USER_TIMERS: usize = 32; const MAX_USER_TIMERS: usize = 32;
const IDEAL_PEERS:u32 = 10; const IDEAL_PEERS: u32 = 10;
pub type NodeId = H512; pub type NodeId = H512;
pub type TimerToken = usize; pub type TimerToken = usize;
@ -28,8 +27,8 @@ pub type TimerToken = usize;
struct NetworkConfiguration { struct NetworkConfiguration {
listen_address: SocketAddr, listen_address: SocketAddr,
public_address: SocketAddr, public_address: SocketAddr,
no_nat: bool, nat_enabled: bool,
no_discovery: bool, discovery_enabled: bool,
pin: bool, pin: bool,
} }
@ -38,8 +37,8 @@ impl NetworkConfiguration {
NetworkConfiguration { NetworkConfiguration {
listen_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(), listen_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(),
public_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(), public_address: SocketAddr::from_str("0.0.0.0:30304").unwrap(),
no_nat: false, nat_enabled: true,
no_discovery: false, discovery_enabled: true,
pin: false, pin: false,
} }
} }
@ -54,7 +53,6 @@ pub struct NodeEndpoint {
impl NodeEndpoint { impl NodeEndpoint {
fn from_str(s: &str) -> Result<NodeEndpoint, Error> { fn from_str(s: &str) -> Result<NodeEndpoint, Error> {
println!("{:?}", s);
let address = s.to_socket_addrs().map(|mut i| i.next()); let address = s.to_socket_addrs().map(|mut i| i.next());
match address { match address {
Ok(Some(a)) => Ok(NodeEndpoint { Ok(Some(a)) => Ok(NodeEndpoint {
@ -307,10 +305,6 @@ impl Host {
let listener = TcpListener::bind(&addr).unwrap(); let listener = TcpListener::bind(&addr).unwrap();
// Start listening for incoming connections // Start listening for incoming connections
event_loop.register(&listener, Token(TCP_ACCEPT), EventSet::readable(), PollOpt::edge()).unwrap(); event_loop.register(&listener, Token(TCP_ACCEPT), EventSet::readable(), PollOpt::edge()).unwrap();
// Setup the client socket
//let sock = TcpStream::connect(&addr).unwrap();
// Register the socket
//self.event_loop.register(&sock, CLIENT, EventSet::readable(), PollOpt::edge()).unwrap();
let idle_timeout = event_loop.timeout_ms(Token(IDLE), 1000).unwrap(); //TODO: check delay let idle_timeout = event_loop.timeout_ms(Token(IDLE), 1000).unwrap(); //TODO: check delay
// open the udp socket // open the udp socket
let udp_socket = UdpSocket::bound(&addr).unwrap(); let udp_socket = UdpSocket::bound(&addr).unwrap();
@ -326,7 +320,6 @@ impl Host {
protocol_version: 4, protocol_version: 4,
client_version: "parity".to_string(), client_version: "parity".to_string(),
listen_port: port, listen_port: port,
//capabilities: vec![ CapabilityInfo { protocol: "eth".to_string(), version: 63 }],
capabilities: Vec::new(), capabilities: Vec::new(),
}, },
_udp_socket: udp_socket, _udp_socket: udp_socket,
@ -434,8 +427,6 @@ impl Host {
let node = self.nodes.get_mut(id).unwrap(); let node = self.nodes.get_mut(id).unwrap();
node.last_attempted = Some(::time::now()); node.last_attempted = Some(::time::now());
//blog(NetConnect) << "Attempting connection to node" << _p->id << "@" << ep << "from" << id();
match TcpStream::connect(&node.endpoint.address) { match TcpStream::connect(&node.endpoint.address) {
Ok(socket) => socket, Ok(socket) => socket,
Err(_) => { Err(_) => {
@ -584,9 +575,9 @@ impl Host {
fn kill_connection(&mut self, token: Token, _event_loop: &mut EventLoop<Host>) { fn kill_connection(&mut self, token: Token, _event_loop: &mut EventLoop<Host>) {
self.connections.remove(token); self.connections.remove(token);
} }
} }
impl Handler for Host { impl Handler for Host {
type Timeout = Token; type Timeout = Token;
type Message = HostMessage; type Message = HostMessage;
@ -670,4 +661,4 @@ impl Handler for Host {
} }
} }
} }
} }