Code cleanup

This commit is contained in:
Dennis Schwerdel 2016-06-29 08:43:39 +02:00
parent 72b8eccfa9
commit a698ad95a3
3 changed files with 21 additions and 20 deletions

View File

@ -185,8 +185,9 @@ fn handle_net_message(b: &mut Bencher) {
fn udp_send(b: &mut Bencher) { fn udp_send(b: &mut Bencher) {
let sock = UdpSocket::bind("127.0.0.1:0").unwrap(); let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
let data = [0; 1400]; let data = [0; 1400];
let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1);
b.iter(|| { b.iter(|| {
sock.send_to(&data, "127.0.0.1:1").unwrap() sock.send_to(&data, &addr).unwrap()
}); });
b.bytes = 1400; b.bytes = 1400;
} }

View File

@ -577,33 +577,28 @@ impl<P: Protocol> GenericCloud<P> {
try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, socket4_fd, &mut socket4_event), "Failed to add ipv4 socket to epoll handle: {}"); try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, socket4_fd, &mut socket4_event), "Failed to add ipv4 socket to epoll handle: {}");
try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, socket6_fd, &mut socket6_event), "Failed to add ipv6 socket to epoll handle: {}"); try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, socket6_fd, &mut socket6_event), "Failed to add ipv6 socket to epoll handle: {}");
try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, device_fd, &mut device_event), "Failed to add device to epoll handle: {}"); try_fail!(epoll::ctl(epoll_handle, epoll::util::ctl_op::ADD, device_fd, &mut device_event), "Failed to add device to epoll handle: {}");
let mut events = [epoll::EpollEvent{events: 0, data: 0}; 2]; let mut events = [epoll::EpollEvent{events: 0, data: 0}; 3];
let mut buffer = [0; 64*1024]; let mut buffer = [0; 64*1024];
loop { loop {
let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}") as usize; let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}") as usize;
// Process events // Process events
for evt in events.iter().take(count) { for evt in events.iter().take(count) {
match evt.data { match evt.data {
0 => { 0 | 1 => {
let (size, src) = try_fail!(self.socket4.recv_from(&mut buffer), "Failed to read from ipv4 network socket: {}"); let (size, src) = match evt.data {
match decode(&mut buffer[..size], &mut self.crypto).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) { 0 => try_fail!(self.socket4.recv_from(&mut buffer), "Failed to read from ipv4 network socket: {}"),
Ok(_) => (), 1 => try_fail!(self.socket6.recv_from(&mut buffer), "Failed to read from ipv6 network socket: {}"),
Err(e) => error!("Error: {}, from: {}", e, src) _ => unreachable!()
} };
}, if let Err(e) = decode(&mut buffer[..size], &mut self.crypto).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) {
1 => { error!("Error: {}, from: {}", e, src);
let (size, src) = try_fail!(self.socket6.recv_from(&mut buffer), "Failed to read from ipv6 network socket: {}");
match decode(&mut buffer[..size], &mut self.crypto).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) {
Ok(_) => (),
Err(e) => error!("Error: {}, from: {}", e, src)
} }
}, },
2 => { 2 => {
let start = 64; let start = 64;
let size = try_fail!(self.device.read(&mut buffer[start..]), "Failed to read from tap device: {}"); let size = try_fail!(self.device.read(&mut buffer[start..]), "Failed to read from tap device: {}");
match self.handle_interface_data(&mut buffer, start, start+size) { if let Err(e) = self.handle_interface_data(&mut buffer, start, start+size) {
Ok(_) => (), error!("Error: {}", e);
Err(e) => error!("Error: {}", e)
} }
}, },
_ => unreachable!() _ => unreachable!()
@ -615,9 +610,8 @@ impl<P: Protocol> GenericCloud<P> {
break; break;
} }
// Do the housekeeping // Do the housekeeping
match self.housekeep() { if let Err(e) = self.housekeep() {
Ok(_) => (), error!("Error: {}", e)
Err(e) => error!("Error: {}", e)
} }
self.next_housekeep = now() + 1 self.next_housekeep = now() + 1
} }

View File

@ -149,6 +149,7 @@ fn inc_nonce_12(nonce: &mut [u8; 12]) {
impl Crypto { impl Crypto {
#[inline]
pub fn init() { pub fn init() {
CRYPTO_INIT.call_once(|| { CRYPTO_INIT.call_once(|| {
if unsafe { sodium_init() } != 0 { if unsafe { sodium_init() } != 0 {
@ -157,18 +158,21 @@ impl Crypto {
}); });
} }
#[inline]
pub fn sodium_version() -> String { pub fn sodium_version() -> String {
unsafe { unsafe {
CStr::from_ptr(sodium_version_string()).to_string_lossy().to_string() CStr::from_ptr(sodium_version_string()).to_string_lossy().to_string()
} }
} }
#[inline]
pub fn aes256_available() -> bool { pub fn aes256_available() -> bool {
unsafe { unsafe {
crypto_aead_aes256gcm_is_available() == 1 crypto_aead_aes256gcm_is_available() == 1
} }
} }
#[inline]
pub fn method(&self) -> u8 { pub fn method(&self) -> u8 {
match *self { match *self {
Crypto::None => 0, Crypto::None => 0,
@ -177,6 +181,7 @@ impl Crypto {
} }
} }
#[inline]
pub fn nonce_bytes(&self) -> usize { pub fn nonce_bytes(&self) -> usize {
match *self { match *self {
Crypto::None => 0, Crypto::None => 0,
@ -184,6 +189,7 @@ impl Crypto {
} }
} }
#[inline]
#[allow(unknown_lints)] #[allow(unknown_lints)]
#[allow(match_same_arms)] #[allow(match_same_arms)]
pub fn additional_bytes(&self) -> usize { pub fn additional_bytes(&self) -> usize {