From b4cc4d402ad1591e2ef5daefc69c31749f7d14c8 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Wed, 25 Nov 2015 14:31:05 +0100 Subject: [PATCH] Sending close message at the end --- Cargo.toml | 2 ++ README.md | 2 -- src/cloud.rs | 11 +++++++++++ src/main.rs | 3 ++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf3761b..ccebda8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ docopt = "0.6" rustc-serialize = "0.3" log = "0.3" epoll = "0.2" +signal = "0.1" +nix = "*" libsodium-sys = {version = "0.0.9", optional = true} [build-dependencies] diff --git a/README.md b/README.md index cee3662..0c3d32e 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,7 @@ However there are some open issues: * Encryption has not been thoroughly reviewed, use with care. * The protocol can still change. -* Running on IPv6 is not supported. * The software is not very well tested. -* The closing message is not sent to peers. * The coverage score includes all unused methods from *libsodium* Please feel free to help and contribute code. diff --git a/src/cloud.rs b/src/cloud.rs index 2df7d94..6cead6d 100644 --- a/src/cloud.rs +++ b/src/cloud.rs @@ -9,6 +9,8 @@ use std::marker::PhantomData; use time::{Duration, SteadyTime, precise_time_ns}; use epoll; +use nix::sys::signal::{SIGTERM, SIGQUIT, SIGINT}; +use signal::trap::Trap; use super::types::{Table, Protocol, Range, Error, NetworkId}; use super::device::Device; @@ -270,6 +272,7 @@ impl GenericCloud

{ } pub fn run(&mut self) { + let trap = Trap::trap(&[SIGINT, SIGTERM, SIGQUIT]); let epoll_handle = try_fail!(epoll::create1(0), "Failed to create epoll handle: {}"); let socket_fd = self.socket.as_raw_fd(); let device_fd = self.device.as_raw_fd(); @@ -281,6 +284,10 @@ impl GenericCloud

{ let mut buffer = [0; 64*1024]; loop { let count = try_fail!(epoll::wait(epoll_handle, &mut events, 1000), "Epoll wait failed: {}"); + // Check for signals + if trap.wait(SteadyTime::now()).is_some() { + break; + } // Process events for i in 0..count { match &events[i as usize].data { @@ -310,5 +317,9 @@ impl GenericCloud

{ self.next_housekeep = SteadyTime::now() + Duration::seconds(1) } } + info!("Shutting down..."); + for p in &self.peers.as_vec() { + let _ = self.send_msg(p, &Message::Close); + } } } diff --git a/src/main.rs b/src/main.rs index 2de9976..7e22a1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate time; extern crate docopt; extern crate rustc_serialize; extern crate epoll; +extern crate signal; +extern crate nix; #[cfg(feature = "crypto")] extern crate libsodium_sys; #[macro_use] mod util; @@ -29,7 +31,6 @@ use cloud::GenericCloud; use udpmessage::VERSION; use crypto::Crypto; -//TODO: Call close struct SimpleLogger;