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) {
let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
let data = [0; 1400];
let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1);
b.iter(|| {
sock.send_to(&data, "127.0.0.1:1").unwrap()
sock.send_to(&data, &addr).unwrap()
});
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, 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: {}");
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];
loop {
let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}") as usize;
// Process events
for evt in events.iter().take(count) {
match evt.data {
0 => {
let (size, src) = try_fail!(self.socket4.recv_from(&mut buffer), "Failed to read from ipv4 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)
}
},
1 => {
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)
0 | 1 => {
let (size, src) = match evt.data {
0 => try_fail!(self.socket4.recv_from(&mut buffer), "Failed to read from ipv4 network socket: {}"),
1 => try_fail!(self.socket6.recv_from(&mut buffer), "Failed to read from ipv6 network socket: {}"),
_ => unreachable!()
};
if let Err(e) = decode(&mut buffer[..size], &mut self.crypto).and_then(|(options, msg)| self.handle_net_message(src, options, msg)) {
error!("Error: {}, from: {}", e, src);
}
},
2 => {
let start = 64;
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) {
Ok(_) => (),
Err(e) => error!("Error: {}", e)
if let Err(e) = self.handle_interface_data(&mut buffer, start, start+size) {
error!("Error: {}", e);
}
},
_ => unreachable!()
@ -615,9 +610,8 @@ impl<P: Protocol> GenericCloud<P> {
break;
}
// Do the housekeeping
match self.housekeep() {
Ok(_) => (),
Err(e) => error!("Error: {}", e)
if let Err(e) = self.housekeep() {
error!("Error: {}", e)
}
self.next_housekeep = now() + 1
}

View File

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